dotfiles/deploy

82 lines
1.8 KiB
Bash
Executable file

#!/usr/bin/env bash
set -e
# TODO: use `nixos-rebuild build-vm`
usage="Usage: $(basename $0) [OPTIONS]
Options:
-f, --fresh Remove old content in the nixstore (good for debugging)
-b, --bootloader Reinstall the bootloader
-r, --remote Locally build and remotely deploy Colmena hive
-h, --help Show this message (^_^)"
# delete all cached entries
# to make the system from scratch
collect_garbage () {
sudo nix-collect-garbage --delete-old
}
rebuild_flake () {
# make sure all changes are visible to nixos
git add . --verbose
local FLAGS=
if [ "$1" = "reinstall-bootloader" ]; then
FLAGS="--install-bootloader"
# sudo nixos-rebuild switch --flake . --install-bootloader
# STC_DISPLAY_ALL_UNITS=1 (verbose, show output of all units)
fi
# LOG="$(mktemp /tmp/rebuild-XXXXXXXX)"
LOG="./rebuild.log"
echo "[*] Logging to $LOG"
sudo nixos-rebuild switch --flake . $FLAGS 2>&1 | tee "$LOG"
#nixos-rebuild build --flake .# --cores 8 -j 1
}
deploy_hive () {
echo "[+] Adding keys to ssh-agent"
ssh-add ~/.ssh/id_hyrule
printf "\n"
git add . --verbose
# Deploy to all Colmena hives
colmena build --experimental-flake-eval
colmena apply --experimental-flake-eval
# colmena apply --on hyrule --experimental-flake-eval
}
# check which flags were given
flag_fresh=false
flag_bootloader=false
for flag in "$@"; do
case "$flag" in
-r|--remote)
deploy_hive
exit 0 ;;
-f|--fresh)
flag_fresh=true ;;
-b|--bootloader)
flag_bootloader=true ;;
-h|--help)
echo "$usage"
exit 0 ;;
*)
echo "[!] Unknown flag \"$flag\""
exit 1 ;;
esac
done
# delete cached items in nixstore
if [ "$flag_fresh" = true ]; then
collect_garbage
exit 0
fi
# nixos-rebuild switch ...
if [ "$flag_bootloader" = true ]; then
collect_garbage
rebuild_flake "reinstall-bootloader"
else
rebuild_flake
fi