| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2013, 2014 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 which) |
| 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 which) |
| 36 | #:use-module (gnu packages less) |
| 37 | #:use-module (gnu packages zile) |
| 38 | #:use-module (gnu packages nano) |
| 39 | #:use-module (gnu packages lsof) |
| 40 | #:use-module (gnu packages gawk) |
| 41 | #:use-module (gnu packages man) |
| 42 | #:use-module (gnu packages compression) |
| 43 | #:use-module (gnu packages firmware) |
| 44 | #:autoload (gnu packages cryptsetup) (cryptsetup) |
| 45 | #:use-module (gnu services) |
| 46 | #:use-module (gnu services dmd) |
| 47 | #:use-module (gnu services base) |
| 48 | #:use-module (gnu system grub) |
| 49 | #:use-module (gnu system shadow) |
| 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-packages |
| 74 | operating-system-timezone |
| 75 | operating-system-locale |
| 76 | operating-system-locale-definitions |
| 77 | operating-system-mapped-devices |
| 78 | operating-system-file-systems |
| 79 | operating-system-activation-script |
| 80 | |
| 81 | operating-system-derivation |
| 82 | operating-system-profile |
| 83 | operating-system-grub.cfg |
| 84 | |
| 85 | local-host-aliases |
| 86 | %setuid-programs |
| 87 | %base-packages |
| 88 | %base-firmware |
| 89 | |
| 90 | luks-device-mapping)) |
| 91 | |
| 92 | ;;; Commentary: |
| 93 | ;;; |
| 94 | ;;; This module supports whole-system configuration. |
| 95 | ;;; |
| 96 | ;;; Code: |
| 97 | |
| 98 | ;; System-wide configuration. |
| 99 | ;; TODO: Add per-field docstrings/stexi. |
| 100 | (define-record-type* <operating-system> operating-system |
| 101 | make-operating-system |
| 102 | operating-system? |
| 103 | (kernel operating-system-kernel ; package |
| 104 | (default linux-libre)) |
| 105 | (bootloader operating-system-bootloader) ; <grub-configuration> |
| 106 | |
| 107 | (initrd operating-system-initrd ; (list fs) -> M derivation |
| 108 | (default base-initrd)) |
| 109 | (firmware operating-system-firmware ; list of packages |
| 110 | (default %base-firmware)) |
| 111 | |
| 112 | (host-name operating-system-host-name) ; string |
| 113 | (hosts-file operating-system-hosts-file ; M item | #f |
| 114 | (default #f)) |
| 115 | |
| 116 | (mapped-devices operating-system-mapped-devices ; list of <mapped-device> |
| 117 | (default '())) |
| 118 | (file-systems operating-system-file-systems) ; list of fs |
| 119 | (swap-devices operating-system-swap-devices ; list of strings |
| 120 | (default '())) |
| 121 | |
| 122 | (users operating-system-users ; list of user accounts |
| 123 | (default '())) |
| 124 | (groups operating-system-groups ; list of user groups |
| 125 | (default %base-groups)) |
| 126 | |
| 127 | (skeletons operating-system-skeletons ; list of name/monadic value |
| 128 | (default (default-skeletons))) |
| 129 | (issue operating-system-issue ; string |
| 130 | (default %default-issue)) |
| 131 | |
| 132 | (packages operating-system-packages ; list of (PACKAGE OUTPUT...) |
| 133 | (default %base-packages)) ; or just PACKAGE |
| 134 | |
| 135 | (timezone operating-system-timezone) ; string |
| 136 | (locale operating-system-locale ; string |
| 137 | (default "en_US.utf8")) |
| 138 | (locale-definitions operating-system-locale-definitions ; list of <locale-definition> |
| 139 | (default %default-locale-definitions)) |
| 140 | |
| 141 | (services operating-system-user-services ; list of monadic services |
| 142 | (default %base-services)) |
| 143 | |
| 144 | (pam-services operating-system-pam-services ; list of PAM services |
| 145 | (default (base-pam-services))) |
| 146 | (setuid-programs operating-system-setuid-programs |
| 147 | (default %setuid-programs)) ; list of string-valued gexps |
| 148 | |
| 149 | (sudoers operating-system-sudoers ; /etc/sudoers contents |
| 150 | (default %sudoers-specification))) |
| 151 | |
| 152 | \f |
| 153 | ;;; |
| 154 | ;;; Derivation. |
| 155 | ;;; |
| 156 | |
| 157 | (define* (file-union name files) |
| 158 | "Return a derivation that builds a directory containing all of FILES. Each |
| 159 | item in FILES must be a list where the first element is the file name to use |
| 160 | in the new directory, and the second element is a gexp denoting the target |
| 161 | file." |
| 162 | (define builder |
| 163 | #~(begin |
| 164 | (mkdir #$output) |
| 165 | (chdir #$output) |
| 166 | #$@(map (match-lambda |
| 167 | ((target source) |
| 168 | #~(symlink #$source #$target))) |
| 169 | files))) |
| 170 | |
| 171 | (gexp->derivation name builder)) |
| 172 | |
| 173 | (define (directory-union name things) |
| 174 | "Return a directory that is the union of THINGS." |
| 175 | (match things |
| 176 | ((one) |
| 177 | ;; Only one thing; return it. |
| 178 | (with-monad %store-monad (return one))) |
| 179 | (_ |
| 180 | (gexp->derivation name |
| 181 | #~(begin |
| 182 | (use-modules (guix build union)) |
| 183 | (union-build #$output '#$things)) |
| 184 | #:modules '((guix build union)) |
| 185 | #:local-build? #t)))) |
| 186 | |
| 187 | \f |
| 188 | ;;; |
| 189 | ;;; Services. |
| 190 | ;;; |
| 191 | |
| 192 | (define (open-luks-device source target) |
| 193 | "Return a gexp that maps SOURCE to TARGET as a LUKS device, using |
| 194 | 'cryptsetup'." |
| 195 | #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup") |
| 196 | "open" "--type" "luks" |
| 197 | #$source #$target))) |
| 198 | |
| 199 | (define (close-luks-device source target) |
| 200 | "Return a gexp that closes TARGET, a LUKS device." |
| 201 | #~(zero? (system* (string-append #$cryptsetup "/sbin/cryptsetup") |
| 202 | "close" #$target))) |
| 203 | |
| 204 | (define luks-device-mapping |
| 205 | ;; The type of LUKS mapped devices. |
| 206 | (mapped-device-kind |
| 207 | (open open-luks-device) |
| 208 | (close close-luks-device))) |
| 209 | |
| 210 | (define (other-file-system-services os) |
| 211 | "Return file system services for the file systems of OS that are not marked |
| 212 | as 'needed-for-boot'." |
| 213 | (define file-systems |
| 214 | (remove file-system-needed-for-boot? |
| 215 | (operating-system-file-systems os))) |
| 216 | |
| 217 | (define (device-mappings fs) |
| 218 | (filter (lambda (md) |
| 219 | (string=? (string-append "/dev/mapper/" |
| 220 | (mapped-device-target md)) |
| 221 | (file-system-device fs))) |
| 222 | (operating-system-mapped-devices os))) |
| 223 | |
| 224 | (define (requirements fs) |
| 225 | (map (lambda (md) |
| 226 | (symbol-append 'device-mapping- |
| 227 | (string->symbol (mapped-device-target md)))) |
| 228 | (device-mappings fs))) |
| 229 | |
| 230 | (sequence %store-monad |
| 231 | (map (lambda (fs) |
| 232 | (match fs |
| 233 | (($ <file-system> device title target type flags opts |
| 234 | #f check? create?) |
| 235 | (file-system-service device target type |
| 236 | #:title title |
| 237 | #:requirements (requirements fs) |
| 238 | #:check? check? |
| 239 | #:create-mount-point? create? |
| 240 | #:options opts |
| 241 | #:flags flags)))) |
| 242 | file-systems))) |
| 243 | |
| 244 | (define (mapped-device-user device file-systems) |
| 245 | "Return a file system among FILE-SYSTEMS that uses DEVICE, or #f." |
| 246 | (let ((target (string-append "/dev/mapper/" (mapped-device-target device)))) |
| 247 | (find (lambda (fs) |
| 248 | (string=? (file-system-device fs) target)) |
| 249 | file-systems))) |
| 250 | |
| 251 | (define (operating-system-user-mapped-devices os) |
| 252 | "Return the subset of mapped devices that can be installed in |
| 253 | user-land--i.e., those not needed during boot." |
| 254 | (let ((devices (operating-system-mapped-devices os)) |
| 255 | (file-systems (operating-system-file-systems os))) |
| 256 | (filter (lambda (md) |
| 257 | (let ((user (mapped-device-user md file-systems))) |
| 258 | (or (not user) |
| 259 | (not (file-system-needed-for-boot? user))))) |
| 260 | devices))) |
| 261 | |
| 262 | (define (operating-system-boot-mapped-devices os) |
| 263 | "Return the subset of mapped devices that must be installed during boot, |
| 264 | from the initrd." |
| 265 | (let ((devices (operating-system-mapped-devices os)) |
| 266 | (file-systems (operating-system-file-systems os))) |
| 267 | (filter (lambda (md) |
| 268 | (let ((user (mapped-device-user md file-systems))) |
| 269 | (and user (file-system-needed-for-boot? user)))) |
| 270 | devices))) |
| 271 | |
| 272 | (define (device-mapping-services os) |
| 273 | "Return the list of device-mapping services for OS as a monadic list." |
| 274 | (sequence %store-monad |
| 275 | (map (lambda (md) |
| 276 | (let* ((source (mapped-device-source md)) |
| 277 | (target (mapped-device-target md)) |
| 278 | (type (mapped-device-type md)) |
| 279 | (open (mapped-device-kind-open type)) |
| 280 | (close (mapped-device-kind-close type))) |
| 281 | (device-mapping-service target |
| 282 | (open source target) |
| 283 | (close source target)))) |
| 284 | (operating-system-user-mapped-devices os)))) |
| 285 | |
| 286 | (define (swap-services os) |
| 287 | "Return the list of swap services for OS as a monadic list." |
| 288 | (sequence %store-monad |
| 289 | (map swap-service (operating-system-swap-devices os)))) |
| 290 | |
| 291 | (define (essential-services os) |
| 292 | "Return the list of essential services for OS. These are special services |
| 293 | that implement part of what's declared in OS are responsible for low-level |
| 294 | bookkeeping." |
| 295 | (define known-fs |
| 296 | (map file-system-mount-point (operating-system-file-systems os))) |
| 297 | |
| 298 | (mlet* %store-monad ((mappings (device-mapping-services os)) |
| 299 | (root-fs (root-file-system-service)) |
| 300 | (other-fs (other-file-system-services os)) |
| 301 | (unmount (user-unmount-service known-fs)) |
| 302 | (swaps (swap-services os)) |
| 303 | (procs (user-processes-service |
| 304 | (map (compose first service-provision) |
| 305 | other-fs))) |
| 306 | (host-name (host-name-service |
| 307 | (operating-system-host-name os)))) |
| 308 | (return (cons* host-name procs root-fs unmount |
| 309 | (append other-fs mappings swaps))))) |
| 310 | |
| 311 | (define (operating-system-services os) |
| 312 | "Return all the services of OS, including \"internal\" services that do not |
| 313 | explicitly appear in OS." |
| 314 | (mlet %store-monad |
| 315 | ((user (sequence %store-monad (operating-system-user-services os))) |
| 316 | (essential (essential-services os))) |
| 317 | (return (append essential user)))) |
| 318 | |
| 319 | \f |
| 320 | ;;; |
| 321 | ;;; /etc. |
| 322 | ;;; |
| 323 | |
| 324 | (define %base-firmware |
| 325 | ;; Firmware usable by default. |
| 326 | (list ath9k-htc-firmware)) |
| 327 | |
| 328 | (define %base-packages |
| 329 | ;; Default set of packages globally visible. It should include anything |
| 330 | ;; required for basic administrator tasks. |
| 331 | (cons* procps psmisc which less zile nano |
| 332 | (@ (gnu packages admin) dmd) guix |
| 333 | lsof ;for Guix's 'list-runtime-roots' |
| 334 | pciutils usbutils |
| 335 | util-linux inetutils isc-dhcp wireless-tools |
| 336 | net-tools ; XXX: remove when Inetutils suffices |
| 337 | man-db |
| 338 | |
| 339 | ;; The 'sudo' command is already in %SETUID-PROGRAMS, but we also |
| 340 | ;; want the other commands and the man pages (notably because |
| 341 | ;; auto-completion in Emacs shell relies on man pages.) |
| 342 | sudo |
| 343 | |
| 344 | ;; Get 'insmod' & co. from kmod, not module-init-tools, since udev |
| 345 | ;; already depends on it anyway. |
| 346 | kmod eudev |
| 347 | |
| 348 | e2fsprogs kbd |
| 349 | |
| 350 | ;; The packages below are also in %FINAL-INPUTS, so take them from |
| 351 | ;; there to avoid duplication. |
| 352 | (map canonical-package |
| 353 | (list guile-2.0 bash coreutils findutils grep sed |
| 354 | diffutils patch gawk tar gzip bzip2 xz lzip)))) |
| 355 | |
| 356 | (define %default-issue |
| 357 | ;; Default contents for /etc/issue. |
| 358 | " |
| 359 | This is the GNU system. Welcome.\n") |
| 360 | |
| 361 | (define (local-host-aliases host-name) |
| 362 | "Return aliases for HOST-NAME, to be used in /etc/hosts." |
| 363 | (string-append "127.0.0.1 localhost " host-name "\n" |
| 364 | "::1 localhost " host-name "\n")) |
| 365 | |
| 366 | (define (default-/etc/hosts host-name) |
| 367 | "Return the default /etc/hosts file." |
| 368 | (text-file "hosts" (local-host-aliases host-name))) |
| 369 | |
| 370 | (define* (etc-directory #:key |
| 371 | (locale "C") (timezone "Europe/Paris") |
| 372 | (issue "Hello!\n") |
| 373 | (skeletons '()) |
| 374 | (pam-services '()) |
| 375 | (profile "/run/current-system/profile") |
| 376 | hosts-file |
| 377 | (sudoers "")) |
| 378 | "Return a derivation that builds the static part of the /etc directory." |
| 379 | (mlet* %store-monad |
| 380 | ((pam.d (pam-services->directory pam-services)) |
| 381 | (sudoers (text-file "sudoers" sudoers)) |
| 382 | (login.defs (text-file "login.defs" "# Empty for now.\n")) |
| 383 | (shells (text-file "shells" ; used by xterm and others |
| 384 | "\ |
| 385 | /bin/sh |
| 386 | /run/current-system/profile/bin/sh |
| 387 | /run/current-system/profile/bin/bash\n")) |
| 388 | (issue (text-file "issue" issue)) |
| 389 | |
| 390 | ;; For now, generate a basic config so that /etc/hosts is honored. |
| 391 | (nsswitch (text-file "nsswitch.conf" |
| 392 | "hosts: files dns\n")) |
| 393 | |
| 394 | ;; Startup file for POSIX-compliant login shells, which set system-wide |
| 395 | ;; environment variables. |
| 396 | (profile (text-file* "profile" "\ |
| 397 | export LANG=\"" locale "\" |
| 398 | export TZ=\"" timezone "\" |
| 399 | export TZDIR=\"" tzdata "/share/zoneinfo\" |
| 400 | |
| 401 | # Tell 'modprobe' & co. where to look for modules. |
| 402 | export LINUX_MODULE_DIRECTORY=/run/booted-system/kernel/lib/modules |
| 403 | |
| 404 | export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin |
| 405 | export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH |
| 406 | export MANPATH=$HOME/.guix-profile/share/man:/run/current-system/profile/share/man |
| 407 | export INFOPATH=$HOME/.guix-profile/share/info:/run/current-system/profile/share/info |
| 408 | ")) |
| 409 | (skel (skeleton-directory skeletons))) |
| 410 | (file-union "etc" |
| 411 | `(("services" ,#~(string-append #$net-base "/etc/services")) |
| 412 | ("protocols" ,#~(string-append #$net-base "/etc/protocols")) |
| 413 | ("rpc" ,#~(string-append #$net-base "/etc/rpc")) |
| 414 | ("pam.d" ,#~#$pam.d) |
| 415 | ("login.defs" ,#~#$login.defs) |
| 416 | ("issue" ,#~#$issue) |
| 417 | ("nsswitch.conf" ,#~#$nsswitch) |
| 418 | ("skel" ,#~#$skel) |
| 419 | ("shells" ,#~#$shells) |
| 420 | ("profile" ,#~#$profile) |
| 421 | ("hosts" ,#~#$hosts-file) |
| 422 | ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/" |
| 423 | #$timezone)) |
| 424 | ("sudoers" ,#~#$sudoers))))) |
| 425 | |
| 426 | (define (operating-system-profile os) |
| 427 | "Return a derivation that builds the system profile of OS." |
| 428 | (profile-derivation (manifest (map package->manifest-entry |
| 429 | (operating-system-packages os))))) |
| 430 | |
| 431 | (define %root-account |
| 432 | ;; Default root account. |
| 433 | (user-account |
| 434 | (name "root") |
| 435 | (password "") |
| 436 | (uid 0) (group "root") |
| 437 | (comment "System administrator") |
| 438 | (home-directory "/root"))) |
| 439 | |
| 440 | (define (operating-system-accounts os) |
| 441 | "Return the user accounts for OS, including an obligatory 'root' account." |
| 442 | (define users |
| 443 | ;; Make sure there's a root account. |
| 444 | (if (find (lambda (user) |
| 445 | (and=> (user-account-uid user) zero?)) |
| 446 | (operating-system-users os)) |
| 447 | (operating-system-users os) |
| 448 | (cons %root-account (operating-system-users os)))) |
| 449 | |
| 450 | (mlet %store-monad ((services (operating-system-services os))) |
| 451 | (return (append users |
| 452 | (append-map service-user-accounts services))))) |
| 453 | |
| 454 | (define (operating-system-etc-directory os) |
| 455 | "Return that static part of the /etc directory of OS." |
| 456 | (mlet* %store-monad |
| 457 | ((services (operating-system-services os)) |
| 458 | (pam-services -> |
| 459 | ;; Services known to PAM. |
| 460 | (delete-duplicates |
| 461 | (append (operating-system-pam-services os) |
| 462 | (append-map service-pam-services services)))) |
| 463 | (profile-drv (operating-system-profile os)) |
| 464 | (skeletons (operating-system-skeletons os)) |
| 465 | (/etc/hosts (or (operating-system-hosts-file os) |
| 466 | (default-/etc/hosts (operating-system-host-name os))))) |
| 467 | (etc-directory #:pam-services pam-services |
| 468 | #:skeletons skeletons |
| 469 | #:issue (operating-system-issue os) |
| 470 | #:locale (operating-system-locale os) |
| 471 | #:timezone (operating-system-timezone os) |
| 472 | #:hosts-file /etc/hosts |
| 473 | #:sudoers (operating-system-sudoers os) |
| 474 | #:profile profile-drv))) |
| 475 | |
| 476 | (define %setuid-programs |
| 477 | ;; Default set of setuid-root programs. |
| 478 | (let ((shadow (@ (gnu packages admin) shadow))) |
| 479 | (list #~(string-append #$shadow "/bin/passwd") |
| 480 | #~(string-append #$shadow "/bin/su") |
| 481 | #~(string-append #$inetutils "/bin/ping") |
| 482 | #~(string-append #$sudo "/bin/sudo") |
| 483 | #~(string-append #$fuse "/bin/fusermount")))) |
| 484 | |
| 485 | (define %sudoers-specification |
| 486 | ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel' |
| 487 | ;; group can do anything. See |
| 488 | ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>. |
| 489 | ;; TODO: Add a declarative API. |
| 490 | "root ALL=(ALL) ALL |
| 491 | %wheel ALL=(ALL) ALL\n") |
| 492 | |
| 493 | (define (user-group->gexp group) |
| 494 | "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for |
| 495 | 'active-groups'." |
| 496 | #~(list #$(user-group-name group) |
| 497 | #$(user-group-password group) |
| 498 | #$(user-group-id group) |
| 499 | #$(user-group-system? group))) |
| 500 | |
| 501 | (define (user-account->gexp account) |
| 502 | "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for |
| 503 | 'activate-users'." |
| 504 | #~`(#$(user-account-name account) |
| 505 | #$(user-account-uid account) |
| 506 | #$(user-account-group account) |
| 507 | #$(user-account-supplementary-groups account) |
| 508 | #$(user-account-comment account) |
| 509 | #$(user-account-home-directory account) |
| 510 | ,#$(user-account-shell account) ; this one is a gexp |
| 511 | #$(user-account-password account) |
| 512 | #$(user-account-system? account))) |
| 513 | |
| 514 | (define (modprobe-wrapper) |
| 515 | "Return a wrapper for the 'modprobe' command that knows where modules live. |
| 516 | |
| 517 | This wrapper is typically invoked by the Linux kernel ('call_modprobe', in |
| 518 | kernel/kmod.c), a situation where the 'LINUX_MODULE_DIRECTORY' environment |
| 519 | variable is not set---hence the need for this wrapper." |
| 520 | (let ((modprobe "/run/current-system/profile/bin/modprobe")) |
| 521 | (gexp->script "modprobe" |
| 522 | #~(begin |
| 523 | (setenv "LINUX_MODULE_DIRECTORY" |
| 524 | "/run/booted-system/kernel/lib/modules") |
| 525 | (apply execl #$modprobe |
| 526 | (cons #$modprobe (cdr (command-line)))))))) |
| 527 | |
| 528 | (define (operating-system-activation-script os) |
| 529 | "Return the activation script for OS---i.e., the code that \"activates\" the |
| 530 | stateful part of OS, including user accounts and groups, special directories, |
| 531 | etc." |
| 532 | (define %modules |
| 533 | '((gnu build activation) |
| 534 | (gnu build linux-boot) |
| 535 | (gnu build linux-modules) |
| 536 | (gnu build file-systems) |
| 537 | (guix build utils) |
| 538 | (guix elf))) |
| 539 | |
| 540 | (define (service-activations services) |
| 541 | ;; Return the activation scripts for SERVICES. |
| 542 | (let ((gexps (filter-map service-activate services))) |
| 543 | (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>) |
| 544 | gexps)))) |
| 545 | |
| 546 | (mlet* %store-monad ((services (operating-system-services os)) |
| 547 | (actions (service-activations services)) |
| 548 | (etc (operating-system-etc-directory os)) |
| 549 | (modules (imported-modules %modules)) |
| 550 | (compiled (compiled-modules %modules)) |
| 551 | (modprobe (modprobe-wrapper)) |
| 552 | (firmware (directory-union |
| 553 | "firmware" (operating-system-firmware os))) |
| 554 | (accounts (operating-system-accounts os))) |
| 555 | (define setuid-progs |
| 556 | (operating-system-setuid-programs os)) |
| 557 | |
| 558 | (define user-specs |
| 559 | (map user-account->gexp accounts)) |
| 560 | |
| 561 | (define groups |
| 562 | (append (operating-system-groups os) |
| 563 | (append-map service-user-groups services))) |
| 564 | |
| 565 | (define group-specs |
| 566 | (map user-group->gexp groups)) |
| 567 | |
| 568 | (gexp->file "activate" |
| 569 | #~(begin |
| 570 | (eval-when (expand load eval) |
| 571 | ;; Make sure 'use-modules' below succeeds. |
| 572 | (set! %load-path (cons #$modules %load-path)) |
| 573 | (set! %load-compiled-path |
| 574 | (cons #$compiled %load-compiled-path))) |
| 575 | |
| 576 | (use-modules (gnu build activation)) |
| 577 | |
| 578 | ;; Make sure /bin/sh is valid and current. |
| 579 | (activate-/bin/sh |
| 580 | (string-append #$(canonical-package bash) |
| 581 | "/bin/sh")) |
| 582 | |
| 583 | ;; Populate /etc. |
| 584 | (activate-etc #$etc) |
| 585 | |
| 586 | ;; Add users and user groups. |
| 587 | (setenv "PATH" |
| 588 | (string-append #$(@ (gnu packages admin) shadow) |
| 589 | "/sbin")) |
| 590 | (activate-users+groups (list #$@user-specs) |
| 591 | (list #$@group-specs)) |
| 592 | |
| 593 | ;; Activate setuid programs. |
| 594 | (activate-setuid-programs (list #$@setuid-progs)) |
| 595 | |
| 596 | ;; Tell the kernel to use our 'modprobe' command. |
| 597 | (activate-modprobe #$modprobe) |
| 598 | |
| 599 | ;; Tell the kernel where firmware is. |
| 600 | (activate-firmware |
| 601 | (string-append #$firmware "/lib/firmware")) |
| 602 | |
| 603 | ;; Run the services' activation snippets. |
| 604 | ;; TODO: Use 'load-compiled'. |
| 605 | (for-each primitive-load '#$actions) |
| 606 | |
| 607 | ;; Set up /run/current-system. |
| 608 | (activate-current-system))))) |
| 609 | |
| 610 | (define (operating-system-boot-script os) |
| 611 | "Return the boot script for OS---i.e., the code started by the initrd once |
| 612 | we're running in the final root." |
| 613 | (mlet* %store-monad ((services (operating-system-services os)) |
| 614 | (activate (operating-system-activation-script os)) |
| 615 | (dmd-conf (dmd-configuration-file services))) |
| 616 | (gexp->file "boot" |
| 617 | #~(begin |
| 618 | ;; Activate the system. |
| 619 | ;; TODO: Use 'load-compiled'. |
| 620 | (primitive-load #$activate) |
| 621 | |
| 622 | ;; Keep track of the booted system. |
| 623 | (false-if-exception (delete-file "/run/booted-system")) |
| 624 | (symlink (readlink "/run/current-system") |
| 625 | "/run/booted-system") |
| 626 | |
| 627 | ;; Close any remaining open file descriptors to be on the |
| 628 | ;; safe side. This must be the very last thing we do, |
| 629 | ;; because Guile has internal FDs such as 'sleep_pipe' |
| 630 | ;; that need to be alive. |
| 631 | (let loop ((fd 3)) |
| 632 | (when (< fd 1024) |
| 633 | (false-if-exception (close-fdes fd)) |
| 634 | (loop (+ 1 fd)))) |
| 635 | |
| 636 | ;; Start dmd. |
| 637 | (execl (string-append #$dmd "/bin/dmd") |
| 638 | "dmd" "--config" #$dmd-conf))))) |
| 639 | |
| 640 | (define (operating-system-root-file-system os) |
| 641 | "Return the root file system of OS." |
| 642 | (find (match-lambda |
| 643 | (($ <file-system> _ _ "/") #t) |
| 644 | (_ #f)) |
| 645 | (operating-system-file-systems os))) |
| 646 | |
| 647 | (define (operating-system-initrd-file os) |
| 648 | "Return a gexp denoting the initrd file of OS." |
| 649 | (define boot-file-systems |
| 650 | (filter file-system-needed-for-boot? |
| 651 | (operating-system-file-systems os))) |
| 652 | |
| 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 |
| 660 | #:mapped-devices mapped-devices))) |
| 661 | (return #~(string-append #$initrd "/initrd")))) |
| 662 | |
| 663 | (define (operating-system-locale-directory os) |
| 664 | "Return the directory containing the locales compiled for the definitions |
| 665 | listed in OS. The C library expects to find it under |
| 666 | /run/current-system/locale." |
| 667 | ;; While we're at it, check whether the locale of OS is defined. |
| 668 | (unless (member (operating-system-locale os) |
| 669 | (map locale-definition-name |
| 670 | (operating-system-locale-definitions os))) |
| 671 | (raise (condition |
| 672 | (&message (message "system locale lacks a definition"))))) |
| 673 | |
| 674 | (locale-directory (operating-system-locale-definitions os))) |
| 675 | |
| 676 | (define (kernel->grub-label kernel) |
| 677 | "Return a label for the GRUB menu entry that boots KERNEL." |
| 678 | (string-append "GNU with " |
| 679 | (string-titlecase (package-name kernel)) " " |
| 680 | (package-version kernel) |
| 681 | " (alpha)")) |
| 682 | |
| 683 | (define* (operating-system-grub.cfg os #:optional (old-entries '())) |
| 684 | "Return the GRUB configuration file for OS. Use OLD-ENTRIES to populate the |
| 685 | \"old entries\" menu." |
| 686 | (mlet* %store-monad |
| 687 | ((system (operating-system-derivation os)) |
| 688 | (root-fs -> (operating-system-root-file-system os)) |
| 689 | (kernel -> (operating-system-kernel os)) |
| 690 | (entries -> (list (menu-entry |
| 691 | (label (kernel->grub-label kernel)) |
| 692 | (linux kernel) |
| 693 | (linux-arguments |
| 694 | (list (string-append "--root=" |
| 695 | (file-system-device root-fs)) |
| 696 | #~(string-append "--system=" #$system) |
| 697 | #~(string-append "--load=" #$system |
| 698 | "/boot"))) |
| 699 | (initrd #~(string-append #$system "/initrd")))))) |
| 700 | (grub-configuration-file (operating-system-bootloader os) entries |
| 701 | #:old-entries old-entries))) |
| 702 | |
| 703 | (define (operating-system-parameters-file os) |
| 704 | "Return a file that describes the boot parameters of OS. The primary use of |
| 705 | this file is the reconstruction of GRUB menu entries for old configurations." |
| 706 | (mlet %store-monad ((initrd (operating-system-initrd-file os)) |
| 707 | (root -> (operating-system-root-file-system os)) |
| 708 | (label -> (kernel->grub-label |
| 709 | (operating-system-kernel os)))) |
| 710 | (gexp->file "parameters" |
| 711 | #~(boot-parameters (version 0) |
| 712 | (label #$label) |
| 713 | (root-device #$(file-system-device root)) |
| 714 | (kernel #$(operating-system-kernel os)) |
| 715 | (initrd #$initrd))))) |
| 716 | |
| 717 | (define (operating-system-derivation os) |
| 718 | "Return a derivation that builds OS." |
| 719 | (mlet* %store-monad |
| 720 | ((profile (operating-system-profile os)) |
| 721 | (etc (operating-system-etc-directory os)) |
| 722 | (boot (operating-system-boot-script os)) |
| 723 | (kernel -> (operating-system-kernel os)) |
| 724 | (initrd (operating-system-initrd-file os)) |
| 725 | (locale (operating-system-locale-directory os)) |
| 726 | (params (operating-system-parameters-file os))) |
| 727 | (file-union "system" |
| 728 | `(("boot" ,#~#$boot) |
| 729 | ("kernel" ,#~#$kernel) |
| 730 | ("parameters" ,#~#$params) |
| 731 | ("initrd" ,initrd) |
| 732 | ("profile" ,#~#$profile) |
| 733 | ("locale" ,#~#$locale) ;used by libc |
| 734 | ("etc" ,#~#$etc))))) |
| 735 | |
| 736 | ;;; system.scm ends here |