1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
5 ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
6 ;;; Copyright © 2014 Cyrill Schenkel <cyrill.schenkel@gmail.com>
7 ;;; Copyright © 2014, 2015, 2017 Alex Kost <alezost@gmail.com>
8 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
9 ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
10 ;;; Copyright © 2016 Roel Janssen <roel@gnu.org>
11 ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch>
13 ;;; This file is part of GNU Guix.
15 ;;; GNU Guix is free software; you can redistribute it and/or modify it
16 ;;; under the terms of the GNU General Public License as published by
17 ;;; the Free Software Foundation; either version 3 of the License, or (at
18 ;;; your option) any later version.
20 ;;; GNU Guix is distributed in the hope that it will be useful, but
21 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;;; GNU General Public License for more details.
25 ;;; You should have received a copy of the GNU General Public License
26 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
28 (define-module (guix ui)
29 #:use-module (guix gexp)
30 #:use-module (guix utils)
31 #:use-module (guix store)
32 #:use-module (guix config)
33 #:use-module (guix packages)
34 #:use-module (guix profiles)
35 #:use-module (guix derivations)
36 #:use-module (guix combinators)
37 #:use-module (guix build-system)
38 #:use-module (guix serialization)
39 #:use-module ((guix build utils) #:select (mkdir-p))
40 #:use-module ((guix licenses) #:select (license? license-name))
41 #:use-module ((guix build syscalls)
42 #:select (free-disk-space terminal-columns))
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-11)
45 #:use-module (srfi srfi-19)
46 #:use-module (srfi srfi-26)
47 #:use-module (srfi srfi-31)
48 #:use-module (srfi srfi-34)
49 #:use-module (srfi srfi-35)
50 #:autoload (ice-9 ftw) (scandir)
51 #:use-module (ice-9 match)
52 #:use-module (ice-9 format)
53 #:use-module (ice-9 regex)
54 #:autoload (system repl repl) (start-repl)
55 #:autoload (system repl debug) (make-debug stack->vector)
56 #:use-module (texinfo)
57 #:use-module (texinfo plain-text)
58 #:use-module (texinfo string-utils)
68 show-bug-report-information
72 show-derivation-outputs
75 show-manifest-transaction
76 call-with-error-handling
80 read/eval-package-expression
85 package-description-string
86 package-synopsis-string
89 package-specification->name+version+output
94 display-profile-content
95 display-profile-content-diff
109 ;;; User interface facilities for command-line tools.
113 (define %gettext-domain
114 ;; Text domain for strings used in the tools.
117 (define %package-text-domain
118 ;; Text domain for package synopses and descriptions.
121 (define G_ (cut gettext <> %gettext-domain))
122 (define N_ (cut ngettext <> <> <> %gettext-domain))
125 "Return the translation of the package description or synopsis MSGID."
126 ;; Descriptions/synopses might occasionally be empty strings, even if that
127 ;; is something we try to avoid. Since (gettext "") can return a non-empty
128 ;; string, explicitly check for that case.
129 (if (string-null? msgid)
131 (gettext msgid %package-text-domain)))
133 (define-syntax-rule (define-diagnostic name prefix)
134 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
138 (define (augmented-format-string fmt)
139 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
142 ((name (underscore fmt) args (... ...))
143 (and (string? (syntax->datum #'fmt))
144 (free-identifier=? #'underscore #'G_))
145 (with-syntax ((fmt* (augmented-format-string #'fmt))
146 (prefix (datum->syntax x prefix)))
147 #'(format (guix-warning-port) (gettext fmt*)
148 (program-name) (program-name) prefix
150 ((name (N-underscore singular plural n) args (... ...))
151 (and (string? (syntax->datum #'singular))
152 (string? (syntax->datum #'plural))
153 (free-identifier=? #'N-underscore #'N_))
154 (with-syntax ((s (augmented-format-string #'singular))
155 (p (augmented-format-string #'plural))
156 (prefix (datum->syntax x prefix)))
157 #'(format (guix-warning-port)
158 (ngettext s p n %gettext-domain)
159 (program-name) (program-name) prefix
160 args (... ...))))))))
162 (define-diagnostic warning "warning: ") ; emit a warning
163 (define-diagnostic info "")
165 (define-diagnostic report-error "error: ")
166 (define-syntax-rule (leave args ...)
167 "Emit an error message and exit."
169 (report-error args ...)
172 (define (make-user-module modules)
173 "Return a new user module with the additional MODULES loaded."
174 ;; Module in which the machine description file is loaded.
175 (let ((module (make-fresh-user-module)))
176 (for-each (lambda (iface)
177 (module-use! module (resolve-interface iface)))
181 (define* (load* file user-module
182 #:key (on-error 'nothing-special))
183 "Load the user provided Scheme source code FILE."
184 (define (frame-with-source frame)
185 ;; Walk from FRAME upwards until source location information is found.
186 (let loop ((frame frame)
190 (if (frame-source frame)
192 (loop (frame-previous frame) frame)))))
194 (define (error-string frame args)
195 (call-with-output-string
197 (apply display-error frame port (cdr args)))))
200 (make-prompt-tag "user-code"))
204 ;; XXX: Force a recompilation to avoid ABI issues.
205 (set! %fresh-auto-compile #t)
206 (set! %load-should-auto-compile #t)
208 (save-module-excursion
210 (set-current-module user-module)
212 ;; Hide the "auto-compiling" messages.
213 (parameterize ((current-warning-port (%make-void-port "w")))
214 (call-with-prompt tag
216 ;; Give 'load' an absolute file name so that it doesn't try to
217 ;; search for FILE in %LOAD-PATH. Note: use 'load', not
218 ;; 'primitive-load', so that FILE is compiled, which then allows us
219 ;; to provide better error reporting with source line numbers.
220 (load (canonicalize-path file)))
223 ;; XXX: Errors are reported from the pre-unwind handler below, but
224 ;; calling 'exit' from there has no effect, so we call it here.
226 (rec (handle-error . args)
227 ;; Capture the stack up to this procedure call, excluded, and pass
228 ;; the faulty stack frame to 'report-load-error'.
229 (let* ((stack (make-stack #t handle-error tag))
230 (depth (stack-length stack))
231 (last (and (> depth 0) (stack-ref stack 0)))
232 (frame (frame-with-source
234 (stack-ref stack 1) ;skip the 'throw' frame
237 (report-load-error file args frame)
242 (display (G_ "entering debugger; type ',bt' for a backtrace\n"))
243 (start-repl #:debug (make-debug (stack->vector stack) 0
244 (error-string frame args)
247 (newline (current-error-port))
248 (display-backtrace stack (current-error-port)))
252 (define* (report-load-error file args #:optional frame)
253 "Report the failure to load FILE, a user-provided Scheme file.
254 ARGS is the list of arguments received by the 'throw' handler."
256 (('system-error . rest)
257 (let ((err (system-error-errno args)))
258 (report-error (G_ "failed to load '~a': ~a~%") file (strerror err))))
259 (('syntax-error proc message properties form . rest)
260 (let ((loc (source-properties->location properties)))
261 (format (current-error-port) (G_ "~a: error: ~a~%")
262 (location->string loc) message)))
264 (if (message-condition? obj)
265 (report-error (G_ "~a~%")
266 (gettext (condition-message obj)
268 (report-error (G_ "exception thrown: ~s~%") obj)))
270 (report-error (G_ "failed to load '~a':~%") file)
271 (apply display-error frame (current-error-port) args))))
273 (define (warn-about-load-error file args) ;FIXME: factorize with ↑
274 "Report the failure to load FILE, a user-provided Scheme file, without
275 exiting. ARGS is the list of arguments received by the 'throw' handler."
277 (('system-error . rest)
278 (let ((err (system-error-errno args)))
279 (warning (G_ "failed to load '~a': ~a~%") file (strerror err))))
280 (('syntax-error proc message properties form . rest)
281 (let ((loc (source-properties->location properties)))
282 (format (current-error-port) (G_ "~a: warning: ~a~%")
283 (location->string loc) message)))
285 (if (message-condition? obj)
286 (warning (G_ "failed to load '~a': ~a~%")
288 (gettext (condition-message obj) %gettext-domain))
289 (warning (G_ "failed to load '~a': exception thrown: ~s~%")
292 (warning (G_ "failed to load '~a':~%") file)
293 (apply display-error #f (current-error-port) args))))
295 (define (install-locale)
296 "Install the current locale settings."
299 (setlocale LC_ALL ""))
301 (warning (G_ "failed to install locale: ~a~%")
302 (strerror (system-error-errno args))))))
304 (define (initialize-guix)
305 "Perform the usual initialization for stand-alone Guix commands."
306 ;; By default don't annoy users with deprecation warnings. In practice,
307 ;; 'define-deprecated' in (ice-9 deprecated) arranges so that those warnings
308 ;; are emitted at expansion-time only, but there are cases where they could
309 ;; slip through, for instance when interpreting code.
310 (unless (getenv "GUILE_WARN_DEPRECATED")
311 (debug-disable 'warn-deprecated))
314 (textdomain %gettext-domain)
316 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
317 ;; notified via an EPIPE later.
318 (sigaction SIGPIPE SIG_IGN)
320 (setvbuf (current-output-port) _IOLBF)
321 (setvbuf (current-error-port) _IOLBF))
323 (define* (show-version-and-exit #:optional (command (car (command-line))))
324 "Display version information for COMMAND and `(exit 0)'."
325 (simple-format #t "~a (~a) ~a~%"
326 command %guix-package-name %guix-version)
327 (format #t "Copyright ~a 2017 ~a"
328 ;; TRANSLATORS: Translate "(C)" to the copyright symbol
329 ;; (C-in-a-circle), if this symbol is available in the user's
330 ;; locale. Otherwise, do not translate "(C)"; leave it as-is. */
332 (G_ "the Guix authors\n"))
334 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
335 This is free software: you are free to change and redistribute it.
336 There is NO WARRANTY, to the extent permitted by law.
340 (define (show-bug-report-information)
341 ;; TRANSLATORS: The placeholder indicates the bug-reporting address for this
342 ;; package. Please add another line saying "Report translation bugs to
343 ;; ...\n" with the address for translation bugs (typically your translation
344 ;; team's web or email address).
346 Report bugs to: ~a.") %guix-bug-report-address)
348 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
350 General help using GNU software: <http://www.gnu.org/gethelp/>"))
353 (define (augmented-system-error-handler file)
354 "Return a 'system-error' handler that mentions FILE in its message."
355 (lambda (key proc fmt args errno)
356 ;; Augment the FMT and ARGS with information about TARGET (this
357 ;; information is missing as of Guile 2.0.11, making the exception
359 (apply throw key proc "~A: ~S"
360 (list (strerror (car errno)) file)
363 (define-syntax-rule (error-reporting-wrapper proc (args ...) file)
364 "Wrap PROC such that its 'system-error' exceptions are augmented to mention
366 (let ((real-proc (@ (guile) proc)))
370 (real-proc args ...))
371 (augmented-system-error-handler file)))))
374 ;; We 'set!' the global binding because (gnu build ...) modules and similar
375 ;; typically don't use (guix ui).
376 (error-reporting-wrapper symlink (source target) target))
379 ;; Note: here we use 'set!', not #:replace, because UIs typically use
380 ;; 'copy-recursively', which doesn't use (guix ui).
381 (error-reporting-wrapper copy-file (source target) target))
383 (set! canonicalize-path
384 (error-reporting-wrapper canonicalize-path (file) file))
387 (define (make-regexp* regexp . flags)
388 "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error
390 (catch 'regular-expression-syntax
392 (apply make-regexp regexp flags))
393 (lambda (key proc message . rest)
394 (leave (G_ "'~a' is not a valid regular expression: ~a~%")
397 (define (string->number* str)
398 "Like `string->number', but error out with an error message on failure."
399 (or (string->number str)
400 (leave (G_ "~a: invalid number~%") str)))
402 (define (size->number str)
403 "Convert STR, a storage measurement representation such as \"1024\" or
404 \"1MiB\", to a number of bytes. Raise an error if STR could not be
407 (string-rindex str char-set:digit))
410 (and unit-pos (substring str (+ 1 unit-pos))))
412 (let* ((numstr (if unit-pos
413 (substring str 0 (+ 1 unit-pos))
415 (num (string->number numstr)))
417 (leave (G_ "invalid number: ~a~%") numstr))
419 ((compose inexact->exact round)
422 ((or "KiB" "K" "k") (expt 2 10))
423 ((or "MiB" "M") (expt 2 20))
424 ((or "GiB" "G") (expt 2 30))
425 ((or "TiB" "T") (expt 2 40))
426 ((or "PiB" "P") (expt 2 50))
427 ((or "EiB" "E") (expt 2 60))
428 ((or "ZiB" "Z") (expt 2 70))
429 ((or "YiB" "Y") (expt 2 80))
440 (leave (G_ "unknown unit: ~a~%") unit)))))))
442 (define (call-with-error-handling thunk)
443 "Call THUNK within a user-friendly error handler."
444 (define (port-filename* port)
445 ;; 'port-filename' returns #f for non-file ports, but it raises an
446 ;; exception for file ports that are closed. Work around that.
447 (and (not (port-closed? port))
448 (port-filename port)))
450 (guard (c ((package-input-error? c)
451 (let* ((package (package-error-package c))
452 (input (package-error-invalid-input c))
453 (location (package-location package))
454 (file (location-file location))
455 (line (location-line location))
456 (column (location-column location)))
457 (leave (G_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
459 (package-full-name package) input)))
460 ((package-cross-build-system-error? c)
461 (let* ((package (package-error-package c))
462 (loc (package-location package))
463 (system (package-build-system package)))
464 (leave (G_ "~a: ~a: build system `~a' does not support cross builds~%")
465 (location->string loc)
466 (package-full-name package)
467 (build-system-name system))))
468 ((gexp-input-error? c)
469 (let ((input (package-error-invalid-input c)))
470 (leave (G_ "~s: invalid G-expression input~%")
471 (gexp-error-invalid-input c))))
472 ((profile-not-found-error? c)
473 (leave (G_ "profile '~a' does not exist~%")
474 (profile-error-profile c)))
475 ((missing-generation-error? c)
476 (leave (G_ "generation ~a of profile '~a' does not exist~%")
477 (missing-generation-error-generation c)
478 (profile-error-profile c)))
479 ((profile-collision-error? c)
480 (let ((entry (profile-collision-error-entry c))
481 (conflict (profile-collision-error-conflict c)))
482 (define (report-parent-entries entry)
483 (let ((parent (force (manifest-entry-parent entry))))
484 (when (manifest-entry? parent)
485 (report-error (G_ " ... propagated from ~a@~a~%")
486 (manifest-entry-name parent)
487 (manifest-entry-version parent))
488 (report-parent-entries parent))))
490 (report-error (G_ "profile contains conflicting entries for ~a:~a~%")
491 (manifest-entry-name entry)
492 (manifest-entry-output entry))
493 (report-error (G_ " first entry: ~a@~a:~a ~a~%")
494 (manifest-entry-name entry)
495 (manifest-entry-version entry)
496 (manifest-entry-output entry)
497 (manifest-entry-item entry))
498 (report-parent-entries entry)
499 (report-error (G_ " second entry: ~a@~a:~a ~a~%")
500 (manifest-entry-name conflict)
501 (manifest-entry-version conflict)
502 (manifest-entry-output conflict)
503 (manifest-entry-item conflict))
504 (report-parent-entries conflict)
507 (let ((file (nar-error-file c))
508 (port (nar-error-port c)))
510 (leave (G_ "corrupt input while restoring '~a' from ~s~%")
511 file (or (port-filename* port) port))
512 (leave (G_ "corrupt input while restoring archive from ~s~%")
513 (or (port-filename* port) port)))))
514 ((nix-connection-error? c)
515 (leave (G_ "failed to connect to `~a': ~a~%")
516 (nix-connection-error-file c)
517 (strerror (nix-connection-error-code c))))
518 ((nix-protocol-error? c)
519 ;; FIXME: Server-provided error messages aren't i18n'd.
520 (leave (G_ "build failed: ~a~%")
521 (nix-protocol-error-message c)))
522 ((derivation-missing-output-error? c)
523 (leave (G_ "reference to invalid output '~a' of derivation '~a'~%")
524 (derivation-missing-output c)
525 (derivation-file-name (derivation-error-derivation c))))
526 ((file-search-error? c)
527 (leave (G_ "file '~a' could not be found in these \
528 directories:~{ ~a~}~%")
529 (file-search-error-file-name c)
530 (file-search-error-search-path c)))
531 ((message-condition? c)
532 ;; Normally '&message' error conditions have an i18n'd message.
534 (gettext (condition-message c) %gettext-domain))))
535 ;; Catch EPIPE and the likes.
538 (lambda (key proc format-string format-args . rest)
539 (leave (G_ "~a: ~a~%") proc
540 (apply format #f format-string format-args))))))
542 (define-syntax-rule (leave-on-EPIPE exp ...)
543 "Run EXP... in a context when EPIPE errors are caught and lead to 'exit'
544 with successful exit code. This is useful when writing to the standard output
545 may lead to EPIPE, because the standard output is piped through 'head' or
551 ;; We really have to exit this brutally, otherwise Guile eventually
552 ;; attempts to flush all the ports, leading to an uncaught EPIPE down
554 (if (= EPIPE (system-error-errno args))
556 (apply throw args)))))
558 (define %guix-user-module
559 ;; Module in which user expressions are evaluated.
560 ;; Compute lazily to avoid circularity with (guix gexp).
562 (let ((module (make-module)))
563 (beautify-user-module! module)
564 ;; Use (guix gexp) so that one can use #~ & co.
565 (module-use! module (resolve-interface '(guix gexp)))
568 (define (read/eval str)
569 "Read and evaluate STR, raising an error if something goes wrong."
572 (call-with-input-string str read))
574 (leave (G_ "failed to read expression ~s: ~s~%")
578 (eval exp (force %guix-user-module)))
580 (report-error (G_ "failed to evaluate expression '~a':~%") exp)
582 (('syntax-error proc message properties form . rest)
583 (report-error (G_ "syntax error: ~a~%") message))
585 (if (message-condition? obj)
586 (report-error (G_ "~a~%")
587 (gettext (condition-message obj)
589 (report-error (G_ "exception thrown: ~s~%") obj)))
591 (apply display-error #f (current-error-port) args))
595 (define (read/eval-package-expression str)
596 "Read and evaluate STR and return the package it refers to, or exit an
598 (match (read/eval str)
601 (leave (G_ "expression ~s does not evaluate to a package~%")
604 (define (show-derivation-outputs derivation)
605 "Show the output file names of DERIVATION."
606 (format #t "~{~a~%~}"
609 (derivation->output-path derivation out-name)))
610 (derivation-outputs derivation))))
612 (define (check-available-space need)
613 "Make sure at least NEED bytes are available in the store. Otherwise emit a
615 (let ((free (catch 'system-error
617 (free-disk-space (%store-prefix)))
619 (when (and free (>= need free))
620 (warning (G_ "at least ~,1h MB needed but only ~,1h MB available in ~a~%")
621 (/ need 1e6) (/ free 1e6) (%store-prefix)))))
623 (define* (show-what-to-build store drv
624 #:key dry-run? (use-substitutes? #t)
625 (mode (build-mode normal)))
626 "Show what will or would (depending on DRY-RUN?) be built in realizing the
627 derivations listed in DRV using MODE, a 'build-mode' value. Return #t if
628 there's something to build, #f otherwise. When USE-SUBSTITUTES?, check and
629 report what is prerequisites are available for download."
630 (define substitutable-info
631 ;; Call 'substitutation-oracle' upfront so we don't end up launching the
632 ;; substituter many times. This makes a big difference, especially when
633 ;; DRV is a long list as is the case with 'guix environment'.
635 (substitution-oracle store drv #:mode mode)
638 (define (built-or-substitutable? drv)
639 (or (null? (derivation-outputs drv))
640 (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists
641 (or (valid-path? store out)
642 (substitutable-info out)))))
644 (let*-values (((build download)
645 (fold2 (lambda (drv build download)
647 (derivation-prerequisites-to-build
651 substitutable-info)))
652 (values (append b build)
653 (append d download))))
656 ((build) ; add the DRV themselves
658 (append (map derivation-file-name
659 (remove built-or-substitutable? drv))
660 (map derivation-input-path build))))
661 ((download) ; add the references of DOWNLOAD
665 (filter-map (lambda (item)
666 (if (valid-path? store item)
668 (substitutable-info item)))
670 substitutable-references
673 (define installed-size
674 (reduce + 0 (map substitutable-nar-size download)))
676 (define download-size
677 (/ (reduce + 0 (map substitutable-download-size download))
680 (define display-download-size?
681 ;; Sometimes narinfos lack information about the download size. Only
682 ;; display when we have information for all of DOWNLOAD.
683 (not (any (compose zero? substitutable-download-size) download)))
687 (format (current-error-port)
688 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
689 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
692 (if display-download-size?
693 (format (current-error-port)
694 ;; TRANSLATORS: "MB" is for "megabyte"; it should be
695 ;; translated to the corresponding abbreviation.
696 (G_ "~:[~,1h MB would be downloaded:~%~{ ~a~%~}~;~]")
699 (map substitutable-path download))
700 (format (current-error-port)
701 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
702 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
705 (map substitutable-path download))))
707 (format (current-error-port)
708 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
709 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
712 (if display-download-size?
713 (format (current-error-port)
714 ;; TRANSLATORS: "MB" is for "megabyte"; it should be
715 ;; translated to the corresponding abbreviation.
716 (G_ "~:[~,1h MB will be downloaded:~%~{ ~a~%~}~;~]")
719 (map substitutable-path download))
720 (format (current-error-port)
721 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
722 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
725 (map substitutable-path download)))))
727 (check-available-space installed-size)
731 (define show-what-to-build*
732 (store-lift show-what-to-build))
734 (define (right-arrow port)
735 "Return either a string containing the 'RIGHT ARROW' character, or an ASCII
736 replacement if PORT is not Unicode-capable."
737 (let ((encoding (port-encoding port))
739 (catch 'encoding-error
741 (call-with-output-string
743 (set-port-encoding! port encoding)
744 (set-port-conversion-strategy! port 'error)
745 (display arrow port))))
749 (define* (show-manifest-transaction store manifest transaction
751 "Display what will/would be installed/removed from MANIFEST by TRANSACTION."
752 (define (package-strings name version output item)
753 (map (lambda (name version output item)
754 (format #f " ~a~:[:~a~;~*~]\t~a\t~a"
756 (equal? output "out") output version
758 (package-output store item output)
760 name version output item))
762 (define → ;an arrow that can be represented on stderr
763 (right-arrow (current-error-port)))
765 (define (upgrade-string name old-version new-version output item)
766 (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a"
767 name (equal? output "out") output
768 old-version → new-version
770 (package-output store item output)
773 (let-values (((remove install upgrade downgrade)
774 (manifest-transaction-effects manifest transaction)))
776 ((($ <manifest-entry> name version output item) ..1)
777 (let ((len (length name))
778 (remove (package-strings name version output item)))
780 (format (current-error-port)
781 (N_ "The following package would be removed:~%~{~a~%~}~%"
782 "The following packages would be removed:~%~{~a~%~}~%"
785 (format (current-error-port)
786 (N_ "The following package will be removed:~%~{~a~%~}~%"
787 "The following packages will be removed:~%~{~a~%~}~%"
792 (((($ <manifest-entry> name old-version)
793 . ($ <manifest-entry> _ new-version output item)) ..1)
794 (let ((len (length name))
795 (downgrade (map upgrade-string
796 name old-version new-version output item)))
798 (format (current-error-port)
799 (N_ "The following package would be downgraded:~%~{~a~%~}~%"
800 "The following packages would be downgraded:~%~{~a~%~}~%"
803 (format (current-error-port)
804 (N_ "The following package will be downgraded:~%~{~a~%~}~%"
805 "The following packages will be downgraded:~%~{~a~%~}~%"
810 (((($ <manifest-entry> name old-version)
811 . ($ <manifest-entry> _ new-version output item)) ..1)
812 (let ((len (length name))
813 (upgrade (map upgrade-string
814 name old-version new-version output item)))
816 (format (current-error-port)
817 (N_ "The following package would be upgraded:~%~{~a~%~}~%"
818 "The following packages would be upgraded:~%~{~a~%~}~%"
821 (format (current-error-port)
822 (N_ "The following package will be upgraded:~%~{~a~%~}~%"
823 "The following packages will be upgraded:~%~{~a~%~}~%"
828 ((($ <manifest-entry> name version output item _) ..1)
829 (let ((len (length name))
830 (install (package-strings name version output item)))
832 (format (current-error-port)
833 (N_ "The following package would be installed:~%~{~a~%~}~%"
834 "The following packages would be installed:~%~{~a~%~}~%"
837 (format (current-error-port)
838 (N_ "The following package will be installed:~%~{~a~%~}~%"
839 "The following packages will be installed:~%~{~a~%~}~%"
844 (define-syntax with-error-handling
846 "Run BODY within a user-friendly error condition handler."
848 (call-with-error-handling
852 (define (location->string loc)
853 "Return a human-friendly, GNU-standard representation of LOC."
855 (#f (G_ "<unknown location>"))
856 (($ <location> file line column)
857 (format #f "~a:~a:~a" file line column))))
859 (define* (config-directory #:key (ensure? #t))
860 "Return the name of the configuration directory, after making sure that it
861 exists if ENSURE? is true. Honor the XDG specs,
862 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
863 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
864 (and=> (getenv "HOME")
865 (cut string-append <> "/.config")))
866 (cut string-append <> "/guix"))))
873 (let ((err (system-error-errno args)))
874 ;; ERR is necessarily different from EEXIST.
875 (leave (G_ "failed to create configuration directory `~a': ~a~%")
876 dir (strerror err)))))))
878 (define* (fill-paragraph str width #:optional (column 0))
879 "Fill STR such that each line contains at most WIDTH characters, assuming
880 that the first character is at COLUMN.
882 When STR contains a single line break surrounded by other characters, it is
883 converted to a space; sequences of more than one line break are preserved."
884 (define (maybe-break chr result)
886 ((column newlines chars)
889 `(,column ,(+ 1 newlines) ,chars))
891 (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1))
892 (chars (case newlines
895 (append (make-list spaces #\space) chars))
897 (append (make-list newlines #\newline) chars))))
898 (column (case newlines
900 ((1) (+ spaces column))
902 (let ((chars (cons chr chars))
903 (column (+ 1 column)))
905 (let*-values (((before after)
906 (break (cut eqv? #\space <>) chars))
916 (drop-while (cut eqv? #\space <>)
918 `(,column 0 ,chars))) ; unbreakable
919 `(,column 0 ,chars)))))))))
921 (match (string-fold maybe-break
924 ((column newlines chars)
925 (list->string (reverse chars)))))
933 (make-parameter (terminal-columns)))
935 (set! (@@ (texinfo plain-text) wrap*)
936 ;; XXX: Monkey patch this private procedure to let 'package->recutils'
937 ;; parameterize the fill of description field correctly.
939 (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*))))
940 (fill-string (string-concatenate strings)
941 #:line-width (%text-width) #:initial-indent indent
942 #:subsequent-indent indent))))
944 (define (texi->plain-text str)
945 "Return a plain-text representation of texinfo fragment STR."
946 ;; 'texi-fragment->stexi' uses a string port so make sure it's a
947 ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.)
948 (with-fluids ((%default-port-encoding "UTF-8"))
949 (stexi->plain-text (texi-fragment->stexi str))))
951 (define (package-field-string package field-accessor)
952 "Return a plain-text representation of PACKAGE field."
953 (and=> (field-accessor package)
954 (compose texi->plain-text P_)))
956 (define (package-description-string package)
957 "Return a plain-text representation of PACKAGE description field."
958 (package-field-string package package-description))
960 (define (package-synopsis-string package)
961 "Return a plain-text representation of PACKAGE synopsis field."
962 (package-field-string package package-synopsis))
964 (define (string->recutils str)
965 "Return a version of STR where newlines have been replaced by newlines
966 followed by \"+ \", which makes for a valid multi-line field value in the
969 (string-fold-right (lambda (chr result)
970 (if (eqv? chr #\newline)
971 (cons* chr #\+ #\space result)
976 (define* (package->recutils p port #:optional (width (%text-width))
977 #:key (extra-fields '()))
978 "Write to PORT a `recutils' record of package P, arranging to fit within
979 WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to emit."
981 ;; The available number of columns once we've taken into account space for
982 ;; the initial "+ " prefix.
983 (if (> width 2) (- width 2) width))
985 (define (dependencies->recutils packages)
986 (let ((list (string-join (delete-duplicates
987 (map package-full-name
988 (sort packages package<?))) " ")))
990 (fill-paragraph list width*
991 (string-length "dependencies: ")))))
993 (define (package<? p1 p2)
994 (string<? (package-full-name p1) (package-full-name p2)))
996 ;; Note: Don't i18n field names so that people can post-process it.
997 (format port "name: ~a~%" (package-name p))
998 (format port "version: ~a~%" (package-version p))
999 (format port "outputs: ~a~%" (string-join (package-outputs p)))
1000 (format port "systems: ~a~%"
1001 (string-join (package-transitive-supported-systems p)))
1002 (format port "dependencies: ~a~%"
1003 (match (package-direct-inputs p)
1004 (((labels inputs . _) ...)
1005 (dependencies->recutils (filter package? inputs)))))
1006 (format port "location: ~a~%"
1007 (or (and=> (package-location p) location->string)
1010 ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in
1011 ;; field identifiers.
1012 (format port "homepage: ~a~%" (package-home-page p))
1014 (format port "license: ~a~%"
1015 (match (package-license p)
1016 (((? license? licenses) ...)
1017 (string-join (map license-name licenses)
1019 ((? license? license)
1020 (license-name license))
1023 (format port "synopsis: ~a~%"
1024 (string-map (match-lambda
1027 (or (and=> (package-synopsis-string p) P_)
1032 (parameterize ((%text-width width*))
1034 (string-append "description: "
1035 (or (and=> (package-description p) P_)
1038 (for-each (match-lambda
1040 (let ((field (symbol->string field)))
1041 (format port "~a: ~a~%"
1043 (fill-paragraph (object->string value) width*
1044 (string-length field))))))
1048 (define (string->generations str)
1049 "Return the list of generations matching a pattern in STR. This function
1050 accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
1051 (define (maybe-integer)
1052 (let ((x (string->number str)))
1056 (define (maybe-comma-separated-integers)
1057 (let ((lst (delete-duplicates
1059 (string-split str #\,)))))
1060 (and (every integer? lst)
1063 (cond ((maybe-integer)
1066 ((maybe-comma-separated-integers)
1069 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
1072 (let ((s (string->number (match:substring match 1)))
1073 (e (string->number (match:substring match 2))))
1074 (and (every integer? (list s e))
1076 (iota (1+ (- e s)) s)))))
1077 ((string-match "^([0-9]+)\\.\\.$" str)
1080 (let ((s (string->number (match:substring match 1))))
1083 ((string-match "^\\.\\.([0-9]+)$" str)
1086 (let ((e (string->number (match:substring match 1))))
1091 (define (string->duration str)
1092 "Return the duration matching a pattern in STR. This function accepts the
1093 following patterns: \"1d\", \"1w\", \"1m\"."
1094 (define (hours->duration hours match)
1095 (make-time time-duration 0
1096 (* 3600 hours (string->number (match:substring match 1)))))
1098 (cond ((string-match "^([0-9]+)s$" str)
1101 (make-time time-duration 0
1102 (string->number (match:substring match 1)))))
1103 ((string-match "^([0-9]+)h$" str)
1106 (hours->duration 1 match)))
1107 ((string-match "^([0-9]+)d$" str)
1110 (hours->duration 24 match)))
1111 ((string-match "^([0-9]+)w$" str)
1114 (hours->duration (* 24 7) match)))
1115 ((string-match "^([0-9]+)m$" str)
1118 (hours->duration (* 24 30) match)))
1121 (define* (matching-generations str profile
1122 #:key (duration-relation <=))
1123 "Return the list of available generations matching a pattern in STR. See
1124 'string->generations' and 'string->duration' for the list of valid patterns.
1125 When STR is a duration pattern, return all the generations whose ctime has
1126 DURATION-RELATION with the current time."
1127 (define (valid-generations lst)
1128 (define (valid-generation? n)
1129 (any (cut = n <>) (generation-numbers profile)))
1131 (fold-right (lambda (x acc)
1132 (if (valid-generation? x)
1138 (define (filter-generations generations)
1142 (drop-while (cut > n <>)
1143 (generation-numbers profile)))
1145 (valid-generations (iota n 1)))
1147 (valid-generations lst))
1150 (define (filter-by-duration duration)
1151 (define (time-at-midnight time)
1152 ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and
1154 (let ((d (time-utc->date time)))
1157 (date-day d) (date-month d)
1158 (date-year d) (date-zone-offset d)))))
1160 (define generation-ctime-alist
1161 (map (lambda (number)
1165 (generation-time profile number)))))
1166 (generation-numbers profile)))
1171 (let ((s (time-second
1172 (subtract-duration (time-at-midnight (current-time))
1174 (delete #f (map (lambda (x)
1175 (and (duration-relation s (cdr x))
1177 generation-ctime-alist))))))
1179 (cond ((string->generations str)
1182 ((string->duration str)
1187 (define (display-generation profile number)
1188 "Display a one-line summary of generation NUMBER of PROFILE."
1189 (unless (zero? number)
1190 (let ((header (format #f (G_ "Generation ~a\t~a") number
1193 (generation-time profile number))
1195 (current (generation-number profile)))
1196 (if (= number current)
1197 ;; TRANSLATORS: The word "current" here is an adjective for
1198 ;; "Generation", as in "current generation". Use the appropriate
1199 ;; gender where applicable.
1200 (format #t (G_ "~a\t(current)~%") header)
1201 (format #t "~a~%" header)))))
1203 (define (display-profile-content-diff profile gen1 gen2)
1204 "Display the changed packages in PROFILE GEN2 compared to generation GEN2."
1206 (define (equal-entry? first second)
1207 (string= (manifest-entry-item first) (manifest-entry-item second)))
1209 (define (display-entry entry prefix)
1211 (($ <manifest-entry> name version output location _)
1212 (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location))))
1214 (define (list-entries number)
1215 (manifest-entries (profile-manifest (generation-file-name profile number))))
1217 (define (display-diff profile old new)
1218 (display-generation profile new)
1219 (let ((added (lset-difference
1220 equal-entry? (list-entries new) (list-entries old)))
1221 (removed (lset-difference
1222 equal-entry? (list-entries old) (list-entries new))))
1223 (for-each (cut display-entry <> "+") added)
1224 (for-each (cut display-entry <> "-") removed)
1227 (display-diff profile gen1 gen2))
1229 (define (display-profile-content profile number)
1230 "Display the packages in PROFILE, generation NUMBER, in a human-readable
1232 (for-each (match-lambda
1233 (($ <manifest-entry> name version output location _)
1234 (format #t " ~a\t~a\t~a\t~a~%"
1235 name version output location)))
1237 ;; Show most recently installed packages last.
1240 (profile-manifest (generation-file-name profile number))))))
1242 (define (display-generation-change previous current)
1243 (format #t (G_ "switched from generation ~a to ~a~%") previous current))
1245 (define (roll-back* store profile)
1246 "Like 'roll-back', but display what is happening."
1249 (roll-back store profile))
1250 display-generation-change))
1252 (define (switch-to-generation* profile number)
1253 "Like 'switch-generation', but display what is happening."
1254 (let ((previous (switch-to-generation profile number)))
1255 (display-generation-change previous number)))
1257 (define (delete-generation* store profile generation)
1258 "Like 'delete-generation', but display what is going on."
1259 (format #t (G_ "deleting ~a~%")
1260 (generation-file-name profile generation))
1261 (delete-generation store profile generation))
1263 (define* (package-specification->name+version+output spec
1264 #:optional (output "out"))
1265 "Parse package specification SPEC and return three value: the specified
1266 package name, version number (or #f), and output name (or OUTPUT). SPEC may
1267 optionally contain a version number and an output name, as in these examples:
1274 (let*-values (((name sub-drv)
1275 (match (string-rindex spec #\:)
1276 (#f (values spec output))
1277 (colon (values (substring spec 0 colon)
1278 (substring spec (+ 1 colon))))))
1280 (package-name->name+version name)))
1281 (values name version sub-drv)))
1285 ;;; Command-line option processing.
1288 (define (show-guix-usage)
1289 (format (current-error-port)
1290 (G_ "Try `guix --help' for more information.~%"))
1293 (define (command-files)
1294 "Return the list of source files that define Guix sub-commands."
1296 (and=> (search-path %load-path "guix.scm")
1297 (compose (cut string-append <> "/guix/scripts")
1301 (cut string-suffix? ".scm" <>))
1304 (scandir directory dot-scm?)
1308 "Return the list of Guix command names."
1309 (map (compose (cut string-drop-right <> 4)
1313 (define (show-guix-help)
1314 (define (internal? command)
1315 (member command '("substitute" "authenticate" "offload"
1316 "perform-download")))
1318 (format #t (G_ "Usage: guix COMMAND ARGS...
1319 Run COMMAND with ARGS.\n"))
1321 (format #t (G_ "COMMAND must be one of the sub-commands listed below:\n"))
1323 ;; TODO: Display a synopsis of each command.
1324 (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
1326 (show-bug-report-information))
1328 (define program-name
1329 ;; Name of the command-line program currently executing, or #f.
1330 (make-parameter #f))
1332 (define (run-guix-command command . args)
1333 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
1338 (resolve-interface `(guix scripts ,command)))
1340 (format (current-error-port)
1341 (G_ "guix: ~a: command not found~%") command)
1342 (show-guix-usage))))
1344 (let ((command-main (module-ref module
1345 (symbol-append 'guix- command))))
1346 (parameterize ((program-name command))
1347 ;; Disable canonicalization so we don't don't stat unreasonably.
1348 (with-fluids ((%file-port-name-canonicalization #f))
1352 (apply command-main args))
1354 ;; Abuse 'exit-hook' (which is normally meant to be used by the
1355 ;; REPL) to run things like profiling hooks upon completion.
1356 (run-hook exit-hook)))))))
1358 (define (run-guix . args)
1359 "Run the 'guix' command defined by command line ARGS.
1360 Unlike 'guix-main', this procedure assumes that locale, i18n support,
1361 and signal handling has already been set up."
1362 (define option? (cut string-prefix? "-" <>))
1364 ;; The default %LOAD-EXTENSIONS includes the empty string, which doubles the
1365 ;; number of 'stat' calls per entry in %LOAD-PATH. Shamelessly remove it.
1366 (set! %load-extensions '(".scm"))
1370 (format (current-error-port)
1371 (G_ "guix: missing command name~%"))
1373 ((or ("-h") ("--help"))
1376 (show-version-and-exit "guix"))
1377 (((? option? o) args ...)
1378 (format (current-error-port)
1379 (G_ "guix: unrecognized option '~a'~%") o)
1382 (apply run-guix-command (string->symbol command)
1387 (apply run-guix-command
1388 (string->symbol command)
1391 (define guix-warning-port
1392 (make-parameter (current-warning-port)))
1394 (define (guix-main arg0 . args)
1396 (apply run-guix args))
1398 ;;; ui.scm ends here