system: Allow users to PTRACE_ATTACH to their own processes.
[jackhill/guix/guix.git] / gnu / system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu system)
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix gexp)
24 #:use-module (guix records)
25 #:use-module (guix packages)
26 #:use-module (guix derivations)
27 #:use-module (guix profiles)
28 #:use-module (gnu packages base)
29 #:use-module (gnu packages bash)
30 #:use-module (gnu packages guile)
31 #:use-module (gnu packages admin)
32 #:use-module (gnu packages linux)
33 #:use-module (gnu packages pciutils)
34 #:use-module (gnu packages package-management)
35 #:use-module (gnu packages less)
36 #:use-module (gnu packages zile)
37 #:use-module (gnu packages nano)
38 #:use-module (gnu packages lsof)
39 #:use-module (gnu packages gawk)
40 #:use-module (gnu packages man)
41 #:use-module (gnu packages compression)
42 #:use-module (gnu packages firmware)
43 #:autoload (gnu packages cryptsetup) (cryptsetup)
44 #:use-module (gnu services)
45 #:use-module (gnu services dmd)
46 #:use-module (gnu services base)
47 #:use-module (gnu system grub)
48 #:use-module (gnu system shadow)
49 #:use-module (gnu system nss)
50 #:use-module (gnu system locale)
51 #:use-module (gnu system linux)
52 #:use-module (gnu system linux-initrd)
53 #:use-module (gnu system file-systems)
54 #:use-module (ice-9 match)
55 #:use-module (srfi srfi-1)
56 #:use-module (srfi srfi-26)
57 #:use-module (srfi srfi-34)
58 #:use-module (srfi srfi-35)
59 #:export (operating-system
60 operating-system?
61
62 operating-system-bootloader
63 operating-system-services
64 operating-system-user-services
65 operating-system-packages
66 operating-system-host-name
67 operating-system-hosts-file
68 operating-system-kernel
69 operating-system-initrd
70 operating-system-users
71 operating-system-groups
72 operating-system-issue
73 operating-system-timezone
74 operating-system-locale
75 operating-system-locale-definitions
76 operating-system-mapped-devices
77 operating-system-file-systems
78 operating-system-activation-script
79
80 operating-system-derivation
81 operating-system-profile
82 operating-system-grub.cfg
83
84 local-host-aliases
85 %setuid-programs
86 %base-packages
87 %base-firmware
88
89 luks-device-mapping))
90
91 ;;; Commentary:
92 ;;;
93 ;;; This module supports whole-system configuration.
94 ;;;
95 ;;; Code:
96
97 ;; System-wide configuration.
98 ;; TODO: Add per-field docstrings/stexi.
99 (define-record-type* <operating-system> operating-system
100 make-operating-system
101 operating-system?
102 (kernel operating-system-kernel ; package
103 (default linux-libre))
104 (bootloader operating-system-bootloader) ; <grub-configuration>
105
106 (initrd operating-system-initrd ; (list fs) -> M derivation
107 (default base-initrd))
108 (firmware operating-system-firmware ; list of packages
109 (default %base-firmware))
110
111 (host-name operating-system-host-name) ; string
112 (hosts-file operating-system-hosts-file ; M item | #f
113 (default #f))
114
115 (mapped-devices operating-system-mapped-devices ; list of <mapped-device>
116 (default '()))
117 (file-systems operating-system-file-systems) ; list of fs
118 (swap-devices operating-system-swap-devices ; list of strings
119 (default '()))
120
121 (users operating-system-users ; list of user accounts
122 (default '()))
123 (groups operating-system-groups ; list of user groups
124 (default %base-groups))
125
126 (skeletons operating-system-skeletons ; list of name/monadic value
127 (default (default-skeletons)))
128 (issue operating-system-issue ; string
129 (default %default-issue))
130
131 (packages operating-system-packages ; list of (PACKAGE OUTPUT...)
132 (default %base-packages)) ; or just PACKAGE
133
134 (timezone operating-system-timezone) ; string
135 (locale operating-system-locale ; string
136 (default "en_US.utf8"))
137 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
138 (default %default-locale-definitions))
139 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
140 (default %default-nss))
141
142 (services operating-system-user-services ; list of monadic services
143 (default %base-services))
144
145 (pam-services operating-system-pam-services ; list of PAM services
146 (default (base-pam-services)))
147 (setuid-programs operating-system-setuid-programs
148 (default %setuid-programs)) ; list of string-valued gexps
149
150 (sudoers operating-system-sudoers ; /etc/sudoers contents
151 (default %sudoers-specification)))
152
153 \f
154 ;;;
155 ;;; Derivation.
156 ;;;
157
158 (define* (file-union name files)
159 "Return a derivation that builds a directory containing all of FILES. Each
160 item in FILES must be a list where the first element is the file name to use
161 in the new directory, and the second element is a gexp denoting the target
162 file."
163 (define builder
164 #~(begin
165 (mkdir #$output)
166 (chdir #$output)
167 #$@(map (match-lambda
168 ((target source)
169 #~(symlink #$source #$target)))
170 files)))
171
172 (gexp->derivation name builder))
173
174 (define (directory-union name things)
175 "Return a directory that is the union of THINGS."
176 (match things
177 ((one)
178 ;; Only one thing; return it.
179 (with-monad %store-monad (return one)))
180 (_
181 (gexp->derivation name
182 #~(begin
183 (use-modules (guix build union))
184 (union-build #$output '#$things))
185 #:modules '((guix build union))
186 #:local-build? #t))))
187
188 \f
189 ;;;
190 ;;; Services.
191 ;;;
192
193 (define (open-luks-device source target)
194 "Return a gexp that maps SOURCE to TARGET as a LUKS device, using
195 'cryptsetup'."
196 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
197 "open" "--type" "luks"
198 #$source #$target)))
199
200 (define (close-luks-device source target)
201 "Return a gexp that closes TARGET, a LUKS device."
202 #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup")
203 "close" #$target)))
204
205 (define luks-device-mapping
206 ;; The type of LUKS mapped devices.
207 (mapped-device-kind
208 (open open-luks-device)
209 (close close-luks-device)))
210
211 (define (other-file-system-services os)
212 "Return file system services for the file systems of OS that are not marked
213 as 'needed-for-boot'."
214 (define file-systems
215 (remove file-system-needed-for-boot?
216 (operating-system-file-systems os)))
217
218 (define (device-mappings fs)
219 (filter (lambda (md)
220 (string=? (string-append "/dev/mapper/"
221 (mapped-device-target md))
222 (file-system-device fs)))
223 (operating-system-mapped-devices os)))
224
225 (define (requirements fs)
226 (map (lambda (md)
227 (symbol-append 'device-mapping-
228 (string->symbol (mapped-device-target md))))
229 (device-mappings fs)))
230
231 (sequence %store-monad
232 (map (lambda (fs)
233 (match fs
234 (($ <file-system> device title target type flags opts
235 #f check? create?)
236 (file-system-service device target type
237 #:title title
238 #:requirements (requirements fs)
239 #:check? check?
240 #:create-mount-point? create?
241 #:options opts
242 #:flags flags))))
243 file-systems)))
244
245 (define (mapped-device-user device file-systems)
246 "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f."
247 (let ((target (string-append "/dev/mapper/" (mapped-device-target device))))
248 (find (lambda (fs)
249 (string=? (file-system-device fs) target))
250 file-systems)))
251
252 (define (operating-system-user-mapped-devices os)
253 "Return the subset of mapped devices that can be installed in
254 user-land--i.e., those not needed during boot."
255 (let ((devices (operating-system-mapped-devices os))
256 (file-systems (operating-system-file-systems os)))
257 (filter (lambda (md)
258 (let ((user (mapped-device-user md file-systems)))
259 (or (not user)
260 (not (file-system-needed-for-boot? user)))))
261 devices)))
262
263 (define (operating-system-boot-mapped-devices os)
264 "Return the subset of mapped devices that must be installed during boot,
265 from the initrd."
266 (let ((devices (operating-system-mapped-devices os))
267 (file-systems (operating-system-file-systems os)))
268 (filter (lambda (md)
269 (let ((user (mapped-device-user md file-systems)))
270 (and user (file-system-needed-for-boot? user))))
271 devices)))
272
273 (define (device-mapping-services os)
274 "Return the list of device-mapping services for OS as a monadic list."
275 (sequence %store-monad
276 (map (lambda (md)
277 (let* ((source (mapped-device-source md))
278 (target (mapped-device-target md))
279 (type (mapped-device-type md))
280 (open (mapped-device-kind-open type))
281 (close (mapped-device-kind-close type)))
282 (device-mapping-service target
283 (open source target)
284 (close source target))))
285 (operating-system-user-mapped-devices os))))
286
287 (define (swap-services os)
288 "Return the list of swap services for OS as a monadic list."
289 (sequence %store-monad
290 (map swap-service (operating-system-swap-devices os))))
291
292 (define (essential-services os)
293 "Return the list of essential services for OS. These are special services
294 that implement part of what's declared in OS are responsible for low-level
295 bookkeeping."
296 (define known-fs
297 (map file-system-mount-point (operating-system-file-systems os)))
298
299 (mlet* %store-monad ((mappings (device-mapping-services os))
300 (root-fs (root-file-system-service))
301 (other-fs (other-file-system-services os))
302 (unmount (user-unmount-service known-fs))
303 (swaps (swap-services os))
304 (procs (user-processes-service
305 (map (compose first service-provision)
306 other-fs)))
307 (host-name (host-name-service
308 (operating-system-host-name os))))
309 (return (cons* host-name procs root-fs unmount
310 (append other-fs mappings swaps)))))
311
312 (define (operating-system-services os)
313 "Return all the services of OS, including \"internal\" services that do not
314 explicitly appear in OS."
315 (mlet %store-monad
316 ((user (sequence %store-monad (operating-system-user-services os)))
317 (essential (essential-services os)))
318 (return (append essential user))))
319
320 \f
321 ;;;
322 ;;; /etc.
323 ;;;
324
325 (define %base-firmware
326 ;; Firmware usable by default.
327 (list ath9k-htc-firmware))
328
329 (define %base-packages
330 ;; Default set of packages globally visible. It should include anything
331 ;; required for basic administrator tasks.
332 (cons* procps psmisc which less zile nano
333 (@ (gnu packages admin) dmd) guix
334 lsof ;for Guix's 'list-runtime-roots'
335 pciutils usbutils
336 util-linux inetutils isc-dhcp
337
338 ;; wireless-tools is deprecated in favor of iw, but it's still what
339 ;; many people are familiar with, so keep it around.
340 iw wireless-tools
341
342 net-tools ; XXX: remove when Inetutils suffices
343 man-db
344
345 ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also
346 ;; want the other commands and the man pages (notably because
347 ;; auto-completion in Emacs shell relies on man pages.)
348 sudo
349
350 ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev
351 ;; already depends on it anyway.
352 kmod eudev
353
354 e2fsprogs kbd
355
356 bash-completion
357
358 ;; The packages below are also in %FINAL-INPUTS, so take them from
359 ;; there to avoid duplication.
360 (map canonical-package
361 (list guile-2.0 bash coreutils findutils grep sed
362 diffutils patch gawk tar gzip bzip2 xz lzip))))
363
364 (define %default-issue
365 ;; Default contents for /etc/issue.
366 "
367 This is the GNU system. Welcome.\n")
368
369 (define (local-host-aliases host-name)
370 "Return aliases for HOST-NAME, to be used in /etc/hosts."
371 (string-append "127.0.0.1 localhost " host-name "\n"
372 "::1 localhost " host-name "\n"))
373
374 (define (default-/etc/hosts host-name)
375 "Return the default /etc/hosts file."
376 (text-file "hosts" (local-host-aliases host-name)))
377
378 (define (emacs-site-file)
379 "Return the Emacs 'site-start.el' file. That file contains the necessary
380 settings for 'guix.el' to work out-of-the-box."
381 (gexp->file "site-start.el"
382 #~(progn
383 ;; Add the "normal" elisp directory to the search path;
384 ;; guix.el may be there.
385 (add-to-list
386 'load-path
387 "/run/current-system/profile/share/emacs/site-lisp")
388
389 ;; Attempt to load guix.el.
390 (require 'guix-init nil t)
391
392 (when (require 'geiser-guile nil t)
393 ;; Make sure Geiser's Scheme modules are in Guile's search
394 ;; path.
395 (add-to-list
396 'geiser-guile-load-path
397 "/run/current-system/profile/share/geiser/guile")))))
398
399 (define (emacs-site-directory)
400 "Return the Emacs site directory, aka. /etc/emacs."
401 (mlet %store-monad ((file (emacs-site-file)))
402 (gexp->derivation "emacs"
403 #~(begin
404 (mkdir #$output)
405 (chdir #$output)
406 (symlink #$file "site-start.el")))))
407
408 (define* (etc-directory #:key
409 (locale "C") (timezone "Europe/Paris")
410 (issue "Hello!\n")
411 (skeletons '())
412 (pam-services '())
413 (profile "/run/current-system/profile")
414 hosts-file nss
415 (sudoers ""))
416 "Return a derivation that builds the static part of the /etc directory."
417 (mlet* %store-monad
418 ((pam.d (pam-services->directory pam-services))
419 (sudoers (text-file "sudoers" sudoers))
420 (login.defs (text-file "login.defs" "# Empty for now.\n"))
421
422 ;; /etc/shells is used by xterm and other programs. We don't check
423 ;; whether these shells are installed, should be OK.
424 (shells (text-file "shells"
425 "\
426 /bin/sh
427 /run/current-system/profile/bin/sh
428 /run/current-system/profile/bin/bash
429 /run/current-system/profile/bin/fish
430 /run/current-system/profile/bin/tcsh
431 /run/current-system/profile/bin/zsh\n"))
432 (emacs (emacs-site-directory))
433 (issue (text-file "issue" issue))
434 (nsswitch (text-file "nsswitch.conf"
435 (name-service-switch->string nss)))
436
437 ;; Startup file for POSIX-compliant login shells, which set system-wide
438 ;; environment variables.
439 (profile (text-file* "profile" "\
440 export LANG=\"" locale "\"
441 export TZ=\"" timezone "\"
442 export TZDIR=\"" tzdata "/share/zoneinfo\"
443
444 # Tell 'modprobe' & co. where to look for modules.
445 export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules
446
447 export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin
448 export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH
449 export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man
450 export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info
451
452 export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share
453 export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg
454
455 # Append the directory of 'site-start.el' to the search path.
456 export EMACSLOADPATH=:/etc/emacs
457
458 # By default, applications that use D-Bus, such as Emacs, abort at startup
459 # when /etc/machine-id is missing. Make sure these warnings are non-fatal.
460 export DBUS_FATAL_WARNINGS=0
461
462 # These variables are honored by OpenSSL (libssl) and Git.
463 export SSL_CERT_DIR=/etc/ssl/certs
464 export SSL_CERT_FILE=\"$SSL_CERT_DIR/ca-certificates.crt\"
465 export GIT_SSL_CAINFO=\"$SSL_CERT_FILE\"
466
467 # Allow Aspell to find dictionaries installed in the user profile.
468 export ASPELL_CONF=\"dict-dir $HOME/.guix-profile/lib/aspell\"
469
470 if [ -n \"$BASH_VERSION\" -a -f /etc/bashrc ]
471 then
472 # Load Bash-specific initialization code.
473 source /etc/bashrc
474 fi
475 "))
476
477 (bashrc (text-file "bashrc" "\
478 # Bash-specific initialization.
479
480 # The 'bash-completion' package.
481 if [ -f /run/current-system/profile/etc/profile.d/bash_completion.sh ]
482 then
483 # Bash-completion sources ~/.bash_completion. It installs a dynamic
484 # completion loader that searches its own completion files as well
485 # as those in ~/.guix-profile and /run/current-system/profile.
486 source /run/current-system/profile/etc/profile.d/bash_completion.sh
487 fi\n"))
488 (skel (skeleton-directory skeletons)))
489 (file-union "etc"
490 `(("services" ,#~(string-append #$net-base "/etc/services"))
491 ("protocols" ,#~(string-append #$net-base "/etc/protocols"))
492 ("rpc" ,#~(string-append #$net-base "/etc/rpc"))
493 ("emacs" ,#~#$emacs)
494 ("pam.d" ,#~#$pam.d)
495 ("login.defs" ,#~#$login.defs)
496 ("issue" ,#~#$issue)
497 ("nsswitch.conf" ,#~#$nsswitch)
498 ("skel" ,#~#$skel)
499 ("shells" ,#~#$shells)
500 ("profile" ,#~#$profile)
501 ("bashrc" ,#~#$bashrc)
502 ("hosts" ,#~#$hosts-file)
503 ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/"
504 #$timezone))
505 ("sudoers" ,#~#$sudoers)))))
506
507 (define (operating-system-profile os)
508 "Return a derivation that builds the system profile of OS."
509 (profile-derivation (manifest (map package->manifest-entry
510 (operating-system-packages os)))))
511
512 (define %root-account
513 ;; Default root account.
514 (user-account
515 (name "root")
516 (password "")
517 (uid 0) (group "root")
518 (comment "System administrator")
519 (home-directory "/root")))
520
521 (define (operating-system-accounts os)
522 "Return the user accounts for OS, including an obligatory 'root' account."
523 (define users
524 ;; Make sure there's a root account.
525 (if (find (lambda (user)
526 (and=> (user-account-uid user) zero?))
527 (operating-system-users os))
528 (operating-system-users os)
529 (cons %root-account (operating-system-users os))))
530
531 (mlet %store-monad ((services (operating-system-services os)))
532 (return (append users
533 (append-map service-user-accounts services)))))
534
535 (define (operating-system-etc-directory os)
536 "Return that static part of the /etc directory of OS."
537 (mlet* %store-monad
538 ((services (operating-system-services os))
539 (pam-services ->
540 ;; Services known to PAM.
541 (append (operating-system-pam-services os)
542 (append-map service-pam-services services)))
543 (profile-drv (operating-system-profile os))
544 (skeletons (operating-system-skeletons os))
545 (/etc/hosts (or (operating-system-hosts-file os)
546 (default-/etc/hosts (operating-system-host-name os)))))
547 (etc-directory #:pam-services pam-services
548 #:skeletons skeletons
549 #:issue (operating-system-issue os)
550 #:locale (operating-system-locale os)
551 #:nss (operating-system-name-service-switch os)
552 #:timezone (operating-system-timezone os)
553 #:hosts-file /etc/hosts
554 #:sudoers (operating-system-sudoers os)
555 #:profile profile-drv)))
556
557 (define %setuid-programs
558 ;; Default set of setuid-root programs.
559 (let ((shadow (@ (gnu packages admin) shadow)))
560 (list #~(string-append #$shadow "/bin/passwd")
561 #~(string-append #$shadow "/bin/su")
562 #~(string-append #$inetutils "/bin/ping")
563 #~(string-append #$sudo "/bin/sudo")
564 #~(string-append #$fuse "/bin/fusermount"))))
565
566 (define %sudoers-specification
567 ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel'
568 ;; group can do anything. See
569 ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>.
570 ;; TODO: Add a declarative API.
571 "root ALL=(ALL) ALL
572 %wheel ALL=(ALL) ALL\n")
573
574 (define (user-group->gexp group)
575 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
576 'active-groups'."
577 #~(list #$(user-group-name group)
578 #$(user-group-password group)
579 #$(user-group-id group)
580 #$(user-group-system? group)))
581
582 (define (user-account->gexp account)
583 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
584 'activate-users'."
585 #~`(#$(user-account-name account)
586 #$(user-account-uid account)
587 #$(user-account-group account)
588 #$(user-account-supplementary-groups account)
589 #$(user-account-comment account)
590 #$(user-account-home-directory account)
591 ,#$(user-account-shell account) ; this one is a gexp
592 #$(user-account-password account)
593 #$(user-account-system? account)))
594
595 (define (modprobe-wrapper)
596 "Return a wrapper for the 'modprobe' command that knows where modules live.
597
598 This wrapper is typically invoked by the Linux kernel ('call_modprobe', in
599 kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment
600 variable is not set---hence the need for this wrapper."
601 (let ((modprobe "/run/current-system/profile/bin/modprobe"))
602 (gexp->script "modprobe"
603 #~(begin
604 (setenv "LINUX_MODULE_DIRECTORY"
605 "/run/booted-system/kernel/lib/modules")
606 (apply execl #$modprobe
607 (cons #$modprobe (cdr (command-line))))))))
608
609 (define (operating-system-activation-script os)
610 "Return the activation script for OS---i.e., the code that \"activates\" the
611 stateful part of OS, including user accounts and groups, special directories,
612 etc."
613 (define %modules
614 '((gnu build activation)
615 (gnu build linux-boot)
616 (gnu build linux-modules)
617 (gnu build file-systems)
618 (guix build utils)
619 (guix elf)))
620
621 (define (service-activations services)
622 ;; Return the activation scripts for SERVICES.
623 (let ((gexps (filter-map service-activate services)))
624 (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>)
625 gexps))))
626
627 (mlet* %store-monad ((services (operating-system-services os))
628 (actions (service-activations services))
629 (etc (operating-system-etc-directory os))
630 (modules (imported-modules %modules))
631 (compiled (compiled-modules %modules))
632 (modprobe (modprobe-wrapper))
633 (firmware (directory-union
634 "firmware" (operating-system-firmware os)))
635 (accounts (operating-system-accounts os)))
636 (define setuid-progs
637 (operating-system-setuid-programs os))
638
639 (define user-specs
640 (map user-account->gexp accounts))
641
642 (define groups
643 (append (operating-system-groups os)
644 (append-map service-user-groups services)))
645
646 (define group-specs
647 (map user-group->gexp groups))
648
649 (gexp->file "activate"
650 #~(begin
651 (eval-when (expand load eval)
652 ;; Make sure 'use-modules' below succeeds.
653 (set! %load-path (cons #$modules %load-path))
654 (set! %load-compiled-path
655 (cons #$compiled %load-compiled-path)))
656
657 (use-modules (gnu build activation))
658
659 ;; Make sure /bin/sh is valid and current.
660 (activate-/bin/sh
661 (string-append #$(canonical-package bash)
662 "/bin/sh"))
663
664 ;; Populate /etc.
665 (activate-etc #$etc)
666
667 ;; Add users and user groups.
668 (setenv "PATH"
669 (string-append #$(@ (gnu packages admin) shadow)
670 "/sbin"))
671 (activate-users+groups (list #$@user-specs)
672 (list #$@group-specs))
673
674 ;; Activate setuid programs.
675 (activate-setuid-programs (list #$@setuid-progs))
676
677 ;; Tell the kernel to use our 'modprobe' command.
678 (activate-modprobe #$modprobe)
679
680 ;; Tell the kernel where firmware is.
681 (activate-firmware
682 (string-append #$firmware "/lib/firmware"))
683
684 ;; Let users debug their own processes!
685 (activate-ptrace-attach)
686
687 ;; Run the services' activation snippets.
688 ;; TODO: Use 'load-compiled'.
689 (for-each primitive-load '#$actions)
690
691 ;; Set up /run/current-system.
692 (activate-current-system)))))
693
694 (define (operating-system-boot-script os)
695 "Return the boot script for OS---i.e., the code started by the initrd once
696 we're running in the final root."
697 (mlet* %store-monad ((services (operating-system-services os))
698 (activate (operating-system-activation-script os))
699 (dmd-conf (dmd-configuration-file services)))
700 (gexp->file "boot"
701 #~(begin
702 (use-modules (guix build utils))
703
704 ;; Clean out /tmp and /var/run.
705 ;;
706 ;; XXX This needs to happen before service activations, so
707 ;; it has to be here, but this also implicitly assumes
708 ;; that /tmp and /var/run are on the root partition.
709 (false-if-exception (delete-file-recursively "/tmp"))
710 (false-if-exception (delete-file-recursively "/var/run"))
711 (false-if-exception (mkdir "/tmp"))
712 (false-if-exception (chmod "/tmp" #o1777))
713 (false-if-exception (mkdir "/var/run"))
714 (false-if-exception (chmod "/var/run" #o755))
715
716 ;; Activate the system.
717 ;; TODO: Use 'load-compiled'.
718 (primitive-load #$activate)
719
720 ;; Keep track of the booted system.
721 (false-if-exception (delete-file "/run/booted-system"))
722 (symlink (readlink "/run/current-system")
723 "/run/booted-system")
724
725 ;; Close any remaining open file descriptors to be on the
726 ;; safe side. This must be the very last thing we do,
727 ;; because Guile has internal FDs such as 'sleep_pipe'
728 ;; that need to be alive.
729 (let loop ((fd 3))
730 (when (< fd 1024)
731 (false-if-exception (close-fdes fd))
732 (loop (+ 1 fd))))
733
734 ;; Start dmd.
735 (execl (string-append #$dmd "/bin/dmd")
736 "dmd" "--config" #$dmd-conf)))))
737
738 (define (operating-system-root-file-system os)
739 "Return the root file system of OS."
740 (find (match-lambda
741 (($ <file-system> _ _ "/") #t)
742 (_ #f))
743 (operating-system-file-systems os)))
744
745 (define (operating-system-initrd-file os)
746 "Return a gexp denoting the initrd file of OS."
747 (define boot-file-systems
748 (filter file-system-needed-for-boot?
749 (operating-system-file-systems os)))
750
751 (define mapped-devices
752 (operating-system-boot-mapped-devices os))
753
754 (define make-initrd
755 (operating-system-initrd os))
756
757 (mlet %store-monad ((initrd (make-initrd boot-file-systems
758 #:linux (operating-system-kernel os)
759 #:mapped-devices mapped-devices)))
760 (return #~(string-append #$initrd "/initrd"))))
761
762 (define (operating-system-locale-directory os)
763 "Return the directory containing the locales compiled for the definitions
764 listed in OS. The C library expects to find it under
765 /run/current-system/locale."
766 ;; While we're at it, check whether the locale of OS is defined.
767 (unless (member (operating-system-locale os)
768 (map locale-definition-name
769 (operating-system-locale-definitions os)))
770 (raise (condition
771 (&message (message "system locale lacks a definition")))))
772
773 (locale-directory (operating-system-locale-definitions os)))
774
775 (define (kernel->grub-label kernel)
776 "Return a label for the GRUB menu entry that boots KERNEL."
777 (string-append "GNU with "
778 (string-titlecase (package-name kernel)) " "
779 (package-version kernel)
780 " (alpha)"))
781
782 (define* (operating-system-grub.cfg os #:optional (old-entries '()))
783 "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the
784 \"old entries\" menu."
785 (mlet* %store-monad
786 ((system (operating-system-derivation os))
787 (root-fs -> (operating-system-root-file-system os))
788 (kernel -> (operating-system-kernel os))
789 (entries -> (list (menu-entry
790 (label (kernel->grub-label kernel))
791 (linux kernel)
792 (linux-arguments
793 (list (string-append "--root="
794 (file-system-device root-fs))
795 #~(string-append "--system=" #$system)
796 #~(string-append "--load=" #$system
797 "/boot")))
798 (initrd #~(string-append #$system "/initrd"))))))
799 (grub-configuration-file (operating-system-bootloader os) entries
800 #:old-entries old-entries)))
801
802 (define (operating-system-parameters-file os)
803 "Return a file that describes the boot parameters of OS. The primary use of
804 this file is the reconstruction of GRUB menu entries for old configurations."
805 (mlet %store-monad ((initrd (operating-system-initrd-file os))
806 (root -> (operating-system-root-file-system os))
807 (label -> (kernel->grub-label
808 (operating-system-kernel os))))
809 (gexp->file "parameters"
810 #~(boot-parameters (version 0)
811 (label #$label)
812 (root-device #$(file-system-device root))
813 (kernel #$(operating-system-kernel os))
814 (initrd #$initrd)))))
815
816 (define (operating-system-derivation os)
817 "Return a derivation that builds OS."
818 (mlet* %store-monad
819 ((profile (operating-system-profile os))
820 (etc (operating-system-etc-directory os))
821 (boot (operating-system-boot-script os))
822 (kernel -> (operating-system-kernel os))
823 (initrd (operating-system-initrd-file os))
824 (locale (operating-system-locale-directory os))
825 (params (operating-system-parameters-file os)))
826 (file-union "system"
827 `(("boot" ,#~#$boot)
828 ("kernel" ,#~#$kernel)
829 ("parameters" ,#~#$params)
830 ("initrd" ,initrd)
831 ("profile" ,#~#$profile)
832 ("locale" ,#~#$locale) ;used by libc
833 ("etc" ,#~#$etc)))))
834
835 ;;; system.scm ends here