Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / system.scm
CommitLineData
033adfe7 1;;; GNU Guix --- Functional package management for GNU
29824d80 2;;; Copyright © 2013, 2014, 2015, 2016 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>
033adfe7
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (gnu system)
23 #:use-module (guix store)
24 #:use-module (guix monads)
02100028 25 #:use-module (guix gexp)
033adfe7
LC
26 #:use-module (guix records)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29fce8b6 29 #:use-module (guix profiles)
84765839 30 #:use-module (guix ui)
033adfe7
LC
31 #:use-module (gnu packages base)
32 #:use-module (gnu packages bash)
bdb36958 33 #:use-module (gnu packages guile)
9de46ffb 34 #:use-module (gnu packages admin)
8a07c289 35 #:use-module (gnu packages linux)
a96a82d7 36 #:use-module (gnu packages pciutils)
033adfe7 37 #:use-module (gnu packages package-management)
6f436c54
LC
38 #:use-module (gnu packages less)
39 #:use-module (gnu packages zile)
55e70e65
LC
40 #:use-module (gnu packages nano)
41 #:use-module (gnu packages lsof)
a68c6967 42 #:use-module (gnu packages gawk)
18316d86 43 #:use-module (gnu packages man)
939c5c31 44 #:use-module (gnu packages texinfo)
a68c6967 45 #:use-module (gnu packages compression)
f34c56be 46 #:use-module (gnu packages firmware)
db4fdc04 47 #:use-module (gnu services)
0190c1c0 48 #:use-module (gnu services shepherd)
db4fdc04 49 #:use-module (gnu services base)
033adfe7
LC
50 #:use-module (gnu system grub)
51 #:use-module (gnu system shadow)
996ed739 52 #:use-module (gnu system nss)
598e19dc 53 #:use-module (gnu system locale)
6e828634 54 #:use-module (gnu system pam)
735c6dd7 55 #:use-module (gnu system linux-initrd)
c5df1839 56 #:use-module (gnu system file-systems)
060d62a7 57 #:use-module (gnu system mapped-devices)
033adfe7
LC
58 #:use-module (ice-9 match)
59 #:use-module (srfi srfi-1)
60 #:use-module (srfi srfi-26)
598e19dc
LC
61 #:use-module (srfi srfi-34)
62 #:use-module (srfi srfi-35)
033adfe7
LC
63 #:export (operating-system
64 operating-system?
d5b429ab
LC
65
66 operating-system-bootloader
033adfe7 67 operating-system-services
217a5b85 68 operating-system-user-services
033adfe7 69 operating-system-packages
fd3bfc44 70 operating-system-host-name
c65e1834 71 operating-system-hosts-file
fd3bfc44 72 operating-system-kernel
44d5f54e 73 operating-system-kernel-file
ee2a6304 74 operating-system-kernel-arguments
fd3bfc44
LC
75 operating-system-initrd
76 operating-system-users
77 operating-system-groups
548d4c13 78 operating-system-issue
fd3bfc44
LC
79 operating-system-timezone
80 operating-system-locale
598e19dc 81 operating-system-locale-definitions
34760ae7 82 operating-system-locale-libcs
5dae0186 83 operating-system-mapped-devices
83bcd0b8 84 operating-system-file-systems
6b779207 85 operating-system-store-file-system
2bdd7ac1
LC
86 operating-system-user-mapped-devices
87 operating-system-boot-mapped-devices
b25937e3 88 operating-system-activation-script
b2fef041
LC
89 operating-system-user-accounts
90 operating-system-shepherd-service-names
033adfe7 91
1aa0033b 92 operating-system-derivation
83bcd0b8 93 operating-system-profile
6f436c54 94 operating-system-grub.cfg
239db054
DT
95 operating-system-etc-directory
96 operating-system-locale-directory
97 operating-system-boot-script
98
b8300494
AK
99 boot-parameters
100 boot-parameters?
101 boot-parameters-label
102 boot-parameters-root-device
1ef8b72a
CM
103 boot-parameters-store-device
104 boot-parameters-store-mount-point
b8300494
AK
105 boot-parameters-kernel
106 boot-parameters-kernel-arguments
d7b342d8 107 boot-parameters-initrd
b8300494
AK
108 read-boot-parameters
109
568841d4 110 local-host-aliases
df5ce088 111 %setuid-programs
5dae0186 112 %base-packages
374f14c2 113 %base-firmware))
033adfe7
LC
114
115;;; Commentary:
116;;;
117;;; This module supports whole-system configuration.
118;;;
119;;; Code:
120
121;; System-wide configuration.
122;; TODO: Add per-field docstrings/stexi.
123(define-record-type* <operating-system> operating-system
124 make-operating-system
125 operating-system?
126 (kernel operating-system-kernel ; package
57082417 127 (default linux-libre))
ee2a6304
LC
128 (kernel-arguments operating-system-kernel-arguments
129 (default '())) ; list of gexps/strings
d5b429ab
LC
130 (bootloader operating-system-bootloader) ; <grub-configuration>
131
83bcd0b8 132 (initrd operating-system-initrd ; (list fs) -> M derivation
060238ae 133 (default base-initrd))
f34c56be
LC
134 (firmware operating-system-firmware ; list of packages
135 (default %base-firmware))
033adfe7
LC
136
137 (host-name operating-system-host-name) ; string
24e02c28 138 (hosts-file operating-system-hosts-file ; file-like | #f
c65e1834 139 (default #f))
033adfe7 140
5dae0186
LC
141 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
142 (default '()))
8a6d2731 143 (file-systems operating-system-file-systems) ; list of fs
2a13d05e
LC
144 (swap-devices operating-system-swap-devices ; list of strings
145 (default '()))
033adfe7
LC
146
147 (users operating-system-users ; list of user accounts
bf87f38a 148 (default %base-user-accounts))
033adfe7 149 (groups operating-system-groups ; list of user groups
773e956d 150 (default %base-groups))
033adfe7 151
40281c54
LC
152 (skeletons operating-system-skeletons ; list of name/monadic value
153 (default (default-skeletons)))
548d4c13
LC
154 (issue operating-system-issue ; string
155 (default %default-issue))
40281c54 156
033adfe7 157 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
6f436c54 158 (default %base-packages)) ; or just PACKAGE
033adfe7
LC
159
160 (timezone operating-system-timezone) ; string
8a6d2731 161 (locale operating-system-locale ; string
598e19dc
LC
162 (default "en_US.utf8"))
163 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
164 (default %default-locale-definitions))
34760ae7
LC
165 (locale-libcs operating-system-locale-libcs ; list of <packages>
166 (default %default-locale-libcs))
996ed739
LC
167 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
168 (default %default-nss))
033adfe7 169
217a5b85 170 (services operating-system-user-services ; list of monadic services
09e028f4
LC
171 (default %base-services))
172
173 (pam-services operating-system-pam-services ; list of PAM services
174 (default (base-pam-services)))
175 (setuid-programs operating-system-setuid-programs
69689380 176 (default %setuid-programs)) ; list of string-valued gexps
033adfe7 177
f5a9ffa0
AK
178 (sudoers-file operating-system-sudoers-file ; file-like
179 (default %sudoers-specification)))
033adfe7 180
2717a89a 181\f
40281c54
LC
182;;;
183;;; Services.
184;;;
185
aa1145df
LC
186(define (non-boot-file-system-service os)
187 "Return the file system service for the file systems of OS that are not
188marked as 'needed-for-boot'."
023f391c 189 (define file-systems
4d6b879c 190 (remove file-system-needed-for-boot?
023f391c
LC
191 (operating-system-file-systems os)))
192
5dae0186 193 (define (device-mappings fs)
ab64483f 194 (let ((device (file-system-device fs)))
29824d80 195 (if (string? device) ;title is 'device
ab64483f
LC
196 (filter (lambda (md)
197 (string=? (string-append "/dev/mapper/"
198 (mapped-device-target md))
199 device))
200 (operating-system-mapped-devices os))
201 '())))
5dae0186 202
e502bf89
LC
203 (define (add-dependencies fs)
204 ;; Add the dependencies due to device mappings to FS.
205 (file-system
206 (inherit fs)
207 (dependencies
208 (delete-duplicates (append (device-mappings fs)
209 (file-system-dependencies fs))
210 eq?))))
211
aa1145df
LC
212 (service file-system-service-type
213 (map add-dependencies file-systems)))
023f391c 214
de1c158f
LC
215(define (mapped-device-user device file-systems)
216 "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f."
217 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
218 (find (lambda (fs)
2bdd7ac1
LC
219 (or (member device (file-system-dependencies fs))
220 (and (eq? 'device (file-system-title fs))
221 (string=? (file-system-device fs) target))))
de1c158f
LC
222 file-systems)))
223
224(define (operating-system-user-mapped-devices os)
225 "Return the subset of mapped devices that can be installed in
226user-land--i.e., those not needed during boot."
9cb426b8
LC
227 (let ((devices (operating-system-mapped-devices os))
228 (file-systems (operating-system-file-systems os)))
229 (filter (lambda (md)
230 (let ((user (mapped-device-user md file-systems)))
231 (or (not user)
232 (not (file-system-needed-for-boot? user)))))
233 devices)))
de1c158f
LC
234
235(define (operating-system-boot-mapped-devices os)
236 "Return the subset of mapped devices that must be installed during boot,
237from the initrd."
9cb426b8
LC
238 (let ((devices (operating-system-mapped-devices os))
239 (file-systems (operating-system-file-systems os)))
240 (filter (lambda (md)
241 (let ((user (mapped-device-user md file-systems)))
242 (and user (file-system-needed-for-boot? user))))
243 devices)))
de1c158f 244
5dae0186 245(define (device-mapping-services os)
be1c2c54 246 "Return the list of device-mapping services for OS as a list."
4da8c19e 247 (map device-mapping-service
be1c2c54 248 (operating-system-user-mapped-devices os)))
5dae0186 249
2a13d05e 250(define (swap-services os)
be1c2c54
LC
251 "Return the list of swap services for OS."
252 (map swap-service (operating-system-swap-devices os)))
2a13d05e 253
44d5f54e
LC
254(define* (system-linux-image-file-name #:optional (system (%current-system)))
255 "Return the basename of the kernel image file for SYSTEM."
256 ;; FIXME: Evaluate the conditional based on the actual current system.
257 (if (string-prefix? "mips" (%current-system))
258 "vmlinuz"
259 "bzImage"))
260
261(define (operating-system-kernel-file os)
262 "Return an object representing the absolute file name of the kernel image of
263OS."
264 (file-append (operating-system-kernel os)
265 "/" (system-linux-image-file-name os)))
266
d62e201c
LC
267(define* (operating-system-directory-base-entries os #:key container?)
268 "Return the basic entries of the 'system' directory of OS for use as the
269value of the SYSTEM-SERVICE-TYPE service."
af4c3fd5 270 (mlet %store-monad ((locale (operating-system-locale-directory os)))
d62e201c 271 (if container?
af4c3fd5 272 (return `(("locale" ,locale)))
d62e201c
LC
273 (mlet %store-monad
274 ((kernel -> (operating-system-kernel os))
275 (initrd (operating-system-initrd-file os))
276 (params (operating-system-parameters-file os)))
277 (return `(("kernel" ,kernel)
278 ("parameters" ,params)
279 ("initrd" ,initrd)
d62e201c
LC
280 ("locale" ,locale))))))) ;used by libc
281
0adfe95a 282(define* (essential-services os #:key container?)
217a5b85
LC
283 "Return the list of essential services for OS. These are special services
284that implement part of what's declared in OS are responsible for low-level
0adfe95a
LC
285bookkeeping. CONTAINER? determines whether to return the list of services for
286a container or that of a \"bare metal\" system."
d6e2a622
LC
287 (define known-fs
288 (map file-system-mount-point (operating-system-file-systems os)))
289
be1c2c54
LC
290 (let* ((mappings (device-mapping-services os))
291 (root-fs (root-file-system-service))
aa1145df 292 (other-fs (non-boot-file-system-service os))
be1c2c54
LC
293 (unmount (user-unmount-service known-fs))
294 (swaps (swap-services os))
295 (procs (user-processes-service
aa1145df 296 (service-parameters other-fs)))
d62e201c
LC
297 (host-name (host-name-service (operating-system-host-name os)))
298 (entries (operating-system-directory-base-entries
299 os #:container? container?)))
300 (cons* (service system-service-type entries)
301 %boot-service
0adfe95a 302
d4053c71
AK
303 ;; %SHEPHERD-ROOT-SERVICE must come first so that the gexp that
304 ;; execs shepherd comes last in the boot script (XXX). Likewise,
305 ;; the cleanup service must come last so that its gexp runs before
306 ;; activation code.
307 %shepherd-root-service
be7be9e8
LC
308 %activation-service
309 (service cleanup-service-type #f)
0adfe95a
LC
310
311 (pam-root-service (operating-system-pam-services os))
312 (account-service (append (operating-system-accounts os)
313 (operating-system-groups os))
314 (operating-system-skeletons os))
315 (operating-system-etc-service os)
e43e84ba 316 (service fstab-service-type '())
6bad7524
SB
317 (session-environment-service
318 (operating-system-environment-variables os))
0adfe95a
LC
319 host-name procs root-fs unmount
320 (service setuid-program-service-type
321 (operating-system-setuid-programs os))
af4c3fd5
LC
322 (service profile-service-type
323 (operating-system-packages os))
aa1145df
LC
324 other-fs
325 (append mappings swaps
0adfe95a
LC
326
327 ;; Add the firmware service, unless we are building for a
328 ;; container.
329 (if container?
330 '()
a241a7ac
LC
331 (list %linux-bare-metal-service
332 (service firmware-service-type
0adfe95a
LC
333 (operating-system-firmware os))))))))
334
335(define* (operating-system-services os #:key container?)
217a5b85
LC
336 "Return all the services of OS, including \"internal\" services that do not
337explicitly appear in OS."
be1c2c54 338 (append (operating-system-user-services os)
0adfe95a 339 (essential-services os #:container? container?)))
217a5b85 340
40281c54
LC
341\f
342;;;
343;;; /etc.
344;;;
345
f34c56be
LC
346(define %base-firmware
347 ;; Firmware usable by default.
348 (list ath9k-htc-firmware))
349
6f436c54
LC
350(define %base-packages
351 ;; Default set of packages globally visible. It should include anything
352 ;; required for basic administrator tasks.
55e70e65 353 (cons* procps psmisc which less zile nano
55e70e65 354 lsof ;for Guix's 'list-runtime-roots'
a96a82d7 355 pciutils usbutils
be681773
LC
356 util-linux inetutils isc-dhcp
357
358 ;; wireless-tools is deprecated in favor of iw, but it's still what
359 ;; many people are familiar with, so keep it around.
16cc9961 360 iw wireless-tools rfkill
be681773 361
5dccaffe 362 iproute
9b762b8d 363 net-tools ; XXX: remove when Inetutils suffices
18316d86 364 man-db
02683c33 365 info-reader ;the standalone Info reader (no Perl)
03e9998f 366
a8a086e3
LC
367 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
368 ;; want the other commands and the man pages (notably because
369 ;; auto-completion in Emacs shell relies on man pages.)
370 sudo
371
03e9998f
LC
372 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
373 ;; already depends on it anyway.
255f7308 374 kmod eudev
03e9998f 375
b63dbd44 376 e2fsprogs kbd
9b762b8d 377
1d167b6e
LC
378 bash-completion
379
9b762b8d
LC
380 ;; The packages below are also in %FINAL-INPUTS, so take them from
381 ;; there to avoid duplication.
382 (map canonical-package
a68c6967 383 (list guile-2.0 bash coreutils findutils grep sed
4b164c45 384 diffutils patch gawk tar gzip bzip2 xz lzip))))
6f436c54 385
548d4c13
LC
386(define %default-issue
387 ;; Default contents for /etc/issue.
388 "
389This is the GNU system. Welcome.\n")
390
568841d4
LC
391(define (local-host-aliases host-name)
392 "Return aliases for HOST-NAME, to be used in /etc/hosts."
393 (string-append "127.0.0.1 localhost " host-name "\n"
394 "::1 localhost " host-name "\n"))
395
c65e1834
LC
396(define (default-/etc/hosts host-name)
397 "Return the default /etc/hosts file."
24e02c28 398 (plain-file "hosts" (local-host-aliases host-name)))
c65e1834 399
0adfe95a
LC
400(define* (operating-system-etc-service os)
401 "Return a <service> that builds containing the static part of the /etc
402directory."
403 (let ((login.defs (plain-file "login.defs" "# Empty for now.\n"))
404
0adfe95a
LC
405 (issue (plain-file "issue" (operating-system-issue os)))
406 (nsswitch (plain-file "nsswitch.conf"
407 (name-service-switch->string
408 (operating-system-name-service-switch os))))
409
410 ;; Startup file for POSIX-compliant login shells, which set system-wide
411 ;; environment variables.
412 (profile (mixed-text-file "profile" "\
c05c4321 413# Crucial variables that could be missing in the profiles' 'etc/profile'
d9959421
LC
414# because they would require combining both profiles.
415# FIXME: See <http://bugs.gnu.org/20255>.
97ab2c0f 416export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
95f92d4e 417export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
00239d05
SB
418export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
419export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
420
d9959421
LC
421# Ignore the default value of 'PATH'.
422unset PATH
423
424# Load the system profile's settings.
425GUIX_PROFILE=/run/current-system/profile \\
669786da 426. /run/current-system/profile/etc/profile
d9959421
LC
427
428# Prepend setuid programs.
429export PATH=/run/setuid-programs:$PATH
430
cad7e6ab
CSLL
431# Since 'lshd' does not use pam_env, /etc/environment must be explicitly
432# loaded when someone logs in via SSH. See <http://bugs.gnu.org/22175>.
433# We need 'PATH' to be defined here, for 'cat' and 'cut'. Do this before
434# reading the user's 'etc/profile' to allow variables to be overridden.
435if [ -f /etc/environment -a -n \"$SSH_CLIENT\" \\
436 -a -z \"$LINUX_MODULE_DIRECTORY\" ]
437then
438 . /etc/environment
439 export `cat /etc/environment | cut -d= -f1`
440fi
441
507c71d6 442if [ -f \"$HOME/.guix-profile/etc/profile\" ]
d9959421
LC
443then
444 # Load the user profile's settings.
445 GUIX_PROFILE=\"$HOME/.guix-profile\" \\
669786da 446 . \"$HOME/.guix-profile/etc/profile\"
d9959421
LC
447else
448 # At least define this one so that basic things just work
449 # when the user installs their first package.
450 export PATH=\"$HOME/.guix-profile/bin:$PATH\"
451fi
452
11202482
LC
453# Set the umask, notably for users logging in via 'lsh'.
454# See <http://bugs.gnu.org/22650>.
455umask 022
2a5f0db4 456
4af7c83b
LC
457# Allow GStreamer-based applications to find plugins.
458export GST_PLUGIN_PATH=\"$HOME/.guix-profile/lib/gstreamer-1.0\"
459
1d167b6e
LC
460if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
461then
462 # Load Bash-specific initialization code.
669786da 463 . /etc/bashrc
1d167b6e 464fi
40281c54 465"))
1d167b6e 466
0adfe95a 467 (bashrc (plain-file "bashrc" "\
1d167b6e
LC
468# Bash-specific initialization.
469
470# The 'bash-completion' package.
471if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
472then
473 # Bash-completion sources ~/.bash_completion. It installs a dynamic
474 # completion loader that searches its own completion files as well
475 # as those in ~/.guix-profile and /run/current-system/profile.
476 source /run/current-system/profile/etc/profile.d/bash_completion.sh
0adfe95a
LC
477fi\n")))
478 (etc-service
9e41130b
LC
479 `(("services" ,(file-append net-base "/etc/services"))
480 ("protocols" ,(file-append net-base "/etc/protocols"))
481 ("rpc" ,(file-append net-base "/etc/rpc"))
0adfe95a
LC
482 ("login.defs" ,#~#$login.defs)
483 ("issue" ,#~#$issue)
484 ("nsswitch.conf" ,#~#$nsswitch)
0adfe95a
LC
485 ("profile" ,#~#$profile)
486 ("bashrc" ,#~#$bashrc)
487 ("hosts" ,#~#$(or (operating-system-hosts-file os)
488 (default-/etc/hosts (operating-system-host-name os))))
9e41130b
LC
489 ("localtime" ,(file-append tzdata "/share/zoneinfo/"
490 (operating-system-timezone os)))
0adfe95a 491 ("sudoers" ,(operating-system-sudoers-file os))))))
033adfe7 492
ab6a279a
LC
493(define %root-account
494 ;; Default root account.
495 (user-account
496 (name "root")
497 (password "")
498 (uid 0) (group "root")
499 (comment "System administrator")
500 (home-directory "/root")))
501
0b6f49ef 502(define (operating-system-accounts os)
0adfe95a
LC
503 "Return the user accounts for OS, including an obligatory 'root' account,
504and excluding accounts requested by services."
505 ;; Make sure there's a root account.
506 (if (find (lambda (user)
507 (and=> (user-account-uid user) zero?))
508 (operating-system-users os))
509 (operating-system-users os)
510 (cons %root-account (operating-system-users os))))
0b6f49ef 511
84765839
LC
512(define (maybe-string->file file-name thing)
513 "If THING is a string, return a <plain-file> with THING as its content.
514Otherwise just return THING.
515
516This is for backward-compatibility of fields that used to be strings and are
517now file-like objects.."
518 (match thing
519 ((? string?)
520 (warning (_ "using a string for file '~a' is deprecated; \
521use 'plain-file' instead~%")
522 file-name)
523 (plain-file file-name thing))
524 (x
525 x)))
526
24e02c28
LC
527(define (maybe-file->monadic file-name thing)
528 "If THING is a value in %STORE-MONAD, return it as is; otherwise return
529THING in the %STORE-MONAD.
530
531This is for backward-compatibility of fields that used to be monadic values
532and are now file-like objects."
533 (with-monad %store-monad
534 (match thing
535 ((? procedure?)
536 (warning (_ "using a monadic value for '~a' is deprecated; \
537use 'plain-file' instead~%")
538 file-name)
539 thing)
540 (x
541 (return x)))))
542
0b6f49ef
LC
543(define (operating-system-etc-directory os)
544 "Return that static part of the /etc directory of OS."
0adfe95a
LC
545 (etc-directory
546 (fold-services (operating-system-services os)
547 #:target-type etc-service-type)))
033adfe7 548
6bad7524
SB
549(define (operating-system-environment-variables os)
550 "Return the environment variables of OS for
551@var{session-environment-service-type}, to be used in @file{/etc/environment}."
552 `(("LANG" . ,(operating-system-locale os))
553 ("TZ" . ,(operating-system-timezone os))
9e41130b 554 ("TZDIR" . ,(file-append tzdata "/share/zoneinfo"))
6bad7524
SB
555 ;; Tell 'modprobe' & co. where to look for modules.
556 ("LINUX_MODULE_DIRECTORY" . "/run/booted-system/kernel/lib/modules")
557 ;; These variables are honored by OpenSSL (libssl) and Git.
558 ("SSL_CERT_DIR" . "/etc/ssl/certs")
559 ("SSL_CERT_FILE" . "/etc/ssl/certs/ca-certificates.crt")
560 ("GIT_SSL_CAINFO" . "/etc/ssl/certs/ca-certificates.crt")
ae05e366
LC
561
562 ;; 'GTK_DATA_PREFIX' must name one directory where GTK+ themes are
563 ;; searched for.
564 ("GTK_DATA_PREFIX" . "/run/current-system/profile")
565
6bad7524
SB
566 ;; By default, applications that use D-Bus, such as Emacs, abort at startup
567 ;; when /etc/machine-id is missing. Make sure these warnings are non-fatal.
9f58fe3d 568 ("DBUS_FATAL_WARNINGS" . "0")))
6bad7524 569
09e028f4
LC
570(define %setuid-programs
571 ;; Default set of setuid-root programs.
3b65abac 572 (let ((shadow (@ (gnu packages admin) shadow)))
9e41130b
LC
573 (list (file-append shadow "/bin/passwd")
574 (file-append shadow "/bin/su")
575 (file-append inetutils "/bin/ping")
576 (file-append inetutils "/bin/ping6")
577 (file-append sudo "/bin/sudo")
578 (file-append fuse "/bin/fusermount"))))
69689380
LC
579
580(define %sudoers-specification
581 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
582 ;; group can do anything. See
583 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
584 ;; TODO: Add a declarative API.
84765839
LC
585 (plain-file "sudoers" "\
586root ALL=(ALL) ALL
587%wheel ALL=(ALL) ALL\n"))
09e028f4 588
239db054 589(define* (operating-system-activation-script os #:key container?)
484a2b3a
LC
590 "Return the activation script for OS---i.e., the code that \"activates\" the
591stateful part of OS, including user accounts and groups, special directories,
592etc."
0adfe95a
LC
593 (let* ((services (operating-system-services os #:container? container?))
594 (activation (fold-services services
595 #:target-type activation-service-type)))
596 (activation-service->script activation)))
484a2b3a 597
239db054 598(define* (operating-system-boot-script os #:key container?)
484a2b3a 599 "Return the boot script for OS---i.e., the code started by the initrd once
239db054
DT
600we're running in the final root. When CONTAINER? is true, skip all
601hardware-related operations as necessary when booting a Linux container."
0adfe95a 602 (let* ((services (operating-system-services os #:container? container?))
d62e201c 603 (boot (fold-services services #:target-type boot-service-type)))
0adfe95a
LC
604 ;; BOOT is the script as a monadic value.
605 (service-parameters boot)))
2106d3fc 606
b2fef041
LC
607(define (operating-system-user-accounts os)
608 "Return the list of user accounts of OS."
609 (let* ((services (operating-system-services os))
610 (account (fold-services services
611 #:target-type account-service-type)))
612 (filter user-account?
613 (service-parameters account))))
614
615(define (operating-system-shepherd-service-names os)
616 "Return the list of Shepherd service names for OS."
617 (append-map shepherd-service-provision
618 (service-parameters
619 (fold-services (operating-system-services os)
620 #:target-type
621 shepherd-root-service-type))))
622
d62e201c
LC
623(define* (operating-system-derivation os #:key container?)
624 "Return a derivation that builds OS."
625 (let* ((services (operating-system-services os #:container? container?))
626 (system (fold-services services)))
627 ;; SYSTEM contains the derivation as a monadic value.
628 (service-parameters system)))
629
af4c3fd5
LC
630(define* (operating-system-profile os #:key container?)
631 "Return a derivation that builds the system profile of OS."
632 (mlet* %store-monad
633 ((services -> (operating-system-services os #:container? container?))
634 (profile (fold-services services
635 #:target-type profile-service-type)))
636 (match profile
637 (("profile" profile)
638 (return profile)))))
639
83bcd0b8
LC
640(define (operating-system-root-file-system os)
641 "Return the root file system of OS."
642 (find (match-lambda
d4c87617 643 (($ <file-system> _ _ "/") #t)
83bcd0b8
LC
644 (_ #f))
645 (operating-system-file-systems os)))
646
b4140694
LC
647(define (operating-system-initrd-file os)
648 "Return a gexp denoting the initrd file of OS."
83bcd0b8 649 (define boot-file-systems
4d6b879c 650 (filter file-system-needed-for-boot?
83bcd0b8
LC
651 (operating-system-file-systems os)))
652
de1c158f
LC
653 (define mapped-devices
654 (operating-system-boot-mapped-devices os))
655
656 (define make-initrd
657 (operating-system-initrd os))
658
659 (mlet %store-monad ((initrd (make-initrd boot-file-systems
0d275f4a 660 #:linux (operating-system-kernel os)
de1c158f 661 #:mapped-devices mapped-devices)))
ab20d74a 662 (return (file-append initrd "/initrd"))))
b4140694 663
f5582b2c
LC
664(define (locale-name->definition* name)
665 "Variant of 'locale-name->definition' that raises an error upon failure."
666 (match (locale-name->definition name)
667 (#f
668 (raise (condition
669 (&message
670 (message (format #f (_ "~a: invalid locale name") name))))))
671 (def def)))
672
598e19dc
LC
673(define (operating-system-locale-directory os)
674 "Return the directory containing the locales compiled for the definitions
675listed in OS. The C library expects to find it under
676/run/current-system/locale."
f5582b2c
LC
677 (define name
678 (operating-system-locale os))
679
680 (define definitions
681 ;; While we're at it, check whether NAME is defined and add it if needed.
682 (if (member name (map locale-definition-name
683 (operating-system-locale-definitions os)))
684 (operating-system-locale-definitions os)
685 (cons (locale-name->definition* name)
686 (operating-system-locale-definitions os))))
687
688 (locale-directory definitions
34760ae7 689 #:libcs (operating-system-locale-libcs os)))
598e19dc 690
2d23e6f0
LC
691(define (kernel->grub-label kernel)
692 "Return a label for the GRUB menu entry that boots KERNEL."
42de9608 693 (string-append "GNU with "
2d23e6f0
LC
694 (string-titlecase (package-name kernel)) " "
695 (package-version kernel)
d0a65256 696 " (beta)"))
2d23e6f0 697
6b779207
LC
698(define (store-file-system file-systems)
699 "Return the file system object among FILE-SYSTEMS that contains the store."
700 (match (filter (lambda (fs)
701 (and (file-system-mount? fs)
702 (not (memq 'bind-mount (file-system-flags fs)))
703 (string-prefix? (file-system-mount-point fs)
704 (%store-prefix))))
705 file-systems)
706 ((and candidates (head . tail))
707 (reduce (lambda (fs1 fs2)
708 (if (> (string-length (file-system-mount-point fs1))
709 (string-length (file-system-mount-point fs2)))
710 fs1
711 fs2))
712 head
713 candidates))))
714
715(define (operating-system-store-file-system os)
716 "Return the file system that contains the store of OS."
717 (store-file-system (operating-system-file-systems os)))
718
fe6e3fe2
LC
719(define* (operating-system-grub.cfg os #:optional (old-entries '()))
720 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
721\"old entries\" menu."
0b6f49ef 722 (mlet* %store-monad
b4140694 723 ((system (operating-system-derivation os))
83bcd0b8 724 (root-fs -> (operating-system-root-file-system os))
6b779207 725 (store-fs -> (operating-system-store-file-system os))
44d5f54e
LC
726 (label -> (kernel->grub-label (operating-system-kernel os)))
727 (kernel -> (operating-system-kernel-file os))
0f65f54e 728 (initrd (operating-system-initrd-file os))
f453f637
LC
729 (root-device -> (if (eq? 'uuid (file-system-title root-fs))
730 (uuid->string (file-system-device root-fs))
731 (file-system-device root-fs)))
033adfe7 732 (entries -> (list (menu-entry
44d5f54e 733 (label label)
1ef8b72a
CM
734
735 ;; The device where the kernel and initrd live.
736 (device (file-system-device store-fs))
737 (device-mount-point
738 (file-system-mount-point store-fs))
739
033adfe7 740 (linux kernel)
f6a7b21d 741 (linux-arguments
f453f637 742 (cons* (string-append "--root=" root-device)
ee2a6304
LC
743 #~(string-append "--system=" #$system)
744 #~(string-append "--load=" #$system
745 "/boot")
746 (operating-system-kernel-arguments os)))
0f65f54e 747 (initrd initrd)))))
1ef8b72a 748 (grub-configuration-file (operating-system-bootloader os) entries
fe6e3fe2 749 #:old-entries old-entries)))
b4140694 750
64e40dbb
LC
751(define (operating-system-parameters-file os)
752 "Return a file that describes the boot parameters of OS. The primary use of
753this file is the reconstruction of GRUB menu entries for old configurations."
754 (mlet %store-monad ((initrd (operating-system-initrd-file os))
755 (root -> (operating-system-root-file-system os))
1ef8b72a 756 (store -> (operating-system-store-file-system os))
64e40dbb
LC
757 (label -> (kernel->grub-label
758 (operating-system-kernel os))))
759 (gexp->file "parameters"
1ef8b72a
CM
760 #~(boot-parameters
761 (version 0)
762 (label #$label)
763 (root-device #$(file-system-device root))
764 (kernel #$(operating-system-kernel-file os))
765 (kernel-arguments
766 #$(operating-system-kernel-arguments os))
767 (initrd #$initrd)
768 (store
769 (device #$(case (file-system-title store)
770 ((uuid) (file-system-device store))
771 ((label) (file-system-device store))
772 (else #f)))
773 (mount-point #$(file-system-mount-point store))))
2b418579 774 #:set-load-path? #f)))
64e40dbb 775
b8300494
AK
776\f
777;;;
778;;; Boot parameters
779;;;
780
781(define-record-type* <boot-parameters>
782 boot-parameters make-boot-parameters boot-parameters?
783 (label boot-parameters-label)
1ef8b72a
CM
784 ;; Because we will use the 'store-device' to create the GRUB search command,
785 ;; the 'store-device' has slightly different semantics than 'root-device'.
786 ;; The 'store-device' can be a file system uuid, a file system label, or #f,
787 ;; but it cannot be a device path such as "/dev/sda3", since GRUB would not
788 ;; understand that. The 'root-device', on the other hand, corresponds
789 ;; exactly to the device field of the <file-system> object representing the
790 ;; OS's root file system, so it might be a device path like "/dev/sda3".
b8300494 791 (root-device boot-parameters-root-device)
1ef8b72a
CM
792 (store-device boot-parameters-store-device)
793 (store-mount-point boot-parameters-store-mount-point)
b8300494 794 (kernel boot-parameters-kernel)
d7b342d8
LC
795 (kernel-arguments boot-parameters-kernel-arguments)
796 (initrd boot-parameters-initrd))
b8300494
AK
797
798(define (read-boot-parameters port)
799 "Read boot parameters from PORT and return the corresponding
800<boot-parameters> object or #f if the format is unrecognized."
801 (match (read port)
802 (('boot-parameters ('version 0)
803 ('label label) ('root-device root)
804 ('kernel linux)
805 rest ...)
806 (boot-parameters
807 (label label)
808 (root-device root)
44d5f54e
LC
809
810 ;; In the past, we would store the directory name of the kernel instead
811 ;; of the absolute file name of its image. Detect that and correct it.
812 (kernel (if (string=? linux (direct-store-path linux))
813 (string-append linux "/"
814 (system-linux-image-file-name))
815 linux))
816
b8300494
AK
817 (kernel-arguments
818 (match (assq 'kernel-arguments rest)
819 ((_ args) args)
d7b342d8
LC
820 (#f '()))) ;the old format
821
822 (initrd
823 (match (assq 'initrd rest)
824 (('initrd ('string-append directory file)) ;the old format
825 (string-append directory file))
826 (('initrd (? string? file))
1ef8b72a
CM
827 file)))
828
829 (store-device
830 (match (assq 'store rest)
831 (('store ('device device) _ ...)
832 device)
833 (_ ;the old format
834 root)))
835
836 (store-mount-point
837 (match (assq 'store rest)
838 (('store ('device _) ('mount-point mount-point) _ ...)
839 mount-point)
840 (_ ;the old format
841 "/")))))
b8300494
AK
842 (x ;unsupported format
843 (warning (_ "unrecognized boot parameters for '~a'~%")
844 system)
845 #f)))
846
033adfe7 847;;; system.scm ends here