1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 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 profiles)
25 #:use-module (guix sets)
26 #:use-module (guix ui)
27 #:use-module (gnu packages base)
28 #:use-module (gnu packages bash)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
31 #:use-module (srfi srfi-9 gnu)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 vlist)
36 #:use-module (ice-9 match)
37 #:export (service-extension
43 service-type-extensions
57 missing-target-service-error?
58 missing-target-service-error-service
59 missing-target-service-error-target-type
60 ambiguous-target-service-error?
61 ambiguous-target-service-error-service
62 ambiguous-target-service-error-target-type
67 activation-service-type
68 activation-service->script
69 %linux-bare-metal-service
72 setuid-program-service-type
80 file-union)) ;XXX: for lack of a better place
84 ;;; This module defines a broad notion of "service types" and "services."
86 ;;; A service type describe how its instances extend instances of other
87 ;;; service types. For instance, some services extend the instance of
88 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
89 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
90 ;;; <shepherd-service>.
92 ;;; When applicable, the service type defines how it can itself be extended,
93 ;;; by providing one procedure to compose extensions, and one procedure to
96 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
97 ;;; instance, which is the root of the service DAG. Its value is the
98 ;;; derivation that produces the 'system' directory as returned by
99 ;;; 'operating-system-derivation'.
101 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
102 ;;; "folds" by propagating extensions down the graph; it returns the root
103 ;;; service after the applying all its extensions.
107 (define-record-type <service-extension>
108 (service-extension target compute)
110 (target service-extension-target) ;<service-type>
111 (compute service-extension-compute)) ;params -> params
113 (define-record-type* <service-type> service-type make-service-type
115 (name service-type-name) ;symbol (for debugging)
117 ;; Things extended by services of this type.
118 (extensions service-type-extensions) ;list of <service-extensions>
120 ;; Given a list of extensions, "compose" them.
121 (compose service-type-compose ;list of Any -> Any
124 ;; Extend the services' own parameters with the extension composition.
125 (extend service-type-extend ;list of Any -> parameters
128 (define (write-service-type type port)
129 (format port "#<service-type ~a ~a>"
130 (service-type-name type)
131 (number->string (object-address type) 16)))
133 (set-record-type-printer! <service-type> write-service-type)
135 ;; Services of a given type.
136 (define-record-type <service>
137 (service type parameters)
140 (parameters service-parameters))
143 (define-syntax %modify-service
147 ((_ svc (kind param => exp ...) clauses ...)
148 (if (eq? (service-kind svc) kind)
149 (let ((param (service-parameters svc)))
150 (service (service-kind svc)
152 (%modify-service svc clauses ...)))))
154 (define-syntax modify-services
156 "Modify the services listed in SERVICES according to CLAUSES. Each clause
159 (TYPE VARIABLE => BODY)
161 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
162 identifier that is bound within BODY to the value of the service of that
163 TYPE. Consider this example:
165 (modify-services %base-services
166 (guix-service-type config =>
169 (use-substitutes? #f)
170 (extra-options '(\"--gc-keep-derivations\"))))
171 (mingetty-service-type config =>
172 (mingetty-configuration
174 (motd (plain-file \"motd\" \"Hi there!\")))))
176 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
177 all the MINGETTY-SERVICE-TYPE instances.
179 This is a shorthand for (map (lambda (svc) ...) %base-services)."
180 ((_ services clauses ...)
181 (map (lambda (service)
182 (%modify-service service clauses ...))
190 (define (system-derivation mentries mextensions)
191 "Return as a monadic value the derivation of the 'system' directory
192 containing the given entries."
193 (mlet %store-monad ((entries mentries)
194 (extensions (sequence %store-monad mextensions)))
197 (append entries (concatenate extensions))))))
199 (define system-service-type
200 ;; This is the ultimate service type, the root of the service DAG. The
201 ;; service of this type is extended by monadic name/item pairs. These items
202 ;; end up in the "system directory" as returned by
203 ;; 'operating-system-derivation'.
204 (service-type (name 'system)
207 (extend system-derivation)))
209 (define (compute-boot-script _ mexps)
210 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
212 ;; Clean up and activate the system, then spawn shepherd.
213 #~(begin #$@gexps))))
215 (define (boot-script-entry mboot)
216 "Return, as a monadic value, an entry for the boot script in the system
218 (mlet %store-monad ((boot mboot))
219 (return `(("boot" ,boot)))))
221 (define boot-service-type
222 ;; The service of this type is extended by being passed gexps as monadic
223 ;; values. It aggregates them in a single script, as a monadic value, which
224 ;; becomes its 'parameters'. It is the only service that extends nothing.
225 (service-type (name 'boot)
227 (list (service-extension system-service-type
230 (extend compute-boot-script)))
232 (define %boot-service
233 ;; The service that produces the boot script.
234 (service boot-service-type #t))
236 (define (cleanup-gexp _)
237 "Return as a monadic value a gexp to clean up /tmp and similar places upon
240 '((guix build utils)))
242 (mlet %store-monad ((modules (imported-modules %modules))
243 (compiled (compiled-modules %modules)))
245 (eval-when (expand load eval)
246 ;; Make sure 'use-modules' below succeeds.
247 (set! %load-path (cons #$modules %load-path))
248 (set! %load-compiled-path
249 (cons #$compiled %load-compiled-path)))
251 (use-modules (guix build utils))
253 ;; Clean out /tmp and /var/run.
255 ;; XXX This needs to happen before service activations, so it
256 ;; has to be here, but this also implicitly assumes that /tmp
257 ;; and /var/run are on the root partition.
258 (letrec-syntax ((fail-safe (syntax-rules ()
264 (fail-safe rest ...)))
267 ;; Ignore I/O errors so the system can boot.
269 (delete-file-recursively "/tmp")
270 (delete-file-recursively "/var/run")
272 (chmod "/tmp" #o1777)
274 (chmod "/var/run" #o755)))))))
276 (define cleanup-service-type
277 ;; Service that cleans things up in /tmp and similar.
278 (service-type (name 'cleanup)
280 (list (service-extension boot-service-type
283 (define* (file-union name files) ;FIXME: Factorize.
284 "Return a <computed-file> that builds a directory containing all of FILES.
285 Each item in FILES must be a list where the first element is the file name to
286 use in the new directory, and the second element is a gexp denoting the target
292 #$@(map (match-lambda
295 ;; Stat the source to abort early if it
299 (symlink #$source #$target))))
302 (define (directory-union name things)
303 "Return a directory that is the union of THINGS."
306 ;; Only one thing; return it.
311 (use-modules (guix build union))
312 (union-build #$output '#$things))
313 #:modules '((guix build union))))))
315 (define* (activation-service->script service)
316 "Return as a monadic value the activation script for SERVICE, a service of
317 ACTIVATION-SCRIPT-TYPE."
318 (activation-script (service-parameters service)))
320 (define (activation-script gexps)
321 "Return the system's activation script, which evaluates GEXPS."
323 '((gnu build activation)
324 (gnu build linux-boot)
325 (gnu build linux-modules)
326 (gnu build file-systems)
328 (guix build syscalls)
329 (guix build bournish)
332 (define (service-activations)
333 ;; Return the activation scripts for SERVICES.
335 (cut gexp->file "activate-service" <>)
338 (mlet* %store-monad ((actions (service-activations))
339 (modules (imported-modules %modules))
340 (compiled (compiled-modules %modules)))
341 (gexp->file "activate"
343 (eval-when (expand load eval)
344 ;; Make sure 'use-modules' below succeeds.
345 (set! %load-path (cons #$modules %load-path))
346 (set! %load-compiled-path
347 (cons #$compiled %load-compiled-path)))
349 (use-modules (gnu build activation))
351 ;; Make sure /bin/sh is valid and current.
353 (string-append #$(canonical-package bash) "/bin/sh"))
355 ;; Run the services' activation snippets.
356 ;; TODO: Use 'load-compiled'.
357 (for-each primitive-load '#$actions)
359 ;; Set up /run/current-system.
360 (activate-current-system)))))
362 (define (gexps->activation-gexp gexps)
363 "Return a gexp that runs the activation script containing GEXPS."
364 (mlet %store-monad ((script (activation-script gexps)))
365 (return #~(primitive-load #$script))))
367 (define (second-argument a b) b)
369 (define activation-service-type
370 (service-type (name 'activate)
372 (list (service-extension boot-service-type
373 gexps->activation-gexp)))
375 (extend second-argument)))
377 (define %activation-service
378 ;; The activation service produces the activation script from the gexps it
380 (service activation-service-type #t))
382 (define %modprobe-wrapper
383 ;; Wrapper for the 'modprobe' command that knows where modules live.
385 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
386 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
387 ;; environment variable is not set---hence the need for this wrapper.
388 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
389 (program-file "modprobe"
391 (setenv "LINUX_MODULE_DIRECTORY"
392 "/run/booted-system/kernel/lib/modules")
393 (apply execl #$modprobe
394 (cons #$modprobe (cdr (command-line))))))))
396 (define %linux-kernel-activation
397 ;; Activation of the Linux kernel running on the bare metal (as opposed to
398 ;; running in a container.)
400 ;; Tell the kernel to use our 'modprobe' command.
401 (activate-modprobe #$%modprobe-wrapper)
403 ;; Let users debug their own processes!
404 (activate-ptrace-attach)))
406 (define linux-bare-metal-service-type
407 (service-type (name 'linux-bare-metal)
409 (list (service-extension activation-service-type
410 (const %linux-kernel-activation))))))
412 (define %linux-bare-metal-service
413 ;; The service that does things that are needed on the "bare metal", but not
414 ;; necessary or impossible in a container.
415 (service linux-bare-metal-service-type #f))
417 (define (etc-directory service)
418 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
419 (files->etc-directory (service-parameters service)))
421 (define (files->etc-directory files)
422 (file-union "etc" files))
424 (define (etc-entry files)
425 "Return an entry for the /etc directory consisting of FILES in the system
427 (with-monad %store-monad
428 (return `(("etc" ,(files->etc-directory files))))))
430 (define etc-service-type
431 (service-type (name 'etc)
434 (service-extension activation-service-type
437 (files->etc-directory files)))
438 #~(activate-etc #$etc))))
439 (service-extension system-service-type etc-entry)))
440 (compose concatenate)
443 (define (etc-service files)
444 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
445 FILES must be a list of name/file-like object pairs."
446 (service etc-service-type files))
448 (define setuid-program-service-type
449 (service-type (name 'setuid-program)
451 (list (service-extension activation-service-type
453 #~(activate-setuid-programs
454 (list #$@programs))))))
455 (compose concatenate)
458 (define (packages->profile-entry packages)
459 "Return a system entry for the profile containing PACKAGES."
460 (mlet %store-monad ((profile (profile-derivation
461 (manifest (map package->manifest-entry
462 (delete-duplicates packages eq?))))))
463 (return `(("profile" ,profile)))))
465 (define profile-service-type
466 ;; The service that populates the system's profile---i.e.,
467 ;; /run/current-system/profile. It is extended by package lists.
468 (service-type (name 'profile)
470 (list (service-extension system-service-type
471 packages->profile-entry)))
472 (compose concatenate)
475 (define (firmware->activation-gexp firmware)
476 "Return a gexp to make the packages listed in FIRMWARE loadable by the
478 (let ((directory (directory-union "firmware" firmware)))
479 ;; Tell the kernel where firmware is.
480 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
482 (define firmware-service-type
483 ;; The service that collects firmware.
484 (service-type (name 'firmware)
486 (list (service-extension activation-service-type
487 firmware->activation-gexp)))
488 (compose concatenate)
496 (define-condition-type &service-error &error
499 (define-condition-type &missing-target-service-error &service-error
500 missing-target-service-error?
501 (service missing-target-service-error-service)
502 (target-type missing-target-service-error-target-type))
504 (define-condition-type &ambiguous-target-service-error &service-error
505 ambiguous-target-service-error?
506 (service ambiguous-target-service-error-service)
507 (target-type ambiguous-target-service-error-target-type))
509 (define (service-back-edges services)
510 "Return a procedure that, when passed a <service>, returns the list of
511 <service> objects that depend on it."
512 (define (add-edges service edges)
513 (define (add-edge extension edges)
514 (let ((target-type (service-extension-target extension)))
515 (match (filter (lambda (service)
516 (eq? (service-kind service) target-type))
519 (vhash-consq target service edges))
522 (condition (&missing-target-service-error
524 (target-type target-type))
527 (format #f (_ "no target of type '~a' for service ~s")
528 (service-type-name target-type)
532 (condition (&ambiguous-target-service-error
534 (target-type target-type))
538 (_ "more than one target service of type '~a'")
539 (service-type-name target-type))))))))))
541 (fold add-edge edges (service-type-extensions (service-kind service))))
543 (let ((edges (fold add-edges vlist-null services)))
545 (reverse (vhash-foldq* cons '() node edges)))))
547 (define* (fold-services services
548 #:key (target-type system-service-type))
549 "Fold SERVICES by propagating their extensions down to the root of type
550 TARGET-TYPE; return the root service adjusted accordingly."
552 (service-back-edges services))
554 (define (matching-extension target)
555 (let ((target (service-kind target)))
557 (($ <service-extension> type)
558 (eq? type target)))))
560 (define (apply-extension target)
562 (match (find (matching-extension target)
563 (service-type-extensions (service-kind service)))
564 (($ <service-extension> _ compute)
565 (compute (service-parameters service))))))
567 (match (filter (lambda (service)
568 (eq? (service-kind service) target-type))
571 (let loop ((sink sink))
572 (let* ((dependents (map loop (dependents sink)))
573 (extensions (map (apply-extension sink) dependents))
574 (extend (service-type-extend (service-kind sink)))
575 (compose (service-type-compose (service-kind sink)))
576 (params (service-parameters sink)))
577 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
578 ;; different type than the elements of EXTENSIONS.
580 (service (service-kind sink)
581 (extend params (compose extensions)))
585 (condition (&missing-target-service-error
587 (target-type target-type))
589 (message (format #f (_ "service of type '~a' not found")
590 (service-type-name target-type)))))))
593 (condition (&ambiguous-target-service-error
595 (target-type target-type))
599 (_ "more than one target service of type '~a'")
600 (service-type-name target-type)))))))))
602 ;;; services.scm ends here.