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