| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; |
| 4 | ;;; This file is part of GNU Guix. |
| 5 | ;;; |
| 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 7 | ;;; under the terms of the GNU General Public License as published by |
| 8 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
| 9 | ;;; your option) any later version. |
| 10 | ;;; |
| 11 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | ;;; GNU General Public License for more details. |
| 15 | ;;; |
| 16 | ;;; You should have received a copy of the GNU General Public License |
| 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | (define-module (gnu system) |
| 20 | #:use-module (guix store) |
| 21 | #:use-module (guix monads) |
| 22 | #:use-module (guix gexp) |
| 23 | #:use-module (guix records) |
| 24 | #:use-module (guix packages) |
| 25 | #:use-module (guix derivations) |
| 26 | #:use-module (guix profiles) |
| 27 | #:use-module (gnu packages base) |
| 28 | #:use-module (gnu packages bash) |
| 29 | #:use-module (gnu packages guile) |
| 30 | #:use-module (gnu packages admin) |
| 31 | #:use-module (gnu packages linux) |
| 32 | #:use-module (gnu packages pciutils) |
| 33 | #:use-module (gnu packages package-management) |
| 34 | #:use-module (gnu packages less) |
| 35 | #:use-module (gnu packages zile) |
| 36 | #:use-module (gnu packages nano) |
| 37 | #:use-module (gnu packages lsof) |
| 38 | #:use-module (gnu packages gawk) |
| 39 | #:use-module (gnu packages man) |
| 40 | #:use-module (gnu packages compression) |
| 41 | #:use-module (gnu packages firmware) |
| 42 | #:autoload (gnu packages cryptsetup) (cryptsetup) |
| 43 | #:use-module (gnu services) |
| 44 | #:use-module (gnu services dmd) |
| 45 | #:use-module (gnu services base) |
| 46 | #:use-module (gnu system grub) |
| 47 | #:use-module (gnu system shadow) |
| 48 | #:use-module (gnu system nss) |
| 49 | #:use-module (gnu system locale) |
| 50 | #:use-module (gnu system linux) |
| 51 | #:use-module (gnu system linux-initrd) |
| 52 | #:use-module (gnu system file-systems) |
| 53 | #:use-module (ice-9 match) |
| 54 | #:use-module (srfi srfi-1) |
| 55 | #:use-module (srfi srfi-26) |
| 56 | #:use-module (srfi srfi-34) |
| 57 | #:use-module (srfi srfi-35) |
| 58 | #:export (operating-system |
| 59 | operating-system? |
| 60 | |
| 61 | operating-system-bootloader |
| 62 | operating-system-services |
| 63 | operating-system-user-services |
| 64 | operating-system-packages |
| 65 | operating-system-host-name |
| 66 | operating-system-hosts-file |
| 67 | operating-system-kernel |
| 68 | operating-system-initrd |
| 69 | operating-system-users |
| 70 | operating-system-groups |
| 71 | operating-system-issue |
| 72 | operating-system-packages |
| 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 | ;; The packages below are also in %FINAL-INPUTS, so take them from |
| 357 | ;; there to avoid duplication. |
| 358 | (map canonical-package |
| 359 | (list guile-2.0 bash coreutils findutils grep sed |
| 360 | diffutils patch gawk tar gzip bzip2 xz lzip)))) |
| 361 | |
| 362 | (define %default-issue |
| 363 | ;; Default contents for /etc/issue. |
| 364 | " |
| 365 | This is the GNU system. Welcome.\n") |
| 366 | |
| 367 | (define (local-host-aliases host-name) |
| 368 | "Return aliases for HOST-NAME, to be used in /etc/hosts." |
| 369 | (string-append "127.0.0.1 localhost " host-name "\n" |
| 370 | "::1 localhost " host-name "\n")) |
| 371 | |
| 372 | (define (default-/etc/hosts host-name) |
| 373 | "Return the default /etc/hosts file." |
| 374 | (text-file "hosts" (local-host-aliases host-name))) |
| 375 | |
| 376 | (define (emacs-site-file) |
| 377 | "Return the Emacs 'site-start.el' file. That file contains the necessary |
| 378 | settings for 'guix.el' to work out-of-the-box." |
| 379 | (gexp->file "site-start.el" |
| 380 | #~(progn |
| 381 | ;; Add the "normal" elisp directory to the search path; |
| 382 | ;; guix.el may be there. |
| 383 | (add-to-list |
| 384 | 'load-path |
| 385 | "/run/current-system/profile/share/emacs/site-lisp") |
| 386 | |
| 387 | ;; Attempt to load guix.el. |
| 388 | (require 'guix-init nil t) |
| 389 | |
| 390 | (when (require 'geiser-guile nil t) |
| 391 | ;; Make sure Geiser's Scheme modules are in Guile's search |
| 392 | ;; path. |
| 393 | (add-to-list |
| 394 | 'geiser-guile-load-path |
| 395 | "/run/current-system/profile/share/geiser/guile"))))) |
| 396 | |
| 397 | (define (emacs-site-directory) |
| 398 | "Return the Emacs site directory, aka. /etc/emacs." |
| 399 | (mlet %store-monad ((file (emacs-site-file))) |
| 400 | (gexp->derivation "emacs" |
| 401 | #~(begin |
| 402 | (mkdir #$output) |
| 403 | (chdir #$output) |
| 404 | (symlink #$file "site-start.el"))))) |
| 405 | |
| 406 | (define* (etc-directory #:key |
| 407 | (locale "C") (timezone "Europe/Paris") |
| 408 | (issue "Hello!\n") |
| 409 | (skeletons '()) |
| 410 | (pam-services '()) |
| 411 | (profile "/run/current-system/profile") |
| 412 | hosts-file nss |
| 413 | (sudoers "")) |
| 414 | "Return a derivation that builds the static part of the /etc directory." |
| 415 | (mlet* %store-monad |
| 416 | ((pam.d (pam-services->directory pam-services)) |
| 417 | (sudoers (text-file "sudoers" sudoers)) |
| 418 | (login.defs (text-file "login.defs" "# Empty for now.\n")) |
| 419 | (shells (text-file "shells" ; used by xterm and others |
| 420 | "\ |
| 421 | /bin/sh |
| 422 | /run/current-system/profile/bin/sh |
| 423 | /run/current-system/profile/bin/bash\n")) |
| 424 | (emacs (emacs-site-directory)) |
| 425 | (issue (text-file "issue" issue)) |
| 426 | (nsswitch (text-file "nsswitch.conf" |
| 427 | (name-service-switch->string nss))) |
| 428 | |
| 429 | ;; Startup file for POSIX-compliant login shells, which set system-wide |
| 430 | ;; environment variables. |
| 431 | (profile (text-file* "profile" "\ |
| 432 | export LANG=\"" locale "\" |
| 433 | export TZ=\"" timezone "\" |
| 434 | export TZDIR=\"" tzdata "/share/zoneinfo\" |
| 435 | |
| 436 | # Tell 'modprobe' & co. where to look for modules. |
| 437 | export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules |
| 438 | |
| 439 | export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin |
| 440 | export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH |
| 441 | export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man |
| 442 | export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info |
| 443 | |
| 444 | export XDG_DATA_DIRS=$HOME/.guix-profile/share:/run/current-system/profile/share |
| 445 | export XDG_CONFIG_DIRS=$HOME/.guix-profile/etc/xdg:/run/current-system/profile/etc/xdg |
| 446 | |
| 447 | # Append the directory of 'site-start.el' to the search path. |
| 448 | export EMACSLOADPATH=:/etc/emacs |
| 449 | |
| 450 | # By default, applications that use D-Bus, such as Emacs, abort at startup |
| 451 | # when /etc/machine-id is missing. Make sure these warnings are non-fatal. |
| 452 | export DBUS_FATAL_WARNINGS=0 |
| 453 | |
| 454 | # These variables are honored by OpenSSL (libssl) and Git. |
| 455 | export SSL_CERT_DIR=/etc/ssl/certs |
| 456 | export SSL_CERT_FILE=\"$SSL_CERT_DIR/ca-certificates.crt\" |
| 457 | export GIT_SSL_CAINFO=\"$SSL_CERT_FILE\" |
| 458 | |
| 459 | # Allow Aspell to find dictionaries installed in the user profile. |
| 460 | export ASPELL_CONF=\"dict-dir $HOME/.guix-profile/lib/aspell\" |
| 461 | ")) |
| 462 | (skel (skeleton-directory skeletons))) |
| 463 | (file-union "etc" |
| 464 | `(("services" ,#~(string-append #$net-base "/etc/services")) |
| 465 | ("protocols" ,#~(string-append #$net-base "/etc/protocols")) |
| 466 | ("rpc" ,#~(string-append #$net-base "/etc/rpc")) |
| 467 | ("emacs" ,#~#$emacs) |
| 468 | ("pam.d" ,#~#$pam.d) |
| 469 | ("login.defs" ,#~#$login.defs) |
| 470 | ("issue" ,#~#$issue) |
| 471 | ("nsswitch.conf" ,#~#$nsswitch) |
| 472 | ("skel" ,#~#$skel) |
| 473 | ("shells" ,#~#$shells) |
| 474 | ("profile" ,#~#$profile) |
| 475 | ("hosts" ,#~#$hosts-file) |
| 476 | ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/" |
| 477 | #$timezone)) |
| 478 | ("sudoers" ,#~#$sudoers))))) |
| 479 | |
| 480 | (define (operating-system-profile os) |
| 481 | "Return a derivation that builds the system profile of OS." |
| 482 | (profile-derivation (manifest (map package->manifest-entry |
| 483 | (operating-system-packages os))))) |
| 484 | |
| 485 | (define %root-account |
| 486 | ;; Default root account. |
| 487 | (user-account |
| 488 | (name "root") |
| 489 | (password "") |
| 490 | (uid 0) (group "root") |
| 491 | (comment "System administrator") |
| 492 | (home-directory "/root"))) |
| 493 | |
| 494 | (define (operating-system-accounts os) |
| 495 | "Return the user accounts for OS, including an obligatory 'root' account." |
| 496 | (define users |
| 497 | ;; Make sure there's a root account. |
| 498 | (if (find (lambda (user) |
| 499 | (and=> (user-account-uid user) zero?)) |
| 500 | (operating-system-users os)) |
| 501 | (operating-system-users os) |
| 502 | (cons %root-account (operating-system-users os)))) |
| 503 | |
| 504 | (mlet %store-monad ((services (operating-system-services os))) |
| 505 | (return (append users |
| 506 | (append-map service-user-accounts services))))) |
| 507 | |
| 508 | (define (operating-system-etc-directory os) |
| 509 | "Return that static part of the /etc directory of OS." |
| 510 | (mlet* %store-monad |
| 511 | ((services (operating-system-services os)) |
| 512 | (pam-services -> |
| 513 | ;; Services known to PAM. |
| 514 | (delete-duplicates |
| 515 | (append (operating-system-pam-services os) |
| 516 | (append-map service-pam-services services)))) |
| 517 | (profile-drv (operating-system-profile os)) |
| 518 | (skeletons (operating-system-skeletons os)) |
| 519 | (/etc/hosts (or (operating-system-hosts-file os) |
| 520 | (default-/etc/hosts (operating-system-host-name os))))) |
| 521 | (etc-directory #:pam-services pam-services |
| 522 | #:skeletons skeletons |
| 523 | #:issue (operating-system-issue os) |
| 524 | #:locale (operating-system-locale os) |
| 525 | #:nss (operating-system-name-service-switch os) |
| 526 | #:timezone (operating-system-timezone os) |
| 527 | #:hosts-file /etc/hosts |
| 528 | #:sudoers (operating-system-sudoers os) |
| 529 | #:profile profile-drv))) |
| 530 | |
| 531 | (define %setuid-programs |
| 532 | ;; Default set of setuid-root programs. |
| 533 | (let ((shadow (@ (gnu packages admin) shadow))) |
| 534 | (list #~(string-append #$shadow "/bin/passwd") |
| 535 | #~(string-append #$shadow "/bin/su") |
| 536 | #~(string-append #$inetutils "/bin/ping") |
| 537 | #~(string-append #$sudo "/bin/sudo") |
| 538 | #~(string-append #$fuse "/bin/fusermount")))) |
| 539 | |
| 540 | (define %sudoers-specification |
| 541 | ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel' |
| 542 | ;; group can do anything. See |
| 543 | ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>. |
| 544 | ;; TODO: Add a declarative API. |
| 545 | "root ALL=(ALL) ALL |
| 546 | %wheel ALL=(ALL) ALL\n") |
| 547 | |
| 548 | (define (user-group->gexp group) |
| 549 | "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for |
| 550 | 'active-groups'." |
| 551 | #~(list #$(user-group-name group) |
| 552 | #$(user-group-password group) |
| 553 | #$(user-group-id group) |
| 554 | #$(user-group-system? group))) |
| 555 | |
| 556 | (define (user-account->gexp account) |
| 557 | "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for |
| 558 | 'activate-users'." |
| 559 | #~`(#$(user-account-name account) |
| 560 | #$(user-account-uid account) |
| 561 | #$(user-account-group account) |
| 562 | #$(user-account-supplementary-groups account) |
| 563 | #$(user-account-comment account) |
| 564 | #$(user-account-home-directory account) |
| 565 | ,#$(user-account-shell account) ; this one is a gexp |
| 566 | #$(user-account-password account) |
| 567 | #$(user-account-system? account))) |
| 568 | |
| 569 | (define (modprobe-wrapper) |
| 570 | "Return a wrapper for the 'modprobe' command that knows where modules live. |
| 571 | |
| 572 | This wrapper is typically invoked by the Linux kernel ('call_modprobe', in |
| 573 | kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment |
| 574 | variable is not set---hence the need for this wrapper." |
| 575 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) |
| 576 | (gexp->script "modprobe" |
| 577 | #~(begin |
| 578 | (setenv "LINUX_MODULE_DIRECTORY" |
| 579 | "/run/booted-system/kernel/lib/modules") |
| 580 | (apply execl #$modprobe |
| 581 | (cons #$modprobe (cdr (command-line)))))))) |
| 582 | |
| 583 | (define (operating-system-activation-script os) |
| 584 | "Return the activation script for OS---i.e., the code that \"activates\" the |
| 585 | stateful part of OS, including user accounts and groups, special directories, |
| 586 | etc." |
| 587 | (define %modules |
| 588 | '((gnu build activation) |
| 589 | (gnu build linux-boot) |
| 590 | (gnu build linux-modules) |
| 591 | (gnu build file-systems) |
| 592 | (guix build utils) |
| 593 | (guix elf))) |
| 594 | |
| 595 | (define (service-activations services) |
| 596 | ;; Return the activation scripts for SERVICES. |
| 597 | (let ((gexps (filter-map service-activate services))) |
| 598 | (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>) |
| 599 | gexps)))) |
| 600 | |
| 601 | (mlet* %store-monad ((services (operating-system-services os)) |
| 602 | (actions (service-activations services)) |
| 603 | (etc (operating-system-etc-directory os)) |
| 604 | (modules (imported-modules %modules)) |
| 605 | (compiled (compiled-modules %modules)) |
| 606 | (modprobe (modprobe-wrapper)) |
| 607 | (firmware (directory-union |
| 608 | "firmware" (operating-system-firmware os))) |
| 609 | (accounts (operating-system-accounts os))) |
| 610 | (define setuid-progs |
| 611 | (operating-system-setuid-programs os)) |
| 612 | |
| 613 | (define user-specs |
| 614 | (map user-account->gexp accounts)) |
| 615 | |
| 616 | (define groups |
| 617 | (append (operating-system-groups os) |
| 618 | (append-map service-user-groups services))) |
| 619 | |
| 620 | (define group-specs |
| 621 | (map user-group->gexp groups)) |
| 622 | |
| 623 | (gexp->file "activate" |
| 624 | #~(begin |
| 625 | (eval-when (expand load eval) |
| 626 | ;; Make sure 'use-modules' below succeeds. |
| 627 | (set! %load-path (cons #$modules %load-path)) |
| 628 | (set! %load-compiled-path |
| 629 | (cons #$compiled %load-compiled-path))) |
| 630 | |
| 631 | (use-modules (gnu build activation)) |
| 632 | |
| 633 | ;; Make sure /bin/sh is valid and current. |
| 634 | (activate-/bin/sh |
| 635 | (string-append #$(canonical-package bash) |
| 636 | "/bin/sh")) |
| 637 | |
| 638 | ;; Populate /etc. |
| 639 | (activate-etc #$etc) |
| 640 | |
| 641 | ;; Add users and user groups. |
| 642 | (setenv "PATH" |
| 643 | (string-append #$(@ (gnu packages admin) shadow) |
| 644 | "/sbin")) |
| 645 | (activate-users+groups (list #$@user-specs) |
| 646 | (list #$@group-specs)) |
| 647 | |
| 648 | ;; Activate setuid programs. |
| 649 | (activate-setuid-programs (list #$@setuid-progs)) |
| 650 | |
| 651 | ;; Tell the kernel to use our 'modprobe' command. |
| 652 | (activate-modprobe #$modprobe) |
| 653 | |
| 654 | ;; Tell the kernel where firmware is. |
| 655 | (activate-firmware |
| 656 | (string-append #$firmware "/lib/firmware")) |
| 657 | |
| 658 | ;; Run the services' activation snippets. |
| 659 | ;; TODO: Use 'load-compiled'. |
| 660 | (for-each primitive-load '#$actions) |
| 661 | |
| 662 | ;; Set up /run/current-system. |
| 663 | (activate-current-system))))) |
| 664 | |
| 665 | (define (operating-system-boot-script os) |
| 666 | "Return the boot script for OS---i.e., the code started by the initrd once |
| 667 | we're running in the final root." |
| 668 | (mlet* %store-monad ((services (operating-system-services os)) |
| 669 | (activate (operating-system-activation-script os)) |
| 670 | (dmd-conf (dmd-configuration-file services))) |
| 671 | (gexp->file "boot" |
| 672 | #~(begin |
| 673 | ;; Activate the system. |
| 674 | ;; TODO: Use 'load-compiled'. |
| 675 | (primitive-load #$activate) |
| 676 | |
| 677 | ;; Keep track of the booted system. |
| 678 | (false-if-exception (delete-file "/run/booted-system")) |
| 679 | (symlink (readlink "/run/current-system") |
| 680 | "/run/booted-system") |
| 681 | |
| 682 | ;; Close any remaining open file descriptors to be on the |
| 683 | ;; safe side. This must be the very last thing we do, |
| 684 | ;; because Guile has internal FDs such as 'sleep_pipe' |
| 685 | ;; that need to be alive. |
| 686 | (let loop ((fd 3)) |
| 687 | (when (< fd 1024) |
| 688 | (false-if-exception (close-fdes fd)) |
| 689 | (loop (+ 1 fd)))) |
| 690 | |
| 691 | ;; Start dmd. |
| 692 | (execl (string-append #$dmd "/bin/dmd") |
| 693 | "dmd" "--config" #$dmd-conf))))) |
| 694 | |
| 695 | (define (operating-system-root-file-system os) |
| 696 | "Return the root file system of OS." |
| 697 | (find (match-lambda |
| 698 | (($ <file-system> _ _ "/") #t) |
| 699 | (_ #f)) |
| 700 | (operating-system-file-systems os))) |
| 701 | |
| 702 | (define (operating-system-initrd-file os) |
| 703 | "Return a gexp denoting the initrd file of OS." |
| 704 | (define boot-file-systems |
| 705 | (filter file-system-needed-for-boot? |
| 706 | (operating-system-file-systems os))) |
| 707 | |
| 708 | (define mapped-devices |
| 709 | (operating-system-boot-mapped-devices os)) |
| 710 | |
| 711 | (define make-initrd |
| 712 | (operating-system-initrd os)) |
| 713 | |
| 714 | (mlet %store-monad ((initrd (make-initrd boot-file-systems |
| 715 | #:mapped-devices mapped-devices))) |
| 716 | (return #~(string-append #$initrd "/initrd")))) |
| 717 | |
| 718 | (define (operating-system-locale-directory os) |
| 719 | "Return the directory containing the locales compiled for the definitions |
| 720 | listed in OS. The C library expects to find it under |
| 721 | /run/current-system/locale." |
| 722 | ;; While we're at it, check whether the locale of OS is defined. |
| 723 | (unless (member (operating-system-locale os) |
| 724 | (map locale-definition-name |
| 725 | (operating-system-locale-definitions os))) |
| 726 | (raise (condition |
| 727 | (&message (message "system locale lacks a definition"))))) |
| 728 | |
| 729 | (locale-directory (operating-system-locale-definitions os))) |
| 730 | |
| 731 | (define (kernel->grub-label kernel) |
| 732 | "Return a label for the GRUB menu entry that boots KERNEL." |
| 733 | (string-append "GNU with " |
| 734 | (string-titlecase (package-name kernel)) " " |
| 735 | (package-version kernel) |
| 736 | " (alpha)")) |
| 737 | |
| 738 | (define* (operating-system-grub.cfg os #:optional (old-entries '())) |
| 739 | "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the |
| 740 | \"old entries\" menu." |
| 741 | (mlet* %store-monad |
| 742 | ((system (operating-system-derivation os)) |
| 743 | (root-fs -> (operating-system-root-file-system os)) |
| 744 | (kernel -> (operating-system-kernel os)) |
| 745 | (entries -> (list (menu-entry |
| 746 | (label (kernel->grub-label kernel)) |
| 747 | (linux kernel) |
| 748 | (linux-arguments |
| 749 | (list (string-append "--root=" |
| 750 | (file-system-device root-fs)) |
| 751 | #~(string-append "--system=" #$system) |
| 752 | #~(string-append "--load=" #$system |
| 753 | "/boot"))) |
| 754 | (initrd #~(string-append #$system "/initrd")))))) |
| 755 | (grub-configuration-file (operating-system-bootloader os) entries |
| 756 | #:old-entries old-entries))) |
| 757 | |
| 758 | (define (operating-system-parameters-file os) |
| 759 | "Return a file that describes the boot parameters of OS. The primary use of |
| 760 | this file is the reconstruction of GRUB menu entries for old configurations." |
| 761 | (mlet %store-monad ((initrd (operating-system-initrd-file os)) |
| 762 | (root -> (operating-system-root-file-system os)) |
| 763 | (label -> (kernel->grub-label |
| 764 | (operating-system-kernel os)))) |
| 765 | (gexp->file "parameters" |
| 766 | #~(boot-parameters (version 0) |
| 767 | (label #$label) |
| 768 | (root-device #$(file-system-device root)) |
| 769 | (kernel #$(operating-system-kernel os)) |
| 770 | (initrd #$initrd))))) |
| 771 | |
| 772 | (define (operating-system-derivation os) |
| 773 | "Return a derivation that builds OS." |
| 774 | (mlet* %store-monad |
| 775 | ((profile (operating-system-profile os)) |
| 776 | (etc (operating-system-etc-directory os)) |
| 777 | (boot (operating-system-boot-script os)) |
| 778 | (kernel -> (operating-system-kernel os)) |
| 779 | (initrd (operating-system-initrd-file os)) |
| 780 | (locale (operating-system-locale-directory os)) |
| 781 | (params (operating-system-parameters-file os))) |
| 782 | (file-union "system" |
| 783 | `(("boot" ,#~#$boot) |
| 784 | ("kernel" ,#~#$kernel) |
| 785 | ("parameters" ,#~#$params) |
| 786 | ("initrd" ,initrd) |
| 787 | ("profile" ,#~#$profile) |
| 788 | ("locale" ,#~#$locale) ;used by libc |
| 789 | ("etc" ,#~#$etc))))) |
| 790 | |
| 791 | ;;; system.scm ends here |