gnu: Remove unneeded uses of 'libiconv'.
[jackhill/guix/guix.git] / gnu / services.scm
CommitLineData
db4fdc04 1;;; GNU Guix --- Functional package management for GNU
e0bd47b4 2;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
4d343a14 3;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
59bcffa3 4;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
a247f5c7 5;;; Copyright © 2020, 2021 Ricardo Wurmus <rekado@elephly.net>
a3df3825 6;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
a7ac1985
CLW
7;;; Copyright © 2020 Christine Lemmer-Webber <cwebber@dustycloud.org>
8;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
db4fdc04
LC
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (gnu services)
bebc8681 26 #:use-module (guix gexp)
0adfe95a
LC
27 #:use-module (guix monads)
28 #:use-module (guix store)
db4fdc04 29 #:use-module (guix records)
af4c3fd5 30 #:use-module (guix profiles)
0c0c1b21 31 #:use-module (guix discovery)
d466b1fc 32 #:use-module (guix combinators)
33b7cb7a
LC
33 #:use-module (guix channels)
34 #:use-module (guix describe)
0adfe95a
LC
35 #:use-module (guix sets)
36 #:use-module (guix ui)
d51bfe24 37 #:use-module (guix diagnostics)
eb5cf39e 38 #:autoload (guix openpgp) (openpgp-format-fingerprint)
232ccbef 39 #:use-module (guix modules)
a3df3825 40 #:use-module (guix packages)
41 #:use-module (guix utils)
0adfe95a
LC
42 #:use-module (gnu packages base)
43 #:use-module (gnu packages bash)
59bcffa3 44 #:use-module (gnu packages hurd)
a7ac1985 45 #:use-module (gnu system setuid)
0adfe95a
LC
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-9)
48 #:use-module (srfi srfi-9 gnu)
49 #:use-module (srfi srfi-26)
50 #:use-module (srfi srfi-34)
51 #:use-module (srfi srfi-35)
52 #:use-module (ice-9 vlist)
53 #:use-module (ice-9 match)
33b7cb7a 54 #:autoload (ice-9 pretty-print) (pretty-print)
0adfe95a
LC
55 #:export (service-extension
56 service-extension?
7d8b5913
CB
57 service-extension-target
58 service-extension-compute
0adfe95a
LC
59
60 service-type
61 service-type?
5152d13b
LC
62 service-type-name
63 service-type-extensions
64 service-type-compose
65 service-type-extend
1bb895ea 66 service-type-default-value
b714395a
LC
67 service-type-description
68 service-type-location
69
0c0c1b21
LC
70 %service-type-path
71 fold-service-types
49483f71 72 lookup-service-types
0adfe95a 73
db4fdc04 74 service
0adfe95a
LC
75 service?
76 service-kind
efe7d19a
LC
77 service-value
78 service-parameters ;deprecated
0adfe95a 79
71654dfd 80 simple-service
cd6f6c22 81 modify-services
5152d13b 82 service-back-edges
d466b1fc 83 instantiate-missing-services
0adfe95a
LC
84 fold-services
85
86 service-error?
1bb895ea
LC
87 missing-value-service-error?
88 missing-value-service-error-type
89 missing-value-service-error-location
0adfe95a
LC
90 missing-target-service-error?
91 missing-target-service-error-service
92 missing-target-service-error-target-type
93 ambiguous-target-service-error?
94 ambiguous-target-service-error-service
95 ambiguous-target-service-error-target-type
96
d62e201c 97 system-service-type
33b7cb7a 98 provenance-service-type
a396dd01 99 sexp->system-provenance
b91a73a6 100 system-provenance
0adfe95a 101 boot-service-type
be7be9e8 102 cleanup-service-type
0adfe95a
LC
103 activation-service-type
104 activation-service->script
a241a7ac 105 %linux-bare-metal-service
68d8c094
JN
106 %hurd-rc-script
107 %hurd-startup-service
387e1754
LC
108 special-files-service-type
109 extra-special-file
0adfe95a
LC
110 etc-service-type
111 etc-directory
112 setuid-program-service-type
af4c3fd5 113 profile-service-type
0adfe95a 114 firmware-service-type
e0b47290 115 gc-root-service-type
a3df3825 116 linux-builder-service-type
117 linux-builder-configuration
118 linux-builder-configuration?
119 linux-builder-configuration-kernel
120 linux-builder-configuration-modules
121 linux-loadable-module-service-type
0adfe95a
LC
122
123 %boot-service
124 %activation-service
a247f5c7
RW
125 etc-service)
126 #:re-export (;; Note: Re-export 'delete' to allow for proper syntax matching
127 ;; in 'modify-services' forms. See
128 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26805#16>.
129 delete))
0adfe95a
LC
130
131;;; Comment:
132;;;
133;;; This module defines a broad notion of "service types" and "services."
db4fdc04 134;;;
0adfe95a
LC
135;;; A service type describe how its instances extend instances of other
136;;; service types. For instance, some services extend the instance of
137;;; ACCOUNT-SERVICE-TYPE by providing it with accounts and groups to create;
d4053c71
AK
138;;; others extend SHEPHERD-ROOT-SERVICE-TYPE by passing it instances of
139;;; <shepherd-service>.
0adfe95a
LC
140;;;
141;;; When applicable, the service type defines how it can itself be extended,
142;;; by providing one procedure to compose extensions, and one procedure to
143;;; extend itself.
144;;;
d62e201c
LC
145;;; A notable service type is SYSTEM-SERVICE-TYPE, which has a single
146;;; instance, which is the root of the service DAG. Its value is the
147;;; derivation that produces the 'system' directory as returned by
148;;; 'operating-system-derivation'.
0adfe95a
LC
149;;;
150;;; The 'fold-services' procedure can be passed a list of procedures, which it
151;;; "folds" by propagating extensions down the graph; it returns the root
152;;; service after the applying all its extensions.
db4fdc04
LC
153;;;
154;;; Code:
155
0adfe95a
LC
156(define-record-type <service-extension>
157 (service-extension target compute)
158 service-extension?
159 (target service-extension-target) ;<service-type>
160 (compute service-extension-compute)) ;params -> params
161
1bb895ea
LC
162(define &no-default-value
163 ;; Value used to denote service types that have no associated default value.
164 '(no default value))
165
0adfe95a
LC
166(define-record-type* <service-type> service-type make-service-type
167 service-type?
168 (name service-type-name) ;symbol (for debugging)
169
170 ;; Things extended by services of this type.
171 (extensions service-type-extensions) ;list of <service-extensions>
172
173 ;; Given a list of extensions, "compose" them.
174 (compose service-type-compose ;list of Any -> Any
175 (default #f))
176
177 ;; Extend the services' own parameters with the extension composition.
178 (extend service-type-extend ;list of Any -> parameters
1bb895ea
LC
179 (default #f))
180
181 ;; Optional default value for instances of this type.
182 (default-value service-type-default-value ;Any
b714395a
LC
183 (default &no-default-value))
184
185 ;; Meta-data.
186 (description service-type-description ;string
187 (default #f))
188 (location service-type-location ;<location>
189 (default (and=> (current-source-location)
190 source-properties->location))
191 (innate)))
0adfe95a
LC
192
193(define (write-service-type type port)
194 (format port "#<service-type ~a ~a>"
195 (service-type-name type)
196 (number->string (object-address type) 16)))
197
198(set-record-type-printer! <service-type> write-service-type)
199
0c0c1b21
LC
200(define %distro-root-directory
201 ;; Absolute file name of the module hierarchy.
202 (dirname (search-path %load-path "guix.scm")))
203
204(define %service-type-path
205 ;; Search path for service types.
206 (make-parameter `((,%distro-root-directory . "gnu/services")
207 (,%distro-root-directory . "gnu/system"))))
208
3943913f
LC
209(define (all-service-modules)
210 "Return the default set of service modules."
211 (cons (resolve-interface '(gnu services))
3c0128b0
LC
212 (all-modules (%service-type-path)
213 #:warn warn-about-load-error)))
3943913f 214
0c0c1b21
LC
215(define* (fold-service-types proc seed
216 #:optional
3943913f 217 (modules (all-service-modules)))
0c0c1b21
LC
218 "For each service type exported by one of MODULES, call (PROC RESULT). SEED
219is used as the initial value of RESULT."
220 (fold-module-public-variables (lambda (object result)
221 (if (service-type? object)
222 (proc object result)
223 result))
a3d37f3a 224 seed
0c0c1b21
LC
225 modules))
226
49483f71
LC
227(define lookup-service-types
228 (let ((table
229 (delay (fold-service-types (lambda (type result)
230 (vhash-consq (service-type-name type)
231 type result))
232 vlist-null))))
233 (lambda (name)
234 "Return the list of services with the given NAME (a symbol)."
235 (vhash-foldq* cons '() name (force table)))))
236
0adfe95a
LC
237;; Services of a given type.
238(define-record-type <service>
1bb895ea 239 (make-service type value)
db4fdc04 240 service?
0adfe95a 241 (type service-kind)
efe7d19a
LC
242 (value service-value))
243
1bb895ea
LC
244(define-syntax service
245 (syntax-rules ()
246 "Return a service instance of TYPE. The service value is VALUE or, if
247omitted, TYPE's default value."
248 ((_ type value)
249 (make-service type value))
250 ((_ type)
251 (%service-with-default-value (current-source-location)
252 type))))
253
254(define (%service-with-default-value location type)
255 "Return a instance of service type TYPE with its default value, if any. If
256TYPE does not have a default value, an error is raised."
257 ;; TODO: Currently this is a run-time error but with a little bit macrology
258 ;; we could turn it into an expansion-time error.
259 (let ((default (service-type-default-value type)))
260 (if (eq? default &no-default-value)
261 (let ((location (source-properties->location location)))
262 (raise
d51bfe24
LC
263 (make-compound-condition
264 (condition
265 (&missing-value-service-error (type type) (location location)))
266 (formatted-message (G_ "~a: no value specified \
1bb895ea 267for service of type '~a'")
d51bfe24
LC
268 (location->string location)
269 (service-type-name type)))))
1bb895ea
LC
270 (service type default))))
271
272(define-condition-type &service-error &error
273 service-error?)
274
275(define-condition-type &missing-value-service-error &service-error
276 missing-value-service-error?
277 (type missing-value-service-error-type)
278 (location missing-value-service-error-location))
279
280
281\f
282;;;
283;;; Helpers.
284;;;
285
efe7d19a
LC
286(define service-parameters
287 ;; Deprecated alias.
288 service-value)
0adfe95a 289
71654dfd
LC
290(define (simple-service name target value)
291 "Return a service that extends TARGET with VALUE. This works by creating a
292singleton service type NAME, of which the returned service is an instance."
293 (let* ((extension (service-extension target identity))
294 (type (service-type (name name)
295 (extensions (list extension)))))
296 (service type value)))
0adfe95a 297
cd6f6c22 298(define-syntax %modify-service
a247f5c7
RW
299 (syntax-rules (=> delete)
300 ((_ svc (delete kind) clauses ...)
301 (if (eq? (service-kind svc) kind)
302 #f
303 (%modify-service svc clauses ...)))
cd6f6c22
LC
304 ((_ service)
305 service)
306 ((_ svc (kind param => exp ...) clauses ...)
307 (if (eq? (service-kind svc) kind)
efe7d19a 308 (let ((param (service-value svc)))
cd6f6c22
LC
309 (service (service-kind svc)
310 (begin exp ...)))
311 (%modify-service svc clauses ...)))))
312
313(define-syntax modify-services
314 (syntax-rules ()
4d343a14
CM
315 "Modify the services listed in SERVICES according to CLAUSES and return
316the resulting list of services. Each clause must have the form:
cd6f6c22
LC
317
318 (TYPE VARIABLE => BODY)
319
320where TYPE is a service type, such as 'guix-service-type', and VARIABLE is an
321identifier that is bound within BODY to the value of the service of that
61ad9bc2
LC
322TYPE.
323
324Clauses can also remove services of a given type:
325
326 (delete TYPE)
327
328Consider this example:
cd6f6c22
LC
329
330 (modify-services %base-services
331 (guix-service-type config =>
332 (guix-configuration
333 (inherit config)
334 (use-substitutes? #f)
335 (extra-options '(\"--gc-keep-derivations\"))))
336 (mingetty-service-type config =>
337 (mingetty-configuration
338 (inherit config)
a247f5c7
RW
339 (motd (plain-file \"motd\" \"Hi there!\"))))
340 (delete udev-service-type))
cd6f6c22
LC
341
342It changes the configuration of the GUIX-SERVICE-TYPE instance, and that of
a247f5c7
RW
343all the MINGETTY-SERVICE-TYPE instances, and it deletes instances of the
344UDEV-SERVICE-TYPE.
cd6f6c22 345
a247f5c7 346This is a shorthand for (filter-map (lambda (svc) ...) %base-services)."
cd6f6c22 347 ((_ services clauses ...)
a247f5c7
RW
348 (filter-map (lambda (service)
349 (%modify-service service clauses ...))
350 services))))
0adfe95a
LC
351
352\f
353;;;
354;;; Core services.
355;;;
356
0e5c2d5e 357(define (system-derivation entries mextensions)
d62e201c
LC
358 "Return as a monadic value the derivation of the 'system' directory
359containing the given entries."
0e5c2d5e 360 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
223ede4e 361 mextensions)))
d62e201c
LC
362 (lower-object
363 (file-union "system"
364 (append entries (concatenate extensions))))))
365
366(define system-service-type
367 ;; This is the ultimate service type, the root of the service DAG. The
368 ;; service of this type is extended by monadic name/item pairs. These items
369 ;; end up in the "system directory" as returned by
370 ;; 'operating-system-derivation'.
371 (service-type (name 'system)
372 (extensions '())
373 (compose identity)
636bb2b5
LC
374 (extend system-derivation)
375 (description
376 "Build the operating system top-level directory, which in
377turn refers to everything the operating system needs: its kernel, initrd,
378system profile, boot script, and so on.")))
d62e201c 379
378daa8c
LC
380(define (compute-boot-script _ gexps)
381 ;; Reverse GEXPS so that extensions appear in the boot script in the right
661c237b
LC
382 ;; order. That is, user extensions would come first, and extensions added
383 ;; by 'essential-services' (e.g., running shepherd) are guaranteed to come
384 ;; last.
378daa8c
LC
385 (gexp->file "boot"
386 ;; Clean up and activate the system, then spawn shepherd.
387 #~(begin #$@(reverse gexps))))
0adfe95a 388
d62e201c
LC
389(define (boot-script-entry mboot)
390 "Return, as a monadic value, an entry for the boot script in the system
391directory."
392 (mlet %store-monad ((boot mboot))
393 (return `(("boot" ,boot)))))
394
0adfe95a 395(define boot-service-type
378daa8c
LC
396 ;; The service of this type is extended by being passed gexps. It
397 ;; aggregates them in a single script, as a monadic value, which becomes its
398 ;; value.
0adfe95a 399 (service-type (name 'boot)
d62e201c
LC
400 (extensions
401 (list (service-extension system-service-type
402 boot-script-entry)))
7874e9e0 403 (compose identity)
636bb2b5
LC
404 (extend compute-boot-script)
405 (description
406 "Produce the operating system's boot script, which is spawned
407by the initrd once the root file system is mounted.")))
0adfe95a
LC
408
409(define %boot-service
d62e201c 410 ;; The service that produces the boot script.
0adfe95a 411 (service boot-service-type #t))
be7be9e8 412
33b7cb7a
LC
413\f
414;;;
415;;; Provenance tracking.
416;;;
417
418(define (object->pretty-string obj)
419 "Like 'object->string', but using 'pretty-print'."
420 (call-with-output-string
421 (lambda (port)
422 (pretty-print obj port))))
423
424(define (channel->code channel)
425 "Return code to build CHANNEL, ready to be dropped in a 'channels.scm'
426file."
eb5cf39e
LC
427 ;; Since the 'introduction' field is backward-incompatible, and since it's
428 ;; optional when using the "official" 'guix channel, include it if and only
429 ;; if we're referring to a different channel.
430 (let ((intro (and (not (equal? (list channel) %default-channels))
431 (channel-introduction channel))))
432 `(channel (name ',(channel-name channel))
433 (url ,(channel-url channel))
434 (branch ,(channel-branch channel))
435 (commit ,(channel-commit channel))
436 ,@(if intro
437 `((introduction
438 (make-channel-introduction
439 ,(channel-introduction-first-signed-commit intro)
440 (openpgp-fingerprint
441 ,(openpgp-format-fingerprint
442 (channel-introduction-first-commit-signer
443 intro))))))
444 '()))))
33b7cb7a
LC
445
446(define (channel->sexp channel)
447 "Return an sexp describing CHANNEL. The sexp is _not_ code and is meant to
448be parsed by tools; it's potentially more future-proof than code."
eb5cf39e
LC
449 ;; TODO: Add CHANNEL's introduction. Currently we can't do that because
450 ;; older 'guix system describe' expect exactly name/url/branch/commit
451 ;; without any additional fields.
33b7cb7a
LC
452 `(channel (name ,(channel-name channel))
453 (url ,(channel-url channel))
454 (branch ,(channel-branch channel))
455 (commit ,(channel-commit channel))))
456
b91a73a6
LC
457(define (sexp->channel sexp)
458 "Return the channel corresponding to SEXP, an sexp as found in the
459\"provenance\" file produced by 'provenance-service-type'."
460 (match sexp
461 (('channel ('name name)
462 ('url url)
463 ('branch branch)
464 ('commit commit)
465 rest ...)
466 ;; XXX: In the future REST may include a channel introduction.
467 (channel (name name) (url url)
468 (branch branch) (commit commit)))))
469
33b7cb7a
LC
470(define (provenance-file channels config-file)
471 "Return a 'provenance' file describing CHANNELS, a list of channels, and
472CONFIG-FILE, which can be either #f or a <local-file> containing the OS
473configuration being used."
474 (scheme-file "provenance"
475 #~(provenance
476 (version 0)
477 (channels #+@(if channels
478 (map channel->sexp channels)
479 '()))
480 (configuration-file #+config-file))))
481
482(define (provenance-entry config-file)
483 "Return system entries describing the operating system provenance: the
484channels in use and CONFIG-FILE, if it is true."
485 (define profile
486 (current-profile))
487
488 (define channels
489 (and=> profile profile-channels))
490
491 (mbegin %store-monad
492 (let ((config-file (cond ((string? config-file)
5dbfdf8b
LC
493 ;; CONFIG-FILE has been passed typically via
494 ;; 'guix system reconfigure CONFIG-FILE' so we
495 ;; can assume it's valid: tell 'local-file' to
496 ;; not emit a warning.
497 (local-file (assume-valid-file-name config-file)
498 "configuration.scm"))
33b7cb7a
LC
499 ((not config-file)
500 #f)
501 (else
502 config-file))))
503 (return `(("provenance" ,(provenance-file channels config-file))
504 ,@(if channels
505 `(("channels.scm"
506 ,(plain-file "channels.scm"
507 (object->pretty-string
508 `(list
509 ,@(map channel->code channels))))))
510 '())
511 ,@(if config-file
512 `(("configuration.scm" ,config-file))
513 '()))))))
514
515(define provenance-service-type
516 (service-type (name 'provenance)
517 (extensions
518 (list (service-extension system-service-type
519 provenance-entry)))
520 (default-value #f) ;the OS config file
521 (description
522 "Store provenance information about the system in the system
523itself: the channels used when building the system, and its configuration
524file, when available.")))
525
a396dd01
LC
526(define (sexp->system-provenance sexp)
527 "Parse SEXP, an s-expression read from /run/current-system/provenance or
528similar, and return two values: the list of channels listed therein, and the
529OS configuration file or #f."
530 (match sexp
531 (('provenance ('version 0)
532 ('channels channels ...)
533 ('configuration-file config-file))
534 (values (map sexp->channel channels)
535 config-file))
536 (_
537 (values '() #f))))
538
b91a73a6
LC
539(define (system-provenance system)
540 "Given SYSTEM, the file name of a system generation, return two values: the
541list of channels SYSTEM is built from, and its configuration file. If that
542information is missing, return the empty list (for channels) and possibly
543#false (for the configuration file)."
544 (catch 'system-error
545 (lambda ()
a396dd01
LC
546 (sexp->system-provenance
547 (call-with-input-file (string-append system "/provenance")
548 read)))
b91a73a6
LC
549 (lambda _
550 (values '() #f))))
33b7cb7a
LC
551\f
552;;;
553;;; Cleanup.
554;;;
555
be7be9e8 556(define (cleanup-gexp _)
378daa8c
LC
557 "Return a gexp to clean up /tmp and similar places upon boot."
558 (with-imported-modules '((guix build utils))
559 #~(begin
560 (use-modules (guix build utils))
561
562 ;; Clean out /tmp and /var/run.
563 ;;
564 ;; XXX This needs to happen before service activations, so it
565 ;; has to be here, but this also implicitly assumes that /tmp
566 ;; and /var/run are on the root partition.
567 (letrec-syntax ((fail-safe (syntax-rules ()
568 ((_ exp rest ...)
569 (begin
570 (catch 'system-error
571 (lambda () exp)
572 (const #f))
573 (fail-safe rest ...)))
574 ((_)
575 #t))))
576 ;; Ignore I/O errors so the system can boot.
577 (fail-safe
578 ;; Remove stale Shadow lock files as they would lead to
579 ;; failures of 'useradd' & co.
580 (delete-file "/etc/group.lock")
581 (delete-file "/etc/passwd.lock")
582 (delete-file "/etc/.pwd.lock") ;from 'lckpwdf'
583
584 ;; Force file names to be decoded as UTF-8. See
585 ;; <https://bugs.gnu.org/26353>.
586 (setenv "GUIX_LOCPATH"
587 #+(file-append glibc-utf8-locales "/lib/locale"))
588 (setlocale LC_CTYPE "en_US.utf8")
589 (delete-file-recursively "/tmp")
590 (delete-file-recursively "/var/run")
591
592 (mkdir "/tmp")
593 (chmod "/tmp" #o1777)
594 (mkdir "/var/run")
595 (chmod "/var/run" #o755)
596 (delete-file-recursively "/run/udev/watch.old"))))))
be7be9e8
LC
597
598(define cleanup-service-type
599 ;; Service that cleans things up in /tmp and similar.
600 (service-type (name 'cleanup)
601 (extensions
602 (list (service-extension boot-service-type
636bb2b5
LC
603 cleanup-gexp)))
604 (description
605 "Delete files from @file{/tmp}, @file{/var/run}, and other
606temporary locations at boot time.")))
0adfe95a 607
0adfe95a
LC
608(define* (activation-service->script service)
609 "Return as a monadic value the activation script for SERVICE, a service of
610ACTIVATION-SCRIPT-TYPE."
efe7d19a 611 (activation-script (service-value service)))
0adfe95a
LC
612
613(define (activation-script gexps)
614 "Return the system's activation script, which evaluates GEXPS."
378daa8c 615 (define actions
03cbd94d
JK
616 (map (cut program-file "activate-service.scm" <>) gexps))
617
618 (program-file "activate.scm"
619 (with-imported-modules (source-module-closure
620 '((gnu build activation)
621 (guix build utils)))
dac7dd1b
MO
622 #~(begin
623 (use-modules (gnu build activation)
624 (guix build utils))
625
626 ;; Make sure the user accounting database exists. If it
627 ;; does not exist, 'setutxent' does not create it and
628 ;; thus there is no accounting at all.
629 (close-port (open-file "/var/run/utmpx" "a0"))
630
631 ;; Same for 'wtmp', which is populated by mingetty et
632 ;; al.
633 (mkdir-p "/var/log")
634 (close-port (open-file "/var/log/wtmp" "a0"))
635
636 ;; Set up /run/current-system. Among other things this
637 ;; sets up locales, which the activation snippets
638 ;; executed below may expect.
639 (activate-current-system)
640
641 ;; Run the services' activation snippets.
642 ;; TODO: Use 'load-compiled'.
643 (for-each primitive-load '#$actions)))))
0adfe95a
LC
644
645(define (gexps->activation-gexp gexps)
646 "Return a gexp that runs the activation script containing GEXPS."
378daa8c 647 #~(primitive-load #$(activation-script gexps)))
0adfe95a 648
df138dc2
BW
649(define (activation-profile-entry gexps)
650 "Return, as a monadic value, an entry for the activation script in the
651system directory."
652 (mlet %store-monad ((activate (lower-object (activation-script gexps))))
653 (return `(("activate" ,activate)))))
654
3a391e68
LC
655(define (second-argument a b) b)
656
0adfe95a
LC
657(define activation-service-type
658 (service-type (name 'activate)
659 (extensions
660 (list (service-extension boot-service-type
df138dc2
BW
661 gexps->activation-gexp)
662 (service-extension system-service-type
663 activation-profile-entry)))
7874e9e0 664 (compose identity)
636bb2b5
LC
665 (extend second-argument)
666 (description
667 "Run @dfn{activation} code at boot time and upon
668@command{guix system reconfigure} completion.")))
0adfe95a
LC
669
670(define %activation-service
671 ;; The activation service produces the activation script from the gexps it
672 ;; receives.
673 (service activation-service-type #t))
674
a241a7ac
LC
675(define %modprobe-wrapper
676 ;; Wrapper for the 'modprobe' command that knows where modules live.
677 ;;
678 ;; This wrapper is typically invoked by the Linux kernel ('call_modprobe',
679 ;; in kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY'
680 ;; environment variable is not set---hence the need for this wrapper.
681 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
682 (program-file "modprobe"
683 #~(begin
684 (setenv "LINUX_MODULE_DIRECTORY"
685 "/run/booted-system/kernel/lib/modules")
8c88e242
BW
686 ;; FIXME: Remove this crutch when the patch #40422,
687 ;; updating to kmod 27 is merged.
688 (setenv "MODPROBE_OPTIONS"
689 "-C /etc/modprobe.d")
a241a7ac
LC
690 (apply execl #$modprobe
691 (cons #$modprobe (cdr (command-line))))))))
692
693(define %linux-kernel-activation
694 ;; Activation of the Linux kernel running on the bare metal (as opposed to
695 ;; running in a container.)
696 #~(begin
697 ;; Tell the kernel to use our 'modprobe' command.
698 (activate-modprobe #$%modprobe-wrapper)
699
700 ;; Let users debug their own processes!
701 (activate-ptrace-attach)))
702
a241a7ac
LC
703(define %linux-bare-metal-service
704 ;; The service that does things that are needed on the "bare metal", but not
705 ;; necessary or impossible in a container.
6ddf4fcf
LC
706 (simple-service 'linux-bare-metal
707 activation-service-type
708 %linux-kernel-activation))
709
68d8c094
JN
710(define %hurd-rc-script
711 ;; The RC script to be started upon boot.
712 (program-file "rc"
713 (with-imported-modules (source-module-closure
714 '((guix build utils)
715 (gnu build hurd-boot)
716 (guix build syscalls)))
717 #~(begin
718 (use-modules (guix build utils)
719 (gnu build hurd-boot)
720 (guix build syscalls)
721 (ice-9 match)
722 (system repl repl)
723 (srfi srfi-1)
724 (srfi srfi-26))
725 (boot-hurd-system)))))
726
727(define (hurd-rc-entry rc)
728 "Return, as a monadic value, an entry for the RC script in the system
729directory."
730 (mlet %store-monad ((rc (lower-object rc)))
731 (return `(("rc" ,rc)))))
732
733(define hurd-startup-service-type
734 ;; The service that creates the initial SYSTEM/rc startup file.
735 (service-type (name 'startup)
736 (extensions
737 (list (service-extension system-service-type hurd-rc-entry)))
738 (default-value %hurd-rc-script)))
739
740(define %hurd-startup-service
741 ;; The service that produces the RC script.
742 (service hurd-startup-service-type %hurd-rc-script))
a241a7ac 743
387e1754
LC
744(define special-files-service-type
745 ;; Service to install "special files" such as /bin/sh and /usr/bin/env.
746 (service-type
747 (name 'special-files)
748 (extensions
749 (list (service-extension activation-service-type
750 (lambda (files)
751 #~(activate-special-files '#$files)))))
752 (compose concatenate)
636bb2b5
LC
753 (extend append)
754 (description
755 "Add special files to the root file system---e.g.,
756@file{/usr/bin/env}.")))
387e1754
LC
757
758(define (extra-special-file file target)
759 "Use TARGET as the \"special file\" FILE. For example, TARGET might be
760 (file-append coreutils \"/bin/env\")
761and FILE could be \"/usr/bin/env\"."
762 (simple-service (string->symbol (string-append "special-file-" file))
763 special-files-service-type
764 `((,file ,target))))
765
0adfe95a
LC
766(define (etc-directory service)
767 "Return the directory for SERVICE, a service of type ETC-SERVICE-TYPE."
efe7d19a 768 (files->etc-directory (service-value service)))
0adfe95a
LC
769
770(define (files->etc-directory files)
a322e9d1
LC
771 (define (assert-no-duplicates files)
772 (let loop ((files files)
773 (seen (set)))
774 (match files
775 (() #t)
776 (((file _) rest ...)
777 (when (set-contains? seen file)
d51bfe24
LC
778 (raise (formatted-message (G_ "duplicate '~a' entry for /etc")
779 file)))
a322e9d1
LC
780 (loop rest (set-insert file seen))))))
781
782 ;; Detect duplicates early instead of letting them through, eventually
783 ;; leading to a build failure of "etc.drv".
784 (assert-no-duplicates files)
785
0adfe95a
LC
786 (file-union "etc" files))
787
d62e201c
LC
788(define (etc-entry files)
789 "Return an entry for the /etc directory consisting of FILES in the system
790directory."
791 (with-monad %store-monad
792 (return `(("etc" ,(files->etc-directory files))))))
793
0adfe95a
LC
794(define etc-service-type
795 (service-type (name 'etc)
796 (extensions
797 (list
798 (service-extension activation-service-type
799 (lambda (files)
800 (let ((etc
801 (files->etc-directory files)))
d62e201c
LC
802 #~(activate-etc #$etc))))
803 (service-extension system-service-type etc-entry)))
0adfe95a 804 (compose concatenate)
636bb2b5
LC
805 (extend append)
806 (description "Populate the @file{/etc} directory.")))
0adfe95a
LC
807
808(define (etc-service files)
809 "Return a new service of ETC-SERVICE-TYPE that populates /etc with FILES.
810FILES must be a list of name/file-like object pairs."
811 (service etc-service-type files))
812
a7ac1985
CLW
813(define (setuid-program->activation-gexp programs)
814 "Return an activation gexp for setuid-program from PROGRAMS."
815 (let ((programs (map (lambda (program)
816 ;; FIXME This is really ugly, I didn't managed to use
817 ;; "inherit"
818 (let ((program-name (setuid-program-program program))
819 (setuid? (setuid-program-setuid? program))
820 (setgid? (setuid-program-setgid? program))
821 (user (setuid-program-user program))
822 (group (setuid-program-group program)) )
823 #~(setuid-program
824 (setuid? #$setuid?)
825 (setgid? #$setgid?)
826 (user #$user)
827 (group #$group)
828 (program #$program-name))))
829 programs)))
830 (with-imported-modules (source-module-closure
831 '((gnu system setuid)))
832 #~(begin
833 (use-modules (gnu system setuid))
834
835 (activate-setuid-programs (list #$@programs))))))
836
0adfe95a
LC
837(define setuid-program-service-type
838 (service-type (name 'setuid-program)
839 (extensions
840 (list (service-extension activation-service-type
a7ac1985 841 setuid-program->activation-gexp)))
0adfe95a 842 (compose concatenate)
a7ac1985 843 (extend (lambda (config extensions)
e0bd47b4 844 (append config extensions)))
636bb2b5
LC
845 (description
846 "Populate @file{/run/setuid-programs} with the specified
847executables, making them setuid-root.")))
0adfe95a 848
af4c3fd5
LC
849(define (packages->profile-entry packages)
850 "Return a system entry for the profile containing PACKAGES."
07a33b1d
LC
851 ;; XXX: 'mlet' is needed here for one reason: to get the proper
852 ;; '%current-target' and '%current-target-system' bindings when
853 ;; 'packages->manifest' is called, and thus when the 'package-inputs'
854 ;; etc. procedures are called on PACKAGES. That way, conditionals in those
855 ;; inputs see the "correct" value of these two parameters. See
856 ;; <https://issues.guix.gnu.org/44952>.
857 (mlet %store-monad ((_ (current-target-system)))
45bd9133
LC
858 (return `(("profile" ,(profile
859 (content (packages->manifest
860 (delete-duplicates packages eq?)))))))))
af4c3fd5
LC
861
862(define profile-service-type
863 ;; The service that populates the system's profile---i.e.,
864 ;; /run/current-system/profile. It is extended by package lists.
865 (service-type (name 'profile)
866 (extensions
867 (list (service-extension system-service-type
868 packages->profile-entry)))
869 (compose concatenate)
636bb2b5
LC
870 (extend append)
871 (description
872 "This is the @dfn{system profile}, available as
873@file{/run/current-system/profile}. It contains packages that the sysadmin
874wants to be globally available to all the system users.")))
af4c3fd5 875
0adfe95a
LC
876(define (firmware->activation-gexp firmware)
877 "Return a gexp to make the packages listed in FIRMWARE loadable by the
878kernel."
879 (let ((directory (directory-union "firmware" firmware)))
880 ;; Tell the kernel where firmware is.
881 #~(activate-firmware (string-append #$directory "/lib/firmware"))))
882
883(define firmware-service-type
884 ;; The service that collects firmware.
885 (service-type (name 'firmware)
886 (extensions
887 (list (service-extension activation-service-type
888 firmware->activation-gexp)))
889 (compose concatenate)
636bb2b5
LC
890 (extend append)
891 (description
892 "Make ``firmware'' files loadable by the operating system
893kernel. Firmware may then be uploaded to some of the machine's devices, such
894as Wifi cards.")))
0adfe95a 895
e0b47290
LC
896(define (gc-roots->system-entry roots)
897 "Return an entry in the system's output containing symlinks to ROOTS."
898 (mlet %store-monad ((entry (gexp->derivation
899 "gc-roots"
900 #~(let ((roots '#$roots))
901 (mkdir #$output)
902 (chdir #$output)
903 (for-each symlink
904 roots
905 (map number->string
906 (iota (length roots))))))))
907 (return (if (null? roots)
908 '()
909 `(("gc-roots" ,entry))))))
910
911(define gc-root-service-type
912 ;; A service to associate extra garbage-collector roots to the system. This
913 ;; is a simple hack that guarantees that the system retains references to
914 ;; the given list of roots. Roots must be "lowerable" objects like
915 ;; packages, or derivations.
916 (service-type (name 'gc-roots)
917 (extensions
918 (list (service-extension system-service-type
919 gc-roots->system-entry)))
920 (compose concatenate)
636bb2b5
LC
921 (extend append)
922 (description
923 "Register garbage-collector roots---i.e., store items that
dc5729a8
LC
924will not be reclaimed by the garbage collector.")
925 (default-value '())))
e0b47290 926
a3df3825 927;; Configuration for the Linux kernel builder.
928(define-record-type* <linux-builder-configuration>
929 linux-builder-configuration
930 make-linux-builder-configuration
931 linux-builder-configuration?
932 this-linux-builder-configuration
933
934 (kernel linux-builder-configuration-kernel) ; package
935 (modules linux-builder-configuration-modules (default '()))) ; list of packages
936
937(define (package-for-kernel target-kernel module-package)
938 "Return a package like MODULE-PACKAGE, adapted for TARGET-KERNEL, if
939possible (that is if there's a LINUX keyword argument in the build system)."
940 (package
941 (inherit module-package)
942 (arguments
943 (substitute-keyword-arguments (package-arguments module-package)
944 ((#:linux kernel #f)
945 target-kernel)))))
946
947(define (linux-builder-configuration->system-entry config)
948 "Return the kernel entry of the 'system' directory."
949 (let* ((kernel (linux-builder-configuration-kernel config))
950 (modules (linux-builder-configuration-modules config))
951 (kernel (profile
952 (content (packages->manifest
953 (cons kernel
954 (map (lambda (module)
955 (cond
956 ((package? module)
957 (package-for-kernel kernel module))
958 ;; support (,package "kernel-module-output")
959 ((and (list? module) (package? (car module)))
960 (cons (package-for-kernel kernel
961 (car module))
962 (cdr module)))
963 (else
964 module)))
965 modules))))
966 (hooks (list linux-module-database)))))
967 (with-monad %store-monad
968 (return `(("kernel" ,kernel))))))
969
970(define linux-builder-service-type
971 (service-type (name 'linux-builder)
972 (extensions
973 (list (service-extension system-service-type
974 linux-builder-configuration->system-entry)))
975 (default-value '())
976 (compose identity)
977 (extend (lambda (config modifiers)
978 (if (null? modifiers)
979 config
980 ((apply compose modifiers) config))))
981 (description "Builds the linux-libre kernel profile, containing
982the kernel itself and any linux-loadable kernel modules. This can be extended
983with a function that accepts the current configuration and returns a new
984configuration.")))
985
986(define (linux-loadable-module-builder-modifier modules)
987 "Extends linux-builder-service-type by appending the given MODULES to the
988configuration of linux-builder-service-type."
989 (lambda (config)
990 (linux-builder-configuration
991 (inherit config)
992 (modules (append (linux-builder-configuration-modules config)
993 modules)))))
994
995(define linux-loadable-module-service-type
996 (service-type (name 'linux-loadable-modules)
997 (extensions
998 (list (service-extension linux-builder-service-type
999 linux-loadable-module-builder-modifier)))
1000 (default-value '())
1001 (compose concatenate)
1002 (extend append)
1003 (description "Adds packages and package outputs as modules
1004included in the booted linux-libre profile. Other services can extend this
1005service type to add particular modules to the set of linux-loadable modules.")))
1006
1007
0adfe95a
LC
1008\f
1009;;;
1010;;; Service folding.
1011;;;
1012
0adfe95a
LC
1013(define-condition-type &missing-target-service-error &service-error
1014 missing-target-service-error?
1015 (service missing-target-service-error-service)
1016 (target-type missing-target-service-error-target-type))
1017
1018(define-condition-type &ambiguous-target-service-error &service-error
1019 ambiguous-target-service-error?
1020 (service ambiguous-target-service-error-service)
1021 (target-type ambiguous-target-service-error-target-type))
1022
d466b1fc
LC
1023(define (missing-target-error service target-type)
1024 (raise
1025 (condition (&missing-target-service-error
1026 (service service)
1027 (target-type target-type))
1028 (&message
1029 (message
1030 (format #f (G_ "no target of type '~a' for service '~a'")
1031 (service-type-name target-type)
1032 (service-type-name
1033 (service-kind service))))))))
1034
0adfe95a
LC
1035(define (service-back-edges services)
1036 "Return a procedure that, when passed a <service>, returns the list of
1037<service> objects that depend on it."
1038 (define (add-edges service edges)
1039 (define (add-edge extension edges)
1040 (let ((target-type (service-extension-target extension)))
1041 (match (filter (lambda (service)
1042 (eq? (service-kind service) target-type))
1043 services)
1044 ((target)
1045 (vhash-consq target service edges))
1046 (()
d466b1fc 1047 (missing-target-error service target-type))
0adfe95a
LC
1048 (x
1049 (raise
1050 (condition (&ambiguous-target-service-error
1051 (service service)
1052 (target-type target-type))
1053 (&message
1054 (message
1055 (format #f
69daee23 1056 (G_ "more than one target service of type '~a'")
0adfe95a
LC
1057 (service-type-name target-type))))))))))
1058
1059 (fold add-edge edges (service-type-extensions (service-kind service))))
1060
1061 (let ((edges (fold add-edges vlist-null services)))
1062 (lambda (node)
1063 (reverse (vhash-foldq* cons '() node edges)))))
1064
d466b1fc
LC
1065(define (instantiate-missing-services services)
1066 "Return SERVICES, a list, augmented with any services targeted by extensions
1067and missing from SERVICES. Only service types with a default value can be
1068instantiated; other missing services lead to a
1069'&missing-target-service-error'."
1070 (define (adjust-service-list svc result instances)
1071 (fold2 (lambda (extension result instances)
1072 (define target-type
1073 (service-extension-target extension))
1074
1075 (match (vhash-assq target-type instances)
1076 (#f
1077 (let ((default (service-type-default-value target-type)))
1078 (if (eq? &no-default-value default)
1079 (missing-target-error svc target-type)
1080 (let ((new (service target-type)))
1081 (values (cons new result)
1082 (vhash-consq target-type new instances))))))
1083 (_
1084 (values result instances))))
1085 result
1086 instances
1087 (service-type-extensions (service-kind svc))))
1088
9b6c4355
LC
1089 (let loop ((services services))
1090 (define instances
1091 (fold (lambda (service result)
1092 (vhash-consq (service-kind service) service
1093 result))
1094 vlist-null services))
1095
1096 (define adjusted
1097 (fold2 adjust-service-list
1098 services instances
1099 services))
1100
1101 ;; If we instantiated services, they might in turn depend on missing
1102 ;; services. Loop until we've reached fixed point.
1103 (if (= (length adjusted) (vlist-length instances))
1104 adjusted
1105 (loop adjusted))))
d466b1fc 1106
d62e201c
LC
1107(define* (fold-services services
1108 #:key (target-type system-service-type))
0adfe95a
LC
1109 "Fold SERVICES by propagating their extensions down to the root of type
1110TARGET-TYPE; return the root service adjusted accordingly."
1111 (define dependents
1112 (service-back-edges services))
1113
1114 (define (matching-extension target)
1115 (let ((target (service-kind target)))
1116 (match-lambda
1117 (($ <service-extension> type)
1118 (eq? type target)))))
1119
1120 (define (apply-extension target)
1121 (lambda (service)
1122 (match (find (matching-extension target)
1123 (service-type-extensions (service-kind service)))
1124 (($ <service-extension> _ compute)
efe7d19a 1125 (compute (service-value service))))))
0adfe95a
LC
1126
1127 (match (filter (lambda (service)
1128 (eq? (service-kind service) target-type))
1129 services)
1130 ((sink)
2a4309de
LC
1131 ;; Use the state monad to keep track of already-visited services in the
1132 ;; graph and to memoize their value once folded.
1133 (run-with-state
1134 (let loop ((sink sink))
1135 (mlet %state-monad ((visited (current-state)))
1136 (match (vhash-assq sink visited)
1137 (#f
1138 (mlet* %state-monad
1139 ((dependents (mapm %state-monad loop (dependents sink)))
1140 (visited (current-state))
1141 (extensions -> (map (apply-extension sink) dependents))
1142 (extend -> (service-type-extend (service-kind sink)))
1143 (compose -> (service-type-compose (service-kind sink)))
1144 (params -> (service-value sink))
1145 (service
1146 ->
1147 ;; Distinguish COMPOSE and EXTEND because PARAMS typically
1148 ;; has a different type than the elements of EXTENSIONS.
1149 (if extend
1150 (service (service-kind sink)
1151 (extend params (compose extensions)))
1152 sink)))
1153 (mbegin %state-monad
1154 (set-current-state (vhash-consq sink service visited))
1155 (return service))))
1156 ((_ . service) ;SINK was already visited
1157 (return service)))))
1158 vlist-null))
0adfe95a
LC
1159 (()
1160 (raise
d51bfe24
LC
1161 (make-compound-condition
1162 (condition (&missing-target-service-error
1163 (service #f)
1164 (target-type target-type)))
1165 (formatted-message (G_ "service of type '~a' not found")
1166 (service-type-name target-type)))))
0adfe95a
LC
1167 (x
1168 (raise
1169 (condition (&ambiguous-target-service-error
1170 (service #f)
1171 (target-type target-type))
1172 (&message
1173 (message
1174 (format #f
69daee23 1175 (G_ "more than one target service of type '~a'")
0adfe95a 1176 (service-type-name target-type)))))))))
db4fdc04
LC
1177
1178;;; services.scm ends here.