Subject: [bug#76081] [PATCH v9 6/7] services: oci: Migrate oci-configuration to (guix records).

From: Giacomo Leidi <[email-masked]>
Date: Tue, 18 Mar 2025 18:51:11 +0100

This commit migrates oci-configuration to (guix records) singe it
appears (for-home (oci-configuration ...)) does not work as expected
with (gnu services configuration).  This is supposed to be completely
transparent for users and can be reverted in the
future once this has been implemented.

* gnu/service/containers.scm: Migrate oci-configuration to (guix records).
---
 gnu/services/containers.scm | 199 +++++++++++++++++++++---------------
 1 file changed, 117 insertions(+), 82 deletions(-)

diff --git a/gnu/services/containers.scm b/gnu/services/containers.scm
index 5dac8e80f22..7a44e1c0f7c 100644
--- a/gnu/services/containers.scm
+++ b/gnu/services/containers.scm
@@ -39,6 +39,7 @@ (define-module (gnu services containers)
   #:use-module (guix packages)
   #:use-module (guix profiles)
   #:use-module ((guix scripts pack) #:prefix pack:)
+  #:use-module (guix records)
   #:use-module (guix store)
   #:use-module (srfi srfi-1)
   #:use-module (ice-9 format)
@@ -164,6 +165,7 @@ (define-module (gnu services containers)
             oci-container-shepherd-service
             oci-objects-merge-lst
             oci-extension-merge
+            oci-service-extension-wrap-validate
             oci-service-type
             oci-service-accounts
             oci-service-profile
@@ -391,7 +393,7 @@ (define (oci-runtime-name runtime)
 (define (oci-runtime-group runtime maybe-group)
   "Implement the logic behind selection of the group that is to be used by
 Shepherd to execute OCI commands."
-  (if (not (maybe-value-set? maybe-group))
+  (if (eq? maybe-group #f)
       (if (eq? 'podman runtime)
           "cgroup"
           "docker")
@@ -762,62 +764,74 @@ (define (list-of-oci-networks? value)
 (define (package-or-string? value)
   (or (package? value) (string? value)))
 
-(define-maybe/no-serialization package-or-string)
-
-(define-configuration/no-serialization oci-configuration
-  (runtime
-   (symbol 'docker)
-   "The OCI runtime to use to run commands.  It can be either @code{'docker} or
-@code{'podman}."
-   (sanitizer oci-sanitize-runtime))
-  (runtime-cli
-   (maybe-package-or-string)
-   "The OCI runtime command line to be installed in the system profile and used
-to provision OCI resources, it can be either a package or a string representing
-an absolute path to the runtime binary entrypoint.  When unset it will default
-to @code{docker-cli} package for the @code{'docker} runtime or to @code{podman}
-package for the @code{'podman} runtime.")
-  (runtime-extra-arguments
-   (list '())
-   "A list of strings, gexps or file-like objects that will be placed
-after each @command{docker} or @command{podman} invokation.")
-  (user
-   (string "oci-container")
-   "The user name under whose authority OCI runtime commands will be run.")
-  (group
-   (maybe-string)
-   "The group name under whose authority OCI commands will be run.  When
-using the @code{'podman} OCI runtime, this field will be ignored and the
-default group of the user configured in the @code{user} field will be used.")
-  (subuids-range
-   (maybe-subid-range)
-   "An optional @code{subid-range} record allocating subuids for the user from
-the @code{user} field.  When unset, with the rootless Podman OCI runtime, it
-defaults to @code{(subid-range (name \"oci-container\"))}.")
-  (subgids-range
-   (maybe-subid-range)
-   "An optional @code{subid-range} record allocating subgids for the user from
-the @code{user} field.  When unset, with the rootless Podman OCI runtime, it
-defaults to @code{(subid-range (name \"oci-container\"))}.")
-  (containers
-   (list-of-oci-containers '())
-   "The list of @code{oci-container-configuration} records representing the
-containers to provision.  Most users are supposed not to use this field and use
-the @code{oci-extension} record instead.")
-  (networks
-   (list-of-oci-networks '())
-   "The list of @code{oci-network-configuration} records representing the
-networks to provision.  Most users are supposed not to use this field and use
-the @code{oci-extension} record instead.")
-  (volumes
-   (list-of-oci-volumes '())
-   "The list of @code{oci-volume-configuration} records representing the
-volumes to provision.  Most users are supposed not to use this field and use
-the @code{oci-extension} record instead.")
-  (verbose?
-   (boolean #f)
-   "When true, additional output will be printed, allowing to better follow the
-flow of execution."))
+;; (for-home (oci-configuration ...)) is not able to replace for-home? with #t,
+;; pk prints #f. Once for-home will be able to work with (gnu services configuration) the
+;; record can be migrated back to define-configuration.
+(define-record-type* <oci-configuration>
+  oci-configuration
+  make-oci-configuration
+  oci-configuration?
+  this-oci-configuration
+
+  (runtime                  oci-configuration-runtime
+                            (default 'docker))
+  (runtime-cli              oci-configuration-runtime-cli
+                            (default #f))                               ; package or string
+  (runtime-extra-arguments  oci-configuration-runtime-extra-arguments   ; strings or gexps
+                            (default '()))                              ; or file-like objects
+  (user                     oci-configuration-user
+                            (default "oci-container"))
+  (group                    oci-configuration-group                     ; string
+                            (default #f))
+  (subuids-range            oci-configuration-subuids-range             ; subid-range
+                            (default #f))
+  (subgids-range            oci-configuration-subgids-range             ; subid-range
+                            (default #f))
+  (containers               oci-configuration-containers                ; oci-container-configurations
+                            (default '()))
+  (networks                 oci-configuration-networks                  ; oci-network-configurations
+                            (default '()))
+  (volumes                  oci-configuration-volumes                   ; oci-volume-configurations
+                            (default '()))
+  (verbose?                 oci-configuration-verbose?
+                            (default #f))
+  (home-service?            oci-configuration-home-service?
+                            (default for-home?) (innate)))
+
+;; TODO: This procedure can be dropped once we switch to define-configuration for
+;; oci-configuration.
+(define (oci-configuration-valid? config)
+  (define runtime-cli
+    (oci-configuration-runtime-cli config))
+  (define group
+    (oci-configuration-group config))
+  (define subuids-range
+    (oci-configuration-subuids-range config))
+  (define subgids-range
+    (oci-configuration-subgids-range config))
+  (and
+   (symbol?
+    (oci-sanitize-runtime (oci-configuration-runtime config)))
+   (or (eq? runtime-cli #f)
+       (package-or-string? runtime-cli))
+   (list? (oci-configuration-runtime-extra-arguments config))
+   (string? (oci-configuration-user config))
+   (or (eq? group #f)
+       (string? group))
+   (or (eq? subuids-range #f)
+       (subid-range? subuids-range))
+   (or (eq? subgids-range #f)
+       (subid-range? subgids-range))
+   (list-of-oci-containers?
+    (oci-configuration-containers config))
+   (list-of-oci-networks?
+    (oci-configuration-networks config))
+   (list-of-oci-volumes?
+    (oci-configuration-volumes config))
+   (boolean?
+    (oci-configuration-verbose? config))
+   (boolean?
+    (oci-configuration-home-service? config))))
 
 (define (oci-runtime-system-environment runtime user)
   (if (eq? runtime 'podman)
@@ -833,7 +847,7 @@ (define (oci-runtime-cli runtime runtime-cli path)
       ;; It is a user defined absolute path
       runtime-cli
       #~(string-append
-         #$(if (not (maybe-value-set? runtime-cli))
+         #$(if (eq? runtime-cli #f)
                path
                runtime-cli)
          #$(if (eq? 'podman runtime)
@@ -1577,18 +1591,27 @@ (define (oci-configuration->shepherd-services config)
                   (passwd:gid
                    (getpwnam #$user))))
               (oci-runtime-group config (oci-configuration-group config))))
-         (verbose? (oci-configuration-verbose? config)))
-    (oci-state->shepherd-services runtime system-runtime-cli containers networks volumes
-                                  #:user user
-                                  #:group group
-                                  #:verbose? verbose?
-                                  #:runtime-extra-arguments
-                                  runtime-extra-arguments
-                                  #:runtime-environment
-                                  (oci-runtime-system-environment runtime user)
-                                  #:runtime-requirement
-                                  (oci-runtime-system-requirement runtime)
-                                  #:networks-requirement '(networking))))
+         (verbose? (oci-configuration-verbose? config))
+         (home-service?
+          (oci-configuration-home-service? config)))
+    (if home-service?
+        (oci-state->shepherd-services runtime home-runtime-cli containers networks volumes
+                                      #:verbose? verbose?
+                                      #:networks-name
+                                      (oci-networks-home-shepherd-name runtime)
+                                      #:volumes-name
+                                      (oci-volumes-home-shepherd-name runtime))
+        (oci-state->shepherd-services runtime system-runtime-cli containers networks volumes
+                                      #:user user
+                                      #:group group
+                                      #:verbose? verbose?
+                                      #:runtime-extra-arguments
+                                      runtime-extra-arguments
+                                      #:runtime-environment
+                                      (oci-runtime-system-environment runtime user)
+                                      #:runtime-requirement
+                                      (oci-runtime-system-requirement runtime)
+                                      #:networks-requirement '(networking)))))
 
 (define (oci-service-subids config)
   "Return a subids-extension record representing subuids and subgids required by
@@ -1616,14 +1639,14 @@ (define (oci-service-subids config)
   (define subgid-ranges
     (delete-duplicate-ranges
      (cons
-      (if (not (maybe-value-set? subgids))
+      (if (eq? subgids #f)
           (subid-range (name user))
           subgids)
       container-users)))
   (define subuid-ranges
     (delete-duplicate-ranges
      (cons
-      (if (not (maybe-value-set? subuids))
+      (if (eq? subuids #f)
           (subid-range (name user))
           subuids)
       container-users)))
@@ -1682,13 +1705,21 @@ (define (oci-service-profile runtime runtime-cli)
           '()
           (list
            (cond
-            ((maybe-value-set? runtime-cli)
+            ((not (eq? runtime-cli #f))
              runtime-cli)
             ((eq? 'podman runtime)
              podman)
             (else
              docker-cli))))))
 
+(define (oci-service-extension-wrap-validate extension)
+  (lambda (config)
+    (if (oci-configuration-valid? config)
+        (extension config)
+        (raise
+         (formatted-message
+          (G_ "Invalide oci-configuration ~a.") config)))))
+
 (define (oci-configuration-extend config extension)
   (oci-configuration
    (inherit config)
@@ -1717,18 +1748,22 @@ (define oci-service-type
                 (extensions
                  (list
                   (service-extension profile-service-type
-                                     (lambda (config)
-                                       (let ((runtime-cli
-                                              (oci-configuration-runtime-cli config))
-                                             (runtime
-                                              (oci-configuration-runtime config)))
-                                         (oci-service-profile runtime runtime-cli))))
+                                     (oci-service-extension-wrap-validate
+                                      (lambda (config)
+                                        (let ((runtime-cli
+                                               (oci-configuration-runtime-cli config))
+                                              (runtime
+                                               (oci-configuration-runtime config)))
+                                          (oci-service-profile runtime runtime-cli)))))
                   (service-extension subids-service-type
-                                     oci-service-subids)
+                                     (oci-service-extension-wrap-validate
+                                      oci-service-subids))
                   (service-extension account-service-type
-                                     oci-service-accounts)
+                                     (oci-service-extension-wrap-validate
+                                      oci-service-accounts))
                   (service-extension shepherd-root-service-type
-                                     oci-configuration->shepherd-services)))
+                                     (oci-service-extension-wrap-validate
+                                      oci-configuration->shepherd-services))))
                 ;; Concatenate OCI object lists.
                 (compose (lambda (args)
                            (fold oci-extension-merge
-- 
2.48.1