#!/bin/bash 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 \n" printf " Host to build on, or 'local' for localhost\n" exit 1 } 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 fi TARGET_HOST=${POSITIONAL_ARGS[1]:-} case "$ACTION" in "switch" | "boot" | "test") SUDO=sudo ;; *) SUDO= ;; esac ARGS=("$ACTION") # 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 fi 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") else ARGS+=("--flake" "." "--show-trace") fi if [ -n "$BUILD_HOST" ] && [ "$BUILD_HOST" != "local" ]; then ARGS+=("--build-host" "root@$BUILD_HOST") printf "Building on root@%s\n" "$BUILD_HOST" fi printf " > nixos-rebuild %s\n" "${ARGS[*]}" $SUDO nixos-rebuild "${ARGS[@]}"