git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #42846 from ambrop72/optimus-prime-config-master

nixos/xserver: Implement configuration of NVIDIA Optimus via PRIME

commit1ffe83caa7c521366780233f02c1ae092a20a782 authorMatthew Bauer <mjbauer95@gmail.com> dateWed 3 Oct 2018, 22:56 (8 years ago) parent06961ccf261537
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
@@ -222,6 +222,17 @@ in
222222 description = "List of arguments for the X server.";
223223 };
224224
225+ setupCommands = mkOption {
226+ type = types.lines;
227+ default = "";
228+ description = ''
229+ Shell commands executed just after the X server has started.
230+
231+ This option is only effective for display managers for which this feature
232+ is supported; currently these are LightDM, GDM and SDDM.
233+ '';
234+ };
235+
225236 sessionCommands = mkOption {
226237 type = types.lines;
227238 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}/share/xsessions";
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
@@ -62,6 +62,12 @@ let
6262 ${optionalString hasDefaultUserSession ''
6363 user-session=${defaultSessionName}
6464 ''}
65+ ${optionalString (dmcfg.setupCommands != "") ''
66+ display-setup-script=${pkgs.writeScript "lightdm-display-setup" ''
67+ #!${pkgs.bash}/bin/bash
68+ ${dmcfg.setupCommands}
69+ ''}
70+ ''}
6571 ${cfg.extraSeatDefaults}
6672 '';
6773
nixos/modules/services/x11/display-managers/sddm.nix+3−1
@@ -20,6 +20,7 @@ let
2020 Xsetup = pkgs.writeScript "Xsetup" ''
2121 #!/bin/sh
2222 ${cfg.setupScript}
23+ ${dmcfg.setupCommands}
2324 '';
2425
2526 Xstop = pkgs.writeScript "Xstop" ''
@@ -137,7 +138,8 @@ in
137138 xrandr --auto
138139 '';
139140 description = ''
140− A script to execute when starting the display server.
141+ A script to execute when starting the display server. DEPRECATED, please
142+ use <option>services.xserver.displayManager.setupCommands</option>.
141143 '';
142144 };
143145
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 = [
@@ -754,6 +760,7 @@ in
754760 Driver "${driver.driverName or driver.name}"
755761 ${if cfg.useGlamor then ''Option "AccelMethod" "glamor"'' else ""}
756762 ${cfg.deviceSection}
763+ ${driver.deviceSection or ""}
757764 ${xrandrDeviceSection}
758765 EndSection
759766
@@ -765,6 +772,7 @@ in
765772 ''}
766773
767774 ${cfg.screenSection}
775+ ${driver.screenSection or ""}
768776
769777 ${optionalString (cfg.defaultDepth != 0) ''
770778 DefaultDepth ${toString cfg.defaultDepth}
@@ -794,6 +802,8 @@ in
794802 '')}
795803
796804 ${xrandrMonitorSections}
805+
806+ ${cfg.extraConfig}
797807 '';
798808
799809 fonts.enableDefaultFonts = mkDefault true;
pkgs/desktops/gnome-3/core/gdm/default.nix+15−1
@@ -37,13 +37,27 @@ stdenv.mkDerivation rec {
3737
3838 # Disable Access Control because our X does not support FamilyServerInterpreted yet
3939 patches = [
40+ # Change hardcoded paths to nix store paths.
4041 (substituteAll {
4142 src = ./fix-paths.patch;
4243 inherit coreutils plymouth xwayland;
4344 })
45+
46+ # The following patches implement certain environment variables in GDM which are set by
47+ # the gdm configuration module (nixos/modules/services/x11/display-managers/gdm.nix).
48+
49+ # Look for session definition files in the directory specified by GDM_SESSIONS_DIR.
4450 ./sessions_dir.patch
51+
52+ # Allow specifying X server arguments with GDM_X_SERVER_EXTRA_ARGS.
4553 ./gdm-x-session_extra_args.patch
46− ./gdm-session-worker_xserver-path.patch
54+
55+ # Allow specifying a wrapper for running the session command.
56+ ./gdm-x-session_session-wrapper.patch
57+
58+ # Forwards certain environment variables to the gdm-x-session child process
59+ # to ensure that the above two patches actually work.
60+ ./gdm-session-worker_forward-vars.patch
4761 ];
4862
4963 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);