2024-05-12 19:54:39 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2024-05-13 12:17:37 +02:00
|
|
|
ALLOW_REBOOT=
|
2024-05-14 03:08:06 +02:00
|
|
|
BRANCH="main"
|
2024-05-14 11:10:01 +02:00
|
|
|
HOST_NAME=$HOSTNAME
|
2024-05-14 03:08:06 +02:00
|
|
|
NO_DELAY=
|
2024-05-13 12:17:37 +02:00
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
--allow-reboot)
|
|
|
|
ALLOW_REBOOT=1
|
|
|
|
shift
|
|
|
|
;;
|
2024-05-14 03:08:06 +02:00
|
|
|
--branch)
|
2024-05-14 03:48:04 +02:00
|
|
|
BRANCH="$2"
|
|
|
|
shift
|
2024-05-14 03:08:06 +02:00
|
|
|
shift
|
|
|
|
;;
|
2024-05-14 11:10:01 +02:00
|
|
|
--hostname)
|
|
|
|
HOST_NAME="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-05-14 03:08:06 +02:00
|
|
|
--no-delay)
|
|
|
|
NO_DELAY=1
|
|
|
|
shift
|
|
|
|
;;
|
2024-05-13 12:17:37 +02:00
|
|
|
*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2024-05-14 11:10:01 +02:00
|
|
|
HYDRA_URL="https://hydra.kyouma.net/job/nixfiles/${BRANCH}/nixosConfigurations.${HOST_NAME}/latest-finished"
|
2024-05-12 19:54:39 +02:00
|
|
|
|
|
|
|
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 "https://cache.kyouma.net" "${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
|
|
|
|
|
2024-05-14 11:10:01 +02:00
|
|
|
if [[ -n "$ALLOW_REBOOT" ]]; then
|
|
|
|
nixos-needsreboot
|
|
|
|
fi
|
2024-05-12 19:54:39 +02:00
|
|
|
|
2024-05-14 03:08:06 +02:00
|
|
|
if [[ -n "$NO_DELAY" ]]; then
|
|
|
|
delay="1"
|
|
|
|
else
|
|
|
|
delay="$(echo -n $RANDOM | tail -c 2)"
|
|
|
|
fi
|
|
|
|
|
2024-05-12 19:54:39 +02:00
|
|
|
if [[ -f "/var/run/reboot-required" ]]; then
|
2024-05-13 12:17:37 +02:00
|
|
|
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
|
2024-05-12 19:54:39 +02:00
|
|
|
else
|
|
|
|
echo "Activating system now"
|
|
|
|
/nix/var/nix/profiles/system/bin/switch-to-configuration switch
|
|
|
|
echo "Finished upgrade"
|
|
|
|
fi
|