NixOS Nvidia Laptop
Dec 16, 2023
Author: Kinzoku
Add this to your NixOS configuration (by default located at /etc/nixos/configuration.nix)
services.xserver.videoDrivers = [ "nvidia" ];
hardware.nvidia = {
modesetting.enable = true;
prime = {
offload.enable = true;
# Bus ID of the Intel GPU. You can find it using lspci, either under 3D or VGA
intelBusId = "PCI:0:2:0";
# Bus ID of the NVIDIA GPU. You can find it using lspci, either under 3D or VGA
nvidiaBusId = "PCI:1:0:0";
};
};
Let’s walk through what this does
services.xserver.videoDrivers = [ "nvidia" ];
This will add the nvidia drivers to your X server configuration, allowing the nvidia GPU to be used properly for rendering
Next let’s take a look in the hardware.nvidia block
First of all, remember that
foo = {
bar = true;
};
is the same as
foo.bar = true;
This is how Nix works. Nix language tutorial coming soon :)
With this in mind, the next few code blocks will be in the scope of the hardware.nvidia block
modesetting.enable = true;
This will enable kernel modesetting, which fixes some screen tearing issues
prime.offload.enable = true;
This will enable offload support via PRIME. This makes it so all rendering processes will use your CPU’s integrated graphics by default, and will only use your dedicated GPU when explicitly told to
The intelBusId and nvidiaBusId options are just the bus IDs for your intel CPU and nvidia GPU. These values may be different for you, so be sure to use the lspci command to check. They will most likely both be displayed under “VGA compatible controller”
Finally, modify your environment.systemPackages block to include the text wrapped in parentheses
environment.systemPackages = [
# other shiz
(pkgs.writeShellScriptBin "prime-run" ''
export __NV_PRIME_RENDER_OFFLOAD=1
export __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0
export __GLX_VENDOR_LIBRARY_NAME=nvidia
export __VK_LAYER_NV_optimus=NVIDIA_only
exec -a "$0" "$@"
'')
];
Now you can choose to offload any program by prepending the command with prime-run. This will tell the program to render with your dedicated GPU rather than using your CPU’s integrated graphics, leading to better performance and quality