2024-04-07 15:32:30 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
FLAKE=
|
|
|
|
TARGET=
|
2024-08-24 17:23:59 +02:00
|
|
|
ON_NEXT_BOOT=
|
2024-08-08 20:46:16 +02:00
|
|
|
USE_SUBSTITUTES="1"
|
|
|
|
USE_REMOTE_SUDO="1"
|
2024-04-07 15:32:30 +02:00
|
|
|
|
|
|
|
ARGS=()
|
|
|
|
OPTS=()
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
--flake)
|
|
|
|
FLAKE="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--target)
|
|
|
|
TARGET="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-08-24 17:23:59 +02:00
|
|
|
--on-next-boot)
|
|
|
|
ON_NEXT_BOOT="1"
|
|
|
|
shift
|
|
|
|
;;
|
2024-08-08 20:46:16 +02:00
|
|
|
--use-substitutes)
|
|
|
|
USE_SUBSTITUTES="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--no-use-substitutes)
|
|
|
|
USE_SUBSTITUTES=""
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--use-remote-sudo)
|
|
|
|
USE_REMOTE_SUDO="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--no-use-remote-sudo)
|
|
|
|
USE_REMOTE_SUDO=""
|
|
|
|
shift
|
|
|
|
;;
|
2024-04-07 15:32:30 +02:00
|
|
|
--)
|
|
|
|
shift
|
|
|
|
OPTS=( "$@" )
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
echo "unknown option: $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ARGS+=("$1")
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
set -- "${ARGS[@]}"
|
|
|
|
|
|
|
|
if [[ -z ${FLAKE} ]]; then
|
|
|
|
# Flake not specified, use working directory
|
|
|
|
FLAKE="$(pwd)"
|
|
|
|
fi
|
|
|
|
|
2024-04-07 19:48:27 +02:00
|
|
|
FLAKE_STORE_PATH="$(nix flake archive --json "${FLAKE}" | jq -r .path)"
|
|
|
|
|
2024-04-07 15:32:30 +02:00
|
|
|
if [[ "${#ARGS[@]}" -lt 1 ]]; then
|
|
|
|
echo "Command not specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
COMMAND="${ARGS[0]}"
|
|
|
|
|
|
|
|
if [[ "${COMMAND}" != "apply" && "${COMMAND}" != "build" && "${COMMAND}" != "exec" ]]; then
|
|
|
|
echo "Unsupported command: ${COMMAND}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${#ARGS[1]}" -lt 2 ]]; then
|
|
|
|
echo "Host not specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
HOST="${ARGS[1]}"
|
|
|
|
|
|
|
|
if [[ -z ${TARGET} ]]; then
|
|
|
|
# Use fqdn as target
|
2024-04-07 19:48:27 +02:00
|
|
|
TARGET="$(nix --extra-experimental-features "nix-command flakes" eval --raw "${FLAKE_STORE_PATH}#nixosConfigurations.${HOST}" --apply "host: host.config.networking.fqdn")"
|
2024-04-07 15:32:30 +02:00
|
|
|
fi
|
|
|
|
|
2024-08-08 20:46:16 +02:00
|
|
|
APPLY_OPTS=()
|
|
|
|
|
|
|
|
if [[ -n "${USE_SUBSTITUTES}" ]]; then
|
|
|
|
APPLY_OPTS+=("--use-substitutes")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${USE_REMOTE_SUDO}" ]]; then
|
|
|
|
APPLY_OPTS+=("--use-remote-sudo")
|
|
|
|
fi
|
|
|
|
|
2024-08-24 17:23:59 +02:00
|
|
|
APPLY_OPERATION="switch"
|
|
|
|
|
|
|
|
if [[ -n "${ON_NEXT_BOOT}" ]]; then
|
|
|
|
APPLY_OPERATION="boot"
|
|
|
|
fi
|
|
|
|
|
2024-04-07 15:32:30 +02:00
|
|
|
case "${COMMAND}" in
|
|
|
|
apply)
|
2024-08-24 17:23:59 +02:00
|
|
|
nixos-rebuild "${APPLY_OPERATION}" --flake "${FLAKE_STORE_PATH}#${HOST}" --target-host "${TARGET}" "${APPLY_OPTS[@]}"
|
2024-04-07 15:32:30 +02:00
|
|
|
;;
|
|
|
|
build)
|
2024-04-07 19:48:27 +02:00
|
|
|
nixos-rebuild build --flake "${FLAKE_STORE_PATH}#${HOST}"
|
2024-04-07 15:32:30 +02:00
|
|
|
;;
|
|
|
|
exec)
|
|
|
|
# shellcheck disable=SC2029
|
|
|
|
ssh -t "${TARGET}" "${OPTS[@]}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unsupported command: ${COMMAND}"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|