Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use identity_file as a deployment key #452

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -367,10 +367,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
""")
'';
}
Loading