update script

This commit is contained in:
mjallen18
2025-04-15 15:11:45 -05:00
parent 59d1f4dada
commit da782ab76d

View File

@@ -2,23 +2,44 @@
let let
timezone = "America/Chicago"; timezone = "America/Chicago";
update-script = pkgs.writeScriptBin "update-system" '' update-script = pkgs.writeScriptBin "update" ''
#!/usr/bin/env bash #!/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 # ===== Parse arguments =====
COMMIT_MSG=$(git log -1 --pretty=%B | head -n1 | tr -dc '[:alnum:][:space:]-' | tr '[:space:]' '-') MODE=''${1:-"switch"}
TIMESTAMP=$(date +%Y%m%d-%H%M) VALID_MODES=("boot" "switch" "test" "build")
SHORT_HASH=$(git rev-parse --short HEAD) if [[ ! " ''${VALID_MODES[*]} " =~ " $MODE " ]]; then
echo " Invalid mode: '$MODE'. Use one of: $VALID_MODES[*]"
exit 1
fi
# Create a profile name using the timestamp, short hash, and commit message # ===== Git status check =====
PROFILE_NAME="$TIMESTAMP-$SHORT_HASH-$COMMIT_MSG" cd "$REPO_DIR"
DIRTY_TAG=
if [[ -n "$(git status --porcelain)" ]]; then
echo " Git repo is dirty rebuilding with dirty tag."
DIRTY_TAG="dirty-"
fi
# Rebuild the system SHORT_HASH=$(git rev-parse --short HEAD)
sudo nixos-rebuild boot --profile-name "$PROFILE_NAME" 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"
echo "System rebuilt with 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 in
{ {