@@ -55,13 +55,6 @@ let
5555 exec &> >(tee ~/.xsession-errors)
5656 ''}
5757
58− # Tell systemd about our $DISPLAY and $XAUTHORITY.
59− # This is needed by the ssh-agent unit.
60− #
61− # Also tell systemd about the dbus session bus address.
62− # This is required by user units using the session bus.
63− /run/current-system/systemd/bin/systemctl --user import-environment DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS
64−
6558 # Load X defaults. This should probably be safe on wayland too.
6659 ${xorg.xrdb}/bin/xrdb -merge ${xresourcesXft}
6760 if test -e ~/.Xresources; then
@@ -70,6 +63,12 @@ let
7063 ${xorg.xrdb}/bin/xrdb -merge ~/.Xdefaults
7164 fi
7265
66+ # Import environment variables into the systemd user environment.
67+ ${optionalString (cfg.displayManager.importedVariables != []) (
68+ "/run/current-system/systemd/bin/systemctl --user import-environment "
69+ + toString (unique cfg.displayManager.importedVariables)
70+ )}
71+
7372 # Speed up application start by 50-150ms according to
7473 # http://kdemonkey.blogspot.nl/2008/04/magic-trick.html
7574 rm -rf "$HOME/.compose-cache"
@@ -289,6 +288,14 @@ in
289288 '';
290289 };
291290
291+ importedVariables = mkOption {
292+ type = types.listOf (types.strMatching "[a-zA-Z_][a-zA-Z0-9_]*");
293+ visible = false;
294+ description = ''
295+ Environment variables to import into the systemd user environment.
296+ '';
297+ };
298+
292299 job = {
293300
294301 preStart = mkOption {
@@ -360,6 +367,16 @@ in
360367
361368 services.xserver.displayManager.xserverBin = "${xorg.xorgserver.out}/bin/X";
362369
370+ services.xserver.displayManager.importedVariables = [
371+ # This is required by user units using the session bus.
372+ "DBUS_SESSION_BUS_ADDRESS"
373+ # These are needed by the ssh-agent unit.
374+ "DISPLAY"
375+ "XAUTHORITY"
376+ # This is required to specify session within user units (e.g. loginctl lock-session).
377+ "XDG_SESSION_ID"
378+ ];
379+
363380 systemd.user.targets.graphical-session = {
364381 unitConfig = {
365382 RefuseManualStart = false;