nixfiles/pkgs/upgrade-system/upgrade-system.sh
emily 1de66b3795
fixed deployment
weird thing happen when you try to copy things from a binary cache to
a local store
2024-05-14 16:23:51 +02:00

105 lines
2.3 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ALLOW_REBOOT=
BRANCH="main"
BINARY_CACHE=
HOST_NAME=$HOSTNAME
NO_DELAY=
while [[ $# -gt 0 ]]; do
case $1 in
--allow-reboot)
ALLOW_REBOOT=1
shift
;;
--branch)
BRANCH="$2"
shift
shift
;;
--cache)
BINARY_CACHE="$2"
shift
shift
;;
--hostname)
HOST_NAME="$2"
shift
shift
;;
--no-delay)
NO_DELAY=1
shift
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done
HYDRA_URL="https://hydra.kyouma.net/job/nixfiles/${BRANCH}/nixosConfigurations.${HOST_NAME}/latest-finished"
NEW_STORE_PATH="$(curl --fail -s -L -H "Accept: application/json" "${HYDRA_URL}" | jq -r ".buildoutputs.out.path")"
OLD_STORE_PATH="$(readlink -f "/nix/var/nix/profiles/system")"
OLD_STORE_SUFFIX="$(echo -n "$OLD_STORE_PATH" | tail -c 7)"
get_old_path () {
for system in $(find /nix/var/nix/profiles/ -printf "%f\n" | grep "system-" | sort -nr); do
local store_path
store_path="$(readlink -f "/nix/var/nix/profiles/${system}")"
if [[ "$(echo -n "$store_path" | tail -c 7)" != "pre-git" ]]; then
OLD_STORE_PATH=$store_path
break
fi
done
}
if [[ "$OLD_STORE_SUFFIX" == "pre-git" ]]; then
echo "Last update was pushed manually"
echo "Getting newest system profile that came from Hydra"
get_old_path
fi
if [[ "$OLD_STORE_PATH" == "$NEW_STORE_PATH" ]]; then
echo "no update available. exiting"
exit 0
fi
echo "Downloading ${NEW_STORE_PATH}"
nix copy --from "${BINARY_CACHE}" --to "daemon" "${NEW_STORE_PATH}"
echo "Adding path to system profile"
nix-env -p "/nix/var/nix/profiles/system" --set "${NEW_STORE_PATH}"
echo "Set new profile as boot target"
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
if [[ -n "$ALLOW_REBOOT" ]]; then
nixos-needsreboot
fi
if [[ -n "$NO_DELAY" ]]; then
delay="1"
else
delay="$(echo -n $RANDOM | tail -c 2)"
fi
if [[ -f "/var/run/reboot-required" ]]; then
if [[ -n "$ALLOW_REBOOT" ]]; then
echo "Rebooting system in ${delay} Minutes"
shutdown -r +"${delay}"
else
echo "Unattended reboot not allowed"
echo "Reboot system manually"
fi
else
echo "Activating system now"
/nix/var/nix/profiles/system/bin/switch-to-configuration switch
echo "Finished upgrade"
fi