#! /usr/bin/env bash set -xeuo pipefail declare username host system config_nix config_tf # INSTANTIATE command=(nix-instantiate --argstr system "$system" --argstr config_nix "$config_nix" --argstr config_tf "$config_tf" ./nixos.nix) # instantiate the config in /nix/store "${command[@]}" -A out_path # DEPLOY sshOpts=( -o BatchMode=yes -o StrictHostKeyChecking=no # TODO set key for production # ${if key-file == null then "" else "-i ${key-file}"} # NOTE the below options are for tests -o ConnectTimeout=1 -o ServerAliveInterval=1 ) destination="$username@$host" # get the realized derivation to deploy outPath=$(nix-store --realize "$("${command[@]}" --show-trace --eval --strict --json | jq -r '.drv_path')") # deploy the config by nix-copy-closure NIX_SSHOPTS="${sshOpts[*]}" nix-copy-closure --to "$destination" "$outPath" --gzip --use-substitutes # switch the remote host to the config # NOTE checks here are for tests - in production time-outs could be a real thing, rather than indicator of success! # shellcheck disable=SC2029 output=$(ssh "${sshOpts[@]}" "$destination" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?" echo "output: $output" if [[ $output != *"Timeout, server $host not responding"* ]]; then echo "non-timeout error: $output" exit 1 else exit 0 fi