system: Allow separated /boot and encrypted root.
[jackhill/guix/guix.git] / guix / scripts / system.scm
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>
9 ;;; Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
10 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
11 ;;;
12 ;;; This file is part of GNU Guix.
13 ;;;
14 ;;; GNU Guix is free software; you can redistribute it and/or modify it
15 ;;; under the terms of the GNU General Public License as published by
16 ;;; the Free Software Foundation; either version 3 of the License, or (at
17 ;;; your option) any later version.
18 ;;;
19 ;;; GNU Guix is distributed in the hope that it will be useful, but
20 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;;; GNU General Public License for more details.
23 ;;;
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
26
27 (define-module (guix scripts system)
28 #:use-module (guix config)
29 #:use-module (guix ui)
30 #:use-module ((guix status) #:select (with-status-verbosity))
31 #:use-module (guix store)
32 #:autoload (guix base16) (bytevector->base16-string)
33 #:autoload (guix store database)
34 (sqlite-register store-database-file call-with-database)
35 #:autoload (guix build store-copy) (copy-store-item)
36 #:use-module (guix describe)
37 #:use-module (guix grafts)
38 #:use-module (guix gexp)
39 #:use-module (guix derivations)
40 #:use-module (guix packages)
41 #:use-module (guix utils)
42 #:use-module (guix monads)
43 #:use-module (guix records)
44 #:use-module (guix profiles)
45 #:use-module (guix scripts)
46 #:use-module (guix channels)
47 #:use-module (guix scripts build)
48 #:autoload (guix scripts package) (delete-generations
49 delete-matching-generations)
50 #:autoload (guix scripts pull) (channel-commit-hyperlink)
51 #:use-module (guix graph)
52 #:use-module (guix scripts graph)
53 #:use-module (guix scripts system reconfigure)
54 #:use-module (guix build utils)
55 #:use-module (guix progress)
56 #:use-module ((guix build syscalls) #:select (terminal-columns))
57 #:use-module (gnu build install)
58 #:autoload (gnu build file-systems)
59 (find-partition-by-label find-partition-by-uuid)
60 #:autoload (gnu build linux-modules)
61 (device-module-aliases matching-modules)
62 #:use-module (gnu system linux-initrd)
63 #:use-module (gnu image)
64 #:use-module (gnu system)
65 #:use-module (gnu bootloader)
66 #:use-module (gnu system file-systems)
67 #:use-module (gnu system image)
68 #:use-module (gnu system mapped-devices)
69 #:use-module (gnu system linux-container)
70 #:use-module (gnu system uuid)
71 #:use-module (gnu system vm)
72 #:use-module (gnu services)
73 #:use-module (gnu services shepherd)
74 #:use-module (gnu services herd)
75 #:use-module (srfi srfi-1)
76 #:use-module (srfi srfi-11)
77 #:use-module (srfi srfi-19)
78 #:use-module (srfi srfi-26)
79 #:use-module (srfi srfi-34)
80 #:use-module (srfi srfi-35)
81 #:use-module (srfi srfi-37)
82 #:use-module (ice-9 format)
83 #:use-module (ice-9 match)
84 #:use-module (rnrs bytevectors)
85 #:export (guix-system
86 read-operating-system))
87
88 \f
89 ;;;
90 ;;; Operating system declaration.
91 ;;;
92
93 (define %user-module
94 ;; Module in which the machine description file is loaded.
95 (make-user-module '((gnu system)
96 (gnu services)
97 (gnu system shadow))))
98
99 (define (read-operating-system file)
100 "Read the operating-system declaration from FILE and return it."
101 (load* file %user-module))
102
103 \f
104 ;;;
105 ;;; Installation.
106 ;;;
107
108 (define-syntax-rule (save-load-path-excursion body ...)
109 "Save the current values of '%load-path' and '%load-compiled-path', run
110 BODY..., and restore them."
111 (let ((path %load-path)
112 (cpath %load-compiled-path))
113 (dynamic-wind
114 (const #t)
115 (lambda ()
116 body ...)
117 (lambda ()
118 (set! %load-path path)
119 (set! %load-compiled-path cpath)))))
120
121 (define-syntax-rule (save-environment-excursion body ...)
122 "Save the current environment variables, run BODY..., and restore them."
123 (let ((env (environ)))
124 (dynamic-wind
125 (const #t)
126 (lambda ()
127 body ...)
128 (lambda ()
129 (environ env)))))
130
131 (define topologically-sorted*
132 (store-lift topologically-sorted))
133
134
135 (define* (copy-item item info target db
136 #:key (log-port (current-error-port)))
137 "Copy ITEM to the store under root directory TARGET and populate DB with the
138 given INFO, a <path-info> record."
139 (let ((dest (string-append target item)))
140 (format log-port "copying '~a'...~%" item)
141
142 ;; Remove DEST if it exists to make sure that (1) we do not fail badly
143 ;; while trying to overwrite it (see <http://bugs.gnu.org/20722>), and
144 ;; (2) we end up with the right contents.
145 (when (false-if-exception (lstat dest))
146 (for-each make-file-writable
147 (find-files dest (lambda (file stat)
148 (eq? 'directory (stat:type stat)))
149 #:directories? #t))
150 (delete-file-recursively dest))
151
152 (copy-store-item item target
153 #:deduplicate? #t)
154
155 (sqlite-register db
156 #:path item
157 #:references (path-info-references info)
158 #:deriver (path-info-deriver info)
159 #:hash (string-append
160 "sha256:"
161 (bytevector->base16-string (path-info-hash info)))
162 #:nar-size (path-info-nar-size info))))
163
164 (define* (copy-closure item target
165 #:key (log-port (current-error-port)))
166 "Copy ITEM and all its dependencies to the store under root directory
167 TARGET, and register them."
168 (mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
169 (info (mapm %store-monad query-path-info* to-copy))
170 (size -> (reduce + 0 (map path-info-nar-size info))))
171 (define progress-bar
172 (progress-reporter/bar (length to-copy)
173 (format #f (G_ "copying to '~a'...")
174 target)))
175
176 (define state
177 (string-append target "/var/guix"))
178
179 (check-available-space size target)
180
181 ;; Explicitly use "TARGET/var/guix" as the state directory to avoid
182 ;; reproducing the user's current settings; see
183 ;; <http://bugs.gnu.org/18049>.
184 (call-with-database (store-database-file #:prefix target
185 #:state-directory state)
186 (lambda (db)
187 (call-with-progress-reporter progress-bar
188 (lambda (report)
189 (let ((void (%make-void-port "w")))
190 (for-each (lambda (item info)
191 (copy-item item info target db #:log-port void)
192 (report))
193 to-copy info))))))
194
195 (return *unspecified*)))
196
197 (define* (install os-drv target
198 #:key (log-port (current-output-port))
199 install-bootloader? bootloader bootcfg)
200 "Copy the closure of BOOTCFG, which includes the output of OS-DRV, to
201 directory TARGET. TARGET must be an absolute directory name since that's what
202 'register-path' expects.
203
204 When INSTALL-BOOTLOADER? is true, install bootloader using BOOTCFG."
205 (define (maybe-copy to-copy)
206 (with-monad %store-monad
207 (if (string=? target "/")
208 (begin
209 (warning (G_ "initializing the current root file system~%"))
210 (return #t))
211 (begin
212 ;; Make sure the target store exists.
213 (mkdir-p (string-append target (%store-prefix)))
214
215 ;; Copy items to the new store.
216 (copy-closure to-copy target #:log-port log-port)))))
217
218 ;; Make sure TARGET is root-owned when running as root, but still allow
219 ;; non-root uses (useful for testing.) See
220 ;; <http://lists.gnu.org/archive/html/guix-devel/2015-05/msg00452.html>.
221 (if (zero? (geteuid))
222 (chown target 0 0)
223 (warning (G_ "not running as 'root', so \
224 the ownership of '~a' may be incorrect!~%")
225 target))
226
227 ;; If a previous installation was attempted, make sure we start anew; in
228 ;; particular, we don't want to keep a store database that might not
229 ;; correspond to what we're actually putting in the store.
230 (let ((state (string-append target "/var/guix")))
231 (when (file-exists? state)
232 (delete-file-recursively state)))
233
234 (chmod target #o755)
235 (let ((os-dir (derivation->output-path os-drv))
236 (format (lift format %store-monad))
237 (populate (lift2 populate-root-file-system %store-monad)))
238
239 (mlet %store-monad ((bootcfg (lower-object bootcfg)))
240 (mbegin %store-monad
241 ;; Copy the closure of BOOTCFG, which includes OS-DIR,
242 ;; eventual background image and so on.
243 (maybe-copy (derivation->output-path bootcfg))
244
245 ;; Create a bunch of additional files.
246 (format log-port "populating '~a'...~%" target)
247 (populate os-dir target)
248
249 (mwhen install-bootloader?
250 (install-bootloader local-eval bootloader bootcfg
251 #:target target)
252 (return
253 (info (G_ "bootloader successfully installed on '~a'~%")
254 (bootloader-configuration-target bootloader))))))))
255
256 \f
257 ;;;
258 ;;; Reconfiguration.
259 ;;;
260
261 (define %system-profile
262 ;; The system profile.
263 (string-append %state-directory "/profiles/system"))
264
265 (define-syntax-rule (with-shepherd-error-handling mbody ...)
266 "Catch and report Shepherd errors that arise when binding MBODY, a monadic
267 expression in %STORE-MONAD."
268 (lambda (store)
269 (catch 'system-error
270 (lambda ()
271 (guard (c ((shepherd-error? c)
272 (values (report-shepherd-error c) store)))
273 (values (run-with-store store (mbegin %store-monad mbody ...))
274 store)))
275 (lambda (key proc format-string format-args errno . rest)
276 (warning (G_ "while talking to shepherd: ~a~%")
277 (apply format #f format-string format-args))
278 (values #f store)))))
279
280 (define (report-shepherd-error error)
281 "Report ERROR, a '&shepherd-error' error condition object."
282 (when error
283 (cond ((service-not-found-error? error)
284 (warning (G_ "service '~a' could not be found~%")
285 (service-not-found-error-service error)))
286 ((action-not-found-error? error)
287 (warning (G_ "service '~a' does not have an action '~a'~%")
288 (action-not-found-error-service error)
289 (action-not-found-error-action error)))
290 ((action-exception-error? error)
291 (warning (G_ "exception caught while executing '~a' \
292 on service '~a':~%")
293 (action-exception-error-action error)
294 (action-exception-error-service error))
295 (print-exception (current-error-port) #f
296 (action-exception-error-key error)
297 (action-exception-error-arguments error)))
298 ((unknown-shepherd-error? error)
299 (warning (G_ "something went wrong: ~s~%")
300 (unknown-shepherd-error-sexp error)))
301 ((shepherd-error? error)
302 (warning (G_ "shepherd error~%"))))
303
304 ;; Don't leave users out in the cold and explain what that means and what
305 ;; they can do.
306 (warning (G_ "some services could not be upgraded~%"))
307 (display-hint (G_ "To allow changes to all the system services to take
308 effect, you will need to reboot."))))
309
310 (define-syntax-rule (unless-file-not-found exp)
311 (catch 'system-error
312 (lambda ()
313 exp)
314 (lambda args
315 (if (= ENOENT (system-error-errno args))
316 #f
317 (apply throw args)))))
318
319 (define (seconds->string seconds)
320 "Return a string representing the date for SECONDS."
321 (let ((time (make-time time-utc 0 seconds)))
322 (date->string (time-utc->date time)
323 "~Y-~m-~d ~H:~M")))
324
325 (define* (profile-boot-parameters #:optional (profile %system-profile)
326 (numbers
327 (reverse (generation-numbers profile))))
328 "Return a list of 'boot-parameters' for the generations of PROFILE specified
329 by NUMBERS, which is a list of generation numbers. The list is ordered from
330 the most recent to the oldest profiles."
331 (define (system->boot-parameters system number time)
332 (unless-file-not-found
333 (let* ((params (read-boot-parameters-file system))
334 (label (boot-parameters-label params)))
335 (boot-parameters
336 (inherit params)
337 (label (string-append label " (#"
338 (number->string number) ", "
339 (seconds->string time) ")"))))))
340 (let* ((systems (map (cut generation-file-name profile <>)
341 numbers))
342 (times (map (lambda (system)
343 (unless-file-not-found
344 (stat:mtime (lstat system))))
345 systems)))
346 (filter-map system->boot-parameters systems numbers times)))
347
348 \f
349 ;;;
350 ;;; Roll-back.
351 ;;;
352 (define (roll-back-system store)
353 "Roll back the system profile to its previous generation. STORE is an open
354 connection to the store."
355 (switch-to-system-generation store "-1"))
356
357 \f
358 ;;;
359 ;;; Switch generations.
360 ;;;
361 (define (switch-to-system-generation store spec)
362 "Switch the system profile to the generation specified by SPEC, and
363 re-install bootloader with a configuration file that uses the specified system
364 generation as its default entry. STORE is an open connection to the store."
365 (let ((number (relative-generation-spec->number %system-profile spec)))
366 (if number
367 (begin
368 (reinstall-bootloader store number)
369 (switch-to-generation* %system-profile number))
370 (leave (G_ "cannot switch to system generation '~a'~%") spec))))
371
372 (define* (system-bootloader-name #:optional (system %system-profile))
373 "Return the bootloader name stored in SYSTEM's \"parameters\" file."
374 (let ((params (unless-file-not-found
375 (read-boot-parameters-file system))))
376 (boot-parameters-bootloader-name params)))
377
378 (define (reinstall-bootloader store number)
379 "Re-install bootloader for existing system profile generation NUMBER.
380 STORE is an open connection to the store."
381 (let* ((generation (generation-file-name %system-profile number))
382 ;; Detect the bootloader used in %system-profile.
383 (bootloader (lookup-bootloader-by-name (system-bootloader-name)))
384
385 ;; Use the detected bootloader with default configuration.
386 ;; It will be enough to allow the system to boot.
387 (bootloader-config (bootloader-configuration
388 (bootloader bootloader)))
389
390 ;; Make the specified system generation the default entry.
391 (params (first (profile-boot-parameters %system-profile
392 (list number))))
393 (locale (boot-parameters-locale params))
394 (store-crypto-devices (boot-parameters-store-crypto-devices params))
395 (store-directory-prefix
396 (boot-parameters-store-directory-prefix params))
397 (old-generations
398 (delv number (reverse (generation-numbers %system-profile))))
399 (old-params (profile-boot-parameters
400 %system-profile old-generations))
401 (entries (cons (boot-parameters->menu-entry params)
402 (boot-parameters-bootloader-menu-entries params)))
403 (old-entries (map boot-parameters->menu-entry old-params)))
404 (run-with-store store
405 (mlet* %store-monad
406 ((bootcfg (lower-object
407 ((bootloader-configuration-file-generator bootloader)
408 bootloader-config entries
409 #:locale locale
410 #:store-crypto-devices store-crypto-devices
411 #:store-directory-prefix store-directory-prefix
412 #:old-entries old-entries)))
413 (drvs -> (list bootcfg)))
414 (mbegin %store-monad
415 (built-derivations drvs)
416 ;; Only install bootloader configuration file.
417 (install-bootloader local-eval bootloader-config bootcfg
418 #:run-installer? #f))))))
419
420 \f
421 ;;;
422 ;;; Graphs.
423 ;;;
424
425 (define (service-node-label service)
426 "Return a label to represent SERVICE."
427 (let ((type (service-kind service))
428 (value (service-value service)))
429 (string-append (symbol->string (service-type-name type))
430 (cond ((or (number? value) (symbol? value))
431 (string-append " " (object->string value)))
432 ((string? value)
433 (string-append " " value))
434 ((file-system? value)
435 (string-append " " (file-system-mount-point value)))
436 (else
437 "")))))
438
439 (define (service-node-type services)
440 "Return a node type for SERVICES. Since <service> instances are not
441 self-contained (they express dependencies on service types, not on services),
442 we have to create the 'edges' procedure dynamically as a function of the full
443 list of services."
444 (node-type
445 (name "service")
446 (description "the DAG of services")
447 (identifier (lift1 object-address %store-monad))
448 (label service-node-label)
449 (edges (lift1 (service-back-edges services) %store-monad))))
450
451 (define (shepherd-service-node-label service)
452 "Return a label for a node representing a <shepherd-service>."
453 (string-join (map symbol->string (shepherd-service-provision service))))
454
455 (define (shepherd-service-node-type services)
456 "Return a node type for SERVICES, a list of <shepherd-service>."
457 (node-type
458 (name "shepherd-service")
459 (description "the dependency graph of shepherd services")
460 (identifier (lift1 shepherd-service-node-label %store-monad))
461 (label shepherd-service-node-label)
462 (edges (lift1 (shepherd-service-back-edges services) %store-monad))))
463
464 \f
465 ;;;
466 ;;; Generations.
467 ;;;
468
469 (define* (display-system-generation number
470 #:optional (profile %system-profile))
471 "Display a summary of system generation NUMBER in a human-readable format."
472 (define (display-channel channel)
473 (format #t " ~a:~%" (channel-name channel))
474 (format #t (G_ " repository URL: ~a~%") (channel-url channel))
475 (when (channel-branch channel)
476 (format #t (G_ " branch: ~a~%") (channel-branch channel)))
477 (format #t (G_ " commit: ~a~%")
478 (if (supports-hyperlinks?)
479 (channel-commit-hyperlink channel)
480 (channel-commit channel))))
481
482 (unless (zero? number)
483 (let* ((generation (generation-file-name profile number))
484 (params (read-boot-parameters-file generation))
485 (label (boot-parameters-label params))
486 (bootloader-name (boot-parameters-bootloader-name params))
487 (root (boot-parameters-root-device params))
488 (root-device (if (bytevector? root)
489 (uuid->string root)
490 root))
491 (kernel (boot-parameters-kernel params))
492 (multiboot-modules (boot-parameters-multiboot-modules params)))
493 (define-values (channels config-file)
494 (system-provenance generation))
495
496 (display-generation profile number)
497 (format #t (G_ " file name: ~a~%") generation)
498 (format #t (G_ " canonical file name: ~a~%") (readlink* generation))
499 ;; TRANSLATORS: Please preserve the two-space indentation.
500 (format #t (G_ " label: ~a~%") label)
501 (format #t (G_ " bootloader: ~a~%") bootloader-name)
502
503 ;; TRANSLATORS: The '~[', '~;', and '~]' sequences in this string must
504 ;; be preserved. They denote conditionals, such that the result will
505 ;; look like:
506 ;; root device: UUID: 12345-678
507 ;; or:
508 ;; root device: label: "my-root"
509 ;; or just:
510 ;; root device: /dev/sda3
511 (format #t (G_ " root device: ~[UUID: ~a~;label: ~s~;~a~]~%")
512 (cond ((uuid? root-device) 0)
513 ((file-system-label? root-device) 1)
514 (else 2))
515 (file-system-device->string root-device))
516
517 (format #t (G_ " kernel: ~a~%") kernel)
518
519 (match multiboot-modules
520 (() #f)
521 (((modules . _) ...)
522 (format #t (G_ " multiboot: ~a~%")
523 (string-join modules "\n "))))
524
525 (unless (null? channels)
526 ;; TRANSLATORS: Here "channel" is the same terminology as used in
527 ;; "guix describe" and "guix pull --channels".
528 (format #t (G_ " channels:~%"))
529 (for-each display-channel channels))
530 (when config-file
531 (format #t (G_ " configuration file: ~a~%")
532 (if (supports-hyperlinks?)
533 (file-hyperlink config-file)
534 config-file))))))
535
536 (define* (list-generations pattern #:optional (profile %system-profile))
537 "Display in a human-readable format all the system generations matching
538 PATTERN, a string. When PATTERN is #f, display all the system generations."
539 (cond ((not (file-exists? profile)) ; XXX: race condition
540 (raise (condition (&profile-not-found-error
541 (profile profile)))))
542 ((not pattern)
543 (for-each display-system-generation (profile-generations profile)))
544 ((matching-generations pattern profile)
545 =>
546 (lambda (numbers)
547 (if (null-list? numbers)
548 (exit 1)
549 (leave-on-EPIPE
550 (for-each display-system-generation numbers)))))))
551
552 \f
553 ;;;
554 ;;; File system declaration checks.
555 ;;;
556
557 (define (check-file-system-availability file-systems)
558 "Check whether the UUIDs or partition labels that FILE-SYSTEMS refer to, if
559 any, are available. Raise an error if they're not."
560 (define relevant
561 (filter (lambda (fs)
562 (and (file-system-mount? fs)
563 (not (member (file-system-type fs)
564 %pseudo-file-system-types))
565 ;; Don't try to validate network file systems.
566 (not (string-prefix? "nfs" (file-system-type fs)))
567 (not (memq 'bind-mount (file-system-flags fs)))))
568 file-systems))
569
570 (define labeled
571 (filter (lambda (fs)
572 (file-system-label? (file-system-device fs)))
573 relevant))
574
575 (define literal
576 (filter (lambda (fs)
577 (string? (file-system-device fs)))
578 relevant))
579
580 (define uuid
581 (filter (lambda (fs)
582 (uuid? (file-system-device fs)))
583 relevant))
584
585 (define fail? #f)
586
587 (define (file-system-location* fs)
588 (and=> (file-system-location fs)
589 source-properties->location))
590
591 (let-syntax ((error (syntax-rules ()
592 ((_ args ...)
593 (begin
594 (set! fail? #t)
595 (report-error args ...))))))
596 (for-each (lambda (fs)
597 (catch 'system-error
598 (lambda ()
599 (stat (file-system-device fs)))
600 (lambda args
601 (let ((errno (system-error-errno args))
602 (device (file-system-device fs)))
603 (error (file-system-location* fs)
604 (G_ "device '~a' not found: ~a~%")
605 device (strerror errno))
606 (unless (string-prefix? "/" device)
607 (display-hint (format #f (G_ "If '~a' is a file system
608 label, write @code{(file-system-label ~s)} in your @code{device} field.")
609 device device)))))))
610 literal)
611 (for-each (lambda (fs)
612 (let ((label (file-system-label->string
613 (file-system-device fs))))
614 (unless (find-partition-by-label label)
615 (error (file-system-location* fs)
616 (G_ "file system with label '~a' not found~%")
617 label))))
618 labeled)
619 (for-each (lambda (fs)
620 (unless (find-partition-by-uuid (file-system-device fs))
621 (error (file-system-location* fs)
622 (G_ "file system with UUID '~a' not found~%")
623 (uuid->string (file-system-device fs)))))
624 uuid)
625
626 (when fail?
627 ;; Better be safe than sorry.
628 (exit 1))))
629
630 (define (check-mapped-devices os)
631 "Check that each of MAPPED-DEVICES is valid according to the 'check'
632 procedure of its type."
633 (define boot-mapped-devices
634 (operating-system-boot-mapped-devices os))
635
636 (define (needed-for-boot? md)
637 (memq md boot-mapped-devices))
638
639 (define initrd-modules
640 (operating-system-initrd-modules os))
641
642 (for-each (lambda (md)
643 (let ((check (mapped-device-kind-check
644 (mapped-device-type md))))
645 ;; We expect CHECK to raise an exception with a detailed
646 ;; '&message' if something goes wrong.
647 (check md
648 #:needed-for-boot? (needed-for-boot? md)
649 #:initrd-modules initrd-modules)))
650 (operating-system-mapped-devices os)))
651
652 (define (check-initrd-modules os)
653 "Check that modules needed by 'needed-for-boot' file systems in OS are
654 available in the initrd. Note that mapped devices are responsible for
655 checking this by themselves in their 'check' procedure."
656 (define (file-system-/dev fs)
657 (let ((device (file-system-device fs)))
658 (match device
659 ((? string?)
660 device)
661 ((? uuid?)
662 (find-partition-by-uuid device))
663 ((? file-system-label?)
664 (find-partition-by-label (file-system-label->string device))))))
665
666 (define file-systems
667 (filter file-system-needed-for-boot?
668 (operating-system-file-systems os)))
669
670 (for-each (lambda (fs)
671 (check-device-initrd-modules (file-system-/dev fs)
672 (operating-system-initrd-modules os)
673 (source-properties->location
674 (file-system-location fs))))
675 file-systems))
676
677 \f
678 ;;;
679 ;;; Action.
680 ;;;
681
682 (define* (system-derivation-for-action os action
683 #:key image-size image-type
684 full-boot? container-shared-network?
685 mappings label
686 volatile-root?)
687 "Return as a monadic value the derivation for OS according to ACTION."
688 (mlet %store-monad ((target (current-target-system)))
689 (case action
690 ((build init reconfigure)
691 (operating-system-derivation os))
692 ((container)
693 (container-script
694 os
695 #:mappings mappings
696 #:shared-network? container-shared-network?))
697 ((vm-image)
698 (system-qemu-image os #:disk-image-size image-size))
699 ((vm)
700 (system-qemu-image/shared-store-script os
701 #:full-boot? full-boot?
702 #:disk-image-size
703 (if full-boot?
704 image-size
705 (* 70 (expt 2 20)))
706 #:mappings mappings))
707 ((disk-image)
708 (let* ((base-image (os->image os #:type image-type))
709 (base-target (image-target base-image)))
710 (lower-object
711 (system-image
712 (image
713 (inherit (if label
714 (image-with-label base-image label)
715 base-image))
716 (target (or base-target target))
717 (size image-size)
718 (operating-system os)
719 (volatile-root? volatile-root?))))))
720 ((docker-image)
721 (system-docker-image os
722 #:shared-network? container-shared-network?)))))
723
724 (define (maybe-suggest-running-guix-pull)
725 "Suggest running 'guix pull' if this has never been done before."
726 ;; Check whether we're running a 'guix pull'-provided 'guix' command. When
727 ;; 'current-profile' returns #f, we may be running the globally-installed
728 ;; 'guix' and thus run the risk of deploying an older 'guix'. See
729 ;; <https://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html>
730 (unless (or (current-profile) (getenv "GUIX_UNINSTALLED"))
731 (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
732 (warning (G_ "Failing to do that may downgrade your system!~%"))))
733
734 (define (bootloader-installer-script installer
735 bootloader device target)
736 "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
737 and TARGET arguments."
738 (scheme-file "bootloader-installer"
739 (with-imported-modules '((gnu build bootloader)
740 (guix build utils))
741 #~(begin
742 (use-modules (gnu build bootloader)
743 (guix build utils)
744 (ice-9 binary-ports)
745 (srfi srfi-34)
746 (srfi srfi-35))
747
748 (guard (c ((message-condition? c) ;XXX: i18n
749 (format (current-error-port) "error: ~a~%"
750 (condition-message c))
751 (exit 1)))
752 (#$installer #$bootloader #$device #$target)
753 (info (G_ "bootloader successfully installed on '~a'~%")
754 #$device))))))
755
756 (define (local-eval exp)
757 "Evaluate EXP, a G-Expression, in-place."
758 (mlet* %store-monad ((lowered (lower-gexp exp))
759 (_ (built-derivations (lowered-gexp-inputs lowered))))
760 (save-load-path-excursion
761 (set! %load-path (lowered-gexp-load-path lowered))
762 (set! %load-compiled-path (lowered-gexp-load-compiled-path lowered))
763 (return (primitive-eval (lowered-gexp-sexp lowered))))))
764
765 (define* (perform-action action os
766 #:key
767 (validate-reconfigure ensure-forward-reconfigure)
768 save-provenance?
769 skip-safety-checks?
770 install-bootloader?
771 dry-run? derivations-only?
772 use-substitutes? bootloader-target target
773 image-size image-type
774 volatile-root?
775 full-boot? label container-shared-network?
776 (mappings '())
777 (gc-root #f))
778 "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
779 bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
780 target root directory; IMAGE-SIZE is the size of the image to be built, for
781 the 'vm-image' and 'disk-image' actions. IMAGE-TYPE is the type of image to
782 be built. When VOLATILE-ROOT? is #t, the root file system is mounted
783 volatile.
784
785 FULL-BOOT? is used for the 'vm' action; it determines whether to
786 boot directly to the kernel or to the bootloader. CONTAINER-SHARED-NETWORK?
787 determines if the container will use a separate network namespace.
788
789 When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
790 building anything.
791
792 When GC-ROOT is a path, also make that path an indirect root of the build
793 output when building a system derivation, such as a disk image.
794
795 When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
796 static checks."
797 (define println
798 (cut format #t "~a~%" <>))
799
800 (define menu-entries
801 (if (eq? 'init action)
802 '()
803 (map boot-parameters->menu-entry (profile-boot-parameters))))
804
805 (define bootloader
806 (operating-system-bootloader os))
807
808 (define bootcfg
809 (and (memq action '(init reconfigure))
810 (operating-system-bootcfg os menu-entries)))
811
812 (when (eq? action 'reconfigure)
813 (maybe-suggest-running-guix-pull)
814 (check-forward-update validate-reconfigure))
815
816 ;; Check whether the declared file systems exist. This is better than
817 ;; instantiating a broken configuration. Assume that we can only check if
818 ;; running as root.
819 (when (and (not skip-safety-checks?)
820 (memq action '(init reconfigure)))
821 (check-mapped-devices os)
822 (when (zero? (getuid))
823 (check-file-system-availability (operating-system-file-systems os))
824 (check-initrd-modules os)))
825
826 (mlet* %store-monad
827 ((sys (system-derivation-for-action os action
828 #:label label
829 #:image-type image-type
830 #:image-size image-size
831 #:volatile-root? volatile-root?
832 #:full-boot? full-boot?
833 #:container-shared-network? container-shared-network?
834 #:mappings mappings))
835
836 ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
837 ;; --no-bootloader is passed, because we then use it as a GC root.
838 ;; See <http://bugs.gnu.org/21068>.
839 (drvs (mapm/accumulate-builds lower-object
840 (if (memq action '(init reconfigure))
841 (list sys bootcfg)
842 (list sys))))
843 (% (if derivations-only?
844 (return (for-each (compose println derivation-file-name)
845 drvs))
846 (built-derivations drvs))))
847
848 (if (or dry-run? derivations-only?)
849 (return #f)
850 (begin
851 (for-each (compose println derivation->output-path)
852 drvs)
853
854 (case action
855 ((reconfigure)
856 (newline)
857 (format #t (G_ "activating system...~%"))
858 (mbegin %store-monad
859 (switch-to-system local-eval os)
860 (mwhen install-bootloader?
861 (install-bootloader local-eval bootloader bootcfg
862 #:target (or target "/"))
863 (return
864 (info (G_ "bootloader successfully installed on '~a'~%")
865 (bootloader-configuration-target bootloader))))
866 (with-shepherd-error-handling
867 (upgrade-shepherd-services local-eval os)
868 (return (format #t (G_ "\
869 To complete the upgrade, run 'herd restart SERVICE' to stop,
870 upgrade, and restart each service that was not automatically restarted.\n")))
871 (return (format #t (G_ "\
872 Run 'herd status' to view the list of services on your system.\n"))))))
873 ((init)
874 (newline)
875 (format #t (G_ "initializing operating system under '~a'...~%")
876 target)
877 (install sys (canonicalize-path target)
878 #:install-bootloader? install-bootloader?
879 #:bootloader bootloader
880 #:bootcfg bootcfg))
881 (else
882 ;; All we had to do was to build SYS and maybe register an
883 ;; indirect GC root.
884 (let ((output (derivation->output-path sys)))
885 (mbegin %store-monad
886 (mwhen gc-root
887 (register-root* (list output) gc-root))
888 (return output)))))))))
889
890 (define (export-extension-graph os port)
891 "Export the service extension graph of OS to PORT."
892 (let* ((services (operating-system-services os))
893 (system (find (lambda (service)
894 (eq? (service-kind service) system-service-type))
895 services)))
896 (export-graph (list system) (current-output-port)
897 #:node-type (service-node-type services)
898 #:reverse-edges? #t)))
899
900 (define (export-shepherd-graph os port)
901 "Export the graph of shepherd services of OS to PORT."
902 (let* ((services (operating-system-services os))
903 (pid1 (fold-services services
904 #:target-type shepherd-root-service-type))
905 (shepherds (service-value pid1)) ;list of <shepherd-service>
906 (sinks (filter (lambda (service)
907 (null? (shepherd-service-requirement service)))
908 shepherds)))
909 (export-graph sinks (current-output-port)
910 #:node-type (shepherd-service-node-type shepherds)
911 #:reverse-edges? #t)))
912
913 \f
914 ;;;
915 ;;; Images.
916 ;;;
917
918 (define (list-image-types)
919 "Print the available image types."
920 (display (G_ "The available image types are:\n"))
921 (newline)
922 (format #t "~{ - ~a ~%~}" (map image-type-name (force %image-types))))
923
924 \f
925 ;;;
926 ;;; Options.
927 ;;;
928
929 (define (show-help)
930 (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
931 Build the operating system declared in FILE according to ACTION.
932 Some ACTIONS support additional ARGS.\n"))
933 (newline)
934 (display (G_ "The valid values for ACTION are:\n"))
935 (newline)
936 (display (G_ "\
937 search search for existing service types\n"))
938 (display (G_ "\
939 reconfigure switch to a new operating system configuration\n"))
940 (display (G_ "\
941 roll-back switch to the previous operating system configuration\n"))
942 (display (G_ "\
943 describe describe the current system\n"))
944 (display (G_ "\
945 list-generations list the system generations\n"))
946 (display (G_ "\
947 switch-generation switch to an existing operating system configuration\n"))
948 (display (G_ "\
949 delete-generations delete old system generations\n"))
950 (display (G_ "\
951 build build the operating system without installing anything\n"))
952 (display (G_ "\
953 container build a container that shares the host's store\n"))
954 (display (G_ "\
955 vm build a virtual machine image that shares the host's store\n"))
956 (display (G_ "\
957 vm-image build a freestanding virtual machine image\n"))
958 (display (G_ "\
959 disk-image build a disk image, suitable for a USB stick\n"))
960 (display (G_ "\
961 docker-image build a Docker image\n"))
962 (display (G_ "\
963 init initialize a root file system to run GNU\n"))
964 (display (G_ "\
965 extension-graph emit the service extension graph in Dot format\n"))
966 (display (G_ "\
967 shepherd-graph emit the graph of shepherd services in Dot format\n"))
968
969 (show-build-options-help)
970 (display (G_ "
971 -d, --derivation return the derivation of the given system"))
972 (display (G_ "
973 -e, --expression=EXPR consider the operating-system EXPR evaluates to
974 instead of reading FILE, when applicable"))
975 (display (G_ "
976 --allow-downgrades for 'reconfigure', allow downgrades to earlier
977 channel revisions"))
978 (display (G_ "
979 --on-error=STRATEGY
980 apply STRATEGY (one of nothing-special, backtrace,
981 or debug) when an error occurs while reading FILE"))
982 (display (G_ "
983 --list-image-types list available image types"))
984 (display (G_ "
985 -t, --image-type=TYPE for 'disk-image', produce an image of TYPE"))
986 (display (G_ "
987 --image-size=SIZE for 'vm-image', produce an image of SIZE"))
988 (display (G_ "
989 --no-bootloader for 'init', do not install a bootloader"))
990 (display (G_ "
991 --volatile for 'disk-image', make the root file system volatile"))
992 (display (G_ "
993 --label=LABEL for 'disk-image', label disk image with LABEL"))
994 (display (G_ "
995 --save-provenance save provenance information"))
996 (display (G_ "
997 --share=SPEC for 'vm' and 'container', share host file system with
998 read/write access according to SPEC"))
999 (display (G_ "
1000 --expose=SPEC for 'vm' and 'container', expose host file system
1001 directory as read-only according to SPEC"))
1002 (display (G_ "
1003 -N, --network for 'container', allow containers to access the network"))
1004 (display (G_ "
1005 -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container',
1006 and 'build', make FILE a symlink to the result, and
1007 register it as a garbage collector root"))
1008 (display (G_ "
1009 --full-boot for 'vm', make a full boot sequence"))
1010 (display (G_ "
1011 --skip-checks skip file system and initrd module safety checks"))
1012 (display (G_ "
1013 --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
1014 (display (G_ "
1015 -v, --verbosity=LEVEL use the given verbosity LEVEL"))
1016 (newline)
1017 (display (G_ "
1018 -h, --help display this help and exit"))
1019 (display (G_ "
1020 -V, --version display version information and exit"))
1021 (newline)
1022 (show-bug-report-information))
1023
1024 (define %options
1025 ;; Specifications of the command-line options.
1026 (cons* (option '(#\h "help") #f #f
1027 (lambda args
1028 (show-help)
1029 (exit 0)))
1030 (option '(#\V "version") #f #f
1031 (lambda args
1032 (show-version-and-exit "guix system")))
1033 (option '(#\e "expression") #t #f
1034 (lambda (opt name arg result)
1035 (alist-cons 'expression arg result)))
1036 (option '(#\d "derivation") #f #f
1037 (lambda (opt name arg result)
1038 (alist-cons 'derivations-only? #t result)))
1039 (option '("allow-downgrades") #f #f
1040 (lambda (opt name arg result)
1041 (alist-cons 'validate-reconfigure
1042 warn-about-backward-reconfigure
1043 result)))
1044 (option '("on-error") #t #f
1045 (lambda (opt name arg result)
1046 (alist-cons 'on-error (string->symbol arg)
1047 result)))
1048 (option '(#\t "image-type") #t #f
1049 (lambda (opt name arg result)
1050 (alist-cons 'image-type (string->symbol arg)
1051 result)))
1052 (option '("list-image-types") #f #f
1053 (lambda (opt name arg result)
1054 (list-image-types)
1055 (exit 0)))
1056 (option '("image-size") #t #f
1057 (lambda (opt name arg result)
1058 (alist-cons 'image-size (size->number arg)
1059 result)))
1060 (option '(#\N "network") #f #f
1061 (lambda (opt name arg result)
1062 (alist-cons 'container-shared-network? #t result)))
1063 (option '("no-bootloader" "no-grub") #f #f
1064 (lambda (opt name arg result)
1065 (alist-cons 'install-bootloader? #f result)))
1066 (option '("volatile") #f #f
1067 (lambda (opt name arg result)
1068 (alist-cons 'volatile-root? #t result)))
1069 (option '("label") #t #f
1070 (lambda (opt name arg result)
1071 (alist-cons 'label arg result)))
1072 (option '("full-boot") #f #f
1073 (lambda (opt name arg result)
1074 (alist-cons 'full-boot? #t result)))
1075 (option '("save-provenance") #f #f
1076 (lambda (opt name arg result)
1077 (alist-cons 'save-provenance? #t result)))
1078 (option '("skip-checks") #f #f
1079 (lambda (opt name arg result)
1080 (alist-cons 'skip-safety-checks? #t result)))
1081
1082 (option '("share") #t #f
1083 (lambda (opt name arg result)
1084 (alist-cons 'file-system-mapping
1085 (specification->file-system-mapping arg #t)
1086 result)))
1087 (option '("expose") #t #f
1088 (lambda (opt name arg result)
1089 (alist-cons 'file-system-mapping
1090 (specification->file-system-mapping arg #f)
1091 result)))
1092
1093 (option '(#\n "dry-run") #f #f
1094 (lambda (opt name arg result)
1095 (alist-cons 'dry-run? #t result)))
1096 (option '(#\v "verbosity") #t #f
1097 (lambda (opt name arg result)
1098 (let ((level (string->number* arg)))
1099 (alist-cons 'verbosity level
1100 (alist-delete 'verbosity result)))))
1101 (option '(#\s "system") #t #f
1102 (lambda (opt name arg result)
1103 (alist-cons 'system arg
1104 (alist-delete 'system result eq?))))
1105 (option '("target") #t #f
1106 (lambda (opt name arg result)
1107 (alist-cons 'target arg
1108 (alist-delete 'target result eq?))))
1109 (option '(#\r "root") #t #f
1110 (lambda (opt name arg result)
1111 (alist-cons 'gc-root arg result)))
1112 %standard-build-options))
1113
1114 (define %default-options
1115 ;; Alist of default option values.
1116 `((system . ,(%current-system))
1117 (target . #f)
1118 (substitutes? . #t)
1119 (offload? . #t)
1120 (print-build-trace? . #t)
1121 (print-extended-build-trace? . #t)
1122 (multiplexed-build-output? . #t)
1123 (graft? . #t)
1124 (debug . 0)
1125 (verbosity . #f) ;default
1126 (validate-reconfigure . ,ensure-forward-reconfigure)
1127 (image-type . raw)
1128 (image-size . guess)
1129 (install-bootloader? . #t)
1130 (label . #f)
1131 (volatile-root? . #f)))
1132
1133 (define (verbosity-level opts)
1134 "Return the verbosity level based on OPTS, the alist of parsed options."
1135 (or (assoc-ref opts 'verbosity)
1136 (if (eq? (assoc-ref opts 'action) 'build)
1137 2 1)))
1138
1139 \f
1140 ;;;
1141 ;;; Entry point.
1142 ;;;
1143
1144 (define (process-action action args opts)
1145 "Process ACTION, a sub-command, with the arguments are listed in ARGS.
1146 ACTION must be one of the sub-commands that takes an operating system
1147 declaration as an argument (a file name.) OPTS is the raw alist of options
1148 resulting from command-line parsing."
1149 (define (ensure-operating-system file-or-exp obj)
1150 (unless (operating-system? obj)
1151 (leave (G_ "'~a' does not return an operating system~%")
1152 file-or-exp))
1153 obj)
1154
1155 (define save-provenance?
1156 (or (assoc-ref opts 'save-provenance?)
1157 (memq action '(init reconfigure))))
1158
1159 (let* ((file (match args
1160 (() #f)
1161 ((x . _) x)))
1162 (expr (assoc-ref opts 'expression))
1163 (system (assoc-ref opts 'system))
1164 (target (assoc-ref opts 'target))
1165 (transform (if save-provenance?
1166 (cut operating-system-with-provenance <> file)
1167 identity))
1168 (os (transform
1169 (ensure-operating-system
1170 (or file expr)
1171 (cond
1172 ((and expr file)
1173 (leave
1174 (G_ "both file and expression cannot be specified~%")))
1175 (expr
1176 (read/eval expr))
1177 (file
1178 (load* file %user-module
1179 #:on-error (assoc-ref opts 'on-error)))
1180 (else
1181 (leave (G_ "no configuration specified~%")))))))
1182
1183 (dry? (assoc-ref opts 'dry-run?))
1184 (bootloader? (assoc-ref opts 'install-bootloader?))
1185 (label (assoc-ref opts 'label))
1186 (target-file (match args
1187 ((first second) second)
1188 (_ #f)))
1189 (bootloader-target
1190 (and bootloader?
1191 (bootloader-configuration-target
1192 (operating-system-bootloader os)))))
1193
1194 (with-store store
1195 (set-build-options-from-command-line store opts)
1196
1197 (with-build-handler (build-notifier #:use-substitutes?
1198 (assoc-ref opts 'substitutes?)
1199 #:verbosity
1200 (verbosity-level opts)
1201 #:dry-run?
1202 (assoc-ref opts 'dry-run?))
1203 (run-with-store store
1204 (mbegin %store-monad
1205 (set-guile-for-build (default-guile))
1206 (case action
1207 ((extension-graph)
1208 (export-extension-graph os (current-output-port)))
1209 ((shepherd-graph)
1210 (export-shepherd-graph os (current-output-port)))
1211 (else
1212 (unless (memq action '(build init))
1213 (warn-about-old-distro #:suggested-command
1214 "guix system reconfigure"))
1215
1216 (perform-action action os
1217 #:dry-run? dry?
1218 #:derivations-only? (assoc-ref opts
1219 'derivations-only?)
1220 #:use-substitutes? (assoc-ref opts 'substitutes?)
1221 #:skip-safety-checks?
1222 (assoc-ref opts 'skip-safety-checks?)
1223 #:validate-reconfigure
1224 (assoc-ref opts 'validate-reconfigure)
1225 #:image-type (lookup-image-type-by-name
1226 (assoc-ref opts 'image-type))
1227 #:image-size (assoc-ref opts 'image-size)
1228 #:volatile-root?
1229 (assoc-ref opts 'volatile-root?)
1230 #:full-boot? (assoc-ref opts 'full-boot?)
1231 #:container-shared-network?
1232 (assoc-ref opts 'container-shared-network?)
1233 #:mappings (filter-map (match-lambda
1234 (('file-system-mapping . m)
1235 m)
1236 (_ #f))
1237 opts)
1238 #:install-bootloader? bootloader?
1239 #:label label
1240 #:target target-file
1241 #:bootloader-target bootloader-target
1242 #:gc-root (assoc-ref opts 'gc-root)))))
1243 #:target target
1244 #:system system)))
1245 (warn-about-disk-space)))
1246
1247 (define (resolve-subcommand name)
1248 (let ((module (resolve-interface
1249 `(guix scripts system ,(string->symbol name))))
1250 (proc (string->symbol (string-append "guix-system-" name))))
1251 (module-ref module proc)))
1252
1253 (define (process-command command args opts)
1254 "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
1255 argument list and OPTS is the option alist."
1256 (define-syntax-rule (with-store* store exp ...)
1257 (with-store store
1258 (set-build-options-from-command-line store opts)
1259 exp ...))
1260
1261 (case command
1262 ;; The following commands do not need to use the store, and they do not need
1263 ;; an operating system configuration file.
1264 ((list-generations)
1265 (let ((pattern (match args
1266 (() #f)
1267 ((pattern) pattern)
1268 (x (leave (G_ "wrong number of arguments~%"))))))
1269 (list-generations pattern)))
1270 ((describe)
1271 (match (generation-number %system-profile)
1272 (0
1273 (error (G_ "no system generation, nothing to describe~%")))
1274 (generation
1275 (display-system-generation generation))))
1276 ((search)
1277 (apply (resolve-subcommand "search") args))
1278 ;; The following commands need to use the store, but they do not need an
1279 ;; operating system configuration file.
1280 ((delete-generations)
1281 (let ((pattern (match args
1282 (() #f)
1283 ((pattern) pattern)
1284 (x (leave (G_ "wrong number of arguments~%"))))))
1285 (with-store* store
1286 (delete-matching-generations store %system-profile pattern)
1287 (reinstall-bootloader store (generation-number %system-profile)))))
1288 ((switch-generation)
1289 (let ((pattern (match args
1290 ((pattern) pattern)
1291 (x (leave (G_ "wrong number of arguments~%"))))))
1292 (with-store* store
1293 (switch-to-system-generation store pattern))))
1294 ((roll-back)
1295 (let ((pattern (match args
1296 (() "")
1297 (x (leave (G_ "wrong number of arguments~%"))))))
1298 (with-store* store
1299 (roll-back-system store))))
1300 ;; The following commands need to use the store, and they also
1301 ;; need an operating system configuration file.
1302 (else (process-action command args opts))))
1303
1304 (define-command (guix-system . args)
1305 (synopsis "build and deploy full operating systems")
1306
1307 (define (parse-sub-command arg result)
1308 ;; Parse sub-command ARG and augment RESULT accordingly.
1309 (if (assoc-ref result 'action)
1310 (alist-cons 'argument arg result)
1311 (let ((action (string->symbol arg)))
1312 (case action
1313 ((build container vm vm-image disk-image reconfigure init
1314 extension-graph shepherd-graph
1315 list-generations describe
1316 delete-generations roll-back
1317 switch-generation search docker-image)
1318 (alist-cons 'action action result))
1319 (else (leave (G_ "~a: unknown action~%") action))))))
1320
1321 (define (match-pair car)
1322 ;; Return a procedure that matches a pair with CAR.
1323 (match-lambda
1324 ((head . tail)
1325 (and (eq? car head) tail))
1326 (_ #f)))
1327
1328 (define (option-arguments opts)
1329 ;; Extract the plain arguments from OPTS.
1330 (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
1331 (count (length args))
1332 (action (assoc-ref opts 'action))
1333 (expr (assoc-ref opts 'expression)))
1334 (define (fail)
1335 (leave (G_ "wrong number of arguments for action '~a'~%")
1336 action))
1337
1338 (unless action
1339 (format (current-error-port)
1340 (G_ "guix system: missing command name~%"))
1341 (format (current-error-port)
1342 (G_ "Try 'guix system --help' for more information.~%"))
1343 (exit 1))
1344
1345 (case action
1346 ((build container vm vm-image disk-image docker-image reconfigure)
1347 (unless (or (= count 1)
1348 (and expr (= count 0)))
1349 (fail)))
1350 ((init)
1351 (unless (= count 2)
1352 (fail))))
1353 args))
1354
1355 (with-error-handling
1356 (let* ((opts (parse-command-line args %options
1357 (list %default-options)
1358 #:argument-handler
1359 parse-sub-command))
1360 (args (option-arguments opts))
1361 (command (assoc-ref opts 'action)))
1362 (parameterize ((%graft? (assoc-ref opts 'graft?)))
1363 (with-status-verbosity (verbosity-level opts)
1364 (process-command command args opts))))))
1365
1366 ;;; Local Variables:
1367 ;;; eval: (put 'with-store* 'scheme-indent-function 1)
1368 ;;; End:
1369
1370 ;;; system.scm ends here