fix(installer): stamp host SSH keys with the target hostname - #235
Open
RISK-alt wants to merge 1 commit into
Open
fix(installer): stamp host SSH keys with the target hostname#235RISK-alt wants to merge 1 commit into
RISK-alt wants to merge 1 commit into
Conversation
Host SSH keys are generated during installation by `ssh-keygen -A`, run
through `nixos-enter`. `nixos-enter` chroots into /mnt but keeps the
installer's UTS namespace, and `ssh-keygen` stamps the key comment with
the hostname of the running system -- the installer, not the machine
being installed.
`buildUSBInstaller` overrides the installer hostname to
`m${inventoryId}`. `toString null` is the empty string in Nix, so on a
machine whose `inventoryId` is unset that hostname is literally `m`,
and the installed keys end up commented `root@m`.
Rewrite the comments after generation, with the hostname the target
system actually boots with.
The TPM2-backed keys produced by `ssh-tpm-keygen -A` are deliberately
left alone: `ssh-keygen -c` cannot rewrite a key sealed in the TPM.
Worth a follow-up if you want those consistent too.
RISK-alt
requested review from
jdauphant-dinum,
olebrouster-dinum,
plegrand-dinum and
rlahfa-dinum
as code owners
August 8, 2026 15:16
rlahfa-dinum
approved these changes
Aug 10, 2026
rlahfa-dinum
left a comment
Contributor
There was a problem hiding this comment.
Looks reasonable to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Installed machines end up with their host SSH keys commented
root@minstead of carrying their own hostname.Root cause
Host keys are generated at install time by
ssh-keygen -A, run throughnixos-enter(ageKeysProvisionScriptinlib/default.nix).nixos-enterchroots into/mntbut keeps the installer's UTS namespace, andssh-keygentakes the key comment from the hostname of the running system — the installer, not the machine being installed.buildUSBInstalleroverrides that hostname tom${inventoryId}(lib/default.nix:391). In NixtoString nullis the empty string, so on a machine whoseinventoryIdis unset the installer's hostname is literallym— henceroot@m.Confirmed experimentally:
ssh-keygen -Areads the hostname from the UTS namespace, and forcing it withunshare -uchanges the comment accordingly.The fix
Rewrite the comments after generation with
ssh-keygen -c, using the hostname the target system actually boots with.ssh-keygen -cupdates the comment in both the private key file and its.pub.Testing
/mntand the hostname forced tom,ssh-keygen -Aproducesroot@mon every key type — the exact symptom reported. Running the lines this change emits, extracted verbatim from the generatedautoinstall-terminalscript, then rewrites every key toroot@<target hostname>, in the private keys and the.pubfiles alike.buildInstallerSysteminstantiated withageHostKeys = trueemits the loop with the target hostname correctly interpolated.nix-build -A testspasses, all four tests, includingidempotent-autoinstall— which exercisesbuildInstallerSystem, the function this PR changes.nixfmt -sc,statix check --config statix.tomlandreuse --root . lintall pass.Not covered: a real install on physical hardware. Key generation itself is untouched; the change only rewrites comments afterwards.
Out of scope
The TPM2-backed keys produced by
ssh-tpm-keygen -Amost likely carry the same wrong comment, butssh-keygen -ccannot rewrite a key sealed in the TPM. Happy to follow up if you want those consistent too — it would need a different mechanism, so I left it out rather than guess.Closes #115