#! /usr/bin/env bash set -xeuo pipefail declare username host system module args deployment_name deployment_type key_file root_path # DEPLOY sshOpts=( -o BatchMode=yes -o StrictHostKeyChecking=no # NOTE the below options are for tests -o ConnectTimeout=1 -o ServerAliveInterval=1 ) if [[ -n "$key_file" ]]; then sshOpts+=( -i "$key_file" ) fi destination="$username@$host" command=(nix-instantiate --show-trace --expr " import $root_path/deployment/nixos.nix { system = \"$system\"; configuration = (import \"$root_path/$module\" (builtins.fromJSON ''$args'')).$deployment_name.$deployment_type.nixos-configuration; } ") # INSTANTIATE # instantiate the config in /nix/store "${command[@]}" -A out_path # 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 # shellcheck disable=SC2029 ssh "${sshOpts[@]}" "$destination" "nix-env --profile /nix/var/nix/profiles/system --set $outPath" # shellcheck disable=SC2029 output=$(ssh "${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