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
81 file-union)) ;XXX: for lack of a better place
85 ;;; This module defines a broad notion of "service types" and "services."
87 ;;; A service type describe how its instances extend instances of other
88 ;;; service types. For instance, some services extend the instance of
89 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
90 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
91 ;;; <shepherd-service>.
93 ;;; When applicable, the service type defines how it can itself be extended,
94 ;;; by providing one procedure to compose extensions, and one procedure to
97 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
98 ;;; instance, which is the root of the service DAG. Its value is the
99 ;;; derivation that produces the 'system' directory as returned by
100 ;;; 'operating-system-derivation'.
102 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
103 ;;; "folds" by propagating extensions down the graph; it returns the root
104 ;;; service after the applying all its extensions.
108 (define-record-type <service-extension>
109 (service-extension target compute)
111 (target service-extension-target) ;<service-type>
112 (compute service-extension-compute)) ;params -> params
114 (define-record-type* <service-type> service-type make-service-type
116 (name service-type-name) ;symbol (for debugging)
118 ;; Things extended by services of this type.
119 (extensions service-type-extensions) ;list of <service-extensions>
121 ;; Given a list of extensions, "compose" them.
122 (compose service-type-compose ;list of Any -> Any
125 ;; Extend the services' own parameters with the extension composition.
126 (extend service-type-extend ;list of Any -> parameters
129 (define (write-service-type type port)
130 (format port "#<service-type ~a ~a>"
131 (service-type-name type)
132 (number->string (object-address type) 16)))
134 (set-record-type-printer! <service-type> write-service-type)
136 ;; Services of a given type.
137 (define-record-type <service>
138 (service type parameters)
141 (parameters service-parameters))
144 (define-syntax %modify-service
148 ((_ svc (kind param => exp ...) clauses ...)
149 (if (eq? (service-kind svc) kind)
150 (let ((param (service-parameters svc)))
151 (service (service-kind svc)
153 (%modify-service svc clauses ...)))))
155 (define-syntax modify-services
157 "Modify the services listed in SERVICES according to CLAUSES and return
158 the resulting list of services. Each clause must have the form:
160 (TYPE VARIABLE => BODY)
162 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
163 identifier that is bound within BODY to the value of the service of that
164 TYPE. Consider this example:
166 (modify-services %base-services
167 (guix-service-type config =>
170 (use-substitutes? #f)
171 (extra-options '(\"--gc-keep-derivations\"))))
172 (mingetty-service-type config =>
173 (mingetty-configuration
175 (motd (plain-file \"motd\" \"Hi there!\")))))
177 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
178 all the MINGETTY-SERVICE-TYPE instances.
180 This is a shorthand for (map (lambda (svc) ...) %base-services)."
181 ((_ services clauses ...)
182 (map (lambda (service)
183 (%modify-service service clauses ...))
191 (define (system-derivation mentries mextensions)
192 "Return as a monadic value the derivation of the 'system' directory
193 containing the given entries."
194 (mlet %store-monad ((entries mentries)
195 (extensions (sequence %store-monad mextensions)))
198 (append entries (concatenate extensions))))))
200 (define system-service-type
201 ;; This is the ultimate service type, the root of the service DAG. The
202 ;; service of this type is extended by monadic name/item pairs. These items
203 ;; end up in the "system directory" as returned by
204 ;; 'operating-system-derivation'.
205 (service-type (name 'system)
208 (extend system-derivation)))
210 (define (compute-boot-script _ mexps)
211 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
213 ;; Clean up and activate the system, then spawn shepherd.
214 #~(begin #$@gexps))))
216 (define (boot-script-entry mboot)
217 "Return, as a monadic value, an entry for the boot script in the system
219 (mlet %store-monad ((boot mboot))
220 (return `(("boot" ,boot)))))
222 (define boot-service-type
223 ;; The service of this type is extended by being passed gexps as monadic
224 ;; values. It aggregates them in a single script, as a monadic value, which
225 ;; becomes its 'parameters'. It is the only service that extends nothing.
226 (service-type (name 'boot)
228 (list (service-extension system-service-type
231 (extend compute-boot-script)))
233 (define %boot-service
234 ;; The service that produces the boot script.
235 (service boot-service-type #t))
237 (define (cleanup-gexp _)
238 "Return as a monadic value a gexp to clean up /tmp and similar places upon
241 '((guix build utils)))
243 (mlet %store-monad ((modules (imported-modules %modules))
244 (compiled (compiled-modules %modules)))
246 (eval-when (expand load eval)
247 ;; Make sure 'use-modules' below succeeds.
248 (set! %load-path (cons #$modules %load-path))
249 (set! %load-compiled-path
250 (cons #$compiled %load-compiled-path)))
252 (use-modules (guix build utils))
254 ;; Clean out /tmp and /var/run.
256 ;; XXX This needs to happen before service activations, so it
257 ;; has to be here, but this also implicitly assumes that /tmp
258 ;; and /var/run are on the root partition.
259 (letrec-syntax ((fail-safe (syntax-rules ()
265 (fail-safe rest ...)))
268 ;; Ignore I/O errors so the system can boot.
270 (delete-file-recursively "/tmp")
271 (delete-file-recursively "/var/run")
273 (chmod "/tmp" #o1777)
275 (chmod "/var/run" #o755)))))))
277 (define cleanup-service-type
278 ;; Service that cleans things up in /tmp and similar.
279 (service-type (name 'cleanup)
281 (list (service-extension boot-service-type
284 (define* (file-union name files) ;FIXME: Factorize.
285 "Return a <computed-file> that builds a directory containing all of FILES.
286 Each item in FILES must be a list where the first element is the file name to
287 use in the new directory, and the second element is a gexp denoting the target
293 #$@(map (match-lambda
296 ;; Stat the source to abort early if it
300 (symlink #$source #$target))))
303 (define (directory-union name things)
304 "Return a directory that is the union of THINGS."
307 ;; Only one thing; return it.
312 (use-modules (guix build union))
313 (union-build #$output '#$things))
314 #:modules '((guix build union))))))
316 (define* (activation-service->script service)
317 "Return as a monadic value the activation script for SERVICE, a service of
318 ACTIVATION-SCRIPT-TYPE."
319 (activation-script (service-parameters service)))
321 (define (activation-script gexps)
322 "Return the system's activation script, which evaluates GEXPS."
324 '((gnu build activation)
325 (gnu build linux-boot)
326 (gnu build linux-modules)
327 (gnu build file-systems)
329 (guix build syscalls)
330 (guix build bournish)
333 (define (service-activations)
334 ;; Return the activation scripts for SERVICES.
336 (cut gexp->file "activate-service" <>)
339 (mlet* %store-monad ((actions (service-activations))
340 (modules (imported-modules %modules))
341 (compiled (compiled-modules %modules)))
342 (gexp->file "activate"
344 (eval-when (expand load eval)
345 ;; Make sure 'use-modules' below succeeds.
346 (set! %load-path (cons #$modules %load-path))
347 (set! %load-compiled-path
348 (cons #$compiled %load-compiled-path)))
350 (use-modules (gnu build activation))
352 ;; Make sure /bin/sh is valid and current.
354 (string-append #$(canonical-package bash) "/bin/sh"))
356 ;; Run the services' activation snippets.
357 ;; TODO: Use 'load-compiled'.
358 (for-each primitive-load '#$actions)
360 ;; Set up /run/current-system.
361 (activate-current-system)))))
363 (define (gexps->activation-gexp gexps)
364 "Return a gexp that runs the activation script containing GEXPS."
365 (mlet %store-monad ((script (activation-script gexps)))
366 (return #~(primitive-load #$script))))
368 (define (second-argument a b) b)
370 (define activation-service-type
371 (service-type (name 'activate)
373 (list (service-extension boot-service-type
374 gexps->activation-gexp)))
376 (extend second-argument)))
378 (define %activation-service
379 ;; The activation service produces the activation script from the gexps it
381 (service activation-service-type #t))
383 (define %modprobe-wrapper
384 ;; Wrapper for the 'modprobe' command that knows where modules live.
386 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
387 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
388 ;; environment variable is not set---hence the need for this wrapper.
389 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
390 (program-file "modprobe"
392 (setenv "LINUX_MODULE_DIRECTORY"
393 "/run/booted-system/kernel/lib/modules")
394 (apply execl #$modprobe
395 (cons #$modprobe (cdr (command-line))))))))
397 (define %linux-kernel-activation
398 ;; Activation of the Linux kernel running on the bare metal (as opposed to
399 ;; running in a container.)
401 ;; Tell the kernel to use our 'modprobe' command.
402 (activate-modprobe #$%modprobe-wrapper)
404 ;; Let users debug their own processes!
405 (activate-ptrace-attach)))
407 (define linux-bare-metal-service-type
408 (service-type (name 'linux-bare-metal)
410 (list (service-extension activation-service-type
411 (const %linux-kernel-activation))))))
413 (define %linux-bare-metal-service
414 ;; The service that does things that are needed on the "bare metal", but not
415 ;; necessary or impossible in a container.
416 (service linux-bare-metal-service-type #f))
418 (define (etc-directory service)
419 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
420 (files->etc-directory (service-parameters service)))
422 (define (files->etc-directory files)
423 (file-union "etc" files))
425 (define (etc-entry files)
426 "Return an entry for the /etc directory consisting of FILES in the system
428 (with-monad %store-monad
429 (return `(("etc" ,(files->etc-directory files))))))
431 (define etc-service-type
432 (service-type (name 'etc)
435 (service-extension activation-service-type
438 (files->etc-directory files)))
439 #~(activate-etc #$etc))))
440 (service-extension system-service-type etc-entry)))
441 (compose concatenate)
444 (define (etc-service files)
445 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
446 FILES must be a list of name/file-like object pairs."
447 (service etc-service-type files))
449 (define setuid-program-service-type
450 (service-type (name 'setuid-program)
452 (list (service-extension activation-service-type
454 #~(activate-setuid-programs
455 (list #$@programs))))))
456 (compose concatenate)
459 (define (packages->profile-entry packages)
460 "Return a system entry for the profile containing PACKAGES."
461 (mlet %store-monad ((profile (profile-derivation
463 (delete-duplicates packages eq?)))))
464 (return `(("profile" ,profile)))))
466 (define profile-service-type
467 ;; The service that populates the system's profile---i.e.,
468 ;; /run/current-system/profile. It is extended by package lists.
469 (service-type (name 'profile)
471 (list (service-extension system-service-type
472 packages->profile-entry)))
473 (compose concatenate)
476 (define (firmware->activation-gexp firmware)
477 "Return a gexp to make the packages listed in FIRMWARE loadable by the
479 (let ((directory (directory-union "firmware" firmware)))
480 ;; Tell the kernel where firmware is.
481 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
483 (define firmware-service-type
484 ;; The service that collects firmware.
485 (service-type (name 'firmware)
487 (list (service-extension activation-service-type
488 firmware->activation-gexp)))
489 (compose concatenate)
497 (define-condition-type &service-error &error
500 (define-condition-type &missing-target-service-error &service-error
501 missing-target-service-error?
502 (service missing-target-service-error-service)
503 (target-type missing-target-service-error-target-type))
505 (define-condition-type &ambiguous-target-service-error &service-error
506 ambiguous-target-service-error?
507 (service ambiguous-target-service-error-service)
508 (target-type ambiguous-target-service-error-target-type))
510 (define (service-back-edges services)
511 "Return a procedure that, when passed a <service>, returns the list of
512 <service> objects that depend on it."
513 (define (add-edges service edges)
514 (define (add-edge extension edges)
515 (let ((target-type (service-extension-target extension)))
516 (match (filter (lambda (service)
517 (eq? (service-kind service) target-type))
520 (vhash-consq target service edges))
523 (condition (&missing-target-service-error
525 (target-type target-type))
528 (format #f (_ "no target of type '~a' for service ~s")
529 (service-type-name target-type)
533 (condition (&ambiguous-target-service-error
535 (target-type target-type))
539 (_ "more than one target service of type '~a'")
540 (service-type-name target-type))))))))))
542 (fold add-edge edges (service-type-extensions (service-kind service))))
544 (let ((edges (fold add-edges vlist-null services)))
546 (reverse (vhash-foldq* cons '() node edges)))))
548 (define* (fold-services services
549 #:key (target-type system-service-type))
550 "Fold SERVICES by propagating their extensions down to the root of type
551 TARGET-TYPE; return the root service adjusted accordingly."
553 (service-back-edges services))
555 (define (matching-extension target)
556 (let ((target (service-kind target)))
558 (($ <service-extension> type)
559 (eq? type target)))))
561 (define (apply-extension target)
563 (match (find (matching-extension target)
564 (service-type-extensions (service-kind service)))
565 (($ <service-extension> _ compute)
566 (compute (service-parameters service))))))
568 (match (filter (lambda (service)
569 (eq? (service-kind service) target-type))
572 (let loop ((sink sink))
573 (let* ((dependents (map loop (dependents sink)))
574 (extensions (map (apply-extension sink) dependents))
575 (extend (service-type-extend (service-kind sink)))
576 (compose (service-type-compose (service-kind sink)))
577 (params (service-parameters sink)))
578 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
579 ;; different type than the elements of EXTENSIONS.
581 (service (service-kind sink)
582 (extend params (compose extensions)))
586 (condition (&missing-target-service-error
588 (target-type target-type))
590 (message (format #f (_ "service of type '~a' not found")
591 (service-type-name target-type)))))))
594 (condition (&ambiguous-target-service-error
596 (target-type target-type))
600 (_ "more than one target service of type '~a'")
601 (service-type-name target-type)))))))))
603 ;;; services.scm ends here.