From b0651cc2173427857b172604f85da6afe69e1d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Fri, 18 Jun 2021 11:52:20 +0200 Subject: [PATCH] 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. --- nixos/default.nix | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/nixos/default.nix b/nixos/default.nix index 626b66d9..dc45c989 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -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; };