#!/nix/store/f15k3dpilmiyv6zgpib289rnjykgr1r4-bash-5.3p9/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/5j9cg6adv3d2l403fcfklz4lmf9fd0l8-systemd-258.3/bin/loginctl disable-linger "${nonLingeringUserNames[@]}"
fi
if (( ${#lingeringUserNames[*]} > 0 )); then
    /nix/store/5j9cg6adv3d2l403fcfklz4lmf9fd0l8-systemd-258.3/bin/loginctl enable-linger "${lingeringUserNames[@]}"
fi


