@@ -26,9 +26,73 @@ let
2626 nvidia_libs32 = (nvidiaForKernel pkgs_i686.linuxPackages).override { libsOnly = true; kernel = null; };
2727
2828 enabled = nvidia_x11 != null;
29+
30+ cfg = config.hardware.nvidia;
31+ optimusCfg = cfg.optimus_prime;
2932in
3033
3134{
35+ options = {
36+ hardware.nvidia.modesetting.enable = lib.mkOption {
37+ type = lib.types.bool;
38+ default = false;
39+ description = ''
40+ Enable kernel modesetting when using the NVIDIA proprietary driver.
41+
42+ Enabling this fixes screen tearing when using Optimus via PRIME (see
43+ <option>hardware.nvidia.optimus_prime.enable</option>. This is not enabled
44+ by default because it is not officially supported by NVIDIA and would not
45+ work with SLI.
46+ '';
47+ };
48+
49+ hardware.nvidia.optimus_prime.enable = lib.mkOption {
50+ type = lib.types.bool;
51+ default = false;
52+ description = ''
53+ Enable NVIDIA Optimus support using the NVIDIA proprietary driver via PRIME.
54+ If enabled, the NVIDIA GPU will be always on and used for all rendering,
55+ while enabling output to displays attached only to the integrated Intel GPU
56+ without a multiplexer.
57+
58+ Note that this option only has any effect if the "nvidia" driver is specified
59+ in <option>services.xserver.videoDrivers</option>, and it should preferably
60+ be the only driver there.
61+
62+ If this is enabled, then the bus IDs of the NVIDIA and Intel GPUs have to be
63+ specified (<option>hardware.nvidia.optimus_prime.nvidiaBusId</option> and
64+ <option>hardware.nvidia.optimus_prime.intelBusId</option>).
65+
66+ If you enable this, you may want to also enable kernel modesetting for the
67+ NVIDIA driver (<option>hardware.nvidia.modesetting.enable</option>) in order
68+ to prevent tearing.
69+
70+ Note that this configuration will only be successful when a display manager
71+ for which the <option>services.xserver.displayManager.setupCommands</option>
72+ option is supported is used; notably, SLiM is not supported.
73+ '';
74+ };
75+
76+ hardware.nvidia.optimus_prime.nvidiaBusId = lib.mkOption {
77+ type = lib.types.string;
78+ default = "";
79+ example = "PCI:1:0:0";
80+ description = ''
81+ Bus ID of the NVIDIA GPU. You can find it using lspci; for example if lspci
82+ shows the NVIDIA GPU at "01:00.0", set this option to "PCI:1:0:0".
83+ '';
84+ };
85+
86+ hardware.nvidia.optimus_prime.intelBusId = lib.mkOption {
87+ type = lib.types.string;
88+ default = "";
89+ example = "PCI:0:2:0";
90+ description = ''
91+ Bus ID of the Intel GPU. You can find it using lspci; for example if lspci
92+ shows the Intel GPU at "00:02.0", set this option to "PCI:0:2:0".
93+ '';
94+ };
95+ };
3296
3397 config = mkIf enabled {
3498 assertions = [
@@ -36,16 +100,62 @@ in
36100 assertion = config.services.xserver.displayManager.gdm.wayland;
37101 message = "NVidia drivers don't support wayland";
38102 }
103+ {
104+ assertion = !optimusCfg.enable ||
105+ (optimusCfg.nvidiaBusId != "" && optimusCfg.intelBusId != "");
106+ message = ''
107+ When NVIDIA Optimus via PRIME is enabled, the GPU bus IDs must configured.
108+ '';
109+ }
39110 ];
40111
41− services.xserver.drivers = singleton
42− { name = "nvidia"; modules = [ nvidia_x11.bin ]; libPath = [ nvidia_x11 ]; };
112+ # If Optimus/PRIME is enabled, we:
113+ # - Specify the configured NVIDIA GPU bus ID in the Device section for the
114+ # "nvidia" driver.
115+ # - Add the AllowEmptyInitialConfiguration option to the Screen section for the
116+ # "nvidia" driver, in order to allow the X server to start without any outputs.
117+ # - Add a separate Device section for the Intel GPU, using the "modesetting"
118+ # driver and with the configured BusID.
119+ # - Reference that Device section from the ServerLayout section as an inactive
120+ # device.
121+ # - Configure the display manager to run specific `xrandr` commands which will
122+ # configure/enable displays connected to the Intel GPU.
123+
124+ services.xserver.drivers = singleton {
125+ name = "nvidia";
126+ modules = [ nvidia_x11.bin ];
127+ libPath = [ nvidia_x11 ];
128+ deviceSection = optionalString optimusCfg.enable
129+ ''
130+ BusID "${optimusCfg.nvidiaBusId}"
131+ '';
132+ screenSection =
133+ ''
134+ Option "RandRRotation" "on"
135+ ${optionalString optimusCfg.enable "Option \"AllowEmptyInitialConfiguration\""}
136+ '';
137+ };
43138
44− services.xserver.screenSection =
139+ services.xserver.extraConfig = optionalString optimusCfg.enable
140+ ''
141+ Section "Device"
142+ Identifier "nvidia-optimus-intel"
143+ Driver "modesetting"
144+ BusID "${optimusCfg.intelBusId}"
145+ Option "AccelMethod" "none"
146+ EndSection
147+ '';
148+ services.xserver.serverLayoutSection = optionalString optimusCfg.enable
45149 ''
46− Option "RandRRotation" "on"
150+ Inactive "nvidia-optimus-intel"
47151 '';
48152
153+ services.xserver.displayManager.setupCommands = optionalString optimusCfg.enable ''
154+ # Added by nvidia configuration module for Optimus/PRIME.
155+ ${pkgs.xorg.xrandr}/bin/xrandr --setprovideroutputsource modesetting NVIDIA-0
156+ ${pkgs.xorg.xrandr}/bin/xrandr --auto
157+ '';
158+
49159 environment.etc."nvidia/nvidia-application-profiles-rc" = mkIf nvidia_x11.useProfiles {
50160 source = "${nvidia_x11.bin}/share/nvidia/nvidia-application-profiles-rc";
51161 };
@@ -62,6 +172,8 @@ in
62172 boot.kernelModules = [ "nvidia-uvm" ] ++
63173 lib.optionals config.services.xserver.enable [ "nvidia" "nvidia_modeset" "nvidia_drm" ];
64174
175+ # If requested enable modesetting via kernel parameter.
176+ boot.kernelParams = optional cfg.modesetting.enable "nvidia-drm.modeset=1";
65177
66178 # Create /dev/nvidia-uvm when the nvidia-uvm module is loaded.
67179 services.udev.extraRules =