-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathdefault.nix
More file actions
208 lines (190 loc) · 4.48 KB
/
default.nix
File metadata and controls
208 lines (190 loc) · 4.48 KB
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
{
nixpkgs ? import <nixpkgs> { },
}:
with nixpkgs;
rec {
toStorePath =
target:
# If a store path has been given but is not a derivation, add the missing context
# to it so it will be propagated properly as a build input.
if !(lib.isDerivation target) && lib.isStorePath target then
let
path = toString target;
in
builtins.appendContext path {
"${path}" = {
path = true;
};
}
# Otherwise, add to the store. This takes care of appending the store path
# in the context automatically.
else
"${target}";
arx =
{
drvToBundle,
archive,
startup,
}:
stdenv.mkDerivation {
name = if drvToBundle != null then "${drvToBundle.pname}-arx" else "arx";
passthru = {
inherit drvToBundle;
};
buildCommand = ''
# tmpdir has a additional `/` in the beginning to work around `QualifiedPath` checking for `|/|./|../|`
${haskellPackages.arx}/bin/arx tmpx \
--tmpdir '/$HOME/.cache' \
--shared \
-rm! ${archive} \
-o $out // ${startup}
chmod +x $out
'';
};
maketar =
{ targets }:
let
closure = closureInfo { rootPaths = targets; };
in
stdenv.mkDerivation {
name = "maketar";
buildInputs = [ perl ];
exportReferencesGraph = map (x: [
("closure-" + baseNameOf x)
x
]) targets;
buildCommand = ''
storePaths=$(cat ${closure}/store-paths)
# https://reproducible-builds.org/docs/archives
tar -cf - \
--owner=0 --group=0 --mode=u+rw,uga+r \
--hard-dereference \
--mtime="@$SOURCE_DATE_EPOCH" \
--format=gnu \
--sort=name \
$storePaths | bzip2 -z > $out
'';
};
# TODO: eventually should this go in nixpkgs?
nix-user-chroot = lib.makeOverridable stdenv.mkDerivation {
name = "nix-user-chroot-2c52b5f";
src = ./nix-user-chroot;
buildInputs = [
stdenv.cc.cc.libgcc or null
];
makeFlags = [ ];
# hack to use when /nix/store is not available
postFixup = ''
exe=$out/bin/nix-user-chroot
patchelf \
--set-interpreter .$(patchelf --print-interpreter $exe) \
--set-rpath $(patchelf --print-rpath $exe | sed 's|/nix/store/|./nix/store/|g') \
$exe
'';
installPhase = ''
runHook preInstall
mkdir -p $out/bin/
cp nix-user-chroot $out/bin/nix-user-chroot
runHook postInstall
'';
meta.platforms = lib.platforms.linux;
};
makebootstrap =
{
targets,
startup,
drvToBundle ? null,
}:
arx {
inherit drvToBundle startup;
archive = maketar {
inherit targets;
};
};
makeStartup =
{
target,
nixUserChrootFlags,
nix-user-chroot',
run,
initScript,
}:
let
# Avoid re-adding a store path into the store
path = toStorePath target;
in
writeScript "startup" ''
#!/bin/sh
${initScript}
.${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${path}${run} "$@"
'';
nix-bootstrap =
{
target,
extraTargets ? [ ],
run,
nix-user-chroot' ? nix-user-chroot,
nixUserChrootFlags ? "",
initScript ? "",
}:
let
script = makeStartup {
inherit
target
nixUserChrootFlags
nix-user-chroot'
run
initScript
;
};
in
makebootstrap {
startup = ".${script} '\"$@\"'";
targets = [ "${script}" ] ++ extraTargets;
};
nix-bootstrap-nix =
{
target,
run,
extraTargets ? [ ],
}:
nix-bootstrap-path {
inherit target run;
extraTargets = [
gnutar
bzip2
xz
gzip
coreutils
bash
]
++ extraTargets;
};
# special case adding path to the environment before launch
nix-bootstrap-path =
let
nix-user-chroot'' =
targets:
nix-user-chroot.overrideDerivation (o: {
buildInputs = o.buildInputs ++ targets;
makeFlags = o.makeFlags ++ [
''ENV_PATH="${lib.makeBinPath targets}"''
];
});
in
{
target,
extraTargets ? [ ],
run,
initScript ? "",
}:
nix-bootstrap {
inherit
target
extraTargets
run
initScript
;
nix-user-chroot' = nix-user-chroot'' extraTargets;
};
}