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