Skip to content

Commit dcd1dfb

Browse files
committed
[LW-12086] Don’t use symlinks
1 parent 51eb36d commit dcd1dfb

File tree

1 file changed

+65
-43
lines changed

1 file changed

+65
-43
lines changed

nix/internal/any-darwin.nix

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ in rec {
113113
sed -r 's+@executable_path/\$relative_bin_to_lib/\$lib_dir+@executable_path+g' -i $out/bundle-macos.sh
114114
'';
115115

116-
mkBundle = exes: let
117-
unbundled = pkgs.linkFarm "exes" (lib.mapAttrsToList (name: path: {
118-
name = "bin/" + name;
119-
inherit path;
120-
}) exes);
116+
# XXX: they cannot be a symlinks, because:
117+
# 1) cardano-launcher looks at its own `realpath` to determine DAEDALUS_INSTALL_DIRECTORY,
118+
# 2) symlinks under Contents/MacOS break code signing, and notarization.
119+
mkBundle = exeName: target: let
120+
unbundled = pkgs.linkFarm "exes" { "bin/${exeName}" = target; };
121121
in (import nix-bundle-exe-same-dir {
122122
inherit pkgs;
123123
bin_dir = "bundle";
@@ -132,35 +132,53 @@ in rec {
132132
) + ''
133133
mv $out/bundle/* $out/
134134
rmdir $out/bundle
135+
${moveDylibsToSubdir exeName}
135136
'';
136137
});
137138

138-
# XXX: cardano-launcher cannot be a symlink, because it looks at its own
139-
# `realpath` to determine DAEDALUS_INSTALL_DIRECTORY:
140-
bundle-cardano-launcher = (mkBundle {
141-
"cardano-launcher" = common.cardano-shell.haskellPackages.cardano-launcher.components.exes.cardano-launcher + "/bin/cardano-launcher";
142-
}).overrideAttrs (drv: {
143-
buildCommand = let exeName = "cardano-launcher"; in drv.buildCommand + ''
144-
(
145-
cd $out
146-
mkdir -p bundle-${exeName}
147-
mv *.dylib bundle-${exeName}/
148-
otool -L ${exeName} \
149-
| grep -E '^\s*@executable_path' \
139+
moveDylibsToSubdir = exeName: ''
140+
(
141+
export PATH=${lib.makeBinPath (with pkgs; [ darwin.cctools darwin.binutils darwin.sigtool nukeReferences ])}:"$PATH"
142+
143+
cd $out
144+
mkdir -p ${exeName}-lib
145+
mv *.dylib ${exeName}-lib/
146+
otool -L ${exeName} \
147+
| { grep -E '^\s*@executable_path' || true ; } \
148+
| sed -r 's/^\s*//g ; s/ \(.*//g' \
149+
| while IFS= read -r lib ; do
150+
install_name_tool -change "$lib" "$(sed <<<"$lib" -r 's,@executable_path/,@executable_path/${exeName}-lib/,g')" ${exeName}
151+
done
152+
nuke-refs ${exeName}
153+
codesign -f -s - ${exeName} || true
154+
155+
cd ${exeName}-lib
156+
ls *.dylib | while IFS= read -r dylib ; do
157+
otool -L "$dylib" \
158+
| { grep -E '^\s*@executable_path' || true ; } \
150159
| sed -r 's/^\s*//g ; s/ \(.*//g' \
151160
| while IFS= read -r lib ; do
152-
install_name_tool -change "$lib" "$(sed <<<"$lib" -r 's,@executable_path/,@executable_path/bundle-${exeName}/,g')" ${exeName}
153-
done
154-
)
155-
'';
156-
});
161+
install_name_tool -change "$lib" "$(sed <<<"$lib" -r 's,@executable_path/,@loader_path/,g')" "$dylib"
162+
done
163+
nuke-refs "$dylib"
164+
codesign -f -s - "$dylib" || true
165+
done
166+
)
167+
'';
157168

158-
bundle-cardano-node = mkBundle { "cardano-node" = lib.getExe common.cardano-node; };
159-
bundle-cardano-cli = mkBundle { "cardano-cli" = lib.getExe common.cardano-cli; };
160-
bundle-cardano-address = mkBundle { "cardano-address" = lib.getExe common.cardano-address; };
161-
bundle-cardano-wallet = pkgs.runCommandNoCC "bundle-cardano-wallet" {} ''cp -r ${common.cardano-wallet}/bin $out''; # upstream bundles it
162-
bundle-mock-token-metadata-server = mkBundle { "mock-token-metadata-server" = lib.getExe common.mock-token-metadata-server; };
163-
bundle-local-cluster = mkBundle { "local-cluster" = lib.getExe common.walletPackages.local-cluster; };
169+
bundle-cardano-launcher = mkBundle "cardano-launcher" (common.cardano-shell.haskellPackages.cardano-launcher.components.exes.cardano-launcher + "/bin/cardano-launcher");
170+
bundle-cardano-node = mkBundle "cardano-node" (lib.getExe common.cardano-node);
171+
bundle-cardano-cli = mkBundle "cardano-cli" (lib.getExe common.cardano-cli);
172+
bundle-cardano-address = mkBundle "cardano-address" (lib.getExe common.cardano-address);
173+
bundle-mock-token-metadata-server = mkBundle "mock-token-metadata-server" (lib.getExe common.mock-token-metadata-server);
174+
bundle-local-cluster = mkBundle "local-cluster" (lib.getExe common.walletPackages.local-cluster);
175+
176+
# Unfortunately they bundle it upstream, but not in a subdir:
177+
bundle-cardano-wallet = pkgs.runCommandNoCC "bundle-cardano-wallet" {} ''
178+
cp -r ${common.cardano-wallet}/bin $out
179+
chmod -R +w $out
180+
${moveDylibsToSubdir "cardano-wallet"}
181+
'';
164182

165183
# HID.node and others depend on `/nix/store`, we have to bundle them, too:
166184
bundleNodeJsNativeModule = pkgs.writeShellScript "bundleNodeJsNativeModule" ''
@@ -177,10 +195,18 @@ in rec {
177195
sed -r 's/@executable_path/@loader_path/g' -i "$tmpdir"/bundle-macos.sh
178196
bash "$tmpdir"/bundle-macos.sh "$tmpdir" "$target"
179197
rm "$tmpdir"/bundle-macos.sh
180-
mv "$tmpdir/bundle" "$(dirname "$target")/bundle-$(basename "$target")"
198+
199+
mv "$tmpdir/bundle" "$(dirname "$target")/$(basename "$target")"-lib
181200
rmdir "$tmpdir"
182201
rm "$target"
183-
ln -s "bundle-$(basename "$target")/$(basename "$target")" "$target"
202+
mv "$(dirname "$target")/$(basename "$target")-lib/$(basename "$target")" "$target"
203+
204+
otool -L "$target" \
205+
| { grep -E '^\s*@loader_path' || true ; } \
206+
| sed -r 's/^\s*//g ; s/ \(.*//g' \
207+
| while IFS= read -r lib ; do
208+
install_name_tool -change "$lib" "$(sed <<<"$lib" -r 's,@loader_path/,@loader_path/'"$(basename "$target")"'-lib/,g')" "$target"
209+
done
184210
'';
185211

186212
package = genClusters (cluster: let
@@ -259,19 +285,15 @@ in rec {
259285
cp installers/launcher-config.yaml "$dataDir"/
260286
261287
cp -r ${bundle-cardano-launcher}/. "$dir"/
262-
263-
${lib.concatStringsSep "\n" (lib.mapAttrsToList (exe: bundle: ''
264-
cp -r ${bundle} "$dir"/bundle-${exe}
265-
ln -s bundle-${exe}/${exe} "$dir"/${exe}
266-
'') ({
267-
"cardano-node" = bundle-cardano-node;
268-
"cardano-cli" = bundle-cardano-cli;
269-
"cardano-address" = bundle-cardano-address;
270-
"cardano-wallet" = bundle-cardano-wallet;
271-
} // (lib.optionalAttrs (cluster == "selfnode") {
272-
"mock-token-metadata-server" = bundle-mock-token-metadata-server;
273-
"local-cluster" = bundle-local-cluster;
274-
})))}
288+
cp -r ${bundle-cardano-node }/. "$dir"/
289+
cp -r ${bundle-cardano-cli }/. "$dir"/
290+
cp -r ${bundle-cardano-address }/. "$dir"/
291+
cp -r ${bundle-cardano-wallet }/. "$dir"/
292+
293+
${lib.optionalString (cluster == "selfnode") ''
294+
cp -r ${bundle-mock-token-metadata-server}/. "$dir"/
295+
cp -r ${bundle-local-cluster }/. "$dir"/
296+
''}
275297
276298
cp installers/{config.yaml,genesis.json,topology.yaml} "$dataDir"/
277299
${if (cluster != "selfnode") then ''

0 commit comments

Comments
 (0)