activationScripts: cleanup the "nixos activated" graphical notification

This commit is contained in:
2024-09-19 10:05:56 +00:00
parent 717af4e5ff
commit b0edd56ba2

View File

@@ -1319,19 +1319,28 @@ in
}));
system.activationScripts.notifyActive = lib.mkIf config.sane.programs.guiApps.enabled {
text = lib.concatStringsSep "\n" ([
''
tryNotifyUser() {
local user="$1"
local new_path="$PATH:/etc/profiles/per-user/$user/bin:${pkgs.sudo}/bin:${pkgs.libnotify}/bin"
local version="$(cat $systemConfig/nixos-version)"
PATH="$new_path" sudo -u "$user" \
env PATH="$new_path" NIXOS_VERSION="$version" /bin/sh -c \
'. $HOME/.profile; dbus_file="$XDG_RUNTIME_DIR/bus"; if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -e "$dbus_file" ]; then export DBUS_SESSION_BUS_ADDRESS="unix:path=$dbus_file"; fi ; if [ -n "$DBUS_SESSION_BUS_ADDRESS" ]; then notify-send "nixos activated" "version: $NIXOS_VERSION" ; fi'
}
''
] ++ lib.mapAttrsToList
(user: en: lib.optionalString en "tryNotifyUser ${user} > /dev/null")
text = let
notify-active = pkgs.writeShellScriptBin "notify-active-inner" ''
export PATH="/etc/profiles/per-user/$USER/bin:$PATH:${pkgs.libnotify}/bin"
systemConfig="$1"
NIXOS_VERSION="$(cat $systemConfig/nixos-version)"
if [ -e "$HOME/.profile" ]; then
. "$HOME/.profile"
fi
dbus_file="$XDG_RUNTIME_DIR/bus"
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -e "$dbus_file" ]; then
export DBUS_SESSION_BUS_ADDRESS="unix:path=$dbus_file"
fi
if [ -n "$DBUS_SESSION_BUS_ADDRESS" ]; then
notify-send "nixos activated" "version: $NIXOS_VERSION"
fi
'';
in lib.concatStringsSep "\n" (lib.mapAttrsToList
(user: en: lib.optionalString en ''${lib.getExe pkgs.sudo} -u "${user}" "${lib.getExe notify-active}" "$systemConfig" > /dev/null'')
config.sane.programs.guiApps.enableFor.user
);
};