1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com>
5 ;;; Copyright © 2017, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
7 ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
8 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (guix scripts system)
26 #:use-module (guix config)
27 #:use-module (guix ui)
28 #:use-module ((guix status) #:select (with-status-verbosity))
29 #:use-module (guix store)
30 #:autoload (guix store database) (register-path)
31 #:use-module (guix describe)
32 #:use-module (guix grafts)
33 #:use-module (guix gexp)
34 #:use-module (guix derivations)
35 #:use-module (guix packages)
36 #:use-module (guix utils)
37 #:use-module (guix monads)
38 #:use-module (guix records)
39 #:use-module (guix profiles)
40 #:use-module (guix scripts)
41 #:use-module (guix channels)
42 #:use-module (guix scripts build)
43 #:autoload (guix scripts package) (delete-generations
44 delete-matching-generations)
45 #:autoload (guix scripts pull) (channel-commit-hyperlink)
46 #:use-module (guix graph)
47 #:use-module (guix scripts graph)
48 #:use-module (guix scripts system reconfigure)
49 #:use-module (guix build utils)
50 #:use-module (guix progress)
51 #:use-module ((guix build syscalls) #:select (terminal-columns))
52 #:use-module (gnu build install)
53 #:autoload (gnu build file-systems)
54 (find-partition-by-label find-partition-by-uuid)
55 #:autoload (gnu build linux-modules)
56 (device-module-aliases matching-modules)
57 #:use-module (gnu system linux-initrd)
58 #:use-module (gnu image)
59 #:use-module (gnu system)
60 #:use-module (gnu bootloader)
61 #:use-module (gnu system file-systems)
62 #:use-module (gnu system image)
63 #:use-module (gnu system mapped-devices)
64 #:use-module (gnu system linux-container)
65 #:use-module (gnu system uuid)
66 #:use-module (gnu system vm)
67 #:use-module (gnu services)
68 #:use-module (gnu services shepherd)
69 #:use-module (gnu services herd)
70 #:use-module (srfi srfi-1)
71 #:use-module (srfi srfi-11)
72 #:use-module (srfi srfi-19)
73 #:use-module (srfi srfi-26)
74 #:use-module (srfi srfi-34)
75 #:use-module (srfi srfi-35)
76 #:use-module (srfi srfi-37)
77 #:use-module (ice-9 match)
78 #:use-module (rnrs bytevectors)
80 read-operating-system))
84 ;;; Operating system declaration.
88 ;; Module in which the machine description file is loaded.
89 (make-user-module '((gnu system)
91 (gnu system shadow))))
93 (define (read-operating-system file)
94 "Read the operating-system declaration from FILE and return it."
95 (load* file %user-module))
102 (define-syntax-rule (save-load-path-excursion body ...)
103 "Save the current values of '%load-path' and '%load-compiled-path', run
104 BODY..., and restore them."
105 (let ((path %load-path)
106 (cpath %load-compiled-path))
112 (set! %load-path path)
113 (set! %load-compiled-path cpath)))))
115 (define-syntax-rule (save-environment-excursion body ...)
116 "Save the current environment variables, run BODY..., and restore them."
117 (let ((env (environ)))
125 (define topologically-sorted*
126 (store-lift topologically-sorted))
129 (define* (copy-item item references target
130 #:key (log-port (current-error-port)))
131 "Copy ITEM to the store under root directory TARGET and register it with
132 REFERENCES as its set of references."
133 (let ((dest (string-append target item))
134 (state (string-append target "/var/guix")))
135 (format log-port "copying '~a'...~%" item)
137 ;; Remove DEST if it exists to make sure that (1) we do not fail badly
138 ;; while trying to overwrite it (see <http://bugs.gnu.org/20722>), and
139 ;; (2) we end up with the right contents.
140 (when (false-if-exception (lstat dest))
141 (for-each make-file-writable
142 (find-files dest (lambda (file stat)
143 (eq? 'directory (stat:type stat)))
145 (delete-file-recursively dest))
147 (copy-recursively item dest
148 #:log (%make-void-port "w"))
150 ;; Register ITEM; as a side-effect, it resets timestamps, etc.
151 ;; Explicitly use "TARGET/var/guix" as the state directory, to avoid
152 ;; reproducing the user's current settings; see
153 ;; <http://bugs.gnu.org/18049>.
154 (unless (register-path item
156 #:state-directory state
157 #:references references)
158 (leave (G_ "failed to register '~a' under '~a'~%")
161 (define* (copy-closure item target
162 #:key (log-port (current-error-port)))
163 "Copy ITEM and all its dependencies to the store under root directory
164 TARGET, and register them."
165 (mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
166 (refs (mapm %store-monad references* to-copy))
167 (info (mapm %store-monad query-path-info*
169 (append to-copy (concatenate refs)))))
170 (size -> (reduce + 0 (map path-info-nar-size info))))
172 (progress-reporter/bar (length to-copy)
173 (format #f (G_ "copying to '~a'...")
176 (check-available-space size target)
178 (call-with-progress-reporter progress-bar
180 (let ((void (%make-void-port "w")))
181 (for-each (lambda (item refs)
182 (copy-item item refs target #:log-port void)
186 (return *unspecified*)))
188 (define* (install os-drv target
189 #:key (log-port (current-output-port))
190 install-bootloader? bootloader bootcfg)
191 "Copy the closure of BOOTCFG, which includes the output of OS-DRV, to
192 directory TARGET. TARGET must be an absolute directory name since that's what
193 'register-path' expects.
195 When INSTALL-BOOTLOADER? is true, install bootloader using BOOTCFG."
196 (define (maybe-copy to-copy)
197 (with-monad %store-monad
198 (if (string=? target "/")
200 (warning (G_ "initializing the current root file system~%"))
203 ;; Make sure the target store exists.
204 (mkdir-p (string-append target (%store-prefix)))
206 ;; Copy items to the new store.
207 (copy-closure to-copy target #:log-port log-port)))))
209 ;; Make sure TARGET is root-owned when running as root, but still allow
210 ;; non-root uses (useful for testing.) See
211 ;; <http://lists.gnu.org/archive/html/guix-devel/2015-05/msg00452.html>.
212 (if (zero? (geteuid))
214 (warning (G_ "not running as 'root', so \
215 the ownership of '~a' may be incorrect!~%")
218 ;; If a previous installation was attempted, make sure we start anew; in
219 ;; particular, we don't want to keep a store database that might not
220 ;; correspond to what we're actually putting in the store.
221 (let ((state (string-append target "/var/guix")))
222 (when (file-exists? state)
223 (delete-file-recursively state)))
226 (let ((os-dir (derivation->output-path os-drv))
227 (format (lift format %store-monad))
228 (populate (lift2 populate-root-file-system %store-monad)))
230 (mlet %store-monad ((bootcfg (lower-object bootcfg)))
232 ;; Copy the closure of BOOTCFG, which includes OS-DIR,
233 ;; eventual background image and so on.
234 (maybe-copy (derivation->output-path bootcfg))
236 ;; Create a bunch of additional files.
237 (format log-port "populating '~a'...~%" target)
238 (populate os-dir target)
240 (mwhen install-bootloader?
241 (install-bootloader local-eval bootloader bootcfg
244 (info (G_ "bootloader successfully installed on '~a'~%")
245 (bootloader-configuration-target bootloader))))))))
252 (define %system-profile
253 ;; The system profile.
254 (string-append %state-directory "/profiles/system"))
256 (define-syntax-rule (with-shepherd-error-handling mbody ...)
257 "Catch and report Shepherd errors that arise when binding MBODY, a monadic
258 expression in %STORE-MONAD."
262 (guard (c ((shepherd-error? c)
263 (values (report-shepherd-error c) store)))
264 (values (run-with-store store (mbegin %store-monad mbody ...))
266 (lambda (key proc format-string format-args errno . rest)
267 (warning (G_ "while talking to shepherd: ~a~%")
268 (apply format #f format-string format-args))
269 (values #f store)))))
271 (define (report-shepherd-error error)
272 "Report ERROR, a '&shepherd-error' error condition object."
273 (cond ((service-not-found-error? error)
274 (report-error (G_ "service '~a' could not be found~%")
275 (service-not-found-error-service error)))
276 ((action-not-found-error? error)
277 (report-error (G_ "service '~a' does not have an action '~a'~%")
278 (action-not-found-error-service error)
279 (action-not-found-error-action error)))
280 ((action-exception-error? error)
281 (report-error (G_ "exception caught while executing '~a' \
283 (action-exception-error-action error)
284 (action-exception-error-service error))
285 (print-exception (current-error-port) #f
286 (action-exception-error-key error)
287 (action-exception-error-arguments error)))
288 ((unknown-shepherd-error? error)
289 (report-error (G_ "something went wrong: ~s~%")
290 (unknown-shepherd-error-sexp error)))
291 ((shepherd-error? error)
292 (report-error (G_ "shepherd error~%")))
293 ((not error) ;not an error
296 (define-syntax-rule (unless-file-not-found exp)
301 (if (= ENOENT (system-error-errno args))
303 (apply throw args)))))
305 (define (seconds->string seconds)
306 "Return a string representing the date for SECONDS."
307 (let ((time (make-time time-utc 0 seconds)))
308 (date->string (time-utc->date time)
311 (define* (profile-boot-parameters #:optional (profile %system-profile)
313 (reverse (generation-numbers profile))))
314 "Return a list of 'boot-parameters' for the generations of PROFILE specified
315 by NUMBERS, which is a list of generation numbers. The list is ordered from
316 the most recent to the oldest profiles."
317 (define (system->boot-parameters system number time)
318 (unless-file-not-found
319 (let* ((params (read-boot-parameters-file system))
320 (label (boot-parameters-label params)))
323 (label (string-append label " (#"
324 (number->string number) ", "
325 (seconds->string time) ")"))))))
326 (let* ((systems (map (cut generation-file-name profile <>)
328 (times (map (lambda (system)
329 (unless-file-not-found
330 (stat:mtime (lstat system))))
332 (filter-map system->boot-parameters systems numbers times)))
338 (define (roll-back-system store)
339 "Roll back the system profile to its previous generation. STORE is an open
340 connection to the store."
341 (switch-to-system-generation store "-1"))
345 ;;; Switch generations.
347 (define (switch-to-system-generation store spec)
348 "Switch the system profile to the generation specified by SPEC, and
349 re-install bootloader with a configuration file that uses the specified system
350 generation as its default entry. STORE is an open connection to the store."
351 (let ((number (relative-generation-spec->number %system-profile spec)))
354 (reinstall-bootloader store number)
355 (switch-to-generation* %system-profile number))
356 (leave (G_ "cannot switch to system generation '~a'~%") spec))))
358 (define* (system-bootloader-name #:optional (system %system-profile))
359 "Return the bootloader name stored in SYSTEM's \"parameters\" file."
360 (let ((params (unless-file-not-found
361 (read-boot-parameters-file system))))
362 (boot-parameters-bootloader-name params)))
364 (define (reinstall-bootloader store number)
365 "Re-install bootloader for existing system profile generation NUMBER.
366 STORE is an open connection to the store."
367 (let* ((generation (generation-file-name %system-profile number))
368 ;; Detect the bootloader used in %system-profile.
369 (bootloader (lookup-bootloader-by-name (system-bootloader-name)))
371 ;; Use the detected bootloader with default configuration.
372 ;; It will be enough to allow the system to boot.
373 (bootloader-config (bootloader-configuration
374 (bootloader bootloader)))
376 ;; Make the specified system generation the default entry.
377 (params (first (profile-boot-parameters %system-profile
380 (delv number (reverse (generation-numbers %system-profile))))
381 (old-params (profile-boot-parameters
382 %system-profile old-generations))
383 (entries (cons (boot-parameters->menu-entry params)
384 (boot-parameters-bootloader-menu-entries params)))
385 (old-entries (map boot-parameters->menu-entry old-params)))
386 (run-with-store store
388 ((bootcfg (lower-object
389 ((bootloader-configuration-file-generator bootloader)
390 bootloader-config entries
391 #:old-entries old-entries)))
392 (drvs -> (list bootcfg)))
394 (built-derivations drvs)
395 ;; Only install bootloader configuration file.
396 (install-bootloader local-eval bootloader-config bootcfg
397 #:run-installer? #f))))))
404 (define (service-node-label service)
405 "Return a label to represent SERVICE."
406 (let ((type (service-kind service))
407 (value (service-value service)))
408 (string-append (symbol->string (service-type-name type))
409 (cond ((or (number? value) (symbol? value))
410 (string-append " " (object->string value)))
412 (string-append " " value))
413 ((file-system? value)
414 (string-append " " (file-system-mount-point value)))
418 (define (service-node-type services)
419 "Return a node type for SERVICES. Since <service> instances are not
420 self-contained (they express dependencies on service types, not on services),
421 we have to create the 'edges' procedure dynamically as a function of the full
425 (description "the DAG of services")
426 (identifier (lift1 object-address %store-monad))
427 (label service-node-label)
428 (edges (lift1 (service-back-edges services) %store-monad))))
430 (define (shepherd-service-node-label service)
431 "Return a label for a node representing a <shepherd-service>."
432 (string-join (map symbol->string (shepherd-service-provision service))))
434 (define (shepherd-service-node-type services)
435 "Return a node type for SERVICES, a list of <shepherd-service>."
437 (name "shepherd-service")
438 (description "the dependency graph of shepherd services")
439 (identifier (lift1 shepherd-service-node-label %store-monad))
440 (label shepherd-service-node-label)
441 (edges (lift1 (shepherd-service-back-edges services) %store-monad))))
448 (define (sexp->channel sexp)
449 "Return the channel corresponding to SEXP, an sexp as found in the
450 \"provenance\" file produced by 'provenance-service-type'."
452 (('channel ('name name)
456 (channel (name name) (url url)
457 (branch branch) (commit commit)))))
459 (define* (display-system-generation number
460 #:optional (profile %system-profile))
461 "Display a summary of system generation NUMBER in a human-readable format."
462 (define (display-channel channel)
463 (format #t " ~a:~%" (channel-name channel))
464 (format #t (G_ " repository URL: ~a~%") (channel-url channel))
465 (when (channel-branch channel)
466 (format #t (G_ " branch: ~a~%") (channel-branch channel)))
467 (format #t (G_ " commit: ~a~%")
468 (if (supports-hyperlinks?)
469 (channel-commit-hyperlink channel)
470 (channel-commit channel))))
472 (unless (zero? number)
473 (let* ((generation (generation-file-name profile number))
474 (params (read-boot-parameters-file generation))
475 (label (boot-parameters-label params))
476 (bootloader-name (boot-parameters-bootloader-name params))
477 (root (boot-parameters-root-device params))
478 (root-device (if (bytevector? root)
481 (kernel (boot-parameters-kernel params))
482 (provenance (catch 'system-error
484 (call-with-input-file
485 (string-append generation "/provenance")
488 (display-generation profile number)
489 (format #t (G_ " file name: ~a~%") generation)
490 (format #t (G_ " canonical file name: ~a~%") (readlink* generation))
491 ;; TRANSLATORS: Please preserve the two-space indentation.
492 (format #t (G_ " label: ~a~%") label)
493 (format #t (G_ " bootloader: ~a~%") bootloader-name)
495 ;; TRANSLATORS: The '~[', '~;', and '~]' sequences in this string must
496 ;; be preserved. They denote conditionals, such that the result will
498 ;; root device: UUID: 12345-678
500 ;; root device: label: "my-root"
502 ;; root device: /dev/sda3
503 (format #t (G_ " root device: ~[UUID: ~a~;label: ~s~;~a~]~%")
504 (cond ((uuid? root-device) 0)
505 ((file-system-label? root-device) 1)
507 (file-system-device->string root-device))
509 (format #t (G_ " kernel: ~a~%") kernel)
513 (('provenance ('version 0)
514 ('channels channels ...)
515 ('configuration-file config-file))
516 (unless (null? channels)
517 ;; TRANSLATORS: Here "channel" is the same terminology as used in
518 ;; "guix describe" and "guix pull --channels".
519 (format #t (G_ " channels:~%"))
520 (for-each display-channel (map sexp->channel channels)))
522 (format #t (G_ " configuration file: ~a~%")
523 (if (supports-hyperlinks?)
524 (file-hyperlink config-file)
527 (define* (list-generations pattern #:optional (profile %system-profile))
528 "Display in a human-readable format all the system generations matching
529 PATTERN, a string. When PATTERN is #f, display all the system generations."
530 (cond ((not (file-exists? profile)) ; XXX: race condition
531 (raise (condition (&profile-not-found-error
532 (profile profile)))))
534 (for-each display-system-generation (profile-generations profile)))
535 ((matching-generations pattern profile)
538 (if (null-list? numbers)
541 (for-each display-system-generation numbers)))))))
545 ;;; File system declaration checks.
548 (define (check-file-system-availability file-systems)
549 "Check whether the UUIDs or partition labels that FILE-SYSTEMS refer to, if
550 any, are available. Raise an error if they're not."
553 (and (file-system-mount? fs)
554 (not (member (file-system-type fs)
555 %pseudo-file-system-types))
556 ;; Don't try to validate network file systems.
557 (not (string-prefix? "nfs" (file-system-type fs)))
558 (not (memq 'bind-mount (file-system-flags fs)))))
563 (file-system-label? (file-system-device fs)))
568 (string? (file-system-device fs)))
573 (uuid? (file-system-device fs)))
578 (define (file-system-location* fs)
580 (source-properties->location
581 (file-system-location fs))))
583 (let-syntax ((error (syntax-rules ()
587 (format (current-error-port)
589 (for-each (lambda (fs)
592 (stat (file-system-device fs)))
594 (let ((errno (system-error-errno args))
595 (device (file-system-device fs)))
596 (error (G_ "~a: error: device '~a' not found: ~a~%")
597 (file-system-location* fs) device
599 (unless (string-prefix? "/" device)
600 (display-hint (format #f (G_ "If '~a' is a file system
601 label, write @code{(file-system-label ~s)} in your @code{device} field.")
604 (for-each (lambda (fs)
605 (let ((label (file-system-label->string
606 (file-system-device fs))))
607 (unless (find-partition-by-label label)
608 (error (G_ "~a: error: file system with label '~a' not found~%")
609 (file-system-location* fs) label))))
611 (for-each (lambda (fs)
612 (unless (find-partition-by-uuid (file-system-device fs))
613 (error (G_ "~a: error: file system with UUID '~a' not found~%")
614 (file-system-location* fs)
615 (uuid->string (file-system-device fs)))))
619 ;; Better be safe than sorry.
622 (define (check-mapped-devices os)
623 "Check that each of MAPPED-DEVICES is valid according to the 'check'
624 procedure of its type."
625 (define boot-mapped-devices
626 (operating-system-boot-mapped-devices os))
628 (define (needed-for-boot? md)
629 (memq md boot-mapped-devices))
631 (define initrd-modules
632 (operating-system-initrd-modules os))
634 (for-each (lambda (md)
635 (let ((check (mapped-device-kind-check
636 (mapped-device-type md))))
637 ;; We expect CHECK to raise an exception with a detailed
638 ;; '&message' if something goes wrong.
640 #:needed-for-boot? (needed-for-boot? md)
641 #:initrd-modules initrd-modules)))
642 (operating-system-mapped-devices os)))
644 (define (check-initrd-modules os)
645 "Check that modules needed by 'needed-for-boot' file systems in OS are
646 available in the initrd. Note that mapped devices are responsible for
647 checking this by themselves in their 'check' procedure."
648 (define (file-system-/dev fs)
649 (let ((device (file-system-device fs)))
654 (find-partition-by-uuid device))
655 ((? file-system-label?)
656 (find-partition-by-label (file-system-label->string device))))))
659 (filter file-system-needed-for-boot?
660 (operating-system-file-systems os)))
662 (for-each (lambda (fs)
663 (check-device-initrd-modules (file-system-/dev fs)
664 (operating-system-initrd-modules os)
665 (source-properties->location
666 (file-system-location fs))))
674 (define* (system-derivation-for-action os base-image action
675 #:key image-size file-system-type
676 full-boot? container-shared-network?
678 "Return as a monadic value the derivation for OS according to ACTION."
680 ((build init reconfigure)
681 (operating-system-derivation os))
686 #:shared-network? container-shared-network?))
688 (system-qemu-image os #:disk-image-size image-size))
690 (system-qemu-image/shared-store-script os
691 #:full-boot? full-boot?
696 #:mappings mappings))
703 (operating-system os)))))
705 (system-docker-image os #:shared-network? container-shared-network?))))
707 (define (maybe-suggest-running-guix-pull)
708 "Suggest running 'guix pull' if this has never been done before."
709 ;; Check whether we're running a 'guix pull'-provided 'guix' command. When
710 ;; 'current-profile' returns #f, we may be running the globally-installed
711 ;; 'guix' and thus run the risk of deploying an older 'guix'. See
712 ;; <https://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html>
713 (unless (or (current-profile) (getenv "GUIX_UNINSTALLED"))
714 (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
715 (warning (G_ "Failing to do that may downgrade your system!~%"))))
717 (define (bootloader-installer-script installer
718 bootloader device target)
719 "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
720 and TARGET arguments."
721 (scheme-file "bootloader-installer"
722 (with-imported-modules '((gnu build bootloader)
725 (use-modules (gnu build bootloader)
731 (guard (c ((message-condition? c) ;XXX: i18n
732 (format (current-error-port) "error: ~a~%"
733 (condition-message c))
735 (#$installer #$bootloader #$device #$target)
736 (info (G_ "bootloader successfully installed on '~a'~%")
739 (define (local-eval exp)
740 "Evaluate EXP, a G-Expression, in-place."
741 (mlet* %store-monad ((lowered (lower-gexp exp))
742 (_ (built-derivations (lowered-gexp-inputs lowered))))
743 (save-load-path-excursion
744 (set! %load-path (lowered-gexp-load-path lowered))
745 (set! %load-compiled-path (lowered-gexp-load-compiled-path lowered))
746 (return (primitive-eval (lowered-gexp-sexp lowered))))))
748 (define* (perform-action action os
753 dry-run? derivations-only?
754 use-substitutes? bootloader-target target
755 image-size file-system-type full-boot?
756 container-shared-network?
759 "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
760 bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
761 target root directory; IMAGE-SIZE is the size of the image to be built, for
762 the 'vm-image' and 'disk-image' actions. The root file system is created as a
763 FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it
764 determines whether to boot directly to the kernel or to the bootloader.
765 CONTAINER-SHARED-NETWORK? determines if the container will use a separate
768 When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
771 When GC-ROOT is a path, also make that path an indirect root of the build
772 output when building a system derivation, such as a disk image.
774 When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
777 (cut format #t "~a~%" <>))
780 (if (eq? 'init action)
782 (map boot-parameters->menu-entry (profile-boot-parameters))))
785 (operating-system-bootloader os))
788 (and (memq action '(init reconfigure))
789 (operating-system-bootcfg os menu-entries)))
791 (when (eq? action 'reconfigure)
792 (maybe-suggest-running-guix-pull))
794 ;; Check whether the declared file systems exist. This is better than
795 ;; instantiating a broken configuration. Assume that we can only check if
797 (when (and (not skip-safety-checks?)
798 (memq action '(init reconfigure)))
799 (check-mapped-devices os)
800 (when (zero? (getuid))
801 (check-file-system-availability (operating-system-file-systems os))
802 (check-initrd-modules os)))
805 ((target (current-target-system))
806 (image -> (find-image file-system-type target))
807 (sys (system-derivation-for-action os image action
808 #:file-system-type file-system-type
809 #:image-size image-size
810 #:full-boot? full-boot?
811 #:container-shared-network? container-shared-network?
812 #:mappings mappings))
814 ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
815 ;; --no-bootloader is passed, because we then use it as a GC root.
816 ;; See <http://bugs.gnu.org/21068>.
817 (drvs (mapm/accumulate-builds lower-object
818 (if (memq action '(init reconfigure))
821 (% (if derivations-only?
822 (return (for-each (compose println derivation-file-name)
824 (built-derivations drvs))))
826 (if (or dry-run? derivations-only?)
829 (for-each (compose println derivation->output-path)
835 (format #t (G_ "activating system...~%"))
837 (switch-to-system local-eval os)
838 (mwhen install-bootloader?
839 (install-bootloader local-eval bootloader bootcfg
840 #:target (or target "/"))
842 (info (G_ "bootloader successfully installed on '~a'~%")
843 (bootloader-configuration-target bootloader))))
844 (with-shepherd-error-handling
845 (upgrade-shepherd-services local-eval os)
846 (return (format #t (G_ "\
847 To complete the upgrade, run 'herd restart SERVICE' to stop,
848 upgrade, and restart each service that was not automatically restarted.\n"))))))
851 (format #t (G_ "initializing operating system under '~a'...~%")
853 (install sys (canonicalize-path target)
854 #:install-bootloader? install-bootloader?
855 #:bootloader bootloader
858 ;; All we had to do was to build SYS and maybe register an
860 (let ((output (derivation->output-path sys)))
863 (register-root* (list output) gc-root))
864 (return output)))))))))
866 (define (export-extension-graph os port)
867 "Export the service extension graph of OS to PORT."
868 (let* ((services (operating-system-services os))
869 (system (find (lambda (service)
870 (eq? (service-kind service) system-service-type))
872 (export-graph (list system) (current-output-port)
873 #:node-type (service-node-type services)
874 #:reverse-edges? #t)))
876 (define (export-shepherd-graph os port)
877 "Export the graph of shepherd services of OS to PORT."
878 (let* ((services (operating-system-services os))
879 (pid1 (fold-services services
880 #:target-type shepherd-root-service-type))
881 (shepherds (service-value pid1)) ;list of <shepherd-service>
882 (sinks (filter (lambda (service)
883 (null? (shepherd-service-requirement service)))
885 (export-graph sinks (current-output-port)
886 #:node-type (shepherd-service-node-type shepherds)
887 #:reverse-edges? #t)))
895 (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
896 Build the operating system declared in FILE according to ACTION.
897 Some ACTIONS support additional ARGS.\n"))
899 (display (G_ "The valid values for ACTION are:\n"))
902 search search for existing service types\n"))
904 reconfigure switch to a new operating system configuration\n"))
906 roll-back switch to the previous operating system configuration\n"))
908 describe describe the current system\n"))
910 list-generations list the system generations\n"))
912 switch-generation switch to an existing operating system configuration\n"))
914 delete-generations delete old system generations\n"))
916 build build the operating system without installing anything\n"))
918 container build a container that shares the host's store\n"))
920 vm build a virtual machine image that shares the host's store\n"))
922 vm-image build a freestanding virtual machine image\n"))
924 disk-image build a disk image, suitable for a USB stick\n"))
926 docker-image build a Docker image\n"))
928 init initialize a root file system to run GNU\n"))
930 extension-graph emit the service extension graph in Dot format\n"))
932 shepherd-graph emit the graph of shepherd services in Dot format\n"))
934 (show-build-options-help)
936 -d, --derivation return the derivation of the given system"))
938 -e, --expression=EXPR consider the operating-system EXPR evaluates to
939 instead of reading FILE, when applicable"))
942 apply STRATEGY (one of nothing-special, backtrace,
943 or debug) when an error occurs while reading FILE"))
945 --file-system-type=TYPE
946 for 'disk-image', produce a root file system of TYPE
947 (one of 'ext4', 'iso9660')"))
949 --image-size=SIZE for 'vm-image', produce an image of SIZE"))
951 --no-bootloader for 'init', do not install a bootloader"))
953 --save-provenance save provenance information"))
955 --share=SPEC for 'vm', share host file system according to SPEC"))
957 --expose=SPEC for 'vm', expose host file system according to SPEC"))
959 -N, --network for 'container', allow containers to access the network"))
961 -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container',
962 and 'build', make FILE a symlink to the result, and
963 register it as a garbage collector root"))
965 --full-boot for 'vm', make a full boot sequence"))
967 --skip-checks skip file system and initrd module safety checks"))
969 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
971 -v, --verbosity=LEVEL use the given verbosity LEVEL"))
974 -h, --help display this help and exit"))
976 -V, --version display version information and exit"))
978 (show-bug-report-information))
981 ;; Specifications of the command-line options.
982 (cons* (option '(#\h "help") #f #f
986 (option '(#\V "version") #f #f
988 (show-version-and-exit "guix system")))
989 (option '(#\e "expression") #t #f
990 (lambda (opt name arg result)
991 (alist-cons 'expression arg result)))
992 (option '(#\d "derivation") #f #f
993 (lambda (opt name arg result)
994 (alist-cons 'derivations-only? #t result)))
995 (option '("on-error") #t #f
996 (lambda (opt name arg result)
997 (alist-cons 'on-error (string->symbol arg)
999 (option '(#\t "file-system-type") #t #f
1000 (lambda (opt name arg result)
1001 (alist-cons 'file-system-type arg
1003 (option '("image-size") #t #f
1004 (lambda (opt name arg result)
1005 (alist-cons 'image-size (size->number arg)
1007 (option '(#\N "network") #f #f
1008 (lambda (opt name arg result)
1009 (alist-cons 'container-shared-network? #t result)))
1010 (option '("no-bootloader" "no-grub") #f #f
1011 (lambda (opt name arg result)
1012 (alist-cons 'install-bootloader? #f result)))
1013 (option '("full-boot") #f #f
1014 (lambda (opt name arg result)
1015 (alist-cons 'full-boot? #t result)))
1016 (option '("save-provenance") #f #f
1017 (lambda (opt name arg result)
1018 (alist-cons 'save-provenance? #t result)))
1019 (option '("skip-checks") #f #f
1020 (lambda (opt name arg result)
1021 (alist-cons 'skip-safety-checks? #t result)))
1023 (option '("share") #t #f
1024 (lambda (opt name arg result)
1025 (alist-cons 'file-system-mapping
1026 (specification->file-system-mapping arg #t)
1028 (option '("expose") #t #f
1029 (lambda (opt name arg result)
1030 (alist-cons 'file-system-mapping
1031 (specification->file-system-mapping arg #f)
1034 (option '(#\n "dry-run") #f #f
1035 (lambda (opt name arg result)
1036 (alist-cons 'dry-run? #t result)))
1037 (option '(#\v "verbosity") #t #f
1038 (lambda (opt name arg result)
1039 (let ((level (string->number* arg)))
1040 (alist-cons 'verbosity level
1041 (alist-delete 'verbosity result)))))
1042 (option '(#\s "system") #t #f
1043 (lambda (opt name arg result)
1044 (alist-cons 'system arg
1045 (alist-delete 'system result eq?))))
1046 (option '("target") #t #f
1047 (lambda (opt name arg result)
1048 (alist-cons 'target arg
1049 (alist-delete 'target result eq?))))
1050 (option '(#\r "root") #t #f
1051 (lambda (opt name arg result)
1052 (alist-cons 'gc-root arg result)))
1053 %standard-build-options))
1055 (define %default-options
1056 ;; Alist of default option values.
1057 `((system . ,(%current-system))
1061 (print-build-trace? . #t)
1062 (print-extended-build-trace? . #t)
1063 (multiplexed-build-output? . #t)
1066 (verbosity . #f) ;default
1067 (file-system-type . "ext4")
1068 (image-size . guess)
1069 (install-bootloader? . #t)))
1076 (define (process-action action args opts)
1077 "Process ACTION, a sub-command, with the arguments are listed in ARGS.
1078 ACTION must be one of the sub-commands that takes an operating system
1079 declaration as an argument (a file name.) OPTS is the raw alist of options
1080 resulting from command-line parsing."
1081 (define (ensure-operating-system file-or-exp obj)
1082 (unless (operating-system? obj)
1083 (leave (G_ "'~a' does not return an operating system~%")
1087 (define save-provenance?
1088 (or (assoc-ref opts 'save-provenance?)
1089 (memq action '(init reconfigure))))
1091 (let* ((file (match args
1094 (expr (assoc-ref opts 'expression))
1095 (system (assoc-ref opts 'system))
1096 (target (assoc-ref opts 'target))
1097 (transform (if save-provenance?
1098 (cut operating-system-with-provenance <> file)
1101 (ensure-operating-system
1106 (G_ "both file and expression cannot be specified~%")))
1110 (load* file %user-module
1111 #:on-error (assoc-ref opts 'on-error)))
1113 (leave (G_ "no configuration specified~%")))))))
1115 (dry? (assoc-ref opts 'dry-run?))
1116 (bootloader? (assoc-ref opts 'install-bootloader?))
1117 (target-file (match args
1118 ((first second) second)
1122 (bootloader-configuration-target
1123 (operating-system-bootloader os)))))
1126 (set-build-options-from-command-line store opts)
1128 (with-build-handler (build-notifier #:use-substitutes?
1129 (assoc-ref opts 'substitutes?)
1131 (assoc-ref opts 'dry-run?))
1132 (run-with-store store
1133 (mbegin %store-monad
1134 (set-guile-for-build (default-guile))
1137 (export-extension-graph os (current-output-port)))
1139 (export-shepherd-graph os (current-output-port)))
1141 (unless (memq action '(build init))
1142 (warn-about-old-distro #:suggested-command
1143 "guix system reconfigure"))
1145 (perform-action action os
1147 #:derivations-only? (assoc-ref opts
1149 #:use-substitutes? (assoc-ref opts 'substitutes?)
1150 #:skip-safety-checks?
1151 (assoc-ref opts 'skip-safety-checks?)
1152 #:file-system-type (assoc-ref opts 'file-system-type)
1153 #:image-size (assoc-ref opts 'image-size)
1154 #:full-boot? (assoc-ref opts 'full-boot?)
1155 #:container-shared-network?
1156 (assoc-ref opts 'container-shared-network?)
1157 #:mappings (filter-map (match-lambda
1158 (('file-system-mapping . m)
1162 #:install-bootloader? bootloader?
1163 #:target target-file
1164 #:bootloader-target bootloader-target
1165 #:gc-root (assoc-ref opts 'gc-root)))))
1168 (warn-about-disk-space)))
1170 (define (resolve-subcommand name)
1171 (let ((module (resolve-interface
1172 `(guix scripts system ,(string->symbol name))))
1173 (proc (string->symbol (string-append "guix-system-" name))))
1174 (module-ref module proc)))
1176 (define (process-command command args opts)
1177 "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
1178 argument list and OPTS is the option alist."
1179 (define-syntax-rule (with-store* store exp ...)
1181 (set-build-options-from-command-line store opts)
1185 ;; The following commands do not need to use the store, and they do not need
1186 ;; an operating system configuration file.
1188 (let ((pattern (match args
1191 (x (leave (G_ "wrong number of arguments~%"))))))
1192 (list-generations pattern)))
1194 (match (generation-number %system-profile)
1196 (error (G_ "no system generation, nothing to describe~%")))
1198 (display-system-generation generation))))
1200 (apply (resolve-subcommand "search") args))
1201 ;; The following commands need to use the store, but they do not need an
1202 ;; operating system configuration file.
1203 ((delete-generations)
1204 (let ((pattern (match args
1207 (x (leave (G_ "wrong number of arguments~%"))))))
1209 (delete-matching-generations store %system-profile pattern)
1210 (reinstall-bootloader store (generation-number %system-profile)))))
1211 ((switch-generation)
1212 (let ((pattern (match args
1214 (x (leave (G_ "wrong number of arguments~%"))))))
1216 (switch-to-system-generation store pattern))))
1218 (let ((pattern (match args
1220 (x (leave (G_ "wrong number of arguments~%"))))))
1222 (roll-back-system store))))
1223 ;; The following commands need to use the store, and they also
1224 ;; need an operating system configuration file.
1225 (else (process-action command args opts))))
1227 (define (guix-system . args)
1228 (define (parse-sub-command arg result)
1229 ;; Parse sub-command ARG and augment RESULT accordingly.
1230 (if (assoc-ref result 'action)
1231 (alist-cons 'argument arg result)
1232 (let ((action (string->symbol arg)))
1234 ((build container vm vm-image disk-image reconfigure init
1235 extension-graph shepherd-graph
1236 list-generations describe
1237 delete-generations roll-back
1238 switch-generation search docker-image)
1239 (alist-cons 'action action result))
1240 (else (leave (G_ "~a: unknown action~%") action))))))
1242 (define (match-pair car)
1243 ;; Return a procedure that matches a pair with CAR.
1246 (and (eq? car head) tail))
1249 (define (option-arguments opts)
1250 ;; Extract the plain arguments from OPTS.
1251 (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
1252 (count (length args))
1253 (action (assoc-ref opts 'action))
1254 (expr (assoc-ref opts 'expression)))
1256 (leave (G_ "wrong number of arguments for action '~a'~%")
1260 (format (current-error-port)
1261 (G_ "guix system: missing command name~%"))
1262 (format (current-error-port)
1263 (G_ "Try 'guix system --help' for more information.~%"))
1267 ((build container vm vm-image disk-image docker-image reconfigure)
1268 (unless (or (= count 1)
1269 (and expr (= count 0)))
1276 (with-error-handling
1277 (let* ((opts (parse-command-line args %options
1278 (list %default-options)
1281 (args (option-arguments opts))
1282 (command (assoc-ref opts 'action)))
1283 (parameterize ((%graft? (assoc-ref opts 'graft?)))
1284 (with-status-verbosity (or (assoc-ref opts 'verbosity)
1285 (if (eq? command 'build) 2 1))
1286 (process-command command args opts))))))
1288 ;;; Local Variables:
1289 ;;; eval: (put 'with-store* 'scheme-indent-function 1)
1292 ;;; system.scm ends here