!DBFhtjpqmJNENpLDOv:nixos.org

NixOS systemd

611 Members
NixOS ❤️ systemd172 Servers

Load older messages


SenderMessageTime
6 Mar 2025
@gdamjan:spodeli.orggdamjan for the client side (ssh client, but actually VM host), you need the systemd-ssh-proxy executable and some ssh config magic (shipped with systemd) 19:43:14
@elvishjerricco:matrix.orgElvishJerriccoi'm so confused19:43:53
@gdamjan:spodeli.orggdamjanah, and also the generator needs to find that sshd is "installed"19:44:29
@gdamjan:spodeli.orggdamjannot sure, but didn't the kernel automatically load socket modules ?19:44:44
@elvishjerricco:matrix.orgElvishJerricco ok so we have a .socket unit, which depends on a vsock, and we're hoping the kernel auto-loads the module? 19:45:48
@elvishjerricco:matrix.orgElvishJerricco (really, the .socket unit should just depend on modprobe@whatever-the-vsock-module-is-called.service) 19:46:32
@gdamjan:spodeli.orggdamjanconsidering I haven't configured any vsock module to be loaded explicitly, and yet they are :)19:46:43
@elvishjerricco:matrix.orgElvishJerricco
$ git grep vsock | wc -l
220

jfc

19:47:42
@elvishjerricco:matrix.orgElvishJerricco *
$ cd systemd
$ git grep vsock | wc -l
220

jfc

19:47:56
@gdamjan:spodeli.orggdamjanthe generator does seem to check that the system is a guest19:48:46
@elvishjerricco:matrix.orgElvishJerriccook so we've got some kind of option here19:49:10
@elvishjerricco:matrix.orgElvishJerriccodo we need the ssh generator?19:49:32
@elvishjerricco:matrix.orgElvishJerricco * gdamjan: do we need the ssh generator? 19:49:39
@gdamjan:spodeli.orggdamjanin theory you can just add a static unit for vsock, right?19:49:58
@elvishjerricco:matrix.orgElvishJerriccoI don't understand19:50:11
@elvishjerricco:matrix.orgElvishJerriccoI thought we just don't need anything?19:50:31
@gdamjan:spodeli.orggdamjanhow do you mean? :D19:50:56
@gdamjan:spodeli.orggdamjansshd will not natively listen on vsock, so you need "something"19:51:09
@gdamjan:spodeli.orggdamjan
# /run/systemd/generator/sshd-vsock.socket
# Automatically generated by systemd-ssh-generator

[Unit]
Description=OpenSSH Server Socket (systemd-ssh-generator, AF_VSOCK)
Documentation=man:systemd-ssh-generator(8)
Wants=ssh-access.target
Before=ssh-access.target

[Socket]
ListenStream=vsock::22
Accept=yes
PollLimitIntervalSec=30s
PollLimitBurst=50
19:51:17
@gdamjan:spodeli.orggdamjanthis is the generated .socket unit ^19:51:26
@raitobezarius:matrix.orgraitobezarius
In reply to @arianvp:matrix.org
Not 100% sure. Raito will know
there's a generator thingie
19:51:56
@gdamjan:spodeli.orggdamjan

and the service

# /run/systemd/generator/sshd-generated@.service
# Automatically generated by systemd-ssh-generator

[Unit]
Description=OpenSSH Per-Connection Server Daemon
Documentation=man:systemd-ssh-generator(8) man:sshd(8)
[Service]
ExecStart=-/usr/bin/sshd -i -o "AuthorizedKeysFile ${CREDENTIALS_DIRECTORY}/ssh.ephemeral-authorized_keys-all .ssh>
StandardInput=socket
ImportCredential=ssh.ephemeral-authorized_keys-all
19:52:04
@raitobezarius:matrix.orgraitobezariusi can pull you my patch again if you need it19:52:06
@arianvp:matrix.orgArianI mean is vsock available without a kernel module19:52:18
@raitobezarius:matrix.orgraitobezariusyes19:52:21
@raitobezarius:matrix.orgraitobezariusvsock is always available in the kernel19:52:24
@raitobezarius:matrix.orgraitobezariusit's a native AF_19:52:28
@arianvp:matrix.orgArianYeh I think many cases what generators do conflicts with what nixos module system does19:52:30
@arianvp:matrix.orgArianAnd sometimes makes sense to just generate units at NixOS level instead19:52:44
@raitobezarius:matrix.orgraitobezarius
From 8d29f82f74491c0e929d7518747d91651fc6e821 Mon Sep 17 00:00:00 2001
From: Raito Bezarius <masterancpp@gmail.com>
Date: Sun, 1 Dec 2024 21:23:06 +0100
Subject: [PATCH] nixos/programs/ssh: support connection to a AF_VSOCK/AF_UNIX
 upon demand

Since systemd 256, it's possible to connect to VMs and containers
without using any classical IP networking.

Rather, we can leverage modern AF_VSOCK (for virtual machine
communications) or classical AF_UNIX (for containers).

See
https://www.freedesktop.org/software/systemd/man/256/systemd-ssh-proxy.html
for more documentation.

Change-Id: I2e06a6ebb6d0f34c9cfd69d6dc2aef983b47ed6f
Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
---
 nixos/modules/programs/ssh.nix | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/nixos/modules/programs/ssh.nix b/nixos/modules/programs/ssh.nix
index f2ef248d7866..55c2f4d248a0 100644
--- a/nixos/modules/programs/ssh.nix
+++ b/nixos/modules/programs/ssh.nix
@@ -26,6 +26,22 @@ let
   knownHostsFiles = [ "/etc/ssh/ssh_known_hosts" ]
     ++ builtins.map pkgs.copyPathToStore cfg.knownHostsFiles;
 
+  # Taken from https://www.freedesktop.org/software/systemd/man/256/systemd-ssh-proxy.html
+  sshSystemdProxyConfig = ''
+    # AF_VSOCK / AF_UNIX support for SSH
+    # Built for VMs / containers access without going via the classical networking stack.
+    Host unix/* vsock/*
+      ProxyCommand ${config.systemd.package}/lib/systemd/systemd-ssh-proxy %h %p
+      ProxyUseFdpass yes
+      CheckHostIP no
+
+    # Local host SSH without classical networking
+    Host .host
+      ProxyCommand ${config.systemd.package}/lib/systemd/systemd-ssh-proxy unix/run/ssh-unix-local/socket %p
+      ProxyUseFdpass yes
+      CheckHostIP no
+  '';
+
 in
 {
   ###### interface
@@ -33,6 +49,18 @@ in
   options = {
 
     programs.ssh = {
+      enableSystemdSshProxySupport = lib.mkOption {
+        type = lib.types.bool;
+        default = false;
+        description = ''
+          Whether to configure `ssh` so that it's possible to perform:
+          - `ssh vsock/<cid>`
+          - `ssh unix/a/path/to/some/container/unix/socket`
+          - `ssh some_name.host` to connect to the local host without involving networking
+
+          See https://www.freedesktop.org/software/systemd/man/256/systemd-ssh-proxy.html for details.
+        '';
+      };
 
       enableAskPassword = lib.mkOption {
         type = lib.types.bool;
@@ -295,6 +323,7 @@ in
         Host *
         GlobalKnownHostsFile ${builtins.concatStringsSep " " knownHostsFiles}
 
+        ${lib.optionalString (cfg.enableSystemdSshProxySupport) sshSystemdProxyConfig}
         ${lib.optionalString (!config.networking.enableIPv6) "AddressFamily inet"}
         ${lib.optionalString cfg.setXAuthLocation "XAuthLocation ${pkgs.xorg.xauth}/bin/xauth"}
         ${lib.optionalString (cfg.forwardX11 != null) "ForwardX11 ${if cfg.forwardX11 then "yes" else "no"}"}
-- 
2.47.0

this is almost all what you need IIRC

19:53:09

Show newer messages


Back to Room ListRoom Version: 6