git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/xserver: Implement configuration of NVIDIA Optimus via PRIME

This adds configuration options which automate the configuration of NVIDIA Optimus using PRIME. This allows using the NVIDIA proprietary driver on Optimus laptops, in order to render using the NVIDIA GPU while outputting to displays connected only to the integrated Intel GPU. It also adds an option for enabling kernel modesetting for the NVIDIA driver (via a kernel command line flag); this is particularly useful together with Optimus/PRIME because it fixes tearing on PRIME-connected screens. The user still needs to enable the Optimus/PRIME feature and specify the bus IDs of the Intel and NVIDIA GPUs, but this is still much easier for users and more reliable. The implementation handles both the X configuration file as well as getting display managers to run certain necessary `xrandr` commands just after X has started. Configuration of commands run after X startup is done using a new configuration option `services.xserver.displayManager.setupCommands`. Support for this option is implemented for LightDM, GDM and SDDM; all of these have been tested with this feature including logging into a Plasma session. Note: support of `setupCommands` for GDM is implemented by making GDM run the session executable via a wrapper; the wrapper will run the `setupCommands` before execing. This seemed like the simplest and most reliable approach, and solves running these commands both for GDM's X server and user X servers (GDM starts separate X servers for itself and user sessions). An alternative approach would be with autostart files but that seems harder to set up and less reliable. Note that some simple features for X configuration file generation (in `xserver.nix`) are added which are used in the implementation: - `services.xserver.extraConfig`: Allows adding arbitrary new sections. This is used to add the Device section for the Intel GPU. - `deviceSection` and `screenSection` within `services.xserver.drivers`. This allows the nvidia configuration module to add additional contents into the `Device` and `Screen` sections of the "nvidia" driver, and not into such sections for other drivers that may be enabled.

commitf26153754a1b6ac0d72adde9c75e1473463b4dbb authorAmbroz Bizjak <ambrop7@gmail.com> dateSat 30 Jun 2018, 09:33 (8 years ago) parent49f6ea4
10 files changed+244−23
nixos/modules/hardware/video/nvidia.nix+116−4 nixos/modules/services/x11/display-managers/default.nix+11 nixos/modules/services/x11/display-managers/gdm.nix+12 nixos/modules/services/x11/display-managers/lightdm.nix+6 nixos/modules/services/x11/display-managers/sddm.nix+3−1 nixos/modules/services/x11/xserver.nix+10 pkgs/desktops/gnome-3/core/gdm/default.nix+15−1 pkgs/desktops/gnome-3/core/gdm/gdm-session-worker_forward-vars.patch+31 pkgs/desktops/gnome-3/core/gdm/gdm-session-worker_xserver-path.patch−17 pkgs/desktops/gnome-3/core/gdm/gdm-x-session_session-wrapper.patch+40
nixos/modules/hardware/video/nvidia.nix+116−4
@@ -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 =
nixos/modules/services/x11/display-managers/default.nix+11
@@ -227,6 +227,17 @@ in
227227 description = "List of arguments for the X server.";
228228 };
229229
230+ setupCommands = mkOption {
231+ type = types.lines;
232+ default = "";
233+ description = ''
234+ Shell commands executed just after the X server has started.
235+
236+ This option is only effective for display managers for which this feature
237+ is supported; currently these are LightDM, GDM and SDDM.
238+ '';
239+ };
240+
230241 sessionCommands = mkOption {
231242 type = types.lines;
232243 default = "";
nixos/modules/services/x11/display-managers/gdm.nix+12
@@ -7,6 +7,13 @@ let
77 cfg = config.services.xserver.displayManager;
88 gdm = pkgs.gnome3.gdm;
99
10+ xSessionWrapper = if (cfg.setupCommands == "") then null else
11+ pkgs.writeScript "gdm-x-session-wrapper" ''
12+ #!${pkgs.bash}/bin/bash
13+ ${cfg.setupCommands}
14+ exec "$@"
15+ '';
16+
1017in
1118
1219{
@@ -112,6 +119,11 @@ in
112119 GDM_SESSIONS_DIR = "${cfg.session.desktops}";
113120 # Find the mouse
114121 XCURSOR_PATH = "~/.icons:${pkgs.gnome3.adwaita-icon-theme}/share/icons";
122+ } // optionalAttrs (xSessionWrapper != null) {
123+ # Make GDM use this wrapper before running the session, which runs the
124+ # configured setupCommands. This relies on a patched GDM which supports
125+ # this environment variable.
126+ GDM_X_SESSION_WRAPPER = "${xSessionWrapper}";
115127 };
116128 execCmd = "exec ${gdm}/bin/gdm";
117129 };
nixos/modules/services/x11/display-managers/lightdm.nix+6
@@ -61,6 +61,12 @@ let
6161 ${optionalString hasDefaultUserSession ''
6262 user-session=${defaultSessionName}
6363 ''}
64+ ${optionalString (dmcfg.setupCommands != "") ''
65+ display-setup-script=${pkgs.writeScript "lightdm-display-setup" ''
66+ #!${pkgs.bash}/bin/bash
67+ ${dmcfg.setupCommands}
68+ ''}
69+ ''}
6470 ${cfg.extraSeatDefaults}
6571 '';
6672
nixos/modules/services/x11/display-managers/sddm.nix+3−1
@@ -31,6 +31,7 @@ let
3131 rm -fr /var/lib/sddm/.cache/sddm-greeter/qmlcache
3232
3333 ${cfg.setupScript}
34+ ${dmcfg.setupCommands}
3435 '';
3536
3637 Xstop = pkgs.writeScript "Xstop" ''
@@ -148,7 +149,8 @@ in
148149 xrandr --auto
149150 '';
150151 description = ''
151− A script to execute when starting the display server.
152+ A script to execute when starting the display server. DEPRECATED, please
153+ use <option>services.xserver.displayManager.setupCommands</option>.
152154 '';
153155 };
154156
nixos/modules/services/x11/xserver.nix+10
@@ -374,6 +374,12 @@ in
374374 description = "Contents of the first Monitor section of the X server configuration file.";
375375 };
376376
377+ extraConfig = mkOption {
378+ type = types.lines;
379+ default = "";
380+ description = "Additional contents (sections) included in the X server configuration file";
381+ };
382+
377383 xrandrHeads = mkOption {
378384 default = [];
379385 example = [
@@ -741,6 +747,7 @@ in
741747 Driver "${driver.driverName or driver.name}"
742748 ${if cfg.useGlamor then ''Option "AccelMethod" "glamor"'' else ""}
743749 ${cfg.deviceSection}
750+ ${driver.deviceSection or ""}
744751 ${xrandrDeviceSection}
745752 EndSection
746753
@@ -752,6 +759,7 @@ in
752759 ''}
753760
754761 ${cfg.screenSection}
762+ ${driver.screenSection or ""}
755763
756764 ${optionalString (cfg.defaultDepth != 0) ''
757765 DefaultDepth ${toString cfg.defaultDepth}
@@ -781,6 +789,8 @@ in
781789 '')}
782790
783791 ${xrandrMonitorSections}
792+
793+ ${cfg.extraConfig}
784794 '';
785795
786796 fonts.enableDefaultFonts = mkDefault true;
pkgs/desktops/gnome-3/core/gdm/default.nix+15−1
@@ -36,13 +36,27 @@ stdenv.mkDerivation rec {
3636
3737 # Disable Access Control because our X does not support FamilyServerInterpreted yet
3838 patches = [
39+ # Change hardcoded paths to nix store paths.
3940 (substituteAll {
4041 src = ./fix-paths.patch;
4142 inherit coreutils plymouth xwayland;
4243 })
44+
45+ # The following patches implement certain environment variables in GDM which are set by
46+ # the gdm configuration module (nixos/modules/services/x11/display-managers/gdm.nix).
47+
48+ # Look for session definition files in the directory specified by GDM_SESSIONS_DIR.
4349 ./sessions_dir.patch
50+
51+ # Allow specifying X server arguments with GDM_X_SERVER_EXTRA_ARGS.
4452 ./gdm-x-session_extra_args.patch
45− ./gdm-session-worker_xserver-path.patch
53+
54+ # Allow specifying a wrapper for running the session command.
55+ ./gdm-x-session_session-wrapper.patch
56+
57+ # Forwards certain environment variables to the gdm-x-session child process
58+ # to ensure that the above two patches actually work.
59+ ./gdm-session-worker_forward-vars.patch
4660 ];
4761
4862 installFlags = [
pkgs/desktops/gnome-3/core/gdm/gdm-session-worker_forward-vars.patchnew file+31
@@ -0,0 +1,31 @@
1+diff --git a/daemon/gdm-session-worker.c b/daemon/gdm-session-worker.c
2+index 9ef4c5b..94da834 100644
3+--- a/daemon/gdm-session-worker.c
4++++ b/daemon/gdm-session-worker.c
5+@@ -1515,6 +1515,16 @@ gdm_session_worker_load_env_d (GdmSessionWorker *worker)
6+ g_object_unref (dir);
7+ }
8+
9++static void
10++gdm_session_worker_forward_var (GdmSessionWorker *worker, char const *var)
11++{
12++ char const *value = g_getenv(var);
13++ if (value != NULL) {
14++ g_debug ("forwarding %s= %s", var, value);
15++ gdm_session_worker_set_environment_variable(worker, var, value);
16++ }
17++}
18++
19+ static gboolean
20+ gdm_session_worker_accredit_user (GdmSessionWorker *worker,
21+ GError **error)
22+@@ -1559,6 +1569,9 @@ gdm_session_worker_accredit_user (GdmSessionWorker *worker,
23+ goto out;
24+ }
25+
26++ gdm_session_worker_forward_var(worker, "GDM_X_SERVER_EXTRA_ARGS");
27++ gdm_session_worker_forward_var(worker, "GDM_X_SESSION_WRAPPER");
28++
29+ gdm_session_worker_update_environment_from_passwd_info (worker,
30+ uid,
31+ gid,
pkgs/desktops/gnome-3/core/gdm/gdm-session-worker_xserver-path.patchdeleted−17
@@ -1,17 +0,0 @@
1−diff --git a/daemon/gdm-session-worker.c.orig b/daemon/gdm-session-worker.c
2−index 7bbda49..592691d 100644
3−--- a/daemon/gdm-session-worker.c.orig
4−+++ b/daemon/gdm-session-worker.c
5−@@ -1557,6 +1557,12 @@ gdm_session_worker_accredit_user (GdmSessionWorker *worker,
6− goto out;
7− }
8−
9−+ if (g_getenv ("GDM_X_SERVER_EXTRA_ARGS") != NULL) {
10−+ g_debug ("forwarding GDM_X_SERVER_EXTRA_ARGS= %s", g_getenv("GDM_X_SERVER_EXTRA_ARGS"));
11−+ gdm_session_worker_set_environment_variable (worker, "GDM_X_SERVER_EXTRA_ARGS",
12−+ g_getenv("GDM_X_SERVER_EXTRA_ARGS"));
13−+ }
14−+
15− gdm_session_worker_update_environment_from_passwd_info (worker,
16− uid,
17− gid,
pkgs/desktops/gnome-3/core/gdm/gdm-x-session_session-wrapper.patchnew file+40
@@ -0,0 +1,40 @@
1+diff --git a/daemon/gdm-x-session.c b/daemon/gdm-x-session.c
2+index 88fe96f..b1b140a 100644
3+--- a/daemon/gdm-x-session.c
4++++ b/daemon/gdm-x-session.c
5+@@ -664,18 +664,34 @@ spawn_session (State *state,
6+ state->session_command,
7+ NULL);
8+ } else {
9++ char const *session_wrapper;
10++ char *eff_session_command;
11+ int ret;
12+ char **argv;
13+
14+- ret = g_shell_parse_argv (state->session_command,
15++ session_wrapper = g_getenv("GDM_X_SESSION_WRAPPER");
16++ if (session_wrapper != NULL) {
17++ char *quoted_wrapper = g_shell_quote(session_wrapper);
18++ eff_session_command = g_strjoin(" ", quoted_wrapper, state->session_command, NULL);
19++ g_free(quoted_wrapper);
20++ } else {
21++ eff_session_command = state->session_command;
22++ }
23++
24++ ret = g_shell_parse_argv (eff_session_command,
25+ NULL,
26+ &argv,
27+ &error);
28+
29++ if (session_wrapper != NULL) {
30++ g_free(eff_session_command);
31++ }
32++
33+ if (!ret) {
34+ g_debug ("could not parse session arguments: %s", error->message);
35+ goto out;
36+ }
37++
38+ subprocess = g_subprocess_launcher_spawnv (launcher,
39+ (const char * const *) argv,
40+ &error);