nix-config/scripts/nixos-sync.sh

40 lines
1.2 KiB
Bash

#!/usr/bin/env bash
# This script checks the remote Git repository for changes
# and triggers a nixos-rebuild if a new commit is found.
# Usage: ./nixos-sync.sh <REPO_URL> <BRANCH>
set -euo pipefail
REPO_URL=${1:-"https://git.depeuter.dev/Bos55/nix-config.git"}
BRANCH=${2:-"main"}
echo "Checking remote hash for $REPO_URL branch $BRANCH..."
# Fetch remote hash, fallback to unknown if it fails
REMOTE_HASH=$(git ls-remote "$REPO_URL" "refs/heads/$BRANCH" | awk '{print $1}' || true)
if [ -z "$REMOTE_HASH" ]; then
echo "WARNING: Could not fetch remote hash. Forcing rebuild to be safe."
REMOTE_HASH="unknown_remote"
fi
LOCAL_HASH="unknown_local"
if [ -f /run/current-system/configurationRevision ]; then
LOCAL_HASH=$(cat /run/current-system/configurationRevision)
fi
echo "Remote hash: $REMOTE_HASH"
echo "Local hash: $LOCAL_HASH"
if [ "$REMOTE_HASH" = "$LOCAL_HASH" ] && [ "$REMOTE_HASH" != "unknown_remote" ] && [ "$LOCAL_HASH" != "unknown" ]; then
echo "Hashes match. No update needed."
exit 0
fi
echo "Hashes differ or unknown. Triggering nixos-rebuild..."
# Trigger the build and switch
nixos-rebuild switch --flake "git+$REPO_URL?dir=nixos&ref=$BRANCH"
echo "Update successful."