| 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 (gnu packages base) |
| 27 | #:use-module (gnu packages bash) |
| 28 | #:use-module (gnu packages admin) |
| 29 | #:use-module (gnu packages linux) |
| 30 | #:use-module (gnu packages package-management) |
| 31 | #:use-module (gnu packages which) |
| 32 | #:use-module (gnu packages less) |
| 33 | #:use-module (gnu packages zile) |
| 34 | #:use-module (gnu services) |
| 35 | #:use-module (gnu services dmd) |
| 36 | #:use-module (gnu services base) |
| 37 | #:use-module (gnu system grub) |
| 38 | #:use-module (gnu system shadow) |
| 39 | #:use-module (gnu system linux) |
| 40 | #:use-module (gnu system linux-initrd) |
| 41 | #:use-module (gnu system file-systems) |
| 42 | #:use-module (ice-9 match) |
| 43 | #:use-module (srfi srfi-1) |
| 44 | #:use-module (srfi srfi-26) |
| 45 | #:export (operating-system |
| 46 | operating-system? |
| 47 | |
| 48 | operating-system-bootloader |
| 49 | operating-system-services |
| 50 | operating-system-user-services |
| 51 | operating-system-packages |
| 52 | operating-system-host-name |
| 53 | operating-system-kernel |
| 54 | operating-system-initrd |
| 55 | operating-system-users |
| 56 | operating-system-groups |
| 57 | operating-system-issue |
| 58 | operating-system-packages |
| 59 | operating-system-timezone |
| 60 | operating-system-locale |
| 61 | operating-system-file-systems |
| 62 | |
| 63 | operating-system-derivation |
| 64 | operating-system-profile |
| 65 | operating-system-grub.cfg |
| 66 | |
| 67 | %base-packages)) |
| 68 | |
| 69 | ;;; Commentary: |
| 70 | ;;; |
| 71 | ;;; This module supports whole-system configuration. |
| 72 | ;;; |
| 73 | ;;; Code: |
| 74 | |
| 75 | ;; System-wide configuration. |
| 76 | ;; TODO: Add per-field docstrings/stexi. |
| 77 | (define-record-type* <operating-system> operating-system |
| 78 | make-operating-system |
| 79 | operating-system? |
| 80 | (kernel operating-system-kernel ; package |
| 81 | (default linux-libre)) |
| 82 | (bootloader operating-system-bootloader) ; <grub-configuration> |
| 83 | |
| 84 | (initrd operating-system-initrd ; (list fs) -> M derivation |
| 85 | (default qemu-initrd)) |
| 86 | |
| 87 | (host-name operating-system-host-name) ; string |
| 88 | |
| 89 | (file-systems operating-system-file-systems ; list of fs |
| 90 | (default '())) |
| 91 | |
| 92 | (users operating-system-users ; list of user accounts |
| 93 | (default '())) |
| 94 | (groups operating-system-groups ; list of user groups |
| 95 | (default (list (user-group |
| 96 | (name "root") |
| 97 | (id 0))))) |
| 98 | |
| 99 | (skeletons operating-system-skeletons ; list of name/monadic value |
| 100 | (default (default-skeletons))) |
| 101 | (issue operating-system-issue ; string |
| 102 | (default %default-issue)) |
| 103 | |
| 104 | (packages operating-system-packages ; list of (PACKAGE OUTPUT...) |
| 105 | (default %base-packages)) ; or just PACKAGE |
| 106 | |
| 107 | (timezone operating-system-timezone) ; string |
| 108 | (locale operating-system-locale) ; string |
| 109 | |
| 110 | (services operating-system-user-services ; list of monadic services |
| 111 | (default %base-services)) |
| 112 | |
| 113 | (pam-services operating-system-pam-services ; list of PAM services |
| 114 | (default (base-pam-services))) |
| 115 | (setuid-programs operating-system-setuid-programs |
| 116 | (default %setuid-programs)) ; list of string-valued gexps |
| 117 | |
| 118 | (sudoers operating-system-sudoers ; /etc/sudoers contents |
| 119 | (default %sudoers-specification))) |
| 120 | |
| 121 | \f |
| 122 | ;;; |
| 123 | ;;; Derivation. |
| 124 | ;;; |
| 125 | |
| 126 | (define* (union inputs |
| 127 | #:key (guile (%guile-for-build)) (system (%current-system)) |
| 128 | (name "union")) |
| 129 | "Return a derivation that builds the union of INPUTS. INPUTS is a list of |
| 130 | input tuples." |
| 131 | (define builder |
| 132 | #~(begin |
| 133 | (use-modules (guix build union)) |
| 134 | |
| 135 | (define inputs '#$inputs) |
| 136 | |
| 137 | (setvbuf (current-output-port) _IOLBF) |
| 138 | (setvbuf (current-error-port) _IOLBF) |
| 139 | |
| 140 | (format #t "building union `~a' with ~a packages...~%" |
| 141 | #$output (length inputs)) |
| 142 | (union-build #$output inputs))) |
| 143 | |
| 144 | (gexp->derivation name builder |
| 145 | #:system system |
| 146 | #:modules '((guix build union)) |
| 147 | #:guile-for-build guile |
| 148 | #:local-build? #t)) |
| 149 | |
| 150 | (define* (file-union name files) |
| 151 | "Return a derivation that builds a directory containing all of FILES. Each |
| 152 | item in FILES must be a list where the first element is the file name to use |
| 153 | in the new directory, and the second element is a gexp denoting the target |
| 154 | file." |
| 155 | (define builder |
| 156 | #~(begin |
| 157 | (mkdir #$output) |
| 158 | (chdir #$output) |
| 159 | #$@(map (match-lambda |
| 160 | ((target source) |
| 161 | #~(symlink #$source #$target))) |
| 162 | files))) |
| 163 | |
| 164 | (gexp->derivation name builder)) |
| 165 | |
| 166 | \f |
| 167 | ;;; |
| 168 | ;;; Services. |
| 169 | ;;; |
| 170 | |
| 171 | (define (other-file-system-services os) |
| 172 | "Return file system services for the file systems of OS that are not marked |
| 173 | as 'needed-for-boot'." |
| 174 | (define file-systems |
| 175 | (remove (lambda (fs) |
| 176 | (or (file-system-needed-for-boot? fs) |
| 177 | (string=? "/" (file-system-mount-point fs)))) |
| 178 | (operating-system-file-systems os))) |
| 179 | |
| 180 | (sequence %store-monad |
| 181 | (map (match-lambda |
| 182 | (($ <file-system> device title target type flags opts |
| 183 | #f check?) |
| 184 | (file-system-service device target type |
| 185 | #:title title |
| 186 | #:check? check? |
| 187 | #:options opts))) |
| 188 | file-systems))) |
| 189 | |
| 190 | (define (essential-services os) |
| 191 | "Return the list of essential services for OS. These are special services |
| 192 | that implement part of what's declared in OS are responsible for low-level |
| 193 | bookkeeping." |
| 194 | (mlet* %store-monad ((root-fs (root-file-system-service)) |
| 195 | (other-fs (other-file-system-services os)) |
| 196 | (procs (user-processes-service |
| 197 | (map (compose first service-provision) |
| 198 | other-fs))) |
| 199 | (host-name (host-name-service |
| 200 | (operating-system-host-name os)))) |
| 201 | (return (cons* host-name procs root-fs other-fs)))) |
| 202 | |
| 203 | (define (operating-system-services os) |
| 204 | "Return all the services of OS, including \"internal\" services that do not |
| 205 | explicitly appear in OS." |
| 206 | (mlet %store-monad |
| 207 | ((user (sequence %store-monad (operating-system-user-services os))) |
| 208 | (essential (essential-services os))) |
| 209 | (return (append essential user)))) |
| 210 | |
| 211 | \f |
| 212 | ;;; |
| 213 | ;;; /etc. |
| 214 | ;;; |
| 215 | |
| 216 | (define %base-packages |
| 217 | ;; Default set of packages globally visible. It should include anything |
| 218 | ;; required for basic administrator tasks. |
| 219 | (list bash coreutils findutils grep sed |
| 220 | procps psmisc less zile |
| 221 | guile-final (@ (gnu packages admin) dmd) guix |
| 222 | util-linux inetutils isc-dhcp |
| 223 | net-tools ; XXX: remove when Inetutils suffices |
| 224 | module-init-tools kbd)) |
| 225 | |
| 226 | (define %default-issue |
| 227 | ;; Default contents for /etc/issue. |
| 228 | " |
| 229 | This is the GNU system. Welcome.\n") |
| 230 | |
| 231 | (define* (etc-directory #:key |
| 232 | (locale "C") (timezone "Europe/Paris") |
| 233 | (issue "Hello!\n") |
| 234 | (skeletons '()) |
| 235 | (pam-services '()) |
| 236 | (profile "/run/current-system/profile") |
| 237 | (sudoers "")) |
| 238 | "Return a derivation that builds the static part of the /etc directory." |
| 239 | (mlet* %store-monad |
| 240 | ((pam.d (pam-services->directory pam-services)) |
| 241 | (sudoers (text-file "sudoers" sudoers)) |
| 242 | (login.defs (text-file "login.defs" "# Empty for now.\n")) |
| 243 | (shells (text-file "shells" ; used by xterm and others |
| 244 | "\ |
| 245 | /bin/sh |
| 246 | /run/current-system/profile/bin/sh |
| 247 | /run/current-system/profile/bin/bash\n")) |
| 248 | (issue (text-file "issue" issue)) |
| 249 | |
| 250 | ;; TODO: Generate bashrc from packages' search-paths. |
| 251 | (bashrc (text-file* "bashrc" " |
| 252 | export PS1='\\u@\\h\\$ ' |
| 253 | |
| 254 | export LC_ALL=\"" locale "\" |
| 255 | export TZ=\"" timezone "\" |
| 256 | export TZDIR=\"" tzdata "/share/zoneinfo\" |
| 257 | |
| 258 | export PATH=$HOME/.guix-profile/bin:/run/current-system/profile/bin |
| 259 | export PATH=/run/setuid-programs:/run/current-system/profile/sbin:$PATH |
| 260 | export CPATH=$HOME/.guix-profile/include:" profile "/include |
| 261 | export LIBRARY_PATH=$HOME/.guix-profile/lib:" profile "/lib |
| 262 | alias ls='ls -p --color' |
| 263 | alias ll='ls -l' |
| 264 | ")) |
| 265 | (skel (skeleton-directory skeletons))) |
| 266 | (file-union "etc" |
| 267 | `(("services" ,#~(string-append #$net-base "/etc/services")) |
| 268 | ("protocols" ,#~(string-append #$net-base "/etc/protocols")) |
| 269 | ("rpc" ,#~(string-append #$net-base "/etc/rpc")) |
| 270 | ("pam.d" ,#~#$pam.d) |
| 271 | ("login.defs" ,#~#$login.defs) |
| 272 | ("issue" ,#~#$issue) |
| 273 | ("skel" ,#~#$skel) |
| 274 | ("shells" ,#~#$shells) |
| 275 | ("profile" ,#~#$bashrc) |
| 276 | ("localtime" ,#~(string-append #$tzdata "/share/zoneinfo/" |
| 277 | #$timezone)) |
| 278 | ("sudoers" ,#~#$sudoers))))) |
| 279 | |
| 280 | (define (operating-system-profile os) |
| 281 | "Return a derivation that builds the default profile of OS." |
| 282 | ;; TODO: Replace with a real profile with a manifest. |
| 283 | (union (operating-system-packages os) |
| 284 | #:name "default-profile")) |
| 285 | |
| 286 | (define %root-account |
| 287 | ;; Default root account. |
| 288 | (user-account |
| 289 | (name "root") |
| 290 | (password "") |
| 291 | (uid 0) (group "root") |
| 292 | (comment "System administrator") |
| 293 | (home-directory "/root"))) |
| 294 | |
| 295 | (define (operating-system-accounts os) |
| 296 | "Return the user accounts for OS, including an obligatory 'root' account." |
| 297 | (define users |
| 298 | ;; Make sure there's a root account. |
| 299 | (if (find (lambda (user) |
| 300 | (and=> (user-account-uid user) zero?)) |
| 301 | (operating-system-users os)) |
| 302 | (operating-system-users os) |
| 303 | (cons %root-account (operating-system-users os)))) |
| 304 | |
| 305 | (mlet %store-monad ((services (operating-system-services os))) |
| 306 | (return (append users |
| 307 | (append-map service-user-accounts services))))) |
| 308 | |
| 309 | (define (operating-system-etc-directory os) |
| 310 | "Return that static part of the /etc directory of OS." |
| 311 | (mlet* %store-monad |
| 312 | ((services (operating-system-services os)) |
| 313 | (pam-services -> |
| 314 | ;; Services known to PAM. |
| 315 | (delete-duplicates |
| 316 | (append (operating-system-pam-services os) |
| 317 | (append-map service-pam-services services)))) |
| 318 | (profile-drv (operating-system-profile os)) |
| 319 | (skeletons (operating-system-skeletons os))) |
| 320 | (etc-directory #:pam-services pam-services |
| 321 | #:skeletons skeletons |
| 322 | #:issue (operating-system-issue os) |
| 323 | #:locale (operating-system-locale os) |
| 324 | #:timezone (operating-system-timezone os) |
| 325 | #:sudoers (operating-system-sudoers os) |
| 326 | #:profile profile-drv))) |
| 327 | |
| 328 | (define %setuid-programs |
| 329 | ;; Default set of setuid-root programs. |
| 330 | (let ((shadow (@ (gnu packages admin) shadow))) |
| 331 | (list #~(string-append #$shadow "/bin/passwd") |
| 332 | #~(string-append #$shadow "/bin/su") |
| 333 | #~(string-append #$inetutils "/bin/ping") |
| 334 | #~(string-append #$sudo "/bin/sudo") |
| 335 | #~(string-append #$fuse "/bin/fusermount")))) |
| 336 | |
| 337 | (define %sudoers-specification |
| 338 | ;; Default /etc/sudoers contents: 'root' and all members of the 'wheel' |
| 339 | ;; group can do anything. See |
| 340 | ;; <http://www.sudo.ws/sudo/man/1.8.10/sudoers.man.html>. |
| 341 | ;; TODO: Add a declarative API. |
| 342 | "root ALL=(ALL) ALL |
| 343 | %wheel ALL=(ALL) ALL\n") |
| 344 | |
| 345 | (define (user-group->gexp group) |
| 346 | "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for |
| 347 | 'active-groups'." |
| 348 | #~(list #$(user-group-name group) |
| 349 | #$(user-group-password group) |
| 350 | #$(user-group-id group))) |
| 351 | |
| 352 | (define (user-account->gexp account) |
| 353 | "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for |
| 354 | 'activate-users'." |
| 355 | #~`(#$(user-account-name account) |
| 356 | #$(user-account-uid account) |
| 357 | #$(user-account-group account) |
| 358 | #$(user-account-supplementary-groups account) |
| 359 | #$(user-account-comment account) |
| 360 | #$(user-account-home-directory account) |
| 361 | ,#$(user-account-shell account) ; this one is a gexp |
| 362 | #$(user-account-password account))) |
| 363 | |
| 364 | (define (operating-system-activation-script os) |
| 365 | "Return the activation script for OS---i.e., the code that \"activates\" the |
| 366 | stateful part of OS, including user accounts and groups, special directories, |
| 367 | etc." |
| 368 | (define %modules |
| 369 | '((guix build activation) |
| 370 | (guix build utils) |
| 371 | (guix build linux-initrd))) |
| 372 | |
| 373 | (define (service-activations services) |
| 374 | ;; Return the activation scripts for SERVICES. |
| 375 | (let ((gexps (filter-map service-activate services))) |
| 376 | (sequence %store-monad (map (cut gexp->file "activate-service.scm" <>) |
| 377 | gexps)))) |
| 378 | |
| 379 | (mlet* %store-monad ((services (operating-system-services os)) |
| 380 | (actions (service-activations services)) |
| 381 | (etc (operating-system-etc-directory os)) |
| 382 | (modules (imported-modules %modules)) |
| 383 | (compiled (compiled-modules %modules)) |
| 384 | (accounts (operating-system-accounts os))) |
| 385 | (define setuid-progs |
| 386 | (operating-system-setuid-programs os)) |
| 387 | |
| 388 | (define user-specs |
| 389 | (map user-account->gexp accounts)) |
| 390 | |
| 391 | (define groups |
| 392 | (append (operating-system-groups os) |
| 393 | (append-map service-user-groups services))) |
| 394 | |
| 395 | (define group-specs |
| 396 | (map user-group->gexp groups)) |
| 397 | |
| 398 | (gexp->file "boot" |
| 399 | #~(begin |
| 400 | (eval-when (expand load eval) |
| 401 | ;; Make sure 'use-modules' below succeeds. |
| 402 | (set! %load-path (cons #$modules %load-path)) |
| 403 | (set! %load-compiled-path |
| 404 | (cons #$compiled %load-compiled-path))) |
| 405 | |
| 406 | (use-modules (guix build activation)) |
| 407 | |
| 408 | ;; Populate /etc. |
| 409 | (activate-etc #$etc) |
| 410 | |
| 411 | ;; Add users and user groups. |
| 412 | (setenv "PATH" |
| 413 | (string-append #$(@ (gnu packages admin) shadow) |
| 414 | "/sbin")) |
| 415 | (activate-users+groups (list #$@user-specs) |
| 416 | (list #$@group-specs)) |
| 417 | |
| 418 | ;; Activate setuid programs. |
| 419 | (activate-setuid-programs (list #$@setuid-progs)) |
| 420 | |
| 421 | ;; Run the services' activation snippets. |
| 422 | ;; TODO: Use 'load-compiled'. |
| 423 | (for-each primitive-load '#$actions) |
| 424 | |
| 425 | ;; Set up /run/current-system. |
| 426 | (activate-current-system))))) |
| 427 | |
| 428 | (define (operating-system-boot-script os) |
| 429 | "Return the boot script for OS---i.e., the code started by the initrd once |
| 430 | we're running in the final root." |
| 431 | (mlet* %store-monad ((services (operating-system-services os)) |
| 432 | (activate (operating-system-activation-script os)) |
| 433 | (dmd-conf (dmd-configuration-file services))) |
| 434 | (gexp->file "boot" |
| 435 | #~(begin |
| 436 | ;; Activate the system. |
| 437 | ;; TODO: Use 'load-compiled'. |
| 438 | (primitive-load #$activate) |
| 439 | |
| 440 | ;; Keep track of the booted system. |
| 441 | (false-if-exception (delete-file "/run/booted-system")) |
| 442 | (symlink (readlink "/run/current-system") |
| 443 | "/run/booted-system") |
| 444 | |
| 445 | ;; Close any remaining open file descriptors to be on the |
| 446 | ;; safe side. This must be the very last thing we do, |
| 447 | ;; because Guile has internal FDs such as 'sleep_pipe' |
| 448 | ;; that need to be alive. |
| 449 | (let loop ((fd 3)) |
| 450 | (when (< fd 1024) |
| 451 | (false-if-exception (close-fdes fd)) |
| 452 | (loop (+ 1 fd)))) |
| 453 | |
| 454 | ;; Start dmd. |
| 455 | (execl (string-append #$dmd "/bin/dmd") |
| 456 | "dmd" "--config" #$dmd-conf))))) |
| 457 | |
| 458 | (define (operating-system-root-file-system os) |
| 459 | "Return the root file system of OS." |
| 460 | (find (match-lambda |
| 461 | (($ <file-system> _ _ "/") #t) |
| 462 | (_ #f)) |
| 463 | (operating-system-file-systems os))) |
| 464 | |
| 465 | (define (operating-system-initrd-file os) |
| 466 | "Return a gexp denoting the initrd file of OS." |
| 467 | (define boot-file-systems |
| 468 | (filter (match-lambda |
| 469 | (($ <file-system> device title "/") |
| 470 | #t) |
| 471 | (($ <file-system> device title mount-point type flags |
| 472 | options boot?) |
| 473 | boot?)) |
| 474 | (operating-system-file-systems os))) |
| 475 | |
| 476 | (mlet %store-monad |
| 477 | ((initrd ((operating-system-initrd os) boot-file-systems))) |
| 478 | (return #~(string-append #$initrd "/initrd")))) |
| 479 | |
| 480 | (define (operating-system-grub.cfg os) |
| 481 | "Return the GRUB configuration file for OS." |
| 482 | (mlet* %store-monad |
| 483 | ((system (operating-system-derivation os)) |
| 484 | (root-fs -> (operating-system-root-file-system os)) |
| 485 | (kernel -> (operating-system-kernel os)) |
| 486 | (entries -> (list (menu-entry |
| 487 | (label (string-append |
| 488 | "GNU system with " |
| 489 | (package-full-name kernel) |
| 490 | " (technology preview)")) |
| 491 | (linux kernel) |
| 492 | (linux-arguments |
| 493 | (list (string-append "--root=" |
| 494 | (file-system-device root-fs)) |
| 495 | #~(string-append "--system=" #$system) |
| 496 | #~(string-append "--load=" #$system |
| 497 | "/boot"))) |
| 498 | (initrd #~(string-append #$system "/initrd")))))) |
| 499 | (grub-configuration-file (operating-system-bootloader os) entries))) |
| 500 | |
| 501 | (define (operating-system-derivation os) |
| 502 | "Return a derivation that builds OS." |
| 503 | (mlet* %store-monad |
| 504 | ((profile (operating-system-profile os)) |
| 505 | (etc (operating-system-etc-directory os)) |
| 506 | (boot (operating-system-boot-script os)) |
| 507 | (kernel -> (operating-system-kernel os)) |
| 508 | (initrd (operating-system-initrd-file os))) |
| 509 | (file-union "system" |
| 510 | `(("boot" ,#~#$boot) |
| 511 | ("kernel" ,#~#$kernel) |
| 512 | ("initrd" ,initrd) |
| 513 | ("profile" ,#~#$profile) |
| 514 | ("etc" ,#~#$etc))))) |
| 515 | |
| 516 | ;;; system.scm ends here |