From 6af2a6650f81586ee98cb304f7e9e1d264d93f01 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 10 Nov 2022 14:42:06 +0100 Subject: [PATCH] init nixos-remote --- nixos-remote | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 nixos-remote diff --git a/nixos-remote b/nixos-remote new file mode 100755 index 0000000..58c6d9a --- /dev/null +++ b/nixos-remote @@ -0,0 +1,110 @@ +#!/usr/bin/env bash +set -eufo pipefail +set -x + +showUsage() { + cat <&2 + exit 1 +} + +nix_args=() + +while [[ $# -gt 0 ]]; do + case "$1" in + -f | --flake) + flake=$2 + shift + ;; + --argstr | --arg) + nix_args+=("$1" "$2" "$3") + shift + shift + ;; + --help) + showUsage + exit 0 + ;; + *) + if [ -z ${ssh_connection+x} ]; then + ssh_connection=$1 + else + showUsage + exit 1 + fi + ;; + esac + shift +done + +# ssh wrapper +ssh_() { + ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$@" +} + +# first check if the remote system is kexec booted +# if $(ssh_ "$ssh_connection" -- test -e /etc/is-kexec); then +if $(ssh_ "$ssh_connection" -- test -e /etc/NIXOS); then + is_kexec=y +fi + + +echo ${is_kexec-n} +if [ ${is_kexec-n} != "y" ]; then + # TODO we probably need an architecture detection here + ssh_ "$ssh_connection" << SSH +set -efux +fetch(){ + if command -v curl >/dev/null 2>&1; then + curl --fail -Ss -L "\$1" + elif command -v wget >/dev/null 2>&1; then + wget "\$1" -O- + else + echo "no downloader (curl or wget) found, bailing out" + exit 1 + fi +} +rm -rf /root/kexec +mkdir -p /root/kexec +fetch https://github.com/nix-community/nixos-images/releases/download/nixos-22.05/nixos-kexec-installer-x86_64-linux.tar.gz | tar -C /root/kexec -xvzf- +export TMPDIR=/root/kexec +setsid /root/kexec/kexec/run +SSH + # wait for machine to become unreachable + while ssh_ "$ssh_connection" -- exit 0; do sleep 1; done + + # watiting for machine to become available again + until ssh_ -o ConnectTimeout=10 "$ssh_connection" -- exit 0; do sleep 5; done +fi + + +ssh_ "$ssh_connection" << SSH +set -efux +$(declare -p nix_args) +nix --extra-experimental-features nix-command --extra-experimental-features flakes \ + run github:nix-community/disko \ + --no-write-lock-file -- \ + --debug -m create "\${nix_args[@]}" --flake "$flake" + +nix --extra-experimental-features nix-command --extra-experimental-features flakes \ + run github:nix-community/disko \ + --no-write-lock-file -- \ + --debug -m mount "\${nix_args[@]}" --flake "$flake" + +nixos-install --flake "$flake" +reboot +SSH