diff mbox series

[bug#46043] Allow booting with custom shepherd package (fixed)

Message ID 874kj8bv8w.fsf@telenet.be
State Accepted
Headers show
Series [bug#46043] Allow booting with custom shepherd package (fixed) | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

M Jan. 22, 2021, 7:57 p.m. UTC
[previous e-mail was bogus. ggrr emacs, why can't I open e-mail
I was composing and saved with C-x C-f and continue later,
even if I set mml-mode]

Hi Guix,

If I didn't miss anything, this patch allows for booting with a non-standard
shepherd package, which may be useful for using features from a not-yet
released shepherd (presuming you've packaged the custom shepherd first :)).

I'll send a follow-up e-mail to confirm/disconfirm whether this works
well.

Maxime

Comments

Leo Prikler Jan. 23, 2021, 8:25 a.m. UTC | #1
Hi Maxime,

Am Freitag, den 22.01.2021, 20:57 +0100 schrieb Maxime Devos:
>     (compose concatenate)
> -   (extend append)
> +   (extend (lambda (config extra-services)
> +             (shepherd-configuration
> +               (inherit config)
> +               (services (append (shepherd-configuration-services
> config)
> +                                 extra-services)))))
I think you should also patch compose or drop it.  I sadly don't know,
what impact that would have, but IIUC you should only spawn one
shepherd-root-service anyway.

Regards,
Leo
M Jan. 23, 2021, 3:11 p.m. UTC | #2
Hi Leo,
On Sat, 2021-01-23 at 09:25 +0100, Leo Prikler wrote:
> Hi Maxime,
> 
> Am Freitag, den 22.01.2021, 20:57 +0100 schrieb Maxime Devos:
> >     (compose concatenate)
> > -   (extend append)
> > +   (extend (lambda (config extra-services)
> > +             (shepherd-configuration
> > +               (inherit config)
> > +               (services (append (shepherd-configuration-services
> > config)
> > +                                 extra-services)))))
> I think you should also patch compose or drop it.  I sadly don't know,
> what impact that would have, but IIUC you should only spawn one
> shepherd-root-service anyway.

Only one shepherd-root-service-type should be spawned, correct.
In practice, this means you'll need to use a construct like this
(if I didn't make any errors):

  (modify-services %base-services
    (shepherd-root-service-type c =>
      (shepherd-configuration
        (inherit c)
        (shepherd a-custom-shepherd))))

For clarification: shepherd services should be added
via the service-extension mechanism, not by
manually changing the 'services' field
(though the latter is a possibility now).

About compose and concatenate: I think it is correct as-is.
If compose is dropped, then shepherd-root-service-type wouldn't be
extensible anymore. I don't see how it could be patched
in a way that makes sense.

Compare with udev-service-type as described in the manual,
which follows the same structure.

Regards,
Maxime
Leo Prikler Jan. 23, 2021, 3:25 p.m. UTC | #3
Hi,

Am Samstag, den 23.01.2021, 16:11 +0100 schrieb Maxime Devos:
> [...]
> 
> About compose and concatenate: I think it is correct as-is.
> If compose is dropped, then shepherd-root-service-type wouldn't be
> extensible anymore. I don't see how it could be patched
> in a way that makes sense.
Ahh, my bad.  It seems I don't have a strong enough understanding of
the difference between compose and extend yet.  Nvm then.

Regards,
Leo
diff mbox series

Patch

From e4f6743a5502eb6c878ee2c3bf7f7153f8006e01 Mon Sep 17 00:00:00 2001
From: Maxime Devos <maximedevos@telenet.be>
Date: Fri, 22 Jan 2021 20:06:55 +0100
Subject: [PATCH] services: shepherd: allow custom 'shepherd' package

* gnu/services/shepherd.scm
  (<shepherd-configuration>): New record.
  (shepherd-boot-gexp, shepherd-root-service-type): Use it.
  (scm->go, shepherd-configuration-file): Allow passing custom
  shepherd package.
* doc/guix.texi (Shepherd Services). Document it.
---
 doc/guix.texi             | 17 +++++++++++++-
 gnu/services/shepherd.scm | 48 +++++++++++++++++++++++++++++----------
 2 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index a765b3792e..344ffcfea5 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -32707,9 +32707,24 @@  The service type for the Shepherd ``root service''---i.e., PID@tie{}1.
 
 This is the service type that extensions target when they want to create
 shepherd services (@pxref{Service Types and Services}, for an example).
-Each extension must pass a list of @code{<shepherd-service>}.
+Each extension must pass a list of @code{<shepherd-service>}. Its
+value must be a @code{shepherd-configuration}, as described below:
 @end defvr
 
+@deftp {Data Type} shepherd-configuration
+This data type represents shepherd's configuration.
+
+@table @code
+@item shepherd (default: @code{shepherd})
+The shepherd package to use.
+
+@item services (default: @code{'()})
+A list of @code{<shepherd-service>} to start.
+You should probably use the service extension
+mechanism instead (@pxref{Shepherd Services}).
+@end table
+@end deftp
+
 @defvr {Scheme Variable} %shepherd-root-service
 This service represents PID@tie{}1.
 @end defvr
diff --git a/gnu/services/shepherd.scm b/gnu/services/shepherd.scm
index d2f9776288..748b19957a 100644
--- a/gnu/services/shepherd.scm
+++ b/gnu/services/shepherd.scm
@@ -3,6 +3,7 @@ 
 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
 ;;; Copyright © 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -36,7 +37,12 @@ 
   #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-34)
   #:use-module (srfi srfi-35)
-  #:export (shepherd-root-service-type
+  #:export (shepherd-configuration
+            shepherd-configuration?
+            shepherd-configuration-shepherd
+            shepherd-configuration-services
+
+            shepherd-root-service-type
             %shepherd-root-service
             shepherd-service-type
 
@@ -76,7 +82,18 @@ 
 ;;; Code:
 
 
-(define (shepherd-boot-gexp services)
+(define-record-type* <shepherd-configuration>
+  shepherd-configuration make-shepherd-configuration
+  shepherd-configuration?
+  (shepherd shepherd-configuration-shepherd
+            (default shepherd)) ; package
+  (services shepherd-configuration-services
+            (default '()))) ; list of <shepherd-service>
+
+(define (shepherd-boot-gexp config)
+  "Return a gexp starting the shepherd service."
+  (let ((shepherd (shepherd-configuration-shepherd config))
+        (services (shepherd-configuration-services config)))
   #~(begin
       ;; Keep track of the booted system.
       (false-if-exception (delete-file "/run/booted-system"))
@@ -95,7 +112,7 @@ 
       ;; Start shepherd.
       (execl #$(file-append shepherd "/bin/shepherd")
              "shepherd" "--config"
-             #$(shepherd-configuration-file services))))
+             #$(shepherd-configuration-file services #:shepherd shepherd)))))
 
 (define shepherd-root-service-type
   (service-type
@@ -103,20 +120,25 @@ 
    ;; Extending the root shepherd service (aka. PID 1) happens by
    ;; concatenating the list of services provided by the extensions.
    (compose concatenate)
-   (extend append)
+   (extend (lambda (config extra-services)
+             (shepherd-configuration
+               (inherit config)
+               (services (append (shepherd-configuration-services config)
+                                 extra-services)))))
    (extensions (list (service-extension boot-service-type
                                         shepherd-boot-gexp)
                      (service-extension profile-service-type
                                         (const (list shepherd)))))
+   (default-value (shepherd-configuration))
    (description
     "Run the GNU Shepherd as PID 1---i.e., the operating system's first
 process.  The Shepherd takes care of managing services such as daemons by
 ensuring they are started and stopped in the right order.")))
 
 (define %shepherd-root-service
-  ;; The root shepherd service, aka. PID 1.  Its parameter is a list of
-  ;; <shepherd-service> objects.
-  (service shepherd-root-service-type '()))
+  ;; The root shepherd service, aka. PID 1.  Its parameter is a
+  ;; <shepherd-configuration>.
+  (service shepherd-root-service-type))
 
 (define-syntax shepherd-service-type
   (syntax-rules (description)
@@ -270,9 +292,9 @@  stored."
                                    #~(#$name #$doc #$proc)))
                                 (shepherd-service-actions service))))))))
 
-(define (scm->go file)
+(define* (scm->go file #:key (shepherd shepherd))
   "Compile FILE, which contains code to be loaded by shepherd's config file,
-and return the resulting '.go' file."
+and return the resulting '.go' file. SHEPHERD is used as shepherd package."
   (let-system (system target)
     (with-extensions (list shepherd)
       (computed-file (string-append (basename (scheme-file-name file) ".scm")
@@ -294,11 +316,13 @@  and return the resulting '.go' file."
                      #:options '(#:local-build? #t
                                  #:substitutable? #f)))))
 
-(define (shepherd-configuration-file services)
-  "Return the shepherd configuration file for SERVICES."
+(define* (shepherd-configuration-file services #:key (shepherd shepherd))
+  "Return the shepherd configuration file for SERVICES. SHEPHERD is used
+as shepherd package."
   (assert-valid-graph services)
 
-  (let ((files (map shepherd-service-file services)))
+  (let ((files (map shepherd-service-file services))
+        (scm->go (cute scm->go <> #:shepherd shepherd)))
     (define config
       #~(begin
           (use-modules (srfi srfi-34)
-- 
2.30.0