NixOS Config Switching
Published: 2020-12-05 01:29:22

---
title: NixOS Config Switching
published: true
date: 2020-12-04 22:05:00 UTC
tags: 
canonical_url: https://kevincox.ca/2020/12/04/nixos-config-switching/
---

<main><p>After using NixOS for about 3 months I found another benefit of the declarative and unified configuration. My laptop has a multi-GPU setup with Intel+NVIDIA. However the NVIDIA drivers don’t support multi-GPU under Wayland. Wayland is significantly better for my day-to-day use so I stick to the Intel drivers. However when playing games, using the NVIDIA GPU with proprietary drivers provides a significant performance advantage. In order to accommodate these two use cases I find myself switching between the two GPUs. (until my new desktop with an AMD card arrives <a href="https://github.com/swaywm/sway/blob/b5cc11b2268820fd49151a5a868c5fff285a652c/sway/main.c#L99-L103"><code>--my-next-gpu-wont-be-nvidia</code></a>)</p>

<p>On <a href="https://www.archlinux.org/">Arch</a> I would solve this by removing and recreating the <code>nouveau</code> modprobe blacklist and rebooting. This was enough to get the NVIDIA drivers running and I could enjoy my games. This wasn’t too bad to manage as it was only one file but it basically relied on two commands in my shell history. It would also become very annoying if I needed to change more than one thing (for example if I also needed to tweak my X config).</p>

<p>On NixOS I can support both using the same config and mostly declaratively. Here is a simplified version of my config.</p>

<div><pre><code><span>{</span><span>...</span><span>}:</span> <span>let</span>
	<span>nvidia</span> <span>=</span> <span>builtins</span><span>.</span><span>getEnv</span> <span>"NVIDIA"</span> <span>!=</span> <span>""</span><span>;</span>
<span>in</span> <span>{</span>
	<span>services</span><span>.</span><span>xserver</span> <span>=</span> <span>{</span>
		<span>enable</span> <span>=</span> <span>true</span><span>;</span>
		<span>displayManager</span> <span>=</span> <span>{</span>
			<span>gdm</span><span>.</span><span>enable</span> <span>=</span> <span>!</span><span>nvidia</span><span>;</span>
			<span>lightdm</span><span>.</span><span>enable</span> <span>=</span> <span>nvidia</span><span>;</span>
		<span>};</span>

		<span>videoDrivers</span> <span>=</span> <span>if</span> <span>nvidia</span> <span>then</span> <span>[</span> <span>"nvidia"</span> <span>]</span> <span>else</span> <span>[];</span>
	<span>};</span>

	<span>hardware</span><span>.</span><span>nvidia</span><span>.</span><span>modesetting</span><span>.</span><span>enable</span> <span>=</span> <span>true</span><span>;</span>
	<span>hardware</span><span>.</span><span>nvidia</span><span>.</span><span>prime</span> <span>=</span> <span>{</span>
		<span>sync</span><span>.</span><span>enable</span> <span>=</span> <span>true</span><span>;</span>

		<span># Bus IDs are the first three numbers from `lspci`.</span>
		<span>intelBusId</span> <span>=</span> <span>"PCI:0:2:0"</span><span>;</span>
		<span>nvidiaBusId</span> <span>=</span> <span>"PCI:1:0:0"</span><span>;</span>
	<span>};</span>
<span>}</span>
</code></pre></div>
<p>The main thing this this config does is enables the NVIDIA drivers based on the environment variable <code>NVIDIA</code>. However due to <a href="https://github.com/NixOS/nixpkgs/issues/90152#issuecomment-735740865">a bug</a>, NVIDIA drivers aren’t working on <a href="https://en.wikipedia.org/wiki/GNOME_Display_Manager">GDM</a> at the moment. In order to sidestep this issue I swap out GDM for <a href="https://github.com/canonical/lightdm">lightdm</a> when using the NVIDIA drivers. I might consider adding more customization in the future, such as disabling unnecessary services if it makes a noticeable performance difference.</p>

<p>Now I can simply enter “gaming mode” by running <code>sudo env NVIDIA=1 nixos-rebuild boot &amp;&amp; reboot</code>. Then I can go back by rebuilding my system normally.</p>

<p>Another option is to build the “gaming” profile separately like <code>sudo env NVIDIA=1 nixos-rebuild boot -p nvidia</code>. Then I can just select that profile from the boot menu. This has the advantage that is is easier to update the profiles separately. For example I could keep using the gaming profile from before the GDM issue appeared and still regularly update the main profile.</p>

<p>This is quite a simple change, but I can definitely imagine use cases for a gaming mode, streaming mode, work mode and personal mode. Making it this easy to switch between arbitrarily different configurations opens up a lot of posibilities that were unimaginable with the configuration methods of more traditional distributions.</p>