Jump to content

Nvidia: Difference between revisions

2,468 bytes added ,  Tuesday at 20:25
m
fixed link to „Unfree software“ (capitalization)
imported>Jasper-at-windswept
(Update to match pull request on Nixpkgs #290938 and reference help for obtaining driver version 535)
m (fixed link to „Unfree software“ (capitalization))
 
(23 intermediate revisions by 9 users not shown)
Line 1: Line 1:
= NixOS Manual =
There are several kernel drivers that allow Nvidia GPUs to work:
First check out the [https://nixos.org/manual/nixos/stable/#sec-x11-graphics-cards-nvidia method] in the NixOS Manual.


= Installing Nvidia Drivers on NixOS =
* Nouveau is the stable 3rd-party open-source driver. You probably have it by default.
* Nvidia's proprietary kernel module. Better 3D performance than nouveau.
* Nvidia's open-source kernel module, which is beta-quality and x11-only.


NixOS uses a functional package management approach, which necessitates specific procedures for driver installation. When considering NVIDIA GPU drivers in a Linux environment, the installation process can be more complex compared to AMD and Intel. This complexity arises primarily because NVIDIA's official drivers are closed source and not typically bundled with many distributions. This document outlines the technical steps required to install NVIDIA GPU drivers on NixOS, factoring in both the unique nature of NixOS and the proprietary status of NVIDIA's drivers.
This article aims to explain how to use all three of the non-default drivers, and how to disable the Nvidia GPU if you don't want it to make heat.


== Enable Unfree Software Repositories ==
(A GPU driver is not ''purely'' kernel-mode; there are also userspace parts that allow the graphics API to use the kernel part. Mesa, for example, has userspace parts for Nouveau and NVK; they are automatically used when appropriate, so no configuration is needed.<ref>If you insist on checking which driver is in use, try running <code>vulkaninfo | grep -i "deviceName\|driverID"</code>. If you see DRIVER_ID_MESA_NVK, that means you are on the shiny new open-source Vulkan driver.</ref> Nvidia's two kernel drivers also need to be hooked up using userspace libraries provided by Nvidia.)


Make sure to allow Unfree Software. The unfree NVIDIA packages include <code>nvidia-x11</code>, <code>nvidia-settings</code>, and <code>nvidia-persistenced</code>.
= The proprietary driver =


== Determining the Correct Driver Version ==
NixOS uses a functional package management approach &ndash; this section, like many others, will tell you how to change your <code>configuration.nix</code> to define what kind of environment you want on your system.


You will next need to determine the appropriate driver version for your card. The following options are available:
For the impatient:
* [https://nixos.org/manual/nixos/unstable/#sec-x11-graphics-cards-nvidia Nvidia section] in the NixOS Manual offers a shorter guide.
* Basic installation is as simple as <code>services.xserver.videoDrivers = [ "nvidia" ];</code> (once you have unfree software enabled), which causes the default stable version to be pulled in.
* Older cards may need an older driver; see [[#Determining the correct driver version|Determining the correct driver version]], which describes <code>hardware.nvidia.package</code>.
* [[Nvidia#Modifying NixOS configuration|Modifying the NixOS Configuration]] puts it all together and shows the other <code>hardware.nvidia</code> settings, some irrelevant to this driver (e.g. <code>hardware.nvidia.open</code>, which takes you to the Nvidia open kernel driver.)


<syntaxhighlight lang="nix">
== Enable unfree software repositories ==  
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.beta;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.production;  # (installs 550)
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.vulkan_beta;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_470;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_390;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_340;
</syntaxhighlight>


Out of the above, `stable` and `beta` will work for the latest RTX cards and some lower cards so long as they're not considered "legacy" by Nvidia. For "legacy" cards, you can consult the [https://www.nvidia.com/en-us/drivers/unix/legacy-gpu/ Nvidia official legacy driver list] and check whether your device is supported by the 470, 390 or 340 branches. If so, you can use the corresponding <code>legacy_470</code>, <code>legacy_390</code> or <code>legacy_340</code> driver. For a full list of options, consult the [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/os-specific/linux/nvidia-x11/default.nix nvidia-x11 module repository].
Make sure to allow [[Unfree software|unfree software]]. The unfree NVIDIA packages include <code>nvidia-x11</code>, <code>nvidia-settings</code>, and <code>nvidia-persistenced</code>.


{{note|As of early March 2024 the <code>production</code> driver has been updated from <code>535</code> to <code>550</code>. This is a breaking change for some people, especially those on Wayland.
== Modifying NixOS configuration ==
To resolve this follow the steps under [https://nixos.wiki/wiki/Nvidia#Running_the_new_RTX_SUPER_on_nixos_stable Running the new RTX SUPER on nixos stable]}}
 
Once you've determined the correct driver version, note it down; you'll need it in the next step.
 
== Modifying NixOS Configuration ==


Ensure that the following is in your NixOS configuration file (customizing as you prefer):  
Ensure that the following is in your NixOS configuration file (customizing as you prefer):  
Line 41: Line 33:
{
{


   # Enable OpenGL
   # Enable graphics driver in NixOS unstable/NixOS 24.11
   hardware.opengl = {
   hardware.graphics.enable = true;
    enable = true;
  # The same as above but for NixOS 23.11
    driSupport = true;
  #hardware.opengl = {
    driSupport32Bit = true;
  #  enable = true;
   };
  #  driSupport = true;
   #};


   # Load nvidia driver for Xorg and Wayland
   # Load "nvidia" driver for Xorg and Wayland
   services.xserver.videoDrivers = ["nvidia"]; # or "nvidiaLegacy470 etc.
   services.xserver.videoDrivers = ["nvidia"];


   hardware.nvidia = {
   hardware.nvidia = {
Line 72: Line 65:
     # https://github.com/NVIDIA/open-gpu-kernel-modules#compatible-gpus  
     # https://github.com/NVIDIA/open-gpu-kernel-modules#compatible-gpus  
     # Only available from driver 515.43.04+
     # Only available from driver 515.43.04+
     # Currently alpha-quality/buggy, so false is currently the recommended setting.
     # Currently "beta quality", so false is currently the recommended setting.
     open = false;
     open = false;


Line 86: Line 79:
</nowiki>}}
</nowiki>}}


== Laptop Configuration: Hybrid Graphics (Nvidia Optimus PRIME) ==
=== Determining the correct driver version ===
 
You might need to determine the appropriate driver version for your card. Some of the options available are:
 
<syntaxhighlight lang="nix">
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;      # (390 on i686, else production)
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.beta;        # (555)
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.production;  # (installs 550)
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.vulkan_beta; # (550, but different)
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_470;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_390;
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_340;
</syntaxhighlight>
 
Out of the above, <code>stable</code> and <code>beta</code> will work for the latest RTX cards and some lower cards so long as they're not considered "legacy" by Nvidia. For "legacy" cards, you can consult the [https://www.nvidia.com/en-us/drivers/unix/legacy-gpu/ Nvidia official legacy driver list] and check whether your device is supported by the 470, 390 or 340 branches. If so, you can use the corresponding <code>legacy_470</code>, <code>legacy_390</code> or <code>legacy_340</code> driver. For a full list of options, consult the [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/os-specific/linux/nvidia-x11/default.nix nvidia-x11 module repository].
 
{{note|As of early March 2024 the <code>production</code> driver has been updated from <code>535</code> to <code>550</code>. This is a breaking change for some people, especially those on Wayland.
To resolve this, follow the steps under [[Nvidia#Running the new RTX SUPER on NixOS stable|Running the new RTX SUPER on NixOS stable]]}}
 
Once you've determined the correct driver version, note it down; you'll need it in the next step.


In order to correctly finish configuring your Nvidia graphics driver, you must follow the below steps, which differ depending on whether or not you are using a hybrid graphics setup or not. A laptop with hybrid graphics possesses both an integrated GPU (often from the central processor) and a discrete, more powerful Nvidia GPU, typically for performance-intensive tasks. This dual-GPU setup allows for power-saving during basic tasks and higher graphics performance when needed.
== Laptop configuration: hybrid graphics (Optimus PRIME) ==
 
In order to correctly finish configuring your Nvidia graphics driver, you must follow the below steps, which differ depending on whether or not you are using a hybrid graphics setup or not. A laptop with hybrid graphics possesses both an integrated GPU (often part of the central processor) and a discrete, more powerful Nvidia GPU, typically for performance-intensive tasks. This dual-GPU setup allows for power-saving during basic tasks and higher graphics performance when needed.


'''Nvidia Optimus PRIME''' is a technology developed by Nvidia to optimize the power consumption and performance of laptops equipped with their GPUs. It seamlessly switches between the integrated graphics, usually from Intel, for lightweight tasks to save power, and the discrete Nvidia GPU for performance-intensive tasks like gaming or video editing. By dynamically balancing graphics power and battery life, Optimus provides the best of both worlds, ensuring that users get longer battery life without sacrificing graphical performance.
'''Nvidia Optimus PRIME''' is a technology developed by Nvidia to optimize the power consumption and performance of laptops equipped with their GPUs. It seamlessly switches between the integrated graphics, usually from Intel, for lightweight tasks to save power, and the discrete Nvidia GPU for performance-intensive tasks like gaming or video editing. By dynamically balancing graphics power and battery life, Optimus provides the best of both worlds, ensuring that users get longer battery life without sacrificing graphical performance.


=== Configuring Optimus PRIME: Bus ID Values (Mandatory) ===
=== Optimus PRIME: bus ID values (mandatory) ===


Before we can continue, we must mandatorily first determine the Bus ID values for both your Nvidia and Intel GPUs. This step will be essential regardless of which configuration you later adopt.
Before we can continue, we must first determine the bus ID values for both your Nvidia and Intel and AMD GPUs. This step will be essential regardless of which configuration you later adopt.


First, install the <code>lshw</code> package in order to be able to use the <code>lshw</code> command, then run:
First, install the <code>lshw</code> package in order to be able to use the <code>lshw</code> command, then run:
Line 103: Line 117:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
*-display                
*-display
description: i915drmfb
description: i915drmfb
physical id: 0
physical id: 0
Line 136: Line 150:
intelBusId = "PCI:0:2:0";
intelBusId = "PCI:0:2:0";
nvidiaBusId = "PCI:14:0:0";
nvidiaBusId = "PCI:14:0:0";
        # amdgpuBusId = "PCI:54:0:0"; For AMD GPU
};
};
}
}
</nowiki>}}
</nowiki>}}


=== Optimus PRIME Option A: Offload Mode ===
=== Optimus PRIME modes ===


''Offload Mode is available in NixOS 20.09 and higher, and requires an Nvidia card of the Turing generation or newer, and an Intel chipset from the Coffee Lake generation or newer.''
==== Offload mode ====
 
:''Offload mode is available in NixOS 20.09 and higher, and requires an Nvidia card of the Turing generation or newer, an Intel chipset from the Coffee Lake generation or newer, or an AMD Ryzen CPU like the 5800H .''


Offload mode puts your Nvidia GPU to sleep and lets the Intel GPU handle all tasks, except if you call the Nvidia GPU specifically by "offloading" an application to it. For example, you can run your laptop normally and it will use the energy-efficient Intel GPU all day, and then you can offload a game from Steam onto the Nvidia GPU to make the Nvidia GPU run that game only. For many, this is the most desirable option.
Offload mode puts your Nvidia GPU to sleep and lets the Intel GPU handle all tasks, except if you call the Nvidia GPU specifically by "offloading" an application to it. For example, you can run your laptop normally and it will use the energy-efficient Intel GPU all day, and then you can offload a game from Steam onto the Nvidia GPU to make the Nvidia GPU run that game only. For many, this is the most desirable option.
Line 168: Line 185:
intelBusId = "PCI:0:2:0";
intelBusId = "PCI:0:2:0";
nvidiaBusId = "PCI:14:0:0";
nvidiaBusId = "PCI:14:0:0";
        # amdgpuBusId = "PCI:54:0:0"; For AMD GPU.
};
};
}
}
</nowiki>}}
</nowiki>}}


=== Optimus PRIME Option B: Sync Mode ===
==== Sync mode ====


Enabling PRIME sync introduces better performance and greatly reduces screen tearing, at the expense of higher power consumption since the Nvidia GPU will not go to sleep completely unless called for, as is the case in Offload Mode. It may also cause its own issues in rare cases. '''PRIME Sync and Offload Mode cannot be enabled at the same time.'''
Enabling PRIME sync introduces better performance and greatly reduces screen tearing, at the expense of higher power consumption since the Nvidia GPU will not go to sleep completely unless called for, as is the case in Offload Mode. It may also cause its own issues in rare cases. '''PRIME Sync and Offload Mode cannot be enabled at the same time.'''
Line 186: Line 204:
     nvidiaBusId = "PCI:14:0:0";
     nvidiaBusId = "PCI:14:0:0";
     intelBusId = "PCI:0:2:0";
     intelBusId = "PCI:0:2:0";
    # amdgpuBusId = "PCI:54:0:0"; For AMD GPU
   };
   };
}
}
</nowiki>}}
</nowiki>}}


=== Optimus Option C: Reverse Sync Mode (Experimental) ===
==== Reverse sync mode (experimental) ====


This feature is relatively new and may not work properly on all systems ([https://forums.developer.nvidia.com/t/the-all-new-outputsink-feature-aka-reverse-prime/129828 see discussion]). It is also only available on driver 460.39 or newer. Reverse sync also only works with <code>services.xserver.displayManager.setupCommands</code> compatible Display Managers (LightDM, GDM and SDDM).
This feature is relatively new and may not work properly on all systems ([https://forums.developer.nvidia.com/t/the-all-new-outputsink-feature-aka-reverse-prime/129828 see discussion]). It is only available on driver 460.39 or newer. Reverse sync only works with <code>services.xserver.displayManager.setupCommands</code> compatible Display Managers (LightDM, GDM and SDDM).


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 204: Line 223:
     intelBusId = "PCI:0:2:0";
     intelBusId = "PCI:0:2:0";
     nvidiaBusId = "PCI:14:0:0";
     nvidiaBusId = "PCI:14:0:0";
    # amdgpuBusId = "PCI:54:0:0"; For AMD GPU
   };
   };
}
}
</nowiki>}}
</nowiki>}}


== Useful Tips ==
== Useful tips ==


=== Check NixosHardware ===
=== Check nixos-hardware ===
You should check the [https://github.com/NixOS/nixos-hardware nixoshardware] GitHub repository. It is possible that someone already wrote a hardware configuration for your device and that usually takes care of drivers. If so, follow the upstream documentation to enable the required modules.   
You should check the [https://github.com/NixOS/nixos-hardware nixos-hardware] GitHub repository. It is possible that someone already wrote a hardware configuration for your device and that usually takes care of drivers. If so, follow the upstream documentation to enable the required modules.   


=== Multiple Boot Configurations ===
=== Multiple boot configurations ===
Imagine you have a laptop that you mostly use in clamshell mode (docked, connected to an external display and plugged into a charger) but that you sometimes use on the go.
Imagine you have a laptop that you mostly use in clamshell mode (docked, connected to an external display and plugged into a charger) but that you sometimes use on the go.


Line 249: Line 269:
See the [[CUDA]] wiki page.
See the [[CUDA]] wiki page.


=== Using Steam in Offload Mode ===
=== Using Steam in offload mode ===
In order to automatically launch Steam in offload mode, you need to add the following to your <code>~/.bashrc</code>:
In order to automatically launch Steam in offload mode, you need to add the following to your <code>~/.bashrc</code>:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 271: Line 291:
Then restart your graphical environment session (or simply reboot).
Then restart your graphical environment session (or simply reboot).


=== Running the new RTX SUPER on nixos stable ===
=== Running the new RTX SUPER on NixOS stable ===


The new RTX Super are not supported by the 545 driver. On Nixos stable, you want to use the 535 driver that come from unstable branch or the 550 (beta).
The new RTX Super cards are not supported by the 545 driver. On NixOS stable, you want to use the 535 driver that come from unstable branch or the 550 (beta).
To do that you need to manually call the driver you want in your config.
To do that you need to manually call the driver you want in your config.
Check on this link to choose the driver you want and change your config accordingly :
Check on this link to choose the driver you want and change your config accordingly :
Line 282: Line 302:


{
{
  # Enable graphics driver in NixOS unstable/NixOS 24.11
  hardware.graphics.enable = true;
  # The same as above but for NixOS 23.11
  #hardware.opengl = {
  #  enable = true;
  #  driSupport = true;
  #};


# Enable OpenGL
   # Load nvidia driver for Xorg and Wayland
   hardware.opengl = {
    enable = true;
    driSupport = true;
    driSupport32Bit = true;
  };
 
# Load nvidia driver for Xorg and Wayland
   services.xserver.videoDrivers = ["nvidia"];
   services.xserver.videoDrivers = ["nvidia"];
    
    
Line 326: Line 346:
== Troubleshooting ==
== Troubleshooting ==


=== Booting to Text Mode ===
=== Booting to text mode ===
If you encounter the problem of booting to text mode you might try adding the Nvidia kernel module manually with:
If you encounter the problem of booting to text mode you might try adding the Nvidia kernel module manually with:


Line 334: Line 354:
</syntaxHighlight>
</syntaxHighlight>


=== Screen Tearing Issues ===
=== Screen tearing issues ===


First, try to switch to PRIME Sync Mode, as described above. If that doesn't work, try forcing a composition pipeline.
First, try to switch to PRIME sync mode, as described above. If that doesn't work, try forcing a composition pipeline.


{{note|Forcing a full composition pipeline has been reported to reduce the performance of some OpenGL applications and may produce issues in WebGL. It also drastically increases the time the driver needs to clock down after load.}}
{{note|Forcing a full composition pipeline has been reported to reduce the performance of some OpenGL applications and may produce issues in WebGL. It also drastically increases the time the driver needs to clock down after load.}}
Line 344: Line 364:
</nowiki>}}
</nowiki>}}


=== Flickering Issues with Picom  ===
=== Flickering with Picom  ===
{{file|~/.config/picom/picom.conf|conf|<nowiki>
{{file|~/.config/picom/picom.conf|conf|<nowiki>
unredir-if-possible = false;
unredir-if-possible = false;
Line 351: Line 371:
</nowiki>}}
</nowiki>}}


=== Graphical Corruption and System Crashes on Suspend/Resume ===
=== Graphical corruption and system crashes on suspend/resume ===
<code>powerManagement.enable = true</code> can sometimes fix this, but is itself unstable and is known to cause suspend issues.
<code>powerManagement.enable = true</code> can sometimes fix this, but is itself unstable and is known to cause suspend issues.


If you have a modern Nvidia GPU (Turing [https://en.wikipedia.org/wiki/Turing_(microarchitecture)#Products_using_Turing] or later), you may also want to investigate the <code>hardware.nvidia.powerManagement.finegrained</code> option: [https://download.nvidia.com/XFree86/Linux-x86_64/460.73.01/README/dynamicpowermanagement.html]
If you have a modern Nvidia GPU (Turing [https://en.wikipedia.org/wiki/Turing_(microarchitecture)#Products_using_Turing] or later), you may also want to investigate the <code>hardware.nvidia.powerManagement.finegrained</code> option: [https://download.nvidia.com/XFree86/Linux-x86_64/460.73.01/README/dynamicpowermanagement.html]


=== Black Screen or Nothing Works on Laptops ===
=== Black screen or 'nothing works' on laptops ===
The kernel module <code>i915</code> for intel or <code>amdgpu</code> for AMD may interfere with the Nvidia driver. This may result in a black screen when switching to the virtual terminal, or when exiting the X session. A possible workaround is to disable the integrated GPU by blacklisting the module, using the following configuration option (see also [https://discourse.nixos.org/t/nvidia-gpu-and-i915-kernel-module/21307/3]):
The kernel module<code>i915</code>for Intel or<code>amdgpu</code>for AMD may interfere with the Nvidia driver. This may result in a black screen when switching to the virtual terminal, or when exiting the X session. A possible workaround is to disable the integrated GPU by blacklisting the module, using the following configuration option (see also [https://discourse.nixos.org/t/nvidia-gpu-and-i915-kernel-module/21307/3]):


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
Line 365: Line 385:
boot.kernelParams = [ "module_blacklist=amdgpu" ];
boot.kernelParams = [ "module_blacklist=amdgpu" ];
</syntaxHighlight>
</syntaxHighlight>
= The Nvidia open driver =
The Nvidia open driver is enabled via:
<syntaxHighlight lang="nix">
services.xserver.videoDrivers = [ "nvidia" ];
hardware.nvidia.open = true;
</syntaxHighlight>
The Nvidia open driver is ''very'' similar to its proprietary sibling. Most content in the proprietary section above apply; just copy over the proprietary config above and toggle the "open" option, and you are set.
Like its proprietary sibling, it also requires some non-free userspace packages that allow software to talk to the kernel driver. As a result, you still need to enable unfree packages.
Be warned that the Nvidia open driver is considered "beta quality". Performance may be very slightly lower.


= Disable Nvidia dGPU completely =
= Disable Nvidia dGPU completely =


completely disable dGPU, saving battery. Probably not all configurations and module blacklists are required but this worked successfully
It's possible to completely disable the dGPU, saving battery. Probably not all configurations and module blacklists are required but this worked successfully:


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
trusted
596

edits