2009-01-02 17:07:01 +01:00
|
|
|
{pkgs, config, ...}:
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
with pkgs.lib;
|
2009-01-02 17:07:01 +01:00
|
|
|
|
|
|
|
let
|
2009-09-02 19:35:24 +02:00
|
|
|
|
2009-05-29 16:25:56 +02:00
|
|
|
ids = config.ids;
|
2011-11-29 07:08:55 +01:00
|
|
|
users = config.users;
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2011-11-29 07:08:55 +01:00
|
|
|
userOpts = {name, config, ...}:
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-11-29 07:08:55 +01:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = with types; uniq string;
|
|
|
|
description = "The name of the user account. If undefined, the name of the attribute set will be used.";
|
|
|
|
};
|
|
|
|
description = mkOption {
|
|
|
|
type = with types; uniq string;
|
|
|
|
default = "";
|
|
|
|
description = "A short description of the user account.";
|
|
|
|
};
|
|
|
|
uid = mkOption {
|
|
|
|
type = with types; uniq (nullOr int);
|
|
|
|
default = null;
|
|
|
|
description = "The account UID. If undefined, NixOS will select a UID.";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = with types; uniq string;
|
|
|
|
default = "nogroup";
|
|
|
|
description = "The user's primary group.";
|
|
|
|
};
|
|
|
|
extraGroups = mkOption {
|
|
|
|
type = types.listOf types.string;
|
|
|
|
default = [];
|
|
|
|
description = "The user's auxiliary groups.";
|
|
|
|
};
|
|
|
|
home = mkOption {
|
|
|
|
type = with types; uniq string;
|
|
|
|
default = "/var/empty";
|
|
|
|
description = "The user's home directory.";
|
|
|
|
};
|
|
|
|
shell = mkOption {
|
|
|
|
type = with types; uniq string;
|
|
|
|
default = "/noshell";
|
|
|
|
description = "The path to the user's shell.";
|
|
|
|
};
|
|
|
|
createHome = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "If true, the home directory will be created automatically.";
|
|
|
|
};
|
|
|
|
useDefaultShell = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "If true, the user's shell will be set to <literal>users.defaultUserShell</literal>.";
|
|
|
|
};
|
|
|
|
password = mkOption {
|
|
|
|
type = with types; uniq (nullOr string);
|
|
|
|
default = null;
|
|
|
|
description = "The user's password. If undefined, no password is set for the user. Warning: do not set confidential information here because this data would be readable by all. This option should only be used for public account such as guest.";
|
|
|
|
};
|
|
|
|
isSystemUser = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Indicates if the user is a system user or not.";
|
|
|
|
};
|
|
|
|
createUser = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "
|
|
|
|
Indicates if the user should be created automatically as a local user.
|
|
|
|
Set this to false if the user for instance is an LDAP user. NixOS will
|
|
|
|
then not modify any of the basic properties for the user account.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
};
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2011-11-29 07:08:55 +01:00
|
|
|
config = {
|
|
|
|
name = mkDefault name;
|
|
|
|
uid = mkDefault (attrByPath [name] null ids.uids);
|
|
|
|
shell = mkIf config.useDefaultShell (mkDefault users.defaultUserShell);
|
|
|
|
};
|
|
|
|
};
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2007-11-16 14:26:34 +01:00
|
|
|
# Groups to be created/updated by NixOS.
|
|
|
|
groups =
|
2007-06-10 22:13:12 +02:00
|
|
|
let
|
2011-09-14 20:20:50 +02:00
|
|
|
defaultGroups =
|
2009-09-02 19:35:24 +02:00
|
|
|
[ { name = "root";
|
2007-06-10 22:13:12 +02:00
|
|
|
gid = ids.gids.root;
|
|
|
|
}
|
2007-08-16 17:09:06 +02:00
|
|
|
{ name = "wheel";
|
|
|
|
gid = ids.gids.wheel;
|
|
|
|
}
|
2008-07-02 20:03:43 +02:00
|
|
|
{ name = "disk";
|
|
|
|
gid = ids.gids.disk;
|
|
|
|
}
|
|
|
|
{ name = "kmem";
|
|
|
|
gid = ids.gids.kmem;
|
|
|
|
}
|
|
|
|
{ name = "tty";
|
|
|
|
gid = ids.gids.tty;
|
|
|
|
}
|
|
|
|
{ name = "floppy";
|
|
|
|
gid = ids.gids.floppy;
|
|
|
|
}
|
|
|
|
{ name = "uucp";
|
|
|
|
gid = ids.gids.uucp;
|
|
|
|
}
|
|
|
|
{ name = "lp";
|
|
|
|
gid = ids.gids.lp;
|
|
|
|
}
|
2009-08-11 11:17:30 +02:00
|
|
|
{ name = "cdrom";
|
|
|
|
gid = ids.gids.cdrom;
|
|
|
|
}
|
|
|
|
{ name = "tape";
|
|
|
|
gid = ids.gids.tape;
|
|
|
|
}
|
2011-11-04 18:40:00 +01:00
|
|
|
{ name = "audio";
|
|
|
|
gid = ids.gids.audio;
|
|
|
|
}
|
2009-08-11 11:17:30 +02:00
|
|
|
{ name = "video";
|
|
|
|
gid = ids.gids.video;
|
|
|
|
}
|
|
|
|
{ name = "dialout";
|
|
|
|
gid = ids.gids.dialout;
|
|
|
|
}
|
2007-06-10 22:13:12 +02:00
|
|
|
{ name = "nogroup";
|
|
|
|
gid = ids.gids.nogroup;
|
|
|
|
}
|
|
|
|
{ name = "users";
|
|
|
|
gid = ids.gids.users;
|
|
|
|
}
|
|
|
|
{ name = "nixbld";
|
|
|
|
gid = ids.gids.nixbld;
|
|
|
|
}
|
2010-04-22 15:56:26 +02:00
|
|
|
{ name = "utmp";
|
|
|
|
gid = ids.gids.utmp;
|
|
|
|
}
|
2007-06-10 22:13:12 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
addAttrs =
|
|
|
|
{ name, gid ? "" }:
|
|
|
|
{ inherit name gid; };
|
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
in map addAttrs (defaultGroups ++ config.users.extraGroups);
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
# Note: the 'X' in front of the password is to distinguish between
|
|
|
|
# having an empty password, and not having a password.
|
2011-11-29 07:08:55 +01:00
|
|
|
serializedUser = userName: let u = getAttr userName config.users.extraUsers; in "${u.name}\n${u.description}\n${if u.uid != null then toString u.uid else ""}\n${u.group}\n${toString (concatStringsSep "," u.extraGroups)}\n${u.home}\n${u.shell}\n${toString u.createHome}\n${if u.password != null then "X" + u.password else ""}\n${toString u.isSystemUser}\n${if u.createUser then "yes" else "no"}\n";
|
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
serializedGroup = g: "${g.name}\n${toString g.gid}";
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-03-06 13:26:16 +01:00
|
|
|
# keep this extra file so that cat can be used to pass special chars such as "`" which is used in the avahi daemon
|
2011-11-29 07:08:55 +01:00
|
|
|
usersFile = pkgs.writeText "users" (
|
|
|
|
concatMapStrings serializedUser (attrNames config.users.extraUsers)
|
|
|
|
);
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
###### interface
|
2009-01-02 17:07:01 +01:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
users.extraUsers = mkOption {
|
2011-11-29 07:08:55 +01:00
|
|
|
default = {};
|
|
|
|
type = types.loaOf types.optionSet;
|
|
|
|
example = {
|
|
|
|
alice = {
|
|
|
|
uid = 1234;
|
|
|
|
description = "Alice";
|
|
|
|
home = "/home/alice";
|
|
|
|
createHome = true;
|
|
|
|
group = "users";
|
|
|
|
extraGroups = ["wheel"];
|
|
|
|
shell = "/bin/sh";
|
|
|
|
password = "foobar";
|
|
|
|
};
|
|
|
|
};
|
2009-09-02 19:35:24 +02:00
|
|
|
description = ''
|
|
|
|
Additional user accounts to be created automatically by the system.
|
|
|
|
'';
|
2011-11-29 07:08:55 +01:00
|
|
|
options = [ userOpts ];
|
2009-09-02 19:35:24 +02:00
|
|
|
};
|
2009-01-02 17:07:01 +01:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
users.extraGroups = mkOption {
|
|
|
|
default = [];
|
|
|
|
example =
|
|
|
|
[ { name = "students";
|
|
|
|
gid = 1001;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
Additional groups to be created automatically by the system.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-09-02 19:35:24 +02:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
2011-11-29 07:08:55 +01:00
|
|
|
users.extraUsers = {
|
|
|
|
root = {
|
|
|
|
description = "System administrator";
|
|
|
|
home = "/root";
|
|
|
|
shell = config.users.defaultUserShell;
|
|
|
|
group = "root";
|
|
|
|
};
|
|
|
|
nobody = {
|
|
|
|
description = "Unprivileged account (don't use!)";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2010-09-13 17:41:38 +02:00
|
|
|
system.activationScripts.rootPasswd = stringAfter [ "etc" ]
|
|
|
|
''
|
|
|
|
# If there is no password file yet, create a root account with an
|
|
|
|
# empty password.
|
|
|
|
if ! test -e /etc/passwd; then
|
|
|
|
rootHome=/root
|
|
|
|
touch /etc/passwd; chmod 0644 /etc/passwd
|
|
|
|
touch /etc/group; chmod 0644 /etc/group
|
|
|
|
touch /etc/shadow; chmod 0600 /etc/shadow
|
|
|
|
# Can't use useradd, since it complains that it doesn't know us
|
|
|
|
# (bootstrap problem!).
|
|
|
|
echo "root:x:0:0:System administrator:$rootHome:${config.users.defaultUserShell}" >> /etc/passwd
|
|
|
|
echo "root::::::::" >> /etc/shadow
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
system.activationScripts.users = stringAfter [ "groups" ]
|
2009-09-02 19:35:24 +02:00
|
|
|
''
|
2009-12-16 14:35:03 +01:00
|
|
|
echo "updating users..."
|
|
|
|
|
2009-03-06 13:26:16 +01:00
|
|
|
cat ${usersFile} | while true; do
|
2009-01-02 17:07:01 +01:00
|
|
|
read name || break
|
|
|
|
read description
|
|
|
|
read uid
|
|
|
|
read group
|
|
|
|
read extraGroups
|
|
|
|
read home
|
|
|
|
read shell
|
|
|
|
read createHome
|
2009-09-02 19:35:24 +02:00
|
|
|
read password
|
2011-06-27 10:50:30 +02:00
|
|
|
read isSystemUser
|
2011-11-29 07:08:55 +01:00
|
|
|
read createUser
|
|
|
|
|
|
|
|
if ! test "$createUser" = "yes"; then
|
|
|
|
continue
|
|
|
|
fi
|
2009-01-02 17:07:01 +01:00
|
|
|
|
|
|
|
if ! curEnt=$(getent passwd "$name"); then
|
2011-06-27 10:50:30 +02:00
|
|
|
useradd ''${isSystemUser:+--system} \
|
2009-01-02 17:07:01 +01:00
|
|
|
--comment "$description" \
|
|
|
|
''${uid:+--uid $uid} \
|
|
|
|
--gid "$group" \
|
|
|
|
--groups "$extraGroups" \
|
|
|
|
--home "$home" \
|
|
|
|
--shell "$shell" \
|
2010-06-02 23:10:48 +02:00
|
|
|
''${createHome:+--create-home} \
|
|
|
|
"$name"
|
2009-09-02 19:35:24 +02:00
|
|
|
if test "''${password:0:1}" = 'X'; then
|
2010-06-03 01:02:09 +02:00
|
|
|
(echo "''${password:1}"; echo "''${password:1}") | ${pkgs.shadow}/bin/passwd "$name"
|
2009-09-02 19:35:24 +02:00
|
|
|
fi
|
2009-01-02 17:07:01 +01:00
|
|
|
else
|
|
|
|
#echo "updating user $name..."
|
|
|
|
oldIFS="$IFS"; IFS=:; set -- $curEnt; IFS="$oldIFS"
|
|
|
|
prevUid=$3
|
|
|
|
prevHome=$6
|
|
|
|
# Don't change the UID if it's the same, otherwise usermod
|
|
|
|
# will complain.
|
|
|
|
if test "$prevUid" = "$uid"; then unset uid; fi
|
|
|
|
# Don't change the home directory if it's the same to prevent
|
|
|
|
# unnecessary warnings about logged in users.
|
|
|
|
if test "$prevHome" = "$home"; then unset home; fi
|
|
|
|
usermod \
|
|
|
|
--comment "$description" \
|
|
|
|
''${uid:+--uid $uid} \
|
|
|
|
--gid "$group" \
|
|
|
|
--groups "$extraGroups" \
|
|
|
|
''${home:+--home "$home"} \
|
2010-06-02 23:10:48 +02:00
|
|
|
--shell "$shell" \
|
|
|
|
"$name"
|
2009-12-16 14:35:03 +01:00
|
|
|
fi
|
|
|
|
|
2009-03-06 13:26:16 +01:00
|
|
|
done
|
2010-09-13 17:41:38 +02:00
|
|
|
'';
|
2009-01-02 17:07:01 +01:00
|
|
|
|
2010-09-13 17:41:38 +02:00
|
|
|
system.activationScripts.groups = stringAfter [ "rootPasswd" "binsh" "etc" "var" ]
|
2009-09-02 19:35:24 +02:00
|
|
|
''
|
2009-12-16 14:35:03 +01:00
|
|
|
echo "updating groups..."
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
while true; do
|
|
|
|
read name || break
|
|
|
|
read gid
|
|
|
|
|
|
|
|
if ! curEnt=$(getent group "$name"); then
|
|
|
|
groupadd --system \
|
2010-06-02 23:10:48 +02:00
|
|
|
''${gid:+--gid $gid} \
|
|
|
|
"$name"
|
2009-01-02 17:07:01 +01:00
|
|
|
else
|
|
|
|
#echo "updating group $name..."
|
|
|
|
oldIFS="$IFS"; IFS=:; set -- $curEnt; IFS="$oldIFS"
|
|
|
|
prevGid=$3
|
|
|
|
if test -n "$gid" -a "$prevGid" != "$gid"; then
|
2010-06-02 23:10:48 +02:00
|
|
|
groupmod --gid $gid "$name"
|
2009-01-02 17:07:01 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done <<EndOfGroupList
|
|
|
|
${concatStringsSep "\n" (map serializedGroup groups)}
|
|
|
|
EndOfGroupList
|
2010-09-13 17:41:38 +02:00
|
|
|
'';
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
};
|
2009-09-02 19:35:24 +02:00
|
|
|
|
2007-11-09 19:49:45 +01:00
|
|
|
}
|