Skip to content

Commit

Permalink
[LW-10649] Conform to the expectations of the already deployed update…
Browse files Browse the repository at this point in the history
…-runner
  • Loading branch information
michalrus committed Jul 5, 2024
1 parent d17ba50 commit 4b5094d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 24 deletions.
4 changes: 2 additions & 2 deletions installers/nix/linux.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let
daedalus-frontend = writeScriptBin "daedalus-frontend" ''
#!${stdenv.shell}
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
cd "''${DAEDALUS_DIR}/${cluster}/"
Expand All @@ -39,7 +39,7 @@ let
export PATH="${daedalus-frontend}/bin/:${daedalus-bridge}/bin:$PATH"
''}
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
export CLUSTER=${cluster'}
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
export DAEDALUS_CONFIG=${if sandboxed then "/nix/var/nix/profiles/profile-${linuxClusterBinName}/etc" else daedalus-config}
Expand Down
53 changes: 36 additions & 17 deletions nix/internal/linux-self-extracting-archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ if [ "$our_checksum" != 00000000000000000000000000000000000000000000000000000000
exit 1
fi

# We could be running as an auto-update from Daedalus ≤5.4.0 using `nix-chroot`,
# then our behavior should be different:
in_chroot=
if [ "${1-}" = "--extract" ] && [ -e /nix/var/nix/profiles/profile-@CLUSTER@ ] ; then
in_chroot=1
fi

target="$HOME"/.daedalus/@CLUSTER@
if [ -e "$target" ] ; then
echo "Found an older version of Daedalus "@CLUSTER@", removing it..."
Expand All @@ -22,18 +29,8 @@ if [ -e "$target" ] ; then
fi
mkdir -p "$target"

old_nix="$HOME"/.daedalus/nix
if [ -e "$old_nix" ] ; then
old_clusters=$(ls "$old_nix"/var/nix/profiles/ | grep '^profile-' | grep -v '[0-9]' || true)
if [ "$old_clusters" = "profile-"@CLUSTER@ ] ; then
# If the user *only* used Mainnet (most common), we're safe to remove the whole ~/.daedalus/nix:
echo "Found an older non-portable version of Daedalus in $old_nix, removing it..."
chmod -R +w "$old_nix"
rm -rf "$old_nix"
else
# But if it contains more Daedaluses for other networks, we can't risk breaking them:
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."
fi
if [ -z "$in_chroot" ] ; then
@REMOVE_OLD_NIX_CHROOT@
fi

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

# Move a faux `satisfyOldUpdateRunner` to $PWD so that the old `update-runner` doesn’t error out:
chmod +w "$target"
chmod -R +w "$target"/dat
if [ -z "$in_chroot" ] ; then
rm -rf "$target"/dat
else
mv "$target"/dat ./
fi
chmod -w "$target"

echo "Setting up a .desktop entry..."
mkdir -p "$HOME"/.local/share/applications
chmod -R +w "$target"/share/applications
Expand All @@ -55,10 +62,22 @@ sed -r "s+INSERT_ICON_PATH_HERE+$target/share/icon_large.png+g" -i "$target"/sha
chmod -R -w "$target"/share/applications
ln -sf "$target"/share/applications/*.desktop "$HOME"/.local/share/applications/Daedalus-@CLUSTER@.desktop

echo "Installed successfully!"
echo
echo "Now, either:"
echo " 1. In a terminal, run $(echo "$target"/bin/* | sed -r "s+^$HOME+~+")"
echo " 2. Or select Start -> Daedalus "@CLUSTER@"."
# Backwards compatibility:
XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}"
DAEDALUS_DIR="${XDG_DATA_HOME}/Daedalus"
mkdir -p "$DAEDALUS_DIR"/@CLUSTER@/
ln -sf "$target"/bin/daedalus "$DAEDALUS_DIR"/@CLUSTER@/namespaceHelper

if [ -z "$in_chroot" ] ; then
echo "Installed successfully!"
echo
echo "Now, either:"
echo " 1. In a terminal, run $(echo "$target"/bin/* | sed -r "s+^$HOME+~+")"
echo " 2. Or select Start -> Daedalus "@CLUSTER@"."
else
# Now, the currently running `cardano-launcher` will try to restart `daedalus-frontend`. See the
# new `daedalus-frontend` stub in `satisfyOldUpdateRunner`.
:
fi

exit 0
95 changes: 90 additions & 5 deletions nix/internal/x86_64-linux.nix
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ in rec {
export ENTRYPOINT_DIR
export PATH="$ENTRYPOINT_DIR/libexec:$PATH"
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
export CLUSTER=${cluster}
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
export DAEDALUS_CONFIG="$ENTRYPOINT_DIR/config"
Expand All @@ -291,6 +291,10 @@ in rec {
mkdir -p "''${DAEDALUS_DIR}/${cluster}"/Secrets
cd "''${DAEDALUS_DIR}/${cluster}/"
if [ -e "''${DAEDALUS_DIR}/${cluster}"/daedalus_lockfile.old-nix-chroot ] ; then
rm "''${DAEDALUS_DIR}/${cluster}"/daedalus_lockfile.old-nix-chroot || true
fi
exec cardano-launcher --config "$ENTRYPOINT_DIR/config/launcher-config.yaml"
''} $out/bin/daedalus
Expand Down Expand Up @@ -323,14 +327,31 @@ in rec {
icon = "INSERT_ICON_PATH_HERE";
});

# On Windows/macOS, auto-update just launches the new installer, and exits the previous Daedalus.
#
# On Linux, however, it starts `update-runner` on PATH (updateRunnerBin from launcher-config.yaml),
# with argv[1] set to the path of the new installer, shows progress, and then exits Daedalus with
# code 20. Which is a signal for `cardano-launcher` to restart `daedalus-frontend` on PATH
# (daedalusBin from launcher-config.yaml).
#
# The old `update-runner` (≤5.4.0) has certain expectations about what's in the installer, so we
# have to provide a shim, when our installer is being run with `--extract`.
#
# We also have to change `daedalus-frontend` in the old nix-chroot sandbox to use `escape-hatch` to
# start the new `daedalus` with `xdg-run`, and `exit 0`. But first move the `daedalus_lockfile`
# to another location, because during the first launch, there will briefly be 2 `cardano-launcher`s.
#
# Now, if there’s no previous nix-chroot, i.e. if the upgrade is from ≥5.5.0 to something newer,
# then TODO

# XXX: Be *super careful* changing this!!! You WILL DELETE user data if you make a mistake.
selfExtractingArchive = genClusters (cluster: let
scriptTemplate = __replaceStrings [
"@CLUSTER@"
"@SHA256_SUM@"
"@REMOVE_OLD_NIX_CHROOT@"
] [
(lib.escapeShellArg cluster)
(lib.escapeShellArg)
removeOldNixChroot.${cluster}
] (__readFile ./linux-self-extracting-archive.sh);
script = __replaceStrings ["1010101010"] [(toString (1000000000 + __stringLength scriptTemplate))] scriptTemplate;
version = (builtins.fromJSON (builtins.readFile ../../package.json)).version;
Expand All @@ -344,7 +365,7 @@ in rec {
chmod +x $target
echo 'Compressing (xz)...'
tar -cJf tmp-archive.tar.xz -C ${newBundle.${cluster}} .
tar -cJf tmp-archive.tar.xz -C ${newBundle.${cluster}} . -C ${satisfyOldUpdateRunner.${cluster}} .
checksum=$(sha256sum tmp-archive.tar.xz | cut -d' ' -f1)
sed -r "s/0000000000000000000000000000000000000000000000000000000000000000/$checksum/g" -i $target
Expand All @@ -356,6 +377,70 @@ in rec {
echo "file binary-dist \"$target\"" >$out/nix-support/hydra-build-products
'');

# We only want to remove the old nix-chroot, if it contains only one cluster
# variant – the one we’re updating. Otherwise, we’ll break other cluster
# installations of that user.
removeOldNixChroot = genClusters (cluster: ''
old_nix="$HOME"/.daedalus/nix
old_etc="$HOME"/.daedalus/etc
if [ -e "$old_nix" ] ; then
old_clusters=$(ls "$old_nix"/var/nix/profiles/ | grep '^profile-' | grep -v '[0-9]' || true)
if [ "$old_clusters" = "profile-${cluster}" ] ; then
# If the user *only* used Mainnet (most common), we're safe to remove the whole ~/.daedalus/nix:
echo "Found an older non-portable version of Daedalus in $old_nix, removing it..."
chmod -R +w "$old_nix"
chmod -R +w "$old_etc" || true
rm -rf "$old_nix" "$old_etc" || true
else
# But if it contains more Daedaluses for other networks, we can't risk breaking them:
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."
fi
fi
'');

satisfyOldUpdateRunner = genClusters (cluster: let
tarball = pkgs.callPackage (pkgs.path + "/nixos/lib/make-system-tarball.nix") {
fileName = "tarball"; # don't rename
contents = [];
storeContents = [{
symlink = "firstGeneration";
object = pkgs.buildEnv {
name = "profile";
paths = [
# We need an auto-update stub to hook into the old auto-update process, and make it launch
# our new portable Daedalus outside of `nix-chroot`.
#
# The previously running `cardano-launcher` (inside the `nix-chroot`) will try to restart
# `/bin/daedalus-frontend` after a successful update, so we have to hook here: start the new
# independent (nohup, setsid, don’t inherit fds) Deadalus (and new cardano-launcher) after
# moving the old cardano-launcher’s lockfile out of the way. The old launcher will exit
# after our `exit 0` below.
#
# And at the very end we get rid of the previous `nix-chroot`.
(pkgs.writeShellScriptBin "daedalus-frontend" ''
set -euo pipefail
echo -n "$HOME/.daedalus${pkgs.writeScript "escape-and-scrap-chroot" ''
#!/bin/sh
set -eu
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus"
mv "$DAEDALUS_DIR"/${cluster}/daedalus_lockfile \
"$DAEDALUS_DIR"/${cluster}/daedalus_lockfile.old-nix-chroot || true
nohup setsid ~/.daedalus/${cluster}/bin/daedalus </dev/null >/dev/null 2>/dev/null &
sleep 5
${removeOldNixChroot.${cluster}}
''}" >/escape-hatch
exit 0
'')
];
};
}];
};
in pkgs.runCommandNoCC "satisfy-old-update-runner" {} ''
mkdir -p $out/dat${tarball}
cp -r ${tarball}/. $out/dat${tarball}/
'');

electronBin = pkgs.stdenv.mkDerivation {
name = "electron-${electronVersion}";
src = linuxSources.electron;
Expand Down Expand Up @@ -440,7 +525,7 @@ in rec {
set -ex
test -z "$XDG_DATA_HOME" && { XDG_DATA_HOME="''${HOME}/.local/share"; }
XDG_DATA_HOME="''${XDG_DATA_HOME:-''${HOME}/.local/share}"
export DAEDALUS_DIR="''${XDG_DATA_HOME}/Daedalus/${cluster}"
mkdir -pv $DAEDALUS_DIR/Logs/pub
Expand Down

0 comments on commit 4b5094d

Please sign in to comment.