1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
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)
21 #:use-module (guix gexp)
22 #:use-module (guix monads)
23 #:use-module (guix store)
24 #:use-module (guix records)
25 #:use-module (guix profiles)
26 #:use-module (guix sets)
27 #:use-module (guix ui)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages bash)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-9)
32 #:use-module (srfi srfi-9 gnu)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-35)
36 #:use-module (ice-9 vlist)
37 #:use-module (ice-9 match)
38 #:export (service-extension
44 service-type-extensions
58 missing-target-service-error?
59 missing-target-service-error-service
60 missing-target-service-error-target-type
61 ambiguous-target-service-error?
62 ambiguous-target-service-error-service
63 ambiguous-target-service-error-target-type
68 activation-service-type
69 activation-service->script
70 %linux-bare-metal-service
73 setuid-program-service-type
82 file-union)) ;XXX: for lack of a better place
86 ;;; This module defines a broad notion of "service types" and "services."
88 ;;; A service type describe how its instances extend instances of other
89 ;;; service types. For instance, some services extend the instance of
90 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
91 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
92 ;;; <shepherd-service>.
94 ;;; When applicable, the service type defines how it can itself be extended,
95 ;;; by providing one procedure to compose extensions, and one procedure to
98 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
99 ;;; instance, which is the root of the service DAG. Its value is the
100 ;;; derivation that produces the 'system' directory as returned by
101 ;;; 'operating-system-derivation'.
103 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
104 ;;; "folds" by propagating extensions down the graph; it returns the root
105 ;;; service after the applying all its extensions.
109 (define-record-type <service-extension>
110 (service-extension target compute)
112 (target service-extension-target) ;<service-type>
113 (compute service-extension-compute)) ;params -> params
115 (define-record-type* <service-type> service-type make-service-type
117 (name service-type-name) ;symbol (for debugging)
119 ;; Things extended by services of this type.
120 (extensions service-type-extensions) ;list of <service-extensions>
122 ;; Given a list of extensions, "compose" them.
123 (compose service-type-compose ;list of Any -> Any
126 ;; Extend the services' own parameters with the extension composition.
127 (extend service-type-extend ;list of Any -> parameters
130 (define (write-service-type type port)
131 (format port "#<service-type ~a ~a>"
132 (service-type-name type)
133 (number->string (object-address type) 16)))
135 (set-record-type-printer! <service-type> write-service-type)
137 ;; Services of a given type.
138 (define-record-type <service>
139 (service type parameters)
142 (parameters service-parameters))
145 (define-syntax %modify-service
149 ((_ svc (kind param => exp ...) clauses ...)
150 (if (eq? (service-kind svc) kind)
151 (let ((param (service-parameters svc)))
152 (service (service-kind svc)
154 (%modify-service svc clauses ...)))))
156 (define-syntax modify-services
158 "Modify the services listed in SERVICES according to CLAUSES and return
159 the resulting list of services. Each clause must have the form:
161 (TYPE VARIABLE => BODY)
163 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
164 identifier that is bound within BODY to the value of the service of that
165 TYPE. Consider this example:
167 (modify-services %base-services
168 (guix-service-type config =>
171 (use-substitutes? #f)
172 (extra-options '(\"--gc-keep-derivations\"))))
173 (mingetty-service-type config =>
174 (mingetty-configuration
176 (motd (plain-file \"motd\" \"Hi there!\")))))
178 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
179 all the MINGETTY-SERVICE-TYPE instances.
181 This is a shorthand for (map (lambda (svc) ...) %base-services)."
182 ((_ services clauses ...)
183 (map (lambda (service)
184 (%modify-service service clauses ...))
192 (define (system-derivation mentries mextensions)
193 "Return as a monadic value the derivation of the 'system' directory
194 containing the given entries."
195 (mlet %store-monad ((entries mentries)
196 (extensions (sequence %store-monad mextensions)))
199 (append entries (concatenate extensions))))))
201 (define system-service-type
202 ;; This is the ultimate service type, the root of the service DAG. The
203 ;; service of this type is extended by monadic name/item pairs. These items
204 ;; end up in the "system directory" as returned by
205 ;; 'operating-system-derivation'.
206 (service-type (name 'system)
209 (extend system-derivation)))
211 (define (compute-boot-script _ mexps)
212 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
214 ;; Clean up and activate the system, then spawn shepherd.
215 #~(begin #$@gexps))))
217 (define (boot-script-entry mboot)
218 "Return, as a monadic value, an entry for the boot script in the system
220 (mlet %store-monad ((boot mboot))
221 (return `(("boot" ,boot)))))
223 (define boot-service-type
224 ;; The service of this type is extended by being passed gexps as monadic
225 ;; values. It aggregates them in a single script, as a monadic value, which
226 ;; becomes its 'parameters'. It is the only service that extends nothing.
227 (service-type (name 'boot)
229 (list (service-extension system-service-type
232 (extend compute-boot-script)))
234 (define %boot-service
235 ;; The service that produces the boot script.
236 (service boot-service-type #t))
238 (define (cleanup-gexp _)
239 "Return as a monadic value a gexp to clean up /tmp and similar places upon
241 (with-monad %store-monad
242 (with-imported-modules '((guix build utils))
244 (use-modules (guix build utils))
246 ;; Clean out /tmp and /var/run.
248 ;; XXX This needs to happen before service activations, so it
249 ;; has to be here, but this also implicitly assumes that /tmp
250 ;; and /var/run are on the root partition.
251 (letrec-syntax ((fail-safe (syntax-rules ()
257 (fail-safe rest ...)))
260 ;; Ignore I/O errors so the system can boot.
262 (delete-file-recursively "/tmp")
263 (delete-file-recursively "/var/run")
265 (chmod "/tmp" #o1777)
267 (chmod "/var/run" #o755))))))))
269 (define cleanup-service-type
270 ;; Service that cleans things up in /tmp and similar.
271 (service-type (name 'cleanup)
273 (list (service-extension boot-service-type
276 (define* (file-union name files) ;FIXME: Factorize.
277 "Return a <computed-file> that builds a directory containing all of FILES.
278 Each item in FILES must be a list where the first element is the file name to
279 use in the new directory, and the second element is a gexp denoting the target
285 #$@(map (match-lambda
288 ;; Stat the source to abort early if it
292 (symlink #$source #$target))))
295 (define (directory-union name things)
296 "Return a directory that is the union of THINGS."
299 ;; Only one thing; return it.
303 (with-imported-modules '((guix build union))
305 (use-modules (guix build union))
306 (union-build #$output '#$things)))))))
308 (define* (activation-service->script service)
309 "Return as a monadic value the activation script for SERVICE, a service of
310 ACTIVATION-SCRIPT-TYPE."
311 (activation-script (service-parameters service)))
313 (define (activation-script gexps)
314 "Return the system's activation script, which evaluates GEXPS."
316 '((gnu build activation)
317 (gnu build linux-boot)
318 (gnu build linux-modules)
319 (gnu build file-systems)
321 (guix build syscalls)
322 (guix build bournish)
325 (define (service-activations)
326 ;; Return the activation scripts for SERVICES.
328 (cut gexp->file "activate-service" <>)
331 (mlet* %store-monad ((actions (service-activations)))
332 (gexp->file "activate"
333 (with-imported-modules %modules
335 (use-modules (gnu build activation))
337 ;; Make sure /bin/sh is valid and current.
339 (string-append #$(canonical-package bash) "/bin/sh"))
341 ;; Run the services' activation snippets.
342 ;; TODO: Use 'load-compiled'.
343 (for-each primitive-load '#$actions)
345 ;; Set up /run/current-system.
346 (activate-current-system))))))
348 (define (gexps->activation-gexp gexps)
349 "Return a gexp that runs the activation script containing GEXPS."
350 (mlet %store-monad ((script (activation-script gexps)))
351 (return #~(primitive-load #$script))))
353 (define (second-argument a b) b)
355 (define activation-service-type
356 (service-type (name 'activate)
358 (list (service-extension boot-service-type
359 gexps->activation-gexp)))
361 (extend second-argument)))
363 (define %activation-service
364 ;; The activation service produces the activation script from the gexps it
366 (service activation-service-type #t))
368 (define %modprobe-wrapper
369 ;; Wrapper for the 'modprobe' command that knows where modules live.
371 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
372 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
373 ;; environment variable is not set---hence the need for this wrapper.
374 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
375 (program-file "modprobe"
377 (setenv "LINUX_MODULE_DIRECTORY"
378 "/run/booted-system/kernel/lib/modules")
379 (apply execl #$modprobe
380 (cons #$modprobe (cdr (command-line))))))))
382 (define %linux-kernel-activation
383 ;; Activation of the Linux kernel running on the bare metal (as opposed to
384 ;; running in a container.)
386 ;; Tell the kernel to use our 'modprobe' command.
387 (activate-modprobe #$%modprobe-wrapper)
389 ;; Let users debug their own processes!
390 (activate-ptrace-attach)))
392 (define linux-bare-metal-service-type
393 (service-type (name 'linux-bare-metal)
395 (list (service-extension activation-service-type
396 (const %linux-kernel-activation))))))
398 (define %linux-bare-metal-service
399 ;; The service that does things that are needed on the "bare metal", but not
400 ;; necessary or impossible in a container.
401 (service linux-bare-metal-service-type #f))
403 (define (etc-directory service)
404 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
405 (files->etc-directory (service-parameters service)))
407 (define (files->etc-directory files)
408 (file-union "etc" files))
410 (define (etc-entry files)
411 "Return an entry for the /etc directory consisting of FILES in the system
413 (with-monad %store-monad
414 (return `(("etc" ,(files->etc-directory files))))))
416 (define etc-service-type
417 (service-type (name 'etc)
420 (service-extension activation-service-type
423 (files->etc-directory files)))
424 #~(activate-etc #$etc))))
425 (service-extension system-service-type etc-entry)))
426 (compose concatenate)
429 (define (etc-service files)
430 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
431 FILES must be a list of name/file-like object pairs."
432 (service etc-service-type files))
434 (define setuid-program-service-type
435 (service-type (name 'setuid-program)
437 (list (service-extension activation-service-type
439 #~(activate-setuid-programs
440 (list #$@programs))))))
441 (compose concatenate)
444 (define (packages->profile-entry packages)
445 "Return a system entry for the profile containing PACKAGES."
446 (mlet %store-monad ((profile (profile-derivation
448 (delete-duplicates packages eq?)))))
449 (return `(("profile" ,profile)))))
451 (define profile-service-type
452 ;; The service that populates the system's profile---i.e.,
453 ;; /run/current-system/profile. It is extended by package lists.
454 (service-type (name 'profile)
456 (list (service-extension system-service-type
457 packages->profile-entry)))
458 (compose concatenate)
461 (define (firmware->activation-gexp firmware)
462 "Return a gexp to make the packages listed in FIRMWARE loadable by the
464 (let ((directory (directory-union "firmware" firmware)))
465 ;; Tell the kernel where firmware is.
466 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
468 (define firmware-service-type
469 ;; The service that collects firmware.
470 (service-type (name 'firmware)
472 (list (service-extension activation-service-type
473 firmware->activation-gexp)))
474 (compose concatenate)
477 (define (gc-roots->system-entry roots)
478 "Return an entry in the system's output containing symlinks to ROOTS."
479 (mlet %store-monad ((entry (gexp->derivation
481 #~(let ((roots '#$roots))
487 (iota (length roots))))))))
488 (return (if (null? roots)
490 `(("gc-roots" ,entry))))))
492 (define gc-root-service-type
493 ;; A service to associate extra garbage-collector roots to the system. This
494 ;; is a simple hack that guarantees that the system retains references to
495 ;; the given list of roots. Roots must be "lowerable" objects like
496 ;; packages, or derivations.
497 (service-type (name 'gc-roots)
499 (list (service-extension system-service-type
500 gc-roots->system-entry)))
501 (compose concatenate)
509 (define-condition-type &service-error &error
512 (define-condition-type &missing-target-service-error &service-error
513 missing-target-service-error?
514 (service missing-target-service-error-service)
515 (target-type missing-target-service-error-target-type))
517 (define-condition-type &ambiguous-target-service-error &service-error
518 ambiguous-target-service-error?
519 (service ambiguous-target-service-error-service)
520 (target-type ambiguous-target-service-error-target-type))
522 (define (service-back-edges services)
523 "Return a procedure that, when passed a <service>, returns the list of
524 <service> objects that depend on it."
525 (define (add-edges service edges)
526 (define (add-edge extension edges)
527 (let ((target-type (service-extension-target extension)))
528 (match (filter (lambda (service)
529 (eq? (service-kind service) target-type))
532 (vhash-consq target service edges))
535 (condition (&missing-target-service-error
537 (target-type target-type))
540 (format #f (_ "no target of type '~a' for service ~s")
541 (service-type-name target-type)
545 (condition (&ambiguous-target-service-error
547 (target-type target-type))
551 (_ "more than one target service of type '~a'")
552 (service-type-name target-type))))))))))
554 (fold add-edge edges (service-type-extensions (service-kind service))))
556 (let ((edges (fold add-edges vlist-null services)))
558 (reverse (vhash-foldq* cons '() node edges)))))
560 (define* (fold-services services
561 #:key (target-type system-service-type))
562 "Fold SERVICES by propagating their extensions down to the root of type
563 TARGET-TYPE; return the root service adjusted accordingly."
565 (service-back-edges services))
567 (define (matching-extension target)
568 (let ((target (service-kind target)))
570 (($ <service-extension> type)
571 (eq? type target)))))
573 (define (apply-extension target)
575 (match (find (matching-extension target)
576 (service-type-extensions (service-kind service)))
577 (($ <service-extension> _ compute)
578 (compute (service-parameters service))))))
580 (match (filter (lambda (service)
581 (eq? (service-kind service) target-type))
584 (let loop ((sink sink))
585 (let* ((dependents (map loop (dependents sink)))
586 (extensions (map (apply-extension sink) dependents))
587 (extend (service-type-extend (service-kind sink)))
588 (compose (service-type-compose (service-kind sink)))
589 (params (service-parameters sink)))
590 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
591 ;; different type than the elements of EXTENSIONS.
593 (service (service-kind sink)
594 (extend params (compose extensions)))
598 (condition (&missing-target-service-error
600 (target-type target-type))
602 (message (format #f (_ "service of type '~a' not found")
603 (service-type-name target-type)))))))
606 (condition (&ambiguous-target-service-error
608 (target-type target-type))
612 (_ "more than one target service of type '~a'")
613 (service-type-name target-type)))))))))
615 ;;; services.scm ends here.