17
17
storeDir ? builtins . storeDir ,
18
18
pigz ,
19
19
zstd ,
20
+ stdenv ,
21
+ glibc ,
20
22
} :
21
23
let
22
24
inherit ( lib )
70
72
command ? null ,
71
73
run ? null ,
72
74
maxLayers ? 100 ,
75
+ uname ? "nixbld" ,
73
76
} :
74
77
assert lib . assertMsg ( ! ( drv . drvAttrs . __structuredAttrs or false ) )
75
78
"streamNixShellImage: Does not work with the derivation ${ drv . name } because it uses __structuredAttrs" ;
83
86
exec ${ lib . escapeShellArg ( valueToString drv . drvAttrs . builder ) } ${ lib . escapeShellArgs ( map valueToString drv . drvAttrs . args ) }
84
87
'' ;
85
88
86
- staticPath = "${ dirOf shell } :${ lib . makeBinPath [ builder ] } " ;
89
+ staticPath = "${ dirOf shell } :${
90
+ lib . makeBinPath (
91
+ lib . flatten [
92
+ builder
93
+ drv . buildInputs
94
+ ]
95
+ )
96
+ } " ;
87
97
88
98
# https://github.com/NixOS/nix/blob/2.8.0/src/nix-build/nix-build.cc#L493-L526
89
99
rcfile = writeText "nix-shell-rc" ''
109
119
'' }
110
120
'' ;
111
121
122
+ nixConfFile = writeText "nix-conf" ''
123
+ experimental-features = nix-command flakes
124
+ '' ;
125
+
126
+ etcNixConf = runCommand "etcd-nix-conf" { } ''
127
+ mkdir -p $out/etc/nix/
128
+ ln -s ${ nixConfFile } $out/etc/nix/nix.conf
129
+ '' ;
130
+
112
131
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/globals.hh#L464-L465
113
132
sandboxBuildDir = "/build" ;
114
133
142
161
# TODO: Make configurable?
143
162
NIX_BUILD_CORES = "1" ;
144
163
164
+ # Make sure we get the libraries for C and C++ in.
165
+ LD_LIBRARY_PATH = lib . makeLibraryPath [ stdenv . cc . cc ] ;
145
166
}
146
167
// drvEnv
147
168
// {
@@ -153,10 +174,10 @@ let
153
174
TMPDIR = sandboxBuildDir ;
154
175
TEMPDIR = sandboxBuildDir ;
155
176
TMP = sandboxBuildDir ;
156
- TEMP = sandboxBuildDir ;
177
+ TEMP = "/tmp" ;
157
178
158
179
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L1015-L1019
159
- PWD = sandboxBuildDir ;
180
+ PWD = homeDirectory ;
160
181
161
182
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L1071-L1074
162
183
# We don't set it here because the output here isn't handled in any special way
@@ -172,16 +193,17 @@ let
172
193
contents = [
173
194
binSh
174
195
usrBinEnv
196
+ etcNixConf
175
197
( fakeNss . override {
176
198
# Allows programs to look up the build user's home directory
177
199
# https://github.com/NixOS/nix/blob/ffe155abd36366a870482625543f9bf924a58281/src/libstore/build/local-derivation-goal.cc#L906-L910
178
200
# Slightly differs however: We use the passed-in homeDirectory instead of sandboxBuildDir.
179
201
# We're doing this because it's arguably a bug in Nix that sandboxBuildDir is used here: https://github.com/NixOS/nix/issues/6379
180
202
extraPasswdLines = [
181
- "nixbld :x:${ toString uid } :${ toString gid } :Build user:${ homeDirectory } :/noshell "
203
+ "${ toString uname } :x:${ toString uid } :${ toString gid } :Build user:${ homeDirectory } :${ lib . escapeShellArg shell } "
182
204
] ;
183
205
extraGroupLines = [
184
- "nixbld :!:${ toString gid } :"
206
+ "${ toString uname } :!:${ toString gid } :"
185
207
] ;
186
208
} )
187
209
] ;
197
219
# Gives the user control over the build directory
198
220
mkdir -p .${ sandboxBuildDir }
199
221
chown -R ${ toString uid } :${ toString gid } .${ sandboxBuildDir }
222
+
223
+ mkdir -p .${ homeDirectory }
224
+ chown -R ${ toString uid } :${ toString gid } .${ homeDirectory }
225
+
226
+ mkdir -p ./tmp
227
+ chown -R ${ toString uid } :${ toString gid } ./tmp
228
+
229
+ mkdir -p ./etc/skel
230
+ chown -R ${ toString uid } :${ toString gid } ./etc/skel
231
+
232
+ # Create traditional /lib or /lib64 as needed.
233
+ # For aarch64 (arm64):
234
+ if [ -e "${ glibc } /lib/ld-linux-aarch64.so.1" ]; then
235
+ mkdir -p ./lib
236
+ ln -s "${ glibc } /lib/ld-linux-aarch64.so.1" ./lib/ld-linux-aarch64.so.1
237
+ fi
238
+
239
+ # For x86_64:
240
+ if [ -e "${ glibc } /lib64/ld-linux-x86-64.so.2" ]; then
241
+ mkdir -p ./lib64
242
+ ln -s "${ glibc } /lib64/ld-linux-x86-64.so.2" ./lib64/ld-linux-x86-64.so.2
243
+ fi
200
244
'' ;
201
245
202
246
# Run this image as the given uid/gid
@@ -215,11 +259,12 @@ let
215
259
shell
216
260
rcfile
217
261
] ;
218
- config . WorkingDir = sandboxBuildDir ;
262
+ config . WorkingDir = homeDirectory ;
219
263
config . Env = lib . mapAttrsToList ( name : value : "${ name } =${ value } " ) envVars ;
220
264
} ;
221
265
in
222
266
{
267
+ inherit streamNixShellImage ;
223
268
224
269
# This function streams a docker image that behaves like a nix-shell for a derivation
225
270
# Docs: doc/build-helpers/images/dockertools.section.md
0 commit comments