diff options
| author | Michael Hoang <Enzime@users.noreply.github.com> | 2024-10-24 01:33:54 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-24 01:33:54 +1100 |
| commit | d99f9ae9fdfbcc36b81d264678bf58004464892e (patch) | |
| tree | 6b52dda4ea719baf6484cc128d84f491949de4ec /modules/users/default.nix | |
| parent | 64d9d1ae25215c274c37e3e4016977a6779cf0d3 (diff) | |
| parent | 8c02940d702170feea7947f768aa807c11b65a41 (diff) | |
Merge pull request #1108 from Enzime/users
Use `sysadminctl` instead of `dscl`
Diffstat (limited to 'modules/users/default.nix')
| -rw-r--r-- | modules/users/default.nix | 92 |
1 files changed, 64 insertions, 28 deletions
diff --git a/modules/users/default.nix b/modules/users/default.nix index cd0986d..ce77d4d 100644 --- a/modules/users/default.nix +++ b/modules/users/default.nix @@ -8,7 +8,6 @@ let group = import ./group.nix; user = import ./user.nix; - toArguments = concatMapStringsSep " " (v: "'${v}'"); toGID = v: { "${toString v.gid}" = v.name; }; toUID = v: { "${toString v.uid}" = v.name; }; @@ -96,45 +95,49 @@ in system.activationScripts.groups.text = mkIf (cfg.knownGroups != []) '' echo "setting up groups..." >&2 - ${concatMapStringsSep "\n" (v: '' + ${concatMapStringsSep "\n" (v: let + dsclGroup = lib.escapeShellArg "/Groups/${v.name}"; + in '' ${optionalString cfg.forceRecreate '' - g=$(dscl . -read '/Groups/${v.name}' PrimaryGroupID 2> /dev/null) || true + g=$(dscl . -read ${dsclGroup} PrimaryGroupID 2> /dev/null) || true g=''${g#PrimaryGroupID: } if [[ "$g" -eq ${toString v.gid} ]]; then echo "deleting group ${v.name}..." >&2 - dscl . -delete '/Groups/${v.name}' 2> /dev/null + dscl . -delete ${dsclGroup} 2> /dev/null else echo "[1;31mwarning: existing group '${v.name}' has unexpected gid $g, skipping...[0m" >&2 fi ''} - g=$(dscl . -read '/Groups/${v.name}' PrimaryGroupID 2> /dev/null) || true + g=$(dscl . -read ${dsclGroup} PrimaryGroupID 2> /dev/null) || true g=''${g#PrimaryGroupID: } if [ -z "$g" ]; then echo "creating group ${v.name}..." >&2 - dscl . -create '/Groups/${v.name}' PrimaryGroupID ${toString v.gid} - dscl . -create '/Groups/${v.name}' RealName '${v.description}' + dscl . -create ${dsclGroup} PrimaryGroupID ${toString v.gid} + dscl . -create ${dsclGroup} RealName ${lib.escapeShellArg v.description} g=${toString v.gid} fi if [ "$g" -eq ${toString v.gid} ]; then - g=$(dscl . -read '/Groups/${v.name}' GroupMembership 2> /dev/null) || true + g=$(dscl . -read ${dsclGroup} GroupMembership 2> /dev/null) || true if [ "$g" != 'GroupMembership: ${concatStringsSep " " v.members}' ]; then echo "updating group members ${v.name}..." >&2 - dscl . -create '/Groups/${v.name}' GroupMembership ${toArguments v.members} + dscl . -create ${dsclGroup} GroupMembership ${lib.escapeShellArgs v.members} fi else echo "[1;31mwarning: existing group '${v.name}' has unexpected gid $g, skipping...[0m" >&2 fi '') createdGroups} - ${concatMapStringsSep "\n" (name: '' - g=$(dscl . -read '/Groups/${name}' PrimaryGroupID 2> /dev/null) || true + ${concatMapStringsSep "\n" (name: let + dsclGroup = lib.escapeShellArg "/Groups/${name}"; + in '' + g=$(dscl . -read ${dsclGroup} PrimaryGroupID 2> /dev/null) || true g=''${g#PrimaryGroupID: } if [ -n "$g" ]; then if [ "$g" -gt 501 ]; then echo "deleting group ${name}..." >&2 - dscl . -delete '/Groups/${name}' 2> /dev/null + dscl . -delete ${dsclGroup} 2> /dev/null else echo "[1;31mwarning: existing group '${name}' has unexpected gid $g, skipping...[0m" >&2 fi @@ -145,44 +148,77 @@ in system.activationScripts.users.text = mkIf (cfg.knownUsers != []) '' echo "setting up users..." >&2 - ${concatMapStringsSep "\n" (v: '' + deleteUser() { + fullDiskAccess=false + + if cat /Library/Preferences/com.apple.TimeMachine.plist > /dev/null 2>&1; then + fullDiskAccess=true + fi + + if [[ "$fullDiskAccess" != true ]]; then + printf >&2 '\e[1;31merror: users cannot be deleted without Full Disk Access, aborting activation\e[0m\n' + printf >&2 'The user %s could not be deleted as `darwin-rebuild` was not executed with Full Disk Access.' "$1" + + printf >&2 'Opening "Privacy & Security" > "Full Disk Access" in System Settings\n' + printf >&2 '\n' + # This command will fail if run as root and System Settings is already running + # even if System Settings was launched by root. + sudo -u $SUDO_USER open "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles" + + if [[ -n "$SSH_CONNECTION" ]]; then + printf >&2 'Please enable Full Disk Access for programs over SSH by flipping\n' + printf >&2 'the switch for `sshd-keygen-wrapper`.\n' + else + printf >&2 'Please enable Full Disk Access for your terminal emulator by flipping\n' + printf >&2 'the switch in System Settings.\n' + fi + + exit 1 + fi + + sysadminctl -deleteUser "$1" 2> /dev/null + + if id -u "$1" > /dev/null 2>&1; then + printf >&2 '\e[1;31merror: failed to delete user %s, aborting activation\e[0m\n', "$1" + exit 1 + fi + } + + ${concatMapStringsSep "\n" (v: let + name = lib.escapeShellArg v.name; + dsclUser = lib.escapeShellArg "/Users/${v.name}"; + in '' ${optionalString cfg.forceRecreate '' - u=$(dscl . -read '/Users/${v.name}' UniqueID 2> /dev/null) || true - u=''${u#UniqueID: } + u=$(id -u ${name} 2> /dev/null) || true if [[ "$u" -eq ${toString v.uid} ]]; then echo "deleting user ${v.name}..." >&2 - dscl . -delete '/Users/${v.name}' 2> /dev/null + deleteUser ${name} else echo "[1;31mwarning: existing user '${v.name}' has unexpected uid $u, skipping...[0m" >&2 fi ''} - u=$(dscl . -read '/Users/${v.name}' UniqueID 2> /dev/null) || true - u=''${u#UniqueID: } + u=$(id -u ${name} 2> /dev/null) || true if [[ -n "$u" && "$u" -ne "${toString v.uid}" ]]; then echo "[1;31mwarning: existing user '${v.name}' has unexpected uid $u, skipping...[0m" >&2 else if [ -z "$u" ]; then echo "creating user ${v.name}..." >&2 - dscl . -create '/Users/${v.name}' UniqueID ${toString v.uid} - dscl . -create '/Users/${v.name}' PrimaryGroupID ${toString v.gid} - dscl . -create '/Users/${v.name}' IsHidden ${if v.isHidden then "1" else "0"} - dscl . -create '/Users/${v.name}' RealName '${v.description}' - dscl . -create '/Users/${v.name}' NFSHomeDirectory '${v.home}' - ${optionalString v.createHome "createhomedir -cu '${v.name}'"} + sysadminctl -addUser ${lib.escapeShellArgs ([ v.name "-UID" v.uid "-GID" v.gid ] ++ (lib.optionals (v.description != null) [ "-fullName" v.description ]) ++ [ "-home" v.home "-shell" (shellPath v.shell) ])} + dscl . -create ${dsclUser} IsHidden ${if v.isHidden then "1" else "0"} + ${optionalString v.createHome "createhomedir -cu ${name}"} fi # Always set the shell path, in case it was updated - dscl . -create '/Users/${v.name}' UserShell ${lib.escapeShellArg (shellPath v.shell)} + dscl . -create ${dsclUser} UserShell ${lib.escapeShellArg (shellPath v.shell)} fi '') createdUsers} ${concatMapStringsSep "\n" (name: '' - u=$(dscl . -read '/Users/${name}' UniqueID 2> /dev/null) || true - u=''${u#UniqueID: } + u=$(id -u ${lib.escapeShellArg name} 2> /dev/null) || true if [ -n "$u" ]; then if [ "$u" -gt 501 ]; then echo "deleting user ${name}..." >&2 - dscl . -delete '/Users/${name}' 2> /dev/null + deleteUser ${lib.escapeShellArg name} else echo "[1;31mwarning: existing user '${name}' has unexpected uid $u, skipping...[0m" >&2 fi |
