@@ -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 {
@@ -393,6 +400,16 @@ in
393400
394401 services.xserver.displayManager.xserverBin = "${xorg.xorgserver.out}/bin/X";
395402
403+ services.xserver.displayManager.importedVariables = [
404+ # This is required by user units using the session bus.
405+ "DBUS_SESSION_BUS_ADDRESS"
406+ # These are needed by the ssh-agent unit.
407+ "DISPLAY"
408+ "XAUTHORITY"
409+ # This is required to specify session within user units (e.g. loginctl lock-session).
410+ "XDG_SESSION_ID"
411+ ];
412+
396413 systemd.user.targets.graphical-session = {
397414 unitConfig = {
398415 RefuseManualStart = false;