1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services shepherd)
21 #:use-module (guix ui)
22 #:use-module (guix sets)
23 #:use-module (guix gexp)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix records)
27 #:use-module (guix derivations) ;imported-modules, etc.
28 #:use-module (gnu services)
29 #:use-module (gnu services herd)
30 #:use-module (gnu packages admin)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 vlist)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:export (shepherd-root-service-type
38 %shepherd-root-service
43 shepherd-service-documentation
44 shepherd-service-provision
45 shepherd-service-canonical-name
46 shepherd-service-requirement
47 shepherd-service-respawn?
48 shepherd-service-start
50 shepherd-service-auto-start?
51 shepherd-service-modules
57 shepherd-service-lookup-procedure
58 shepherd-service-back-edges
59 shepherd-service-upgrade))
63 ;;; Instantiating system services as a shepherd configuration file.
68 (define (shepherd-boot-gexp services)
69 (mlet %store-monad ((shepherd-conf (shepherd-configuration-file services)))
71 ;; Keep track of the booted system.
72 (false-if-exception (delete-file "/run/booted-system"))
73 (symlink (readlink "/run/current-system")
76 ;; Close any remaining open file descriptors to be on the safe
77 ;; side. This must be the very last thing we do, because
78 ;; Guile has internal FDs such as 'sleep_pipe' that need to be
82 (false-if-exception (close-fdes fd))
86 (execl #$(file-append shepherd "/bin/shepherd")
87 "shepherd" "--config" #$shepherd-conf)))))
89 (define shepherd-root-service-type
92 ;; Extending the root shepherd service (aka. PID 1) happens by
93 ;; concatenating the list of services provided by the extensions.
96 (extensions (list (service-extension boot-service-type
98 (service-extension profile-service-type
99 (const (list shepherd)))))))
101 (define %shepherd-root-service
102 ;; The root shepherd service, aka. PID 1. Its parameter is a list of
103 ;; <shepherd-service> objects.
104 (service shepherd-root-service-type '()))
106 (define-syntax-rule (shepherd-service-type service-name proc)
107 "Return a <service-type> denoting a simple shepherd service--i.e., the type
108 for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else."
112 (list (service-extension shepherd-root-service-type
113 (compose list proc))))))
115 (define %default-imported-modules
116 ;; Default set of modules imported for a service's consumption.
118 (guix build syscalls)))
120 (define %default-modules
121 ;; Default set of modules visible in a service's file.
125 (guix build syscalls)))
127 (define-record-type* <shepherd-service>
128 shepherd-service make-shepherd-service
130 (documentation shepherd-service-documentation ;string
131 (default "[No documentation.]"))
132 (provision shepherd-service-provision) ;list of symbols
133 (requirement shepherd-service-requirement ;list of symbols
135 (respawn? shepherd-service-respawn? ;Boolean
137 (start shepherd-service-start) ;g-expression (procedure)
138 (stop shepherd-service-stop ;g-expression (procedure)
139 (default #~(const #f)))
140 (auto-start? shepherd-service-auto-start? ;Boolean
142 (modules shepherd-service-modules ;list of module names
143 (default %default-modules)))
145 (define (shepherd-service-canonical-name service)
146 "Return the 'canonical name' of SERVICE."
147 (first (shepherd-service-provision service)))
149 (define (assert-valid-graph services)
150 "Raise an error if SERVICES does not define a valid shepherd service graph,
151 for instance if a service requires a nonexistent service, or if more than one
152 service uses a given name.
154 These are constraints that shepherd's 'register-service' verifies but we'd
155 better verify them here statically than wait until PID 1 halts with an
158 ;; The set of provisions (symbols). Bail out if a symbol is given more
160 (fold (lambda (service set)
161 (define (assert-unique symbol)
162 (when (set-contains? set symbol)
166 (format #f (G_ "service '~a' provided more than once")
169 (for-each assert-unique (shepherd-service-provision service))
170 (fold set-insert set (shepherd-service-provision service)))
174 (define (assert-satisfied-requirements service)
175 ;; Bail out if the requirements of SERVICE aren't satisfied.
176 (for-each (lambda (requirement)
177 (unless (set-contains? provisions requirement)
181 (format #f (G_ "service '~a' requires '~a', \
182 which is not provided by any service")
183 (match (shepherd-service-provision service)
187 (shepherd-service-requirement service)))
189 (for-each assert-satisfied-requirements services))
191 (define (shepherd-service-file-name service)
192 "Return the file name where the initialization code for SERVICE is to be
194 (let ((provisions (string-join (map symbol->string
195 (shepherd-service-provision service)))))
196 (string-append "shepherd-"
197 (string-map (match-lambda
204 (define (shepherd-service-file service)
205 "Return a file defining SERVICE."
206 (gexp->file (shepherd-service-file-name service)
207 (with-imported-modules %default-imported-modules
209 (use-modules #$@(shepherd-service-modules service))
212 #:docstring '#$(shepherd-service-documentation service)
213 #:provides '#$(shepherd-service-provision service)
214 #:requires '#$(shepherd-service-requirement service)
215 #:respawn? '#$(shepherd-service-respawn? service)
216 #:start #$(shepherd-service-start service)
217 #:stop #$(shepherd-service-stop service))))))
219 (define (shepherd-configuration-file services)
220 "Return the shepherd configuration file for SERVICES."
221 (assert-valid-graph services)
223 (mlet %store-monad ((files (mapm %store-monad
224 shepherd-service-file services)))
227 (use-modules (srfi srfi-34)
228 (system repl error-handling))
230 ;; Arrange to spawn a REPL if something goes wrong. This is better
231 ;; than a kernel panic.
232 (call-with-error-handling
234 (apply register-services (map primitive-load '#$files))
236 ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around
238 (setenv "PATH" "/run/current-system/profile/bin")
240 (format #t "starting services...~%")
241 (for-each (lambda (service)
242 ;; In the Shepherd 0.3 the 'start' method can raise
243 ;; '&action-runtime-error' if it fails, so protect
244 ;; against it. (XXX: 'action-runtime-error?' is not
245 ;; exported is 0.3, hence 'service-error?'.)
246 (guard (c ((service-error? c)
247 (format (current-error-port)
248 "failed to start service '~a'~%"
251 '#$(append-map shepherd-service-provision
252 (filter shepherd-service-auto-start?
255 (gexp->file "shepherd.conf" config)))
257 (define* (shepherd-service-lookup-procedure services
260 shepherd-service-provision))
261 "Return a procedure that, when passed a symbol, return the item among
262 SERVICES that provides this symbol. PROVISION must be a one-argument
263 procedure that takes a service and returns the list of symbols it provides."
264 (let ((services (fold (lambda (service result)
265 (fold (cut vhash-consq <> service <>)
267 (provision service)))
271 (match (vhash-assq name services)
272 ((_ . service) service)
275 (define* (shepherd-service-back-edges services
277 (provision shepherd-service-provision)
278 (requirement shepherd-service-requirement))
279 "Return a procedure that, when given a <shepherd-service> from SERVICES,
280 returns the list of <shepherd-service> that depend on it.
282 Use PROVISION and REQUIREMENT as one-argument procedures that return the
283 symbols provided/required by a service."
284 (define provision->service
285 (shepherd-service-lookup-procedure services provision))
288 (fold (lambda (service edges)
289 (fold (lambda (requirement edges)
290 (vhash-consq (provision->service requirement) service
293 (requirement service)))
298 (vhash-foldq* cons '() service edges)))
300 (define (shepherd-service-upgrade live target)
301 "Return two values: the subset of LIVE (a list of <live-service>) that needs
302 to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that
304 (define (essential? service)
305 (memq (first (live-service-provision service))
308 (define lookup-target
309 (shepherd-service-lookup-procedure target
310 shepherd-service-provision))
313 (shepherd-service-lookup-procedure live
314 live-service-provision))
316 (define (running? service)
317 (and=> (lookup-live (shepherd-service-canonical-name service))
318 live-service-running))
320 (define (stopped service)
321 (match (lookup-live (shepherd-service-canonical-name service))
323 (service (and (not (live-service-running service))
326 (define live-service-dependents
327 (shepherd-service-back-edges live
328 #:provision live-service-provision
329 #:requirement live-service-requirement))
331 (define (obsolete? service)
332 (match (lookup-target (first (live-service-provision service)))
333 (#f (every obsolete? (live-service-dependents service)))
337 ;; Only load services that are either new or currently stopped.
338 (remove running? target))
341 ;; Unload services that are (1) no longer required, or (2) are in TO-LOAD.
343 (append (filter obsolete? live)
344 (filter-map stopped to-load))))
346 (values to-unload to-load))
348 ;;; shepherd.scm ends here