Skip to content

Commit

Permalink
modules/nixos/common/update: switch to apply script, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zowoq committed Nov 11, 2024
1 parent 872b09a commit 8685581
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions modules/nixos/common/update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@ arch=$(uname -m)
hostname=$(uname -n)
p=$(curl -L https://buildbot.nix-community.org/nix-outputs/nix-community/infra/master/"$arch"-linux.host-"$hostname")

if [[ "$(readlink /run/booted-system)" == "$p" ]]; then
exit 0
fi
if [[ "$(readlink /run/current-system)" == "$p" ]]; then
exit 0
fi

nix-store --realise "$p"
nix-env --profile /nix/var/nix/profiles/system --set "$p"
nix-store --add-root /run/inbound-system "$p"

booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules} && cat /run/booted-system/kernel-params)"
built="$(readlink "$p"/{initrd,kernel,kernel-modules} && cat "$p"/kernel-params)"
if [[ $booted != "$built" ]]; then
if [[ -e /run/current-system ]]; then
echo "--- diff to current-system"
nvd diff /run/current-system "$p"
echo "---"
fi
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
booted="$(
readlink /run/booted-system/{initrd,kernel,kernel-modules} &&
cat /run/booted-system/kernel-params
)"

inbound="$(
readlink /run/inbound-system/{initrd,kernel,kernel-modules} &&
cat /run/inbound-system/kernel-params
)"

if [[ $booted != "$inbound" ]]; then
echo "--- diff to current-system"
nvd diff /run/current-system /run/inbound-system
echo "---"
/run/inbound-system/bin/apply boot
# don't use kexec if system is virtualized, reboots are fast enough
if ! systemd-detect-virt -q; then
kexec --load "$p"/kernel --initrd="$p"/initrd --append="$(cat "$p"/kernel-params) init=$p/init"
kexec \
--load /run/inbound-system/kernel \
--initrd=/run/inbound-system/initrd \
--append="$(cat /run/inbound-system/kernel-params) init=/run/inbound-system/init"
fi
if [[ ! -e /run/systemd/shutdown/scheduled ]]; then
shutdown -r "+$(shuf -i 5-60 -n 1)"
fi
else
/nix/var/nix/profiles/system/bin/switch-to-configuration switch
/run/inbound-system/bin/apply switch
fi

0 comments on commit 8685581

Please sign in to comment.