diff --git a/hosts/default.nix b/hosts/default.nix index 8c8704d..c7e61cc 100755 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -2,23 +2,44 @@ let timezone = "America/Chicago"; - update-script = pkgs.writeScriptBin "update-system" '' - #!/usr/bin/env bash + update-script = pkgs.writeScriptBin "update" '' + #!/usr/bin/env bash + set -euo pipefail - cd /etc/nixos + # ===== Config ===== + REPO_DIR="/etc/nixos" + HOST=$(hostname) + FLAKE="$REPO_DIR#$HOST" + PROFILE_DIR="/nix/var/nix/profiles/system-profiles" - # Get commit message and timestamp for the tag - COMMIT_MSG=$(git log -1 --pretty=%B | head -n1 | tr -dc '[:alnum:][:space:]-' | tr '[:space:]' '-') - TIMESTAMP=$(date +%Y%m%d-%H%M) - SHORT_HASH=$(git rev-parse --short HEAD) - - # Create a profile name using the timestamp, short hash, and commit message - PROFILE_NAME="$TIMESTAMP-$SHORT_HASH-$COMMIT_MSG" - - # Rebuild the system - sudo nixos-rebuild boot --profile-name "$PROFILE_NAME" + # ===== Parse arguments ===== + MODE=''${1:-"switch"} + VALID_MODES=("boot" "switch" "test" "build") + if [[ ! " ''${VALID_MODES[*]} " =~ " $MODE " ]]; then + echo "❌ Invalid mode: '$MODE'. Use one of: $VALID_MODES[*]" + exit 1 + fi - echo "System rebuilt with profile: $PROFILE_NAME" + # ===== Git status check ===== + cd "$REPO_DIR" + DIRTY_TAG= + if [[ -n "$(git status --porcelain)" ]]; then + echo "⚠️ Git repo is dirty — rebuilding with dirty tag." + DIRTY_TAG="dirty-" + fi + + SHORT_HASH=$(git rev-parse --short HEAD) + COMMIT_MSG=$(git log -1 --pretty=%s | tr ' ' '_' | tr -dc '[:alnum:]_-') + TIMESTAMP=$(date -u +%Y%m%dT%H%M%SZ) + PROFILE_NAME=$TIMESTAMP-$DIRTY_TAG$SHORT_HASH-$COMMIT_MSG + echo "✅ Building profile: $PROFILE_NAME" + + # Use --profile-name only when MODE is 'switch' or 'boot' + if [[ "$MODE" == "switch" || "$MODE" == "boot" ]]; then + sudo nixos-rebuild "$MODE" --flake "$FLAKE" --profile-name "$PROFILE_NAME" + else + sudo nixos-rebuild "$MODE" --flake "$FLAKE" + fi ''; in {