#!/usr/bin/env bash set -euo pipefail cd "$( dirname "${BASH_SOURCE[0]}" )" export NIX_PATH= host=$1 COPY_USER=yorick get_target_host() { TARGET_HOST=$(nix eval --raw -f vpn.nix ips.$host) TARGET_HOST=$(ssh $TARGET_HOST ip --json r get 1.1.1.1 | jq -r '.[0].prefsrc') } peek() { echo $ "$@" > /dev/stderr command "$@" } nix() { peek nix --extra-experimental-features "nix-command flakes" "$@" } nix-build() { peek nix-build "$@" } case $2 in ssh) get_target_host peek ssh root@"$TARGET_HOST" ;; build) nix build -f servers.nix "$host" --show-trace ;; copy) get_target_host nix copy -f servers.nix "$host" --show-trace --to "ssh://$COPY_USER@$TARGET_HOST" ;; test) get_target_host outPath=$(nix-build servers.nix -A "$host") nix copy -f servers.nix "$host" --show-trace --to "ssh://$COPY_USER@$TARGET_HOST" peek ssh root@"$TARGET_HOST" $outPath/bin/switch-to-configuration test ;; boot) get_target_host outPath=$(nix-build servers.nix -A "$host") nix copy "$outPath" --show-trace --to "ssh://$COPY_USER@$TARGET_HOST" peek ssh root@"$TARGET_HOST" nix-env -p "/nix/var/nix/profiles/system" --set "$outPath" peek ssh root@"$TARGET_HOST" $outPath/bin/switch-to-configuration boot ;; switch) get_target_host outPath=$(nix-build servers.nix -A "$host") nix copy "$outPath" --show-trace --to "ssh://$COPY_USER@$TARGET_HOST" peek ssh root@"$TARGET_HOST" nix-env -p "/nix/var/nix/profiles/system" --set "$outPath" peek ssh root@"$TARGET_HOST" $outPath/bin/switch-to-configuration switch ;; esac