9
9
, maxLayers ? 100
10
10
, nixConf ? { }
11
11
, flake-registry ? null
12
+ , uid ? 0
13
+ , gid ? 0
14
+ , uname ? "root"
15
+ , gname ? "root"
12
16
} :
13
17
let
14
18
defaultPkgs = with pkgs ; [
50
54
description = "Unprivileged account (don't use!)" ;
51
55
} ;
52
56
57
+ } // lib . optionalAttrs ( uid != 0 ) {
58
+ "${ uname } " = {
59
+ uid = uid ;
60
+ shell = "${ pkgs . bashInteractive } /bin/bash" ;
61
+ home = "/home/${ uname } " ;
62
+ gid = gid ;
63
+ groups = [ "${ gname } " ] ;
64
+ description = "Nix user" ;
65
+ } ;
53
66
} // lib . listToAttrs (
54
67
map
55
68
(
70
83
root . gid = 0 ;
71
84
nixbld . gid = 30000 ;
72
85
nobody . gid = 65534 ;
86
+ } // lib . optionalAttrs ( gid != 0 ) {
87
+ "${ gname } " . gid = gid ;
73
88
} ;
74
89
75
90
userToPasswd = (
150
165
in
151
166
"${ n } = ${ vStr } " ) ( defaultNixConf // nixConf ) ) ) + "\n " ;
152
167
168
+ userHome = if uid == 0 then "/root" else "/home/${ uname } " ;
169
+
153
170
baseSystem =
154
171
let
155
172
nixpkgs = pkgs . path ;
@@ -237,26 +254,26 @@ let
237
254
mkdir -p $out/etc/nix
238
255
cat $nixConfContentsPath > $out/etc/nix/nix.conf
239
256
240
- mkdir -p $out/root
241
- mkdir -p $out/nix/var/nix/profiles/per-user/root
257
+ mkdir -p $out${ userHome }
258
+ mkdir -p $out/nix/var/nix/profiles/per-user/${ uname }
242
259
243
260
ln -s ${ profile } $out/nix/var/nix/profiles/default-1-link
244
261
ln -s $out/nix/var/nix/profiles/default-1-link $out/nix/var/nix/profiles/default
245
- ln -s /nix/var/nix/profiles/default $out/root /.nix-profile
262
+ ln -s /nix/var/nix/profiles/default $out${ userHome } /.nix-profile
246
263
247
- ln -s ${ channel } $out/nix/var/nix/profiles/per-user/root /channels-1-link
248
- ln -s $out/nix/var/nix/profiles/per-user/root /channels-1-link $out/nix/var/nix/profiles/per-user/root /channels
264
+ ln -s ${ channel } $out/nix/var/nix/profiles/per-user/${ uname } /channels-1-link
265
+ ln -s $out/nix/var/nix/profiles/per-user/${ uname } /channels-1-link $out/nix/var/nix/profiles/per-user/${ uname } /channels
249
266
250
- mkdir -p $out/root /.nix-defexpr
251
- ln -s $out/nix/var/nix/profiles/per-user/root /channels $out/root /.nix-defexpr/channels
252
- echo "${ channelURL } ${ channelName } " > $out/root /.nix-channels
267
+ mkdir -p $out${ userHome } /.nix-defexpr
268
+ ln -s $out/nix/var/nix/profiles/per-user/${ uname } /channels $out${ userHome } /.nix-defexpr/channels
269
+ echo "${ channelURL } ${ channelName } " > $out${ userHome } /.nix-channels
253
270
254
271
mkdir -p $out/bin $out/usr/bin
255
272
ln -s ${ pkgs . coreutils } /bin/env $out/usr/bin/env
256
273
ln -s ${ pkgs . bashInteractive } /bin/bash $out/bin/sh
257
274
258
275
'' + ( lib . optionalString ( flake-registry-path != null ) ''
259
- nixCacheDir="/root /.cache/nix"
276
+ nixCacheDir="${ userHome } /.cache/nix"
260
277
mkdir -p $out$nixCacheDir
261
278
globalFlakeRegistryPath="$nixCacheDir/flake-registry.json"
262
279
ln -s ${ flake-registry-path } $out$globalFlakeRegistryPath
268
285
in
269
286
pkgs . dockerTools . buildLayeredImageWithNixDb {
270
287
271
- inherit name tag maxLayers ;
288
+ inherit name tag maxLayers uid gid uname gname ;
272
289
273
290
contents = [ baseSystem ] ;
274
291
@@ -279,25 +296,28 @@ pkgs.dockerTools.buildLayeredImageWithNixDb {
279
296
fakeRootCommands = ''
280
297
chmod 1777 tmp
281
298
chmod 1777 var/tmp
299
+ chown -R ${ toString uid } :${ toString gid } .${ userHome }
300
+ chown -R ${ toString uid } :${ toString gid } nix
282
301
'' ;
283
302
284
303
config = {
285
- Cmd = [ "/root/.nix-profile/bin/bash" ] ;
304
+ Cmd = [ "${ userHome } /.nix-profile/bin/bash" ] ;
305
+ User = "${ toString uid } :${ toString gid } " ;
286
306
Env = [
287
- "USER=root "
307
+ "USER=${ uname } "
288
308
"PATH=${ lib . concatStringsSep ":" [
289
- "/root /.nix-profile/bin"
309
+ "${ userHome } /.nix-profile/bin"
290
310
"/nix/var/nix/profiles/default/bin"
291
311
"/nix/var/nix/profiles/default/sbin"
292
312
] } "
293
313
"MANPATH=${ lib . concatStringsSep ":" [
294
- "/root /.nix-profile/share/man"
314
+ "${ userHome } /.nix-profile/share/man"
295
315
"/nix/var/nix/profiles/default/share/man"
296
316
] } "
297
317
"SSL_CERT_FILE=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
298
318
"GIT_SSL_CAINFO=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
299
319
"NIX_SSL_CERT_FILE=/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"
300
- "NIX_PATH=/nix/var/nix/profiles/per-user/root /channels:/root /.nix-defexpr/channels"
320
+ "NIX_PATH=/nix/var/nix/profiles/per-user/${ uname } /channels:${ userHome } /.nix-defexpr/channels"
301
321
] ;
302
322
} ;
303
323
0 commit comments