Skip to content

systemd: make systemd-ssh-generator work#372979

Merged
philiptaron merged 8 commits intoNixOS:stagingfrom
NyCodeGHG:systemd-ssh
Mar 16, 2025
Merged

systemd: make systemd-ssh-generator work#372979
philiptaron merged 8 commits intoNixOS:stagingfrom
NyCodeGHG:systemd-ssh

Conversation

@NyCodeGHG
Copy link
Member

@NyCodeGHG NyCodeGHG commented Jan 11, 2025

Makes systemd-ssh-generator and systemd-ssh-proxy work on NixOS.

I successfully built this and ran nixosTests.systemd-ssh-proxy and nixosTests.openssh.
I also built the minimal iso and manually tested the vsock feature with a qemu vm.

closes #371191

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes 8.has: module (update) This PR changes an existing module in `nixos/` 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Jan 11, 2025
@nix-owners nix-owners bot requested review from flokli and kloenk January 11, 2025 16:48
@NyCodeGHG NyCodeGHG marked this pull request as ready for review January 11, 2025 21:30
@github-actions github-actions bot added the 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. label Jan 27, 2025
Copy link
Member

@phaer phaer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice to see this coming to upstream NixOS, thank you very much!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a proper issue about this yet, but systemd itself would also support passing credentials via kernel params which could be very useful for VMs. But that doesn't work with our current systemd-in-stage-1 implementation. @ElvishJerricco wrote a reproducer for that in https://gist.github.com/ElvishJerricco/dca95eb4ea9fc410bd525c3b15b68fdd

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Feb 1, 2025
@wegank wegank removed the 12.approvals: 1 This PR was reviewed and approved by one person. label Feb 4, 2025
Copy link
Contributor

@r-vdp r-vdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good, I will try to test it out over the next days.

@NyCodeGHG NyCodeGHG force-pushed the systemd-ssh branch 2 times, most recently from 63585df to 3ae5dfe Compare February 5, 2025 18:51
@NyCodeGHG
Copy link
Member Author

NyCodeGHG commented Feb 5, 2025

rebased onto latest staging to get around bluez and linux build failure when building nixosTests.systemd-ssh-proxy.driverInteractive

We need to patch systemd to make systemd-ssh-generator(8) work:

- systemd doesn't follow symlinks when checking for a packaged
  [email protected] unit
- systemd tries to link the ssh config dropins by default with tmpfiles
  to /usr, that is not possible, so we include the snippet manually.
@NyCodeGHG
Copy link
Member Author

We should probably rename programs.ssh.enableSystemdSshProxy to programs.ssh.systemd-ssh-proxy.enable, that looks a lot better

Copy link
Contributor

@philiptaron philiptaron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few questions about the systemd service files I'd like to understand before ✅, but I'm inclined to merge after that.

The one piece that looks most scary to me is the new tmpfiles.d configuration, which is doing more than just SSH stuff.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://linux.die.net/man/8/sshd:

-i Specifies that sshd is being run from inetd(8). sshd is normally not run from inetd because it needs to generate the server key before it can respond to the client, and this may take tens of seconds. Clients would have to wait too long if the key was regenerated every time. However, with small key sizes (e.g. 512) using sshd from inetd may be feasible.

This documentation makes me confused. I'm assuming socket activation is inetd-like, and this is the flag that's different. However the rest (about waiting too long, generating keys) is odd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also confused by this, not sure if host keys are meant?
Since those are generated a single time

Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐍🪔

Wonderful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this worked example of how to do this. One of the huge benefits of tests is just showing how to do things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been nice to avoid using an ISO for this. That's a very large build to be pushing to the cache that no one will ever use (yes, Hydra pushes these intermediate builds).

Also, this is technically nested virtualisation (double nested if the builder itself is a VM), and I'm not sure if we can take that as a given on the hardware we use for Hydra builders?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can test this functionality with nixos-containers instead. That would avoid both problems. I'll give it a look.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit introducing this patch says:

systemd tries to link the ssh config dropins by default with tmpfiles to /usr, that is not possible, so we include the snippet manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit introducing this patch says:

systemd doesn't follow symlinks when checking for a packaged [email protected] unit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future: sshdconfdir (note the d) is about https://github.com/systemd/systemd/blob/3a15daf4402ba2e35c27f21339e987d89f4f8a97/src/userdb/20-systemd-userdb.conf.in

Make sure SSH authorized keys recorded in user records can be consumed by SSH

Copy link
Contributor

@philiptaron philiptaron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm planning on approving after the patch to do only the SSH provisioning (not the hosts or MOTD stuff) lands.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'd like that.

This enables provisioning of root ssh keys with systemd credentials
(e.g. passed in via smbios strings or kernel params)
Copy link
Contributor

@philiptaron philiptaron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test passes, and tested again rebased onto latest staging.

I did notice a few odd things:

  1. There are i686 stdenv things in the closure of the test!
  2. There are musl things in the closure of the test!
  3. The test ends successfully, but in the last messages on screen is
vm-test-run-systemd-ssh-proxy> virthost # [   30.559645] systemd[1]: Failed to start SSH per-connection Daemon (PID 1077/UID 1000).
vm-test-run-systemd-ssh-proxy> virthost # [   30.560918] systemd[1]: session-1.scope: Deactivated successfully.
vm-test-run-systemd-ssh-proxy> virthost # [   30.563206] systemd-logind[728]: Removed session 1.

I didn't debug further.

@philiptaron philiptaron merged commit f4dd3ba into NixOS:staging Mar 16, 2025
26 of 27 checks passed
@NyCodeGHG NyCodeGHG deleted the systemd-ssh branch March 16, 2025 11:01
@ElvishJerricco ElvishJerricco mentioned this pull request Mar 17, 2025
13 tasks
@sternenseemann
Copy link
Member

I think this may have broken more NixOS tests. I've found an instance of this in the GitLab test which blocks the channel #398846. It's likely worth checking if this affects other tests as well.

This was referenced Apr 15, 2025
@NyCodeGHG
Copy link
Member Author

@sternenseemann thanks for noticing! I've had a quick look with a few ripgreps and the only test which looked like it could have been affected was nixosTests.xxh, but that one passes.

github-merge-queue bot pushed a commit that referenced this pull request Sep 20, 2025
SSH key generation was split out into its own systemd service in
#372979, but dependent service
definitions weren't updated.

The `apply-ec2-data` service needs to run before SSH key generation,
as it fetches host keys defined in ec2 user data and these keys should
take priority over generating new ones. Currently, the ordering
doesn't specify which should run first of `apply-ec2-data` and
`sshd-keygen`; in practice it seems that `sshd-keygen` often wins the
race, though.

Update the dependencies so that `apply-ec2-data` always runs first.
nixpkgs-ci bot pushed a commit that referenced this pull request Sep 20, 2025
SSH key generation was split out into its own systemd service in
#372979, but dependent service
definitions weren't updated.

The `apply-ec2-data` service needs to run before SSH key generation,
as it fetches host keys defined in ec2 user data and these keys should
take priority over generating new ones. Currently, the ordering
doesn't specify which should run first of `apply-ec2-data` and
`sshd-keygen`; in practice it seems that `sshd-keygen` often wins the
race, though.

Update the dependencies so that `apply-ec2-data` always runs first.

(cherry picked from commit d9ac3ba)
aaron-nall pushed a commit to aaron-nall/nixpkgs that referenced this pull request Sep 30, 2025
SSH key generation was split out into its own systemd service in
NixOS#372979, but dependent service
definitions weren't updated.

The `apply-ec2-data` service needs to run before SSH key generation,
as it fetches host keys defined in ec2 user data and these keys should
take priority over generating new ones. Currently, the ordering
doesn't specify which should run first of `apply-ec2-data` and
`sshd-keygen`; in practice it seems that `sshd-keygen` often wins the
race, though.

Update the dependencies so that `apply-ec2-data` always runs first.

(cherry picked from commit d9ac3ba)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants