1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu services)
20 #:use-module (guix gexp)
21 #:use-module (guix monads)
22 #:use-module (guix store)
23 #:use-module (guix records)
24 #:use-module (guix sets)
25 #:use-module (guix ui)
26 #:use-module (gnu packages base)
27 #:use-module (gnu packages bash)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 vlist)
35 #:use-module (ice-9 match)
36 #:export (service-extension
42 service-type-extensions
56 missing-target-service-error?
57 missing-target-service-error-service
58 missing-target-service-error-target-type
59 ambiguous-target-service-error?
60 ambiguous-target-service-error-service
61 ambiguous-target-service-error-target-type
64 activation-service-type
65 activation-service->script
66 %linux-bare-metal-service
69 setuid-program-service-type
76 file-union)) ;XXX: for lack of a better place
80 ;;; This module defines a broad notion of "service types" and "services."
82 ;;; A service type describe how its instances extend instances of other
83 ;;; service types. For instance, some services extend the instance of
84 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
85 ;;; others extend DMD-ROOT-SERVICE-TYPE by passing it instances of
88 ;;; When applicable, the service type defines how it can itself be extended,
89 ;;; by providing one procedure to compose extensions, and one procedure to
92 ;;; A notable service type is BOOT-SERVICE-TYPE, which has a single instance,
93 ;;; %BOOT-SERVICE. %BOOT-SERVICE constitutes the root of the service DAG. It
94 ;;; produces the boot script that the initrd loads.
96 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
97 ;;; "folds" by propagating extensions down the graph; it returns the root
98 ;;; service after the applying all its extensions.
102 (define-record-type <service-extension>
103 (service-extension target compute)
105 (target service-extension-target) ;<service-type>
106 (compute service-extension-compute)) ;params -> params
108 (define-record-type* <service-type> service-type make-service-type
110 (name service-type-name) ;symbol (for debugging)
112 ;; Things extended by services of this type.
113 (extensions service-type-extensions) ;list of <service-extensions>
115 ;; Given a list of extensions, "compose" them.
116 (compose service-type-compose ;list of Any -> Any
119 ;; Extend the services' own parameters with the extension composition.
120 (extend service-type-extend ;list of Any -> parameters
123 (define (write-service-type type port)
124 (format port "#<service-type ~a ~a>"
125 (service-type-name type)
126 (number->string (object-address type) 16)))
128 (set-record-type-printer! <service-type> write-service-type)
130 ;; Services of a given type.
131 (define-record-type <service>
132 (service type parameters)
135 (parameters service-parameters))
138 (define-syntax %modify-service
142 ((_ svc (kind param => exp ...) clauses ...)
143 (if (eq? (service-kind svc) kind)
144 (let ((param (service-parameters svc)))
145 (service (service-kind svc)
147 (%modify-service svc clauses ...)))))
149 (define-syntax modify-services
151 "Modify the services listed in SERVICES according to CLAUSES. Each clause
154 (TYPE VARIABLE => BODY)
156 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
157 identifier that is bound within BODY to the value of the service of that
158 TYPE. Consider this example:
160 (modify-services %base-services
161 (guix-service-type config =>
164 (use-substitutes? #f)
165 (extra-options '(\"--gc-keep-derivations\"))))
166 (mingetty-service-type config =>
167 (mingetty-configuration
169 (motd (plain-file \"motd\" \"Hi there!\")))))
171 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
172 all the MINGETTY-SERVICE-TYPE instances.
174 This is a shorthand for (map (lambda (svc) ...) %base-services)."
175 ((_ services clauses ...)
176 (map (lambda (service)
177 (%modify-service service clauses ...))
185 (define (compute-boot-script mexps)
186 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
189 (use-modules (guix build utils))
191 ;; Clean out /tmp and /var/run.
193 ;; XXX This needs to happen before service activations, so
194 ;; it has to be here, but this also implicitly assumes
195 ;; that /tmp and /var/run are on the root partition.
196 (false-if-exception (delete-file-recursively "/tmp"))
197 (false-if-exception (delete-file-recursively "/var/run"))
198 (false-if-exception (mkdir "/tmp"))
199 (false-if-exception (chmod "/tmp" #o1777))
200 (false-if-exception (mkdir "/var/run"))
201 (false-if-exception (chmod "/var/run" #o755))
203 ;; Activate the system and spawn dmd.
206 (define (second-argument a b) b)
208 (define boot-service-type
209 ;; The service of this type is extended by being passed gexps as monadic
210 ;; values. It aggregates them in a single script, as a monadic value, which
211 ;; becomes its 'parameters'. It is the only service that extends nothing.
212 (service-type (name 'boot)
214 (compose compute-boot-script)
215 (extend second-argument)))
217 (define %boot-service
218 ;; This is the ultimate service, the root of the service DAG.
219 (service boot-service-type #t))
221 (define* (file-union name files) ;FIXME: Factorize.
222 "Return a <computed-file> that builds a directory containing all of FILES.
223 Each item in FILES must be a list where the first element is the file name to
224 use in the new directory, and the second element is a gexp denoting the target
230 #$@(map (match-lambda
232 #~(symlink #$source #$target)))
235 (define (directory-union name things)
236 "Return a directory that is the union of THINGS."
239 ;; Only one thing; return it.
244 (use-modules (guix build union))
245 (union-build #$output '#$things))
246 #:modules '((guix build union))))))
248 (define* (activation-service->script service)
249 "Return as a monadic value the activation script for SERVICE, a service of
250 ACTIVATION-SCRIPT-TYPE."
251 (activation-script (service-parameters service)))
253 (define (activation-script gexps)
254 "Return the system's activation script, which evaluates GEXPS."
256 '((gnu build activation)
257 (gnu build linux-boot)
258 (gnu build linux-modules)
259 (gnu build file-systems)
261 (guix build syscalls)
264 (define (service-activations)
265 ;; Return the activation scripts for SERVICES.
267 (cut gexp->file "activate-service" <>)
270 (mlet* %store-monad ((actions (service-activations))
271 (modules (imported-modules %modules))
272 (compiled (compiled-modules %modules)))
273 (gexp->file "activate"
275 (eval-when (expand load eval)
276 ;; Make sure 'use-modules' below succeeds.
277 (set! %load-path (cons #$modules %load-path))
278 (set! %load-compiled-path
279 (cons #$compiled %load-compiled-path)))
281 (use-modules (gnu build activation))
283 ;; Make sure /bin/sh is valid and current.
285 (string-append #$(canonical-package bash) "/bin/sh"))
287 ;; Run the services' activation snippets.
288 ;; TODO: Use 'load-compiled'.
289 (for-each primitive-load '#$actions)
291 ;; Set up /run/current-system.
292 (activate-current-system)))))
294 (define (gexps->activation-gexp gexps)
295 "Return a gexp that runs the activation script containing GEXPS."
296 (mlet %store-monad ((script (activation-script gexps)))
297 (return #~(primitive-load #$script))))
299 (define activation-service-type
300 (service-type (name 'activate)
302 (list (service-extension boot-service-type
303 gexps->activation-gexp)))
305 (extend second-argument)))
307 (define %activation-service
308 ;; The activation service produces the activation script from the gexps it
310 (service activation-service-type #t))
312 (define %modprobe-wrapper
313 ;; Wrapper for the 'modprobe' command that knows where modules live.
315 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
316 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
317 ;; environment variable is not set---hence the need for this wrapper.
318 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
319 (program-file "modprobe"
321 (setenv "LINUX_MODULE_DIRECTORY"
322 "/run/booted-system/kernel/lib/modules")
323 (apply execl #$modprobe
324 (cons #$modprobe (cdr (command-line))))))))
326 (define %linux-kernel-activation
327 ;; Activation of the Linux kernel running on the bare metal (as opposed to
328 ;; running in a container.)
330 ;; Tell the kernel to use our 'modprobe' command.
331 (activate-modprobe #$%modprobe-wrapper)
333 ;; Let users debug their own processes!
334 (activate-ptrace-attach)))
336 (define linux-bare-metal-service-type
337 (service-type (name 'linux-bare-metal)
339 (list (service-extension activation-service-type
340 (const %linux-kernel-activation))))))
342 (define %linux-bare-metal-service
343 ;; The service that does things that are needed on the "bare metal", but not
344 ;; necessary or impossible in a container.
345 (service linux-bare-metal-service-type #f))
347 (define (etc-directory service)
348 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
349 (files->etc-directory (service-parameters service)))
351 (define (files->etc-directory files)
352 (file-union "etc" files))
354 (define etc-service-type
355 (service-type (name 'etc)
358 (service-extension activation-service-type
361 (files->etc-directory files)))
362 #~(activate-etc #$etc))))))
363 (compose concatenate)
366 (define (etc-service files)
367 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
368 FILES must be a list of name/file-like object pairs."
369 (service etc-service-type files))
371 (define setuid-program-service-type
372 (service-type (name 'setuid-program)
374 (list (service-extension activation-service-type
376 #~(activate-setuid-programs
377 (list #$@programs))))))
378 (compose concatenate)
381 (define (firmware->activation-gexp firmware)
382 "Return a gexp to make the packages listed in FIRMWARE loadable by the
384 (let ((directory (directory-union "firmware" firmware)))
385 ;; Tell the kernel where firmware is.
386 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
388 (define firmware-service-type
389 ;; The service that collects firmware.
390 (service-type (name 'firmware)
392 (list (service-extension activation-service-type
393 firmware->activation-gexp)))
394 (compose concatenate)
402 (define-condition-type &service-error &error
405 (define-condition-type &missing-target-service-error &service-error
406 missing-target-service-error?
407 (service missing-target-service-error-service)
408 (target-type missing-target-service-error-target-type))
410 (define-condition-type &ambiguous-target-service-error &service-error
411 ambiguous-target-service-error?
412 (service ambiguous-target-service-error-service)
413 (target-type ambiguous-target-service-error-target-type))
415 (define (service-back-edges services)
416 "Return a procedure that, when passed a <service>, returns the list of
417 <service> objects that depend on it."
418 (define (add-edges service edges)
419 (define (add-edge extension edges)
420 (let ((target-type (service-extension-target extension)))
421 (match (filter (lambda (service)
422 (eq? (service-kind service) target-type))
425 (vhash-consq target service edges))
428 (condition (&missing-target-service-error
430 (target-type target-type))
433 (format #f (_ "no target of type '~a' for service ~s")
434 (service-type-name target-type)
438 (condition (&ambiguous-target-service-error
440 (target-type target-type))
444 (_ "more than one target service of type '~a'")
445 (service-type-name target-type))))))))))
447 (fold add-edge edges (service-type-extensions (service-kind service))))
449 (let ((edges (fold add-edges vlist-null services)))
451 (reverse (vhash-foldq* cons '() node edges)))))
453 (define* (fold-services services #:key (target-type boot-service-type))
454 "Fold SERVICES by propagating their extensions down to the root of type
455 TARGET-TYPE; return the root service adjusted accordingly."
457 (service-back-edges services))
459 (define (matching-extension target)
460 (let ((target (service-kind target)))
462 (($ <service-extension> type)
463 (eq? type target)))))
465 (define (apply-extension target)
467 (match (find (matching-extension target)
468 (service-type-extensions (service-kind service)))
469 (($ <service-extension> _ compute)
470 (compute (service-parameters service))))))
472 (match (filter (lambda (service)
473 (eq? (service-kind service) target-type))
476 (let loop ((sink sink))
477 (let* ((dependents (map loop (dependents sink)))
478 (extensions (map (apply-extension sink) dependents))
479 (extend (service-type-extend (service-kind sink)))
480 (compose (service-type-compose (service-kind sink)))
481 (params (service-parameters sink)))
482 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
483 ;; different type than the elements of EXTENSIONS.
485 (service (service-kind sink)
486 (extend params (compose extensions)))
490 (condition (&missing-target-service-error
492 (target-type target-type))
494 (message (format #f (_ "service of type '~a' not found")
495 (service-type-name target-type)))))))
498 (condition (&ambiguous-target-service-error
500 (target-type target-type))
504 (_ "more than one target service of type '~a'")
505 (service-type-name target-type)))))))))
507 ;;; services.scm ends here.