gnu: system: Fix computation of boot parameters for LUKS devices.
[jackhill/guix/guix.git] / gnu / services.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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 discovery)
27 #:use-module (guix sets)
28 #:use-module (guix ui)
29 #:use-module ((guix utils) #:select (source-properties->location))
30 #:use-module (guix modules)
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bash)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-9)
35 #:use-module (srfi srfi-9 gnu)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:use-module (ice-9 vlist)
40 #:use-module (ice-9 match)
41 #:export (service-extension
42 service-extension?
43 service-extension-target
44 service-extension-compute
45
46 service-type
47 service-type?
48 service-type-name
49 service-type-extensions
50 service-type-compose
51 service-type-extend
52 service-type-default-value
53 service-type-description
54 service-type-location
55
56 %service-type-path
57 fold-service-types
58
59 service
60 service?
61 service-kind
62 service-value
63 service-parameters ;deprecated
64
65 simple-service
66 modify-services
67 service-back-edges
68 fold-services
69
70 service-error?
71 missing-value-service-error?
72 missing-value-service-error-type
73 missing-value-service-error-location
74 missing-target-service-error?
75 missing-target-service-error-service
76 missing-target-service-error-target-type
77 ambiguous-target-service-error?
78 ambiguous-target-service-error-service
79 ambiguous-target-service-error-target-type
80
81 system-service-type
82 boot-service-type
83 cleanup-service-type
84 activation-service-type
85 activation-service->script
86 %linux-bare-metal-service
87 special-files-service-type
88 extra-special-file
89 etc-service-type
90 etc-directory
91 setuid-program-service-type
92 profile-service-type
93 firmware-service-type
94 gc-root-service-type
95
96 %boot-service
97 %activation-service
98 etc-service
99
100 file-union)) ;XXX: for lack of a better place
101
102 ;;; Comment:
103 ;;;
104 ;;; This module defines a broad notion of "service types" and "services."
105 ;;;
106 ;;; A service type describe how its instances extend instances of other
107 ;;; service types. For instance, some services extend the instance of
108 ;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
109 ;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
110 ;;; <shepherd-service>.
111 ;;;
112 ;;; When applicable, the service type defines how it can itself be extended,
113 ;;; by providing one procedure to compose extensions, and one procedure to
114 ;;; extend itself.
115 ;;;
116 ;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
117 ;;; instance, which is the root of the service DAG. Its value is the
118 ;;; derivation that produces the 'system' directory as returned by
119 ;;; 'operating-system-derivation'.
120 ;;;
121 ;;; The 'fold-services' procedure can be passed a list of procedures, which it
122 ;;; "folds" by propagating extensions down the graph; it returns the root
123 ;;; service after the applying all its extensions.
124 ;;;
125 ;;; Code:
126
127 (define-record-type <service-extension>
128 (service-extension target compute)
129 service-extension?
130 (target service-extension-target) ;<service-type>
131 (compute service-extension-compute)) ;params -> params
132
133 (define &no-default-value
134 ;; Value used to denote service types that have no associated default value.
135 '(no default value))
136
137 (define-record-type* <service-type> service-type make-service-type
138 service-type?
139 (name service-type-name) ;symbol (for debugging)
140
141 ;; Things extended by services of this type.
142 (extensions service-type-extensions) ;list of <service-extensions>
143
144 ;; Given a list of extensions, "compose" them.
145 (compose service-type-compose ;list of Any -> Any
146 (default #f))
147
148 ;; Extend the services' own parameters with the extension composition.
149 (extend service-type-extend ;list of Any -> parameters
150 (default #f))
151
152 ;; Optional default value for instances of this type.
153 (default-value service-type-default-value ;Any
154 (default &no-default-value))
155
156 ;; Meta-data.
157 (description service-type-description ;string
158 (default #f))
159 (location service-type-location ;<location>
160 (default (and=> (current-source-location)
161 source-properties->location))
162 (innate)))
163
164 (define (write-service-type type port)
165 (format port "#<service-type ~a ~a>"
166 (service-type-name type)
167 (number->string (object-address type) 16)))
168
169 (set-record-type-printer! <service-type> write-service-type)
170
171 (define %distro-root-directory
172 ;; Absolute file name of the module hierarchy.
173 (dirname (search-path %load-path "guix.scm")))
174
175 (define %service-type-path
176 ;; Search path for service types.
177 (make-parameter `((,%distro-root-directory . "gnu/services")
178 (,%distro-root-directory . "gnu/system"))))
179
180 (define* (fold-service-types proc seed
181 #:optional
182 (modules (all-modules (%service-type-path))))
183 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
184 is used as the initial value of RESULT."
185 (fold-module-public-variables (lambda (object result)
186 (if (service-type? object)
187 (proc object result)
188 result))
189 '()
190 modules))
191
192 ;; Services of a given type.
193 (define-record-type <service>
194 (make-service type value)
195 service?
196 (type service-kind)
197 (value service-value))
198
199 (define-syntax service
200 (syntax-rules ()
201 "Return a service instance of TYPE. The service value is VALUE or, if
202 omitted, TYPE's default value."
203 ((_ type value)
204 (make-service type value))
205 ((_ type)
206 (%service-with-default-value (current-source-location)
207 type))))
208
209 (define (%service-with-default-value location type)
210 "Return a instance of service type TYPE with its default value, if any. If
211 TYPE does not have a default value, an error is raised."
212 ;; TODO: Currently this is a run-time error but with a little bit macrology
213 ;; we could turn it into an expansion-time error.
214 (let ((default (service-type-default-value type)))
215 (if (eq? default &no-default-value)
216 (let ((location (source-properties->location location)))
217 (raise
218 (condition
219 (&missing-value-service-error (type type) (location location))
220 (&message
221 (message (format #f (G_ "~a: no value specified \
222 for service of type '~a'")
223 (location->string location)
224 (service-type-name type)))))))
225 (service type default))))
226
227 (define-condition-type &service-error &error
228 service-error?)
229
230 (define-condition-type &missing-value-service-error &service-error
231 missing-value-service-error?
232 (type missing-value-service-error-type)
233 (location missing-value-service-error-location))
234
235
236 \f
237 ;;;
238 ;;; Helpers.
239 ;;;
240
241 (define service-parameters
242 ;; Deprecated alias.
243 service-value)
244
245 (define (simple-service name target value)
246 "Return a service that extends TARGET with VALUE. This works by creating a
247 singleton service type NAME, of which the returned service is an instance."
248 (let* ((extension (service-extension target identity))
249 (type (service-type (name name)
250 (extensions (list extension)))))
251 (service type value)))
252
253 (define-syntax %modify-service
254 (syntax-rules (=>)
255 ((_ service)
256 service)
257 ((_ svc (kind param => exp ...) clauses ...)
258 (if (eq? (service-kind svc) kind)
259 (let ((param (service-value svc)))
260 (service (service-kind svc)
261 (begin exp ...)))
262 (%modify-service svc clauses ...)))))
263
264 (define-syntax modify-services
265 (syntax-rules ()
266 "Modify the services listed in SERVICES according to CLAUSES and return
267 the resulting list of services. Each clause must have the form:
268
269 (TYPE VARIABLE => BODY)
270
271 where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
272 identifier that is bound within BODY to the value of the service of that
273 TYPE. Consider this example:
274
275 (modify-services %base-services
276 (guix-service-type config =>
277 (guix-configuration
278 (inherit config)
279 (use-substitutes? #f)
280 (extra-options '(\"--gc-keep-derivations\"))))
281 (mingetty-service-type config =>
282 (mingetty-configuration
283 (inherit config)
284 (motd (plain-file \"motd\" \"Hi there!\")))))
285
286 It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
287 all the MINGETTY-SERVICE-TYPE instances.
288
289 This is a shorthand for (map (lambda (svc) ...) %base-services)."
290 ((_ services clauses ...)
291 (map (lambda (service)
292 (%modify-service service clauses ...))
293 services))))
294
295 \f
296 ;;;
297 ;;; Core services.
298 ;;;
299
300 (define (system-derivation mentries mextensions)
301 "Return as a monadic value the derivation of the 'system' directory
302 containing the given entries."
303 (mlet %store-monad ((entries mentries)
304 (extensions (sequence %store-monad mextensions)))
305 (lower-object
306 (file-union "system"
307 (append entries (concatenate extensions))))))
308
309 (define system-service-type
310 ;; This is the ultimate service type, the root of the service DAG. The
311 ;; service of this type is extended by monadic name/item pairs. These items
312 ;; end up in the "system directory" as returned by
313 ;; 'operating-system-derivation'.
314 (service-type (name 'system)
315 (extensions '())
316 (compose identity)
317 (extend system-derivation)))
318
319 (define (compute-boot-script _ mexps)
320 (mlet %store-monad ((gexps (sequence %store-monad mexps)))
321 (gexp->file "boot"
322 ;; Clean up and activate the system, then spawn shepherd.
323 #~(begin #$@gexps))))
324
325 (define (boot-script-entry mboot)
326 "Return, as a monadic value, an entry for the boot script in the system
327 directory."
328 (mlet %store-monad ((boot mboot))
329 (return `(("boot" ,boot)))))
330
331 (define boot-service-type
332 ;; The service of this type is extended by being passed gexps as monadic
333 ;; values. It aggregates them in a single script, as a monadic value, which
334 ;; becomes its 'parameters'. It is the only service that extends nothing.
335 (service-type (name 'boot)
336 (extensions
337 (list (service-extension system-service-type
338 boot-script-entry)))
339 (compose append)
340 (extend compute-boot-script)))
341
342 (define %boot-service
343 ;; The service that produces the boot script.
344 (service boot-service-type #t))
345
346 (define (cleanup-gexp _)
347 "Return as a monadic value a gexp to clean up /tmp and similar places upon
348 boot."
349 (with-monad %store-monad
350 (with-imported-modules '((guix build utils))
351 (return #~(begin
352 (use-modules (guix build utils))
353
354 ;; Clean out /tmp and /var/run.
355 ;;
356 ;; XXX This needs to happen before service activations, so it
357 ;; has to be here, but this also implicitly assumes that /tmp
358 ;; and /var/run are on the root partition.
359 (letrec-syntax ((fail-safe (syntax-rules ()
360 ((_ exp rest ...)
361 (begin
362 (catch 'system-error
363 (lambda () exp)
364 (const #f))
365 (fail-safe rest ...)))
366 ((_)
367 #t))))
368 ;; Ignore I/O errors so the system can boot.
369 (fail-safe
370 (delete-file-recursively "/tmp")
371 (delete-file-recursively "/var/run")
372 (mkdir "/tmp")
373 (chmod "/tmp" #o1777)
374 (mkdir "/var/run")
375 (chmod "/var/run" #o755))))))))
376
377 (define cleanup-service-type
378 ;; Service that cleans things up in /tmp and similar.
379 (service-type (name 'cleanup)
380 (extensions
381 (list (service-extension boot-service-type
382 cleanup-gexp)))))
383
384 (define* (file-union name files) ;FIXME: Factorize.
385 "Return a <computed-file> that builds a directory containing all of FILES.
386 Each item in FILES must be a list where the first element is the file name to
387 use in the new directory, and the second element is a gexp denoting the target
388 file."
389 (computed-file name
390 #~(begin
391 (mkdir #$output)
392 (chdir #$output)
393 #$@(map (match-lambda
394 ((target source)
395 #~(begin
396 ;; Stat the source to abort early if it
397 ;; does not exist.
398 (stat #$source)
399
400 (symlink #$source #$target))))
401 files))))
402
403 (define (directory-union name things)
404 "Return a directory that is the union of THINGS."
405 (match things
406 ((one)
407 ;; Only one thing; return it.
408 one)
409 (_
410 (computed-file name
411 (with-imported-modules '((guix build union))
412 #~(begin
413 (use-modules (guix build union))
414 (union-build #$output '#$things)))))))
415
416 (define* (activation-service->script service)
417 "Return as a monadic value the activation script for SERVICE, a service of
418 ACTIVATION-SCRIPT-TYPE."
419 (activation-script (service-value service)))
420
421 (define (activation-script gexps)
422 "Return the system's activation script, which evaluates GEXPS."
423 (define (service-activations)
424 ;; Return the activation scripts for SERVICES.
425 (mapm %store-monad
426 (cut gexp->file "activate-service" <>)
427 gexps))
428
429 (mlet* %store-monad ((actions (service-activations)))
430 (gexp->file "activate"
431 (with-imported-modules (source-module-closure
432 '((gnu build activation)
433 (guix build utils)))
434 #~(begin
435 (use-modules (gnu build activation)
436 (guix build utils))
437
438 ;; Make sure the user accounting database exists. If it
439 ;; does not exist, 'setutxent' does not create it and
440 ;; thus there is no accounting at all.
441 (close-port (open-file "/var/run/utmpx" "a0"))
442
443 ;; Same for 'wtmp', which is populated by mingetty et
444 ;; al.
445 (mkdir-p "/var/log")
446 (close-port (open-file "/var/log/wtmp" "a0"))
447
448 ;; Set up /run/current-system. Among other things this
449 ;; sets up locales, which the activation snippets
450 ;; executed below may expect.
451 (activate-current-system)
452
453 ;; Run the services' activation snippets.
454 ;; TODO: Use 'load-compiled'.
455 (for-each primitive-load '#$actions))))))
456
457 (define (gexps->activation-gexp gexps)
458 "Return a gexp that runs the activation script containing GEXPS."
459 (mlet %store-monad ((script (activation-script gexps)))
460 (return #~(primitive-load #$script))))
461
462 (define (second-argument a b) b)
463
464 (define activation-service-type
465 (service-type (name 'activate)
466 (extensions
467 (list (service-extension boot-service-type
468 gexps->activation-gexp)))
469 (compose append)
470 (extend second-argument)))
471
472 (define %activation-service
473 ;; The activation service produces the activation script from the gexps it
474 ;; receives.
475 (service activation-service-type #t))
476
477 (define %modprobe-wrapper
478 ;; Wrapper for the 'modprobe' command that knows where modules live.
479 ;;
480 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
481 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
482 ;; environment variable is not set---hence the need for this wrapper.
483 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
484 (program-file "modprobe"
485 #~(begin
486 (setenv "LINUX_MODULE_DIRECTORY"
487 "/run/booted-system/kernel/lib/modules")
488 (apply execl #$modprobe
489 (cons #$modprobe (cdr (command-line))))))))
490
491 (define %linux-kernel-activation
492 ;; Activation of the Linux kernel running on the bare metal (as opposed to
493 ;; running in a container.)
494 #~(begin
495 ;; Tell the kernel to use our 'modprobe' command.
496 (activate-modprobe #$%modprobe-wrapper)
497
498 ;; Let users debug their own processes!
499 (activate-ptrace-attach)))
500
501 (define %linux-bare-metal-service
502 ;; The service that does things that are needed on the "bare metal", but not
503 ;; necessary or impossible in a container.
504 (simple-service 'linux-bare-metal
505 activation-service-type
506 %linux-kernel-activation))
507
508
509 (define special-files-service-type
510 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
511 (service-type
512 (name 'special-files)
513 (extensions
514 (list (service-extension activation-service-type
515 (lambda (files)
516 #~(activate-special-files '#$files)))))
517 (compose concatenate)
518 (extend append)))
519
520 (define (extra-special-file file target)
521 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
522 (file-append coreutils \"/bin/env\")
523 and FILE could be \"/usr/bin/env\"."
524 (simple-service (string->symbol (string-append "special-file-" file))
525 special-files-service-type
526 `((,file ,target))))
527
528 (define (etc-directory service)
529 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
530 (files->etc-directory (service-value service)))
531
532 (define (files->etc-directory files)
533 (file-union "etc" files))
534
535 (define (etc-entry files)
536 "Return an entry for the /etc directory consisting of FILES in the system
537 directory."
538 (with-monad %store-monad
539 (return `(("etc" ,(files->etc-directory files))))))
540
541 (define etc-service-type
542 (service-type (name 'etc)
543 (extensions
544 (list
545 (service-extension activation-service-type
546 (lambda (files)
547 (let ((etc
548 (files->etc-directory files)))
549 #~(activate-etc #$etc))))
550 (service-extension system-service-type etc-entry)))
551 (compose concatenate)
552 (extend append)))
553
554 (define (etc-service files)
555 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
556 FILES must be a list of name/file-like object pairs."
557 (service etc-service-type files))
558
559 (define setuid-program-service-type
560 (service-type (name 'setuid-program)
561 (extensions
562 (list (service-extension activation-service-type
563 (lambda (programs)
564 #~(activate-setuid-programs
565 (list #$@programs))))))
566 (compose concatenate)
567 (extend append)))
568
569 (define (packages->profile-entry packages)
570 "Return a system entry for the profile containing PACKAGES."
571 (mlet %store-monad ((profile (profile-derivation
572 (packages->manifest
573 (delete-duplicates packages eq?)))))
574 (return `(("profile" ,profile)))))
575
576 (define profile-service-type
577 ;; The service that populates the system's profile---i.e.,
578 ;; /run/current-system/profile. It is extended by package lists.
579 (service-type (name 'profile)
580 (extensions
581 (list (service-extension system-service-type
582 packages->profile-entry)))
583 (compose concatenate)
584 (extend append)))
585
586 (define (firmware->activation-gexp firmware)
587 "Return a gexp to make the packages listed in FIRMWARE loadable by the
588 kernel."
589 (let ((directory (directory-union "firmware" firmware)))
590 ;; Tell the kernel where firmware is.
591 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
592
593 (define firmware-service-type
594 ;; The service that collects firmware.
595 (service-type (name 'firmware)
596 (extensions
597 (list (service-extension activation-service-type
598 firmware->activation-gexp)))
599 (compose concatenate)
600 (extend append)))
601
602 (define (gc-roots->system-entry roots)
603 "Return an entry in the system's output containing symlinks to ROOTS."
604 (mlet %store-monad ((entry (gexp->derivation
605 "gc-roots"
606 #~(let ((roots '#$roots))
607 (mkdir #$output)
608 (chdir #$output)
609 (for-each symlink
610 roots
611 (map number->string
612 (iota (length roots))))))))
613 (return (if (null? roots)
614 '()
615 `(("gc-roots" ,entry))))))
616
617 (define gc-root-service-type
618 ;; A service to associate extra garbage-collector roots to the system. This
619 ;; is a simple hack that guarantees that the system retains references to
620 ;; the given list of roots. Roots must be "lowerable" objects like
621 ;; packages, or derivations.
622 (service-type (name 'gc-roots)
623 (extensions
624 (list (service-extension system-service-type
625 gc-roots->system-entry)))
626 (compose concatenate)
627 (extend append)))
628
629 \f
630 ;;;
631 ;;; Service folding.
632 ;;;
633
634 (define-condition-type &missing-target-service-error &service-error
635 missing-target-service-error?
636 (service missing-target-service-error-service)
637 (target-type missing-target-service-error-target-type))
638
639 (define-condition-type &ambiguous-target-service-error &service-error
640 ambiguous-target-service-error?
641 (service ambiguous-target-service-error-service)
642 (target-type ambiguous-target-service-error-target-type))
643
644 (define (service-back-edges services)
645 "Return a procedure that, when passed a <service>, returns the list of
646 <service> objects that depend on it."
647 (define (add-edges service edges)
648 (define (add-edge extension edges)
649 (let ((target-type (service-extension-target extension)))
650 (match (filter (lambda (service)
651 (eq? (service-kind service) target-type))
652 services)
653 ((target)
654 (vhash-consq target service edges))
655 (()
656 (raise
657 (condition (&missing-target-service-error
658 (service service)
659 (target-type target-type))
660 (&message
661 (message
662 (format #f (G_ "no target of type '~a' for service '~a'")
663 (service-type-name target-type)
664 (service-type-name
665 (service-kind service))))))))
666 (x
667 (raise
668 (condition (&ambiguous-target-service-error
669 (service service)
670 (target-type target-type))
671 (&message
672 (message
673 (format #f
674 (G_ "more than one target service of type '~a'")
675 (service-type-name target-type))))))))))
676
677 (fold add-edge edges (service-type-extensions (service-kind service))))
678
679 (let ((edges (fold add-edges vlist-null services)))
680 (lambda (node)
681 (reverse (vhash-foldq* cons '() node edges)))))
682
683 (define* (fold-services services
684 #:key (target-type system-service-type))
685 "Fold SERVICES by propagating their extensions down to the root of type
686 TARGET-TYPE; return the root service adjusted accordingly."
687 (define dependents
688 (service-back-edges services))
689
690 (define (matching-extension target)
691 (let ((target (service-kind target)))
692 (match-lambda
693 (($ <service-extension> type)
694 (eq? type target)))))
695
696 (define (apply-extension target)
697 (lambda (service)
698 (match (find (matching-extension target)
699 (service-type-extensions (service-kind service)))
700 (($ <service-extension> _ compute)
701 (compute (service-value service))))))
702
703 (match (filter (lambda (service)
704 (eq? (service-kind service) target-type))
705 services)
706 ((sink)
707 (let loop ((sink sink))
708 (let* ((dependents (map loop (dependents sink)))
709 (extensions (map (apply-extension sink) dependents))
710 (extend (service-type-extend (service-kind sink)))
711 (compose (service-type-compose (service-kind sink)))
712 (params (service-value sink)))
713 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
714 ;; different type than the elements of EXTENSIONS.
715 (if extend
716 (service (service-kind sink)
717 (extend params (compose extensions)))
718 sink))))
719 (()
720 (raise
721 (condition (&missing-target-service-error
722 (service #f)
723 (target-type target-type))
724 (&message
725 (message (format #f (G_ "service of type '~a' not found")
726 (service-type-name target-type)))))))
727 (x
728 (raise
729 (condition (&ambiguous-target-service-error
730 (service #f)
731 (target-type target-type))
732 (&message
733 (message
734 (format #f
735 (G_ "more than one target service of type '~a'")
736 (service-type-name target-type)))))))))
737
738 ;;; services.scm ends here.