gnu: Fix typo in description of xfce-desktop-service.
[jackhill/guix/guix.git] / gnu / services / shepherd.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
c273d81b 2;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
db4fdc04
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
0190c1c0 19(define-module (gnu services shepherd)
116244df
LC
20 #:use-module (guix ui)
21 #:use-module (guix sets)
b5f4e686 22 #:use-module (guix gexp)
e87f0591 23 #:use-module (guix store)
db4fdc04 24 #:use-module (guix monads)
0adfe95a 25 #:use-module (guix records)
e87f0591 26 #:use-module (guix derivations) ;imported-modules, etc.
db4fdc04 27 #:use-module (gnu services)
7b44cae5 28 #:use-module (gnu services herd)
0adfe95a 29 #:use-module (gnu packages admin)
db4fdc04 30 #:use-module (ice-9 match)
80a67734 31 #:use-module (ice-9 vlist)
db4fdc04 32 #:use-module (srfi srfi-1)
80a67734 33 #:use-module (srfi srfi-26)
116244df
LC
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-35)
d4053c71
AK
36 #:export (shepherd-root-service-type
37 %shepherd-root-service
38 shepherd-service-type
39
40 shepherd-service
41 shepherd-service?
42 shepherd-service-documentation
43 shepherd-service-provision
240b57f0 44 shepherd-service-canonical-name
d4053c71
AK
45 shepherd-service-requirement
46 shepherd-service-respawn?
47 shepherd-service-start
48 shepherd-service-stop
49 shepherd-service-auto-start?
50 shepherd-service-modules
fae685b9 51
fae685b9 52 %default-modules
80a67734 53
240b57f0
LC
54 shepherd-service-file
55
a5d78eb6 56 shepherd-service-lookup-procedure
7b44cae5
LC
57 shepherd-service-back-edges
58 shepherd-service-upgrade))
db4fdc04
LC
59
60;;; Commentary:
61;;;
fe1ad5f5 62;;; Instantiating system services as a shepherd configuration file.
db4fdc04
LC
63;;;
64;;; Code:
65
0adfe95a 66
d4053c71 67(define (shepherd-boot-gexp services)
fe1ad5f5 68 (mlet %store-monad ((shepherd-conf (shepherd-configuration-file services)))
0adfe95a
LC
69 (return #~(begin
70 ;; Keep track of the booted system.
71 (false-if-exception (delete-file "/run/booted-system"))
72 (symlink (readlink "/run/current-system")
73 "/run/booted-system")
74
75 ;; Close any remaining open file descriptors to be on the safe
76 ;; side. This must be the very last thing we do, because
77 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
78 ;; alive.
79 (let loop ((fd 3))
80 (when (< fd 1024)
81 (false-if-exception (close-fdes fd))
82 (loop (+ 1 fd))))
83
34044d55
AK
84 ;; Start shepherd.
85 (execl (string-append #$shepherd "/bin/shepherd")
fe1ad5f5 86 "shepherd" "--config" #$shepherd-conf)))))
0adfe95a 87
d4053c71 88(define shepherd-root-service-type
0adfe95a 89 (service-type
d4053c71
AK
90 (name 'shepherd-root)
91 ;; Extending the root shepherd service (aka. PID 1) happens by
92 ;; concatenating the list of services provided by the extensions.
0adfe95a
LC
93 (compose concatenate)
94 (extend append)
d4053c71
AK
95 (extensions (list (service-extension boot-service-type
96 shepherd-boot-gexp)
c273d81b 97 (service-extension profile-service-type
34044d55 98 (const (list shepherd)))))))
0adfe95a 99
d4053c71
AK
100(define %shepherd-root-service
101 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
102 ;; <shepherd-service> objects.
103 (service shepherd-root-service-type '()))
0adfe95a 104
d4053c71
AK
105(define-syntax-rule (shepherd-service-type service-name proc)
106 "Return a <service-type> denoting a simple shepherd service--i.e., the type
107for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else."
0adfe95a 108 (service-type
00184239 109 (name service-name)
0adfe95a 110 (extensions
d4053c71 111 (list (service-extension shepherd-root-service-type
0adfe95a
LC
112 (compose list proc))))))
113
fae685b9
LC
114(define %default-imported-modules
115 ;; Default set of modules imported for a service's consumption.
116 '((guix build utils)
479b417b 117 (guix build syscalls)))
fae685b9
LC
118
119(define %default-modules
120 ;; Default set of modules visible in a service's file.
34044d55 121 `((shepherd service)
fae685b9 122 (oop goops)
fae685b9 123 (guix build utils)
479b417b 124 (guix build syscalls)))
fae685b9 125
d4053c71
AK
126(define-record-type* <shepherd-service>
127 shepherd-service make-shepherd-service
128 shepherd-service?
129 (documentation shepherd-service-documentation ;string
0adfe95a 130 (default "[No documentation.]"))
d4053c71
AK
131 (provision shepherd-service-provision) ;list of symbols
132 (requirement shepherd-service-requirement ;list of symbols
0adfe95a 133 (default '()))
d4053c71 134 (respawn? shepherd-service-respawn? ;Boolean
0adfe95a 135 (default #t))
d4053c71
AK
136 (start shepherd-service-start) ;g-expression (procedure)
137 (stop shepherd-service-stop ;g-expression (procedure)
0adfe95a 138 (default #~(const #f)))
d4053c71 139 (auto-start? shepherd-service-auto-start? ;Boolean
fae685b9 140 (default #t))
d4053c71 141 (modules shepherd-service-modules ;list of module names
a91c3fc7 142 (default %default-modules)))
0adfe95a 143
240b57f0
LC
144(define (shepherd-service-canonical-name service)
145 "Return the 'canonical name' of SERVICE."
146 (first (shepherd-service-provision service)))
0adfe95a 147
2d2651e7 148(define (assert-valid-graph services)
d4053c71
AK
149 "Raise an error if SERVICES does not define a valid shepherd service graph,
150for instance if a service requires a nonexistent service, or if more than one
2d2651e7 151service uses a given name.
116244df 152
d4053c71
AK
153These are constraints that shepherd's 'register-service' verifies but we'd
154better verify them here statically than wait until PID 1 halts with an
155assertion failure."
2d2651e7
LC
156 (define provisions
157 ;; The set of provisions (symbols). Bail out if a symbol is given more
158 ;; than once.
159 (fold (lambda (service set)
160 (define (assert-unique symbol)
161 (when (set-contains? set symbol)
162 (raise (condition
163 (&message
164 (message
165 (format #f (_ "service '~a' provided more than once")
166 symbol)))))))
167
d4053c71
AK
168 (for-each assert-unique (shepherd-service-provision service))
169 (fold set-insert set (shepherd-service-provision service)))
170 (setq 'shepherd)
2d2651e7
LC
171 services))
172
173 (define (assert-satisfied-requirements service)
174 ;; Bail out if the requirements of SERVICE aren't satisfied.
175 (for-each (lambda (requirement)
176 (unless (set-contains? provisions requirement)
177 (raise (condition
178 (&message
179 (message
180 (format #f (_ "service '~a' requires '~a', \
2c2ec261 181which is not provided by any service")
d4053c71 182 (match (shepherd-service-provision service)
2d2651e7
LC
183 ((head . _) head)
184 (_ service))
185 requirement)))))))
d4053c71 186 (shepherd-service-requirement service)))
2d2651e7
LC
187
188 (for-each assert-satisfied-requirements services))
116244df 189
d4053c71 190(define (shepherd-service-file-name service)
fae685b9
LC
191 "Return the file name where the initialization code for SERVICE is to be
192stored."
193 (let ((provisions (string-join (map symbol->string
d4053c71
AK
194 (shepherd-service-provision service)))))
195 (string-append "shepherd-"
fae685b9
LC
196 (string-map (match-lambda
197 (#\/ #\-)
198 (chr chr))
199 provisions)
200 ".scm")))
201
d4053c71 202(define (shepherd-service-file service)
fae685b9 203 "Return a file defining SERVICE."
d4053c71 204 (gexp->file (shepherd-service-file-name service)
a91c3fc7
LC
205 (with-imported-modules %default-imported-modules
206 #~(begin
207 (use-modules #$@(shepherd-service-modules service))
208
209 (make <service>
210 #:docstring '#$(shepherd-service-documentation service)
211 #:provides '#$(shepherd-service-provision service)
212 #:requires '#$(shepherd-service-requirement service)
213 #:respawn? '#$(shepherd-service-respawn? service)
214 #:start #$(shepherd-service-start service)
215 #:stop #$(shepherd-service-stop service))))))
fae685b9 216
fe1ad5f5
AK
217(define (shepherd-configuration-file services)
218 "Return the shepherd configuration file for SERVICES."
2d2651e7 219 (assert-valid-graph services)
116244df 220
a91c3fc7
LC
221 (mlet %store-monad ((files (mapm %store-monad
222 shepherd-service-file services)))
23ed63a1
LC
223 (define config
224 #~(begin
081bd3bd
LC
225 (use-modules (srfi srfi-34)
226 (system repl error-handling))
b9c7ed71 227
234ea8a7
LC
228 ;; Arrange to spawn a REPL if something goes wrong. This is better
229 ;; than a kernel panic.
b9c7ed71
LC
230 (call-with-error-handling
231 (lambda ()
234ea8a7
LC
232 (apply register-services (map primitive-load '#$files))
233
234 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
235 ;; it.
236 (setenv "PATH" "/run/current-system/profile/bin")
237
238 (format #t "starting services...~%")
239 (for-each (lambda (service)
240 ;; In the Shepherd 0.3 the 'start' method can raise
241 ;; '&action-runtime-error' if it fails, so protect
242 ;; against it. (XXX: 'action-runtime-error?' is not
243 ;; exported is 0.3, hence 'service-error?'.)
244 (guard (c ((service-error? c)
245 (format (current-error-port)
246 "failed to start service '~a'~%"
247 service)))
248 (start service)))
249 '#$(append-map shepherd-service-provision
250 (filter shepherd-service-auto-start?
251 services)))))))
23ed63a1 252
fe1ad5f5 253 (gexp->file "shepherd.conf" config)))
db4fdc04 254
a5d78eb6
LC
255(define* (shepherd-service-lookup-procedure services
256 #:optional
257 (provision
258 shepherd-service-provision))
259 "Return a procedure that, when passed a symbol, return the item among
260SERVICES that provides this symbol. PROVISION must be a one-argument
261procedure that takes a service and returns the list of symbols it provides."
262 (let ((services (fold (lambda (service result)
263 (fold (cut vhash-consq <> service <>)
264 result
265 (provision service)))
266 vlist-null
267 services)))
268 (lambda (name)
269 (match (vhash-assq name services)
270 ((_ . service) service)
271 (#f #f)))))
272
6673bddc
LC
273(define* (shepherd-service-back-edges services
274 #:key
275 (provision shepherd-service-provision)
276 (requirement shepherd-service-requirement))
d4053c71 277 "Return a procedure that, when given a <shepherd-service> from SERVICES,
6673bddc
LC
278returns the list of <shepherd-service> that depend on it.
279
280Use PROVISION and REQUIREMENT as one-argument procedures that return the
281symbols provided/required by a service."
80a67734 282 (define provision->service
6673bddc 283 (shepherd-service-lookup-procedure services provision))
80a67734
LC
284
285 (define edges
286 (fold (lambda (service edges)
287 (fold (lambda (requirement edges)
288 (vhash-consq (provision->service requirement) service
289 edges))
290 edges
6673bddc 291 (requirement service)))
80a67734
LC
292 vlist-null
293 services))
294
295 (lambda (service)
296 (vhash-foldq* cons '() service edges)))
297
7b44cae5
LC
298(define (shepherd-service-upgrade live target)
299 "Return two values: the subset of LIVE (a list of <live-service>) that needs
300to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
301needs to be loaded."
302 (define (essential? service)
303 (memq (first (live-service-provision service))
304 '(root shepherd)))
305
306 (define lookup-target
307 (shepherd-service-lookup-procedure target
308 shepherd-service-provision))
309
310 (define lookup-live
311 (shepherd-service-lookup-procedure live
312 live-service-provision))
313
314 (define (running? service)
315 (and=> (lookup-live (shepherd-service-canonical-name service))
316 live-service-running))
317
318 (define (stopped service)
319 (match (lookup-live (shepherd-service-canonical-name service))
320 (#f #f)
321 (service (and (not (live-service-running service))
322 service))))
323
324 (define live-service-dependents
325 (shepherd-service-back-edges live
326 #:provision live-service-provision
327 #:requirement live-service-requirement))
328
329 (define (obsolete? service)
330 (match (lookup-target (first (live-service-provision service)))
331 (#f (every obsolete? (live-service-dependents service)))
332 (_ #f)))
333
334 (define to-load
335 ;; Only load services that are either new or currently stopped.
336 (remove running? target))
337
338 (define to-unload
339 ;; Unload services that are (1) no longer required, or (2) are in TO-LOAD.
340 (remove essential?
341 (append (filter obsolete? live)
342 (filter-map stopped to-load))))
343
344 (values to-unload to-load))
345
0190c1c0 346;;; shepherd.scm ends here