about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-01 19:22:41 +0000
committerPjotr <wrk@bosa.lan>2026-10-01 19:22:41 +0000
commitb60a5b538066d506419412239b0c2f065415de13 (patch)
tree4cb62f72a26511ab99a0c8b9fb35610b051e2505
parent97e25ef377a9f87d94423b1df7bc5a57cd7fa6c3 (diff)
downloadgn-machines-b60a5b538066d506419412239b0c2f065415de13.tar.gz
container-ssh: factor the container SSH setup out of the containers
bnw-container.scm and cgit-container.scm carried identical SSH code
(openssh with password-less root login via hello-container-key, the
pinned ECDSA host key installed into /etc/ssh, and the /var/empty +
/var/run/sshd fix), differing only in the port.  Move it into the new
(genanetwork services container-ssh) module as container-ssh-service,
a service PROCEDURE returning the list of services, parameterized by
port-number (and optionally host key / authorized key).

It is a procedure rather than a service-type on purpose:
openssh-service-type has no extension point that merges
configurations, so a wrapping service-type would have to re-implement
sshd's shepherd, activation and account services.

Both containers now splice (container-ssh-service #:port-number N)
into their services with append.  Behavior-preserving by construction
and verified by content: guix system container resolves to the exact
same store paths as before the refactor (bnw 3jiirryqwjb..., cgit
zga020ym4y3...), and both containers re-tested green (bnw suite 13/13
including the ssh tool check; cgit ssh login, host key, /var/empty and
services all verified over ssh).
-rw-r--r--genenetwork/services/bnw-container.scm72
-rw-r--r--genenetwork/services/cgit-container.scm73
-rw-r--r--genenetwork/services/container-ssh.scm89
3 files changed, 113 insertions, 121 deletions
diff --git a/genenetwork/services/bnw-container.scm b/genenetwork/services/bnw-container.scm
index 99de075..0ccb79f 100644
--- a/genenetwork/services/bnw-container.scm
+++ b/genenetwork/services/bnw-container.scm
@@ -21,30 +21,18 @@
 (define-module (genenetwork services bnw-container)
   #:use-module (gnu)
   #:use-module (genenetwork services bnw)
+  #:use-module (genenetwork services container-ssh)
   #:use-module (guix gexp)
   #:use-module (guix build utils)
   #:use-module (gnu services base)
   #:use-module (gnu services shepherd)
   #:use-module (gnu services sysctl))
-(use-service-modules networking ssh)
 (use-package-modules base bash admin linux)
 
-;; Pinned SSH host key, fetched from the building user's $HOME (the
-;; keys are generated by hand and never enter the repository — see
-;; the BNW README):
-;;
-;;   ssh-keygen -t ecdsa   -f ~/ssh-container-key   -N ""
-;;   ssh-keygen -t ed25519 -f ~/hello-container-key -N ""
-;;
-;; Every container presents the same host key, so known_hosts does
-;; not complain about a changed identification.
-(define %key-directory (getenv "HOME"))
-(define ssh-host-key (local-file
-                      (string-append %key-directory "/ssh-container-key")
-                      "ssh_host_ecdsa_key"))
-(define ssh-host-key.pub (local-file
-                          (string-append %key-directory "/ssh-container-key.pub")
-                          "ssh_host_ecdsa_key.pub"))
+;; The pinned SSH keys live in the building user's $HOME and are
+;; fetched from there at build time; container-ssh-service (shared
+;; with the other containers) wires them into sshd.  See the Keys
+;; section of cgit-README.md.
 
 (operating-system
   (host-name "bnw-container")
@@ -73,51 +61,15 @@
      (service bnw-service-type)
      (service bnw-nginx-service-type
               (bnw-nginx-configuration
-               (anubis? #t)))
+               (anubis? #t))))
 
-     ;; Password-less root login with hello-container-key.  Host keys
-     ;; are pinned (see ssh-host-key above), not generated at boot.
-     (service openssh-service-type
-              (openssh-configuration
-               (port-number 2222)
-               (permit-root-login #t)
-               (password-authentication? #f)
-               (generate-host-keys? #f)
-               (authorized-keys
-                `(("root" ,(local-file
-                            (string-append %key-directory
-                                           "/hello-container-key.pub")
-                            "authorized_keys"))))))
-
-     ;; Install the pinned ECDSA host key into /etc/ssh where sshd
-     ;; looks for it by default.
-     (simple-service 'container-ssh-host-key
-                     activation-service-type
-                     (with-imported-modules '((guix build utils))
-                       #~(begin
-                           (use-modules (guix build utils))
-                           (mkdir-p "/etc/ssh")
-                           (copy-file #$ssh-host-key
-                                      "/etc/ssh/ssh_host_ecdsa_key")
-                           (chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
-                           (copy-file #$ssh-host-key.pub
-                                      "/etc/ssh/ssh_host_ecdsa_key.pub")
-                           (chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))
-
-     ;; sshd needs the privilege separation directory /var/empty; on
-     ;; a full system it is created as the home directory of some
-     ;; system account, but a minimal container has none.  The sshd
-     ;; privsep user's home is /var/run/sshd and is likewise missing
-     ;; in a minimal container (see the cgit container for the same
-     ;; fix).
-     (simple-service 'container-var-empty
-                     activation-service-type
-                     (with-imported-modules '((guix build utils))
-                       #~(begin
-                           (use-modules (guix build utils))
-                           (mkdir-p "/var/empty")
-                           (mkdir-p "/var/run/sshd"))))
+    ;; SSH on port 2222: password-less root login with
+    ;; hello-container-key, the pinned ECDSA host key (not generated
+    ;; at boot) and the /var/empty fix -- all shared with the other
+    ;; containers via container-ssh-service.
+    (container-ssh-service #:port-number 2222)
 
+    (list
      ;; In a container sysctl (writes /proc/sys) and udev (coldplug on
      ;; the static /dev the container runtime sets up) are useless and
      ;; their start-up hangs the whole boot: urandom-seed requires
diff --git a/genenetwork/services/cgit-container.scm b/genenetwork/services/cgit-container.scm
index 3fe951a..d0d8c55 100644
--- a/genenetwork/services/cgit-container.scm
+++ b/genenetwork/services/cgit-container.scm
@@ -35,34 +35,19 @@
 (define-module (genenetwork services cgit-container)
   #:use-module (gnu)
   #:use-module (genenetwork services cgit)
+  #:use-module (genenetwork services container-ssh)
   #:use-module (guix gexp)
   #:use-module (guix build utils)
   #:use-module (gnu services base)
   #:use-module (gnu services shepherd)
   #:use-module (gnu services sysctl)
   #:use-module (gnu packages version-control))
-(use-service-modules networking ssh web)
 (use-package-modules base bash admin compression linux python)
 
-;; The pinned keys live outside the repository, directly in $HOME
-;; (see cgit-README.md) and are fetched from there at build time:
-;;
-;;   ssh-keygen -t ecdsa   -f ~/ssh-container-key   -N ""
-;;   ssh-keygen -t ed25519 -f ~/hello-container-key -N ""
-;;
-;; ssh-container-key is the pinned SSH *host* key the containers
-;; present; hello-container-key authorizes password-less root logins.
-(define %key-directory (getenv "HOME"))
-
-;; Pinned SSH host key (from etc/), so
-;; every container presents the same host key and known_hosts does not
-;; complain about a changed identification.
-(define ssh-host-key (local-file
-                      (string-append %key-directory "/ssh-container-key")
-                      "ssh_host_ecdsa_key"))
-(define ssh-host-key.pub (local-file
-                          (string-append %key-directory "/ssh-container-key.pub")
-                          "ssh_host_ecdsa_key.pub"))
+;; The pinned SSH keys live in the building user's $HOME and are
+;; fetched from there at build time; container-ssh-service (shared
+;; with the other containers) wires them into sshd.  See the Keys
+;; section of cgit-README.md.
 
 (define %cgit-web-config
   (cgit-web-configuration
@@ -100,49 +85,15 @@
      (service cgit-web-service-type
               %cgit-web-config)
      (service cgit-nginx-service-type
-              %cgit-web-config)
-     ;; Password-less root login with hello-container-key.  Host keys
-     ;; are pinned (see ssh-host-key above), not generated at boot.
-     (service openssh-service-type
-              (openssh-configuration
-               (port-number 2224)
-               (permit-root-login #t)
-               (password-authentication? #f)
-               (generate-host-keys? #f)
-               (authorized-keys
-                `(("root" ,(local-file
-                            (string-append %key-directory
-                                           "/hello-container-key.pub")
-                            "authorized_keys"))))))
+              %cgit-web-config))
 
-     ;; Install the pinned ECDSA host key into /etc/ssh where sshd
-     ;; looks for it by default.
-     (simple-service 'container-ssh-host-key
-                     activation-service-type
-                     (with-imported-modules '((guix build utils))
-                       #~(begin
-                           (use-modules (guix build utils))
-                           (mkdir-p "/etc/ssh")
-                           (copy-file #$ssh-host-key
-                                      "/etc/ssh/ssh_host_ecdsa_key")
-                           (chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
-                           (copy-file #$ssh-host-key.pub
-                                      "/etc/ssh/ssh_host_ecdsa_key.pub")
-                           (chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))
-
-     ;; sshd needs the privilege separation directory /var/empty; on
-     ;; a full system it is created as the home directory of some
-     ;; system account, but a minimal container has none.  The sshd
-     ;; privsep user's home is /var/run/sshd; normally user-homes
-     ;; creates it, but it can be skipped in minimal containers.
-     (simple-service 'container-var-empty
-                     activation-service-type
-                     (with-imported-modules '((guix build utils))
-                       #~(begin
-                           (use-modules (guix build utils))
-                           (mkdir-p "/var/empty")
-                           (mkdir-p "/var/run/sshd"))))
+    ;; SSH on port 2224: password-less root login with
+    ;; hello-container-key, the pinned ECDSA host key (not generated
+    ;; at boot) and the /var/empty fix -- all shared with the other
+    ;; containers via container-ssh-service.
+    (container-ssh-service #:port-number 2224)
 
+    (list
      ;; NOTE: cgit-demo-repository-service is intentionally NOT listed:
      ;; /srv/git is a read-only bind mount of the host repositories
      ;; (see %repos-source), so nothing may seed into it.  Create the
diff --git a/genenetwork/services/container-ssh.scm b/genenetwork/services/container-ssh.scm
new file mode 100644
index 0000000..7785d58
--- /dev/null
+++ b/genenetwork/services/container-ssh.scm
@@ -0,0 +1,89 @@
+;;; genenetwork-machines --- Guix configuration for genenetwork machines
+;;;
+;;; Shared SSH support for the minimal system containers run by the
+;;; run-guix-container daemon (bnw-container.scm, cgit-container.scm,
+;;; ...).  It gives a container password-less root login with a pinned
+;;; client key, a pinned SSH host key so every rebuild presents the
+;;; same key (no known_hosts complaints), and the directories sshd
+;;; needs in a minimal container.
+
+(define-module (genenetwork services container-ssh)
+  #:use-module (gnu)
+  #:use-module (guix gexp)
+  #:use-module (gnu services ssh)
+  #:export (container-ssh-service))
+
+;; The pinned keys live outside the repository, directly in $HOME (see
+;; the Keys section of cgit-README.md) and are fetched from there at
+;; build time:
+;;
+;;   ssh-keygen -t ecdsa   -f ~/ssh-container-key   -N ""
+;;   ssh-keygen -t ed25519 -f ~/hello-container-key -N ""
+;;
+;; ssh-container-key is the pinned SSH *host* key the containers
+;; present; hello-container-key authorizes password-less root logins.
+(define %key-directory (getenv "HOME"))
+
+(define* (container-ssh-service #:key
+                                (port-number 2222)
+                                (host-key (local-file
+                                           (string-append %key-directory
+                                                          "/ssh-container-key")
+                                           "ssh_host_ecdsa_key"))
+                                (host-key.pub (local-file
+                                               (string-append %key-directory
+                                                              "/ssh-container-key.pub")
+                                               "ssh_host_ecdsa_key.pub"))
+                                (authorized-key (local-file
+                                                 (string-append %key-directory
+                                                                "/hello-container-key.pub")
+                                                 "authorized_keys")))
+  "Return the services that give a minimal Guix system container SSH
+on PORT-NUMBER: an openssh service with password-less root login for
+AUTHORIZED-KEY, the pinned ECDSA HOST-KEY installed into /etc/ssh
+(generate-host-keys? is #f, so every rebuild presents the same host
+key), and the /var/empty and /var/run/sshd directories sshd needs when
+the container has no system accounts.
+
+This is a service PROCEDURE (returning a list of services) rather than
+a service-type on purpose: openssh-service-type has no extension point
+that merges configurations, so a wrapping service-type would have to
+re-implement sshd's shepherd, activation and account services."
+  (list
+   ;; Password-less root login with the client key.  Host keys are
+   ;; pinned, not generated at boot.
+   (service openssh-service-type
+            (openssh-configuration
+             (port-number port-number)
+             (permit-root-login #t)
+             (password-authentication? #f)
+             (generate-host-keys? #f)
+             (authorized-keys `(("root" ,authorized-key)))))
+
+   ;; Install the pinned ECDSA host key into /etc/ssh where sshd
+   ;; looks for it by default.
+   (simple-service 'container-ssh-host-key
+                   activation-service-type
+                   (with-imported-modules '((guix build utils))
+                     #~(begin
+                         (use-modules (guix build utils))
+                         (mkdir-p "/etc/ssh")
+                         (copy-file #$host-key
+                                    "/etc/ssh/ssh_host_ecdsa_key")
+                         (chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
+                         (copy-file #$host-key.pub
+                                    "/etc/ssh/ssh_host_ecdsa_key.pub")
+                         (chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))
+
+   ;; sshd needs the privilege separation directory /var/empty; on
+   ;; a full system it is created as the home directory of some
+   ;; system account, but a minimal container has none.  The sshd
+   ;; privsep user's home is /var/run/sshd and is likewise missing
+   ;; in a minimal container.
+   (simple-service 'container-var-empty
+                   activation-service-type
+                   (with-imported-modules '((guix build utils))
+                     #~(begin
+                         (use-modules (guix build utils))
+                         (mkdir-p "/var/empty")
+                         (mkdir-p "/var/run/sshd"))))))