From f0c2358da028f89eb151793f57303dfaf5d88f23 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Thu, 5 Dec 2024 13:55:14 +0100 Subject: [PATCH] nixos-anywhere: add --target-host option Mirror the option from nixos-rebuild, so both have the same signature: * Bootstrap: `nixos-anywhere --flake .#targethost --target-host root@1.2.3.4` * Deploy: `nixos-rebuild --flake .#targethost --target-host root@1.2.3.4 switch` --- docs/cli.md | 4 +++- docs/howtos/disko-modes.md | 2 +- docs/howtos/no-os.md | 2 +- docs/howtos/secrets.md | 2 +- docs/quickstart.md | 8 ++++---- src/nixos-anywhere.sh | 8 +++++++- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index c6992237..c745883f 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -1,12 +1,14 @@ # CLI ``` -Usage: nixos-anywhere [options] +Usage: nixos-anywhere [options] [] Options: * -f, --flake set the flake to install the system from. +* --target-host + specified the SSH target host to deploy onto. * -i selects which SSH private key file to use. * -p, --ssh-port diff --git a/docs/howtos/disko-modes.md b/docs/howtos/disko-modes.md index 135c2f8b..501f93f1 100644 --- a/docs/howtos/disko-modes.md +++ b/docs/howtos/disko-modes.md @@ -10,7 +10,7 @@ To only mount existing filesystems, add `--disko-mode mount` to `nixos-anywhere`: ``` -nix run github:nix-community/nixos-anywhere -- --disko-mode mount --flake # root@ +nix run github:nix-community/nixos-anywhere -- --disko-mode mount --flake # --target-host root@ ``` 1. This will first boot into a nixos-installer diff --git a/docs/howtos/no-os.md b/docs/howtos/no-os.md index 28095901..1c07cb0a 100644 --- a/docs/howtos/no-os.md +++ b/docs/howtos/no-os.md @@ -64,7 +64,7 @@ ssh -v nixos@fec0::5054:ff:fe12:3456 You can then use the IP address to run `nixos-anywhere` like this: ``` -nix run github:nix-community/nixos-anywhere -- --flake '.#myconfig' nixos@fec0::5054:ff:fe12:3456 +nix run github:nix-community/nixos-anywhere -- --flake '.#myconfig' --target-host nixos@fec0::5054:ff:fe12:3456 ``` This example assumes a flake in the current directory containing a configuration diff --git a/docs/howtos/secrets.md b/docs/howtos/secrets.md index e8933715..8d95ee78 100644 --- a/docs/howtos/secrets.md +++ b/docs/howtos/secrets.md @@ -35,7 +35,7 @@ pass ssh_host_ed25519_key > "$temp/etc/ssh/ssh_host_ed25519_key" chmod 600 "$temp/etc/ssh/ssh_host_ed25519_key" # Install NixOS to the host system with our secrets -nixos-anywhere --extra-files "$temp" --flake '.#your-host' root@yourip +nixos-anywhere --extra-files "$temp" --flake '.#your-host' --target-host root@yourip ``` ## Example: Uploading Disk Encryption Secrets diff --git a/docs/quickstart.md b/docs/quickstart.md index 80df3f5f..29d26c64 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -217,7 +217,7 @@ You can now run **nixos-anywhere** from the command line as shown below, where: - `` is the IP address of the target machine. ``` -nix run github:nix-community/nixos-anywhere -- --flake # root@ +nix run github:nix-community/nixos-anywhere -- --flake # --target-host root@ ``` The command would look  like this if you had created your files in a directory @@ -225,20 +225,20 @@ named `/home/mydir/test` and the IP address of your target machine is `37.27.18.135`: ``` -nix run github:nix-community/nixos-anywhere -- --flake /home/mydir/test#hetzner-cloud root@37.27.18.135 +nix run github:nix-community/nixos-anywhere -- --flake /home/mydir/test#hetzner-cloud --target-host root@37.27.18.135 ``` If you also need to generate hardware configuration amend flags for nixos-generate-config: ``` -nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-generate-config ./hardware-configuration.nix --flake # root@ +nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-generate-config ./hardware-configuration.nix --flake # --target-host root@ ``` Or these flags if you are using nixos-facter instead: ``` -nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-facter ./facter.json --flake # root@ +nix run github:nix-community/nixos-anywhere -- --generate-hardware-config nixos-facter ./facter.json --flake # --target-host root@ ``` Adjust the location of `./hardware-configuration.nix` and `./facter.json` diff --git a/src/nixos-anywhere.sh b/src/nixos-anywhere.sh index 15049748..e6f8305d 100755 --- a/src/nixos-anywhere.sh +++ b/src/nixos-anywhere.sh @@ -62,12 +62,14 @@ declare -a sshArgs=() showUsage() { cat < +Usage: nixos-anywhere [options] [] Options: * -f, --flake set the flake to install the system from. +* --target-host + specified the SSH target host to deploy onto. * -i selects which SSH private key file to use. * -p, --ssh-port @@ -143,6 +145,10 @@ parseArgs() { flake=$2 shift ;; + --target-host) + sshConnection=$2 + shift + ;; -i) sshPrivateKeyFile=$2 shift