Created
February 1, 2023 22:05
-
-
Save ppenguin/78e017b2410a9fee955e60f9eb9f3394 to your computer and use it in GitHub Desktop.
quick&dirty adaptation of nixos-remote install script with kexec removed (for RAM constrained remote systems)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eufo pipefail | |
# set -x | |
SSHPORT=${SSHPORT:-22} | |
showUsage() { | |
cat <<USAGE | |
Usage: $0 [options] ssh-host | |
Options: | |
* -f, --flake flake | |
set the flake to install the system from | |
* --arg name value | |
pass value to nix-build. can be used to set disk-names for example | |
* --argstr name value | |
pass value to nix-build as string | |
USAGE | |
} | |
abort() { | |
echo "aborted: $*" >&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 | |
;; | |
--no-ssh-copy-id) | |
no_ssh_copy=y | |
;; | |
*) | |
if [ -z ${ssh_connection+x} ]; then | |
ssh_connection=$1 | |
else | |
showUsage | |
exit 1 | |
fi | |
;; | |
esac | |
shift | |
done | |
# ssh wrapper | |
timeout_ssh_() { | |
timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p$SSHPORT "$ssh_connection" "$@" | |
} | |
ssh_() { | |
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p$SSHPORT "$ssh_connection" "$@" | |
} | |
nixCopy() { | |
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p$SSHPORT" nix copy --extra-experimental-features nix-command "$@" | |
} | |
nix_build() { | |
nix \ | |
--experimental-features flakes build \ | |
--extra-experimental-features nix-command \ | |
--no-write-lock-file \ | |
--print-out-paths \ | |
"$@" | |
} | |
nixos_system="" | |
# parse flake nixos-install style syntax, get the system attr | |
if [[ -n "${flake+x}" ]]; then | |
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then | |
flake="${BASH_REMATCH[1]}" | |
flakeAttr="${BASH_REMATCH[2]}" | |
fi | |
if [[ -z "$flakeAttr" ]]; then | |
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." | |
echo "For example, to use the output nixosConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri." | |
exit 1 | |
fi | |
disko_script=$(nix_build "${flake}#nixosConfigurations.${flakeAttr}.config.system.build.disko") | |
nixos_system=$(nix_build "${flake}#nixosConfigurations.${flakeAttr}.config.system.build.toplevel") | |
else | |
abort "flake must be set" | |
fi | |
# wait for machine to become reachable (possibly forever) | |
if [ ${no_ssh_copy-n} != "y" ]; then | |
until ssh-copy-id "$ssh_connection"; do sleep 5; done | |
else | |
until ssh_ -o ConnectTimeout=10 -- exit 0; do sleep 5; done | |
fi | |
nixCopy --to "ssh://$ssh_connection" "$disko_script" | |
ssh_ $disko_script | |
nixCopy --to "ssh://$ssh_connection:${SSHPORT}?remote-store=local?root=/mnt" "$nixos_system" | |
ssh_ << SSH | |
set -efu | |
nixos-install --no-root-passwd --no-channel-copy --system "$nixos_system" | |
reboot | |
SSH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment