2023-08-22 20:05:09 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-06-05 15:53:02 +00:00
|
|
|
}:
|
|
|
|
let
|
2023-10-09 19:29:22 +00:00
|
|
|
nvidiaEnabled = (lib.elem "nvidia" config.services.xserver.videoDrivers);
|
2024-06-05 15:53:02 +00:00
|
|
|
nvidia_x11 = if nvidiaEnabled || cfg.datacenter.enable then cfg.package else null;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfg = config.hardware.nvidia;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
pCfg = cfg.prime;
|
|
|
|
syncCfg = pCfg.sync;
|
|
|
|
offloadCfg = pCfg.offload;
|
2023-02-09 11:40:11 +00:00
|
|
|
reverseSyncCfg = pCfg.reverseSync;
|
|
|
|
primeEnabled = syncCfg.enable || reverseSyncCfg.enable || offloadCfg.enable;
|
2023-08-22 20:05:09 +00:00
|
|
|
busIDType = lib.types.strMatching "([[:print:]]+[\:\@][0-9]{1,3}\:[0-9]{1,2}\:[0-9])?";
|
2023-04-12 12:48:02 +00:00
|
|
|
ibtSupport = cfg.open || (nvidia_x11.ibtSupport or false);
|
2024-06-05 15:53:02 +00:00
|
|
|
settingsFormat = pkgs.formats.keyValue { };
|
|
|
|
in
|
|
|
|
{
|
2020-04-24 23:36:52 +00:00
|
|
|
options = {
|
2023-08-22 20:05:09 +00:00
|
|
|
hardware.nvidia = {
|
2024-04-21 15:54:59 +00:00
|
|
|
datacenter.enable = lib.mkEnableOption ''
|
2023-11-16 04:20:00 +00:00
|
|
|
Data Center drivers for NVIDIA cards on a NVLink topology
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-10-09 19:29:22 +00:00
|
|
|
datacenter.settings = lib.mkOption {
|
|
|
|
type = settingsFormat.type;
|
|
|
|
default = {
|
2024-06-05 15:53:02 +00:00
|
|
|
LOG_LEVEL = 4;
|
|
|
|
LOG_FILE_NAME = "/var/log/fabricmanager.log";
|
|
|
|
LOG_APPEND_TO_LOG = 1;
|
|
|
|
LOG_FILE_MAX_SIZE = 1024;
|
|
|
|
LOG_USE_SYSLOG = 0;
|
|
|
|
DAEMONIZE = 1;
|
|
|
|
BIND_INTERFACE_IP = "127.0.0.1";
|
|
|
|
STARTING_TCP_PORT = 16000;
|
|
|
|
FABRIC_MODE = 0;
|
|
|
|
FABRIC_MODE_RESTART = 0;
|
|
|
|
STATE_FILE_NAME = "/var/tmp/fabricmanager.state";
|
|
|
|
FM_CMD_BIND_INTERFACE = "127.0.0.1";
|
|
|
|
FM_CMD_PORT_NUMBER = 6666;
|
|
|
|
FM_STAY_RESIDENT_ON_FAILURES = 0;
|
|
|
|
ACCESS_LINK_FAILURE_MODE = 0;
|
|
|
|
TRUNK_LINK_FAILURE_MODE = 0;
|
|
|
|
NVSWITCH_FAILURE_MODE = 0;
|
|
|
|
ABORT_CUDA_JOBS_ON_FM_EXIT = 1;
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
|
|
|
defaultText = lib.literalExpression ''
|
2024-06-05 15:53:02 +00:00
|
|
|
{
|
|
|
|
LOG_LEVEL=4;
|
|
|
|
LOG_FILE_NAME="/var/log/fabricmanager.log";
|
|
|
|
LOG_APPEND_TO_LOG=1;
|
|
|
|
LOG_FILE_MAX_SIZE=1024;
|
|
|
|
LOG_USE_SYSLOG=0;
|
|
|
|
DAEMONIZE=1;
|
|
|
|
BIND_INTERFACE_IP="127.0.0.1";
|
|
|
|
STARTING_TCP_PORT=16000;
|
|
|
|
FABRIC_MODE=0;
|
|
|
|
FABRIC_MODE_RESTART=0;
|
|
|
|
STATE_FILE_NAME="/var/tmp/fabricmanager.state";
|
|
|
|
FM_CMD_BIND_INTERFACE="127.0.0.1";
|
|
|
|
FM_CMD_PORT_NUMBER=6666;
|
|
|
|
FM_STAY_RESIDENT_ON_FAILURES=0;
|
|
|
|
ACCESS_LINK_FAILURE_MODE=0;
|
|
|
|
TRUNK_LINK_FAILURE_MODE=0;
|
|
|
|
NVSWITCH_FAILURE_MODE=0;
|
|
|
|
ABORT_CUDA_JOBS_ON_FM_EXIT=1;
|
|
|
|
}
|
2023-10-09 19:29:22 +00:00
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
Additional configuration options for fabricmanager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
powerManagement.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
experimental power management through systemd. For more information, see
|
2023-11-16 04:20:00 +00:00
|
|
|
the NVIDIA docs, on Chapter 21. Configuring Power Management Support
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
powerManagement.finegrained = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
experimental power management of PRIME offload. For more information, see
|
2023-11-16 04:20:00 +00:00
|
|
|
the NVIDIA docs, on Chapter 22. PCI-Express Runtime D3 (RTD3) Power Management
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-08-22 20:05:09 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
dynamicBoost.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
dynamic Boost balances power between the CPU and the GPU for improved
|
|
|
|
performance on supported laptops using the nvidia-powerd daemon. For more
|
2023-11-16 04:20:00 +00:00
|
|
|
information, see the NVIDIA docs, on Chapter 23. Dynamic Boost on Linux
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
modesetting.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
kernel modesetting when using the NVIDIA proprietary driver.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Enabling this fixes screen tearing when using Optimus via PRIME (see
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`hardware.nvidia.prime.sync.enable`. This is not enabled
|
2020-04-24 23:36:52 +00:00
|
|
|
by default because it is not officially supported by NVIDIA and would not
|
2024-07-27 06:49:29 +00:00
|
|
|
work with SLI.
|
|
|
|
|
|
|
|
Enabling this and using version 545 or newer of the proprietary NVIDIA
|
|
|
|
driver causes it to provide its own framebuffer device, which can cause
|
|
|
|
Wayland compositors to work when they otherwise wouldn't.
|
|
|
|
'' // {
|
2024-07-31 10:19:44 +00:00
|
|
|
default = lib.versionAtLeast cfg.package.version "535";
|
|
|
|
defaultText = lib.literalExpression "lib.versionAtLeast cfg.package.version \"535\"";
|
2024-07-27 06:49:29 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
prime.nvidiaBusId = lib.mkOption {
|
|
|
|
type = busIDType;
|
|
|
|
default = "";
|
|
|
|
example = "PCI:1:0:0";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-08-22 20:05:09 +00:00
|
|
|
Bus ID of the NVIDIA GPU. You can find it using lspci; for example if lspci
|
|
|
|
shows the NVIDIA GPU at "01:00.0", set this option to "PCI:1:0:0".
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
prime.intelBusId = lib.mkOption {
|
|
|
|
type = busIDType;
|
|
|
|
default = "";
|
|
|
|
example = "PCI:0:2:0";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-08-22 20:05:09 +00:00
|
|
|
Bus ID of the Intel GPU. You can find it using lspci; for example if lspci
|
|
|
|
shows the Intel GPU at "00:02.0", set this option to "PCI:0:2:0".
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
prime.amdgpuBusId = lib.mkOption {
|
|
|
|
type = busIDType;
|
|
|
|
default = "";
|
|
|
|
example = "PCI:4:0:0";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-08-22 20:05:09 +00:00
|
|
|
Bus ID of the AMD APU. You can find it using lspci; for example if lspci
|
|
|
|
shows the AMD APU at "04:00.0", set this option to "PCI:4:0:0".
|
|
|
|
'';
|
|
|
|
};
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
prime.sync.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
NVIDIA Optimus support using the NVIDIA proprietary driver via PRIME.
|
2020-04-24 23:36:52 +00:00
|
|
|
If enabled, the NVIDIA GPU will be always on and used for all rendering,
|
2023-02-09 11:40:11 +00:00
|
|
|
while enabling output to displays attached only to the integrated Intel/AMD
|
|
|
|
GPU without a multiplexer.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Note that this option only has any effect if the "nvidia" driver is specified
|
2022-08-12 12:06:08 +00:00
|
|
|
in {option}`services.xserver.videoDrivers`, and it should preferably
|
2020-04-24 23:36:52 +00:00
|
|
|
be the only driver there.
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
If this is enabled, then the bus IDs of the NVIDIA and Intel/AMD GPUs have to
|
|
|
|
be specified ({option}`hardware.nvidia.prime.nvidiaBusId` and
|
|
|
|
{option}`hardware.nvidia.prime.intelBusId` or
|
|
|
|
{option}`hardware.nvidia.prime.amdgpuBusId`).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
If you enable this, you may want to also enable kernel modesetting for the
|
2022-08-12 12:06:08 +00:00
|
|
|
NVIDIA driver ({option}`hardware.nvidia.modesetting.enable`) in order
|
2020-04-24 23:36:52 +00:00
|
|
|
to prevent tearing.
|
|
|
|
|
|
|
|
Note that this configuration will only be successful when a display manager
|
2022-08-12 12:06:08 +00:00
|
|
|
for which the {option}`services.xserver.displayManager.setupCommands`
|
2023-11-16 04:20:00 +00:00
|
|
|
option is supported is used
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
prime.allowExternalGpu = lib.mkEnableOption ''
|
2023-11-16 04:20:00 +00:00
|
|
|
configuring X to allow external NVIDIA GPUs when using Prime [Reverse] sync optimus
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
prime.offload.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
render offload support using the NVIDIA proprietary driver via PRIME.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
If this is enabled, then the bus IDs of the NVIDIA and Intel/AMD GPUs have to
|
|
|
|
be specified ({option}`hardware.nvidia.prime.nvidiaBusId` and
|
|
|
|
{option}`hardware.nvidia.prime.intelBusId` or
|
2023-11-16 04:20:00 +00:00
|
|
|
{option}`hardware.nvidia.prime.amdgpuBusId`)
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-02-09 11:40:11 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
prime.offload.enableOffloadCmd = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
adding a `nvidia-offload` convenience script to {option}`environment.systemPackages`
|
2023-02-09 11:40:11 +00:00
|
|
|
for offloading programs to an nvidia device. To work, should have also enabled
|
|
|
|
{option}`hardware.nvidia.prime.offload.enable` or {option}`hardware.nvidia.prime.reverseSync.enable`.
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
Example usage `nvidia-offload sauerbraten_client`
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2023-02-09 11:40:11 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
prime.reverseSync.enable = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
NVIDIA Optimus support using the NVIDIA proprietary driver via reverse
|
2023-02-09 11:40:11 +00:00
|
|
|
PRIME. If enabled, the Intel/AMD GPU will be used for all rendering, while
|
|
|
|
enabling output to displays attached only to the NVIDIA GPU without a
|
|
|
|
multiplexer.
|
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
Warning: This feature is relatively new, depending on your system this might
|
|
|
|
work poorly. AMD support, especially so.
|
|
|
|
See: https://forums.developer.nvidia.com/t/the-all-new-outputsink-feature-aka-reverse-prime/129828
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
Note that this option only has any effect if the "nvidia" driver is specified
|
|
|
|
in {option}`services.xserver.videoDrivers`, and it should preferably
|
|
|
|
be the only driver there.
|
|
|
|
|
|
|
|
If this is enabled, then the bus IDs of the NVIDIA and Intel/AMD GPUs have to
|
|
|
|
be specified ({option}`hardware.nvidia.prime.nvidiaBusId` and
|
|
|
|
{option}`hardware.nvidia.prime.intelBusId` or
|
|
|
|
{option}`hardware.nvidia.prime.amdgpuBusId`).
|
|
|
|
|
|
|
|
If you enable this, you may want to also enable kernel modesetting for the
|
|
|
|
NVIDIA driver ({option}`hardware.nvidia.modesetting.enable`) in order
|
|
|
|
to prevent tearing.
|
|
|
|
|
|
|
|
Note that this configuration will only be successful when a display manager
|
|
|
|
for which the {option}`services.xserver.displayManager.setupCommands`
|
2023-11-16 04:20:00 +00:00
|
|
|
option is supported is used
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
prime.reverseSync.setupCommands.enable =
|
|
|
|
(lib.mkEnableOption ''
|
|
|
|
configure the display manager to be able to use the outputs
|
|
|
|
attached to the NVIDIA GPU.
|
|
|
|
Disable in order to configure the NVIDIA GPU outputs manually using xrandr.
|
|
|
|
Note that this configuration will only be successful when a display manager
|
|
|
|
for which the {option}`services.xserver.displayManager.setupCommands`
|
|
|
|
option is supported is used
|
|
|
|
'')
|
|
|
|
// {
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
nvidiaSettings =
|
2024-04-21 15:54:59 +00:00
|
|
|
(lib.mkEnableOption ''
|
2023-11-16 04:20:00 +00:00
|
|
|
nvidia-settings, NVIDIA's GUI configuration tool
|
2024-04-21 15:54:59 +00:00
|
|
|
'')
|
2024-06-05 15:53:02 +00:00
|
|
|
// {
|
|
|
|
default = true;
|
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
nvidiaPersistenced = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
nvidia-persistenced a update for NVIDIA GPU headless mode, i.e.
|
2023-11-16 04:20:00 +00:00
|
|
|
It ensures all GPUs stay awake even during headless mode
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
forceFullCompositionPipeline = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
forcefully the full composition pipeline.
|
2022-06-16 17:23:12 +00:00
|
|
|
This sometimes fixes screen tearing issues.
|
|
|
|
This has been reported to reduce the performance of some OpenGL applications and may produce issues in WebGL.
|
2023-11-16 04:20:00 +00:00
|
|
|
It also drastically increases the time the driver needs to clock down after load
|
2024-04-21 15:54:59 +00:00
|
|
|
'';
|
2022-06-16 17:23:12 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
package = lib.mkOption {
|
2024-06-05 15:53:02 +00:00
|
|
|
default =
|
|
|
|
config.boot.kernelPackages.nvidiaPackages."${if cfg.datacenter.enable then "dc" else "stable"}";
|
2023-10-09 19:29:22 +00:00
|
|
|
defaultText = lib.literalExpression ''
|
|
|
|
config.boot.kernelPackages.nvidiaPackages."\$\{if cfg.datacenter.enable then "dc" else "stable"}"
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
example = "config.boot.kernelPackages.nvidiaPackages.legacy_470";
|
|
|
|
description = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
The NVIDIA driver package to use.
|
|
|
|
'';
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
2022-06-26 10:26:21 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
open = lib.mkEnableOption ''
|
2023-08-22 20:05:09 +00:00
|
|
|
the open source NVIDIA kernel module
|
2024-07-31 10:19:44 +00:00
|
|
|
'' // {
|
|
|
|
defaultText = lib.literalExpression ''lib.versionAtLeast config.hardware.nvidia.package.version "560"'';
|
|
|
|
};
|
2022-06-26 10:26:21 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
igpuDriver = if pCfg.intelBusId != "" then "modesetting" else "amdgpu";
|
|
|
|
igpuBusId = if pCfg.intelBusId != "" then pCfg.intelBusId else pCfg.amdgpuBusId;
|
|
|
|
in
|
|
|
|
lib.mkIf (nvidia_x11 != null) (
|
|
|
|
lib.mkMerge [
|
|
|
|
# Common
|
|
|
|
({
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !(nvidiaEnabled && cfg.datacenter.enable);
|
|
|
|
message = "You cannot configure both X11 and Data Center drivers at the same time.";
|
|
|
|
}
|
2023-10-09 19:29:22 +00:00
|
|
|
];
|
2024-06-05 15:53:02 +00:00
|
|
|
boot = {
|
|
|
|
blacklistedKernelModules = [
|
|
|
|
"nouveau"
|
|
|
|
"nvidiafb"
|
|
|
|
];
|
|
|
|
|
|
|
|
# Don't add `nvidia-uvm` to `kernelModules`, because we want
|
|
|
|
# `nvidia-uvm` be loaded only after `udev` rules for `nvidia` kernel
|
|
|
|
# module are applied.
|
|
|
|
#
|
|
|
|
# Instead, we use `softdep` to lazily load `nvidia-uvm` kernel module
|
|
|
|
# after `nvidia` kernel module is loaded and `udev` rules are applied.
|
|
|
|
extraModprobeConfig = ''
|
|
|
|
softdep nvidia post: nvidia-uvm
|
|
|
|
'';
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
2024-06-20 14:57:18 +00:00
|
|
|
systemd.tmpfiles.rules = lib.mkIf config.virtualisation.docker.enableNvidia [ "L+ /run/nvidia-docker/bin - - - - ${nvidia_x11.bin}/origBin" ];
|
2024-06-05 15:53:02 +00:00
|
|
|
services.udev.extraRules = ''
|
|
|
|
# Create /dev/nvidia-uvm when the nvidia-uvm module is loaded.
|
|
|
|
KERNEL=="nvidia", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidiactl c 195 255'"
|
|
|
|
KERNEL=="nvidia", RUN+="${pkgs.runtimeShell} -c 'for i in $$(cat /proc/driver/nvidia/gpus/*/information | grep Minor | cut -d \ -f 4); do mknod -m 666 /dev/nvidia$${i} c 195 $${i}; done'"
|
|
|
|
KERNEL=="nvidia_modeset", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidia-modeset c 195 254'"
|
|
|
|
KERNEL=="nvidia_uvm", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidia-uvm c $$(grep nvidia-uvm /proc/devices | cut -d \ -f 1) 0'"
|
|
|
|
KERNEL=="nvidia_uvm", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidia-uvm-tools c $$(grep nvidia-uvm /proc/devices | cut -d \ -f 1) 1'"
|
2023-10-09 19:29:22 +00:00
|
|
|
'';
|
2024-06-20 14:57:18 +00:00
|
|
|
hardware.graphics = {
|
|
|
|
extraPackages = [ nvidia_x11.out ];
|
2024-06-05 15:53:02 +00:00
|
|
|
extraPackages32 = [ nvidia_x11.lib32 ];
|
|
|
|
};
|
|
|
|
environment.systemPackages = [ nvidia_x11.bin ];
|
2024-07-31 10:19:44 +00:00
|
|
|
|
|
|
|
hardware.nvidia.open = lib.mkDefault (lib.versionAtLeast nvidia_x11.version "560");
|
2024-06-05 15:53:02 +00:00
|
|
|
})
|
2024-06-20 14:57:18 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
# X11
|
|
|
|
(lib.mkIf nvidiaEnabled {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = primeEnabled -> pCfg.intelBusId == "" || pCfg.amdgpuBusId == "";
|
|
|
|
message = "You cannot configure both an Intel iGPU and an AMD APU. Pick the one corresponding to your processor.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = offloadCfg.enableOffloadCmd -> offloadCfg.enable || reverseSyncCfg.enable;
|
|
|
|
message = "Offload command requires offloading or reverse prime sync to be enabled.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion =
|
|
|
|
primeEnabled -> pCfg.nvidiaBusId != "" && (pCfg.intelBusId != "" || pCfg.amdgpuBusId != "");
|
|
|
|
message = "When NVIDIA PRIME is enabled, the GPU bus IDs must be configured.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = offloadCfg.enable -> lib.versionAtLeast nvidia_x11.version "435.21";
|
|
|
|
message = "NVIDIA PRIME render offload is currently only supported on versions >= 435.21.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion =
|
|
|
|
(reverseSyncCfg.enable && pCfg.amdgpuBusId != "") -> lib.versionAtLeast nvidia_x11.version "470.0";
|
|
|
|
message = "NVIDIA PRIME render offload for AMD APUs is currently only supported on versions >= 470 beta.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = !(syncCfg.enable && offloadCfg.enable);
|
|
|
|
message = "PRIME Sync and Offload cannot be both enabled";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = !(syncCfg.enable && reverseSyncCfg.enable);
|
|
|
|
message = "PRIME Sync and PRIME Reverse Sync cannot be both enabled";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = !(syncCfg.enable && cfg.powerManagement.finegrained);
|
|
|
|
message = "Sync precludes powering down the NVIDIA GPU.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = cfg.powerManagement.finegrained -> offloadCfg.enable;
|
|
|
|
message = "Fine-grained power management requires offload to be enabled.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = cfg.powerManagement.enable -> lib.versionAtLeast nvidia_x11.version "430.09";
|
|
|
|
message = "Required files for driver based power management only exist on versions >= 430.09.";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = cfg.open -> (cfg.package ? open && cfg.package ? firmware);
|
|
|
|
message = "This version of NVIDIA driver does not provide a corresponding opensource kernel driver";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
assertion = cfg.dynamicBoost.enable -> lib.versionAtLeast nvidia_x11.version "510.39.01";
|
|
|
|
message = "NVIDIA's Dynamic Boost feature only exists on versions >= 510.39.01";
|
|
|
|
}
|
|
|
|
];
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
# If Optimus/PRIME is enabled, we:
|
|
|
|
# - Specify the configured NVIDIA GPU bus ID in the Device section for the
|
|
|
|
# "nvidia" driver.
|
|
|
|
# - Add the AllowEmptyInitialConfiguration option to the Screen section for the
|
|
|
|
# "nvidia" driver, in order to allow the X server to start without any outputs.
|
|
|
|
# - Add a separate Device section for the Intel GPU, using the "modesetting"
|
|
|
|
# driver and with the configured BusID.
|
|
|
|
# - OR add a separate Device section for the AMD APU, using the "amdgpu"
|
|
|
|
# driver and with the configures BusID.
|
|
|
|
# - Reference that Device section from the ServerLayout section as an inactive
|
|
|
|
# device.
|
|
|
|
# - Configure the display manager to run specific `xrandr` commands which will
|
|
|
|
# configure/enable displays connected to the Intel iGPU / AMD APU.
|
|
|
|
|
|
|
|
# reverse sync implies offloading
|
|
|
|
hardware.nvidia.prime.offload.enable = lib.mkDefault reverseSyncCfg.enable;
|
|
|
|
|
|
|
|
services.xserver.drivers =
|
|
|
|
lib.optional primeEnabled {
|
|
|
|
name = igpuDriver;
|
|
|
|
display = offloadCfg.enable;
|
|
|
|
modules = lib.optional (igpuDriver == "amdgpu") pkgs.xorg.xf86videoamdgpu;
|
|
|
|
deviceSection =
|
|
|
|
''
|
|
|
|
BusID "${igpuBusId}"
|
|
|
|
''
|
|
|
|
+ lib.optionalString (syncCfg.enable && igpuDriver != "amdgpu") ''
|
|
|
|
Option "AccelMethod" "none"
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
++ lib.singleton {
|
|
|
|
name = "nvidia";
|
|
|
|
modules = [ nvidia_x11.bin ];
|
|
|
|
display = !offloadCfg.enable;
|
|
|
|
deviceSection =
|
|
|
|
''
|
|
|
|
Option "SidebandSocketPath" "/run/nvidia-xdriver/"
|
|
|
|
''
|
|
|
|
+ lib.optionalString primeEnabled ''
|
|
|
|
BusID "${pCfg.nvidiaBusId}"
|
|
|
|
''
|
|
|
|
+ lib.optionalString pCfg.allowExternalGpu ''
|
|
|
|
Option "AllowExternalGpus"
|
|
|
|
'';
|
|
|
|
screenSection =
|
|
|
|
''
|
|
|
|
Option "RandRRotation" "on"
|
|
|
|
''
|
|
|
|
+ lib.optionalString syncCfg.enable ''
|
|
|
|
Option "AllowEmptyInitialConfiguration"
|
|
|
|
''
|
|
|
|
+ lib.optionalString cfg.forceFullCompositionPipeline ''
|
|
|
|
Option "metamodes" "nvidia-auto-select +0+0 {ForceFullCompositionPipeline=On}"
|
|
|
|
Option "AllowIndirectGLXProtocol" "off"
|
|
|
|
Option "TripleBuffer" "on"
|
|
|
|
'';
|
|
|
|
};
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
services.xserver.serverLayoutSection =
|
|
|
|
lib.optionalString syncCfg.enable ''
|
|
|
|
Inactive "Device-${igpuDriver}[0]"
|
|
|
|
''
|
|
|
|
+ lib.optionalString reverseSyncCfg.enable ''
|
|
|
|
Inactive "Device-nvidia[0]"
|
|
|
|
''
|
|
|
|
+ lib.optionalString offloadCfg.enable ''
|
|
|
|
Option "AllowNVIDIAGPUScreens"
|
|
|
|
'';
|
|
|
|
|
|
|
|
services.xserver.displayManager.setupCommands =
|
|
|
|
let
|
|
|
|
gpuProviderName =
|
|
|
|
if igpuDriver == "amdgpu" then
|
|
|
|
# find the name of the provider if amdgpu
|
|
|
|
"`${lib.getExe pkgs.xorg.xrandr} --listproviders | ${lib.getExe pkgs.gnugrep} -i AMD | ${lib.getExe pkgs.gnused} -n 's/^.*name://p'`"
|
|
|
|
else
|
|
|
|
igpuDriver;
|
|
|
|
providerCmdParams =
|
|
|
|
if syncCfg.enable then "\"${gpuProviderName}\" NVIDIA-0" else "NVIDIA-G0 \"${gpuProviderName}\"";
|
|
|
|
in
|
2024-06-20 14:57:18 +00:00
|
|
|
lib.optionalString
|
|
|
|
(syncCfg.enable || (reverseSyncCfg.enable && reverseSyncCfg.setupCommands.enable))
|
|
|
|
''
|
|
|
|
# Added by nvidia configuration module for Optimus/PRIME.
|
|
|
|
${lib.getExe pkgs.xorg.xrandr} --setprovideroutputsource ${providerCmdParams}
|
|
|
|
${lib.getExe pkgs.xorg.xrandr} --auto
|
|
|
|
'';
|
2024-06-05 15:53:02 +00:00
|
|
|
|
|
|
|
environment.etc = {
|
|
|
|
"nvidia/nvidia-application-profiles-rc" = lib.mkIf nvidia_x11.useProfiles {
|
|
|
|
source = "${nvidia_x11.bin}/share/nvidia/nvidia-application-profiles-rc";
|
|
|
|
};
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
# 'nvidia_x11' installs it's files to /run/opengl-driver/...
|
|
|
|
"egl/egl_external_platform.d".source = "/run/opengl-driver/share/egl/egl_external_platform.d/";
|
|
|
|
};
|
2023-08-22 20:05:09 +00:00
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
hardware.graphics = {
|
2024-06-05 15:53:02 +00:00
|
|
|
extraPackages = [ pkgs.nvidia-vaapi-driver ];
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
2024-06-20 14:57:18 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
environment.systemPackages =
|
|
|
|
lib.optional cfg.nvidiaSettings nvidia_x11.settings
|
|
|
|
++ lib.optional cfg.nvidiaPersistenced nvidia_x11.persistenced
|
|
|
|
++ lib.optional offloadCfg.enableOffloadCmd (
|
|
|
|
pkgs.writeShellScriptBin "nvidia-offload" ''
|
|
|
|
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 "$@"
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
systemd.packages = lib.optional cfg.powerManagement.enable nvidia_x11.out;
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
let
|
|
|
|
nvidiaService = state: {
|
|
|
|
description = "NVIDIA system ${state} actions";
|
|
|
|
path = [ pkgs.kbd ];
|
2023-10-09 19:29:22 +00:00
|
|
|
serviceConfig = {
|
2024-06-05 15:53:02 +00:00
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${nvidia_x11.out}/bin/nvidia-sleep.sh '${state}'";
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
2024-06-05 15:53:02 +00:00
|
|
|
before = [ "systemd-${state}.service" ];
|
|
|
|
requiredBy = [ "systemd-${state}.service" ];
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
2024-06-05 15:53:02 +00:00
|
|
|
in
|
|
|
|
lib.mkMerge [
|
|
|
|
(lib.mkIf cfg.powerManagement.enable {
|
|
|
|
nvidia-suspend = nvidiaService "suspend";
|
|
|
|
nvidia-hibernate = nvidiaService "hibernate";
|
|
|
|
nvidia-resume = (nvidiaService "resume") // {
|
|
|
|
before = [ ];
|
|
|
|
after = [
|
|
|
|
"systemd-suspend.service"
|
|
|
|
"systemd-hibernate.service"
|
|
|
|
];
|
|
|
|
requiredBy = [
|
|
|
|
"systemd-suspend.service"
|
|
|
|
"systemd-hibernate.service"
|
|
|
|
];
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
2024-06-05 15:53:02 +00:00
|
|
|
})
|
|
|
|
(lib.mkIf cfg.nvidiaPersistenced {
|
|
|
|
"nvidia-persistenced" = {
|
|
|
|
description = "NVIDIA Persistence Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
Restart = "always";
|
|
|
|
PIDFile = "/var/run/nvidia-persistenced/nvidia-persistenced.pid";
|
|
|
|
ExecStart = "${lib.getExe nvidia_x11.persistenced} --verbose";
|
|
|
|
ExecStopPost = "${pkgs.coreutils}/bin/rm -rf /var/run/nvidia-persistenced";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.dynamicBoost.enable {
|
|
|
|
"nvidia-powerd" = {
|
|
|
|
description = "nvidia-powerd service";
|
|
|
|
path = [
|
|
|
|
pkgs.util-linux # nvidia-powerd wants lscpu
|
|
|
|
];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
BusName = "nvidia.powerd.server";
|
|
|
|
ExecStart = "${nvidia_x11.bin}/bin/nvidia-powerd";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2024-06-20 14:57:18 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
services.acpid.enable = true;
|
|
|
|
|
|
|
|
services.dbus.packages = lib.optional cfg.dynamicBoost.enable nvidia_x11.bin;
|
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
hardware.firmware = lib.optional (cfg.open || lib.versionAtLeast nvidia_x11.version "555") nvidia_x11.firmware;
|
2024-06-05 15:53:02 +00:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules =
|
|
|
|
[
|
|
|
|
# Remove the following log message:
|
|
|
|
# (WW) NVIDIA: Failed to bind sideband socket to
|
|
|
|
# (WW) NVIDIA: '/var/run/nvidia-xdriver-b4f69129' Permission denied
|
|
|
|
#
|
|
|
|
# https://bbs.archlinux.org/viewtopic.php?pid=1909115#p1909115
|
|
|
|
"d /run/nvidia-xdriver 0770 root users"
|
|
|
|
]
|
|
|
|
++ lib.optional (nvidia_x11.persistenced != null && config.virtualisation.docker.enableNvidia)
|
|
|
|
"L+ /run/nvidia-docker/extras/bin/nvidia-persistenced - - - - ${nvidia_x11.persistenced}/origBin/nvidia-persistenced";
|
|
|
|
|
|
|
|
boot = {
|
|
|
|
extraModulePackages = if cfg.open then [ nvidia_x11.open ] else [ nvidia_x11.bin ];
|
|
|
|
# nvidia-uvm is required by CUDA applications.
|
|
|
|
kernelModules = lib.optionals config.services.xserver.enable [
|
|
|
|
"nvidia"
|
|
|
|
"nvidia_modeset"
|
|
|
|
"nvidia_drm"
|
|
|
|
];
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
# If requested enable modesetting via kernel parameters.
|
2024-06-05 15:53:02 +00:00
|
|
|
kernelParams =
|
|
|
|
lib.optional (offloadCfg.enable || cfg.modesetting.enable) "nvidia-drm.modeset=1"
|
2024-07-27 06:49:29 +00:00
|
|
|
++ lib.optional ((offloadCfg.enable || cfg.modesetting.enable) && lib.versionAtLeast nvidia_x11.version "545") "nvidia-drm.fbdev=1"
|
2024-06-05 15:53:02 +00:00
|
|
|
++ lib.optional cfg.powerManagement.enable "nvidia.NVreg_PreserveVideoMemoryAllocations=1"
|
|
|
|
++ lib.optional cfg.open "nvidia.NVreg_OpenRmEnableUnsupportedGpus=1"
|
|
|
|
++ lib.optional (config.boot.kernelPackages.kernel.kernelAtLeast "6.2" && !ibtSupport) "ibt=off";
|
|
|
|
|
|
|
|
# enable finegrained power management
|
|
|
|
extraModprobeConfig = lib.optionalString cfg.powerManagement.finegrained ''
|
|
|
|
options nvidia "NVreg_DynamicPowerManagement=0x02"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
services.udev.extraRules = lib.optionalString cfg.powerManagement.finegrained (
|
|
|
|
lib.optionalString (lib.versionOlder config.boot.kernelPackages.kernel.version "5.5") ''
|
|
|
|
# Remove NVIDIA USB xHCI Host Controller devices, if present
|
|
|
|
ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c0330", ATTR{remove}="1"
|
|
|
|
|
|
|
|
# Remove NVIDIA USB Type-C UCSI devices, if present
|
|
|
|
ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x0c8000", ATTR{remove}="1"
|
|
|
|
|
|
|
|
# Remove NVIDIA Audio devices, if present
|
|
|
|
ACTION=="add", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x040300", ATTR{remove}="1"
|
|
|
|
''
|
|
|
|
+ ''
|
|
|
|
# Enable runtime PM for NVIDIA VGA/3D controller devices on driver bind
|
|
|
|
ACTION=="bind", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x030000", TEST=="power/control", ATTR{power/control}="auto"
|
|
|
|
ACTION=="bind", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x030200", TEST=="power/control", ATTR{power/control}="auto"
|
|
|
|
|
|
|
|
# Disable runtime PM for NVIDIA VGA/3D controller devices on driver unbind
|
|
|
|
ACTION=="unbind", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x030000", TEST=="power/control", ATTR{power/control}="on"
|
|
|
|
ACTION=="unbind", SUBSYSTEM=="pci", ATTR{vendor}=="0x10de", ATTR{class}=="0x030200", TEST=="power/control", ATTR{power/control}="on"
|
|
|
|
''
|
|
|
|
);
|
|
|
|
})
|
|
|
|
# Data Center
|
|
|
|
(lib.mkIf (cfg.datacenter.enable) {
|
|
|
|
boot.extraModulePackages = [ nvidia_x11.bin ];
|
|
|
|
|
|
|
|
systemd = {
|
|
|
|
tmpfiles.rules =
|
|
|
|
lib.optional (nvidia_x11.persistenced != null && config.virtualisation.docker.enableNvidia)
|
|
|
|
"L+ /run/nvidia-docker/extras/bin/nvidia-persistenced - - - - ${nvidia_x11.persistenced}/origBin/nvidia-persistenced";
|
|
|
|
|
|
|
|
services = lib.mkMerge [
|
|
|
|
({
|
|
|
|
nvidia-fabricmanager = {
|
|
|
|
enable = true;
|
|
|
|
description = "Start NVIDIA NVLink Management";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
unitConfig.After = [ "network-online.target" ];
|
|
|
|
unitConfig.Requires = [ "network-online.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
TimeoutStartSec = 240;
|
|
|
|
ExecStart =
|
|
|
|
let
|
2024-07-01 15:47:52 +00:00
|
|
|
# Since these rely on the `nvidia_x11.fabricmanager` derivation, they're
|
|
|
|
# unsuitable to be mentioned in the configuration defaults, but they _can_
|
|
|
|
# be overridden in `cfg.datacenter.settings` if needed.
|
|
|
|
fabricManagerConfDefaults = {
|
|
|
|
TOPOLOGY_FILE_PATH = "${nvidia_x11.fabricmanager}/share/nvidia-fabricmanager/nvidia/nvswitch";
|
|
|
|
DATABASE_PATH = "${nvidia_x11.fabricmanager}/share/nvidia-fabricmanager/nvidia/nvswitch";
|
|
|
|
};
|
|
|
|
nv-fab-conf = settingsFormat.generate "fabricmanager.conf" (fabricManagerConfDefaults // cfg.datacenter.settings);
|
2024-06-05 15:53:02 +00:00
|
|
|
in
|
2024-01-02 11:29:13 +00:00
|
|
|
"${lib.getExe nvidia_x11.fabricmanager} -c ${nv-fab-conf}";
|
2024-06-05 15:53:02 +00:00
|
|
|
LimitCORE = "infinity";
|
|
|
|
};
|
2024-01-02 11:29:13 +00:00
|
|
|
};
|
2024-06-05 15:53:02 +00:00
|
|
|
})
|
|
|
|
(lib.mkIf cfg.nvidiaPersistenced {
|
|
|
|
"nvidia-persistenced" = {
|
|
|
|
description = "NVIDIA Persistence Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
Restart = "always";
|
|
|
|
PIDFile = "/var/run/nvidia-persistenced/nvidia-persistenced.pid";
|
|
|
|
ExecStart = "${lib.getExe nvidia_x11.persistenced} --verbose";
|
|
|
|
ExecStopPost = "${pkgs.coreutils}/bin/rm -rf /var/run/nvidia-persistenced";
|
|
|
|
};
|
2024-01-02 11:29:13 +00:00
|
|
|
};
|
2024-06-05 15:53:02 +00:00
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
2024-01-02 11:29:13 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
environment.systemPackages =
|
|
|
|
lib.optional cfg.datacenter.enable nvidia_x11.fabricmanager
|
|
|
|
++ lib.optional cfg.nvidiaPersistenced nvidia_x11.persistenced;
|
|
|
|
})
|
|
|
|
]
|
|
|
|
);
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|