nixos: import existing environment during activation

If the user has a running systemd session, source their environment
from the systemd manager and export a few variables in order to allow
activation scripts to reload applications on the fly.

The list of variables to export is arbitrary and could be extended in
the future.

Fixes #1399, fixes #2112.
This commit is contained in:
Naïm Favier 2021-06-18 11:52:20 +02:00 committed by Robert Helgesson
parent eee807560b
commit b0651cc217
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89

View file

@ -144,10 +144,35 @@ in {
TimeoutStartSec = 90;
SyslogIdentifier = "hm-activate-${username}";
# The activation script is run by a login shell to make sure
# that the user is given a sane Nix environment.
ExecStart =
"${pkgs.runtimeShell} -l ${usercfg.home.activationPackage}/activate";
ExecStart = let
systemctl =
"XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/run/user/$UID} systemctl";
sed = "${pkgs.gnused}/bin/sed";
exportedSystemdVariables = concatStringsSep "|" [
"DBUS_SESSION_BUS_ADDRESS"
"DISPLAY"
"WAYLAND_DISPLAY"
"XAUTHORITY"
"XDG_RUNTIME_DIR"
];
setupEnv = pkgs.writeScript "hm-setup-env" ''
#! ${pkgs.runtimeShell} -el
# The activation script is run by a login shell to make sure
# that the user is given a sane environment.
# If the user is logged in, import variables from their current
# session environment.
eval "$(
${systemctl} --user show-environment 2> /dev/null \
| ${sed} -En '/^(${exportedSystemdVariables})=/s/^/export /p'
)"
exec "$1/activate"
'';
in "${setupEnv} ${usercfg.home.activationPackage}";
};
}) cfg.users;
};