#!/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 'nix-instantiate' command is available if ! command -v nix-instantiate > /dev/null; then echo "ERROR: 'nix-instantiate' command not found. Please ensure Nix 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 # Check for host-specific channel file CHANNEL_PATH="$SCRIPT_DIR/../hosts/$HOSTNAME/channel" CHANNEL_OPT="" if [ -f "$CHANNEL_PATH" ]; then CHANNEL_URL=$(cat "$CHANNEL_PATH") # Append /nixexprs.tar.xz to get the actual tarball URL TARBALL_URL="${CHANNEL_URL}/nixexprs.tar.xz" echo "INFO: Using channel '$TARBALL_URL' from '$CHANNEL_PATH'." CHANNEL_OPT="-I nixpkgs=$TARBALL_URL" else echo "WARNING: No channel file found at '$CHANNEL_PATH'. Using system default." >&2 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 nix-instantiate to evaluate the configuration # nix-instantiate fetches fresh tarballs and catches all evaluation errors # unlike nixos-rebuild which may use cached results NIX_OUTPUT_ERR=$(nix-instantiate $SHOW_TRACE_OPT $CHANNEL_OPT -I nixos-config="$CONFIG_PATH" '' -A system 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 nix-instantiate:" echo "$NIX_OUTPUT_ERR" fi exit 0 else echo "ERROR: Dry-build for host '$HOSTNAME' failed. 'nix-instantiate' exited with status $NIX_EXIT_STATUS." >&2 echo "Output from nix-instantiate:" >&2 echo "$NIX_OUTPUT_ERR" >&2 exit "$NIX_EXIT_STATUS" fi