1{
2 stdenv,
3 lib,
4 zlib,
5 curl,
6 icu,
7 libunwind,
8 libuuid,
9 openssl,
10 lttng-ust_2_12,
11 patchelf,
12 writeShellScriptBin,
13}:
14
15let
16 binaryRPath = lib.makeLibraryPath (
17 [
18 stdenv.cc.cc
19 zlib
20 curl
21 icu
22 libunwind
23 libuuid
24 openssl
25 ]
26 ++ lib.optional stdenv.hostPlatform.isLinux lttng-ust_2_12
27 );
28
29in
30writeShellScriptBin "patch-nupkgs" (
31 ''
32 set -euo pipefail
33 shopt -s nullglob
34 isELF() {
35 local fn="$1"
36 local fd
37 local magic
38 exec {fd}< "$fn"
39 read -r -n 4 -u "$fd" magic
40 exec {fd}<&-
41 if [ "$magic" = $'\177ELF' ]; then return 0; else return 1; fi
42 }
43 cd "$1"
44 ''
45 + lib.optionalString stdenv.hostPlatform.isLinux ''
46 for x in */* */*; do
47 # .nupkg.metadata is written last, so we know the packages is complete
48 [[ -d "$x" ]] && [[ -f "$x"/.nupkg.metadata ]] \
49 && [[ ! -f "$x"/.nix-patched ]] || continue
50 echo "Patching package $x"
51 find "$x" -type f -print0 | while IFS= read -rd "" p; do
52 if [[ "$p" != *.nix-patched ]] \
53 && isELF "$p" \
54 && interpreter=$(${patchelf}/bin/patchelf --print-interpreter "$p") \
55 && [ -n "$interpreter" ]; then
56 tmp="$p".$$.nix-patched
57 # if this fails to copy then another process must have patched it
58 cp --reflink=auto "$p" "$tmp" || continue
59 echo "Patchelfing $p as $tmp"
60
61 ${patchelf}/bin/patchelf \
62 --set-interpreter "${stdenv.cc.bintools.dynamicLinker}" \
63 "$tmp"
64
65 # This makes sure that if the binary requires some specific runtime dependencies, it can find it.
66 # This fixes dotnet-built binaries like crossgen2
67 ${patchelf}/bin/patchelf \
68 --add-needed libicui18n.so \
69 --add-needed libicuuc.so \
70 --add-needed libz.so \
71 --add-needed libssl.so \
72 "$tmp"
73 ${patchelf}/bin/patchelf \
74 --add-rpath "${binaryRPath}" \
75 "$tmp"
76
77 mv "$tmp" "$p"
78 fi
79 done
80 touch "$x"/.nix-patched
81 done
82 ''
83 + lib.optionalString stdenv.hostPlatform.isDarwin ''
84 for x in microsoft.dotnet.ilcompiler/*; do
85 # .nupkg.metadata is written last, so we know the packages is complete
86 [[ -d "$x" ]] && [[ -f "$x"/.nupkg.metadata ]] \
87 && [[ ! -f "$x"/.nix-patched-ilcompiler ]] || continue
88 echo "Patching package $x"
89 pushd "$x"
90 sed -i 's: -no_code_signature_warning::g' build/Microsoft.NETCore.Native.targets
91 sed -i 's:Include="-ld_classic"::g' build/Microsoft.NETCore.Native.Unix.targets
92 touch .nix-patched-ilcompiler
93 popd
94 done
95 ''
96)