services: web: Add try-files for the nginx-service-type.
[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 (delete-file-recursively "/tmp")
372 (delete-file-recursively "/var/run")
373 (mkdir "/tmp")
374 (chmod "/tmp" #o1777)
375 (mkdir "/var/run")
376 (chmod "/var/run" #o755))))))))
377
378 (define cleanup-service-type
379 ;; Service that cleans things up in /tmp and similar.
380 (service-type (name 'cleanup)
381 (extensions
382 (list (service-extension boot-service-type
383 cleanup-gexp)))))
384
385 (define* (file-union name files) ;FIXME: Factorize.
386 "Return a <computed-file> that builds a directory containing all of FILES.
387 Each item in FILES must be a list where the first element is the file name to
388 use in the new directory, and the second element is a gexp denoting the target
389 file."
390 (computed-file name
391 #~(begin
392 (mkdir #$output)
393 (chdir #$output)
394 #$@(map (match-lambda
395 ((target source)
396 #~(begin
397 ;; Stat the source to abort early if it
398 ;; does not exist.
399 (stat #$source)
400
401 (symlink #$source #$target))))
402 files))))
403
404 (define (directory-union name things)
405 "Return a directory that is the union of THINGS."
406 (match things
407 ((one)
408 ;; Only one thing; return it.
409 one)
410 (_
411 (computed-file name
412 (with-imported-modules '((guix build union))
413 #~(begin
414 (use-modules (guix build union))
415 (union-build #$output '#$things)))))))
416
417 (define* (activation-service->script service)
418 "Return as a monadic value the activation script for SERVICE, a service of
419 ACTIVATION-SCRIPT-TYPE."
420 (activation-script (service-value service)))
421
422 (define (activation-script gexps)
423 "Return the system's activation script, which evaluates GEXPS."
424 (define (service-activations)
425 ;; Return the activation scripts for SERVICES.
426 (mapm %store-monad
427 (cut gexp->file "activate-service" <>)
428 gexps))
429
430 (mlet* %store-monad ((actions (service-activations)))
431 (gexp->file "activate"
432 (with-imported-modules (source-module-closure
433 '((gnu build activation)
434 (guix build utils)))
435 #~(begin
436 (use-modules (gnu build activation)
437 (guix build utils))
438
439 ;; Make sure the user accounting database exists. If it
440 ;; does not exist, 'setutxent' does not create it and
441 ;; thus there is no accounting at all.
442 (close-port (open-file "/var/run/utmpx" "a0"))
443
444 ;; Same for 'wtmp', which is populated by mingetty et
445 ;; al.
446 (mkdir-p "/var/log")
447 (close-port (open-file "/var/log/wtmp" "a0"))
448
449 ;; Set up /run/current-system. Among other things this
450 ;; sets up locales, which the activation snippets
451 ;; executed below may expect.
452 (activate-current-system)
453
454 ;; Run the services' activation snippets.
455 ;; TODO: Use 'load-compiled'.
456 (for-each primitive-load '#$actions))))))
457
458 (define (gexps->activation-gexp gexps)
459 "Return a gexp that runs the activation script containing GEXPS."
460 (mlet %store-monad ((script (activation-script gexps)))
461 (return #~(primitive-load #$script))))
462
463 (define (second-argument a b) b)
464
465 (define activation-service-type
466 (service-type (name 'activate)
467 (extensions
468 (list (service-extension boot-service-type
469 gexps->activation-gexp)))
470 (compose append)
471 (extend second-argument)))
472
473 (define %activation-service
474 ;; The activation service produces the activation script from the gexps it
475 ;; receives.
476 (service activation-service-type #t))
477
478 (define %modprobe-wrapper
479 ;; Wrapper for the 'modprobe' command that knows where modules live.
480 ;;
481 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
482 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
483 ;; environment variable is not set---hence the need for this wrapper.
484 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
485 (program-file "modprobe"
486 #~(begin
487 (setenv "LINUX_MODULE_DIRECTORY"
488 "/run/booted-system/kernel/lib/modules")
489 (apply execl #$modprobe
490 (cons #$modprobe (cdr (command-line))))))))
491
492 (define %linux-kernel-activation
493 ;; Activation of the Linux kernel running on the bare metal (as opposed to
494 ;; running in a container.)
495 #~(begin
496 ;; Tell the kernel to use our 'modprobe' command.
497 (activate-modprobe #$%modprobe-wrapper)
498
499 ;; Let users debug their own processes!
500 (activate-ptrace-attach)))
501
502 (define %linux-bare-metal-service
503 ;; The service that does things that are needed on the "bare metal", but not
504 ;; necessary or impossible in a container.
505 (simple-service 'linux-bare-metal
506 activation-service-type
507 %linux-kernel-activation))
508
509
510 (define special-files-service-type
511 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
512 (service-type
513 (name 'special-files)
514 (extensions
515 (list (service-extension activation-service-type
516 (lambda (files)
517 #~(activate-special-files '#$files)))))
518 (compose concatenate)
519 (extend append)))
520
521 (define (extra-special-file file target)
522 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
523 (file-append coreutils \"/bin/env\")
524 and FILE could be \"/usr/bin/env\"."
525 (simple-service (string->symbol (string-append "special-file-" file))
526 special-files-service-type
527 `((,file ,target))))
528
529 (define (etc-directory service)
530 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
531 (files->etc-directory (service-value service)))
532
533 (define (files->etc-directory files)
534 (file-union "etc" files))
535
536 (define (etc-entry files)
537 "Return an entry for the /etc directory consisting of FILES in the system
538 directory."
539 (with-monad %store-monad
540 (return `(("etc" ,(files->etc-directory files))))))
541
542 (define etc-service-type
543 (service-type (name 'etc)
544 (extensions
545 (list
546 (service-extension activation-service-type
547 (lambda (files)
548 (let ((etc
549 (files->etc-directory files)))
550 #~(activate-etc #$etc))))
551 (service-extension system-service-type etc-entry)))
552 (compose concatenate)
553 (extend append)))
554
555 (define (etc-service files)
556 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
557 FILES must be a list of name/file-like object pairs."
558 (service etc-service-type files))
559
560 (define setuid-program-service-type
561 (service-type (name 'setuid-program)
562 (extensions
563 (list (service-extension activation-service-type
564 (lambda (programs)
565 #~(activate-setuid-programs
566 (list #$@programs))))))
567 (compose concatenate)
568 (extend append)))
569
570 (define (packages->profile-entry packages)
571 "Return a system entry for the profile containing PACKAGES."
572 (mlet %store-monad ((profile (profile-derivation
573 (packages->manifest
574 (delete-duplicates packages eq?)))))
575 (return `(("profile" ,profile)))))
576
577 (define profile-service-type
578 ;; The service that populates the system's profile---i.e.,
579 ;; /run/current-system/profile. It is extended by package lists.
580 (service-type (name 'profile)
581 (extensions
582 (list (service-extension system-service-type
583 packages->profile-entry)))
584 (compose concatenate)
585 (extend append)))
586
587 (define (firmware->activation-gexp firmware)
588 "Return a gexp to make the packages listed in FIRMWARE loadable by the
589 kernel."
590 (let ((directory (directory-union "firmware" firmware)))
591 ;; Tell the kernel where firmware is.
592 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
593
594 (define firmware-service-type
595 ;; The service that collects firmware.
596 (service-type (name 'firmware)
597 (extensions
598 (list (service-extension activation-service-type
599 firmware->activation-gexp)))
600 (compose concatenate)
601 (extend append)))
602
603 (define (gc-roots->system-entry roots)
604 "Return an entry in the system's output containing symlinks to ROOTS."
605 (mlet %store-monad ((entry (gexp->derivation
606 "gc-roots"
607 #~(let ((roots '#$roots))
608 (mkdir #$output)
609 (chdir #$output)
610 (for-each symlink
611 roots
612 (map number->string
613 (iota (length roots))))))))
614 (return (if (null? roots)
615 '()
616 `(("gc-roots" ,entry))))))
617
618 (define gc-root-service-type
619 ;; A service to associate extra garbage-collector roots to the system. This
620 ;; is a simple hack that guarantees that the system retains references to
621 ;; the given list of roots. Roots must be "lowerable" objects like
622 ;; packages, or derivations.
623 (service-type (name 'gc-roots)
624 (extensions
625 (list (service-extension system-service-type
626 gc-roots->system-entry)))
627 (compose concatenate)
628 (extend append)))
629
630 \f
631 ;;;
632 ;;; Service folding.
633 ;;;
634
635 (define-condition-type &missing-target-service-error &service-error
636 missing-target-service-error?
637 (service missing-target-service-error-service)
638 (target-type missing-target-service-error-target-type))
639
640 (define-condition-type &ambiguous-target-service-error &service-error
641 ambiguous-target-service-error?
642 (service ambiguous-target-service-error-service)
643 (target-type ambiguous-target-service-error-target-type))
644
645 (define (service-back-edges services)
646 "Return a procedure that, when passed a <service>, returns the list of
647 <service> objects that depend on it."
648 (define (add-edges service edges)
649 (define (add-edge extension edges)
650 (let ((target-type (service-extension-target extension)))
651 (match (filter (lambda (service)
652 (eq? (service-kind service) target-type))
653 services)
654 ((target)
655 (vhash-consq target service edges))
656 (()
657 (raise
658 (condition (&missing-target-service-error
659 (service service)
660 (target-type target-type))
661 (&message
662 (message
663 (format #f (G_ "no target of type '~a' for service '~a'")
664 (service-type-name target-type)
665 (service-type-name
666 (service-kind service))))))))
667 (x
668 (raise
669 (condition (&ambiguous-target-service-error
670 (service service)
671 (target-type target-type))
672 (&message
673 (message
674 (format #f
675 (G_ "more than one target service of type '~a'")
676 (service-type-name target-type))))))))))
677
678 (fold add-edge edges (service-type-extensions (service-kind service))))
679
680 (let ((edges (fold add-edges vlist-null services)))
681 (lambda (node)
682 (reverse (vhash-foldq* cons '() node edges)))))
683
684 (define* (fold-services services
685 #:key (target-type system-service-type))
686 "Fold SERVICES by propagating their extensions down to the root of type
687 TARGET-TYPE; return the root service adjusted accordingly."
688 (define dependents
689 (service-back-edges services))
690
691 (define (matching-extension target)
692 (let ((target (service-kind target)))
693 (match-lambda
694 (($ <service-extension> type)
695 (eq? type target)))))
696
697 (define (apply-extension target)
698 (lambda (service)
699 (match (find (matching-extension target)
700 (service-type-extensions (service-kind service)))
701 (($ <service-extension> _ compute)
702 (compute (service-value service))))))
703
704 (match (filter (lambda (service)
705 (eq? (service-kind service) target-type))
706 services)
707 ((sink)
708 (let loop ((sink sink))
709 (let* ((dependents (map loop (dependents sink)))
710 (extensions (map (apply-extension sink) dependents))
711 (extend (service-type-extend (service-kind sink)))
712 (compose (service-type-compose (service-kind sink)))
713 (params (service-value sink)))
714 ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
715 ;; different type than the elements of EXTENSIONS.
716 (if extend
717 (service (service-kind sink)
718 (extend params (compose extensions)))
719 sink))))
720 (()
721 (raise
722 (condition (&missing-target-service-error
723 (service #f)
724 (target-type target-type))
725 (&message
726 (message (format #f (G_ "service of type '~a' not found")
727 (service-type-name target-type)))))))
728 (x
729 (raise
730 (condition (&ambiguous-target-service-error
731 (service #f)
732 (target-type target-type))
733 (&message
734 (message
735 (format #f
736 (G_ "more than one target service of type '~a'")
737 (service-type-name target-type)))))))))
738
739 ;;; services.scm ends here.