2020-04-24 23:36:52 +00:00
|
|
|
# This module provides the proprietary NVIDIA X11 / OpenGL drivers.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2021-02-05 17:12:51 +00:00
|
|
|
nvidia_x11 = let
|
|
|
|
drivers = config.services.xserver.videoDrivers;
|
|
|
|
isDeprecated = str: (hasPrefix "nvidia" str) && (str != "nvidia");
|
|
|
|
hasDeprecated = drivers: any isDeprecated drivers;
|
|
|
|
in if (hasDeprecated drivers) then
|
|
|
|
throw ''
|
|
|
|
Selecting an nvidia driver has been modified for NixOS 19.03. The version is now set using `hardware.nvidia.package`.
|
|
|
|
''
|
|
|
|
else if (elem "nvidia" drivers) then cfg.package else null;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
enabled = nvidia_x11 != null;
|
|
|
|
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;
|
2020-05-15 21:57:56 +00:00
|
|
|
nvidiaPersistencedEnabled = cfg.nvidiaPersistenced;
|
2021-09-18 10:52:07 +00:00
|
|
|
nvidiaSettings = cfg.nvidiaSettings;
|
2022-07-14 12:49:19 +00:00
|
|
|
busIDType = 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);
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
imports =
|
|
|
|
[
|
|
|
|
(mkRenamedOptionModule [ "hardware" "nvidia" "optimus_prime" "enable" ] [ "hardware" "nvidia" "prime" "sync" "enable" ])
|
2023-02-09 11:40:11 +00:00
|
|
|
(mkRenamedOptionModule [ "hardware" "nvidia" "optimus_prime" "allowExternalGpu" ] [ "hardware" "nvidia" "prime" "allowExternalGpu" ])
|
|
|
|
(mkRenamedOptionModule [ "hardware" "nvidia" "prime" "sync" "allowExternalGpu" ] [ "hardware" "nvidia" "prime" "allowExternalGpu" ])
|
2020-04-24 23:36:52 +00:00
|
|
|
(mkRenamedOptionModule [ "hardware" "nvidia" "optimus_prime" "nvidiaBusId" ] [ "hardware" "nvidia" "prime" "nvidiaBusId" ])
|
|
|
|
(mkRenamedOptionModule [ "hardware" "nvidia" "optimus_prime" "intelBusId" ] [ "hardware" "nvidia" "prime" "intelBusId" ])
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
2020-05-15 21:57:56 +00:00
|
|
|
hardware.nvidia.powerManagement.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-05-15 21:57:56 +00:00
|
|
|
Experimental power management through systemd. For more information, see
|
|
|
|
the NVIDIA docs, on Chapter 21. Configuring Power Management Support.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
hardware.nvidia.powerManagement.finegrained = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Experimental power management of PRIME offload. For more information, see
|
|
|
|
the NVIDIA docs, chapter 22. PCI-Express runtime power management.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
hardware.nvidia.modesetting.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Enable kernel modesetting when using the NVIDIA proprietary driver.
|
|
|
|
|
|
|
|
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
|
|
|
|
work with SLI.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hardware.nvidia.prime.nvidiaBusId = mkOption {
|
2022-04-27 09:35:20 +00:00
|
|
|
type = busIDType;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example = "PCI:1:0:0";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +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".
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hardware.nvidia.prime.intelBusId = mkOption {
|
2022-04-27 09:35:20 +00:00
|
|
|
type = busIDType;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example = "PCI:0:2:0";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +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".
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
hardware.nvidia.prime.amdgpuBusId = mkOption {
|
2022-04-27 09:35:20 +00:00
|
|
|
type = busIDType;
|
2021-02-05 17:12:51 +00:00
|
|
|
default = "";
|
|
|
|
example = "PCI:4:0:0";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Bus ID of the AMD APU. You can find it using lspci; for example if lspci
|
2021-06-28 23:13:55 +00:00
|
|
|
shows the AMD APU at "04:00.0", set this option to "PCI:4:0:0".
|
2021-02-05 17:12:51 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
hardware.nvidia.prime.sync.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Enable NVIDIA Optimus support using the NVIDIA proprietary driver via PRIME.
|
|
|
|
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`
|
2020-04-24 23:36:52 +00:00
|
|
|
option is supported is used.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
hardware.nvidia.prime.allowExternalGpu = mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2023-02-09 11:40:11 +00:00
|
|
|
Configure X to allow external NVIDIA GPUs when using Prime [Reverse] sync optimus.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hardware.nvidia.prime.offload.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Enable render offload support using the NVIDIA proprietary driver via PRIME.
|
|
|
|
|
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`).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hardware.nvidia.prime.offload.enableOffloadCmd = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Adds a `nvidia-offload` convenience script to {option}`environment.systemPackages`
|
|
|
|
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`.
|
|
|
|
|
|
|
|
Example usage `nvidia-offload sauerbraten_client`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hardware.nvidia.prime.reverseSync.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
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
|
|
|
|
|
|
|
|
Enable NVIDIA Optimus support using the NVIDIA proprietary driver via reverse
|
|
|
|
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.
|
|
|
|
|
|
|
|
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`
|
|
|
|
option is supported is used.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2021-09-18 10:52:07 +00:00
|
|
|
hardware.nvidia.nvidiaSettings = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-09-18 10:52:07 +00:00
|
|
|
Whether to add nvidia-settings, NVIDIA's GUI configuration tool, to
|
|
|
|
systemPackages.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
hardware.nvidia.nvidiaPersistenced = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-05-15 21:57:56 +00:00
|
|
|
Update for NVIDA GPU headless mode, i.e. nvidia-persistenced. It ensures all
|
|
|
|
GPUs stay awake even during headless mode.
|
|
|
|
'';
|
|
|
|
};
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
hardware.nvidia.forceFullCompositionPipeline = lib.mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-06-16 17:23:12 +00:00
|
|
|
Whether to force-enable the full composition pipeline.
|
|
|
|
This sometimes fixes screen tearing issues.
|
|
|
|
This 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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
hardware.nvidia.package = lib.mkOption {
|
2022-06-16 17:23:12 +00:00
|
|
|
type = types.package;
|
2021-02-05 17:12:51 +00:00
|
|
|
default = config.boot.kernelPackages.nvidiaPackages.stable;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "config.boot.kernelPackages.nvidiaPackages.stable";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
The NVIDIA X11 derivation to use.
|
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression "config.boot.kernelPackages.nvidiaPackages.legacy_340";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
2022-06-26 10:26:21 +00:00
|
|
|
|
|
|
|
hardware.nvidia.open = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-06-26 10:26:21 +00:00
|
|
|
Whether to use the open source kernel module
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
config = let
|
|
|
|
igpuDriver = if pCfg.intelBusId != "" then "modesetting" else "amdgpu";
|
|
|
|
igpuBusId = if pCfg.intelBusId != "" then pCfg.intelBusId else pCfg.amdgpuBusId;
|
|
|
|
in mkIf enabled {
|
2020-04-24 23:36:52 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
2021-02-05 17:12:51 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
{
|
|
|
|
assertion = offloadCfg.enableOffloadCmd -> offloadCfg.enable || reverseSyncCfg.enable;
|
|
|
|
message = ''
|
|
|
|
Offload command requires offloading or reverse prime sync to be enabled.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
{
|
|
|
|
assertion = primeEnabled -> pCfg.nvidiaBusId != "" && (pCfg.intelBusId != "" || pCfg.amdgpuBusId != "");
|
2020-04-24 23:36:52 +00:00
|
|
|
message = ''
|
|
|
|
When NVIDIA PRIME is enabled, the GPU bus IDs must configured.
|
|
|
|
'';
|
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
assertion = offloadCfg.enable -> versionAtLeast nvidia_x11.version "435.21";
|
|
|
|
message = "NVIDIA PRIME render offload is currently only supported on versions >= 435.21.";
|
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
{
|
|
|
|
assertion = (reverseSyncCfg.enable && pCfg.amdgpuBusId != "") -> versionAtLeast nvidia_x11.version "470.0";
|
|
|
|
message = "NVIDIA PRIME render offload for AMD APUs is currently only supported on versions >= 470 beta.";
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
assertion = !(syncCfg.enable && offloadCfg.enable);
|
2023-02-09 11:40:11 +00:00
|
|
|
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";
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
{
|
|
|
|
assertion = !(syncCfg.enable && cfg.powerManagement.finegrained);
|
|
|
|
message = "Sync precludes powering down the NVIDIA GPU.";
|
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
{
|
2021-10-14 00:43:12 +00:00
|
|
|
assertion = cfg.powerManagement.finegrained -> offloadCfg.enable;
|
2021-02-05 17:12:51 +00:00
|
|
|
message = "Fine-grained power management requires offload to be enabled.";
|
|
|
|
}
|
2021-08-08 23:34:03 +00:00
|
|
|
|
|
|
|
{
|
2022-09-09 14:08:57 +00:00
|
|
|
assertion = cfg.powerManagement.enable -> versionAtLeast nvidia_x11.version "430.09";
|
|
|
|
message = "Required files for driver based power management only exist on versions >= 430.09.";
|
2021-08-08 23:34:03 +00:00
|
|
|
}
|
2022-06-26 10:26:21 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
assertion = cfg.open -> (cfg.package ? open && cfg.package ? firmware);
|
|
|
|
message = "This version of NVIDIA driver does not provide a corresponding opensource kernel driver";
|
|
|
|
}
|
2020-04-24 23:36:52 +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.
|
2021-02-05 17:12:51 +00:00
|
|
|
# - OR add a separate Device section for the AMD APU, using the "amdgpu"
|
|
|
|
# driver and with the configures BusID.
|
2020-04-24 23:36:52 +00:00
|
|
|
# - Reference that Device section from the ServerLayout section as an inactive
|
|
|
|
# device.
|
|
|
|
# - Configure the display manager to run specific `xrandr` commands which will
|
2021-02-05 17:12:51 +00:00
|
|
|
# configure/enable displays connected to the Intel iGPU / AMD APU.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
# reverse sync implies offloading
|
|
|
|
hardware.nvidia.prime.offload.enable = mkDefault reverseSyncCfg.enable;
|
|
|
|
|
|
|
|
services.xserver.drivers = optional primeEnabled {
|
2021-02-05 17:12:51 +00:00
|
|
|
name = igpuDriver;
|
2020-04-24 23:36:52 +00:00
|
|
|
display = offloadCfg.enable;
|
2022-10-30 15:09:59 +00:00
|
|
|
modules = optionals (igpuDriver == "amdgpu") [ pkgs.xorg.xf86videoamdgpu ];
|
2020-04-24 23:36:52 +00:00
|
|
|
deviceSection = ''
|
2021-02-05 17:12:51 +00:00
|
|
|
BusID "${igpuBusId}"
|
2022-03-30 09:31:56 +00:00
|
|
|
${optionalString (syncCfg.enable && igpuDriver != "amdgpu") ''Option "AccelMethod" "none"''}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
} ++ singleton {
|
|
|
|
name = "nvidia";
|
|
|
|
modules = [ nvidia_x11.bin ];
|
|
|
|
display = !offloadCfg.enable;
|
|
|
|
deviceSection = optionalString primeEnabled
|
|
|
|
''
|
|
|
|
BusID "${pCfg.nvidiaBusId}"
|
2023-02-09 11:40:11 +00:00
|
|
|
${optionalString pCfg.allowExternalGpu "Option \"AllowExternalGpus\""}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
screenSection =
|
|
|
|
''
|
|
|
|
Option "RandRRotation" "on"
|
2022-06-16 17:23:12 +00:00
|
|
|
'' + optionalString syncCfg.enable ''
|
|
|
|
Option "AllowEmptyInitialConfiguration"
|
|
|
|
'' + optionalString cfg.forceFullCompositionPipeline ''
|
|
|
|
Option "metamodes" "nvidia-auto-select +0+0 {ForceFullCompositionPipeline=On}"
|
|
|
|
Option "AllowIndirectGLXProtocol" "off"
|
|
|
|
Option "TripleBuffer" "on"
|
|
|
|
''
|
|
|
|
;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
services.xserver.serverLayoutSection = optionalString syncCfg.enable ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Inactive "Device-${igpuDriver}[0]"
|
2023-02-09 11:40:11 +00:00
|
|
|
'' + optionalString reverseSyncCfg.enable ''
|
|
|
|
Inactive "Device-nvidia[0]"
|
2020-04-24 23:36:52 +00:00
|
|
|
'' + optionalString offloadCfg.enable ''
|
|
|
|
Option "AllowNVIDIAGPUScreens"
|
|
|
|
'';
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
services.xserver.displayManager.setupCommands = let
|
2023-02-09 11:40:11 +00:00
|
|
|
gpuProviderName = if igpuDriver == "amdgpu" then
|
2022-03-30 09:31:56 +00:00
|
|
|
# find the name of the provider if amdgpu
|
|
|
|
"`${pkgs.xorg.xrandr}/bin/xrandr --listproviders | ${pkgs.gnugrep}/bin/grep -i AMD | ${pkgs.gnused}/bin/sed -n 's/^.*name://p'`"
|
|
|
|
else
|
|
|
|
igpuDriver;
|
2023-02-09 11:40:11 +00:00
|
|
|
providerCmdParams = if syncCfg.enable then "\"${gpuProviderName}\" NVIDIA-0" else "NVIDIA-G0 \"${gpuProviderName}\"";
|
|
|
|
in optionalString (syncCfg.enable || reverseSyncCfg.enable) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
# Added by nvidia configuration module for Optimus/PRIME.
|
2023-02-09 11:40:11 +00:00
|
|
|
${pkgs.xorg.xrandr}/bin/xrandr --setprovideroutputsource ${providerCmdParams}
|
2020-04-24 23:36:52 +00:00
|
|
|
${pkgs.xorg.xrandr}/bin/xrandr --auto
|
|
|
|
'';
|
|
|
|
|
|
|
|
environment.etc."nvidia/nvidia-application-profiles-rc" = mkIf nvidia_x11.useProfiles {
|
|
|
|
source = "${nvidia_x11.bin}/share/nvidia/nvidia-application-profiles-rc";
|
|
|
|
};
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
# 'nvidia_x11' installs it's files to /run/opengl-driver/...
|
|
|
|
environment.etc."egl/egl_external_platform.d".source =
|
|
|
|
"/run/opengl-driver/share/egl/egl_external_platform.d/";
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
hardware.opengl.extraPackages = [
|
|
|
|
nvidia_x11.out
|
|
|
|
pkgs.nvidia-vaapi-driver
|
|
|
|
];
|
|
|
|
hardware.opengl.extraPackages32 = [
|
|
|
|
nvidia_x11.lib32
|
|
|
|
pkgs.pkgsi686Linux.nvidia-vaapi-driver
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-09-18 10:52:07 +00:00
|
|
|
environment.systemPackages = [ nvidia_x11.bin ]
|
2021-12-06 16:07:01 +00:00
|
|
|
++ optionals cfg.nvidiaSettings [ nvidia_x11.settings ]
|
2023-02-09 11:40:11 +00:00
|
|
|
++ optionals nvidiaPersistencedEnabled [ nvidia_x11.persistenced ]
|
|
|
|
++ optionals 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 "$@"
|
|
|
|
'')
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
systemd.packages = optional cfg.powerManagement.enable nvidia_x11.out;
|
|
|
|
|
|
|
|
systemd.services = let
|
|
|
|
baseNvidiaService = state: {
|
|
|
|
description = "NVIDIA system ${state} actions";
|
|
|
|
|
|
|
|
path = with pkgs; [ kbd ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${nvidia_x11.out}/bin/nvidia-sleep.sh '${state}'";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
nvidiaService = sleepState: (baseNvidiaService sleepState) // {
|
|
|
|
before = [ "systemd-${sleepState}.service" ];
|
|
|
|
requiredBy = [ "systemd-${sleepState}.service" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
services = (builtins.listToAttrs (map (t: nameValuePair "nvidia-${t}" (nvidiaService t)) ["hibernate" "suspend"]))
|
|
|
|
// {
|
|
|
|
nvidia-resume = (baseNvidiaService "resume") // {
|
|
|
|
after = [ "systemd-suspend.service" "systemd-hibernate.service" ];
|
|
|
|
requiredBy = [ "systemd-suspend.service" "systemd-hibernate.service" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in optionalAttrs cfg.powerManagement.enable services
|
|
|
|
// optionalAttrs nvidiaPersistencedEnabled {
|
|
|
|
"nvidia-persistenced" = mkIf nvidiaPersistencedEnabled {
|
|
|
|
description = "NVIDIA Persistence Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
Restart = "always";
|
|
|
|
PIDFile = "/var/run/nvidia-persistenced/nvidia-persistenced.pid";
|
|
|
|
ExecStart = "${nvidia_x11.persistenced}/bin/nvidia-persistenced --verbose";
|
|
|
|
ExecStopPost = "${pkgs.coreutils}/bin/rm -rf /var/run/nvidia-persistenced";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.tmpfiles.rules = optional config.virtualisation.docker.enableNvidia
|
|
|
|
"L+ /run/nvidia-docker/bin - - - - ${nvidia_x11.bin}/origBin"
|
|
|
|
++ optional (nvidia_x11.persistenced != null && config.virtualisation.docker.enableNvidia)
|
2022-06-16 17:23:12 +00:00
|
|
|
"L+ /run/nvidia-docker/extras/bin/nvidia-persistenced - - - - ${nvidia_x11.persistenced}/origBin/nvidia-persistenced";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-06-26 10:26:21 +00:00
|
|
|
boot.extraModulePackages = if cfg.open then [ nvidia_x11.open ] else [ nvidia_x11.bin ];
|
|
|
|
hardware.firmware = lib.optional cfg.open nvidia_x11.firmware;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# nvidia-uvm is required by CUDA applications.
|
|
|
|
boot.kernelModules = [ "nvidia-uvm" ] ++
|
|
|
|
optionals config.services.xserver.enable [ "nvidia" "nvidia_modeset" "nvidia_drm" ];
|
|
|
|
|
|
|
|
# If requested enable modesetting via kernel parameter.
|
2020-05-15 21:57:56 +00:00
|
|
|
boot.kernelParams = optional (offloadCfg.enable || cfg.modesetting.enable) "nvidia-drm.modeset=1"
|
2022-06-26 10:26:21 +00:00
|
|
|
++ optional cfg.powerManagement.enable "nvidia.NVreg_PreserveVideoMemoryAllocations=1"
|
2023-03-04 12:14:45 +00:00
|
|
|
++ optional cfg.open "nvidia.NVreg_OpenRmEnableUnsupportedGpus=1"
|
2023-04-12 12:48:02 +00:00
|
|
|
++ optional (config.boot.kernelPackages.kernel.kernelAtLeast "6.2" && !ibtSupport) "ibt=off";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
services.udev.extraRules =
|
|
|
|
''
|
2021-02-05 17:12:51 +00:00
|
|
|
# Create /dev/nvidia-uvm when the nvidia-uvm module is loaded.
|
2022-08-12 12:06:08 +00:00
|
|
|
KERNEL=="nvidia", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidiactl c $$(grep nvidia-frontend /proc/devices | cut -d \ -f 1) 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 $$(grep nvidia-frontend /proc/devices | cut -d \ -f 1) $${i}; done'"
|
|
|
|
KERNEL=="nvidia_modeset", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidia-modeset c $$(grep nvidia-frontend /proc/devices | cut -d \ -f 1) 254'"
|
2020-04-24 23:36:52 +00:00
|
|
|
KERNEL=="nvidia_uvm", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidia-uvm c $$(grep nvidia-uvm /proc/devices | cut -d \ -f 1) 0'"
|
2022-04-27 09:35:20 +00:00
|
|
|
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'"
|
2022-06-16 17:23:12 +00:00
|
|
|
'' + optionalString cfg.powerManagement.finegrained (
|
|
|
|
optionalString (versionOlder config.boot.kernelPackages.kernel.version "5.5") ''
|
2021-02-05 17:12:51 +00:00
|
|
|
# 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"
|
2022-06-16 17:23:12 +00:00
|
|
|
'' + ''
|
2021-02-05 17:12:51 +00:00
|
|
|
# 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"
|
2022-06-16 17:23:12 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
boot.extraModprobeConfig = mkIf cfg.powerManagement.finegrained ''
|
|
|
|
options nvidia "NVreg_DynamicPowerManagement=0x02"
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
boot.blacklistedKernelModules = [ "nouveau" "nvidiafb" ];
|
|
|
|
|
|
|
|
services.acpid.enable = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|