4bad00e8cfff3a6feaa4990c8ae1acef3abd74e9
[jackhill/guix/guix.git] / guix / ui.scm
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>
12 ;;;
13 ;;; This file is part of GNU Guix.
14 ;;;
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.
19 ;;;
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.
24 ;;;
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/>.
27
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)
59 #:export (G_
60 N_
61 P_
62 report-error
63 leave
64 make-user-module
65 load*
66 warn-about-load-error
67 show-version-and-exit
68 show-bug-report-information
69 make-regexp*
70 string->number*
71 size->number
72 show-derivation-outputs
73 show-what-to-build
74 show-what-to-build*
75 show-manifest-transaction
76 call-with-error-handling
77 with-error-handling
78 leave-on-EPIPE
79 read/eval
80 read/eval-package-expression
81 location->string
82 config-directory
83 fill-paragraph
84 texi->plain-text
85 package-description-string
86 package-synopsis-string
87 string->recutils
88 package->recutils
89 package-specification->name+version+output
90 string->generations
91 string->duration
92 matching-generations
93 display-generation
94 display-profile-content
95 display-profile-content-diff
96 roll-back*
97 switch-to-generation*
98 delete-generation*
99 run-guix-command
100 run-guix
101 program-name
102 guix-warning-port
103 warning
104 info
105 guix-main))
106
107 ;;; Commentary:
108 ;;;
109 ;;; User interface facilities for command-line tools.
110 ;;;
111 ;;; Code:
112
113 (define %gettext-domain
114 ;; Text domain for strings used in the tools.
115 "guix")
116
117 (define %package-text-domain
118 ;; Text domain for package synopses and descriptions.
119 "guix-packages")
120
121 (define G_ (cut gettext <> %gettext-domain))
122 (define N_ (cut ngettext <> <> <> %gettext-domain))
123
124 (define (P_ msgid)
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)
130 msgid
131 (gettext msgid %package-text-domain)))
132
133 (define-syntax-rule (define-diagnostic name prefix)
134 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
135 messages."
136 (define-syntax name
137 (lambda (x)
138 (define (augmented-format-string fmt)
139 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
140
141 (syntax-case x ()
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
149 args (... ...))))
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 (... ...))))))))
161
162 (define-diagnostic warning "warning: ") ; emit a warning
163 (define-diagnostic info "")
164
165 (define-diagnostic report-error "error: ")
166 (define-syntax-rule (leave args ...)
167 "Emit an error message and exit."
168 (begin
169 (report-error args ...)
170 (exit 1)))
171
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)))
178 modules)
179 module))
180
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)
187 (previous frame))
188 (if (not frame)
189 previous
190 (if (frame-source frame)
191 frame
192 (loop (frame-previous frame) frame)))))
193
194 (define (error-string frame args)
195 (call-with-output-string
196 (lambda (port)
197 (apply display-error frame port (cdr args)))))
198
199 (define tag
200 (make-prompt-tag "user-code"))
201
202 (catch #t
203 (lambda ()
204 ;; XXX: Force a recompilation to avoid ABI issues.
205 (set! %fresh-auto-compile #t)
206 (set! %load-should-auto-compile #t)
207
208 (save-module-excursion
209 (lambda ()
210 (set-current-module user-module)
211
212 ;; Hide the "auto-compiling" messages.
213 (parameterize ((current-warning-port (%make-void-port "w")))
214 (call-with-prompt tag
215 (lambda ()
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)))
221 (const #f))))))
222 (lambda _
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.
225 (exit 1))
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
233 (if (> depth 1)
234 (stack-ref stack 1) ;skip the 'throw' frame
235 last))))
236
237 (report-load-error file args frame)
238
239 (case on-error
240 ((debug)
241 (newline)
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)
245 #f)))
246 ((backtrace)
247 (newline (current-error-port))
248 (display-backtrace stack (current-error-port)))
249 (else
250 #t))))))
251
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."
255 (match args
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)))
263 (('srfi-34 obj)
264 (if (message-condition? obj)
265 (report-error (G_ "~a~%")
266 (gettext (condition-message obj)
267 %gettext-domain))
268 (report-error (G_ "exception thrown: ~s~%") obj)))
269 ((error args ...)
270 (report-error (G_ "failed to load '~a':~%") file)
271 (apply display-error frame (current-error-port) args))))
272
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."
276 (match args
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)))
284 (('srfi-34 obj)
285 (if (message-condition? obj)
286 (warning (G_ "failed to load '~a': ~a~%")
287 file
288 (gettext (condition-message obj) %gettext-domain))
289 (warning (G_ "failed to load '~a': exception thrown: ~s~%")
290 file obj)))
291 ((error args ...)
292 (warning (G_ "failed to load '~a':~%") file)
293 (apply display-error #f (current-error-port) args))))
294
295 (define (install-locale)
296 "Install the current locale settings."
297 (catch 'system-error
298 (lambda _
299 (setlocale LC_ALL ""))
300 (lambda args
301 (warning (G_ "failed to install locale: ~a~%")
302 (strerror (system-error-errno args))))))
303
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))
312
313 (install-locale)
314 (textdomain %gettext-domain)
315
316 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
317 ;; notified via an EPIPE later.
318 (sigaction SIGPIPE SIG_IGN)
319
320 (setvbuf (current-output-port) _IOLBF)
321 (setvbuf (current-error-port) _IOLBF))
322
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. */
331 (G_ "(C)")
332 (G_ "the Guix authors\n"))
333 (display (G_"\
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.
337 "))
338 (exit 0))
339
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).
345 (format #t (G_ "
346 Report bugs to: ~a.") %guix-bug-report-address)
347 (format #t (G_ "
348 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
349 (display (G_ "
350 General help using GNU software: <http://www.gnu.org/gethelp/>"))
351 (newline))
352
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
358 ;; uninformative.)
359 (apply throw key proc "~A: ~S"
360 (list (strerror (car errno)) file)
361 (list errno))))
362
363 (define-syntax-rule (error-reporting-wrapper proc (args ...) file)
364 "Wrap PROC such that its 'system-error' exceptions are augmented to mention
365 FILE."
366 (let ((real-proc (@ (guile) proc)))
367 (lambda (args ...)
368 (catch 'system-error
369 (lambda ()
370 (real-proc args ...))
371 (augmented-system-error-handler file)))))
372
373 (set! symlink
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))
377
378 (set! copy-file
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))
382
383 (set! canonicalize-path
384 (error-reporting-wrapper canonicalize-path (file) file))
385
386
387 (define (make-regexp* regexp . flags)
388 "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error
389 nicely."
390 (catch 'regular-expression-syntax
391 (lambda ()
392 (apply make-regexp regexp flags))
393 (lambda (key proc message . rest)
394 (leave (G_ "'~a' is not a valid regular expression: ~a~%")
395 regexp message))))
396
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)))
401
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
405 interpreted."
406 (define unit-pos
407 (string-rindex str char-set:digit))
408
409 (define unit
410 (and unit-pos (substring str (+ 1 unit-pos))))
411
412 (let* ((numstr (if unit-pos
413 (substring str 0 (+ 1 unit-pos))
414 str))
415 (num (string->number numstr)))
416 (unless num
417 (leave (G_ "invalid number: ~a~%") numstr))
418
419 ((compose inexact->exact round)
420 (* num
421 (match unit
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))
430 ("kB" (expt 10 3))
431 ("MB" (expt 10 6))
432 ("GB" (expt 10 9))
433 ("TB" (expt 10 12))
434 ("PB" (expt 10 15))
435 ("EB" (expt 10 18))
436 ("ZB" (expt 10 21))
437 ("YB" (expt 10 24))
438 ("" 1)
439 (x
440 (leave (G_ "unknown unit: ~a~%") unit)))))))
441
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)))
449
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~%")
458 file line column
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))))
489
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)
505 (exit 1)))
506 ((nar-error? c)
507 (let ((file (nar-error-file c))
508 (port (nar-error-port c)))
509 (if file
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.
533 (leave (G_ "~a~%")
534 (gettext (condition-message c) %gettext-domain))))
535 ;; Catch EPIPE and the likes.
536 (catch 'system-error
537 thunk
538 (lambda (key proc format-string format-args . rest)
539 (leave (G_ "~a: ~a~%") proc
540 (apply format #f format-string format-args))))))
541
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
546 similar."
547 (catch 'system-error
548 (lambda ()
549 exp ...)
550 (lambda args
551 ;; We really have to exit this brutally, otherwise Guile eventually
552 ;; attempts to flush all the ports, leading to an uncaught EPIPE down
553 ;; the path.
554 (if (= EPIPE (system-error-errno args))
555 (primitive-_exit 0)
556 (apply throw args)))))
557
558 (define %guix-user-module
559 ;; Module in which user expressions are evaluated.
560 ;; Compute lazily to avoid circularity with (guix gexp).
561 (delay
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)))
566 module)))
567
568 (define (read/eval str)
569 "Read and evaluate STR, raising an error if something goes wrong."
570 (let ((exp (catch #t
571 (lambda ()
572 (call-with-input-string str read))
573 (lambda args
574 (leave (G_ "failed to read expression ~s: ~s~%")
575 str args)))))
576 (catch #t
577 (lambda ()
578 (eval exp (force %guix-user-module)))
579 (lambda args
580 (report-error (G_ "failed to evaluate expression '~a':~%") exp)
581 (match args
582 (('syntax-error proc message properties form . rest)
583 (report-error (G_ "syntax error: ~a~%") message))
584 (('srfi-34 obj)
585 (if (message-condition? obj)
586 (report-error (G_ "~a~%")
587 (gettext (condition-message obj)
588 %gettext-domain))
589 (report-error (G_ "exception thrown: ~s~%") obj)))
590 ((error args ...)
591 (apply display-error #f (current-error-port) args))
592 (what? #f))
593 (exit 1)))))
594
595 (define (read/eval-package-expression str)
596 "Read and evaluate STR and return the package it refers to, or exit an
597 error."
598 (match (read/eval str)
599 ((? package? p) p)
600 (x
601 (leave (G_ "expression ~s does not evaluate to a package~%")
602 str))))
603
604 (define (show-derivation-outputs derivation)
605 "Show the output file names of DERIVATION."
606 (format #t "~{~a~%~}"
607 (map (match-lambda
608 ((out-name . out)
609 (derivation->output-path derivation out-name)))
610 (derivation-outputs derivation))))
611
612 (define (check-available-space need)
613 "Make sure at least NEED bytes are available in the store. Otherwise emit a
614 warning."
615 (let ((free (catch 'system-error
616 (lambda ()
617 (free-disk-space (%store-prefix)))
618 (const #f))))
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)))))
622
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'.
634 (if use-substitutes?
635 (substitution-oracle store drv #:mode mode)
636 (const #f)))
637
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)))))
643
644 (let*-values (((build download)
645 (fold2 (lambda (drv build download)
646 (let-values (((b d)
647 (derivation-prerequisites-to-build
648 store drv
649 #:mode mode
650 #:substitutable-info
651 substitutable-info)))
652 (values (append b build)
653 (append d download))))
654 '() '()
655 drv))
656 ((build) ; add the DRV themselves
657 (delete-duplicates
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
662 (if use-substitutes?
663 (delete-duplicates
664 (append download
665 (filter-map (lambda (item)
666 (if (valid-path? store item)
667 #f
668 (substitutable-info item)))
669 (append-map
670 substitutable-references
671 download))))
672 download)))
673 (define installed-size
674 (reduce + 0 (map substitutable-nar-size download)))
675
676 (define download-size
677 (/ (reduce + 0 (map substitutable-download-size download))
678 1e6))
679
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)))
684
685 (if dry-run?
686 (begin
687 (format (current-error-port)
688 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
689 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
690 (length build))
691 (null? build) build)
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~%~}~;~]")
697 (null? download)
698 download-size
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~%~}~;~]"
703 (length download))
704 (null? download)
705 (map substitutable-path download))))
706 (begin
707 (format (current-error-port)
708 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
709 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
710 (length build))
711 (null? build) build)
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~%~}~;~]")
717 (null? download)
718 download-size
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~%~}~;~]"
723 (length download))
724 (null? download)
725 (map substitutable-path download)))))
726
727 (check-available-space installed-size)
728
729 (pair? build)))
730
731 (define show-what-to-build*
732 (store-lift show-what-to-build))
733
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))
738 (arrow "→"))
739 (catch 'encoding-error
740 (lambda ()
741 (call-with-output-string
742 (lambda (port)
743 (set-port-encoding! port encoding)
744 (set-port-conversion-strategy! port 'error)
745 (display arrow port))))
746 (lambda (key . args)
747 "->"))))
748
749 (define* (show-manifest-transaction store manifest transaction
750 #:key dry-run?)
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"
755 name
756 (equal? output "out") output version
757 (if (package? item)
758 (package-output store item output)
759 item)))
760 name version output item))
761
762 (define → ;an arrow that can be represented on stderr
763 (right-arrow (current-error-port)))
764
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
769 (if (package? item)
770 (package-output store item output)
771 item)))
772
773 (let-values (((remove install upgrade downgrade)
774 (manifest-transaction-effects manifest transaction)))
775 (match remove
776 ((($ <manifest-entry> name version output item) ..1)
777 (let ((len (length name))
778 (remove (package-strings name version output item)))
779 (if dry-run?
780 (format (current-error-port)
781 (N_ "The following package would be removed:~%~{~a~%~}~%"
782 "The following packages would be removed:~%~{~a~%~}~%"
783 len)
784 remove)
785 (format (current-error-port)
786 (N_ "The following package will be removed:~%~{~a~%~}~%"
787 "The following packages will be removed:~%~{~a~%~}~%"
788 len)
789 remove))))
790 (x #f))
791 (match downgrade
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)))
797 (if dry-run?
798 (format (current-error-port)
799 (N_ "The following package would be downgraded:~%~{~a~%~}~%"
800 "The following packages would be downgraded:~%~{~a~%~}~%"
801 len)
802 downgrade)
803 (format (current-error-port)
804 (N_ "The following package will be downgraded:~%~{~a~%~}~%"
805 "The following packages will be downgraded:~%~{~a~%~}~%"
806 len)
807 downgrade))))
808 (x #f))
809 (match upgrade
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)))
815 (if dry-run?
816 (format (current-error-port)
817 (N_ "The following package would be upgraded:~%~{~a~%~}~%"
818 "The following packages would be upgraded:~%~{~a~%~}~%"
819 len)
820 upgrade)
821 (format (current-error-port)
822 (N_ "The following package will be upgraded:~%~{~a~%~}~%"
823 "The following packages will be upgraded:~%~{~a~%~}~%"
824 len)
825 upgrade))))
826 (x #f))
827 (match install
828 ((($ <manifest-entry> name version output item _) ..1)
829 (let ((len (length name))
830 (install (package-strings name version output item)))
831 (if dry-run?
832 (format (current-error-port)
833 (N_ "The following package would be installed:~%~{~a~%~}~%"
834 "The following packages would be installed:~%~{~a~%~}~%"
835 len)
836 install)
837 (format (current-error-port)
838 (N_ "The following package will be installed:~%~{~a~%~}~%"
839 "The following packages will be installed:~%~{~a~%~}~%"
840 len)
841 install))))
842 (x #f))))
843
844 (define-syntax with-error-handling
845 (syntax-rules ()
846 "Run BODY within a user-friendly error condition handler."
847 ((_ body ...)
848 (call-with-error-handling
849 (lambda ()
850 body ...)))))
851
852 (define (location->string loc)
853 "Return a human-friendly, GNU-standard representation of LOC."
854 (match loc
855 (#f (G_ "<unknown location>"))
856 (($ <location> file line column)
857 (format #f "~a:~a:~a" file line column))))
858
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"))))
867 (catch 'system-error
868 (lambda ()
869 (when ensure?
870 (mkdir-p dir))
871 dir)
872 (lambda args
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)))))))
877
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.
881
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)
885 (match result
886 ((column newlines chars)
887 (case chr
888 ((#\newline)
889 `(,column ,(+ 1 newlines) ,chars))
890 (else
891 (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1))
892 (chars (case newlines
893 ((0) chars)
894 ((1)
895 (append (make-list spaces #\space) chars))
896 (else
897 (append (make-list newlines #\newline) chars))))
898 (column (case newlines
899 ((0) column)
900 ((1) (+ spaces column))
901 (else 0))))
902 (let ((chars (cons chr chars))
903 (column (+ 1 column)))
904 (if (> column width)
905 (let*-values (((before after)
906 (break (cut eqv? #\space <>) chars))
907 ((len)
908 (length before)))
909 (if (<= len width)
910 `(,len
911 0
912 ,(if (null? after)
913 before
914 (append before
915 (cons #\newline
916 (drop-while (cut eqv? #\space <>)
917 after)))))
918 `(,column 0 ,chars))) ; unbreakable
919 `(,column 0 ,chars)))))))))
920
921 (match (string-fold maybe-break
922 `(,column 0 ())
923 str)
924 ((column newlines chars)
925 (list->string (reverse chars)))))
926
927 \f
928 ;;;
929 ;;; Packages.
930 ;;;
931
932 (define %text-width
933 (make-parameter (terminal-columns)))
934
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.
938 (lambda strings
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))))
943
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))))
950
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_)))
955
956 (define (package-description-string package)
957 "Return a plain-text representation of PACKAGE description field."
958 (package-field-string package package-description))
959
960 (define (package-synopsis-string package)
961 "Return a plain-text representation of PACKAGE synopsis field."
962 (package-field-string package package-synopsis))
963
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
967 `recutils' syntax."
968 (list->string
969 (string-fold-right (lambda (chr result)
970 (if (eqv? chr #\newline)
971 (cons* chr #\+ #\space result)
972 (cons chr result)))
973 '()
974 str)))
975
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."
980 (define width*
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))
984
985 (define (dependencies->recutils packages)
986 (let ((list (string-join (delete-duplicates
987 (map package-full-name
988 (sort packages package<?))) " ")))
989 (string->recutils
990 (fill-paragraph list width*
991 (string-length "dependencies: ")))))
992
993 (define (package<? p1 p2)
994 (string<? (package-full-name p1) (package-full-name p2)))
995
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)
1008 (G_ "unknown")))
1009
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))
1013
1014 (format port "license: ~a~%"
1015 (match (package-license p)
1016 (((? license? licenses) ...)
1017 (string-join (map license-name licenses)
1018 ", "))
1019 ((? license? license)
1020 (license-name license))
1021 (x
1022 (G_ "unknown"))))
1023 (format port "synopsis: ~a~%"
1024 (string-map (match-lambda
1025 (#\newline #\space)
1026 (chr chr))
1027 (or (and=> (package-synopsis-string p) P_)
1028 "")))
1029 (format port "~a~%"
1030 (string->recutils
1031 (string-trim-right
1032 (parameterize ((%text-width width*))
1033 (texi->plain-text
1034 (string-append "description: "
1035 (or (and=> (package-description p) P_)
1036 ""))))
1037 #\newline)))
1038 (for-each (match-lambda
1039 ((field . value)
1040 (let ((field (symbol->string field)))
1041 (format port "~a: ~a~%"
1042 field
1043 (fill-paragraph (object->string value) width*
1044 (string-length field))))))
1045 extra-fields)
1046 (newline port))
1047
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)))
1053 (and (integer? x)
1054 x)))
1055
1056 (define (maybe-comma-separated-integers)
1057 (let ((lst (delete-duplicates
1058 (map string->number
1059 (string-split str #\,)))))
1060 (and (every integer? lst)
1061 lst)))
1062
1063 (cond ((maybe-integer)
1064 =>
1065 list)
1066 ((maybe-comma-separated-integers)
1067 =>
1068 identity)
1069 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
1070 =>
1071 (lambda (match)
1072 (let ((s (string->number (match:substring match 1)))
1073 (e (string->number (match:substring match 2))))
1074 (and (every integer? (list s e))
1075 (<= s e)
1076 (iota (1+ (- e s)) s)))))
1077 ((string-match "^([0-9]+)\\.\\.$" str)
1078 =>
1079 (lambda (match)
1080 (let ((s (string->number (match:substring match 1))))
1081 (and (integer? s)
1082 `(>= ,s)))))
1083 ((string-match "^\\.\\.([0-9]+)$" str)
1084 =>
1085 (lambda (match)
1086 (let ((e (string->number (match:substring match 1))))
1087 (and (integer? e)
1088 `(<= ,e)))))
1089 (else #f)))
1090
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)))))
1097
1098 (cond ((string-match "^([0-9]+)s$" str)
1099 =>
1100 (lambda (match)
1101 (make-time time-duration 0
1102 (string->number (match:substring match 1)))))
1103 ((string-match "^([0-9]+)h$" str)
1104 =>
1105 (lambda (match)
1106 (hours->duration 1 match)))
1107 ((string-match "^([0-9]+)d$" str)
1108 =>
1109 (lambda (match)
1110 (hours->duration 24 match)))
1111 ((string-match "^([0-9]+)w$" str)
1112 =>
1113 (lambda (match)
1114 (hours->duration (* 24 7) match)))
1115 ((string-match "^([0-9]+)m$" str)
1116 =>
1117 (lambda (match)
1118 (hours->duration (* 24 30) match)))
1119 (else #f)))
1120
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)))
1130
1131 (fold-right (lambda (x acc)
1132 (if (valid-generation? x)
1133 (cons x acc)
1134 acc))
1135 '()
1136 lst))
1137
1138 (define (filter-generations generations)
1139 (match generations
1140 (() '())
1141 (('>= n)
1142 (drop-while (cut > n <>)
1143 (generation-numbers profile)))
1144 (('<= n)
1145 (valid-generations (iota n 1)))
1146 ((lst ..1)
1147 (valid-generations lst))
1148 (x #f)))
1149
1150 (define (filter-by-duration duration)
1151 (define (time-at-midnight time)
1152 ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and
1153 ;; hours to zeros.
1154 (let ((d (time-utc->date time)))
1155 (date->time-utc
1156 (make-date 0 0 0 0
1157 (date-day d) (date-month d)
1158 (date-year d) (date-zone-offset d)))))
1159
1160 (define generation-ctime-alist
1161 (map (lambda (number)
1162 (cons number
1163 (time-second
1164 (time-at-midnight
1165 (generation-time profile number)))))
1166 (generation-numbers profile)))
1167
1168 (match duration
1169 (#f #f)
1170 (res
1171 (let ((s (time-second
1172 (subtract-duration (time-at-midnight (current-time))
1173 duration))))
1174 (delete #f (map (lambda (x)
1175 (and (duration-relation s (cdr x))
1176 (first x)))
1177 generation-ctime-alist))))))
1178
1179 (cond ((string->generations str)
1180 =>
1181 filter-generations)
1182 ((string->duration str)
1183 =>
1184 filter-by-duration)
1185 (else #f)))
1186
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
1191 (date->string
1192 (time-utc->date
1193 (generation-time profile number))
1194 "~b ~d ~Y ~T")))
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)))))
1202
1203 (define (display-profile-content-diff profile gen1 gen2)
1204 "Display the changed packages in PROFILE GEN2 compared to generation GEN2."
1205
1206 (define (equal-entry? first second)
1207 (string= (manifest-entry-item first) (manifest-entry-item second)))
1208
1209 (define (display-entry entry prefix)
1210 (match entry
1211 (($ <manifest-entry> name version output location _)
1212 (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location))))
1213
1214 (define (list-entries number)
1215 (manifest-entries (profile-manifest (generation-file-name profile number))))
1216
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)
1225 (newline)))
1226
1227 (display-diff profile gen1 gen2))
1228
1229 (define (display-profile-content profile number)
1230 "Display the packages in PROFILE, generation NUMBER, in a human-readable
1231 way."
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)))
1236
1237 ;; Show most recently installed packages last.
1238 (reverse
1239 (manifest-entries
1240 (profile-manifest (generation-file-name profile number))))))
1241
1242 (define (display-generation-change previous current)
1243 (format #t (G_ "switched from generation ~a to ~a~%") previous current))
1244
1245 (define (roll-back* store profile)
1246 "Like 'roll-back', but display what is happening."
1247 (call-with-values
1248 (lambda ()
1249 (roll-back store profile))
1250 display-generation-change))
1251
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)))
1256
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))
1262
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:
1268
1269 guile
1270 guile@2.0.9
1271 guile:debug
1272 guile@2.0.9:debug
1273 "
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))))))
1279 ((name version)
1280 (package-name->name+version name)))
1281 (values name version sub-drv)))
1282
1283 \f
1284 ;;;
1285 ;;; Command-line option processing.
1286 ;;;
1287
1288 (define (show-guix-usage)
1289 (format (current-error-port)
1290 (G_ "Try `guix --help' for more information.~%"))
1291 (exit 1))
1292
1293 (define (command-files)
1294 "Return the list of source files that define Guix sub-commands."
1295 (define directory
1296 (and=> (search-path %load-path "guix.scm")
1297 (compose (cut string-append <> "/guix/scripts")
1298 dirname)))
1299
1300 (define dot-scm?
1301 (cut string-suffix? ".scm" <>))
1302
1303 (if directory
1304 (scandir directory dot-scm?)
1305 '()))
1306
1307 (define (commands)
1308 "Return the list of Guix command names."
1309 (map (compose (cut string-drop-right <> 4)
1310 basename)
1311 (command-files)))
1312
1313 (define (show-guix-help)
1314 (define (internal? command)
1315 (member command '("substitute" "authenticate" "offload"
1316 "perform-download")))
1317
1318 (format #t (G_ "Usage: guix COMMAND ARGS...
1319 Run COMMAND with ARGS.\n"))
1320 (newline)
1321 (format #t (G_ "COMMAND must be one of the sub-commands listed below:\n"))
1322 (newline)
1323 ;; TODO: Display a synopsis of each command.
1324 (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
1325 string<?))
1326 (show-bug-report-information))
1327
1328 (define program-name
1329 ;; Name of the command-line program currently executing, or #f.
1330 (make-parameter #f))
1331
1332 (define (run-guix-command command . args)
1333 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
1334 found."
1335 (define module
1336 (catch 'misc-error
1337 (lambda ()
1338 (resolve-interface `(guix scripts ,command)))
1339 (lambda -
1340 (format (current-error-port)
1341 (G_ "guix: ~a: command not found~%") command)
1342 (show-guix-usage))))
1343
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))
1349 (dynamic-wind
1350 (const #f)
1351 (lambda ()
1352 (apply command-main args))
1353 (lambda ()
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)))))))
1357
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? "-" <>))
1363
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"))
1367
1368 (match args
1369 (()
1370 (format (current-error-port)
1371 (G_ "guix: missing command name~%"))
1372 (show-guix-usage))
1373 ((or ("-h") ("--help"))
1374 (show-guix-help))
1375 (("--version")
1376 (show-version-and-exit "guix"))
1377 (((? option? o) args ...)
1378 (format (current-error-port)
1379 (G_ "guix: unrecognized option '~a'~%") o)
1380 (show-guix-usage))
1381 (("help" command)
1382 (apply run-guix-command (string->symbol command)
1383 '("--help")))
1384 (("help" args ...)
1385 (show-guix-help))
1386 ((command args ...)
1387 (apply run-guix-command
1388 (string->symbol command)
1389 args))))
1390
1391 (define guix-warning-port
1392 (make-parameter (current-warning-port)))
1393
1394 (define (guix-main arg0 . args)
1395 (initialize-guix)
1396 (apply run-guix args))
1397
1398 ;;; ui.scm ends here