40 stdenv.mkDerivation (finalAttrs: {
41 name = "stdenv-bootstrap-tools";
50 inherit (lib) getBin getDev getLib;
53 (coreutils.override (prevArgs: {
54 # We want coreutils without ACL support.
56 # Cannot use a single binary build, or it gets dynamically linked against gmp.
60 # Increase header size to be able to inject extra RPATHs. Otherwise
61 # x86_64-darwin build fails as:
62 # https://cache.nixos.org/log/g5wyq9xqshan6m3kl21bjn1z88hx48rh-stdenv-bootstrap-tools.drv
63 NIX_LDFLAGS = (prevAttrs.NIX_LDFLAGS or "") + " -headerpad_max_install_names";
66 # Avoid messing with libkrb5 and libnghttp2.
67 curl_ = curlMinimal.override (prevArgs: {
73 unpackScript = writeText "bootstrap-tools-unpack.sh" ''
76 echo Unpacking the bootstrap tools... >&2
83 cp "$path" "$path.new"
84 install_name_tool -id "$path" "$path.new"
85 codesign -f -i "$(basename "$path")" -s - "$path.new"
86 mv -f "$path.new" "$path"
89 find $out/lib -type f -name '*.dylib' -print0 | while IFS= read -r -d $'\0' lib; do
90 updateInstallName "$lib"
93 # as is a wrapper around clang. need to replace the nuked store paths
94 sed -i 's|/.*/bin/|'"$out"'/bin/|' $out/bin/as
96 # Provide a gunzip script.
97 cat > $out/bin/gunzip <<EOF
99 exec $out/bin/gzip -d "\$@"
101 chmod +x $out/bin/gunzip
103 # Provide fgrep/egrep.
104 echo "#! $out/bin/sh" > $out/bin/egrep
105 echo "exec $out/bin/grep -E \"\$@\"" >> $out/bin/egrep
106 echo "#! $out/bin/sh" > $out/bin/fgrep
107 echo "exec $out/bin/grep -F \"\$@\"" >> $out/bin/fgrep
109 cat >$out/bin/dsymutil << EOF
113 chmod +x $out/bin/egrep $out/bin/fgrep $out/bin/dsymutil
118 mkdir -p $out/bin $out/include $out/lib $out/lib/darwin
120 chmod -R u+w $out/include
121 cp -rL ${getDev libiconv}/include/* $out/include
122 cp -rL ${getDev gnugrep.pcre2}/include/* $out/include
125 for i in as ld ar ranlib nm strip otool install_name_tool lipo codesign_allocate; do
126 cp ${getBin darwin.binutils-unwrapped}/bin/$i $out/bin
128 cp -d ${getLib ld64}/lib/libcodedirectory*.dylib $out/lib
130 # Copy coreutils, bash, etc.
131 cp ${getBin coreutils_}/bin/* $out/bin
132 (cd $out/bin && rm vdir dir sha*sum pinky factor pathchk runcon shuf who whoami shred users)
134 cp -d ${getBin bash}/bin/{ba,}sh $out/bin
135 cp -d ${getBin diffutils}/bin/* $out/bin
136 cp ${getBin findutils}/bin/{find,xargs} $out/bin
137 cp -d ${getBin gawk}/bin/{g,}awk $out/bin
138 cp -d ${getBin gnugrep}/bin/grep $out/bin
139 cp -d ${getBin gnumake}/bin/* $out/bin
140 cp -d ${getBin gnused}/bin/* $out/bin
141 cp -d ${getBin patch}/bin/* $out/bin
143 cp -d ${getLib gettext}/lib/libintl*.dylib $out/lib
144 cp -d ${getLib gnugrep.pcre2}/lib/libpcre2*.dylib $out/lib
145 cp -d ${getLib libiconv}/lib/lib*.dylib $out/lib
146 cp -d ${getLib libxml2}/lib/libxml2*.dylib $out/lib
147 cp -d ${getLib ncurses}/lib/libncurses*.dylib $out/lib
149 # copy package extraction tools
150 cp -d ${getBin bzip2}/bin/b{,un}zip2 $out/bin
151 cp ${getBin cpio}/bin/cpio $out/bin
152 cp ${getBin gnutar}/bin/tar $out/bin
153 cp ${getBin gzip}/bin/.gzip-wrapped $out/bin/gzip
154 cp ${getBin pbzx}/bin/pbzx $out/bin
155 cp ${getBin xz}/bin/xz $out/bin
156 cp -d ${getLib bzip2}/lib/libbz2*.dylib $out/lib
157 cp -d ${getLib gmpxx}/lib/libgmp*.dylib $out/lib
158 cp -d ${getLib xarMinimal}/lib/libxar*.dylib $out/lib
159 cp -d ${getLib xz}/lib/liblzma*.dylib $out/lib
160 cp -d ${getLib zlib}/lib/libz*.dylib $out/lib
162 # This used to be in-nixpkgs, but now is in the bundle
163 # because I can't be bothered to make it partially static
164 cp ${getBin curl_}/bin/curl $out/bin
165 cp -d ${getLib curl_}/lib/libcurl*.dylib $out/lib
166 cp -d ${getLib openssl}/lib/*.dylib $out/lib
168 # Copy what we need of clang
169 cp -d ${getBin llvmPackages.clang-unwrapped}/bin/clang{,++,-cl,-cpp,-[0-9]*} $out/bin
170 cp -d ${getLib llvmPackages.clang-unwrapped}/lib/libclang-cpp*.dylib $out/lib
171 cp -rd ${getLib llvmPackages.clang-unwrapped}/lib/clang $out/lib
173 cp -d ${getLib llvmPackages.libcxx}/lib/libc++*.dylib $out/lib
174 mkdir -p $out/lib/darwin
175 cp -d ${getLib llvmPackages.compiler-rt}/lib/darwin/libclang_rt.{,profile_}osx.a $out/lib/darwin
176 cp -d ${getLib llvmPackages.compiler-rt}/lib/libclang_rt.{,profile_}osx.a $out/lib
177 cp -d ${getLib llvmPackages.llvm}/lib/libLLVM.dylib $out/lib
178 cp -d ${getLib libffi}/lib/libffi*.dylib $out/lib
180 cp -rd ${getDev llvmPackages.libcxx}/include/c++ $out/include
182 # Copy tools needed to build the SDK
183 cp -d ${getBin jq}/bin/* $out/bin
184 cp -d ${getBin libtapi}/bin/* $out/bin
186 cp -d ${getLib jq}/lib/lib*.dylib $out/lib
187 cp -d ${getLib oniguruma}/lib/lib*.dylib $out/lib
188 cp -d ${getLib libtapi}/lib/libtapi*.dylib $out/lib
191 cp -d ${getBin darwin.sigtool}/bin/{codesign,sigtool} $out/bin
193 # tools needed to unpack bootstrap archive
194 mkdir -p unpack/bin unpack/lib
195 cp -d ${getBin bash}/bin/{bash,sh} unpack/bin
196 cp ${getBin coreutils_}/bin/mkdir unpack/bin
197 cp ${getBin gnutar}/bin/tar unpack/bin
198 cp ${getBin xz}/bin/xz unpack/bin
199 cp -d ${getLib gettext}/lib/libintl*.dylib unpack/lib
200 cp -d ${getLib libiconv}/lib/lib*.dylib unpack/lib
201 cp -d ${getLib xz}/lib/liblzma*.dylib unpack/lib
202 cp ${unpackScript} unpack/bootstrap-tools-unpack.sh
205 # All files copied. Perform processing to update references to point into
209 chmod -R u+w $out unpack
211 # - change nix store library paths to use @rpath/library
212 # - if needed add an rpath containing lib/
215 local libs=$(${stdenv.cc.targetPrefix}otool -L "$1" | tail -n +2 | grep -o "$NIX_STORE.*-\S*" || true)
218 ${stdenv.cc.targetPrefix}install_name_tool -change $lib "@rpath/$(basename "$lib")" "$1"
221 case "$(dirname "$1")" in
223 # Strip executables even further
224 ${stdenv.cc.targetPrefix}strip "$i"
225 rpath='@executable_path/../lib'
228 # the '/.' suffix is required
229 rpath='@loader_path/.'
232 rpath='@loader_path/..'
235 echo unkown executable $1 >&2
240 # if shared object contains references add an rpath to lib/
241 if ${stdenv.cc.targetPrefix}otool -l "$1"| grep -q '@rpath/'; then
242 ${stdenv.cc.targetPrefix}install_name_tool -add_rpath "$rpath" "$1"
246 # check that linked library paths exist in lib
247 # must be run after rpathify is performed
249 local deps=$(${stdenv.cc.targetPrefix}otool -l "$1"| grep -o '@rpath/[^ ]*' || true)
253 local root="''${1/\/@(lib|bin)\/*}"
254 if [[ ! -e $root/''${lib/@rpath/lib} ]]; then
255 echo "error: $1 missing lib for $lib" >&2
262 for i in {unpack,$out}/bin/* {unpack,$out}/lib{,/darwin}/*.dylib; do
263 if [[ ! -L $i ]] && isMachO "$i"; then
269 nuke-refs {unpack,$out}/bin/*
270 nuke-refs {unpack,$out}/lib/*
271 nuke-refs $out/lib/darwin/*
275 mv $out/.pack $out/pack
280 XZ_OPT="-9 -T $NIX_BUILD_CORES" tar cvJf $out/on-server/bootstrap-tools.tar.xz \
281 --hard-dereference --sort=name --numeric-owner --owner=0 --group=0 --mtime=@1 -C $out/pack .
282 dumpnar $out/unpack | xz -9 -T $NIX_BUILD_CORES > $out/on-server/unpack.nar.xz
285 allowedReferences = [ ];
289 bootstrapTools = "${finalAttrs.finalPackage}/on-server/bootstrap-tools.tar.xz";
290 unpack = runCommand "unpack" { allowedReferences = [ ]; } ''
291 cp -r ${finalAttrs.finalPackage}/unpack $out
297 maintainers = [ lib.maintainers.copumpkin ];