diff --git a/modules/misc/qt.nix b/modules/misc/qt.nix index bb59044dd..60de87742 100644 --- a/modules/misc/qt.nix +++ b/modules/misc/qt.nix @@ -65,8 +65,7 @@ in then [ pkgs.qgnomeplatform ] else [ pkgs.libsForQt5.qtstyleplugins ]; - xsession.profileExtra = - "systemctl --user import-environment QT_QPA_PLATFORMTHEME"; + xsession.importedVariables = [ "QT_QPA_PLATFORMTHEME" ]; # Enable GTK+ style for Qt4 in either case. # It doesn’t support the platform theme packages. diff --git a/modules/services/taffybar.nix b/modules/services/taffybar.nix index 23af231ef..eb71dd054 100644 --- a/modules/services/taffybar.nix +++ b/modules/services/taffybar.nix @@ -42,5 +42,7 @@ in WantedBy = [ "graphical-session.target" ]; }; }; + + xsession.importedVariables = [ "GDK_PIXBUF_MODULE_FILE" ]; }; } diff --git a/modules/xsession.nix b/modules/xsession.nix index 42fe45bb1..8202e0259 100644 --- a/modules/xsession.nix +++ b/modules/xsession.nix @@ -62,10 +62,31 @@ in default = ""; description = "Extra shell commands to run during initialization."; }; + + importedVariables = mkOption { + type = types.listOf (types.strMatching "[a-zA-Z_][a-zA-Z0-9_]*"); + example = [ "GDK_PIXBUF_ICON_LOADER" ]; + visible = false; + description = '' + Environment variables to import into the user systemd + session. The will be available for use by graphical + services. + ''; + }; }; }; config = mkIf cfg.enable { + xsession.importedVariables = [ + "DBUS_SESSION_BUS_ADDRESS" + "DISPLAY" + "SSH_AUTH_SOCK" + "XAUTHORITY" + "XDG_DATA_DIRS" + "XDG_RUNTIME_DIR" + "XDG_SESSION_ID" + ]; + systemd.user = { services = mkIf (config.home.keyboard != null) { setxkbmap = { @@ -118,13 +139,10 @@ in # script starts up graphical-session.target. systemctl --user stop graphical-session.target graphical-session-pre.target - systemctl --user import-environment DBUS_SESSION_BUS_ADDRESS - systemctl --user import-environment DISPLAY - systemctl --user import-environment SSH_AUTH_SOCK - systemctl --user import-environment XAUTHORITY - systemctl --user import-environment XDG_DATA_DIRS - systemctl --user import-environment XDG_RUNTIME_DIR - systemctl --user import-environment XDG_SESSION_ID + ${optionalString (cfg.importedVariables != []) ( + "systemctl --user import-environment " + + toString (unique cfg.importedVariables) + )} ${cfg.profileExtra} diff --git a/tests/default.nix b/tests/default.nix index 3ef1fa43e..8009b90c9 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -33,6 +33,7 @@ import nmt { i3-keybindings = ./modules/services/window-managers/i3-keybindings.nix; } // import ./modules/misc/pam + // import ./modules/misc/xsession // import ./modules/systemd ) // import ./modules/home-environment diff --git a/tests/modules/misc/xsession/basic-xprofile-expected.txt b/tests/modules/misc/xsession/basic-xprofile-expected.txt new file mode 100644 index 000000000..4fa93f97e --- /dev/null +++ b/tests/modules/misc/xsession/basic-xprofile-expected.txt @@ -0,0 +1,16 @@ +. "/test-home/.nix-profile/etc/profile.d/hm-session-vars.sh" + +if [[ -e "$HOME/.profile" ]]; then + . "$HOME/.profile" +fi + +# If there are any running services from a previous session. +# Need to run this in xprofile because the NixOS xsession +# script starts up graphical-session.target. +systemctl --user stop graphical-session.target graphical-session-pre.target + +systemctl --user import-environment DBUS_SESSION_BUS_ADDRESS DISPLAY SSH_AUTH_SOCK XAUTHORITY XDG_DATA_DIRS XDG_RUNTIME_DIR XDG_SESSION_ID EXTRA_IMPORTED_VARIABLE + +profile extra commands + +export HM_XPROFILE_SOURCED=1 diff --git a/tests/modules/misc/xsession/basic-xsession-expected.txt b/tests/modules/misc/xsession/basic-xsession-expected.txt new file mode 100644 index 000000000..20d7c4998 --- /dev/null +++ b/tests/modules/misc/xsession/basic-xsession-expected.txt @@ -0,0 +1,18 @@ +if [[ ! -v HM_XPROFILE_SOURCED ]]; then + . ~/.xprofile +fi +unset HM_XPROFILE_SOURCED + +systemctl --user start hm-graphical-session.target + +init extra commands + +window manager command + +systemctl --user stop graphical-session.target +systemctl --user stop graphical-session-pre.target + +# Wait until the units actually stop. +while [[ -n "$(systemctl --user --no-legend --state=deactivating list-units)" ]]; do + sleep 0.5 +done diff --git a/tests/modules/misc/xsession/basic.nix b/tests/modules/misc/xsession/basic.nix new file mode 100644 index 000000000..81329afce --- /dev/null +++ b/tests/modules/misc/xsession/basic.nix @@ -0,0 +1,30 @@ +{ config, lib, ... }: + +with lib; + +{ + config = { + home.homeDirectory = "/test-home"; + + xsession = { + enable = true; + windowManager.command = "window manager command"; + importedVariables = [ "EXTRA_IMPORTED_VARIABLE" ]; + initExtra = "init extra commands"; + profileExtra = "profile extra commands"; + }; + + nmt.script = '' + assertFileExists home-files/.xprofile + assertFileContent \ + home-files/.xprofile \ + ${./basic-xprofile-expected.txt} + + assertFileExists home-files/.xsession + assertFileContent \ + home-files/.xsession \ + ${./basic-xsession-expected.txt} + + ''; + }; +} diff --git a/tests/modules/misc/xsession/default.nix b/tests/modules/misc/xsession/default.nix new file mode 100644 index 000000000..fdacd3bbc --- /dev/null +++ b/tests/modules/misc/xsession/default.nix @@ -0,0 +1,3 @@ +{ + xsession-basic = ./basic.nix; +}