Skip to content

Commit 3a8ec88

Browse files
authored
Merge pull request #452 from a-kenji/feat/phases/identity_file
feat: Use `identity_file` as a deployment key
2 parents 97b45ac + 5eee16d commit 3a8ec88

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
@@ -385,10 +385,15 @@ runVmTest() {
385385
}
386386

387387
uploadSshKey() {
388-
# we generate a temporary ssh keypair that we can use during nixos-anywhere
389388
# ssh-copy-id requires this directory
390389
mkdir -p "$HOME/.ssh/"
391-
ssh-keygen -t ed25519 -f "$sshKeyDir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
390+
if [[ -n ${sshPrivateKeyFile} ]]; then
391+
cp "$sshPrivateKeyFile" "$sshKeyDir/nixos-anywhere"
392+
ssh-keygen -y -f "$sshKeyDir/nixos-anywhere" >"$sshKeyDir/nixos-anywhere.pub"
393+
else
394+
# we generate a temporary ssh keypair that we can use during nixos-anywhere
395+
ssh-keygen -t ed25519 -f "$sshKeyDir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
396+
fi
392397

393398
declare -a sshCopyIdArgs
394399
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)