gnu: autojump: Update to 22.5.3.
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
ae7a316b 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
5e738ac2 3;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
5dccaffe 4;;; Copyright © 2015, 2016 Alex Kost <alezost@gmail.com>
1ef8b72a 5;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
c76b3046 6;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5144df2c 7;;; Copyright © 2019 Meiyo Peng <meiyo.peng@gmail.com>
033adfe7
LC
8;;;
9;;; This file is part of GNU Guix.
10;;;
11;;; GNU Guix is free software; you can redistribute it and/or modify it
12;;; under the terms of the GNU General Public License as published by
13;;; the Free Software Foundation; either version 3 of the License, or (at
14;;; your option) any later version.
15;;;
16;;; GNU Guix is distributed in the hope that it will be useful, but
17;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;;; GNU General Public License for more details.
20;;;
21;;; You should have received a copy of the GNU General Public License
22;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24(define-module (gnu system)
bdc61ff9 25 #:use-module (guix inferior)
033adfe7
LC
26 #:use-module (guix store)
27 #:use-module (guix monads)
02100028 28 #:use-module (guix gexp)
033adfe7
LC
29 #:use-module (guix records)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
29fce8b6 32 #:use-module (guix profiles)
84765839 33 #:use-module (guix ui)
033adfe7
LC
34 #:use-module (gnu packages base)
35 #:use-module (gnu packages bash)
bdb36958 36 #:use-module (gnu packages guile)
9de46ffb 37 #:use-module (gnu packages admin)
8a07c289 38 #:use-module (gnu packages linux)
a96a82d7 39 #:use-module (gnu packages pciutils)
033adfe7 40 #:use-module (gnu packages package-management)
6f436c54
LC
41 #:use-module (gnu packages less)
42 #:use-module (gnu packages zile)
55e70e65 43 #:use-module (gnu packages nano)
a68c6967 44 #:use-module (gnu packages gawk)
18316d86 45 #:use-module (gnu packages man)
939c5c31 46 #:use-module (gnu packages texinfo)
a68c6967 47 #:use-module (gnu packages compression)
f34c56be 48 #:use-module (gnu packages firmware)
db4fdc04 49 #:use-module (gnu services)
0190c1c0 50 #:use-module (gnu services shepherd)
db4fdc04 51 #:use-module (gnu services base)
b09a8da4 52 #:use-module (gnu bootloader)
033adfe7 53 #:use-module (gnu system shadow)
996ed739 54 #:use-module (gnu system nss)
598e19dc 55 #:use-module (gnu system locale)
6e828634 56 #:use-module (gnu system pam)
735c6dd7 57 #:use-module (gnu system linux-initrd)
9b336338 58 #:use-module (gnu system uuid)
c5df1839 59 #:use-module (gnu system file-systems)
060d62a7 60 #:use-module (gnu system mapped-devices)
033adfe7
LC
61 #:use-module (ice-9 match)
62 #:use-module (srfi srfi-1)
63 #:use-module (srfi srfi-26)
598e19dc
LC
64 #:use-module (srfi srfi-34)
65 #:use-module (srfi srfi-35)
3382bfe9 66 #:use-module (rnrs bytevectors)
033adfe7
LC
67 #:export (operating-system
68 operating-system?
d8bead6c 69 this-operating-system
d5b429ab
LC
70
71 operating-system-bootloader
033adfe7 72 operating-system-services
69cae3d3 73 operating-system-essential-services
217a5b85 74 operating-system-user-services
033adfe7 75 operating-system-packages
fd3bfc44 76 operating-system-host-name
c65e1834 77 operating-system-hosts-file
fd3bfc44 78 operating-system-kernel
44d5f54e 79 operating-system-kernel-file
33f0aa88 80 operating-system-kernel-arguments
bc499b11 81 operating-system-initrd-modules
fd3bfc44
LC
82 operating-system-initrd
83 operating-system-users
84 operating-system-groups
548d4c13 85 operating-system-issue
fd3bfc44
LC
86 operating-system-timezone
87 operating-system-locale
598e19dc 88 operating-system-locale-definitions
34760ae7 89 operating-system-locale-libcs
5dae0186 90 operating-system-mapped-devices
83bcd0b8 91 operating-system-file-systems
6b779207 92 operating-system-store-file-system
2bdd7ac1
LC
93 operating-system-user-mapped-devices
94 operating-system-boot-mapped-devices
b25937e3 95 operating-system-activation-script
b2fef041
LC
96 operating-system-user-accounts
97 operating-system-shepherd-service-names
20abb8c4 98 operating-system-user-kernel-arguments
033adfe7 99
1aa0033b 100 operating-system-derivation
83bcd0b8 101 operating-system-profile
c76b3046 102 operating-system-bootcfg
239db054
DT
103 operating-system-etc-directory
104 operating-system-locale-directory
105 operating-system-boot-script
106
43fe431c
DC
107 system-linux-image-file-name
108
b8300494
AK
109 boot-parameters
110 boot-parameters?
111 boot-parameters-label
112 boot-parameters-root-device
f96752e3 113 boot-parameters-bootloader-name
1ef8b72a
CM
114 boot-parameters-store-device
115 boot-parameters-store-mount-point
b8300494
AK
116 boot-parameters-kernel
117 boot-parameters-kernel-arguments
d7b342d8 118 boot-parameters-initrd
4e4e0185 119 read-boot-parameters
9530e73b 120 read-boot-parameters-file
1975c754 121 boot-parameters->menu-entry
b8300494 122
568841d4 123 local-host-aliases
d0f3a672 124 %root-account
df5ce088 125 %setuid-programs
5dae0186 126 %base-packages
374f14c2 127 %base-firmware))
033adfe7
LC
128
129;;; Commentary:
130;;;
131;;; This module supports whole-system configuration.
132;;;
133;;; Code:
134
a7ef45d9
LC
135(define (bootable-kernel-arguments system root-device)
136 "Return a list of kernel arguments (gexps) to boot SYSTEM from ROOT-DEVICE."
137 (list (string-append "--root="
138 (cond ((uuid? root-device)
139
140 ;; Note: Always use the DCE format because that's
141 ;; what (gnu build linux-boot) expects for the
142 ;; '--root' kernel command-line option.
143 (uuid->string (uuid-bytevector root-device)
144 'dce))
145 ((file-system-label? root-device)
146 (file-system-label->string root-device))
147 (else root-device)))
148 #~(string-append "--system=" #$system)
149 #~(string-append "--load=" #$system "/boot")))
33f0aa88 150
033adfe7
LC
151;; System-wide configuration.
152;; TODO: Add per-field docstrings/stexi.
153(define-record-type* <operating-system> operating-system
154 make-operating-system
155 operating-system?
d8bead6c
LC
156 this-operating-system
157
033adfe7 158 (kernel operating-system-kernel ; package
57082417 159 (default linux-libre))
af98d25a 160 (kernel-arguments operating-system-user-kernel-arguments
ee2a6304 161 (default '())) ; list of gexps/strings
b09a8da4 162 (bootloader operating-system-bootloader) ; <bootloader-configuration>
d5b429ab 163
ae7a316b
LC
164 (keyboard-layout operating-system-keyboard-layout ;#f | <keyboard-layout>
165 (default #f))
e34ae75d 166 (initrd operating-system-initrd ; (list fs) -> file-like
060238ae 167 (default base-initrd))
bc499b11
LC
168 (initrd-modules operating-system-initrd-modules ; list of strings
169 (thunked) ; it's system-dependent
170 (default %base-initrd-modules))
171
f34c56be
LC
172 (firmware operating-system-firmware ; list of packages
173 (default %base-firmware))
033adfe7
LC
174
175 (host-name operating-system-host-name) ; string
24e02c28 176 (hosts-file operating-system-hosts-file ; file-like | #f
c65e1834 177 (default #f))
033adfe7 178
5dae0186
LC
179 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
180 (default '()))
8a6d2731 181 (file-systems operating-system-file-systems) ; list of fs
2a13d05e
LC
182 (swap-devices operating-system-swap-devices ; list of strings
183 (default '()))
033adfe7
LC
184
185 (users operating-system-users ; list of user accounts
bf87f38a 186 (default %base-user-accounts))
033adfe7 187 (groups operating-system-groups ; list of user groups
773e956d 188 (default %base-groups))
033adfe7 189
ac3c14fb 190 (skeletons operating-system-skeletons ; list of name/file-like value
40281c54 191 (default (default-skeletons)))
548d4c13
LC
192 (issue operating-system-issue ; string
193 (default %default-issue))
40281c54 194
033adfe7 195 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 196 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
197
198 (timezone operating-system-timezone) ; string
8a6d2731 199 (locale operating-system-locale ; string
598e19dc
LC
200 (default "en_US.utf8"))
201 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
202 (default %default-locale-definitions))
34760ae7
LC
203 (locale-libcs operating-system-locale-libcs ; list of <packages>
204 (default %default-locale-libcs))
996ed739
LC
205 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
206 (default %default-nss))
033adfe7 207
69cae3d3
LC
208 (essential-services operating-system-essential-services ; list of services
209 (thunked)
d8bead6c 210 (default (essential-services this-operating-system)))
ac3c14fb 211 (services operating-system-user-services ; list of services
09e028f4
LC
212 (default %base-services))
213
214 (pam-services operating-system-pam-services ; list of PAM services
215 (default (base-pam-services)))
216 (setuid-programs operating-system-setuid-programs
69689380 217 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 218
f5a9ffa0
AK
219 (sudoers-file operating-system-sudoers-file ; file-like
220 (default %sudoers-specification)))
033adfe7 221
a7ef45d9 222(define (operating-system-kernel-arguments os root-device)
33f0aa88
DM
223 "Return all the kernel arguments, including the ones not specified
224directly by the user."
a7ef45d9
LC
225 (append (bootable-kernel-arguments os root-device)
226 (operating-system-user-kernel-arguments os)))
33f0aa88 227
2717a89a 228\f
8e815c5b
LC
229;;;
230;;; Boot parameters
231;;;
232
233(define-record-type* <boot-parameters>
234 boot-parameters make-boot-parameters boot-parameters?
235 (label boot-parameters-label)
236 ;; Because we will use the 'store-device' to create the GRUB search command,
237 ;; the 'store-device' has slightly different semantics than 'root-device'.
238 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
239 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
240 ;; understand that. The 'root-device', on the other hand, corresponds
241 ;; exactly to the device field of the <file-system> object representing the
242 ;; OS's root file system, so it might be a device path like "/dev/sda3".
243 (root-device boot-parameters-root-device)
f96752e3 244 (bootloader-name boot-parameters-bootloader-name)
8e815c5b
LC
245 (store-device boot-parameters-store-device)
246 (store-mount-point boot-parameters-store-mount-point)
247 (kernel boot-parameters-kernel)
248 (kernel-arguments boot-parameters-kernel-arguments)
249 (initrd boot-parameters-initrd))
250
90d23ed9
LC
251(define (ensure-not-/dev device)
252 "If DEVICE starts with a slash, return #f. This is meant to filter out
253Linux device names such as /dev/sda, and to preserve GRUB device names and
254file system labels."
255 (if (and (string? device) (string-prefix? "/" device))
256 #f
257 device))
258
8e815c5b
LC
259(define (read-boot-parameters port)
260 "Read boot parameters from PORT and return the corresponding
261<boot-parameters> object or #f if the format is unrecognized."
075681d3
LC
262 (define device-sexp->device
263 (match-lambda
264 (('uuid (? symbol? type) (? bytevector? bv))
265 (bytevector->uuid bv type))
a5acc17a
LC
266 (('file-system-label (? string? label))
267 (file-system-label label))
075681d3
LC
268 ((? bytevector? bv) ;old format
269 (bytevector->uuid bv 'dce))
270 ((? string? device)
a5acc17a
LC
271 ;; It used to be that we would not distinguish between labels and
272 ;; device names. Try to infer the right thing here.
273 (if (string-prefix? "/dev/" device)
274 device
275 (file-system-label device)))))
075681d3 276
8e815c5b
LC
277 (match (read port)
278 (('boot-parameters ('version 0)
279 ('label label) ('root-device root)
280 ('kernel linux)
281 rest ...)
282 (boot-parameters
283 (label label)
7940188e 284 (root-device (device-sexp->device root))
8e815c5b 285
f96752e3
MO
286 (bootloader-name
287 (match (assq 'bootloader-name rest)
bcaf67c4
MO
288 ((_ args) args)
289 (#f 'grub))) ; for compatibility reasons.
290
8e815c5b
LC
291 ;; In the past, we would store the directory name of the kernel instead
292 ;; of the absolute file name of its image. Detect that and correct it.
293 (kernel (if (string=? linux (direct-store-path linux))
294 (string-append linux "/"
295 (system-linux-image-file-name))
296 linux))
297
298 (kernel-arguments
299 (match (assq 'kernel-arguments rest)
300 ((_ args) args)
301 (#f '()))) ;the old format
302
303 (initrd
304 (match (assq 'initrd rest)
305 (('initrd ('string-append directory file)) ;the old format
306 (string-append directory file))
307 (('initrd (? string? file))
308 file)))
309
310 (store-device
db4e8fd5
LC
311 ;; Linux device names like "/dev/sda1" are not suitable GRUB device
312 ;; identifiers, so we just filter them out.
313 (ensure-not-/dev
314 (match (assq 'store rest)
315 (('store ('device #f) _ ...)
316 root-device)
317 (('store ('device device) _ ...)
318 (device-sexp->device device))
319 (_ ;the old format
320 root-device))))
8e815c5b
LC
321
322 (store-mount-point
323 (match (assq 'store rest)
324 (('store ('device _) ('mount-point mount-point) _ ...)
325 mount-point)
326 (_ ;the old format
327 "/")))))
328 (x ;unsupported format
b1059b38
LC
329 (warning (G_ "unrecognized boot parameters at '~a'~%")
330 (port-filename port))
8e815c5b
LC
331 #f)))
332
333(define (read-boot-parameters-file system)
334 "Read boot parameters from SYSTEM's (system or generation) \"parameters\"
335file and returns the corresponding <boot-parameters> object or #f if the
336format is unrecognized.
337The object has its kernel-arguments extended in order to make it bootable."
338 (let* ((file (string-append system "/parameters"))
339 (params (call-with-input-file file read-boot-parameters))
a7ef45d9
LC
340 (root (boot-parameters-root-device params)))
341 (boot-parameters
342 (inherit params)
343 (kernel-arguments (append (bootable-kernel-arguments system root)
344 (boot-parameters-kernel-arguments params))))))
8b22107e 345
1975c754
DM
346(define (boot-parameters->menu-entry conf)
347 (menu-entry
348 (label (boot-parameters-label conf))
349 (device (boot-parameters-store-device conf))
350 (device-mount-point (boot-parameters-store-mount-point conf))
351 (linux (boot-parameters-kernel conf))
352 (linux-arguments (boot-parameters-kernel-arguments conf))
353 (initrd (boot-parameters-initrd conf))))
354
8b22107e 355
8e815c5b 356\f
40281c54
LC
357;;;
358;;; Services.
359;;;
360
aa1145df
LC
361(define (non-boot-file-system-service os)
362 "Return the file system service for the file systems of OS that are not
363marked as 'needed-for-boot'."
023f391c 364 (define file-systems
4d6b879c 365 (remove file-system-needed-for-boot?
023f391c
LC
366 (operating-system-file-systems os)))
367
68a58775
LC
368 (define mapped-devices-for-boot
369 (operating-system-boot-mapped-devices os))
370
5dae0186 371 (define (device-mappings fs)
ab64483f 372 (let ((device (file-system-device fs)))
29824d80 373 (if (string? device) ;title is 'device
ab64483f
LC
374 (filter (lambda (md)
375 (string=? (string-append "/dev/mapper/"
376 (mapped-device-target md))
377 device))
378 (operating-system-mapped-devices os))
379 '())))
5dae0186 380
e502bf89
LC
381 (define (add-dependencies fs)
382 ;; Add the dependencies due to device mappings to FS.
383 (file-system
384 (inherit fs)
385 (dependencies
68a58775
LC
386 (delete-duplicates
387 (remove (cut member <> mapped-devices-for-boot)
388 (append (device-mappings fs)
389 (file-system-dependencies fs)))
390 eq?))))
e502bf89 391
aa1145df
LC
392 (service file-system-service-type
393 (map add-dependencies file-systems)))
023f391c 394
68a58775
LC
395(define (mapped-device-users device file-systems)
396 "Return the subset of FILE-SYSTEMS that use DEVICE."
de1c158f 397 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
68a58775
LC
398 (filter (lambda (fs)
399 (or (member device (file-system-dependencies fs))
400 (and (string? (file-system-device fs))
401 (string=? (file-system-device fs) target))))
402 file-systems)))
de1c158f
LC
403
404(define (operating-system-user-mapped-devices os)
405 "Return the subset of mapped devices that can be installed in
406user-land--i.e., those not needed during boot."
9cb426b8
LC
407 (let ((devices (operating-system-mapped-devices os))
408 (file-systems (operating-system-file-systems os)))
409 (filter (lambda (md)
68a58775
LC
410 (let ((users (mapped-device-users md file-systems)))
411 (not (any file-system-needed-for-boot? users))))
9cb426b8 412 devices)))
de1c158f
LC
413
414(define (operating-system-boot-mapped-devices os)
415 "Return the subset of mapped devices that must be installed during boot,
416from the initrd."
9cb426b8
LC
417 (let ((devices (operating-system-mapped-devices os))
418 (file-systems (operating-system-file-systems os)))
419 (filter (lambda (md)
68a58775
LC
420 (let ((users (mapped-device-users md file-systems)))
421 (any file-system-needed-for-boot? users)))
9cb426b8 422 devices)))
de1c158f 423
5dae0186 424(define (device-mapping-services os)
be1c2c54 425 "Return the list of device-mapping services for OS as a list."
4da8c19e 426 (map device-mapping-service
be1c2c54 427 (operating-system-user-mapped-devices os)))
5dae0186 428
2a13d05e 429(define (swap-services os)
be1c2c54
LC
430 "Return the list of swap services for OS."
431 (map swap-service (operating-system-swap-devices os)))
2a13d05e 432
44d5f54e
LC
433(define* (system-linux-image-file-name #:optional (system (%current-system)))
434 "Return the basename of the kernel image file for SYSTEM."
435 ;; FIXME: Evaluate the conditional based on the actual current system.
43fe431c
DC
436 (cond
437 ((string-prefix? "arm" (%current-system)) "zImage")
438 ((string-prefix? "mips" (%current-system)) "vmlinuz")
fd900d68 439 ((string-prefix? "aarch64" (%current-system)) "Image")
43fe431c 440 (else "bzImage")))
44d5f54e
LC
441
442(define (operating-system-kernel-file os)
443 "Return an object representing the absolute file name of the kernel image of
444OS."
445 (file-append (operating-system-kernel os)
446 "/" (system-linux-image-file-name os)))
447
69cae3d3 448(define* (operating-system-directory-base-entries os)
d62e201c
LC
449 "Return the basic entries of the 'system' directory of OS for use as the
450value of the SYSTEM-SERVICE-TYPE service."
b19a49d0 451 (let ((locale (operating-system-locale-directory os)))
69cae3d3
LC
452 (mlet %store-monad ((kernel -> (operating-system-kernel os))
453 (initrd -> (operating-system-initrd-file os))
454 (params (operating-system-boot-parameters-file os)))
455 (return `(("kernel" ,kernel)
456 ("parameters" ,params)
457 ("initrd" ,initrd)
458 ("locale" ,locale)))))) ;used by libc
459
460(define* (essential-services os)
217a5b85
LC
461 "Return the list of essential services for OS. These are special services
462that implement part of what's declared in OS are responsible for low-level
69cae3d3 463bookkeeping."
d6e2a622
LC
464 (define known-fs
465 (map file-system-mount-point (operating-system-file-systems os)))
466
be1c2c54
LC
467 (let* ((mappings (device-mapping-services os))
468 (root-fs (root-file-system-service))
aa1145df 469 (other-fs (non-boot-file-system-service os))
be1c2c54 470 (swaps (swap-services os))
206a28d8 471 (procs (service user-processes-service-type))
d62e201c 472 (host-name (host-name-service (operating-system-host-name os)))
69cae3d3 473 (entries (operating-system-directory-base-entries os)))
d62e201c
LC
474 (cons* (service system-service-type entries)
475 %boot-service
0adfe95a 476
661c237b 477 ;; %SHEPHERD-ROOT-SERVICE must come last so that the gexp that
d4053c71 478 ;; execs shepherd comes last in the boot script (XXX). Likewise,
661c237b 479 ;; the cleanup service must come first so that its gexp runs before
d4053c71 480 ;; activation code.
be7be9e8 481 (service cleanup-service-type #f)
661c237b
LC
482 %activation-service
483 %shepherd-root-service
0adfe95a
LC
484
485 (pam-root-service (operating-system-pam-services os))
486 (account-service (append (operating-system-accounts os)
487 (operating-system-groups os))
488 (operating-system-skeletons os))
489 (operating-system-etc-service os)
e43e84ba 490 (service fstab-service-type '())
6bad7524
SB
491 (session-environment-service
492 (operating-system-environment-variables os))
6c445817 493 host-name procs root-fs
0adfe95a
LC
494 (service setuid-program-service-type
495 (operating-system-setuid-programs os))
af4c3fd5
LC
496 (service profile-service-type
497 (operating-system-packages os))
aa1145df
LC
498 other-fs
499 (append mappings swaps
0adfe95a 500
69cae3d3
LC
501 ;; Add the firmware service.
502 (list %linux-bare-metal-service
503 (service firmware-service-type
504 (operating-system-firmware os)))))))
505
506(define* (operating-system-services os)
507 "Return all the services of OS, including \"essential\" services."
d466b1fc
LC
508 (instantiate-missing-services
509 (append (operating-system-user-services os)
69cae3d3 510 (operating-system-essential-services os))))
217a5b85 511
40281c54
LC
512\f
513;;;
514;;; /etc.
515;;;
516
f34c56be
LC
517(define %base-firmware
518 ;; Firmware usable by default.
52db41af
EB
519 (list ath9k-htc-firmware
520 openfwwf-firmware))
f34c56be 521
6f436c54
LC
522(define %base-packages
523 ;; Default set of packages globally visible. It should include anything
524 ;; required for basic administrator tasks.
55e70e65 525 (cons* procps psmisc which less zile nano
a96a82d7 526 pciutils usbutils
5576cfab 527 util-linux
af23710f 528 inetutils isc-dhcp
87941d1d 529 (@ (gnu packages admin) shadow) ;for 'passwd'
be681773
LC
530
531 ;; wireless-tools is deprecated in favor of iw, but it's still what
532 ;; many people are familiar with, so keep it around.
5146f22b 533 iw wireless-tools
be681773 534
5dccaffe 535 iproute
9b762b8d 536 net-tools ; XXX: remove when Inetutils suffices
18316d86 537 man-db
02683c33 538 info-reader ;the standalone Info reader (no Perl)
03e9998f 539
a8a086e3
LC
540 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
541 ;; want the other commands and the man pages (notably because
542 ;; auto-completion in Emacs shell relies on man pages.)
543 sudo
544
03e9998f
LC
545 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
546 ;; already depends on it anyway.
255f7308 547 kmod eudev
03e9998f 548
b63dbd44 549 e2fsprogs kbd
9b762b8d 550
1d167b6e
LC
551 bash-completion
552
86f23092
LC
553 ;; XXX: We don't use (canonical-package guile-2.2) here because that
554 ;; would create a collision in the global profile between the GMP
555 ;; variant propagated by 'guile-final' and the GMP variant propagated
556 ;; by 'gnutls', itself propagated by 'guix'.
557 guile-2.2
558
9b762b8d
LC
559 ;; The packages below are also in %FINAL-INPUTS, so take them from
560 ;; there to avoid duplication.
561 (map canonical-package
86f23092 562 (list bash coreutils findutils grep sed
4b164c45 563 diffutils patch gawk tar gzip bzip2 xz lzip))))
6f436c54 564
548d4c13
LC
565(define %default-issue
566 ;; Default contents for /etc/issue.
567 "
568This is the GNU system. Welcome.\n")
569
568841d4
LC
570(define (local-host-aliases host-name)
571 "Return aliases for HOST-NAME, to be used in /etc/hosts."
572 (string-append "127.0.0.1 localhost " host-name "\n"
573 "::1 localhost " host-name "\n"))
574
c65e1834
LC
575(define (default-/etc/hosts host-name)
576 "Return the default /etc/hosts file."
24e02c28 577 (plain-file "hosts" (local-host-aliases host-name)))
c65e1834 578
0adfe95a
LC
579(define* (operating-system-etc-service os)
580 "Return a <service> that builds containing the static part of the /etc
581directory."
e453da13
LF
582 (let ((login.defs
583 (plain-file "login.defs"
584 (string-append
585 "# Default paths for non-login shells started by su(1).\n"
586 "ENV_PATH /run/setuid-programs:"
587 "/run/current-system/profile/bin:"
588 "/run/current-system/profile/sbin\n"
589 "ENV_SUPATH /run/setuid-programs:"
590 "/run/current-system/profile/bin:"
591 "/run/current-system/profile/sbin\n")))
0adfe95a 592
0adfe95a
LC
593 (issue (plain-file "issue" (operating-system-issue os)))
594 (nsswitch (plain-file "nsswitch.conf"
595 (name-service-switch->string
596 (operating-system-name-service-switch os))))
597
598 ;; Startup file for POSIX-compliant login shells, which set system-wide
599 ;; environment variables.
600 (profile (mixed-text-file "profile" "\
c05c4321 601# Crucial variables that could be missing in the profiles' 'etc/profile'
d9959421
LC
602# because they would require combining both profiles.
603# FIXME: See <http://bugs.gnu.org/20255>.
97ab2c0f 604export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
8d09bfe2 605export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
00239d05
SB
606export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
607export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
608
ce380150
TD
609# Make sure libXcursor finds cursors installed into user or system profiles. See <http://bugs.gnu.org/24445>
610export XCURSOR_PATH=$HOME/.icons:$HOME/.guix-profile/share/icons:/run/current-system/profile/share/icons
611
d9959421
LC
612# Ignore the default value of 'PATH'.
613unset PATH
614
615# Load the system profile's settings.
bd7e136d 616GUIX_PROFILE=/run/current-system/profile ; \\
669786da 617. /run/current-system/profile/etc/profile
d9959421 618
cad7e6ab
CSLL
619# Since 'lshd' does not use pam_env, /etc/environment must be explicitly
620# loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
621# We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
622# reading the user's 'etc/profile' to allow variables to be overridden.
623if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
624 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
625then
626 . /etc/environment
627 export `cat /etc/environment | cut -d= -f1`
628fi
629
8d09bfe2
LC
630# Arrange so that ~/.config/guix/current comes first.
631for profile in \"$HOME/.guix-profile\" \"$HOME/.config/guix/current\"
cdc5b932
LC
632do
633 if [ -f \"$profile/etc/profile\" ]
634 then
635 # Load the user profile's settings.
636 GUIX_PROFILE=\"$profile\" ; \\
637 . \"$profile/etc/profile\"
638 else
639 # At least define this one so that basic things just work
640 # when the user installs their first package.
641 export PATH=\"$profile/bin:$PATH\"
642 fi
643done
d9959421 644
a854525a
LC
645# Prepend setuid programs.
646export PATH=/run/setuid-programs:$PATH
647
8d09bfe2
LC
648# Arrange so that ~/.config/guix/current/share/info comes first.
649export INFOPATH=\"$HOME/.config/guix/current/share/info:$INFOPATH\"
650
11202482
LC
651# Set the umask, notably for users logging in via 'lsh'.
652# See <http://bugs.gnu.org/22650>.
653umask 022
2a5f0db4 654
c08da2ee
LC
655# Allow Hunspell-based applications (IceCat, LibreOffice, etc.) to
656# find dictionaries.
657export DICPATH=\"$HOME/.guix-profile/share/hunspell:/run/current-system/profile/share/hunspell\"
658
4af7c83b
LC
659# Allow GStreamer-based applications to find plugins.
660export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
661
1d167b6e
LC
662if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
663then
664 # Load Bash-specific initialization code.
669786da 665 . /etc/bashrc
1d167b6e 666fi
40281c54 667"))
1d167b6e 668
0adfe95a 669 (bashrc (plain-file "bashrc" "\
1d167b6e
LC
670# Bash-specific initialization.
671
672# The 'bash-completion' package.
673if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
674then
675 # Bash-completion sources ~/.bash_completion. It installs a dynamic
676 # completion loader that searches its own completion files as well
677 # as those in ~/.guix-profile and /run/current-system/profile.
678 source /run/current-system/profile/etc/profile.d/bash_completion.sh
0adfe95a
LC
679fi\n")))
680 (etc-service
9e41130b
LC
681 `(("services" ,(file-append net-base "/etc/services"))
682 ("protocols" ,(file-append net-base "/etc/protocols"))
683 ("rpc" ,(file-append net-base "/etc/rpc"))
0adfe95a
LC
684 ("login.defs" ,#~#$login.defs)
685 ("issue" ,#~#$issue)
686 ("nsswitch.conf" ,#~#$nsswitch)
0adfe95a
LC
687 ("profile" ,#~#$profile)
688 ("bashrc" ,#~#$bashrc)
689 ("hosts" ,#~#$(or (operating-system-hosts-file os)
690 (default-/etc/hosts (operating-system-host-name os))))
c694520b
TD
691 ;; Write the operating-system-host-name to /etc/hostname to prevent
692 ;; NetworkManager from changing the system's hostname when connecting
693 ;; to certain networks. Some discussion at
694 ;; https://lists.gnu.org/archive/html/help-guix/2017-09/msg00037.html
695 ("hostname" ,(plain-file "hostname" (operating-system-host-name os)))
9e41130b
LC
696 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
697 (operating-system-timezone os)))
0adfe95a 698 ("sudoers" ,(operating-system-sudoers-file os))))))
033adfe7 699
ab6a279a
LC
700(define %root-account
701 ;; Default root account.
702 (user-account
703 (name "root")
704 (password "")
705 (uid 0) (group "root")
706 (comment "System administrator")
707 (home-directory "/root")))
708
0b6f49ef 709(define (operating-system-accounts os)
0adfe95a
LC
710 "Return the user accounts for OS, including an obligatory 'root' account,
711and excluding accounts requested by services."
712 ;; Make sure there's a root account.
713 (if (find (lambda (user)
714 (and=> (user-account-uid user) zero?))
715 (operating-system-users os))
716 (operating-system-users os)
717 (cons %root-account (operating-system-users os))))
0b6f49ef 718
84765839
LC
719(define (maybe-string->file file-name thing)
720 "If THING is a string, return a <plain-file> with THING as its content.
721Otherwise just return THING.
722
723This is for backward-compatibility of fields that used to be strings and are
724now file-like objects.."
725 (match thing
726 ((? string?)
69daee23 727 (warning (G_ "using a string for file '~a' is deprecated; \
84765839
LC
728use 'plain-file' instead~%")
729 file-name)
730 (plain-file file-name thing))
731 (x
732 x)))
733
24e02c28
LC
734(define (maybe-file->monadic file-name thing)
735 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
736THING in the %STORE-MONAD.
737
738This is for backward-compatibility of fields that used to be monadic values
739and are now file-like objects."
740 (with-monad %store-monad
741 (match thing
742 ((? procedure?)
69daee23 743 (warning (G_ "using a monadic value for '~a' is deprecated; \
24e02c28
LC
744use 'plain-file' instead~%")
745 file-name)
746 thing)
747 (x
748 (return x)))))
749
0b6f49ef
LC
750(define (operating-system-etc-directory os)
751 "Return that static part of the /etc directory of OS."
0adfe95a
LC
752 (etc-directory
753 (fold-services (operating-system-services os)
754 #:target-type etc-service-type)))
033adfe7 755
6bad7524
SB
756(define (operating-system-environment-variables os)
757 "Return the environment variables of OS for
758@var{session-environment-service-type}, to be used in @file{/etc/environment}."
759 `(("LANG" . ,(operating-system-locale os))
54757499
LC
760 ;; Note: No need to set 'TZ' since (1) we provide /etc/localtime, and (2)
761 ;; it doesn't work for setuid binaries. See <https://bugs.gnu.org/29212>.
9e41130b 762 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
6bad7524
SB
763 ;; Tell 'modprobe' & co. where to look for modules.
764 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
765 ;; These variables are honored by OpenSSL (libssl) and Git.
766 ("SSL_CERT_DIR" . "/etc/ssl/certs")
767 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
768 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
ae05e366
LC
769
770 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
771 ;; searched for.
772 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
773
6bad7524
SB
774 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
775 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
946465bb
LC
776 ("DBUS_FATAL_WARNINGS" . "0")
777
778 ;; XXX: Normally we wouldn't need to do this, but our glibc@2.23 package
779 ;; used to look things up in 'PREFIX/lib/locale' instead of
780 ;; '/run/current-system/locale' as was intended. Keep this hack around so
781 ;; that people who still have glibc@2.23-using packages in their profiles
782 ;; can use them correctly.
783 ;; TODO: Remove when glibc@2.23 is long gone.
784 ("GUIX_LOCPATH" . "/run/current-system/locale")))
6bad7524 785
09e028f4
LC
786(define %setuid-programs
787 ;; Default set of setuid-root programs.
3b65abac 788 (let ((shadow (@ (gnu packages admin) shadow)))
9e41130b
LC
789 (list (file-append shadow "/bin/passwd")
790 (file-append shadow "/bin/su")
852241eb
SB
791 (file-append shadow "/bin/newuidmap")
792 (file-append shadow "/bin/newgidmap")
9e41130b
LC
793 (file-append inetutils "/bin/ping")
794 (file-append inetutils "/bin/ping6")
795 (file-append sudo "/bin/sudo")
5144df2c 796 (file-append sudo "/bin/sudoedit")
9e41130b 797 (file-append fuse "/bin/fusermount"))))
69689380
LC
798
799(define %sudoers-specification
800 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
801 ;; group can do anything. See
802 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
803 ;; TODO: Add a declarative API.
84765839
LC
804 (plain-file "sudoers" "\
805root ALL=(ALL) ALL
806%wheel ALL=(ALL) ALL\n"))
09e028f4 807
69cae3d3 808(define* (operating-system-activation-script os)
484a2b3a
LC
809 "Return the activation script for OS---i.e., the code that \"activates\" the
810stateful part of OS, including user accounts and groups, special directories,
811etc."
69cae3d3 812 (let* ((services (operating-system-services os))
0adfe95a
LC
813 (activation (fold-services services
814 #:target-type activation-service-type)))
815 (activation-service->script activation)))
484a2b3a 816
69cae3d3 817(define* (operating-system-boot-script os)
484a2b3a 818 "Return the boot script for OS---i.e., the code started by the initrd once
69cae3d3
LC
819we're running in the final root."
820 (let* ((services (operating-system-services os))
d62e201c 821 (boot (fold-services services #:target-type boot-service-type)))
efe7d19a 822 (service-value boot)))
2106d3fc 823
b2fef041
LC
824(define (operating-system-user-accounts os)
825 "Return the list of user accounts of OS."
826 (let* ((services (operating-system-services os))
827 (account (fold-services services
828 #:target-type account-service-type)))
829 (filter user-account?
efe7d19a 830 (service-value account))))
b2fef041
LC
831
832(define (operating-system-shepherd-service-names os)
833 "Return the list of Shepherd service names for OS."
834 (append-map shepherd-service-provision
efe7d19a 835 (service-value
b2fef041
LC
836 (fold-services (operating-system-services os)
837 #:target-type
838 shepherd-root-service-type))))
839
69cae3d3 840(define* (operating-system-derivation os)
d62e201c 841 "Return a derivation that builds OS."
69cae3d3 842 (let* ((services (operating-system-services os))
d62e201c
LC
843 (system (fold-services services)))
844 ;; SYSTEM contains the derivation as a monadic value.
efe7d19a 845 (service-value system)))
d62e201c 846
69cae3d3 847(define* (operating-system-profile os)
af4c3fd5
LC
848 "Return a derivation that builds the system profile of OS."
849 (mlet* %store-monad
69cae3d3 850 ((services -> (operating-system-services os))
af4c3fd5
LC
851 (profile (fold-services services
852 #:target-type profile-service-type)))
853 (match profile
854 (("profile" profile)
855 (return profile)))))
856
83bcd0b8
LC
857(define (operating-system-root-file-system os)
858 "Return the root file system of OS."
a48d3450
LC
859 (find (lambda (fs)
860 (string=? "/" (file-system-mount-point fs)))
83bcd0b8
LC
861 (operating-system-file-systems os)))
862
b4140694
LC
863(define (operating-system-initrd-file os)
864 "Return a gexp denoting the initrd file of OS."
83bcd0b8 865 (define boot-file-systems
4d6b879c 866 (filter file-system-needed-for-boot?
83bcd0b8
LC
867 (operating-system-file-systems os)))
868
de1c158f
LC
869 (define mapped-devices
870 (operating-system-boot-mapped-devices os))
871
872 (define make-initrd
873 (operating-system-initrd os))
874
d422cbb3
LC
875 (make-initrd boot-file-systems
876 #:linux (operating-system-kernel os)
877 #:linux-modules
878 (operating-system-initrd-modules os)
ae7a316b
LC
879 #:mapped-devices mapped-devices
880 #:keyboard-layout (operating-system-keyboard-layout os)))
b4140694 881
f5582b2c
LC
882(define (locale-name->definition* name)
883 "Variant of 'locale-name->definition' that raises an error upon failure."
884 (match (locale-name->definition name)
885 (#f
886 (raise (condition
887 (&message
69daee23 888 (message (format #f (G_ "~a: invalid locale name") name))))))
f5582b2c
LC
889 (def def)))
890
598e19dc
LC
891(define (operating-system-locale-directory os)
892 "Return the directory containing the locales compiled for the definitions
893listed in OS. The C library expects to find it under
894/run/current-system/locale."
f5582b2c
LC
895 (define name
896 (operating-system-locale os))
897
898 (define definitions
899 ;; While we're at it, check whether NAME is defined and add it if needed.
900 (if (member name (map locale-definition-name
901 (operating-system-locale-definitions os)))
902 (operating-system-locale-definitions os)
903 (cons (locale-name->definition* name)
904 (operating-system-locale-definitions os))))
905
906 (locale-directory definitions
34760ae7 907 #:libcs (operating-system-locale-libcs os)))
598e19dc 908
c2e9942b
MO
909(define (kernel->boot-label kernel)
910 "Return a label for the bootloader menu entry that boots KERNEL."
bdc61ff9
P
911 (cond ((package? kernel)
912 (string-append "GNU with "
913 (string-titlecase (package-name kernel)) " "
914 (package-version kernel)
915 " (beta)"))
916 ((inferior-package? kernel)
917 (string-append "GNU with "
b12f8720 918 (string-titlecase (inferior-package-name kernel)) " "
bdc61ff9
P
919 (inferior-package-version kernel)
920 " (beta)"))
921 (else "GNU")))
2d23e6f0 922
6b779207
LC
923(define (store-file-system file-systems)
924 "Return the file system object among FILE-SYSTEMS that contains the store."
925 (match (filter (lambda (fs)
926 (and (file-system-mount? fs)
927 (not (memq 'bind-mount (file-system-flags fs)))
928 (string-prefix? (file-system-mount-point fs)
929 (%store-prefix))))
930 file-systems)
931 ((and candidates (head . tail))
932 (reduce (lambda (fs1 fs2)
933 (if (> (string-length (file-system-mount-point fs1))
934 (string-length (file-system-mount-point fs2)))
935 fs1
936 fs2))
937 head
938 candidates))))
939
940(define (operating-system-store-file-system os)
941 "Return the file system that contains the store of OS."
942 (store-file-system (operating-system-file-systems os)))
943
c76b3046 944(define* (operating-system-bootcfg os #:optional (old-entries '()))
5ece56dc
LC
945 "Return the bootloader configuration file for OS. Use OLD-ENTRIES,
946a list of <menu-entry>, to populate the \"old entries\" menu."
9782c822
LC
947 (let* ((root-fs (operating-system-root-file-system os))
948 (root-device (file-system-device root-fs))
949 (params (operating-system-boot-parameters
950 os root-device
951 #:system-kernel-arguments? #t))
952 (entry (boot-parameters->menu-entry params))
953 (bootloader-conf (operating-system-bootloader os)))
46c296dc
LC
954 (define generate-config-file
955 (bootloader-configuration-file-generator
956 (bootloader-configuration-bootloader bootloader-conf)))
957
9782c822
LC
958 (generate-config-file bootloader-conf (list entry)
959 #:old-entries old-entries)))
b4140694 960
a7ef45d9
LC
961(define* (operating-system-boot-parameters os root-device
962 #:key system-kernel-arguments?)
963 "Return a monadic <boot-parameters> record that describes the boot
964parameters of OS. When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments
965such as '--root' and '--load' to <boot-parameters>."
35b44681
LC
966 (let* ((initrd (operating-system-initrd-file os))
967 (store (operating-system-store-file-system os))
968 (bootloader (bootloader-configuration-bootloader
969 (operating-system-bootloader os)))
970 (bootloader-name (bootloader-name bootloader))
971 (label (kernel->boot-label (operating-system-kernel os))))
972 (boot-parameters
973 (label label)
974 (root-device root-device)
975 (kernel (operating-system-kernel-file os))
976 (kernel-arguments
977 (if system-kernel-arguments?
978 (operating-system-kernel-arguments os root-device)
979 (operating-system-user-kernel-arguments os)))
980 (initrd initrd)
981 (bootloader-name bootloader-name)
982 (store-device (ensure-not-/dev (file-system-device store)))
983 (store-mount-point (file-system-mount-point store)))))
40fad1c2 984
9b336338
LC
985(define (device->sexp device)
986 "Serialize DEVICE as an sexp (really, as an object with a read syntax.)"
987 (match device
988 ((? uuid? uuid)
075681d3 989 `(uuid ,(uuid-type uuid) ,(uuid-bytevector uuid)))
a5acc17a
LC
990 ((? file-system-label? label)
991 `(file-system-label ,(file-system-label->string label)))
9b336338
LC
992 (_
993 device)))
994
a7ef45d9
LC
995(define* (operating-system-boot-parameters-file os
996 #:key system-kernel-arguments?)
40fad1c2
DM
997 "Return a file that describes the boot parameters of OS. The primary use of
998this file is the reconstruction of GRUB menu entries for old configurations.
a7ef45d9
LC
999
1000When SYSTEM-KERNEL-ARGUMENTS? is true, add kernel arguments such as '--root'
1001and '--load' to the returned file (since the returned file is then usually
1002stored into the content-addressed \"system\" directory, it's usually not a
1003good idea to give it because the content hash would change by the content hash
40fad1c2 1004being stored into the \"parameters\" file)."
35b44681
LC
1005 (let* ((root (operating-system-root-file-system os))
1006 (device (file-system-device root))
1007 (params (operating-system-boot-parameters
1008 os device
1009 #:system-kernel-arguments?
1010 system-kernel-arguments?)))
40fad1c2
DM
1011 (gexp->file "parameters"
1012 #~(boot-parameters
1013 (version 0)
1014 (label #$(boot-parameters-label params))
9b336338
LC
1015 (root-device
1016 #$(device->sexp
1017 (boot-parameters-root-device params)))
40fad1c2
DM
1018 (kernel #$(boot-parameters-kernel params))
1019 (kernel-arguments
1020 #$(boot-parameters-kernel-arguments params))
1021 (initrd #$(boot-parameters-initrd params))
f96752e3 1022 (bootloader-name #$(boot-parameters-bootloader-name params))
40fad1c2 1023 (store
9b336338
LC
1024 (device
1025 #$(device->sexp (boot-parameters-store-device params)))
40fad1c2
DM
1026 (mount-point #$(boot-parameters-store-mount-point params))))
1027 #:set-load-path? #f)))
64e40dbb 1028
96da5d62
LC
1029(define-gexp-compiler (operating-system-compiler (os <operating-system>)
1030 system target)
1031 ((store-lift
1032 (lambda (store)
1033 ;; XXX: This is not super elegant but we can't pass SYSTEM and TARGET to
1034 ;; 'operating-system-derivation'.
1035 (run-with-store store (operating-system-derivation os)
1036 #:system system
1037 #:target target)))))
1038
033adfe7 1039;;; system.scm ends here