1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
|
{
config,
lib,
utils,
pkgs,
...
}:
let
inherit (lib)
any
attrNames
attrValues
concatMap
concatMapStringsSep
elem
filter
filterAttrs
flatten
flip
foldr
generators
getAttr
hasAttr
id
length
listToAttrs
literalExpression
mapAttrs'
mapAttrsToList
match
mkAliasOptionModule
mkDefault
mkIf
mkMerge
mkOption
mkRenamedOptionModule
optional
optionals
stringAfter
stringLength
trace
types
xor
;
ids = config.ids;
cfg = config.users;
# Check whether a password hash will allow login.
allowsLogin =
hash:
hash == "" # login without password
|| !(lib.elem hash [
null # password login disabled
"!" # password login disabled
"!!" # a variant of "!"
"*" # password unset
]);
overrideOrderMutable = "{option}`initialHashedPassword` -> {option}`initialPassword` -> {option}`hashedPassword` -> {option}`password` -> {option}`hashedPasswordFile`";
overrideOrderImmutable = "{option}`initialHashedPassword` -> {option}`hashedPassword` -> {option}`initialPassword` -> {option}`password` -> {option}`hashedPasswordFile`";
overrideOrderText = isMutable: ''
If the option {option}`users.mutableUsers` is
`${if isMutable then "true" else "false"}`, then the order of precedence is as shown
below, where values on the left are overridden by values on the right:
${if isMutable then overrideOrderMutable else overrideOrderImmutable}
'';
multiplePasswordsWarning = ''
If multiple of these password options are set at the same time then a
specific order of precedence is followed, which can lead to surprising
results. The order of precedence differs depending on whether the
{option}`users.mutableUsers` option is set.
'';
overrideDescription = ''
${multiplePasswordsWarning}
${overrideOrderText false}
${overrideOrderText true}
'';
passwordDescription = ''
The {option}`initialHashedPassword`, {option}`hashedPassword`,
{option}`initialPassword`, {option}`password` and
{option}`hashedPasswordFile` options all control what password is set for
the user.
In a system where [](#opt-systemd.sysusers.enable) is `false`, typically
only one of {option}`hashedPassword`, {option}`password`, or
{option}`hashedPasswordFile` will be set.
In a system where [](#opt-systemd.sysusers.enable) or [](#opt-services.userborn.enable) is `true`,
typically only one of {option}`initialPassword`, {option}`initialHashedPassword`,
or {option}`hashedPasswordFile` will be set.
If the option {option}`users.mutableUsers` is true, the password defined
in one of the above password options will only be set when the user is
created for the first time. After that, you are free to change the
password with the ordinary user management commands. If
{option}`users.mutableUsers` is false, you cannot change user passwords,
they will always be set according to the password options.
If none of the password options are set, then no password is assigned to
the user, and the user will not be able to do password-based logins.
${overrideDescription}
'';
hashedPasswordDescription = ''
To generate a hashed password run `mkpasswd`.
If set to an empty string (`""`), this user will be able to log in without
being asked for a password (but not via remote services such as SSH, or
indirectly via {command}`su` or {command}`sudo`). This should only be used
for e.g. bootable live systems. Note: this is different from setting an
empty password, which can be achieved using
{option}`users.users.<name?>.password`.
If set to `null` (default) this user will not be able to log in using a
password (i.e. via {command}`login` command).
'';
userOpts =
{ name, config, ... }:
{
options = {
enable = mkOption {
type = types.bool;
default = true;
example = false;
description = ''
If set to false, the user account will not be created. This is useful for when you wish to conditionally
disable user accounts.
'';
};
name = mkOption {
type = types.passwdEntry types.str;
apply =
x:
assert (
stringLength x < 32 || abort "Username '${x}' is longer than 31 characters which is not allowed!"
);
x;
description = ''
The name of the user account. If undefined, the name of the
attribute set will be used.
'';
};
description = mkOption {
type = types.passwdEntry types.str;
default = "";
example = "Alice Q. User";
description = ''
A short description of the user account, typically the
user's full name. This is actually the “GECOS” or “comment”
field in {file}`/etc/passwd`.
'';
};
uid = mkOption {
type = with types; nullOr int;
default = null;
description = ''
The account UID. If the UID is null, a free UID is picked on
activation.
'';
};
isSystemUser = mkOption {
type = types.bool;
default = false;
description = ''
Indicates if the user is a system user or not. This option
only has an effect if {option}`uid` is
{option}`null`, in which case it determines whether
the user's UID is allocated in the range for system users
(below 1000) or in the range for normal users (starting at
1000).
Exactly one of `isNormalUser` and
`isSystemUser` must be true.
'';
};
isNormalUser = mkOption {
type = types.bool;
default = false;
description = ''
Indicates whether this is an account for a “real” user.
This automatically sets {option}`group` to `users`,
{option}`createHome` to `true`,
{option}`home` to {file}`/home/«username»`,
{option}`useDefaultShell` to `true`,
and {option}`isSystemUser` to `false`.
Exactly one of `isNormalUser` and `isSystemUser` must be true.
'';
};
group = mkOption {
type = types.str;
apply =
x:
assert (
stringLength x < 32 || abort "Group name '${x}' is longer than 31 characters which is not allowed!"
);
x;
default = "";
description = "The user's primary group.";
};
extraGroups = mkOption {
type = types.listOf types.str;
default = [ ];
description = "The user's auxiliary groups.";
};
home = mkOption {
type = types.passwdEntry types.path;
default = "/var/empty";
description = "The user's home directory.";
};
homeMode = mkOption {
type = types.strMatching "[0-7]{1,5}";
default = "700";
description = "The user's home directory mode in numeric format. See {manpage}`chmod(1)`. The mode is only applied if {option}`users.users.<name>.createHome` is true.";
};
cryptHomeLuks = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Path to encrypted luks device that contains
the user's home directory.
'';
};
pamMount = mkOption {
type = with types; attrsOf str;
default = { };
description = ''
Attributes for user's entry in
{file}`pam_mount.conf.xml`.
Useful attributes might include `path`,
`options`, `fstype`, and `server`.
See <https://pam-mount.sourceforge.net/pam_mount.conf.5.html>
for more information.
'';
};
shell = mkOption {
type = types.nullOr (types.either types.shellPackage (types.passwdEntry types.path));
default = pkgs.shadow;
defaultText = literalExpression "pkgs.shadow";
example = literalExpression "pkgs.bashInteractive";
description = ''
The path to the user's shell. Can use shell derivations,
like `pkgs.bashInteractive`. Don’t
forget to enable your shell in
`programs` if necessary,
like `programs.zsh.enable = true;`.
'';
};
ignoreShellProgramCheck = mkOption {
type = types.bool;
default = false;
description = ''
By default, nixos will check that programs.SHELL.enable is set to
true if the user has a custom shell specified. If that behavior isn't
required and there are custom overrides in place to make sure that the
shell is functional, set this to true.
'';
};
subUidRanges = mkOption {
type = with types; listOf (submodule subordinateUidRange);
default = [ ];
example = [
{
startUid = 1000;
count = 1;
}
{
startUid = 100001;
count = 65534;
}
];
description = ''
Subordinate user ids that user is allowed to use.
They are set into {file}`/etc/subuid` and are used
by `newuidmap` for user namespaces.
'';
};
subGidRanges = mkOption {
type = with types; listOf (submodule subordinateGidRange);
default = [ ];
example = [
{
startGid = 100;
count = 1;
}
{
startGid = 1001;
count = 999;
}
];
description = ''
Subordinate group ids that user is allowed to use.
They are set into {file}`/etc/subgid` and are used
by `newgidmap` for user namespaces.
'';
};
autoSubUidGidRange = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Automatically allocate subordinate user and group ids for this user.
Allocated range is currently always of size 65536.
'';
};
createHome = mkOption {
type = types.bool;
default = false;
description = ''
Whether to create the home directory and ensure ownership as well as
permissions to match the user.
'';
};
useDefaultShell = mkOption {
type = types.bool;
default = false;
description = ''
If true, the user's shell will be set to
{option}`users.defaultUserShell`.
'';
};
hashedPassword = mkOption {
type = with types; nullOr (passwdEntry str);
default = null;
description = ''
Specifies the hashed password for the user.
${passwordDescription}
${hashedPasswordDescription}
'';
};
password = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Specifies the (clear text) password for the user.
Warning: do not set confidential information here
because it is world-readable in the Nix store. This option
should only be used for public accounts.
${passwordDescription}
'';
};
hashedPasswordFile = mkOption {
type = with types; nullOr str;
default = config.passwordFile;
defaultText = literalExpression "null";
description = ''
The full path to a file that contains the hash of the user's
password. The password file is read on each system activation. The
file should contain exactly one line, the salted password hash
produced by `mkpasswd`.
${passwordDescription}
'';
};
passwordFile = mkOption {
type = with types; nullOr str;
default = null;
visible = false;
description = "Deprecated alias of hashedPasswordFile";
};
initialHashedPassword = mkOption {
type = with types; nullOr (passwdEntry str);
default = null;
description = ''
Specifies the initial hashed password for the user, i.e. the
hashed password assigned if the user does not already
exist. If {option}`users.mutableUsers` is true, the
password can be changed subsequently using the
{command}`passwd` command. Otherwise, it's
equivalent to setting the {option}`hashedPassword` option.
${passwordDescription}
${hashedPasswordDescription}
'';
};
initialPassword = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Specifies the initial password for the user, i.e. the
password assigned if the user does not already exist. If
{option}`users.mutableUsers` is true, the password
can be changed subsequently using the
{command}`passwd` command. Otherwise, it's
equivalent to setting the {option}`password`
option. The same caveat applies: the password specified here
is world-readable in the Nix store, so it should only be
used for guest accounts or passwords that will be changed
promptly.
${passwordDescription}
'';
};
packages = mkOption {
type = types.listOf types.package;
default = [ ];
example = literalExpression "[ pkgs.firefox pkgs.thunderbird ]";
description = ''
The set of packages that should be made available to the user.
This is in contrast to {option}`environment.systemPackages`,
which adds packages to all users.
'';
};
expires = mkOption {
type = types.nullOr (types.strMatching "[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}");
default = null;
description = ''
Set the date on which the user's account will no longer be
accessible. The date is expressed in the format YYYY-MM-DD, or null
to disable the expiry.
A user whose account is locked must contact the system
administrator before being able to use the system again.
'';
};
linger = mkOption {
type = types.nullOr types.bool;
example = true;
default = null;
description = ''
Whether to enable or disable lingering for this user. Without
lingering, user units will not be started until the user logs in,
and may be stopped on logout depending on the settings in
{file}`logind.conf`.
By default, NixOS will not manage lingering, new users will default
to not lingering, and lingering can be configured imperatively using
`loginctl enable-linger` or `loginctl disable-linger`. Setting
this option to `true` or `false` is the declarative equivalent of
running `loginctl enable-linger` or `loginctl disable-linger`
respectively.
'';
};
};
config = mkMerge [
{
name = mkDefault name;
shell = mkIf config.useDefaultShell (mkDefault cfg.defaultUserShell);
}
(mkIf config.isNormalUser {
group = mkDefault "users";
createHome = mkDefault true;
home = mkDefault "${cfg.defaultUserHome}/${config.name}";
homeMode = mkDefault "700";
useDefaultShell = mkDefault true;
isSystemUser = mkDefault false;
})
# If !mutableUsers, setting ‘initialPassword’ is equivalent to
# setting ‘password’ (and similarly for hashed passwords).
(mkIf (!cfg.mutableUsers && config.initialPassword != null) {
password = mkDefault config.initialPassword;
})
(mkIf (!cfg.mutableUsers && config.initialHashedPassword != null) {
hashedPassword = mkDefault config.initialHashedPassword;
})
(mkIf (config.isNormalUser && config.subUidRanges == [ ] && config.subGidRanges == [ ]) {
autoSubUidGidRange = mkDefault true;
})
];
};
groupOpts =
{ name, config, ... }:
{
options = {
name = mkOption {
type = types.passwdEntry types.str;
description = ''
The name of the group. If undefined, the name of the attribute set
will be used.
'';
};
gid = mkOption {
type = with types; nullOr int;
default = null;
description = ''
The group GID. If the GID is null, a free GID is picked on
activation.
'';
};
members = mkOption {
type = with types; listOf (passwdEntry str);
default = [ ];
description = ''
The user names of the group members, added to the
`/etc/group` file.
'';
};
};
config = {
name = mkDefault name;
members = mapAttrsToList (n: u: u.name) (
filterAttrs (n: u: u.enable && elem config.name u.extraGroups) cfg.users
);
};
};
subordinateUidRange = {
options = {
startUid = mkOption {
type = types.int;
description = ''
Start of the range of subordinate user ids that user is
allowed to use.
'';
};
count = mkOption {
type = types.int;
default = 1;
description = "Count of subordinate user ids";
};
};
};
subordinateGidRange = {
options = {
startGid = mkOption {
type = types.int;
description = ''
Start of the range of subordinate group ids that user is
allowed to use.
'';
};
count = mkOption {
type = types.int;
default = 1;
description = "Count of subordinate group ids";
};
};
};
idsAreUnique =
set: idAttr:
!(foldr
(
name:
args@{ dup, acc }:
let
id = toString (getAttr idAttr (getAttr name set));
exists = hasAttr id acc;
newAcc =
acc
// (listToAttrs [
{
name = id;
value = true;
}
]);
in
if dup then
args
else if exists then
trace "Duplicate ${idAttr} ${id}" {
dup = true;
acc = null;
}
else
{
dup = false;
acc = newAcc;
}
)
{
dup = false;
acc = { };
}
(attrNames set)
).dup;
uidsAreUnique = idsAreUnique (filterAttrs (n: u: u.uid != null) cfg.users) "uid";
gidsAreUnique = idsAreUnique (filterAttrs (n: g: g.gid != null) cfg.groups) "gid";
sdInitrdUidsAreUnique = idsAreUnique (filterAttrs (
n: u: u.uid != null
) config.boot.initrd.systemd.users) "uid";
sdInitrdGidsAreUnique = idsAreUnique (filterAttrs (
n: g: g.gid != null
) config.boot.initrd.systemd.groups) "gid";
groupNames = lib.mapAttrsToList (n: g: g.name) cfg.groups;
usersWithoutExistingGroup = lib.filterAttrs (
n: u: u.group != "" && !lib.elem u.group groupNames
) cfg.users;
usersWithNullShells = attrNames (filterAttrs (name: cfg: cfg.shell == null) cfg.users);
spec = pkgs.writeText "users-groups.json" (
builtins.toJSON {
inherit (cfg) mutableUsers;
users = mapAttrsToList (_: u: {
inherit (u)
name
uid
group
description
home
homeMode
createHome
isSystemUser
password
hashedPasswordFile
hashedPassword
autoSubUidGidRange
subUidRanges
subGidRanges
initialPassword
initialHashedPassword
expires
;
shell = utils.toShellPath u.shell;
}) (filterAttrs (_: u: u.enable) cfg.users);
groups = attrValues cfg.groups;
}
);
systemShells =
let
shells = mapAttrsToList (_: u: u.shell) cfg.users;
in
filter types.shellPackage.check shells;
in
{
imports = [
(mkAliasOptionModule [ "users" "extraUsers" ] [ "users" "users" ])
(mkAliasOptionModule [ "users" "extraGroups" ] [ "users" "groups" ])
(mkRenamedOptionModule
[ "security" "initialRootPassword" ]
[ "users" "users" "root" "initialHashedPassword" ]
)
];
###### interface
options = {
users.mutableUsers = mkOption {
type = types.bool;
default = true;
description = ''
If set to `true`, you are free to add new users and groups to the system
with the ordinary `useradd` and
`groupadd` commands. On system activation, the
existing contents of the `/etc/passwd` and
`/etc/group` files will be merged with the
contents generated from the `users.users` and
`users.groups` options.
The initial password for a user will be set
according to `users.users`, but existing passwords
will not be changed.
::: {.warning}
If set to `false`, the contents of the user and
group files will simply be replaced on system activation. This also
holds for the user passwords; all changed
passwords will be reset according to the
`users.users` configuration on activation.
:::
'';
};
users.enforceIdUniqueness = mkOption {
type = types.bool;
default = true;
description = ''
Whether to require that no two users/groups share the same uid/gid.
'';
};
users.manageLingering = mkOption {
type = types.bool;
default = true;
description = "Whether to manage whether users linger or not.";
example = false;
};
users.users = mkOption {
default = { };
type = with types; attrsOf (submodule userOpts);
example = {
alice = {
uid = 1234;
description = "Alice Q. User";
home = "/home/alice";
createHome = true;
group = "users";
extraGroups = [ "wheel" ];
shell = "/bin/sh";
};
};
description = ''
Additional user accounts to be created automatically by the system.
This can also be used to set options for root.
'';
};
users.groups = mkOption {
default = { };
example = {
students.gid = 1001;
hackers = { };
};
type = with types; attrsOf (submodule groupOpts);
description = ''
Additional groups to be created automatically by the system.
'';
};
users.allowNoPasswordLogin = mkOption {
type = types.bool;
default = false;
description = ''
Disable checking that at least the `root` user or a user in the `wheel` group can log in using
a password or an SSH key.
WARNING: enabling this can lock you out of your system. Enable this only if you know what are you doing.
'';
};
users.defaultUserHome = mkOption {
type = types.str;
default = "/home";
description = ''
The default home directory for normal users.
'';
};
# systemd initrd
boot.initrd.systemd.users = mkOption {
description = ''
Users to include in initrd.
'';
default = { };
type = types.attrsOf (
types.submodule (
{ name, ... }:
{
options.uid = mkOption {
type = types.int;
description = ''
ID of the user in initrd.
'';
defaultText = literalExpression "config.users.users.\${name}.uid";
default = cfg.users.${name}.uid;
};
options.group = mkOption {
type = types.singleLineStr;
description = ''
Group the user belongs to in initrd.
'';
defaultText = literalExpression "config.users.users.\${name}.group";
default = cfg.users.${name}.group;
};
options.shell = mkOption {
type = types.passwdEntry types.path;
description = ''
The path to the user's shell in initrd.
'';
default = "${pkgs.shadow}/bin/nologin";
defaultText = literalExpression "\${pkgs.shadow}/bin/nologin";
};
}
)
);
};
boot.initrd.systemd.groups = mkOption {
description = ''
Groups to include in initrd.
'';
default = { };
type = types.attrsOf (
types.submodule (
{ name, ... }:
{
options.gid = mkOption {
type = types.int;
description = ''
ID of the group in initrd.
'';
defaultText = literalExpression "config.users.groups.\${name}.gid";
default = cfg.groups.${name}.gid;
};
}
)
);
};
};
###### implementation
config =
let
cryptSchemeIdPatternGroup = "(${lib.concatStringsSep "|" pkgs.libxcrypt.enabledCryptSchemeIds})";
in
{
users.users = {
root = {
uid = ids.uids.root;
description = mkDefault "System administrator";
home = "/root";
shell = mkDefault cfg.defaultUserShell;
group = "root";
};
nobody = {
uid = ids.uids.nobody;
isSystemUser = true;
description = mkDefault "Unprivileged account (don't use!)";
group = "nogroup";
};
};
users.groups = {
root.gid = ids.gids.root;
wheel.gid = ids.gids.wheel;
disk.gid = ids.gids.disk;
kmem.gid = ids.gids.kmem;
tty.gid = ids.gids.tty;
floppy.gid = ids.gids.floppy;
uucp.gid = ids.gids.uucp;
lp.gid = ids.gids.lp;
cdrom.gid = ids.gids.cdrom;
tape.gid = ids.gids.tape;
audio.gid = ids.gids.audio;
video.gid = ids.gids.video;
dialout.gid = ids.gids.dialout;
nogroup.gid = ids.gids.nogroup;
users.gid = ids.gids.users;
nixbld.gid = ids.gids.nixbld;
utmp.gid = ids.gids.utmp;
adm.gid = ids.gids.adm;
input.gid = ids.gids.input;
kvm.gid = ids.gids.kvm;
render.gid = ids.gids.render;
sgx.gid = ids.gids.sgx;
shadow.gid = ids.gids.shadow;
clock.gid = ids.gids.clock;
};
system.activationScripts.users =
if !config.systemd.sysusers.enable && !config.services.userborn.enable then
{
supportsDryActivation = true;
text = ''
install -m 0700 -d /root
install -m 0755 -d /home
${
pkgs.perl.withPackages (p: [
p.FileSlurp
p.JSON
])
}/bin/perl \
-w ${./update-users-groups.pl} ${spec}
'';
}
else
""; # keep around for backwards compatibility
systemd.services.linger-users = lib.mkIf cfg.manageLingering {
wantedBy = [ "multi-user.target" ];
after = [ "systemd-logind.service" ];
requires = [ "systemd-logind.service" ];
script =
let
lingeringUsers = filterAttrs (n: v: v.linger == true) cfg.users;
nonLingeringUsers = filterAttrs (n: v: v.linger == false) cfg.users;
lingeringUserNames = mapAttrsToList (n: v: v.name) lingeringUsers;
nonLingeringUserNames = mapAttrsToList (n: v: v.name) nonLingeringUsers;
in
''
${lib.strings.toShellVars { inherit lingeringUserNames nonLingeringUserNames; }}
user_configured () {
# Use `id` to check if the user exists rather than checking the
# NixOS configuration, as it may be that the user has been
# manually configured, which is permitted if users.mutableUsers
# is true (the default).
id "$1" >/dev/null
}
shopt -s dotglob nullglob
for user in *; do
if ! user_configured "$user"; then
# systemd has this user configured to linger despite them not
# existing.
echo "Removing linger for missing user $user" >&2
rm -- "$user"
fi
done
if (( ''${#nonLingeringUserNames[*]} > 0 )); then
${config.systemd.package}/bin/loginctl disable-linger "''${nonLingeringUserNames[@]}"
fi
if (( ''${#lingeringUserNames[*]} > 0 )); then
${config.systemd.package}/bin/loginctl enable-linger "''${lingeringUserNames[@]}"
fi
'';
serviceConfig = {
Type = "oneshot";
StateDirectory = "systemd/linger";
WorkingDirectory = "/var/lib/systemd/linger";
};
};
# for backwards compatibility
system.activationScripts.hashes = stringAfter [ "users" ] "";
# for backwards compatibility
system.activationScripts.groups = stringAfter [ "users" ] "";
# Install all the user shells
environment.systemPackages = systemShells;
environment.etc = mapAttrs' (
_:
{ packages, name, ... }:
{
name = "profiles/per-user/${name}";
value.source = pkgs.buildEnv {
name = "user-environment";
paths = packages;
inherit (config.environment) pathsToLink extraOutputsToInstall;
inherit (config.system.path) ignoreCollisions postBuild;
};
}
) (filterAttrs (_: u: u.packages != [ ]) cfg.users);
environment.profiles = [
"$HOME/.nix-profile"
"\${XDG_STATE_HOME}/nix/profile"
"$HOME/.local/state/nix/profile"
"/etc/profiles/per-user/$USER"
];
# systemd initrd
boot.initrd.systemd = lib.mkIf config.boot.initrd.systemd.enable {
contents = {
"/etc/passwd".text = ''
${lib.concatStringsSep "\n" (
lib.mapAttrsToList (
n:
{
uid,
group,
shell,
}:
let
g = config.boot.initrd.systemd.groups.${group};
in
"${n}:x:${toString uid}:${toString g.gid}::/var/empty:${shell}"
) config.boot.initrd.systemd.users
)}
'';
"/etc/group".text = ''
${lib.concatStringsSep "\n" (
lib.mapAttrsToList (n: { gid }: "${n}:x:${toString gid}:") config.boot.initrd.systemd.groups
)}
'';
"/etc/shells".text =
lib.concatStringsSep "\n" (
lib.unique (lib.mapAttrsToList (_: u: u.shell) config.boot.initrd.systemd.users)
)
+ "\n";
};
storePaths = [ "${pkgs.shadow}/bin/nologin" ];
users = {
root = {
shell = lib.mkDefault "/bin/bash";
};
nobody = { };
};
groups = {
root = { };
nogroup = { };
systemd-journal = { };
tty = { };
dialout = { };
kmem = { };
input = { };
video = { };
render = { };
sgx = { };
audio = { };
video = { };
lp = { };
disk = { };
cdrom = { };
tape = { };
kvm = { };
clock = { };
};
};
assertions = [
{
assertion = !cfg.enforceIdUniqueness || (uidsAreUnique && gidsAreUnique);
message = "UIDs and GIDs must be unique!";
}
{
assertion = !cfg.enforceIdUniqueness || (sdInitrdUidsAreUnique && sdInitrdGidsAreUnique);
message = "systemd initrd UIDs and GIDs must be unique!";
}
{
assertion = usersWithoutExistingGroup == { };
message =
let
errUsers = lib.attrNames usersWithoutExistingGroup;
missingGroups = lib.unique (lib.mapAttrsToList (n: u: u.group) usersWithoutExistingGroup);
mkConfigHint = group: "users.groups.${group} = {};";
in
''
The following users have a primary group that is undefined: ${lib.concatStringsSep " " errUsers}
Hint: Add this to your NixOS configuration:
${lib.concatStringsSep "\n " (map mkConfigHint missingGroups)}
'';
}
{
assertion = !cfg.mutableUsers -> length usersWithNullShells == 0;
message = ''
users.mutableUsers = false has been set,
but found users that have their shell set to null.
If you wish to disable login, set their shell to pkgs.shadow (the default).
Misconfigured users: ${lib.concatStringsSep " " usersWithNullShells}
'';
}
{
# If mutableUsers is false, to prevent users creating a
# configuration that locks them out of the system, ensure that
# there is at least one "privileged" account that has a
# password or an SSH authorized key. Privileged accounts are
# root and users in the wheel group.
# The check does not apply when users.allowNoPasswordLogin
# The check does not apply when users.mutableUsers
assertion =
!cfg.mutableUsers
-> !cfg.allowNoPasswordLogin
-> any id (
mapAttrsToList (
name: user:
(
name == "root"
|| user.group == "wheel"
|| elem "wheel" user.extraGroups
|| elem name (cfg.groups.wheel.members or [ ])
)
&& (
allowsLogin user.hashedPassword
|| user.password != null
|| user.hashedPasswordFile != null
|| user.openssh.authorizedKeys.keys != [ ]
|| user.openssh.authorizedKeys.keyFiles != [ ]
)
) cfg.users
++ [
config.security.googleOsLogin.enable
]
);
message = ''
Neither the root account nor any wheel user has a password or SSH authorized key.
You must set one to prevent being locked out of your system.
If you really want to be locked out of your system, set users.allowNoPasswordLogin = true;
However you are most probably better off by setting users.mutableUsers = true; and
manually running passwd root to set the root password.
'';
}
]
++ flip mapAttrsToList config.boot.initrd.systemd.users (
name: user: {
assertion = config.boot.initrd.systemd.enable -> (baseNameOf user.shell != "cryptsetup-askpass");
message = ''
cryptsetup-askpass is not available in systemd stage 1. Please remove it from: boot.initrd.systemd.users.${name}.shell
Use `systemctl default` instead; see the NixOS 26.05 release notes for details. If you want to continue restricting the command for SSH login, you can use `command="systemctl default"` in SSH authorized keys instead; see `sshd(8)`.
'';
}
)
++ flatten (
flip mapAttrsToList cfg.users (
name: user:
[
(
let
# Things fail in various ways with especially non-ascii usernames.
# This regex mirrors the one from shadow's is_valid_name:
# https://github.com/shadow-maint/shadow/blob/bee77ffc291dfed2a133496db465eaa55e2b0fec/lib/chkname.c#L68
# though without the trailing $, because Samba 3 got its last release
# over 10 years ago and is not in Nixpkgs anymore,
# while later versions don't appear to require anything like that.
nameRegex = "[a-zA-Z0-9_.][a-zA-Z0-9_.-]*";
in
{
assertion = builtins.match nameRegex user.name != null;
message = "The username \"${user.name}\" is not valid, it does not match the regex \"${nameRegex}\".";
}
)
{
assertion = (user.hashedPassword != null) -> (match ".*:.*" user.hashedPassword == null);
message = ''
The password hash of user "${user.name}" contains a ":" character.
This is invalid and would break the login system because the fields
of /etc/shadow (file where hashes are stored) are colon-separated.
Please check the value of option `users.users."${user.name}".hashedPassword`.'';
}
{
assertion = user.isNormalUser && user.uid != null -> user.uid >= 1000;
message = ''
A user cannot have a users.users.${user.name}.uid set below 1000 and set users.users.${user.name}.isNormalUser.
Either users.users.${user.name}.isSystemUser must be set to true instead of users.users.${user.name}.isNormalUser
or users.users.${user.name}.uid must be changed to 1000 or above.
'';
}
{
assertion =
let
# we do an extra check on isNormalUser here, to not trigger this assertion when isNormalUser is set and uid to < 1000
isEffectivelySystemUser =
user.isSystemUser || (user.uid != null && user.uid < 1000 && !user.isNormalUser);
in
xor isEffectivelySystemUser user.isNormalUser;
message = ''
Exactly one of users.users.${user.name}.isSystemUser and users.users.${user.name}.isNormalUser must be set.
'';
}
{
assertion = user.group != "";
message = ''
users.users.${user.name}.group is unset. This used to default to
nogroup, but this is unsafe. For example you can create a group
for this user with:
users.users.${user.name}.group = "${user.name}";
users.groups.${user.name} = {};
'';
}
{
assertion = user.linger != null -> cfg.manageLingering;
message = ''
users.manageLingering is set to false, but
users.users.${user.name}.linger is configured.
If you want NixOS to manage whether user accounts linger or
not, you must set users.manageLingering to true. This is the
default setting.
If you do not want NixOS to manage whether user accounts linger
or not, you must set users.users.${user.name}.linger to null.
This is the default setting provided system.stateVersion is at
least "25.11".
'';
}
]
++ (map
(shell: {
assertion =
!user.ignoreShellProgramCheck
-> (user.shell == pkgs.${shell})
-> (config.programs.${shell}.enable == true);
message = ''
users.users.${user.name}.shell is set to ${shell}, but
programs.${shell}.enable is not true. This will cause the ${shell}
shell to lack the basic nix directories in its PATH and might make
logging in as that user impossible. You can fix it with:
programs.${shell}.enable = true;
If you know what you're doing and you are fine with the behavior,
set users.users.${user.name}.ignoreShellProgramCheck = true;
instead.
'';
})
[
"fish"
"xonsh"
"zsh"
]
)
)
);
warnings =
flip concatMap (attrValues cfg.users) (
user:
let
passwordOptions = [
"hashedPassword"
"hashedPasswordFile"
"password"
]
++ optionals cfg.mutableUsers [
# For immutable users, initialHashedPassword is set to hashedPassword,
# so using these options would always trigger the assertion.
"initialHashedPassword"
"initialPassword"
];
unambiguousPasswordConfiguration =
1 >= length (filter (x: x != null) (map (flip getAttr user) passwordOptions));
in
optional (!unambiguousPasswordConfiguration) ''
The user '${user.name}' has multiple of the options
`initialHashedPassword`, `hashedPassword`, `initialPassword`, `password`
& `hashedPasswordFile` set to a non-null value.
${multiplePasswordsWarning}
${overrideOrderText cfg.mutableUsers}
The values of these options are:
${concatMapStringsSep "\n" (
value: "* users.users.\"${user.name}\".${value}: ${generators.toPretty { } user.${value}}"
) passwordOptions}
''
)
++ filter (x: x != null) (
flip mapAttrsToList cfg.users (
_: user:
# This regex matches a subset of the Modular Crypto Format (MCF)[1]
# informal standard. Since this depends largely on the OS or the
# specific implementation of crypt(3) we only support the (sane)
# schemes implemented by glibc and BSDs. In particular the original
# DES hash is excluded since, having no structure, it would validate
# common mistakes like typing the plaintext password.
#
# [1]: https://en.wikipedia.org/wiki/Crypt_(C)
let
sep = "\\$";
base64 = "[a-zA-Z0-9./]+";
id = cryptSchemeIdPatternGroup;
name = "[a-z0-9-]+";
value = "[a-zA-Z0-9/+.-]+";
options = "${name}(=${value})?(,${name}=${value})*";
scheme = "${id}(${sep}${options})?";
content = "${base64}${sep}${base64}(${sep}${base64})?";
mcf = "^${sep}${scheme}${sep}${content}$";
in
if
(
allowsLogin user.hashedPassword
&& user.hashedPassword != "" # login without password
&& match mcf user.hashedPassword == null
)
then
''
The password hash of user "${user.name}" may be invalid. You must set a
valid hash or the user will be locked out of their account. Please
check the value of option `users.users."${user.name}".hashedPassword`.''
else
null
)
++ flip mapAttrsToList cfg.users (
name: user:
if user.passwordFile != null then
''The option `users.users."${name}".passwordFile' has been renamed ''
+ ''to `users.users."${name}".hashedPasswordFile'.''
else
null
)
);
};
}
|