#! /usr/bin/env bash set -xeuo pipefail declare username host key_file ssh_opts nixos_conf IFS=" " read -r -a ssh_opts <<< "$( (echo "$ssh_opts" | jq -r '@sh') | tr -d \'\")" # DEPLOY sshOpts=( -o BatchMode=yes -o StrictHostKeyChecking=no ) for ssh_opt in "${ssh_opts[@]}"; do sshOpts+=( -o "$ssh_opt" ) done if [[ -n "$key_file" ]]; then sshOpts+=( -i "$key_file" ) fi destination="$username@$host" command=(nix-instantiate --show-trace "${nixos_conf}") # INSTANTIATE # instantiate the config in /nix/store "${command[@]}" -A out_path # get the realized derivation to deploy "${command[@]}" --show-trace --eval --strict --json # FIXME explore import/readFile as ways to instantiate the derivation, potentially allowing to realize the store path up-front from Nix? 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 # shellcheck disable=SC2029 ssh "${sshOpts[@]}" "$destination" "nix-env --profile /nix/var/nix/profiles/system --set $outPath" # shellcheck disable=SC2029 output=$(ssh -o "ConnectTimeout=1" -o "ServerAliveInterval=1" "${sshOpts[@]}" "$destination" "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