#!/nix/store/qsydfxm1vq6q9jac2kq3r8kn0xdmsldf-bash-5.3p3/bin/bash
set -e

declare -a lingeringUserNames=()
declare -a nonLingeringUserNames=()

user_configured () {
    # Use `id` to check if the user exists rather than checking the
    # NixOS configuration, as it may be that the user has been
    # manually configured, which is permitted if users.mutableUsers
    # is true (the default).
    id "$1" >/dev/null
}

shopt -s dotglob nullglob
for user in *; do
    if ! user_configured "$user"; then
        # systemd has this user configured to linger despite them not
        # existing.
        echo "Removing linger for missing user $user" >&2
        rm -- "$user"
    fi
done

if (( ${#nonLingeringUserNames[*]} > 0 )); then
    /nix/store/2gcl8s7879s6pa9jyvvf2n3ak0f3wpfk-systemd-258.1/bin/loginctl disable-linger "${nonLingeringUserNames[@]}"
fi
if (( ${#lingeringUserNames[*]} > 0 )); then
    /nix/store/2gcl8s7879s6pa9jyvvf2n3ak0f3wpfk-systemd-258.1/bin/loginctl enable-linger "${lingeringUserNames[@]}"
fi


