{%- set srv = servers | first -%} #!/bin/bash # {{ srv.hostname }} — nixos-anywhere install # provisioning {{ provisioning_version }} — {{ now }} # Requires: nix (for nix run), hcloud CLI, SSH access to server set -euo pipefail {%- if debug is defined and debug == "yes" %} {%- endif %} HCLOUD_TOKEN="${HCLOUD_TOKEN:?HCLOUD_TOKEN required}" FLAKE_DIR="{{ flake_dir }}" SERVER_IP=$(hcloud server describe "{{ srv.hostname }}" -o format='{{ "{{" }}.PublicNet.IPv4.IP{{ "}}" }}') if [ -z "$SERVER_IP" ]; then echo "{{ srv.hostname }}: cannot resolve IP — is the server running?" exit 1 fi echo "{{ srv.hostname }}: IP=$SERVER_IP" # Wait for SSH to be available (Ubuntu bootstrap, not NixOS yet) echo "{{ srv.hostname }}: waiting for SSH..." for i in $(seq 1 30); do if ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$SERVER_IP" true 2>/dev/null; then break fi sleep 10 done ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$SERVER_IP" true || { echo "{{ srv.hostname }}: SSH unreachable after 300s" exit 1 } echo "{{ srv.hostname }}: SSH ready" # nixos-anywhere: kexec into NixOS, partition via disko, install # --build-on-remote: builds on the target (avoids cross-compilation from macOS aarch64→linux) # --phases kexec,disko,install,reboot — explicit phase control for the orchestrator if command -v nixos-anywhere &>/dev/null; then nixos-anywhere \ --flake "${FLAKE_DIR}#{{ srv.hostname }}" \ --build-on-remote \ root@"$SERVER_IP" elif command -v nix &>/dev/null; then nix run github:nix-community/nixos-anywhere -- \ --flake "${FLAKE_DIR}#{{ srv.hostname }}" \ --build-on-remote \ root@"$SERVER_IP" else echo "nixos-anywhere not found: install via 'nix profile install github:nix-community/nixos-anywhere' or ensure nix is available" exit 1 fi echo "{{ srv.hostname }}: NixOS install complete — server rebooting"