#!/usr/bin/env bash set -Euo pipefail VERBOSE=false SHOW_TRACE_OPT="" # Parse options if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then VERBOSE=true SHOW_TRACE_OPT="--show-trace" shift # Remove the verbose flag from arguments fi # Check if hostname argument is provided if [ "$#" -ne 1 ]; then echo "Usage: $0 [-v|--verbose] " >&2 exit 1 fi HOSTNAME="$1" # Check if 'nixos-rebuild' command is available if ! command -v nixos-rebuild > /dev/null; then echo "ERROR: 'nixos-rebuild' command not found. Please ensure it is installed and in your PATH." >&2 exit 1 fi # Determine the absolute directory where the script itself is located SCRIPT_DIR=$(dirname "$(readlink -f "$0")") # Construct the absolute path to the host's configuration file # and resolve it to a canonical path CONFIG_PATH=$(readlink -f "$SCRIPT_DIR/../hosts/$HOSTNAME/configuration.nix") # Verify that the CONFIG_PATH exists and is a regular file if [ ! -f "$CONFIG_PATH" ]; then echo "ERROR: Configuration file not found at '$CONFIG_PATH' for host '$HOSTNAME'." >&2 exit 1 fi echo "INFO: Attempting dry-build for host '$HOSTNAME' using configuration '$CONFIG_PATH'..." if [ "$VERBOSE" = true ]; then echo "INFO: Verbose mode enabled, --show-trace will be used." fi # Execute nixos-rebuild dry-build # Store the output and error streams, and the exit code NIX_OUTPUT_ERR=$(nixos-rebuild dry-build $SHOW_TRACE_OPT -I nixos-config="$CONFIG_PATH" 2>&1) NIX_EXIT_STATUS=$? # Check the exit status if [ "$NIX_EXIT_STATUS" -eq 0 ]; then echo "INFO: Dry-build for host '$HOSTNAME' completed successfully." if [ "$VERBOSE" = true ]; then echo "Output from nixos-rebuild:" echo "$NIX_OUTPUT_ERR" fi exit 0 else echo "ERROR: Dry-build for host '$HOSTNAME' failed. 'nixos-rebuild' exited with status $NIX_EXIT_STATUS." >&2 echo "Output from nixos-rebuild:" >&2 echo "$NIX_OUTPUT_ERR" >&2 exit "$NIX_EXIT_STATUS" fi