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
|
{
lib,
stdenv,
bashNonInteractive,
bzip2,
coreutils,
cpio,
curlMinimal,
darwin,
diffutils,
dumpnar,
findutils,
gawk,
gettext,
gmpxx,
gnugrep,
gnumake,
gnused,
gnutar,
gzip,
jq,
ld64,
libffi,
libiconv,
libxml2,
llvmPackages,
ncurses,
nukeReferences,
oniguruma,
openssl,
patch,
pbzx,
runCommand,
writeText,
xarMinimal,
xz,
zlib,
}:
stdenv.mkDerivation (finalAttrs: {
name = "stdenv-bootstrap-tools";
nativeBuildInputs = [
dumpnar
nukeReferences
];
buildCommand =
let
inherit (lib) getBin getDev getLib;
coreutils_ = coreutils.override (prevArgs: {
# We want coreutils without ACL support.
aclSupport = false;
# Cannot use a single binary build, or it gets dynamically linked against gmp.
singleBinary = false;
});
# Avoid messing with libkrb5 and libnghttp2.
curl_ = curlMinimal.override (prevArgs: {
gssSupport = false;
http2Support = false;
scpSupport = false;
});
unpackScript = writeText "bootstrap-tools-unpack.sh" ''
set -euo pipefail
echo Unpacking the bootstrap tools... >&2
mkdir $out
tar xf "$1" -C $out
updateInstallName() {
local path="$1"
cp "$path" "$path.new"
install_name_tool -id "$path" "$path.new"
codesign -f -i "$(basename "$path")" -s - "$path.new"
mv -f "$path.new" "$path"
}
find $out/lib -type f -name '*.dylib' -print0 | while IFS= read -r -d $'\0' lib; do
updateInstallName "$lib"
done
# as is a wrapper around clang. need to replace the nuked store paths
sed -i 's|/.*/bin/|'"$out"'/bin/|' $out/bin/as
# Provide a gunzip script.
cat > $out/bin/gunzip <<EOF
#!$out/bin/sh
exec $out/bin/gzip -d "\$@"
EOF
chmod +x $out/bin/gunzip
# Provide fgrep/egrep.
echo "#! $out/bin/sh" > $out/bin/egrep
echo "exec $out/bin/grep -E \"\$@\"" >> $out/bin/egrep
echo "#! $out/bin/sh" > $out/bin/fgrep
echo "exec $out/bin/grep -F \"\$@\"" >> $out/bin/fgrep
cat >$out/bin/dsymutil << EOF
#!$out/bin/sh
EOF
chmod +x $out/bin/egrep $out/bin/fgrep $out/bin/dsymutil
'';
in
''
mkdir -p $out/bin $out/include $out/lib $out/lib/darwin
chmod -R u+w $out/include
cp -rL ${getDev libiconv}/include/* $out/include
cp -rL ${getDev gnugrep.pcre2}/include/* $out/include
# Copy binutils.
for i in as ld ar ranlib nm strip otool install_name_tool lipo codesign_allocate; do
cp ${getBin darwin.binutils-unwrapped}/bin/$i $out/bin
done
cp -d ${getLib ld64}/lib/libcodedirectory*.dylib $out/lib
# Copy coreutils, bash, etc.
cp ${getBin coreutils_}/bin/* $out/bin
(cd $out/bin && rm vdir dir sha*sum pinky factor pathchk shuf who whoami shred users && rm -f runcon)
cp -d ${getBin bashNonInteractive}/bin/{ba,}sh $out/bin
cp -d ${getBin diffutils}/bin/* $out/bin
cp ${getBin findutils}/bin/{find,xargs} $out/bin
cp -d ${getBin gawk}/bin/{g,}awk $out/bin
cp -d ${getBin gnugrep}/bin/grep $out/bin
cp -d ${getBin gnumake}/bin/* $out/bin
cp -d ${getBin gnused}/bin/* $out/bin
cp -d ${getBin patch}/bin/* $out/bin
cp -d ${getLib gettext}/lib/libintl*.dylib $out/lib
cp -d ${getLib gnugrep.pcre2}/lib/libpcre2*.dylib $out/lib
cp -d ${getLib libiconv}/lib/lib*.dylib $out/lib
cp -d ${getLib libxml2}/lib/libxml2*.dylib $out/lib
cp -d ${getLib ncurses}/lib/libncurses*.dylib $out/lib
# copy package extraction tools
cp -d ${getBin bzip2}/bin/b{,un}zip2 $out/bin
cp ${getBin cpio}/bin/cpio $out/bin
cp ${getBin gnutar}/bin/tar $out/bin
cp ${getBin gzip}/bin/gzip $out/bin/gzip
cp ${getBin pbzx}/bin/pbzx $out/bin
cp ${getBin xz}/bin/xz $out/bin
cp -d ${getLib bzip2}/lib/libbz2*.dylib $out/lib
cp -d ${getLib gmpxx}/lib/libgmp*.dylib $out/lib
cp -d ${getLib xarMinimal}/lib/libxar*.dylib $out/lib
cp -d ${getLib xz}/lib/liblzma*.dylib $out/lib
cp -d ${getLib zlib}/lib/libz*.dylib $out/lib
# This used to be in-nixpkgs, but now is in the bundle
# because I can't be bothered to make it partially static
cp ${getBin curl_}/bin/curl $out/bin
cp -d ${getLib curl_}/lib/libcurl*.dylib $out/lib
cp -d ${getLib openssl}/lib/*.dylib $out/lib
# Copy what we need of clang
cp -d ${getBin llvmPackages.clang-unwrapped}/bin/clang{,++,-cl,-cpp,-[0-9]*} $out/bin
cp -d ${getLib llvmPackages.clang-unwrapped}/lib/libclang-cpp*.dylib $out/lib
cp -rd ${getLib llvmPackages.clang-unwrapped}/lib/clang $out/lib
cp -d ${getLib llvmPackages.libcxx}/lib/libc++*.dylib $out/lib
mkdir -p $out/lib/darwin
cp -d ${getLib llvmPackages.compiler-rt}/lib/darwin/libclang_rt.{,profile_}osx.a $out/lib/darwin
cp -d ${getLib llvmPackages.compiler-rt}/lib/libclang_rt.{,profile_}osx.a $out/lib
cp -d ${getLib llvmPackages.llvm}/lib/libLLVM.dylib $out/lib
cp -d ${getLib libffi}/lib/libffi*.dylib $out/lib
cp -rd ${getDev llvmPackages.libcxx}/include/c++ $out/include
# Copy tools needed to build the SDK
cp -d ${getBin jq}/bin/* $out/bin
cp -d ${getBin llvmPackages.llvm}/bin/llvm-readtapi $out/bin
cp -d ${getLib jq}/lib/lib*.dylib $out/lib
cp -d ${getLib oniguruma}/lib/lib*.dylib $out/lib
# copy sigtool
cp -d ${getBin darwin.sigtool}/bin/{codesign,sigtool} $out/bin
# tools needed to unpack bootstrap archive
mkdir -p unpack/bin unpack/lib
cp -d ${getBin bashNonInteractive}/bin/{ba,}sh unpack/bin
cp ${getBin coreutils_}/bin/mkdir unpack/bin
cp ${getBin gnutar}/bin/tar unpack/bin
cp ${getBin xz}/bin/xz unpack/bin
cp -d ${getLib gettext}/lib/libintl*.dylib unpack/lib
cp -d ${getLib libiconv}/lib/lib*.dylib unpack/lib
cp -d ${getLib xz}/lib/liblzma*.dylib unpack/lib
cp ${unpackScript} unpack/bootstrap-tools-unpack.sh
#
# All files copied. Perform processing to update references to point into
# the archive
#
chmod -R u+w $out unpack
# - change nix store library paths to use @rpath/library
# - if needed add an rpath containing lib/
# - strip executable
rpathify() {
local libs=$(${stdenv.cc.targetPrefix}otool -L "$1" | tail -n +2 | grep -o "$NIX_STORE.*-\S*" || true)
local lib rpath
for lib in $libs; do
${stdenv.cc.targetPrefix}install_name_tool -change $lib "@rpath/$(basename "$lib")" "$1"
done
case "$(dirname "$1")" in
*/bin)
# Strip executables even further
${stdenv.cc.targetPrefix}strip "$i"
rpath='@executable_path/../lib'
;;
*/lib)
# the '/.' suffix is required
rpath='@loader_path/.'
;;
*/lib/darwin)
rpath='@loader_path/..'
;;
*)
echo unknown executable $1 >&2
exit 1
;;
esac
# if shared object contains references add an rpath to lib/
if ${stdenv.cc.targetPrefix}otool -l "$1"| grep -q '@rpath/'; then
${stdenv.cc.targetPrefix}install_name_tool -add_rpath "$rpath" "$1"
fi
}
# check that linked library paths exist in lib
# must be run after rpathify is performed
checkDeps() {
local deps=$(${stdenv.cc.targetPrefix}otool -l "$1"| grep -o '@rpath/[^ ]*' || true)
local lib
shopt -s extglob
for lib in $deps; do
local root="''${1/\/@(lib|bin)\/*}"
if [[ ! -e $root/''${lib/@rpath/lib} ]]; then
echo "error: $1 missing lib for $lib" >&2
exit 1
fi
done
shopt -u extglob
}
for i in {unpack,$out}/bin/* {unpack,$out}/lib{,/darwin}/*.dylib; do
if [[ ! -L $i ]] && isMachO "$i"; then
rpathify "$i"
checkDeps "$i"
fi
done
nuke-refs {unpack,$out}/bin/*
nuke-refs {unpack,$out}/lib/*
nuke-refs $out/lib/darwin/*
mkdir $out/.pack
mv $out/* $out/.pack
mv $out/.pack $out/pack
mkdir $out/on-server
cp -r unpack $out
XZ_OPT="-9 -T $NIX_BUILD_CORES" tar cvJf $out/on-server/bootstrap-tools.tar.xz \
--hard-dereference --sort=name --numeric-owner --owner=0 --group=0 --mtime=@1 -C $out/pack .
dumpnar $out/unpack | xz -9 -T $NIX_BUILD_CORES > $out/on-server/unpack.nar.xz
'';
allowedReferences = [ ];
passthru = {
bootstrapFiles = {
bootstrapTools = "${finalAttrs.finalPackage}/on-server/bootstrap-tools.tar.xz";
unpack = runCommand "unpack" { allowedReferences = [ ]; } ''
cp -r ${finalAttrs.finalPackage}/unpack $out
'';
};
};
meta = {
teams = [ lib.teams.darwin ];
};
})
|