Skip to content

Commit 5eee16d

Browse files
committed
feat: Use identity_file as deployment key
Will now use the provided `-i` (identity_file) as a stable deployment key - if specified - rather than always generating a random ephemeral SSH key. This is primarily useful for the following reasons: - error recovery - controlled phases *Error Recovery* If `nixos-anywhere` stops with an error, we might want to connect remotely to the deployed machine. With the ephemeral key this might not be possible, because we don't necessarily have access to it anymore. *Controlled Phases* It is already possible to control which phases should be run. With an ephemeral key outside our control we are not able to resume the phases with a second `nixos-anywhere` invocation, because a different deployment key will now be generated.
1 parent 80a2e7d commit 5eee16d

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

docs/quickstart.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ example uses a local directory on the source machine.
113113
If your SSH key is not found, you will be asked for your password. If you are
114114
using a non-root user, you must have access to sudo without a password. To avoid
115115
SSH password prompts, set the `SSHPASS` environment variable to your password
116-
and add `--env-password` to the `nixos-anywhere` command.
116+
and add `--env-password` to the `nixos-anywhere` command. If providing a
117+
specific SSH key through `-i` (identity_file), this key will then be used for
118+
the installation and no temporary SSH key will be created.
117119

118120
### 7. (Optional) Test your NixOS and Disko configuration
119121

src/nixos-anywhere.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,15 @@ runVmTest() {
367367
}
368368

369369
uploadSshKey() {
370-
# we generate a temporary ssh keypair that we can use during nixos-anywhere
371370
# ssh-copy-id requires this directory
372371
mkdir -p "$HOME/.ssh/"
373-
ssh-keygen -t ed25519 -f "$sshKeyDir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
372+
if [[ -n ${sshPrivateKeyFile} ]]; then
373+
cp "$sshPrivateKeyFile" "$sshKeyDir/nixos-anywhere"
374+
ssh-keygen -y -f "$sshKeyDir/nixos-anywhere" >"$sshKeyDir/nixos-anywhere.pub"
375+
else
376+
# we generate a temporary ssh keypair that we can use during nixos-anywhere
377+
ssh-keygen -t ed25519 -f "$sshKeyDir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
378+
fi
374379

375380
declare -a sshCopyIdArgs
376381
if [[ -n ${sshPrivateKeyFile} ]]; then

tests/flake-module.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
from-nixos-with-sudo-stable = import ./from-nixos-with-sudo.nix testInputsStable;
2121
from-nixos-with-generated-config = import ./from-nixos-generate-config.nix testInputsUnstable;
2222
from-nixos-build-on-remote = import ./from-nixos-build-on-remote.nix testInputsUnstable;
23+
from-nixos-separated-phases = import ./from-nixos-separated-phases.nix testInputsUnstable;
2324
});
2425
}

tests/from-nixos-separated-phases.nix

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(import ./lib/test-base.nix) {
2+
name = "from-nixos-separated-phases";
3+
nodes = {
4+
installer = ./modules/installer.nix;
5+
installed = {
6+
services.openssh.enable = true;
7+
virtualisation.memorySize = 1024;
8+
9+
users.users.nixos = {
10+
isNormalUser = true;
11+
openssh.authorizedKeys.keyFiles = [ ./modules/ssh-keys/ssh.pub ];
12+
extraGroups = [ "wheel" ];
13+
};
14+
security.sudo.enable = true;
15+
security.sudo.wheelNeedsPassword = false;
16+
};
17+
};
18+
testScript = ''
19+
start_all()
20+
21+
with subtest("Kexec Phase"):
22+
installer.succeed("""
23+
nixos-anywhere \
24+
-i /root/.ssh/install_key \
25+
--debug \
26+
--kexec /etc/nixos-anywhere/kexec-installer \
27+
--phases kexec \
28+
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
29+
nixos@installed >&2
30+
""")
31+
32+
with subtest("Disko Phase"):
33+
output = installer.succeed("""
34+
nixos-anywhere \
35+
-i /root/.ssh/install_key \
36+
--debug \
37+
--phases disko \
38+
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
39+
installed >&2
40+
""")
41+
42+
with subtest("Install Phase"):
43+
installer.succeed("""
44+
nixos-anywhere \
45+
-i /root/.ssh/install_key \
46+
--debug \
47+
--phases install \
48+
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
49+
root@installed >&2
50+
""")
51+
'';
52+
}

0 commit comments

Comments
 (0)