maint: Provide the configuration file in the VM image.
[jackhill/guix/guix.git] / gnu / services / shepherd.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
8b9cad01 2;;; Copyright © 2013, 2014, 2015, 2016, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
750a4239 3;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
4245ddcb 4;;; Copyright © 2018 Carlo Zancanaro <carlo@zancanaro.id.au>
db4fdc04
LC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
0190c1c0 21(define-module (gnu services shepherd)
116244df
LC
22 #:use-module (guix ui)
23 #:use-module (guix sets)
b5f4e686 24 #:use-module (guix gexp)
e87f0591 25 #:use-module (guix store)
0adfe95a 26 #:use-module (guix records)
e87f0591 27 #:use-module (guix derivations) ;imported-modules, etc.
db4fdc04 28 #:use-module (gnu services)
7b44cae5 29 #:use-module (gnu services herd)
0adfe95a 30 #:use-module (gnu packages admin)
db4fdc04 31 #:use-module (ice-9 match)
80a67734 32 #:use-module (ice-9 vlist)
db4fdc04 33 #:use-module (srfi srfi-1)
80a67734 34 #:use-module (srfi srfi-26)
116244df
LC
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
d4053c71
AK
37 #:export (shepherd-root-service-type
38 %shepherd-root-service
39 shepherd-service-type
40
41 shepherd-service
42 shepherd-service?
43 shepherd-service-documentation
44 shepherd-service-provision
240b57f0 45 shepherd-service-canonical-name
d4053c71 46 shepherd-service-requirement
95ef8b85 47 shepherd-service-one-shot?
d4053c71
AK
48 shepherd-service-respawn?
49 shepherd-service-start
50 shepherd-service-stop
51 shepherd-service-auto-start?
52 shepherd-service-modules
fae685b9 53
70138308
LC
54 shepherd-action
55 shepherd-action?
56 shepherd-action-name
57 shepherd-action-documentation
58 shepherd-action-procedure
59
fae685b9 60 %default-modules
80a67734 61
240b57f0
LC
62 shepherd-service-file
63
a5d78eb6 64 shepherd-service-lookup-procedure
7b44cae5
LC
65 shepherd-service-back-edges
66 shepherd-service-upgrade))
db4fdc04
LC
67
68;;; Commentary:
69;;;
fe1ad5f5 70;;; Instantiating system services as a shepherd configuration file.
db4fdc04
LC
71;;;
72;;; Code:
73
0adfe95a 74
d4053c71 75(define (shepherd-boot-gexp services)
378daa8c
LC
76 #~(begin
77 ;; Keep track of the booted system.
78 (false-if-exception (delete-file "/run/booted-system"))
79 (symlink (readlink "/run/current-system")
80 "/run/booted-system")
0adfe95a 81
378daa8c
LC
82 ;; Close any remaining open file descriptors to be on the safe
83 ;; side. This must be the very last thing we do, because
84 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
85 ;; alive.
86 (let loop ((fd 3))
87 (when (< fd 1024)
88 (false-if-exception (close-fdes fd))
89 (loop (+ 1 fd))))
0adfe95a 90
378daa8c
LC
91 ;; Start shepherd.
92 (execl #$(file-append shepherd "/bin/shepherd")
93 "shepherd" "--config"
94 #$(shepherd-configuration-file services))))
0adfe95a 95
d4053c71 96(define shepherd-root-service-type
0adfe95a 97 (service-type
d4053c71
AK
98 (name 'shepherd-root)
99 ;; Extending the root shepherd service (aka. PID 1) happens by
100 ;; concatenating the list of services provided by the extensions.
0adfe95a
LC
101 (compose concatenate)
102 (extend append)
d4053c71
AK
103 (extensions (list (service-extension boot-service-type
104 shepherd-boot-gexp)
c273d81b 105 (service-extension profile-service-type
34044d55 106 (const (list shepherd)))))))
0adfe95a 107
d4053c71
AK
108(define %shepherd-root-service
109 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
110 ;; <shepherd-service> objects.
111 (service shepherd-root-service-type '()))
0adfe95a 112
88cd7bbd
LC
113(define-syntax shepherd-service-type
114 (syntax-rules ()
115 "Return a <service-type> denoting a simple shepherd service--i.e., the type
116for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else. When
117DEFAULT is given, use it as the service's default value."
118 ((_ service-name proc default)
119 (service-type
120 (name service-name)
121 (extensions
122 (list (service-extension shepherd-root-service-type
123 (compose list proc))))
124 (default-value default)))
125 ((_ service-name proc)
126 (service-type
127 (name service-name)
128 (extensions
129 (list (service-extension shepherd-root-service-type
130 (compose list proc))))))))
0adfe95a 131
fae685b9
LC
132(define %default-imported-modules
133 ;; Default set of modules imported for a service's consumption.
134 '((guix build utils)
479b417b 135 (guix build syscalls)))
fae685b9
LC
136
137(define %default-modules
138 ;; Default set of modules visible in a service's file.
34044d55 139 `((shepherd service)
fae685b9 140 (oop goops)
fae685b9 141 (guix build utils)
479b417b 142 (guix build syscalls)))
fae685b9 143
d4053c71
AK
144(define-record-type* <shepherd-service>
145 shepherd-service make-shepherd-service
146 shepherd-service?
147 (documentation shepherd-service-documentation ;string
0adfe95a 148 (default "[No documentation.]"))
d4053c71
AK
149 (provision shepherd-service-provision) ;list of symbols
150 (requirement shepherd-service-requirement ;list of symbols
0adfe95a 151 (default '()))
95ef8b85
LC
152 (one-shot? shepherd-service-one-shot? ;Boolean
153 (default #f))
d4053c71 154 (respawn? shepherd-service-respawn? ;Boolean
0adfe95a 155 (default #t))
d4053c71
AK
156 (start shepherd-service-start) ;g-expression (procedure)
157 (stop shepherd-service-stop ;g-expression (procedure)
0adfe95a 158 (default #~(const #f)))
70138308
LC
159 (actions shepherd-service-actions ;list of <shepherd-action>
160 (default '()))
d4053c71 161 (auto-start? shepherd-service-auto-start? ;Boolean
fae685b9 162 (default #t))
d4053c71 163 (modules shepherd-service-modules ;list of module names
a91c3fc7 164 (default %default-modules)))
0adfe95a 165
70138308
LC
166(define-record-type* <shepherd-action>
167 shepherd-action make-shepherd-action
168 shepherd-action?
169 (name shepherd-action-name) ;symbol
170 (procedure shepherd-action-procedure) ;gexp
171 (documentation shepherd-action-documentation)) ;string
172
240b57f0
LC
173(define (shepherd-service-canonical-name service)
174 "Return the 'canonical name' of SERVICE."
175 (first (shepherd-service-provision service)))
0adfe95a 176
2d2651e7 177(define (assert-valid-graph services)
d4053c71
AK
178 "Raise an error if SERVICES does not define a valid shepherd service graph,
179for instance if a service requires a nonexistent service, or if more than one
2d2651e7 180service uses a given name.
116244df 181
d4053c71
AK
182These are constraints that shepherd's 'register-service' verifies but we'd
183better verify them here statically than wait until PID 1 halts with an
184assertion failure."
2d2651e7
LC
185 (define provisions
186 ;; The set of provisions (symbols). Bail out if a symbol is given more
187 ;; than once.
188 (fold (lambda (service set)
189 (define (assert-unique symbol)
190 (when (set-contains? set symbol)
191 (raise (condition
192 (&message
193 (message
69daee23 194 (format #f (G_ "service '~a' provided more than once")
2d2651e7
LC
195 symbol)))))))
196
d4053c71
AK
197 (for-each assert-unique (shepherd-service-provision service))
198 (fold set-insert set (shepherd-service-provision service)))
199 (setq 'shepherd)
2d2651e7
LC
200 services))
201
202 (define (assert-satisfied-requirements service)
203 ;; Bail out if the requirements of SERVICE aren't satisfied.
204 (for-each (lambda (requirement)
205 (unless (set-contains? provisions requirement)
206 (raise (condition
207 (&message
208 (message
69daee23 209 (format #f (G_ "service '~a' requires '~a', \
2c2ec261 210which is not provided by any service")
d4053c71 211 (match (shepherd-service-provision service)
2d2651e7
LC
212 ((head . _) head)
213 (_ service))
214 requirement)))))))
d4053c71 215 (shepherd-service-requirement service)))
2d2651e7
LC
216
217 (for-each assert-satisfied-requirements services))
116244df 218
d4053c71 219(define (shepherd-service-file-name service)
fae685b9
LC
220 "Return the file name where the initialization code for SERVICE is to be
221stored."
222 (let ((provisions (string-join (map symbol->string
d4053c71
AK
223 (shepherd-service-provision service)))))
224 (string-append "shepherd-"
fae685b9
LC
225 (string-map (match-lambda
226 (#\/ #\-)
750a4239 227 (#\ #\-)
fae685b9
LC
228 (chr chr))
229 provisions)
230 ".scm")))
231
d4053c71 232(define (shepherd-service-file service)
fae685b9 233 "Return a file defining SERVICE."
33033a62
LC
234 (scheme-file (shepherd-service-file-name service)
235 (with-imported-modules %default-imported-modules
236 #~(begin
237 (use-modules #$@(shepherd-service-modules service))
238
239 (make <service>
240 #:docstring '#$(shepherd-service-documentation service)
241 #:provides '#$(shepherd-service-provision service)
242 #:requires '#$(shepherd-service-requirement service)
95ef8b85
LC
243
244 ;; The 'one-shot?' slot is new in Shepherd 0.6.0.
245 ;; Older versions ignore it.
246 #:one-shot? '#$(shepherd-service-one-shot? service)
247
33033a62
LC
248 #:respawn? '#$(shepherd-service-respawn? service)
249 #:start #$(shepherd-service-start service)
70138308
LC
250 #:stop #$(shepherd-service-stop service)
251 #:actions
252 (make-actions
253 #$@(map (match-lambda
254 (($ <shepherd-action> name proc doc)
255 #~(#$name #$doc #$proc)))
256 (shepherd-service-actions service))))))))
fae685b9 257
63b0ce39
LC
258(define (scm->go file)
259 "Compile FILE, which contains code to be loaded by shepherd's config file,
260and return the resulting '.go' file."
261 (with-extensions (list shepherd)
262 (computed-file (string-append (basename (scheme-file-name file) ".scm")
263 ".go")
264 #~(begin
265 (use-modules (system base compile))
266
267 ;; Do the same as the Shepherd's 'load-in-user-module'.
268 (let ((env (make-fresh-user-module)))
269 (module-use! env (resolve-interface '(oop goops)))
270 (module-use! env (resolve-interface '(shepherd service)))
271 (compile-file #$file #:output-file #$output
0e833ac5
LC
272 #:env env)))
273
274 ;; It's faster to build locally than to download.
275 #:options '(#:local-build? #t
276 #:substitutable? #f))))
63b0ce39 277
fe1ad5f5
AK
278(define (shepherd-configuration-file services)
279 "Return the shepherd configuration file for SERVICES."
2d2651e7 280 (assert-valid-graph services)
116244df 281
33033a62 282 (let ((files (map shepherd-service-file services)))
23ed63a1
LC
283 (define config
284 #~(begin
081bd3bd
LC
285 (use-modules (srfi srfi-34)
286 (system repl error-handling))
b9c7ed71 287
8b9cad01
LC
288 ;; Specify the default environment visible to all the services.
289 ;; Without this statement, all the environment variables of PID 1
290 ;; are inherited by child services.
291 (default-environment-variables
292 '("PATH=/run/current-system/profile/bin"))
293
8aa752ba
LC
294 ;; Booting off a DVD, especially on a slow machine, can make
295 ;; everything slow. Thus, increase the timeout compared to the
296 ;; default 5s in the Shepherd 0.7.0. See
297 ;; <https://bugs.gnu.org/40572>.
298 ;; XXX: Use something better when the next Shepherd is out.
299 (set! (@@ (shepherd service) %pid-file-timeout) 30)
300
234ea8a7
LC
301 ;; Arrange to spawn a REPL if something goes wrong. This is better
302 ;; than a kernel panic.
b9c7ed71
LC
303 (call-with-error-handling
304 (lambda ()
63b0ce39
LC
305 (apply register-services
306 (map load-compiled '#$(map scm->go files)))))
307
63b0ce39
LC
308 (format #t "starting services...~%")
309 (for-each (lambda (service)
310 ;; In the Shepherd 0.3 the 'start' method can raise
311 ;; '&action-runtime-error' if it fails, so protect
312 ;; against it. (XXX: 'action-runtime-error?' is not
313 ;; exported is 0.3, hence 'service-error?'.)
314 (guard (c ((service-error? c)
315 (format (current-error-port)
316 "failed to start service '~a'~%"
317 service)))
318 (start service)))
319 '#$(append-map shepherd-service-provision
320 (filter shepherd-service-auto-start?
321 services)))
322
323 ;; Hang up stdin. At this point, we assume that 'start' methods
324 ;; that required user interaction on the console (e.g.,
325 ;; 'cryptsetup open' invocations, post-fsck emergency REPL) have
326 ;; completed. User interaction becomes impossible after this
327 ;; call; this avoids situations where services wrongfully lead
328 ;; PID 1 to read from stdin (the console), which users may not
329 ;; have access to (see <https://bugs.gnu.org/23697>).
330 (redirect-port (open-input-file "/dev/null")
331 (current-input-port))))
23ed63a1 332
33033a62 333 (scheme-file "shepherd.conf" config)))
db4fdc04 334
a5d78eb6
LC
335(define* (shepherd-service-lookup-procedure services
336 #:optional
337 (provision
338 shepherd-service-provision))
339 "Return a procedure that, when passed a symbol, return the item among
340SERVICES that provides this symbol. PROVISION must be a one-argument
341procedure that takes a service and returns the list of symbols it provides."
342 (let ((services (fold (lambda (service result)
343 (fold (cut vhash-consq <> service <>)
344 result
345 (provision service)))
346 vlist-null
347 services)))
348 (lambda (name)
349 (match (vhash-assq name services)
350 ((_ . service) service)
351 (#f #f)))))
352
6673bddc
LC
353(define* (shepherd-service-back-edges services
354 #:key
355 (provision shepherd-service-provision)
356 (requirement shepherd-service-requirement))
d4053c71 357 "Return a procedure that, when given a <shepherd-service> from SERVICES,
6673bddc
LC
358returns the list of <shepherd-service> that depend on it.
359
360Use PROVISION and REQUIREMENT as one-argument procedures that return the
361symbols provided/required by a service."
80a67734 362 (define provision->service
6673bddc 363 (shepherd-service-lookup-procedure services provision))
80a67734
LC
364
365 (define edges
366 (fold (lambda (service edges)
367 (fold (lambda (requirement edges)
368 (vhash-consq (provision->service requirement) service
369 edges))
370 edges
6673bddc 371 (requirement service)))
80a67734
LC
372 vlist-null
373 services))
374
375 (lambda (service)
376 (vhash-foldq* cons '() service edges)))
377
7b44cae5
LC
378(define (shepherd-service-upgrade live target)
379 "Return two values: the subset of LIVE (a list of <live-service>) that needs
380to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
4245ddcb 381need to be restarted to complete their upgrade."
7b44cae5
LC
382 (define (essential? service)
383 (memq (first (live-service-provision service))
384 '(root shepherd)))
385
386 (define lookup-target
387 (shepherd-service-lookup-procedure target
388 shepherd-service-provision))
389
390 (define lookup-live
391 (shepherd-service-lookup-procedure live
392 live-service-provision))
393
394 (define (running? service)
395 (and=> (lookup-live (shepherd-service-canonical-name service))
396 live-service-running))
397
7b44cae5
LC
398 (define live-service-dependents
399 (shepherd-service-back-edges live
400 #:provision live-service-provision
401 #:requirement live-service-requirement))
402
403 (define (obsolete? service)
404 (match (lookup-target (first (live-service-provision service)))
405 (#f (every obsolete? (live-service-dependents service)))
406 (_ #f)))
407
4245ddcb
CZ
408 (define to-restart
409 ;; Restart services that are currently running.
410 (filter running? target))
7b44cae5
LC
411
412 (define to-unload
4245ddcb
CZ
413 ;; Unload services that are no longer required.
414 (remove essential? (filter obsolete? live)))
7b44cae5 415
4245ddcb 416 (values to-unload to-restart))
7b44cae5 417
0190c1c0 418;;; shepherd.scm ends here