2023-04-19 01:01:07 +02:00
|
|
|
#!/bin/bash
|
2022-04-18 18:12:06 +02:00
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
function print_help_and_exit() {
|
|
|
|
printf "Usage: rebuild ACTION [HOST]\n"
|
|
|
|
printf "\n"
|
|
|
|
printf "Arguments:\n"
|
|
|
|
printf " ACTION\n"
|
|
|
|
printf " See 'man 8 nixos-rebuild'\n"
|
|
|
|
printf " [HOST]\n"
|
|
|
|
printf " Host to build on\n"
|
|
|
|
printf "\n"
|
|
|
|
printf "Options:\n"
|
|
|
|
printf " -x, --external\n"
|
|
|
|
printf " Assume that the target host is outside my realm of my home\n"
|
|
|
|
printf " -b, --build <host>\n"
|
|
|
|
printf " Host to build on, or 'local' for localhost\n"
|
2022-04-18 18:12:06 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
POSITIONAL_ARGS=()
|
|
|
|
EXTERNAL=
|
|
|
|
BUILD_HOST=
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
-h|--help)
|
|
|
|
print_help_and_exit
|
|
|
|
;;
|
|
|
|
-x|--external)
|
|
|
|
EXTERNAL=yes
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
-b|--build)
|
|
|
|
shift # past argument
|
|
|
|
BUILD_HOST=$1
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
print_help_and_exit
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
ACTION=${POSITIONAL_ARGS[0]:-}
|
|
|
|
if [ -z "${ACTION+x}" ]; then
|
|
|
|
print_help_and_exit
|
2022-04-18 18:12:06 +02:00
|
|
|
fi
|
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
TARGET_HOST=${POSITIONAL_ARGS[1]:-}
|
2022-04-18 18:12:06 +02:00
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
case "$ACTION" in
|
|
|
|
"switch" | "boot" | "test")
|
|
|
|
SUDO=sudo
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
SUDO=
|
|
|
|
;;
|
2022-04-18 18:12:06 +02:00
|
|
|
esac
|
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
ARGS=("$ACTION")
|
2022-04-18 18:12:06 +02:00
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
# Only use my cache if the host is close to me
|
|
|
|
if [ -z "$EXTERNAL" ]; then
|
|
|
|
# Only use my cache if it's up
|
|
|
|
if personal-cache --ping; then
|
|
|
|
ARGS+=("--option" "extra-substituters" "$(personal-cache --url)")
|
|
|
|
printf "Cache is up and running!\n"
|
|
|
|
else
|
|
|
|
printf "Cache is down!\n"
|
|
|
|
fi
|
2022-04-18 18:12:06 +02:00
|
|
|
fi
|
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
if [ -n "$TARGET_HOST" ]; then
|
|
|
|
if [ -z "$BUILD_HOST" ]; then
|
|
|
|
ARGS+=("--build-host" "root@$TARGET_HOST")
|
|
|
|
fi
|
|
|
|
ARGS+=("--target-host" "root@$TARGET_HOST" "--flake" ".#$TARGET_HOST")
|
2022-04-18 18:12:06 +02:00
|
|
|
else
|
2023-04-19 01:01:07 +02:00
|
|
|
ARGS+=("--flake" "." "--show-trace")
|
2022-04-18 18:12:06 +02:00
|
|
|
fi
|
|
|
|
|
2023-08-31 17:35:18 +02:00
|
|
|
if [ -n "$BUILD_HOST" ] && [ "$BUILD_HOST" != "local" ]; then
|
2023-08-27 13:53:27 +02:00
|
|
|
ARGS+=("--build-host" "root@$BUILD_HOST")
|
|
|
|
printf "Building on root@%s\n" "$BUILD_HOST"
|
|
|
|
fi
|
2022-04-18 18:12:06 +02:00
|
|
|
|
2023-08-27 13:53:27 +02:00
|
|
|
printf " > nixos-rebuild %s\n" "${ARGS[*]}"
|
|
|
|
$SUDO nixos-rebuild "${ARGS[@]}"
|