{ pkgs, nixos-anywhere, }: pkgs.writeShellApplication { name = "deploy-thin-client"; runtimeInputs = with pkgs; [age coreutils jq nix openssh util-linux nixos-anywhere]; text = '' usage() { cat <<'EOF' Usage: deploy-thin-client [--check] [--yes] HOST [USER@TARGET] --check Run local and remote preflight checks without installing --yes Skip the destructive installation confirmation EOF } mode=install assume_yes=false while [[ $# -gt 0 ]]; do case "$1" in --check) mode=check; shift ;; --yes) assume_yes=true; shift ;; -h|--help) usage; exit 0 ;; --*) echo "Unknown option: $1" >&2; usage >&2; exit 2 ;; *) break ;; esac done host="''${1:-}" target="''${2:-}" [[ -n "$host" ]] || { usage >&2; exit 2; } [[ "$host" =~ ^AZ-TC-[0-9]+$ ]] || { echo "Invalid Thin Client host: $host" >&2; exit 2; } flake="path:.#$host" echo "Evaluating $flake ..." nix eval --raw "path:.#nixosConfigurations.$host.config.system.build.toplevel.drvPath" >/dev/null disk="$(nix eval --raw "path:.#nixosConfigurations.$host.config.az.tc.deployment.diskDevice")" identity=".secrets/identities/$host.age" [[ -s "$identity" ]] || { echo "Missing host age identity: $identity" >&2 echo "Create it with: mkdir -p .secrets/identities && age-keygen -o $identity" >&2 exit 1 } chmod 600 "$identity" secrets_json="$(nix eval --json "path:.#nixosConfigurations.$host.config.age.secrets")" while IFS= read -r secret_file; do [[ -s "$secret_file" ]] || { echo "Required secret is missing or empty: $secret_file" >&2; exit 1; } if ! age --decrypt -i "$identity" "$secret_file" >/dev/null 2>&1; then echo "Host identity cannot decrypt required secret: $secret_file" >&2 exit 1 fi done < <(jq -r '.[].file' <<<"$secrets_json" | sort -u) echo "Preflight OK: host=$host disk=$disk identity=$identity" [[ -n "$target" ]] || { echo "USER@TARGET is required for $mode mode" >&2; exit 2; } runtime_dir="$(mktemp -d)" cleanup() { rm -rf "$runtime_dir"; } trap cleanup EXIT INT TERM ssh_options=( -o "UserKnownHostsFile=$runtime_dir/known_hosts" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o "PreferredAuthentications=keyboard-interactive,password" -o "PubkeyAuthentication=no" ) ssh "''${ssh_options[@]}" "$target" true echo echo "Target hardware:" ssh "''${ssh_options[@]}" "$target" 'printf "Model: "; cat /sys/class/dmi/id/product_name 2>/dev/null || true; printf "Serial: "; cat /sys/class/dmi/id/product_serial 2>/dev/null || true; lsblk -o NAME,PATH,SIZE,TYPE,MODEL,SERIAL,MOUNTPOINTS' ssh "''${ssh_options[@]}" "$target" test -b "$disk" || { echo "Configured target disk does not exist on target: $disk" >&2; exit 1; } if [[ "$mode" == check ]]; then echo "Remote preflight completed; no changes were made." exit 0 fi if [[ "$assume_yes" != true ]]; then echo echo "WARNING: disko will destroy all data on $disk at $target." read -r -p "Type $host to continue: " confirmation [[ "$confirmation" == "$host" ]] || { echo "Aborted." >&2; exit 1; } fi extra_files="$runtime_dir/extra-files" install -D -m 0600 "$identity" "$extra_files/var/lib/agenix/identity.age" printf -v NIX_SSHOPTS '%q ' "''${ssh_options[@]}" export NIX_SSHOPTS nixos-anywhere \ --flake "$flake" \ --extra-files "$extra_files" \ --print-build-logs \ "$target" ''; }