Skip to content

Commit

Permalink
Merge pull request #452 from a-kenji/feat/phases/identity_file
Browse files Browse the repository at this point in the history
feat: Use `identity_file` as a deployment key
  • Loading branch information
Lassulus authored Jan 31, 2025
2 parents 97b45ac + 5eee16d commit 3a8ec88
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ example uses a local directory on the source machine.
If your SSH key is not found, you will be asked for your password. If you are
using a non-root user, you must have access to sudo without a password. To avoid
SSH password prompts, set the `SSHPASS` environment variable to your password
and add `--env-password` to the `nixos-anywhere` command.
and add `--env-password` to the `nixos-anywhere` command. If providing a
specific SSH key through `-i` (identity_file), this key will then be used for
the installation and no temporary SSH key will be created.

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

Expand Down
9 changes: 7 additions & 2 deletions src/nixos-anywhere.sh
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,15 @@ runVmTest() {
}

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

declare -a sshCopyIdArgs
if [[ -n ${sshPrivateKeyFile} ]]; then
Expand Down
1 change: 1 addition & 0 deletions tests/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
from-nixos-with-sudo-stable = import ./from-nixos-with-sudo.nix testInputsStable;
from-nixos-with-generated-config = import ./from-nixos-generate-config.nix testInputsUnstable;
from-nixos-build-on-remote = import ./from-nixos-build-on-remote.nix testInputsUnstable;
from-nixos-separated-phases = import ./from-nixos-separated-phases.nix testInputsUnstable;
});
}
52 changes: 52 additions & 0 deletions tests/from-nixos-separated-phases.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(import ./lib/test-base.nix) {
name = "from-nixos-separated-phases";
nodes = {
installer = ./modules/installer.nix;
installed = {
services.openssh.enable = true;
virtualisation.memorySize = 1024;

users.users.nixos = {
isNormalUser = true;
openssh.authorizedKeys.keyFiles = [ ./modules/ssh-keys/ssh.pub ];
extraGroups = [ "wheel" ];
};
security.sudo.enable = true;
security.sudo.wheelNeedsPassword = false;
};
};
testScript = ''
start_all()
with subtest("Kexec Phase"):
installer.succeed("""
nixos-anywhere \
-i /root/.ssh/install_key \
--debug \
--kexec /etc/nixos-anywhere/kexec-installer \
--phases kexec \
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
nixos@installed >&2
""")
with subtest("Disko Phase"):
output = installer.succeed("""
nixos-anywhere \
-i /root/.ssh/install_key \
--debug \
--phases disko \
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
installed >&2
""")
with subtest("Install Phase"):
installer.succeed("""
nixos-anywhere \
-i /root/.ssh/install_key \
--debug \
--phases install \
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
root@installed >&2
""")
'';
}

0 comments on commit 3a8ec88

Please sign in to comment.