Skip to content

Commit 4b5094d

Browse files
committed
[LW-10649] Conform to the expectations of the already deployed update-runner
1 parent d17ba50 commit 4b5094d

File tree

3 files changed

+128
-24
lines changed

3 files changed

+128
-24
lines changed

installers/nix/linux.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let
2222
daedalus-frontend = writeScriptBin "daedalus-frontend" ''
2323
#!${stdenv.shell}
2424
25-
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
25+
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
2626
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
2727
2828
cd "''${DAEDALUS_DIR}/${cluster}/"
@@ -39,7 +39,7 @@ let
3939
export PATH="${daedalus-frontend}/bin/:${daedalus-bridge}/bin:$PATH"
4040
''}
4141
42-
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
42+
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
4343
export CLUSTER=${cluster'}
4444
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
4545
export DAEDALUS_CONFIG=${if sandboxed then "/nix/var/nix/profiles/profile-${linuxClusterBinName}/etc" else daedalus-config}

nix/internal/linux-self-extracting-archive.sh

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ if [ "$our_checksum" != 00000000000000000000000000000000000000000000000000000000
1414
exit 1
1515
fi
1616

17+
# We could be running as an auto-update from Daedalus ≤5.4.0 using `nix-chroot`,
18+
# then our behavior should be different:
19+
in_chroot=
20+
if [ "${1-}" = "--extract" ] && [ -e /nix/var/nix/profiles/profile-@CLUSTER@ ] ; then
21+
in_chroot=1
22+
fi
23+
1724
target="$HOME"/.daedalus/@CLUSTER@
1825
if [ -e "$target" ] ; then
1926
echo "Found an older version of Daedalus "@CLUSTER@", removing it..."
@@ -22,18 +29,8 @@ if [ -e "$target" ] ; then
2229
fi
2330
mkdir -p "$target"
2431

25-
old_nix="$HOME"/.daedalus/nix
26-
if [ -e "$old_nix" ] ; then
27-
old_clusters=$(ls "$old_nix"/var/nix/profiles/ | grep '^profile-' | grep -v '[0-9]' || true)
28-
if [ "$old_clusters" = "profile-"@CLUSTER@ ] ; then
29-
# If the user *only* used Mainnet (most common), we're safe to remove the whole ~/.daedalus/nix:
30-
echo "Found an older non-portable version of Daedalus in $old_nix, removing it..."
31-
chmod -R +w "$old_nix"
32-
rm -rf "$old_nix"
33-
else
34-
# But if it contains more Daedaluses for other networks, we can't risk breaking them:
35-
echo "Found older non-portable versions of Daedalus for multiple networks in $old_nix, you are free to remove the directory manually, if you no longer use them."
36-
fi
32+
if [ -z "$in_chroot" ] ; then
33+
@REMOVE_OLD_NIX_CHROOT@
3734
fi
3835

3936
progress_cmd="cat"
@@ -47,6 +44,16 @@ fi
4744
echo "Unpacking..."
4845
tail -c+$((skip_bytes+1)) "$0" | $progress_cmd | tar -C "$target" -xJ
4946

47+
# Move a faux `satisfyOldUpdateRunner` to $PWD so that the old `update-runner` doesn’t error out:
48+
chmod +w "$target"
49+
chmod -R +w "$target"/dat
50+
if [ -z "$in_chroot" ] ; then
51+
rm -rf "$target"/dat
52+
else
53+
mv "$target"/dat ./
54+
fi
55+
chmod -w "$target"
56+
5057
echo "Setting up a .desktop entry..."
5158
mkdir -p "$HOME"/.local/share/applications
5259
chmod -R +w "$target"/share/applications
@@ -55,10 +62,22 @@ sed -r "s+INSERT_ICON_PATH_HERE+$target/share/icon_large.png+g" -i "$target"/sha
5562
chmod -R -w "$target"/share/applications
5663
ln -sf "$target"/share/applications/*.desktop "$HOME"/.local/share/applications/Daedalus-@CLUSTER@.desktop
5764

58-
echo "Installed successfully!"
59-
echo
60-
echo "Now, either:"
61-
echo " 1. In a terminal, run $(echo "$target"/bin/* | sed -r "s+^$HOME+~+")"
62-
echo " 2. Or select Start -> Daedalus "@CLUSTER@"."
65+
# Backwards compatibility:
66+
XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}"
67+
DAEDALUS_DIR="${XDG_DATA_HOME}/Daedalus"
68+
mkdir -p "$DAEDALUS_DIR"/@CLUSTER@/
69+
ln -sf "$target"/bin/daedalus "$DAEDALUS_DIR"/@CLUSTER@/namespaceHelper
70+
71+
if [ -z "$in_chroot" ] ; then
72+
echo "Installed successfully!"
73+
echo
74+
echo "Now, either:"
75+
echo " 1. In a terminal, run $(echo "$target"/bin/* | sed -r "s+^$HOME+~+")"
76+
echo " 2. Or select Start -> Daedalus "@CLUSTER@"."
77+
else
78+
# Now, the currently running `cardano-launcher` will try to restart `daedalus-frontend`. See the
79+
# new `daedalus-frontend` stub in `satisfyOldUpdateRunner`.
80+
:
81+
fi
6382

6483
exit 0

nix/internal/x86_64-linux.nix

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ in rec {
282282
export ENTRYPOINT_DIR
283283
export PATH="$ENTRYPOINT_DIR/libexec:$PATH"
284284
285-
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
285+
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
286286
export CLUSTER=${cluster}
287287
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
288288
export DAEDALUS_CONFIG="$ENTRYPOINT_DIR/config"
@@ -291,6 +291,10 @@ in rec {
291291
mkdir -p "''${DAEDALUS_DIR}/${cluster}"/Secrets
292292
cd "''${DAEDALUS_DIR}/${cluster}/"
293293
294+
if [ -e "''${DAEDALUS_DIR}/${cluster}"/daedalus_lockfile.old-nix-chroot ] ; then
295+
rm "''${DAEDALUS_DIR}/${cluster}"/daedalus_lockfile.old-nix-chroot || true
296+
fi
297+
294298
exec cardano-launcher --config "$ENTRYPOINT_DIR/config/launcher-config.yaml"
295299
''} $out/bin/daedalus
296300
@@ -323,14 +327,31 @@ in rec {
323327
icon = "INSERT_ICON_PATH_HERE";
324328
});
325329

330+
# On Windows/macOS, auto-update just launches the new installer, and exits the previous Daedalus.
331+
#
332+
# On Linux, however, it starts `update-runner` on PATH (updateRunnerBin from launcher-config.yaml),
333+
# with argv[1] set to the path of the new installer, shows progress, and then exits Daedalus with
334+
# code 20. Which is a signal for `cardano-launcher` to restart `daedalus-frontend` on PATH
335+
# (daedalusBin from launcher-config.yaml).
336+
#
337+
# The old `update-runner` (≤5.4.0) has certain expectations about what's in the installer, so we
338+
# have to provide a shim, when our installer is being run with `--extract`.
339+
#
340+
# We also have to change `daedalus-frontend` in the old nix-chroot sandbox to use `escape-hatch` to
341+
# start the new `daedalus` with `xdg-run`, and `exit 0`. But first move the `daedalus_lockfile`
342+
# to another location, because during the first launch, there will briefly be 2 `cardano-launcher`s.
343+
#
344+
# Now, if there’s no previous nix-chroot, i.e. if the upgrade is from ≥5.5.0 to something newer,
345+
# then TODO
346+
326347
# XXX: Be *super careful* changing this!!! You WILL DELETE user data if you make a mistake.
327348
selfExtractingArchive = genClusters (cluster: let
328349
scriptTemplate = __replaceStrings [
329350
"@CLUSTER@"
330-
"@SHA256_SUM@"
351+
"@REMOVE_OLD_NIX_CHROOT@"
331352
] [
332353
(lib.escapeShellArg cluster)
333-
(lib.escapeShellArg)
354+
removeOldNixChroot.${cluster}
334355
] (__readFile ./linux-self-extracting-archive.sh);
335356
script = __replaceStrings ["1010101010"] [(toString (1000000000 + __stringLength scriptTemplate))] scriptTemplate;
336357
version = (builtins.fromJSON (builtins.readFile ../../package.json)).version;
@@ -344,7 +365,7 @@ in rec {
344365
chmod +x $target
345366
346367
echo 'Compressing (xz)...'
347-
tar -cJf tmp-archive.tar.xz -C ${newBundle.${cluster}} .
368+
tar -cJf tmp-archive.tar.xz -C ${newBundle.${cluster}} . -C ${satisfyOldUpdateRunner.${cluster}} .
348369
349370
checksum=$(sha256sum tmp-archive.tar.xz | cut -d' ' -f1)
350371
sed -r "s/0000000000000000000000000000000000000000000000000000000000000000/$checksum/g" -i $target
@@ -356,6 +377,70 @@ in rec {
356377
echo "file binary-dist \"$target\"" >$out/nix-support/hydra-build-products
357378
'');
358379

380+
# We only want to remove the old nix-chroot, if it contains only one cluster
381+
# variant – the one we’re updating. Otherwise, we’ll break other cluster
382+
# installations of that user.
383+
removeOldNixChroot = genClusters (cluster: ''
384+
old_nix="$HOME"/.daedalus/nix
385+
old_etc="$HOME"/.daedalus/etc
386+
if [ -e "$old_nix" ] ; then
387+
old_clusters=$(ls "$old_nix"/var/nix/profiles/ | grep '^profile-' | grep -v '[0-9]' || true)
388+
if [ "$old_clusters" = "profile-${cluster}" ] ; then
389+
# If the user *only* used Mainnet (most common), we're safe to remove the whole ~/.daedalus/nix:
390+
echo "Found an older non-portable version of Daedalus in $old_nix, removing it..."
391+
chmod -R +w "$old_nix"
392+
chmod -R +w "$old_etc" || true
393+
rm -rf "$old_nix" "$old_etc" || true
394+
else
395+
# But if it contains more Daedaluses for other networks, we can't risk breaking them:
396+
echo "Found older non-portable versions of Daedalus for multiple networks in $old_nix, you are free to remove the directory manually, if you no longer use them."
397+
fi
398+
fi
399+
'');
400+
401+
satisfyOldUpdateRunner = genClusters (cluster: let
402+
tarball = pkgs.callPackage (pkgs.path + "/nixos/lib/make-system-tarball.nix") {
403+
fileName = "tarball"; # don't rename
404+
contents = [];
405+
storeContents = [{
406+
symlink = "firstGeneration";
407+
object = pkgs.buildEnv {
408+
name = "profile";
409+
paths = [
410+
# We need an auto-update stub to hook into the old auto-update process, and make it launch
411+
# our new portable Daedalus outside of `nix-chroot`.
412+
#
413+
# The previously running `cardano-launcher` (inside the `nix-chroot`) will try to restart
414+
# `/bin/daedalus-frontend` after a successful update, so we have to hook here: start the new
415+
# independent (nohup, setsid, don’t inherit fds) Deadalus (and new cardano-launcher) after
416+
# moving the old cardano-launcher’s lockfile out of the way. The old launcher will exit
417+
# after our `exit 0` below.
418+
#
419+
# And at the very end we get rid of the previous `nix-chroot`.
420+
(pkgs.writeShellScriptBin "daedalus-frontend" ''
421+
set -euo pipefail
422+
echo -n "$HOME/.daedalus${pkgs.writeScript "escape-and-scrap-chroot" ''
423+
#!/bin/sh
424+
set -eu
425+
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
426+
DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
427+
mv "$DAEDALUS_DIR"/${cluster}/daedalus_lockfile \
428+
"$DAEDALUS_DIR"/${cluster}/daedalus_lockfile.old-nix-chroot || true
429+
nohup setsid ~/.daedalus/${cluster}/bin/daedalus </dev/null >/dev/null 2>/dev/null &
430+
sleep 5
431+
${removeOldNixChroot.${cluster}}
432+
''}" >/escape-hatch
433+
exit 0
434+
'')
435+
];
436+
};
437+
}];
438+
};
439+
in pkgs.runCommandNoCC "satisfy-old-update-runner" {} ''
440+
mkdir -p $out/dat${tarball}
441+
cp -r ${tarball}/. $out/dat${tarball}/
442+
'');
443+
359444
electronBin = pkgs.stdenv.mkDerivation {
360445
name = "electron-${electronVersion}";
361446
src = linuxSources.electron;
@@ -440,7 +525,7 @@ in rec {
440525
set -ex
441526
442527
443-
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
528+
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
444529
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus/${cluster}"
445530
mkdir -pv $DAEDALUS_DIR/Logs/pub
446531

0 commit comments

Comments
 (0)