diff options
| author | Kumar Damani <me@kumardamani.net> | 2026-07-01 04:09:21 +0000 |
|---|---|---|
| committer | Kumar Damani <me@kumardamani.net> | 2026-07-01 04:12:25 +0000 |
| commit | 02926f70770ceb90cf382da2b54f888ba969f423 (patch) | |
| tree | 0a7cc1fc1c68ae91ae49070bea5cd3708b875819 /nix/deploy.sh | |
| parent | bc1ad567d2eac46088285ed7d2d688fffc91a82f (diff) | |
nix based config
Diffstat (limited to 'nix/deploy.sh')
| -rwxr-xr-x | nix/deploy.sh | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/nix/deploy.sh b/nix/deploy.sh new file mode 100755 index 0000000..d3ad649 --- /dev/null +++ b/nix/deploy.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +set -euo pipefail + +HOST="$1" +FLAKE=".#${HOST}" +KEY_DIR="./host-keys/${HOST}" +USER="${2:-root}" + +# Validate +if [ ! -d "$KEY_DIR" ]; then + echo "❌ No host key directory found for '${HOST}'" + exit 1 +fi + +# Validate the per-host secrets file exists +SECRETS_FILE="secrets/${HOST}.yaml" +if [ ! -f "$SECRETS_FILE" ]; then + echo "❌ No secrets file found at ${SECRETS_FILE}" + echo " Run: sops ${SECRETS_FILE}" + exit 1 +fi + + +# ---- Compare host key fingerprints and replace if different ---- +LOCAL_FINGERPRINT=$(ssh-keygen -lf "${KEY_DIR}/ssh_host_ed25519_key.pub" 2>/dev/null | awk '{print $2}') +REMOTE_FINGERPRINT=$(ssh "${USER}@${HOST}" ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub 2>/dev/null | awk '{print $2}') + +if [ "$LOCAL_FINGERPRINT" != "$REMOTE_FINGERPRINT" ]; then + echo "→ [${HOST}] Host key mismatch! Replacing..." + echo " Local: ${LOCAL_FINGERPRINT}" + echo " Remote: ${REMOTE_FINGERPRINT}" + + # Copy the pre-generated key to replace the VM's auto-generated one + scp "${KEY_DIR}/ssh_host_ed25519_key" "${USER}@${HOST}:/etc/ssh/ssh_host_ed25519_key" + scp "${KEY_DIR}/ssh_host_ed25519_key.pub" "${USER}@${HOST}:/etc/ssh/ssh_host_ed25519_key.pub" + ssh "${USER}@${HOST}" chmod 600 /etc/ssh/ssh_host_ed25519_key + ssh "${USER}@${HOST}" chmod 644 /etc/ssh/ssh_host_ed25519_key.pub + + echo "→ [${HOST}] Key replaced. You may get a WARNING on next SSH (host key changed)." + echo " Remove old key with: ssh-keygen -R ${HOST}" +else + echo "→ [${HOST}] Host keys match, no action needed" +fi + +# Deploy +echo "→ [${HOST}] Deploying with nixos-rebuild..." +nixos-rebuild switch \ + --flake "$FLAKE" \ + --target-host "${USER}@${HOST}" \ + --sudo |
