2009-01-02 17:07:01 +01:00
|
|
|
{pkgs, config, ...}:
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
###### interface
|
|
|
|
let
|
|
|
|
inherit (pkgs.lib) mkOption;
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
options = {
|
|
|
|
users = {
|
|
|
|
extraUsers = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{ name = "alice";
|
|
|
|
uid = 1234;
|
|
|
|
description = "Alice";
|
|
|
|
home = "/home/alice";
|
|
|
|
createHome = true;
|
|
|
|
group = "users";
|
|
|
|
extraGroups = ["wheel"];
|
|
|
|
shell = "/bin/sh";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
description = "
|
|
|
|
Additional user accounts to be created automatically by the system.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraGroups = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{ name = "students";
|
|
|
|
gid = 1001;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
description = "
|
|
|
|
Additional groups to be created automatically by the system.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
let
|
2009-05-25 16:19:33 +02:00
|
|
|
ids = import ../../system/ids.nix;
|
2009-01-02 17:07:01 +01:00
|
|
|
defaultShell = config.system.shell;
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2007-11-16 14:26:34 +01:00
|
|
|
# User accounts to be created/updated by NixOS.
|
|
|
|
users =
|
2007-06-08 17:41:12 +02:00
|
|
|
let
|
|
|
|
defaultUsers =
|
|
|
|
[
|
|
|
|
{ name = "root";
|
|
|
|
uid = ids.uids.root;
|
|
|
|
description = "System administrator";
|
|
|
|
home = "/root";
|
|
|
|
shell = defaultShell;
|
2007-11-12 15:59:23 +01:00
|
|
|
group = "root";
|
2007-06-08 17:41:12 +02:00
|
|
|
}
|
|
|
|
{ name = "nobody";
|
|
|
|
uid = ids.uids.nobody;
|
|
|
|
description = "Unprivileged account (don't use!)";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
makeNixBuildUser = nr:
|
|
|
|
{ name = "nixbld${toString nr}";
|
|
|
|
description = "Nix build user ${toString nr}";
|
|
|
|
uid = builtins.add ids.uids.nixbld nr;
|
|
|
|
extraGroups = ["nixbld"];
|
|
|
|
};
|
|
|
|
|
|
|
|
nixBuildUsers = map makeNixBuildUser (pkgs.lib.range 1 10);
|
|
|
|
|
|
|
|
addAttrs =
|
|
|
|
{ name
|
|
|
|
, description
|
|
|
|
, uid ? ""
|
|
|
|
, group ? "nogroup"
|
|
|
|
, extraGroups ? []
|
|
|
|
, home ? "/var/empty"
|
2007-11-16 14:26:34 +01:00
|
|
|
, shell ? (if useDefaultShell then defaultShell else "/noshell")
|
|
|
|
, createHome ? false
|
|
|
|
, useDefaultShell ? false
|
2007-06-08 17:41:12 +02:00
|
|
|
}:
|
2007-11-16 14:26:34 +01:00
|
|
|
{ inherit name description uid group extraGroups home shell createHome; };
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2008-11-18 19:00:09 +01:00
|
|
|
in map addAttrs (defaultUsers ++ nixBuildUsers ++ config.users.extraUsers);
|
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
|
|
|
|
defaultGroups =
|
|
|
|
[
|
|
|
|
{ name = "root";
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
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
|
|
|
inherit (pkgs.lib) concatStringsSep;
|
|
|
|
|
|
|
|
serializedUser = u: "${u.name}\n${u.description}\n${toString u.uid}\n${u.group}\n${toString (concatStringsSep "," u.extraGroups)}\n${u.home}\n${u.shell}\n${toString u.createHome}";
|
|
|
|
serializedGroup = g: "${g.name}\n${toString g.gid}";
|
|
|
|
in
|
|
|
|
|
|
|
|
let
|
2009-05-20 01:51:13 +02:00
|
|
|
inherit (pkgs.stringsWithDeps) fullDepEntry;
|
2009-01-02 17:07:01 +01: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
|
|
|
|
usersFile = pkgs.writeText "users" (concatStringsSep "\n" (map serializedUser users));
|
2009-01-02 17:07:01 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
require = [
|
|
|
|
options
|
|
|
|
|
|
|
|
# config.system.activationScripts
|
2009-05-25 16:19:33 +02:00
|
|
|
# ../system/activate-configuration.nix
|
2009-01-02 17:07:01 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
system = {
|
|
|
|
activationScripts = {
|
|
|
|
|
2009-05-20 01:51:13 +02:00
|
|
|
users = fullDepEntry ''
|
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
|
|
|
|
|
|
|
|
if ! curEnt=$(getent passwd "$name"); then
|
|
|
|
echo "creating user $name..."
|
|
|
|
useradd --system \
|
|
|
|
"$name" \
|
|
|
|
--comment "$description" \
|
|
|
|
''${uid:+--uid $uid} \
|
|
|
|
--gid "$group" \
|
|
|
|
--groups "$extraGroups" \
|
|
|
|
--home "$home" \
|
|
|
|
--shell "$shell" \
|
|
|
|
''${createHome:+--create-home}
|
|
|
|
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 \
|
|
|
|
"$name" \
|
|
|
|
--comment "$description" \
|
|
|
|
''${uid:+--uid $uid} \
|
|
|
|
--gid "$group" \
|
|
|
|
--groups "$extraGroups" \
|
|
|
|
''${home:+--home "$home"} \
|
|
|
|
--shell "$shell"
|
|
|
|
fi
|
2009-03-06 13:26:16 +01:00
|
|
|
done
|
2009-05-20 03:35:46 +02:00
|
|
|
'' [ "groups" ];
|
2009-01-02 17:07:01 +01:00
|
|
|
|
2009-05-20 01:51:13 +02:00
|
|
|
groups = fullDepEntry ''
|
2009-01-02 17:07:01 +01:00
|
|
|
while true; do
|
|
|
|
read name || break
|
|
|
|
read gid
|
|
|
|
|
|
|
|
if ! curEnt=$(getent group "$name"); then
|
|
|
|
echo "creating group $name..."
|
|
|
|
groupadd --system \
|
|
|
|
"$name" \
|
|
|
|
''${gid:+--gid $gid}
|
|
|
|
else
|
|
|
|
#echo "updating group $name..."
|
|
|
|
oldIFS="$IFS"; IFS=:; set -- $curEnt; IFS="$oldIFS"
|
|
|
|
prevGid=$3
|
|
|
|
if test -n "$gid" -a "$prevGid" != "$gid"; then
|
|
|
|
groupmod "$name" --gid $gid
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done <<EndOfGroupList
|
|
|
|
${concatStringsSep "\n" (map serializedGroup groups)}
|
|
|
|
EndOfGroupList
|
2009-05-20 03:35:46 +02:00
|
|
|
'' [ "rootPasswd" "binsh" "etc" "var" ];
|
2007-06-08 17:41:12 +02:00
|
|
|
|
2009-01-02 17:07:01 +01:00
|
|
|
};
|
|
|
|
};
|
2007-11-09 19:49:45 +01:00
|
|
|
}
|