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 25, 2024
1 parent 4ecf127 commit be14073
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions modules/nixos/common/update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@ if [[ "$(readlink /run/current-system)" == "$p" ]]; then
exit 0
fi

nix-store --option narinfo-cache-negative-ttl 0 --realise "$p"
nix-env --profile /nix/var/nix/profiles/system --set "$p"
nix-store \
--option narinfo-cache-negative-ttl 0 \
--add-root /run/inbound-system \
--realise "$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 be14073

Please sign in to comment.