From 44dc0edf9616bc70315bf789b86b1f4083075cda Mon Sep 17 00:00:00 2001 From: Felix Schulze Date: Tue, 3 Jun 2025 18:06:38 +0200 Subject: [PATCH] rebuild script use specified host from env with warnings --- .env.example | 3 +++ .gitignore | 2 ++ rebuild-nix-system.sh | 29 ++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .env.example create mode 100644 .gitignore diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..2ffde46 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +# Copy this file to .env and set your host +# Available hosts: wildfire, drought, +NIXOS_HOST=wildfire \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..795c040 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Environment file +.env \ No newline at end of file diff --git a/rebuild-nix-system.sh b/rebuild-nix-system.sh index 3b99b19..b80a933 100755 --- a/rebuild-nix-system.sh +++ b/rebuild-nix-system.sh @@ -1,10 +1,37 @@ #!/bin/bash +# Source .env file +if [ -f ".env" ]; then + source .env +else + echo "Error: No .env file found. Copy example.env to .env and set your NIXOS_HOST" + exit 1 +fi + +if [ -z "$NIXOS_HOST" ]; then + echo "Error: NIXOS_HOST not set in .env file" + exit 1 +fi + +# Validate that the host configuration exists +AVAILABLE_HOSTS=$(nix flake show --json 2>/dev/null | jq -r '.nixosConfigurations | keys[]' 2>/dev/null) +if [ $? -ne 0 ] || [ -z "$AVAILABLE_HOSTS" ]; then + echo "Warning: Could not validate host configuration. Proceeding anyway..." +else + if ! echo "$AVAILABLE_HOSTS" | grep -q "^$NIXOS_HOST$"; then + echo "Error: Host '$NIXOS_HOST' not found in flake.nix" + echo "Available hosts: $(echo $AVAILABLE_HOSTS | tr '\n' ' ')" + exit 1 + fi +fi + +echo "Building configuration for host: $NIXOS_HOST" + # First, run a check to see if the flake is valid nix flake check # Rebuild the system -sudo nixos-rebuild switch --flake ./#default +sudo nixos-rebuild switch --flake ./#$NIXOS_HOST # Clean up old generations older than 180 days sudo nix-collect-garbage --delete-older-than 180d