1 # Configuration for the pwdutils suite of tools: passwd, useradd, etc.
2 { config, lib, utils, pkgs, ... }:
4 cfg = config.security.loginDefs;
9 security.shadow.enable = lib.mkEnableOption "" // {
12 Enable the shadow authentication suite, which provides critical programs such as su, login, passwd.
14 Note: This is currently experimental. Only disable this if you're
15 confident that you can recover your system if it breaks.
19 security.loginDefs = {
20 package = lib.mkPackageOption pkgs "shadow" { };
22 chfnRestrict = lib.mkOption {
24 Use chfn SUID to allow non-root users to change their account GECOS information.
26 type = lib.types.nullOr lib.types.str;
30 settings = lib.mkOption {
32 Config options for the /etc/login.defs file, that defines
33 the site-specific configuration for the shadow password suite.
34 See login.defs(5) man page for available options.
36 type = lib.types.submodule {
37 freeformType = (pkgs.formats.keyValue { }).type;
38 /* There are three different sources for user/group id ranges, each of which gets
39 used by different programs:
40 - The login.defs file, used by the useradd, groupadd and newusers commands
41 - The update-users-groups.pl file, used by NixOS in the activation phase to
42 decide on which ids to use for declaratively defined users without a static
44 - Systemd compile time options -Dsystem-uid-max= and -Dsystem-gid-max=, used
45 by systemd for features like ConditionUser=@system and systemd-sysusers
48 DEFAULT_HOME = lib.mkOption {
49 description = "Indicate if login is allowed if we can't cd to the home directory.";
51 type = lib.types.enum [ "yes" "no" ];
54 ENCRYPT_METHOD = lib.mkOption {
55 description = "This defines the system default encryption algorithm for encrypting passwords.";
56 # The default crypt() method, keep in sync with the PAM default
58 type = lib.types.enum [ "YESCRYPT" "SHA512" "SHA256" "MD5" "DES"];
61 SYS_UID_MIN = lib.mkOption {
62 description = "Range of user IDs used for the creation of system users by useradd or newusers.";
67 SYS_UID_MAX = lib.mkOption {
68 description = "Range of user IDs used for the creation of system users by useradd or newusers.";
73 UID_MIN = lib.mkOption {
74 description = "Range of user IDs used for the creation of regular users by useradd or newusers.";
79 UID_MAX = lib.mkOption {
80 description = "Range of user IDs used for the creation of regular users by useradd or newusers.";
85 SYS_GID_MIN = lib.mkOption {
86 description = "Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers";
91 SYS_GID_MAX = lib.mkOption {
92 description = "Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers";
97 GID_MIN = lib.mkOption {
98 description = "Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.";
100 type = lib.types.int;
103 GID_MAX = lib.mkOption {
104 description = "Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.";
106 type = lib.types.int;
109 TTYGROUP = lib.mkOption {
111 The terminal permissions: the login tty will be owned by the TTYGROUP group,
112 and the permissions will be set to TTYPERM'';
114 type = lib.types.str;
117 TTYPERM = lib.mkOption {
119 The terminal permissions: the login tty will be owned by the TTYGROUP group,
120 and the permissions will be set to TTYPERM'';
122 type = lib.types.str;
125 # Ensure privacy for newly created home directories.
126 UMASK = lib.mkOption {
127 description = "The file mode creation mask is initialized to this value.";
129 type = lib.types.str;
137 users.defaultUserShell = lib.mkOption {
139 This option defines the default shell assigned to user
140 accounts. This can be either a full system path or a shell package.
142 This must not be a store path, since the path is
143 used outside the store (in particular in /etc/passwd).
145 example = lib.literalExpression "pkgs.zsh";
146 type = lib.types.either lib.types.path lib.types.shellPackage;
150 ###### implementation
152 config = lib.mkMerge [
156 assertion = config.security.shadow.enable || config.services.greetd.enable;
157 message = "You must enable at least one VT login method, either security.shadow.enable or services.greetd.enable";
161 (lib.mkIf config.security.shadow.enable {
164 assertion = cfg.settings.SYS_UID_MIN <= cfg.settings.SYS_UID_MAX;
165 message = "SYS_UID_MIN must be less than or equal to SYS_UID_MAX";
168 assertion = cfg.settings.UID_MIN <= cfg.settings.UID_MAX;
169 message = "UID_MIN must be less than or equal to UID_MAX";
172 assertion = cfg.settings.SYS_GID_MIN <= cfg.settings.SYS_GID_MAX;
173 message = "SYS_GID_MIN must be less than or equal to SYS_GID_MAX";
176 assertion = cfg.settings.GID_MIN <= cfg.settings.GID_MAX;
177 message = "GID_MIN must be less than or equal to GID_MAX";
181 security.loginDefs.settings.CHFN_RESTRICT = lib.mkIf (cfg.chfnRestrict != null) cfg.chfnRestrict;
183 environment.systemPackages = lib.optional config.users.mutableUsers cfg.package
184 ++ lib.optional (lib.types.shellPackage.check config.users.defaultUserShell) config.users.defaultUserShell
185 ++ lib.optional (cfg.chfnRestrict != null) pkgs.util-linux;
188 # Create custom toKeyValue generator
189 # see https://man7.org/linux/man-pages/man5/login.defs.5.html for config specification
191 toKeyValue = lib.generators.toKeyValue {
192 mkKeyValue = lib.generators.mkKeyValueDefault { } " ";
195 # /etc/login.defs: global configuration for pwdutils.
196 # You cannot login without it!
197 "login.defs".source = pkgs.writeText "login.defs" (toKeyValue cfg.settings);
199 # /etc/default/useradd: configuration for useradd.
200 "default/useradd".source = pkgs.writeText "useradd" ''
203 SHELL=${utils.toShellPath config.users.defaultUserShell}
207 security.pam.services = {
216 # Note: useradd, groupadd etc. aren't setuid root, so it
217 # doesn't really matter what the PAM config says as long as it
219 useradd.rootOK = true;
220 usermod.rootOK = true;
221 userdel.rootOK = true;
222 groupadd.rootOK = true;
223 groupmod.rootOK = true;
224 groupmems.rootOK = true;
225 groupdel.rootOK = true;
228 allowNullPassword = true;
232 chpasswd.rootOK = true;
237 mkSetuidRoot = source: {
245 su = mkSetuidRoot "${cfg.package.su}/bin/su";
246 sg = mkSetuidRoot "${cfg.package.out}/bin/sg";
247 newgrp = mkSetuidRoot "${cfg.package.out}/bin/newgrp";
248 newuidmap = mkSetuidRoot "${cfg.package.out}/bin/newuidmap";
249 newgidmap = mkSetuidRoot "${cfg.package.out}/bin/newgidmap";
251 // lib.optionalAttrs config.users.mutableUsers {
252 chsh = mkSetuidRoot "${cfg.package.out}/bin/chsh";
253 passwd = mkSetuidRoot "${cfg.package.out}/bin/passwd";