forked from DavHau/nix-portable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
517 lines (417 loc) · 16.2 KB
/
default.nix
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
with builtins;
{
bwrap,
nix,
proot,
unzip,
zip,
unixtools,
busybox ? pkgs.pkgsStatic.busybox,
cacert ? pkgs.cacert,
compression ? "zstd -19 -T0",
git ? pkgs.git,
gnutar ? pkgs.pkgsStatic.gnutar,
lib ? pkgs.lib,
mkDerivation ? pkgs.stdenv.mkDerivation,
nixpkgsSrc ? pkgs.path,
perl ? pkgs.perl,
pkgs ? import <nixpkgs> {},
xz ? pkgs.pkgsStatic.xz,
zstd ? pkgs.pkgsStatic.zstd,
...
}@inp:
with lib;
let
maketar = targets:
mkDerivation {
name = "maketar";
nativeBuildInputs = [ perl zstd ];
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
buildCommand = ''
storePaths=$(perl ${pkgs.pathsFromGraph} ./closure-*)
mkdir $out
echo $storePaths > $out/index
cp -r ${pkgs.closureInfo { rootPaths = targets; }} $out/closureInfo
tar -cf - \
--owner=0 --group=0 --mode=u+rw,uga+r \
--hard-dereference \
$storePaths | ${compression} > $out/tar
'';
};
packStaticBin = binPath: let
binName = (last (splitString "/" binPath)); in
pkgs.runCommand
binName
{ nativeBuildInputs = [ pkgs.upx ]; }
''
mkdir -p $out/bin
upx -9 -o $out/bin/${binName} ${binPath}
'';
installBin = pkg: bin: ''
unzip -qqoj "\$self" ${ lib.removePrefix "/" "${pkg}/bin/${bin}"} -d \$dir/bin
chmod +wx \$dir/bin/${bin};
'';
caBundleZstd = pkgs.runCommand "cacerts" {} "cat ${cacert}/etc/ssl/certs/ca-bundle.crt | ${inp.zstd}/bin/zstd -19 > $out";
bwrap = packStaticBin "${inp.bwrap}/bin/bwrap";
proot = packStaticBin "${inp.proot}/bin/proot";
zstd = packStaticBin "${inp.zstd}/bin/zstd";
# the default nix store contents to extract when first used
storeTar = maketar ([ cacert nix nixpkgsSrc ]);
# The runtime script which unpacks the necessary files to $HOME/.nix-portable
# and then executes nix via proot or bwrap
# Some shell expressions will be evaluated at build time and some at run time.
# Variables/expressions escaped via `\$` will be evaluated at run time
runtimeScript = ''
#!/usr/bin/env bash
set -e
if [ -n "\$NP_DEBUG" ] && [ "\$NP_DEBUG" -ge 2 ]; then
set -x
fi
# &3 is our error out which we either forward to &2 or to /dev/null
# depending on the setting
if [ -n "\$NP_DEBUG" ] && [ "\$NP_DEBUG" -ge 1 ]; then
debug(){
echo \$@ || true
}
exec 3>&2
else
debug(){
true
}
exec 3>/dev/null
fi
# to reference this script's file
self="\$(realpath \''${BASH_SOURCE[0]})"
# fingerprint will be inserted by builder
fingerprint="_FINGERPRINT_PLACEHOLDER_"
# user specified location for program files and nix store
[ -z "\$NP_LOCATION" ] && NP_LOCATION="\$HOME"
dir="\$NP_LOCATION/.nix-portable"
mkdir -p \$dir/bin
# the fingerprint being present inside a file indicates that
# this version of nix-portable has already been initialized
if test -e \$dir/conf/fingerprint && [ "\$(cat \$dir/conf/fingerprint)" == "\$fingerprint" ]; then
newNPVersion=false
else
newNPVersion=true
fi
# Nix portable ships its own nix.conf
export NIX_CONF_DIR=\$dir/conf/
create_nix_conf(){
sandbox=\$1
mkdir -p \$dir/conf/
rm -f \$dir/conf/nix.conf
echo "build-users-group = " > \$dir/conf/nix.conf
echo "experimental-features = nix-command flakes" >> \$dir/conf/nix.conf
echo "use-sqlite-wal = false" >> \$dir/conf/nix.conf
echo "sandbox-paths = /bin/sh=\$dir/busybox/bin/busybox" >> \$dir/conf/nix.conf
echo "sandbox = \$sandbox" >> \$dir/conf/nix.conf
}
### install files
PATH_OLD="\$PATH"
# as soon as busybox is unpacked, restrict PATH to busybox to ensure reproducibility of this script
# only unpack binaries if necessary
if [ "\$newNPVersion" == "false" ]; then
debug "binaries already installed"
export PATH="\$dir/busybox/bin"
else
debug "installing files"
mkdir -p \$dir/emptyroot
# install busybox
mkdir -p \$dir/busybox/bin
(base64 -d> "\$dir/busybox/bin/busybox" && chmod +x "\$dir/busybox/bin/busybox") << END
$(cat ${busybox}/bin/busybox | base64)
END
busyBins="${toString (attrNames (filterAttrs (d: type: type == "symlink") (readDir "${inp.busybox}/bin")))}"
for bin in \$busyBins; do
[ ! -e "\$dir/busybox/bin/\$bin" ] && ln -s busybox "\$dir/busybox/bin/\$bin"
done
export PATH="\$dir/busybox/bin"
# install other binaries
${installBin zstd "zstd"}
${installBin proot "proot"}
${installBin bwrap "bwrap"}
# install ssl cert bundle
unzip -poj "\$self" ${ lib.removePrefix "/" "${caBundleZstd}"} | \$dir/bin/zstd -d > \$dir/ca-bundle.crt
create_nix_conf false
fi
### setup SSL
# find ssl certs or use from nixpkgs
debug "figuring out ssl certs"
if [ -z "\$SSL_CERT_FILE" ]; then
debug "SSL_CERT_FILE not defined. trying to find certs automatically"
if [ -e /etc/ssl/certs/ca-bundle.crt ]; then
export SSL_CERT_FILE=\$(realpath /etc/ssl/certs/ca-bundle.crt)
debug "found /etc/ssl/certs/ca-bundle.crt with real path \$SSL_CERT_FILE"
elif [ -e /etc/ssl/certs/ca-certificates.crt ]; then
export SSL_CERT_FILE=\$(realpath /etc/ssl/certs/ca-certificates.crt)
debug "found /etc/ssl/certs/ca-certificates.crt with real path \$SSL_CERT_FILE"
elif [ ! -e /etc/ssl/certs ]; then
debug "/etc/ssl/certs does not exist. Will use certs from nixpkgs."
export SSL_CERT_FILE=\$dir/ca-bundle.crt
else
debug "certs seem to reside in /etc/ssl/certs. No need to set up anything"
fi
fi
if [ -n "\$SSL_CERT_FILE" ]; then
sslBind="\$(realpath \$SSL_CERT_FILE) \$dir/ca-bundle.crt"
export SSL_CERT_FILE="\$dir/ca-bundle.crt"
else
sslBind=/etc/ssl
fi
### detecting existing git installation
if [ -n "\$NP_MINIMAL" ]; then
doInstallGit=false
else
existingGit="\$(PATH="\$PATH_OLD:\$PATH" which git 2>&3)" || existingGit=
# if git doesn't exist or resides in /nix/store/*, we need to install git
# inside the wrapped environment.
if [ -z "\$existingGit" ] || [[ "\$(realpath \$existingGit)" == /nix/store/* ]] ; then
doInstallGit=true
else
doInstallGit=false
fi
fi
storePathOfFile(){
file=\$(realpath \$1)
sPath="\$(echo \$file | awk -F "/" 'BEGIN{OFS="/";}{print \$2,\$3,\$4}')"
echo "/\$sPath"
}
collectBinds(){
### gather paths to bind for proot
# we cannot bind / to / without running into a lot of trouble, therefore
# we need to collect all top level directories and bind them inside an empty root
pathsTopLevel="\$(find / -mindepth 1 -maxdepth 1 -not -name nix -not -name dev)"
toBind=""
for p in \$pathsTopLevel; do
if [ -e "\$p" ]; then
real=\$(realpath \$p)
if [ -e "\$real" ]; then
if [[ "\$real" == /nix/store/* ]]; then
storePath=\$(storePathOfFile \$real)
toBind="\$toBind \$storePath \$storePath"
else
toBind="\$toBind \$real \$p"
fi
fi
fi
done
# TODO: add /var/run/dbus/system_bus_socket
paths="/etc/host.conf /etc/hosts /etc/hosts.equiv /etc/mtab /etc/netgroup /etc/networks /etc/passwd /etc/group /etc/nsswitch.conf /etc/resolv.conf /etc/localtime \$HOME"
for p in \$paths; do
if [ -e "\$p" ]; then
real=\$(realpath \$p)
if [ -e "\$real" ]; then
if [[ "\$real" == /nix/store/* ]]; then
storePath=\$(storePathOfFile \$real)
toBind="\$toBind \$storePath \$storePath"
else
toBind="\$toBind \$real \$real"
fi
fi
fi
done
# if we're on a nixos, the /bin/sh symlink will point
# to a /nix/store path which doesn't exit inside the wrapped env
# we fix this by binding busybox/bin to /bin
if test -s /bin/sh && [[ "\$(realpath /bin/sh)" == /nix/store/* ]]; then
toBind="\$toBind \$dir/busybox/bin /bin"
fi
}
makeBindArgs(){
arg=\$1; shift
sep=\$1; shift
binds=""
while :; do
if [ -n "\$1" ]; then
from="\$1"; shift
to="\$1"; shift
binds="\$binds \$arg \$from\$sep\$to";
else
break
fi
done
}
### select container runtime
debug "figuring out which runtime to use"
[ -z "\$NP_BWRAP" ] && NP_BWRAP=\$(PATH="\$PATH_OLD:\$PATH" which bwrap 2>/dev/null) || true
[ -z "\$NP_BWRAP" ] && NP_BWRAP=\$dir/bin/bwrap
debug "bwrap executable: \$NP_BWRAP"
[ -z "\$NP_PROOT" ] && NP_PROOT=\$(PATH="\$PATH_OLD:\$PATH" which proot 2>/dev/null) || true
[ -z "\$NP_PROOT" ] && NP_PROOT=\$dir/bin/proot
debug "proot executable: \$NP_PROOT"
if [ -z "\$NP_RUNTIME" ]; then
# check if bwrap works properly
if \$NP_BWRAP --bind \$dir/emptyroot / --bind \$dir/ /nix --bind \$dir/busybox/bin/busybox "\$dir/true" "\$dir/true" 2>&3 ; then
debug "bwrap seems to work on this system -> will use bwrap"
NP_RUNTIME=bwrap
else
debug "bwrap doesn't work on this system -> will use proot"
NP_RUNTIME=proot
fi
else
debug "runtime selected via NP_RUNTIME : \$NP_RUNTIME"
fi
if [ "\$NP_RUNTIME" == "bwrap" ]; then
collectBinds
makeBindArgs --bind " " \$toBind \$sslBind
run="\$NP_BWRAP \$BWRAP_ARGS \\
--bind \$dir/emptyroot /\\
--dev-bind /dev /dev\\
--bind \$dir/ /nix\\
\$binds"
# --bind \$dir/busybox/bin/busybox /bin/sh\\
else
# proot
collectBinds
makeBindArgs -b ":" \$toBind \$sslBind
run="\$NP_PROOT \$PROOT_ARGS\\
-r \$dir/emptyroot\\
-b /dev:/dev\\
-b \$dir/store:/nix/store\\
\$binds"
# -b \$dir/busybox/bin/busybox:/bin/sh\\
fi
debug "base command will be: \$run"
### setup environment
export NIX_PATH="\$dir/channels:nixpkgs=\$dir/channels/nixpkgs"
mkdir -p \$dir/channels
[ -h \$dir/channels/nixpkgs ] || ln -s ${nixpkgsSrc} \$dir/channels/nixpkgs
### install nix store
# Install all the nix store paths necessary for the current nix-portable version
# We only unpack missing store paths from the tar archive.
# xz must be in PATH
index="$(cat ${storeTar}/index)"
export missing=\$(
for path in \$index; do
if [ ! -e \$dir/store/\$(basename \$path) ]; then
echo "nix/store/\$(basename \$path)"
fi
done
)
if [ -n "\$missing" ]; then
debug "extracting missing store paths"
(
mkdir -p \$dir/tmp \$dir/store/
rm -rf \$dir/tmp/*
cd \$dir/tmp
unzip -qqp "\$self" ${ lib.removePrefix "/" "${storeTar}/tar"} \
| \$dir/bin/zstd -d \
| tar -x \$missing --strip-components 2
mv \$dir/tmp/* \$dir/store/
)
rm -rf \$dir/tmp
fi
if [ -n "\$missing" ]; then
debug "registering new store paths to DB"
reg="$(cat ${storeTar}/closureInfo/registration)"
cmd="\$run \$dir/store${lib.removePrefix "/nix/store" nix}/bin/nix-store --load-db"
debug "running command: \$cmd"
echo "\$reg" | \$cmd
fi
### select executable
# the executable can either be selected by executing './nix-portable BIN_NAME',
# or by symlinking to nix-portable, in which case the name of the symlink selectes the binary
if [ "\$(basename \$0)" == "nix-portable" ]; then
if [ -z "\$1" ]; then
echo "Error: please specify the nix binary to execute"
echo "Alternatively symlink against \$0"
exit 1
elif [ "\$1" == "debug" ]; then
bin="\$(which \$2)"
shift; shift
else
bin="\$dir/store${lib.removePrefix "/nix/store" nix}/bin/\$1"
shift
fi
else
bin="\$dir/store${lib.removePrefix "/nix/store" nix}/bin/\$(basename \$0)"
fi
### check which runtime has been used previously
lastRuntime=\$(cat "\$dir/conf/last_runtime" 2>&3) || true
### check if nix is funtional with or without sandbox
# sandbox-fallback is not reliable: https://github.com/NixOS/nix/issues/4719
if [ "\$newNPVersion" == "true" ] || [ "\$lastRuntime" != "\$NP_RUNTIME" ]; then
nixBin="\$dir/store${lib.removePrefix "/nix/store" nix}/bin/nix-build"
debug "Testing if nix can build stuff without sandbox"
if ! \$run "\$nixBin" -E "(import <nixpkgs> {}).runCommand \\"test\\" {} \\"echo \$(date) > \\\$out\\"" --option sandbox false >&3 2>&3; then
echo "Fatal error: nix is unable to build packages"
exit 1
fi
debug "Testing if nix sandox is functional"
if ! \$run "\$nixBin" -E "(import <nixpkgs> {}).runCommand \\"test\\" {} \\"echo \$(date) > \\\$out\\"" --option sandbox true >&3 2>&3; then
debug "Sandbox doesn't work -> disabling sandbox"
create_nix_conf false
else
debug "Sandboxed builds work -> enabling sandbox"
create_nix_conf true
fi
fi
### save fingerprint and lastRuntime
if [ "\$newNPVersion" == "true" ]; then
echo -n "\$fingerprint" > "\$dir/conf/fingerprint"
fi
if [ "\$lastRuntime" != \$NP_RUNTIME ]; then
echo -n \$NP_RUNTIME > "\$dir/conf/last_runtime"
fi
### set PATH
# restore original PATH and append busybox
export PATH="\$PATH_OLD:\$dir/busybox/bin"
### install git via nix, if git not installed or git installation is in /nix path
if \$doInstallGit && [ ! -e \$dir/store${lib.removePrefix "/nix/store" git.out} ] ; then
echo "Installing git because it's missing. Disable this by setting 'NP_MINIMAL=1'"
\$run \$dir/store${lib.removePrefix "/nix/store" nix}/bin/nix build --impure --no-link --expr "
(import ${nixpkgsSrc} {}).git.out
"
else
debug "git already installed or not required"
fi
# add git if necessary
\$doInstallGit && export PATH="\$PATH:${git.out}/bin"
### run commands
[ -z "\$NP_RUN" ] && NP_RUN="\$run"
if [ "\$NP_RUNTIME" == "proot" ]; then
debug "running command: \$NP_RUN \$bin \$@"
exec \$NP_RUN \$bin "\$@"
else
cmd="\$NP_RUN \$bin \$@"
debug "running command: \$cmd"
exec \$NP_RUN \$bin "\$@"
fi
'';
runtimeScriptEscaped = replaceStrings ["\""] ["\\\""] runtimeScript;
nixPortable = pkgs.runCommand "nix-portable" {nativeBuildInputs = [unixtools.xxd unzip];} ''
mkdir -p $out/bin
echo "${runtimeScriptEscaped}" > $out/bin/nix-portable.zip
xxd $out/bin/nix-portable.zip | tail
sizeA=$(printf "%08x" `stat -c "%s" $out/bin/nix-portable.zip` | tac -rs ..)
echo 504b 0304 0000 0000 0000 0000 0000 0000 | xxd -r -p >> $out/bin/nix-portable.zip
echo 0000 0000 0000 0000 0000 0200 0000 4242 | xxd -r -p >> $out/bin/nix-portable.zip
sizeB=$(printf "%08x" `stat -c "%s" $out/bin/nix-portable.zip` | tac -rs ..)
echo 504b 0102 0000 0000 0000 0000 0000 0000 | xxd -r -p >> $out/bin/nix-portable.zip
echo 0000 0000 0000 0000 0000 0000 0200 0000 | xxd -r -p >> $out/bin/nix-portable.zip
echo 0000 0000 0000 0000 0000 $sizeA 4242 | xxd -r -p >> $out/bin/nix-portable.zip
echo 504b 0506 0000 0000 0000 0100 3000 0000 | xxd -r -p >> $out/bin/nix-portable.zip
echo $sizeB 0000 0000 0000 0000 0000 0000 | xxd -r -p >> $out/bin/nix-portable.zip
unzip -vl $out/bin/nix-portable.zip
zip="${zip}/bin/zip -0"
$zip $out/bin/nix-portable.zip ${proot}/bin/proot
$zip $out/bin/nix-portable.zip ${bwrap}/bin/bwrap
$zip $out/bin/nix-portable.zip ${zstd}/bin/zstd
$zip $out/bin/nix-portable.zip ${storeTar}/tar
$zip $out/bin/nix-portable.zip ${caBundleZstd}
# create fingerprint
fp=$(sha256sum $out/bin/nix-portable.zip | cut -d " " -f 1)
sed -i "s/_FINGERPRINT_PLACEHOLDER_/$fp/g" $out/bin/nix-portable.zip
# fix broken zip header due to manual modification
${zip}/bin/zip -F $out/bin/nix-portable.zip --out $out/bin/nix-portable-fixed.zip
rm $out/bin/nix-portable.zip
mv $out/bin/nix-portable-fixed.zip $out/bin/nix-portable
chmod +x $out/bin/nix-portable
'';
in
nixPortable.overrideAttrs (prev: {
passthru = (prev.passthru or {}) // {
inherit bwrap proot;
};
})