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 (guix modules)
29 #:use-module (gnu packages base)
30 #:use-module (gnu packages bash)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
33 #:use-module (srfi srfi-9 gnu)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:use-module (ice-9 vlist)
38 #:use-module (ice-9 match)
39 #:export (service-extension
41 service-extension-target
42 service-extension-compute
47 service-type-extensions
62 missing-target-service-error?
63 missing-target-service-error-service
64 missing-target-service-error-target-type
65 ambiguous-target-service-error?
66 ambiguous-target-service-error-service
67 ambiguous-target-service-error-target-type
72 activation-service-type
73 activation-service->script
74 %linux-bare-metal-service
77 setuid-program-service-type
86 file-union)) ;XXX: for lack of a better place
90 ;;; This module defines a broad notion of "service types" and "services."
92 ;;; A service type describe how its instances extend instances of other
93 ;;; service types. For instance, some services extend the instance of
94 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
95 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
96 ;;; <shepherd-service>.
98 ;;; When applicable, the service type defines how it can itself be extended,
99 ;;; by providing one procedure to compose extensions, and one procedure to
102 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
103 ;;; instance, which is the root of the service DAG. Its value is the
104 ;;; derivation that produces the 'system' directory as returned by
105 ;;; 'operating-system-derivation'.
107 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
108 ;;; "folds" by propagating extensions down the graph; it returns the root
109 ;;; service after the applying all its extensions.
113 (define-record-type <service-extension>
114 (service-extension target compute)
116 (target service-extension-target) ;<service-type>
117 (compute service-extension-compute)) ;params -> params
119 (define-record-type* <service-type> service-type make-service-type
121 (name service-type-name) ;symbol (for debugging)
123 ;; Things extended by services of this type.
124 (extensions service-type-extensions) ;list of <service-extensions>
126 ;; Given a list of extensions, "compose" them.
127 (compose service-type-compose ;list of Any -> Any
130 ;; Extend the services' own parameters with the extension composition.
131 (extend service-type-extend ;list of Any -> parameters
134 (define (write-service-type type port)
135 (format port "#<service-type ~a ~a>"
136 (service-type-name type)
137 (number->string (object-address type) 16)))
139 (set-record-type-printer! <service-type> write-service-type)
141 ;; Services of a given type.
142 (define-record-type <service>
143 (service type parameters)
146 (parameters service-parameters))
148 (define (simple-service name target value)
149 "Return a service that extends TARGET with VALUE. This works by creating a
150 singleton service type NAME, of which the returned service is an instance."
151 (let* ((extension (service-extension target identity))
152 (type (service-type (name name)
153 (extensions (list extension)))))
154 (service type value)))
156 (define-syntax %modify-service
160 ((_ svc (kind param => exp ...) clauses ...)
161 (if (eq? (service-kind svc) kind)
162 (let ((param (service-parameters svc)))
163 (service (service-kind svc)
165 (%modify-service svc clauses ...)))))
167 (define-syntax modify-services
169 "Modify the services listed in SERVICES according to CLAUSES and return
170 the resulting list of services. Each clause must have the form:
172 (TYPE VARIABLE => BODY)
174 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
175 identifier that is bound within BODY to the value of the service of that
176 TYPE. Consider this example:
178 (modify-services %base-services
179 (guix-service-type config =>
182 (use-substitutes? #f)
183 (extra-options '(\"--gc-keep-derivations\"))))
184 (mingetty-service-type config =>
185 (mingetty-configuration
187 (motd (plain-file \"motd\" \"Hi there!\")))))
189 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
190 all the MINGETTY-SERVICE-TYPE instances.
192 This is a shorthand for (map (lambda (svc) ...) %base-services)."
193 ((_ services clauses ...)
194 (map (lambda (service)
195 (%modify-service service clauses ...))
203 (define (system-derivation mentries mextensions)
204 "Return as a monadic value the derivation of the 'system' directory
205 containing the given entries."
206 (mlet %store-monad ((entries mentries)
207 (extensions (sequence %store-monad mextensions)))
210 (append entries (concatenate extensions))))))
212 (define system-service-type
213 ;; This is the ultimate service type, the root of the service DAG. The
214 ;; service of this type is extended by monadic name/item pairs. These items
215 ;; end up in the "system directory" as returned by
216 ;; 'operating-system-derivation'.
217 (service-type (name 'system)
220 (extend system-derivation)))
222 (define (compute-boot-script _ mexps)
223 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
225 ;; Clean up and activate the system, then spawn shepherd.
226 #~(begin #$@gexps))))
228 (define (boot-script-entry mboot)
229 "Return, as a monadic value, an entry for the boot script in the system
231 (mlet %store-monad ((boot mboot))
232 (return `(("boot" ,boot)))))
234 (define boot-service-type
235 ;; The service of this type is extended by being passed gexps as monadic
236 ;; values. It aggregates them in a single script, as a monadic value, which
237 ;; becomes its 'parameters'. It is the only service that extends nothing.
238 (service-type (name 'boot)
240 (list (service-extension system-service-type
243 (extend compute-boot-script)))
245 (define %boot-service
246 ;; The service that produces the boot script.
247 (service boot-service-type #t))
249 (define (cleanup-gexp _)
250 "Return as a monadic value a gexp to clean up /tmp and similar places upon
252 (with-monad %store-monad
253 (with-imported-modules '((guix build utils))
255 (use-modules (guix build utils))
257 ;; Clean out /tmp and /var/run.
259 ;; XXX This needs to happen before service activations, so it
260 ;; has to be here, but this also implicitly assumes that /tmp
261 ;; and /var/run are on the root partition.
262 (letrec-syntax ((fail-safe (syntax-rules ()
268 (fail-safe rest ...)))
271 ;; Ignore I/O errors so the system can boot.
273 (delete-file-recursively "/tmp")
274 (delete-file-recursively "/var/run")
276 (chmod "/tmp" #o1777)
278 (chmod "/var/run" #o755))))))))
280 (define cleanup-service-type
281 ;; Service that cleans things up in /tmp and similar.
282 (service-type (name 'cleanup)
284 (list (service-extension boot-service-type
287 (define* (file-union name files) ;FIXME: Factorize.
288 "Return a <computed-file> that builds a directory containing all of FILES.
289 Each item in FILES must be a list where the first element is the file name to
290 use in the new directory, and the second element is a gexp denoting the target
296 #$@(map (match-lambda
299 ;; Stat the source to abort early if it
303 (symlink #$source #$target))))
306 (define (directory-union name things)
307 "Return a directory that is the union of THINGS."
310 ;; Only one thing; return it.
314 (with-imported-modules '((guix build union))
316 (use-modules (guix build union))
317 (union-build #$output '#$things)))))))
319 (define* (activation-service->script service)
320 "Return as a monadic value the activation script for SERVICE, a service of
321 ACTIVATION-SCRIPT-TYPE."
322 (activation-script (service-parameters service)))
324 (define (activation-script gexps)
325 "Return the system's activation script, which evaluates GEXPS."
326 (define (service-activations)
327 ;; Return the activation scripts for SERVICES.
329 (cut gexp->file "activate-service" <>)
332 (mlet* %store-monad ((actions (service-activations)))
333 (gexp->file "activate"
334 (with-imported-modules (source-module-closure
335 '((gnu build activation)))
337 (use-modules (gnu build activation))
339 ;; Make sure /bin/sh is valid and current.
341 (string-append #$(canonical-package bash) "/bin/sh"))
343 ;; Set up /run/current-system. Among other things this
344 ;; sets up locales, which the activation snippets
345 ;; executed below may expect.
346 (activate-current-system)
348 ;; Run the services' activation snippets.
349 ;; TODO: Use 'load-compiled'.
350 (for-each primitive-load '#$actions))))))
352 (define (gexps->activation-gexp gexps)
353 "Return a gexp that runs the activation script containing GEXPS."
354 (mlet %store-monad ((script (activation-script gexps)))
355 (return #~(primitive-load #$script))))
357 (define (second-argument a b) b)
359 (define activation-service-type
360 (service-type (name 'activate)
362 (list (service-extension boot-service-type
363 gexps->activation-gexp)))
365 (extend second-argument)))
367 (define %activation-service
368 ;; The activation service produces the activation script from the gexps it
370 (service activation-service-type #t))
372 (define %modprobe-wrapper
373 ;; Wrapper for the 'modprobe' command that knows where modules live.
375 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
376 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
377 ;; environment variable is not set---hence the need for this wrapper.
378 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
379 (program-file "modprobe"
381 (setenv "LINUX_MODULE_DIRECTORY"
382 "/run/booted-system/kernel/lib/modules")
383 (apply execl #$modprobe
384 (cons #$modprobe (cdr (command-line))))))))
386 (define %linux-kernel-activation
387 ;; Activation of the Linux kernel running on the bare metal (as opposed to
388 ;; running in a container.)
390 ;; Tell the kernel to use our 'modprobe' command.
391 (activate-modprobe #$%modprobe-wrapper)
393 ;; Let users debug their own processes!
394 (activate-ptrace-attach)))
396 (define linux-bare-metal-service-type
397 (service-type (name 'linux-bare-metal)
399 (list (service-extension activation-service-type
400 (const %linux-kernel-activation))))))
402 (define %linux-bare-metal-service
403 ;; The service that does things that are needed on the "bare metal", but not
404 ;; necessary or impossible in a container.
405 (service linux-bare-metal-service-type #f))
407 (define (etc-directory service)
408 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
409 (files->etc-directory (service-parameters service)))
411 (define (files->etc-directory files)
412 (file-union "etc" files))
414 (define (etc-entry files)
415 "Return an entry for the /etc directory consisting of FILES in the system
417 (with-monad %store-monad
418 (return `(("etc" ,(files->etc-directory files))))))
420 (define etc-service-type
421 (service-type (name 'etc)
424 (service-extension activation-service-type
427 (files->etc-directory files)))
428 #~(activate-etc #$etc))))
429 (service-extension system-service-type etc-entry)))
430 (compose concatenate)
433 (define (etc-service files)
434 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
435 FILES must be a list of name/file-like object pairs."
436 (service etc-service-type files))
438 (define setuid-program-service-type
439 (service-type (name 'setuid-program)
441 (list (service-extension activation-service-type
443 #~(activate-setuid-programs
444 (list #$@programs))))))
445 (compose concatenate)
448 (define (packages->profile-entry packages)
449 "Return a system entry for the profile containing PACKAGES."
450 (mlet %store-monad ((profile (profile-derivation
452 (delete-duplicates packages eq?)))))
453 (return `(("profile" ,profile)))))
455 (define profile-service-type
456 ;; The service that populates the system's profile---i.e.,
457 ;; /run/current-system/profile. It is extended by package lists.
458 (service-type (name 'profile)
460 (list (service-extension system-service-type
461 packages->profile-entry)))
462 (compose concatenate)
465 (define (firmware->activation-gexp firmware)
466 "Return a gexp to make the packages listed in FIRMWARE loadable by the
468 (let ((directory (directory-union "firmware" firmware)))
469 ;; Tell the kernel where firmware is.
470 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
472 (define firmware-service-type
473 ;; The service that collects firmware.
474 (service-type (name 'firmware)
476 (list (service-extension activation-service-type
477 firmware->activation-gexp)))
478 (compose concatenate)
481 (define (gc-roots->system-entry roots)
482 "Return an entry in the system's output containing symlinks to ROOTS."
483 (mlet %store-monad ((entry (gexp->derivation
485 #~(let ((roots '#$roots))
491 (iota (length roots))))))))
492 (return (if (null? roots)
494 `(("gc-roots" ,entry))))))
496 (define gc-root-service-type
497 ;; A service to associate extra garbage-collector roots to the system. This
498 ;; is a simple hack that guarantees that the system retains references to
499 ;; the given list of roots. Roots must be "lowerable" objects like
500 ;; packages, or derivations.
501 (service-type (name 'gc-roots)
503 (list (service-extension system-service-type
504 gc-roots->system-entry)))
505 (compose concatenate)
513 (define-condition-type &service-error &error
516 (define-condition-type &missing-target-service-error &service-error
517 missing-target-service-error?
518 (service missing-target-service-error-service)
519 (target-type missing-target-service-error-target-type))
521 (define-condition-type &ambiguous-target-service-error &service-error
522 ambiguous-target-service-error?
523 (service ambiguous-target-service-error-service)
524 (target-type ambiguous-target-service-error-target-type))
526 (define (service-back-edges services)
527 "Return a procedure that, when passed a <service>, returns the list of
528 <service> objects that depend on it."
529 (define (add-edges service edges)
530 (define (add-edge extension edges)
531 (let ((target-type (service-extension-target extension)))
532 (match (filter (lambda (service)
533 (eq? (service-kind service) target-type))
536 (vhash-consq target service edges))
539 (condition (&missing-target-service-error
541 (target-type target-type))
544 (format #f (_ "no target of type '~a' for service ~s")
545 (service-type-name target-type)
549 (condition (&ambiguous-target-service-error
551 (target-type target-type))
555 (_ "more than one target service of type '~a'")
556 (service-type-name target-type))))))))))
558 (fold add-edge edges (service-type-extensions (service-kind service))))
560 (let ((edges (fold add-edges vlist-null services)))
562 (reverse (vhash-foldq* cons '() node edges)))))
564 (define* (fold-services services
565 #:key (target-type system-service-type))
566 "Fold SERVICES by propagating their extensions down to the root of type
567 TARGET-TYPE; return the root service adjusted accordingly."
569 (service-back-edges services))
571 (define (matching-extension target)
572 (let ((target (service-kind target)))
574 (($ <service-extension> type)
575 (eq? type target)))))
577 (define (apply-extension target)
579 (match (find (matching-extension target)
580 (service-type-extensions (service-kind service)))
581 (($ <service-extension> _ compute)
582 (compute (service-parameters service))))))
584 (match (filter (lambda (service)
585 (eq? (service-kind service) target-type))
588 (let loop ((sink sink))
589 (let* ((dependents (map loop (dependents sink)))
590 (extensions (map (apply-extension sink) dependents))
591 (extend (service-type-extend (service-kind sink)))
592 (compose (service-type-compose (service-kind sink)))
593 (params (service-parameters sink)))
594 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
595 ;; different type than the elements of EXTENSIONS.
597 (service (service-kind sink)
598 (extend params (compose extensions)))
602 (condition (&missing-target-service-error
604 (target-type target-type))
606 (message (format #f (_ "service of type '~a' not found")
607 (service-type-name target-type)))))))
610 (condition (&ambiguous-target-service-error
612 (target-type target-type))
616 (_ "more than one target service of type '~a'")
617 (service-type-name target-type)))))))))
619 ;;; services.scm ends here.