2018-01-13 12:41:08 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
let
|
2024-10-26 05:13:23 +00:00
|
|
|
|
inherit (lib) concatStringsSep concatMapStringsSep elem filter filterAttrs
|
|
|
|
|
mapAttrs' mapAttrsToList mkIf mkMerge mkOption mkOrder optionalString types;
|
|
|
|
|
|
2018-01-13 12:41:08 +00:00
|
|
|
|
cfg = config.users;
|
|
|
|
|
|
|
|
|
|
group = import ./group.nix;
|
|
|
|
|
user = import ./user.nix;
|
|
|
|
|
|
2020-02-11 15:41:40 +00:00
|
|
|
|
toGID = v: { "${toString v.gid}" = v.name; };
|
|
|
|
|
toUID = v: { "${toString v.uid}" = v.name; };
|
2018-01-14 12:26:18 +00:00
|
|
|
|
|
2018-01-13 12:41:08 +00:00
|
|
|
|
isCreated = list: name: elem name list;
|
|
|
|
|
isDeleted = attrs: name: ! elem name (mapAttrsToList (n: v: v.name) attrs);
|
|
|
|
|
|
2020-02-11 15:41:40 +00:00
|
|
|
|
gids = mapAttrsToList (n: toGID) (filterAttrs (n: v: isCreated cfg.knownGroups v.name) cfg.groups);
|
|
|
|
|
uids = mapAttrsToList (n: toUID) (filterAttrs (n: v: isCreated cfg.knownUsers v.name) cfg.users);
|
|
|
|
|
|
|
|
|
|
createdGroups = mapAttrsToList (n: v: cfg.groups."${v}") cfg.gids;
|
|
|
|
|
createdUsers = mapAttrsToList (n: v: cfg.users."${v}") cfg.uids;
|
2018-01-13 12:41:08 +00:00
|
|
|
|
deletedGroups = filter (n: isDeleted cfg.groups n) cfg.knownGroups;
|
|
|
|
|
deletedUsers = filter (n: isDeleted cfg.users n) cfg.knownUsers;
|
2019-02-23 10:43:39 +00:00
|
|
|
|
|
|
|
|
|
packageUsers = filterAttrs (_: u: u.packages != []) cfg.users;
|
2023-11-23 19:54:06 +00:00
|
|
|
|
|
|
|
|
|
# convert a valid argument to user.shell into a string that points to a shell
|
|
|
|
|
# executable. Logic copied from modules/system/shells.nix.
|
|
|
|
|
shellPath = v:
|
|
|
|
|
if types.shellPackage.check v
|
|
|
|
|
then "/run/current-system/sw${v.shellPath}"
|
|
|
|
|
else v;
|
|
|
|
|
|
2024-10-24 03:14:15 +00:00
|
|
|
|
systemShells =
|
|
|
|
|
let
|
|
|
|
|
shells = mapAttrsToList (_: u: u.shell) cfg.users;
|
|
|
|
|
in
|
|
|
|
|
filter types.shellPackage.check shells;
|
|
|
|
|
|
2018-01-13 12:41:08 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
2024-10-27 14:05:16 +00:00
|
|
|
|
imports = [
|
|
|
|
|
(lib.mkRemovedOptionModule [ "users" "forceRecreate" ] "")
|
|
|
|
|
];
|
|
|
|
|
|
2018-01-13 12:41:08 +00:00
|
|
|
|
options = {
|
|
|
|
|
users.knownGroups = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2018-03-03 12:51:48 +00:00
|
|
|
|
List of groups owned and managed by nix-darwin. Used to indicate
|
|
|
|
|
what users are safe to create/delete based on the configuration.
|
|
|
|
|
Don't add system groups to this.
|
|
|
|
|
'';
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.knownUsers = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2018-03-03 12:51:48 +00:00
|
|
|
|
List of users owned and managed by nix-darwin. Used to indicate
|
|
|
|
|
what users are safe to create/delete based on the configuration.
|
|
|
|
|
Don't add the admin user or other system users to this.
|
|
|
|
|
'';
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.groups = mkOption {
|
2020-09-02 04:20:00 +00:00
|
|
|
|
type = types.attrsOf (types.submodule group);
|
2018-01-13 12:41:08 +00:00
|
|
|
|
default = {};
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "Configuration for groups.";
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.users = mkOption {
|
2020-09-02 04:20:00 +00:00
|
|
|
|
type = types.attrsOf (types.submodule user);
|
2018-01-13 12:41:08 +00:00
|
|
|
|
default = {};
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "Configuration for users.";
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
2018-06-22 10:16:51 +00:00
|
|
|
|
|
2020-02-11 15:41:40 +00:00
|
|
|
|
users.gids = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.uids = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
2024-10-26 01:31:53 +00:00
|
|
|
|
assertions = [
|
|
|
|
|
{
|
|
|
|
|
# We don't check `root` like the rest of the users as on some systems `root`'s
|
|
|
|
|
# home directory is set to `/var/root /private/var/root`
|
|
|
|
|
assertion = cfg.users ? root -> (cfg.users.root.home == null || cfg.users.root.home == "/var/root");
|
|
|
|
|
message = "`users.users.root.home` must be set to either `null` or `/var/root`.";
|
|
|
|
|
}
|
|
|
|
|
];
|
2018-01-13 12:41:08 +00:00
|
|
|
|
|
2020-02-11 15:41:40 +00:00
|
|
|
|
users.gids = mkMerge gids;
|
|
|
|
|
users.uids = mkMerge uids;
|
|
|
|
|
|
2024-10-26 01:31:53 +00:00
|
|
|
|
# NOTE: We put this in `system.checks` as we want this to run first to avoid partial activations
|
|
|
|
|
# however currently that runs at user level activation as that runs before system level activation
|
|
|
|
|
# TODO: replace `$USER` with `$SUDO_USER` when system.checks runs from system level
|
|
|
|
|
system.checks.text = lib.mkAfter ''
|
2024-10-27 13:37:55 +00:00
|
|
|
|
ensurePerms() {
|
|
|
|
|
homeDirectory=$(dscl . -read /Users/nobody NFSHomeDirectory)
|
|
|
|
|
homeDirectory=''${homeDirectory#NFSHomeDirectory: }
|
2024-10-26 01:31:53 +00:00
|
|
|
|
|
2024-10-27 13:37:55 +00:00
|
|
|
|
if ! sudo dscl . -change /Users/nobody NFSHomeDirectory "$homeDirectory" "$homeDirectory" &> /dev/null; then
|
2024-10-26 01:31:53 +00:00
|
|
|
|
if [[ -n "$SSH_CONNECTION" ]]; then
|
2024-10-27 13:37:55 +00:00
|
|
|
|
printf >&2 '\e[1;31merror: users cannot be %s over SSH without Full Disk Access, aborting activation\e[0m\n' "$2"
|
|
|
|
|
printf >&2 'The user %s could not be %s as `darwin-rebuild` was not executed with Full Disk Access over SSH.\n' "$1" "$2"
|
|
|
|
|
printf >&2 'You can either:\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 ' grant Full Disk Access to all programs run over SSH\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'or\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 ' run `darwin-rebuild` in a graphical session.\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'The option "Allow full disk access for remote users" can be found by\n'
|
|
|
|
|
printf >&2 'navigating to System Settings > General > Sharing > Remote Login\n'
|
|
|
|
|
printf >&2 'and then pressing on the i icon next to the switch.\n'
|
|
|
|
|
exit 1
|
2024-10-26 01:31:53 +00:00
|
|
|
|
else
|
2024-10-27 13:37:55 +00:00
|
|
|
|
# The TCC service required to change home directories is `kTCCServiceSystemPolicySysAdminFiles`
|
|
|
|
|
# and we can reset it to ensure the user gets another prompt
|
|
|
|
|
tccutil reset SystemPolicySysAdminFiles > /dev/null
|
|
|
|
|
|
|
|
|
|
if ! sudo dscl . -change /Users/nobody NFSHomeDirectory "$homeDirectory" "$homeDirectory" &> /dev/null; then
|
|
|
|
|
printf >&2 '\e[1;31merror: permission denied when trying to %s user %s, aborting activation\e[0m\n' "$2" "$1"
|
|
|
|
|
printf >&2 '`darwin-rebuild` requires permissions to administrate your computer,\n' "$1" "$2"
|
|
|
|
|
printf >&2 'please accept the dialog that pops up.\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'If you do not wish to be prompted every time `darwin-rebuild updates your users,\n'
|
|
|
|
|
printf >&2 'you can grant Full Disk Access to your terminal emulator in System Settings.\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'This can be found in System Settings > Privacy & Security > Full Disk Access.\n'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2024-10-26 01:31:53 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
${concatMapStringsSep "\n" (v: let
|
|
|
|
|
name = lib.escapeShellArg v.name;
|
|
|
|
|
dsclUser = lib.escapeShellArg "/Users/${v.name}";
|
|
|
|
|
in ''
|
|
|
|
|
u=$(id -u ${name} 2> /dev/null) || true
|
|
|
|
|
if ! [[ -n "$u" && "$u" -ne "${toString v.uid}" ]]; then
|
|
|
|
|
if [ -z "$u" ]; then
|
2024-10-27 13:37:55 +00:00
|
|
|
|
ensurePerms ${name} create
|
2024-10-26 01:31:53 +00:00
|
|
|
|
|
2024-11-01 23:31:40 +00:00
|
|
|
|
${optionalString (v.home != null && v.name != "root") ''
|
|
|
|
|
else
|
|
|
|
|
homeDirectory=$(dscl . -read ${dsclUser} NFSHomeDirectory)
|
|
|
|
|
homeDirectory=''${homeDirectory#NFSHomeDirectory: }
|
|
|
|
|
if [[ ${lib.escapeShellArg v.home} != "$homeDirectory" ]]; then
|
|
|
|
|
printf >&2 '\e[1;31merror: config contains the wrong home directory for %s, aborting activation\e[0m\n' ${name}
|
|
|
|
|
printf >&2 'nix-darwin does not support changing the home directory of existing users.\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'Please set:\n'
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 ' users.users.%s.home = "%s";\n' ${name} "$homeDirectory"
|
|
|
|
|
printf >&2 '\n'
|
|
|
|
|
printf >&2 'or remove it from your configuration.\n'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
''}
|
|
|
|
|
fi
|
2024-10-26 01:31:53 +00:00
|
|
|
|
fi
|
|
|
|
|
'') createdUsers}
|
|
|
|
|
|
2024-10-27 14:05:16 +00:00
|
|
|
|
${concatMapStringsSep "\n" (v: let
|
|
|
|
|
name = lib.escapeShellArg v;
|
|
|
|
|
in ''
|
|
|
|
|
u=$(id -u ${name} 2> /dev/null) || true
|
2024-10-26 01:31:53 +00:00
|
|
|
|
if [ -n "$u" ]; then
|
|
|
|
|
if [ "$u" -gt 501 ]; then
|
2024-10-27 14:05:16 +00:00
|
|
|
|
# TODO: add `darwin.primaryUser` as well
|
|
|
|
|
if [[ ${name} == "$USER" ]]; then
|
|
|
|
|
printf >&2 '\e[1;31merror: refusing to delete the user calling `darwin-rebuild` (%s), aborting activation\e[0m\n', ${name}
|
|
|
|
|
exit 1
|
|
|
|
|
elif [[ ${name} == "root" ]]; then
|
|
|
|
|
printf >&2 '\e[1;31merror: refusing to delete `root`, aborting activation\e[0m\n'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
ensurePerms ${name} delete
|
2024-10-26 01:31:53 +00:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
'') deletedUsers}
|
|
|
|
|
'';
|
|
|
|
|
|
2018-01-13 12:41:08 +00:00
|
|
|
|
system.activationScripts.groups.text = mkIf (cfg.knownGroups != []) ''
|
|
|
|
|
echo "setting up groups..." >&2
|
|
|
|
|
|
2024-10-21 23:08:41 +00:00
|
|
|
|
${concatMapStringsSep "\n" (v: let
|
|
|
|
|
dsclGroup = lib.escapeShellArg "/Groups/${v.name}";
|
|
|
|
|
in ''
|
|
|
|
|
g=$(dscl . -read ${dsclGroup} PrimaryGroupID 2> /dev/null) || true
|
2018-01-13 12:41:08 +00:00
|
|
|
|
g=''${g#PrimaryGroupID: }
|
|
|
|
|
if [ -z "$g" ]; then
|
|
|
|
|
echo "creating group ${v.name}..." >&2
|
2024-10-21 23:08:41 +00:00
|
|
|
|
dscl . -create ${dsclGroup} PrimaryGroupID ${toString v.gid}
|
2024-10-21 23:24:06 +00:00
|
|
|
|
dscl . -create ${dsclGroup} RealName ${lib.escapeShellArg v.description}
|
2018-01-14 12:26:18 +00:00
|
|
|
|
g=${toString v.gid}
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$g" -eq ${toString v.gid} ]; then
|
2024-10-21 23:08:41 +00:00
|
|
|
|
g=$(dscl . -read ${dsclGroup} GroupMembership 2> /dev/null) || true
|
2018-01-14 12:26:18 +00:00
|
|
|
|
if [ "$g" != 'GroupMembership: ${concatStringsSep " " v.members}' ]; then
|
|
|
|
|
echo "updating group members ${v.name}..." >&2
|
2024-10-21 23:08:41 +00:00
|
|
|
|
dscl . -create ${dsclGroup} GroupMembership ${lib.escapeShellArgs v.members}
|
2018-01-13 12:41:08 +00:00
|
|
|
|
fi
|
2018-01-14 12:26:18 +00:00
|
|
|
|
else
|
|
|
|
|
echo "[1;31mwarning: existing group '${v.name}' has unexpected gid $g, skipping...[0m" >&2
|
2018-01-13 12:41:08 +00:00
|
|
|
|
fi
|
|
|
|
|
'') createdGroups}
|
|
|
|
|
|
2024-10-21 23:08:41 +00:00
|
|
|
|
${concatMapStringsSep "\n" (name: let
|
|
|
|
|
dsclGroup = lib.escapeShellArg "/Groups/${name}";
|
|
|
|
|
in ''
|
|
|
|
|
g=$(dscl . -read ${dsclGroup} PrimaryGroupID 2> /dev/null) || true
|
2018-01-13 12:41:08 +00:00
|
|
|
|
g=''${g#PrimaryGroupID: }
|
|
|
|
|
if [ -n "$g" ]; then
|
|
|
|
|
if [ "$g" -gt 501 ]; then
|
|
|
|
|
echo "deleting group ${name}..." >&2
|
2024-10-27 13:37:55 +00:00
|
|
|
|
dscl . -delete ${dsclGroup}
|
2018-01-13 12:41:08 +00:00
|
|
|
|
else
|
|
|
|
|
echo "[1;31mwarning: existing group '${name}' has unexpected gid $g, skipping...[0m" >&2
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
'') deletedGroups}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
system.activationScripts.users.text = mkIf (cfg.knownUsers != []) ''
|
|
|
|
|
echo "setting up users..." >&2
|
|
|
|
|
|
2024-10-21 23:08:41 +00:00
|
|
|
|
${concatMapStringsSep "\n" (v: let
|
2024-10-21 23:20:43 +00:00
|
|
|
|
name = lib.escapeShellArg v.name;
|
2024-10-21 23:08:41 +00:00
|
|
|
|
dsclUser = lib.escapeShellArg "/Users/${v.name}";
|
|
|
|
|
in ''
|
2024-10-21 23:20:43 +00:00
|
|
|
|
u=$(id -u ${name} 2> /dev/null) || true
|
2023-11-24 01:11:18 +00:00
|
|
|
|
if [[ -n "$u" && "$u" -ne "${toString v.uid}" ]]; then
|
|
|
|
|
echo "[1;31mwarning: existing user '${v.name}' has unexpected uid $u, skipping...[0m" >&2
|
2018-01-13 12:41:08 +00:00
|
|
|
|
else
|
2023-11-24 01:11:18 +00:00
|
|
|
|
if [ -z "$u" ]; then
|
|
|
|
|
echo "creating user ${v.name}..." >&2
|
2024-10-24 02:13:52 +00:00
|
|
|
|
|
2024-10-24 12:19:27 +00:00
|
|
|
|
sysadminctl -addUser ${lib.escapeShellArgs ([
|
|
|
|
|
v.name
|
|
|
|
|
"-UID" v.uid
|
|
|
|
|
"-GID" v.gid ]
|
|
|
|
|
++ (lib.optionals (v.description != null) [ "-fullName" v.description ])
|
2024-10-27 23:30:02 +00:00
|
|
|
|
++ [ "-home" (if v.home != null then v.home else "/var/empty") ]
|
2024-10-24 14:16:19 +00:00
|
|
|
|
++ [ "-shell" (if v.shell != null then shellPath v.shell else "/usr/bin/false") ])} 2> /dev/null
|
2024-10-24 02:13:52 +00:00
|
|
|
|
|
|
|
|
|
# We need to check as `sysadminctl -addUser` still exits with exit code 0 when there's an error
|
|
|
|
|
if ! id ${name} &> /dev/null; then
|
|
|
|
|
printf >&2 '\e[1;31merror: failed to create user %s, aborting activation\e[0m\n' ${name}
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2024-10-21 23:08:41 +00:00
|
|
|
|
dscl . -create ${dsclUser} IsHidden ${if v.isHidden then "1" else "0"}
|
2024-10-24 12:19:27 +00:00
|
|
|
|
|
|
|
|
|
# `sysadminctl -addUser` won't create the home directory if we use the `-home`
|
|
|
|
|
# flag so we need to do it ourselves
|
|
|
|
|
${optionalString (v.home != null && v.createHome) "createhomedir -cu ${name} > /dev/null"}
|
2018-01-13 12:41:08 +00:00
|
|
|
|
fi
|
2024-10-24 11:58:35 +00:00
|
|
|
|
|
|
|
|
|
# Update properties on known users to keep them inline with configuration
|
|
|
|
|
dscl . -create ${dsclUser} PrimaryGroupID ${toString v.gid}
|
|
|
|
|
${optionalString (v.description != null) "dscl . -create ${dsclUser} RealName ${lib.escapeShellArg v.description}"}
|
2024-10-24 14:16:19 +00:00
|
|
|
|
${optionalString (v.shell != null) "dscl . -create ${dsclUser} UserShell ${lib.escapeShellArg (shellPath v.shell)}"}
|
2018-01-13 12:41:08 +00:00
|
|
|
|
fi
|
|
|
|
|
'') createdUsers}
|
|
|
|
|
|
|
|
|
|
${concatMapStringsSep "\n" (name: ''
|
2024-10-21 19:58:23 +00:00
|
|
|
|
u=$(id -u ${lib.escapeShellArg name} 2> /dev/null) || true
|
2018-01-13 12:41:08 +00:00
|
|
|
|
if [ -n "$u" ]; then
|
|
|
|
|
if [ "$u" -gt 501 ]; then
|
|
|
|
|
echo "deleting user ${name}..." >&2
|
2024-10-27 13:37:55 +00:00
|
|
|
|
dscl . -delete ${lib.escapeShellArg "/Users/${name}"}
|
2018-01-13 12:41:08 +00:00
|
|
|
|
else
|
|
|
|
|
echo "[1;31mwarning: existing user '${name}' has unexpected uid $u, skipping...[0m" >&2
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
'') deletedUsers}
|
|
|
|
|
'';
|
|
|
|
|
|
2024-10-24 03:14:15 +00:00
|
|
|
|
# Install all the user shells
|
|
|
|
|
environment.systemPackages = systemShells;
|
|
|
|
|
|
2019-02-20 16:20:32 +00:00
|
|
|
|
environment.etc = mapAttrs' (name: { packages, ... }: {
|
|
|
|
|
name = "profiles/per-user/${name}";
|
|
|
|
|
value.source = pkgs.buildEnv {
|
|
|
|
|
name = "user-environment";
|
|
|
|
|
paths = packages;
|
|
|
|
|
inherit (config.environment) pathsToLink extraOutputsToInstall;
|
|
|
|
|
inherit (config.system.path) postBuild;
|
|
|
|
|
};
|
2019-02-23 10:43:39 +00:00
|
|
|
|
}) packageUsers;
|
2019-02-20 16:20:32 +00:00
|
|
|
|
|
2019-02-23 10:43:39 +00:00
|
|
|
|
environment.profiles = mkIf (packageUsers != {}) (mkOrder 900 [ "/etc/profiles/per-user/$USER" ]);
|
2018-01-13 12:41:08 +00:00
|
|
|
|
};
|
|
|
|
|
}
|