guix package: Always upgrade packages that have propagated inputs.
[jackhill/guix/guix.git] / guix / ui.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
74d862e8 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
e49951eb 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
98eb8cbe 4;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
88981dd3
AK
5;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
6;;; Copyright © 2014 Cyrill Schenkel <cyrill.schenkel@gmail.com>
689db38e 7;;; Copyright © 2014, 2015, 2017 Alex Kost <alezost@gmail.com>
88981dd3 8;;; Copyright © 2015 David Thompson <davet@gnu.org>
cf5f2ad3 9;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
e95ae7c2
RJ
10;;; Copyright © 2016 Roel Janssen <roel@gnu.org>
11;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch>
073c34d7 12;;;
233e7676 13;;; This file is part of GNU Guix.
073c34d7 14;;;
233e7676 15;;; GNU Guix is free software; you can redistribute it and/or modify it
073c34d7
LC
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;;;
233e7676 20;;; GNU Guix is distributed in the hope that it will be useful, but
073c34d7
LC
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
233e7676 26;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
073c34d7
LC
27
28(define-module (guix ui)
3e43166f 29 #:use-module (guix gexp)
073c34d7
LC
30 #:use-module (guix utils)
31 #:use-module (guix store)
cdd5d6f9 32 #:use-module (guix config)
073c34d7 33 #:use-module (guix packages)
5d7a8584 34 #:use-module (guix profiles)
9bb2b96a 35 #:use-module (guix derivations)
958dd3ce 36 #:use-module (guix combinators)
b7071bc5
LC
37 #:use-module (guix build-system)
38 #:use-module (guix serialization)
59758816 39 #:use-module ((guix build utils) #:select (mkdir-p))
299112d3 40 #:use-module ((guix licenses) #:select (license? license-name))
dcfc6f21
LC
41 #:use-module ((guix build syscalls)
42 #:select (free-disk-space terminal-columns))
299112d3
LC
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-11)
2cd09108 45 #:use-module (srfi srfi-19)
073c34d7 46 #:use-module (srfi srfi-26)
2abcc97f 47 #:use-module (srfi srfi-31)
073c34d7 48 #:use-module (srfi srfi-34)
c1d52c71 49 #:use-module (srfi srfi-35)
e31ff8b8 50 #:autoload (ice-9 ftw) (scandir)
64fc89b6 51 #:use-module (ice-9 match)
9bb2b96a 52 #:use-module (ice-9 format)
2cd09108 53 #:use-module (ice-9 regex)
db030303
LC
54 #:autoload (system repl repl) (start-repl)
55 #:autoload (system repl debug) (make-debug stack->vector)
1cd4027c
ML
56 #:use-module (texinfo)
57 #:use-module (texinfo plain-text)
58 #:use-module (texinfo string-utils)
69daee23 59 #:export (G_
073c34d7 60 N_
ee764179 61 P_
70e629f5 62 report-error
073c34d7 63 leave
7ea1432e
DT
64 make-user-module
65 load*
4ae7559f 66 warn-about-load-error
cdd5d6f9 67 show-version-and-exit
3441e164 68 show-bug-report-information
fd688c82 69 make-regexp*
969e678e 70 string->number*
1d6243cf 71 size->number
fa394eb9 72 show-derivation-outputs
9bb2b96a 73 show-what-to-build
4d043ab6 74 show-what-to-build*
5d7a8584 75 show-manifest-transaction
073c34d7 76 call-with-error-handling
64fc89b6 77 with-error-handling
df36e629 78 leave-on-EPIPE
ac5de156 79 read/eval
eb0880e7 80 read/eval-package-expression
299112d3 81 location->string
f651b477 82 config-directory
299112d3 83 fill-paragraph
2748ee3b 84 texi->plain-text
1cd4027c 85 package-description-string
689db38e 86 package-synopsis-string
299112d3 87 string->recutils
e49951eb 88 package->recutils
2876b989 89 package-specification->name+version+output
2cd09108
NK
90 string->generations
91 string->duration
e49de93a 92 matching-generations
ad18c7e6
LC
93 display-generation
94 display-profile-content
e95ae7c2 95 display-profile-content-diff
06d45f45
LC
96 roll-back*
97 switch-to-generation*
98 delete-generation*
e49951eb 99 run-guix-command
caa6732e 100 run-guix
a2011be5
LC
101 program-name
102 guix-warning-port
103 warning
240b57f0 104 info
e49951eb 105 guix-main))
073c34d7
LC
106
107;;; Commentary:
108;;;
109;;; User interface facilities for command-line tools.
110;;;
111;;; Code:
112
113(define %gettext-domain
ee764179 114 ;; Text domain for strings used in the tools.
073c34d7
LC
115 "guix")
116
ee764179
LC
117(define %package-text-domain
118 ;; Text domain for package synopses and descriptions.
119 "guix-packages")
120
69daee23 121(define G_ (cut gettext <> %gettext-domain))
073c34d7 122(define N_ (cut ngettext <> <> <> %gettext-domain))
a5c0d8bc
LC
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)))
073c34d7 132
b2a886f6
LC
133(define-syntax-rule (define-diagnostic name prefix)
134 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
135messages."
136 (define-syntax name
137 (lambda (x)
138 (define (augmented-format-string fmt)
139 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
140
89d02b98
LC
141 (syntax-case x ()
142 ((name (underscore fmt) args (... ...))
143 (and (string? (syntax->datum #'fmt))
69daee23 144 (free-identifier=? #'underscore #'G_))
b2a886f6
LC
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 (... ...))))
89d02b98 150 ((name (N-underscore singular plural n) args (... ...))
b2a886f6 151 (and (string? (syntax->datum #'singular))
89d02b98
LC
152 (string? (syntax->datum #'plural))
153 (free-identifier=? #'N-underscore #'N_))
b2a886f6
LC
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
240b57f0 163(define-diagnostic info "")
b2a886f6
LC
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
7ea1432e
DT
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
db030303
LC
181(define* (load* file user-module
182 #:key (on-error 'nothing-special))
7ea1432e 183 "Load the user provided Scheme source code FILE."
2abcc97f
LC
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
db030303
LC
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
7ea1432e
DT
202 (catch #t
203 (lambda ()
2abcc97f 204 ;; XXX: Force a recompilation to avoid ABI issues.
7ea1432e 205 (set! %fresh-auto-compile #t)
2abcc97f 206 (set! %load-should-auto-compile #t)
7ea1432e
DT
207
208 (save-module-excursion
209 (lambda ()
210 (set-current-module user-module)
7ea1432e 211
2abcc97f
LC
212 ;; Hide the "auto-compiling" messages.
213 (parameterize ((current-warning-port (%make-void-port "w")))
db030303
LC
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))))))
2abcc97f
LC
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'.
db030303 229 (let* ((stack (make-stack #t handle-error tag))
2abcc97f
LC
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))))
db030303
LC
236
237 (report-load-error file args frame)
238
239 (case on-error
240 ((debug)
241 (newline)
69daee23 242 (display (G_ "entering debugger; type ',bt' for a backtrace\n"))
db030303
LC
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))))))
2abcc97f
LC
251
252(define* (report-load-error file args #:optional frame)
db030303 253 "Report the failure to load FILE, a user-provided Scheme file.
1151f6ae
LC
254ARGS is the list of arguments received by the 'throw' handler."
255 (match args
e465d9e1 256 (('system-error . rest)
1151f6ae 257 (let ((err (system-error-errno args)))
69daee23 258 (report-error (G_ "failed to load '~a': ~a~%") file (strerror err))))
1151f6ae
LC
259 (('syntax-error proc message properties form . rest)
260 (let ((loc (source-properties->location properties)))
69daee23 261 (format (current-error-port) (G_ "~a: error: ~a~%")
db030303 262 (location->string loc) message)))
23185cea 263 (('srfi-34 obj)
f816dba6 264 (if (message-condition? obj)
69daee23 265 (report-error (G_ "~a~%")
f816dba6
LC
266 (gettext (condition-message obj)
267 %gettext-domain))
69daee23 268 (report-error (G_ "exception thrown: ~s~%") obj)))
1151f6ae 269 ((error args ...)
69daee23 270 (report-error (G_ "failed to load '~a':~%") file)
db030303 271 (apply display-error frame (current-error-port) args))))
1151f6ae 272
4ae7559f
LC
273(define (warn-about-load-error file args) ;FIXME: factorize with ↑
274 "Report the failure to load FILE, a user-provided Scheme file, without
275exiting. ARGS is the list of arguments received by the 'throw' handler."
276 (match args
e465d9e1 277 (('system-error . rest)
4ae7559f 278 (let ((err (system-error-errno args)))
69daee23 279 (warning (G_ "failed to load '~a': ~a~%") file (strerror err))))
4ae7559f
LC
280 (('syntax-error proc message properties form . rest)
281 (let ((loc (source-properties->location properties)))
69daee23 282 (format (current-error-port) (G_ "~a: warning: ~a~%")
4ae7559f 283 (location->string loc) message)))
23185cea 284 (('srfi-34 obj)
f816dba6 285 (if (message-condition? obj)
69daee23 286 (warning (G_ "failed to load '~a': ~a~%")
f816dba6
LC
287 file
288 (gettext (condition-message obj) %gettext-domain))
69daee23 289 (warning (G_ "failed to load '~a': exception thrown: ~s~%")
f816dba6 290 file obj)))
4ae7559f 291 ((error args ...)
69daee23 292 (warning (G_ "failed to load '~a':~%") file)
4ae7559f
LC
293 (apply display-error #f (current-error-port) args))))
294
b2a886f6
LC
295(define (install-locale)
296 "Install the current locale settings."
297 (catch 'system-error
298 (lambda _
299 (setlocale LC_ALL ""))
300 (lambda args
69daee23 301 (warning (G_ "failed to install locale: ~a~%")
b2a886f6
LC
302 (strerror (system-error-errno args))))))
303
e49951eb 304(define (initialize-guix)
633f045f 305 "Perform the usual initialization for stand-alone Guix commands."
ce92d269
LC
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
e49951eb 313 (install-locale)
39e9f95d 314 (textdomain %gettext-domain)
e14c3929
LC
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
e49951eb
MW
320 (setvbuf (current-output-port) _IOLBF)
321 (setvbuf (current-error-port) _IOLBF))
322
cdd5d6f9
LC
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)
d925cdc3
LC
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. */
69daee23
LC
331 (G_ "(C)")
332 (G_ "the Guix authors\n"))
333 (display (G_"\
64a967cc
LC
334License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
335This is free software: you are free to change and redistribute it.
336There is NO WARRANTY, to the extent permitted by law.
337"))
cdd5d6f9
LC
338 (exit 0))
339
3441e164 340(define (show-bug-report-information)
cf5f2ad3
ML
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).
69daee23 345 (format #t (G_ "
3441e164 346Report bugs to: ~a.") %guix-bug-report-address)
69daee23 347 (format #t (G_ "
3441e164 348~a home page: <~a>") %guix-package-name %guix-home-page-url)
69daee23 349 (display (G_ "
3441e164
LC
350General help using GNU software: <http://www.gnu.org/gethelp/>"))
351 (newline))
352
e7ff0543
LC
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
365FILE."
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
7522a016
LC
373(set! symlink
374 ;; We 'set!' the global binding because (gnu build ...) modules and similar
375 ;; typically don't use (guix ui).
e7ff0543 376 (error-reporting-wrapper symlink (source target) target))
44fd6ef1 377
9b14107f
LC
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).
e7ff0543
LC
381 (error-reporting-wrapper copy-file (source target) target))
382
6d30b1b2
LC
383(set! canonicalize-path
384 (error-reporting-wrapper canonicalize-path (file) file))
385
9b14107f 386
fd688c82
LC
387(define (make-regexp* regexp . flags)
388 "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error
389nicely."
390 (catch 'regular-expression-syntax
391 (lambda ()
392 (apply make-regexp regexp flags))
393 (lambda (key proc message . rest)
69daee23 394 (leave (G_ "'~a' is not a valid regular expression: ~a~%")
fd688c82
LC
395 regexp message))))
396
969e678e
LC
397(define (string->number* str)
398 "Like `string->number', but error out with an error message on failure."
399 (or (string->number str)
69daee23 400 (leave (G_ "~a: invalid number~%") str)))
969e678e 401
1d6243cf
LC
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
405interpreted."
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
69daee23 417 (leave (G_ "invalid number: ~a~%") numstr))
1d6243cf
LC
418
419 ((compose inexact->exact round)
420 (* num
421 (match unit
4a44d7bb
LC
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))
1d6243cf
LC
431 ("MB" (expt 10 6))
432 ("GB" (expt 10 9))
433 ("TB" (expt 10 12))
4a44d7bb
LC
434 ("PB" (expt 10 15))
435 ("EB" (expt 10 18))
436 ("ZB" (expt 10 21))
437 ("YB" (expt 10 24))
1d6243cf 438 ("" 1)
e465d9e1 439 (x
69daee23 440 (leave (G_ "unknown unit: ~a~%") unit)))))))
1d6243cf 441
073c34d7
LC
442(define (call-with-error-handling thunk)
443 "Call THUNK within a user-friendly error handler."
bec7f352
LC
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
073c34d7
LC
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)))
69daee23 457 (leave (G_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
073c34d7
LC
458 file line column
459 (package-full-name package) input)))
9b222abe
LC
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)))
69daee23 464 (leave (G_ "~a: ~a: build system `~a' does not support cross builds~%")
9b222abe
LC
465 (location->string loc)
466 (package-full-name package)
467 (build-system-name system))))
3e43166f
LC
468 ((gexp-input-error? c)
469 (let ((input (package-error-invalid-input c)))
69daee23 470 (leave (G_ "~s: invalid G-expression input~%")
3e43166f 471 (gexp-error-invalid-input c))))
c0c018f1 472 ((profile-not-found-error? c)
69daee23 473 (leave (G_ "profile '~a' does not exist~%")
c0c018f1
AK
474 (profile-error-profile c)))
475 ((missing-generation-error? c)
69daee23 476 (leave (G_ "generation ~a of profile '~a' does not exist~%")
c0c018f1
AK
477 (missing-generation-error-generation c)
478 (profile-error-profile c)))
b7071bc5
LC
479 ((nar-error? c)
480 (let ((file (nar-error-file c))
481 (port (nar-error-port c)))
482 (if file
69daee23 483 (leave (G_ "corrupt input while restoring '~a' from ~s~%")
bec7f352 484 file (or (port-filename* port) port))
69daee23 485 (leave (G_ "corrupt input while restoring archive from ~s~%")
bec7f352 486 (or (port-filename* port) port)))))
ef86c39f 487 ((nix-connection-error? c)
69daee23 488 (leave (G_ "failed to connect to `~a': ~a~%")
ef86c39f
LC
489 (nix-connection-error-file c)
490 (strerror (nix-connection-error-code c))))
073c34d7
LC
491 ((nix-protocol-error? c)
492 ;; FIXME: Server-provided error messages aren't i18n'd.
69daee23 493 (leave (G_ "build failed: ~a~%")
c1d52c71 494 (nix-protocol-error-message c)))
f304c9c2 495 ((derivation-missing-output-error? c)
69daee23 496 (leave (G_ "reference to invalid output '~a' of derivation '~a'~%")
f304c9c2
LC
497 (derivation-missing-output c)
498 (derivation-file-name (derivation-error-derivation c))))
d26e1967 499 ((file-search-error? c)
69daee23 500 (leave (G_ "file '~a' could not be found in these \
d26e1967
LC
501directories:~{ ~a~}~%")
502 (file-search-error-file-name c)
503 (file-search-error-search-path c)))
c1d52c71
LC
504 ((message-condition? c)
505 ;; Normally '&message' error conditions have an i18n'd message.
69daee23 506 (leave (G_ "~a~%")
12703d08 507 (gettext (condition-message c) %gettext-domain))))
e14c3929
LC
508 ;; Catch EPIPE and the likes.
509 (catch 'system-error
510 thunk
bde8c0e6 511 (lambda (key proc format-string format-args . rest)
69daee23 512 (leave (G_ "~a: ~a~%") proc
bde8c0e6 513 (apply format #f format-string format-args))))))
073c34d7 514
df36e629
LC
515(define-syntax-rule (leave-on-EPIPE exp ...)
516 "Run EXP... in a context when EPIPE errors are caught and lead to 'exit'
517with successful exit code. This is useful when writing to the standard output
518may lead to EPIPE, because the standard output is piped through 'head' or
519similar."
520 (catch 'system-error
521 (lambda ()
522 exp ...)
523 (lambda args
524 ;; We really have to exit this brutally, otherwise Guile eventually
525 ;; attempts to flush all the ports, leading to an uncaught EPIPE down
526 ;; the path.
527 (if (= EPIPE (system-error-errno args))
528 (primitive-_exit 0)
529 (apply throw args)))))
530
56b82106
LC
531(define %guix-user-module
532 ;; Module in which user expressions are evaluated.
07254feb
LC
533 ;; Compute lazily to avoid circularity with (guix gexp).
534 (delay
535 (let ((module (make-module)))
536 (beautify-user-module! module)
537 ;; Use (guix gexp) so that one can use #~ & co.
538 (module-use! module (resolve-interface '(guix gexp)))
539 module)))
56b82106 540
ac5de156
LC
541(define (read/eval str)
542 "Read and evaluate STR, raising an error if something goes wrong."
eb0880e7
LC
543 (let ((exp (catch #t
544 (lambda ()
545 (call-with-input-string str read))
546 (lambda args
69daee23 547 (leave (G_ "failed to read expression ~s: ~s~%")
eb0880e7 548 str args)))))
ac5de156
LC
549 (catch #t
550 (lambda ()
07254feb 551 (eval exp (force %guix-user-module)))
ac5de156 552 (lambda args
69daee23 553 (report-error (G_ "failed to evaluate expression '~a':~%") exp)
41766807
LC
554 (match args
555 (('syntax-error proc message properties form . rest)
69daee23 556 (report-error (G_ "syntax error: ~a~%") message))
23185cea 557 (('srfi-34 obj)
f816dba6 558 (if (message-condition? obj)
69daee23 559 (report-error (G_ "~a~%")
f816dba6
LC
560 (gettext (condition-message obj)
561 %gettext-domain))
69daee23 562 (report-error (G_ "exception thrown: ~s~%") obj)))
41766807
LC
563 ((error args ...)
564 (apply display-error #f (current-error-port) args))
565 (what? #f))
566 (exit 1)))))
ac5de156
LC
567
568(define (read/eval-package-expression str)
569 "Read and evaluate STR and return the package it refers to, or exit an
570error."
571 (match (read/eval str)
572 ((? package? p) p)
e465d9e1 573 (x
69daee23 574 (leave (G_ "expression ~s does not evaluate to a package~%")
ac5de156 575 str))))
eb0880e7 576
fa394eb9
AK
577(define (show-derivation-outputs derivation)
578 "Show the output file names of DERIVATION."
579 (format #t "~{~a~%~}"
580 (map (match-lambda
581 ((out-name . out)
582 (derivation->output-path derivation out-name)))
583 (derivation-outputs derivation))))
584
dcfc6f21
LC
585(define (check-available-space need)
586 "Make sure at least NEED bytes are available in the store. Otherwise emit a
587warning."
588 (let ((free (catch 'system-error
589 (lambda ()
590 (free-disk-space (%store-prefix)))
591 (const #f))))
592 (when (and free (>= need free))
593 (warning (G_ "at least ~,1h MB needed but only ~,1h MB available in ~a~%")
594 (/ need 1e6) (/ free 1e6) (%store-prefix)))))
595
dd36b51b 596(define* (show-what-to-build store drv
58c08df0
LC
597 #:key dry-run? (use-substitutes? #t)
598 (mode (build-mode normal)))
9bb2b96a 599 "Show what will or would (depending on DRY-RUN?) be built in realizing the
58c08df0
LC
600derivations listed in DRV using MODE, a 'build-mode' value. Return #t if
601there's something to build, #f otherwise. When USE-SUBSTITUTES?, check and
602report what is prerequisites are available for download."
2dc98729 603 (define substitutable-info
e9651e39
LC
604 ;; Call 'substitutation-oracle' upfront so we don't end up launching the
605 ;; substituter many times. This makes a big difference, especially when
606 ;; DRV is a long list as is the case with 'guix environment'.
607 (if use-substitutes?
58c08df0 608 (substitution-oracle store drv #:mode mode)
e9651e39
LC
609 (const #f)))
610
52ddf2ae 611 (define (built-or-substitutable? drv)
f304c9c2
LC
612 (or (null? (derivation-outputs drv))
613 (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists
52ddf2ae 614 (or (valid-path? store out)
2dc98729 615 (substitutable-info out)))))
52ddf2ae 616
dd36b51b 617 (let*-values (((build download)
59688fc4
LC
618 (fold2 (lambda (drv build download)
619 (let-values (((b d)
620 (derivation-prerequisites-to-build
621 store drv
58c08df0 622 #:mode mode
2dc98729
LC
623 #:substitutable-info
624 substitutable-info)))
59688fc4
LC
625 (values (append b build)
626 (append d download))))
dd36b51b
LC
627 '() '()
628 drv))
629 ((build) ; add the DRV themselves
630 (delete-duplicates
59688fc4 631 (append (map derivation-file-name
52ddf2ae 632 (remove built-or-substitutable? drv))
dd36b51b
LC
633 (map derivation-input-path build))))
634 ((download) ; add the references of DOWNLOAD
1a8b7834
LC
635 (if use-substitutes?
636 (delete-duplicates
637 (append download
2dc98729
LC
638 (filter-map (lambda (item)
639 (if (valid-path? store item)
640 #f
641 (substitutable-info item)))
642 (append-map
643 substitutable-references
644 download))))
1a8b7834 645 download)))
dcfc6f21
LC
646 (define installed-size
647 (reduce + 0 (map substitutable-nar-size download)))
648
d9bad2f0
LC
649 (define download-size
650 (/ (reduce + 0 (map substitutable-download-size download))
651 1e6))
652
653 (define display-download-size?
654 ;; Sometimes narinfos lack information about the download size. Only
655 ;; display when we have information for all of DOWNLOAD.
656 (not (any (compose zero? substitutable-download-size) download)))
657
9bb2b96a 658 (if dry-run?
dd36b51b
LC
659 (begin
660 (format (current-error-port)
83e61a73
LC
661 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
662 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
dd36b51b
LC
663 (length build))
664 (null? build) build)
d9bad2f0
LC
665 (if display-download-size?
666 (format (current-error-port)
667 ;; TRANSLATORS: "MB" is for "megabyte"; it should be
668 ;; translated to the corresponding abbreviation.
669 (G_ "~:[~,1h MB would be downloaded:~%~{ ~a~%~}~;~]")
670 (null? download)
671 download-size
672 (map substitutable-path download))
673 (format (current-error-port)
674 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
675 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
676 (length download))
677 (null? download)
678 (map substitutable-path download))))
dd36b51b
LC
679 (begin
680 (format (current-error-port)
83e61a73
LC
681 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
682 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
dd36b51b
LC
683 (length build))
684 (null? build) build)
d9bad2f0
LC
685 (if display-download-size?
686 (format (current-error-port)
687 ;; TRANSLATORS: "MB" is for "megabyte"; it should be
688 ;; translated to the corresponding abbreviation.
689 (G_ "~:[~,1h MB will be downloaded:~%~{ ~a~%~}~;~]")
690 (null? download)
691 download-size
692 (map substitutable-path download))
693 (format (current-error-port)
694 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
695 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
696 (length download))
697 (null? download)
698 (map substitutable-path download)))))
dcfc6f21
LC
699
700 (check-available-space installed-size)
701
dd36b51b 702 (pair? build)))
9bb2b96a 703
4d043ab6
DT
704(define show-what-to-build*
705 (store-lift show-what-to-build))
706
5d7a8584
AK
707(define (right-arrow port)
708 "Return either a string containing the 'RIGHT ARROW' character, or an ASCII
709replacement if PORT is not Unicode-capable."
1062063a
LC
710 (let ((encoding (port-encoding port))
711 (arrow "→"))
712 (catch 'encoding-error
713 (lambda ()
714 (call-with-output-string
715 (lambda (port)
716 (set-port-encoding! port encoding)
717 (set-port-conversion-strategy! port 'error)
718 (display arrow port))))
719 (lambda (key . args)
720 "->"))))
5d7a8584
AK
721
722(define* (show-manifest-transaction store manifest transaction
723 #:key dry-run?)
724 "Display what will/would be installed/removed from MANIFEST by TRANSACTION."
725 (define (package-strings name version output item)
726 (map (lambda (name version output item)
727 (format #f " ~a~:[:~a~;~*~]\t~a\t~a"
728 name
729 (equal? output "out") output version
730 (if (package? item)
731 (package-output store item output)
732 item)))
733 name version output item))
734
735 (define → ;an arrow that can be represented on stderr
736 (right-arrow (current-error-port)))
737
738 (define (upgrade-string name old-version new-version output item)
739 (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a"
740 name (equal? output "out") output
741 old-version → new-version
742 (if (package? item)
743 (package-output store item output)
744 item)))
745
46b23e1a 746 (let-values (((remove install upgrade downgrade)
5d7a8584
AK
747 (manifest-transaction-effects manifest transaction)))
748 (match remove
749 ((($ <manifest-entry> name version output item) ..1)
750 (let ((len (length name))
751 (remove (package-strings name version output item)))
752 (if dry-run?
753 (format (current-error-port)
754 (N_ "The following package would be removed:~%~{~a~%~}~%"
755 "The following packages would be removed:~%~{~a~%~}~%"
756 len)
757 remove)
758 (format (current-error-port)
759 (N_ "The following package will be removed:~%~{~a~%~}~%"
760 "The following packages will be removed:~%~{~a~%~}~%"
761 len)
762 remove))))
30906825 763 (x #f))
46b23e1a
LC
764 (match downgrade
765 (((($ <manifest-entry> name old-version)
766 . ($ <manifest-entry> _ new-version output item)) ..1)
767 (let ((len (length name))
768 (downgrade (map upgrade-string
769 name old-version new-version output item)))
770 (if dry-run?
771 (format (current-error-port)
772 (N_ "The following package would be downgraded:~%~{~a~%~}~%"
773 "The following packages would be downgraded:~%~{~a~%~}~%"
774 len)
775 downgrade)
776 (format (current-error-port)
777 (N_ "The following package will be downgraded:~%~{~a~%~}~%"
778 "The following packages will be downgraded:~%~{~a~%~}~%"
779 len)
780 downgrade))))
30906825 781 (x #f))
5d7a8584
AK
782 (match upgrade
783 (((($ <manifest-entry> name old-version)
784 . ($ <manifest-entry> _ new-version output item)) ..1)
785 (let ((len (length name))
786 (upgrade (map upgrade-string
787 name old-version new-version output item)))
788 (if dry-run?
789 (format (current-error-port)
790 (N_ "The following package would be upgraded:~%~{~a~%~}~%"
791 "The following packages would be upgraded:~%~{~a~%~}~%"
792 len)
793 upgrade)
794 (format (current-error-port)
795 (N_ "The following package will be upgraded:~%~{~a~%~}~%"
796 "The following packages will be upgraded:~%~{~a~%~}~%"
797 len)
798 upgrade))))
30906825 799 (x #f))
5d7a8584
AK
800 (match install
801 ((($ <manifest-entry> name version output item _) ..1)
802 (let ((len (length name))
803 (install (package-strings name version output item)))
804 (if dry-run?
805 (format (current-error-port)
806 (N_ "The following package would be installed:~%~{~a~%~}~%"
807 "The following packages would be installed:~%~{~a~%~}~%"
808 len)
809 install)
810 (format (current-error-port)
811 (N_ "The following package will be installed:~%~{~a~%~}~%"
812 "The following packages will be installed:~%~{~a~%~}~%"
813 len)
814 install))))
30906825 815 (x #f))))
5d7a8584 816
073c34d7
LC
817(define-syntax with-error-handling
818 (syntax-rules ()
819 "Run BODY within a user-friendly error condition handler."
820 ((_ body ...)
821 (call-with-error-handling
822 (lambda ()
823 body ...)))))
824
64fc89b6
LC
825(define (location->string loc)
826 "Return a human-friendly, GNU-standard representation of LOC."
827 (match loc
69daee23 828 (#f (G_ "<unknown location>"))
64fc89b6
LC
829 (($ <location> file line column)
830 (format #f "~a:~a:~a" file line column))))
831
e06ca952 832(define* (config-directory #:key (ensure? #t))
f651b477 833 "Return the name of the configuration directory, after making sure that it
e06ca952 834exists if ENSURE? is true. Honor the XDG specs,
f651b477
LC
835<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
836 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
837 (and=> (getenv "HOME")
838 (cut string-append <> "/.config")))
839 (cut string-append <> "/guix"))))
840 (catch 'system-error
841 (lambda ()
e06ca952
LC
842 (when ensure?
843 (mkdir-p dir))
f651b477
LC
844 dir)
845 (lambda args
59758816
LC
846 (let ((err (system-error-errno args)))
847 ;; ERR is necessarily different from EEXIST.
69daee23 848 (leave (G_ "failed to create configuration directory `~a': ~a~%")
59758816 849 dir (strerror err)))))))
f651b477 850
299112d3
LC
851(define* (fill-paragraph str width #:optional (column 0))
852 "Fill STR such that each line contains at most WIDTH characters, assuming
853that the first character is at COLUMN.
854
855When STR contains a single line break surrounded by other characters, it is
856converted to a space; sequences of more than one line break are preserved."
857 (define (maybe-break chr result)
858 (match result
859 ((column newlines chars)
860 (case chr
861 ((#\newline)
862 `(,column ,(+ 1 newlines) ,chars))
863 (else
3a09e1d2
CS
864 (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1))
865 (chars (case newlines
866 ((0) chars)
867 ((1)
868 (append (make-list spaces #\space) chars))
869 (else
870 (append (make-list newlines #\newline) chars))))
871 (column (case newlines
872 ((0) column)
873 ((1) (+ spaces column))
874 (else 0))))
299112d3
LC
875 (let ((chars (cons chr chars))
876 (column (+ 1 column)))
877 (if (> column width)
878 (let*-values (((before after)
879 (break (cut eqv? #\space <>) chars))
880 ((len)
881 (length before)))
882 (if (<= len width)
883 `(,len
884 0
885 ,(if (null? after)
886 before
3a09e1d2
CS
887 (append before
888 (cons #\newline
889 (drop-while (cut eqv? #\space <>)
890 after)))))
299112d3
LC
891 `(,column 0 ,chars))) ; unbreakable
892 `(,column 0 ,chars)))))))))
893
894 (match (string-fold maybe-break
895 `(,column 0 ())
896 str)
30906825 897 ((column newlines chars)
299112d3
LC
898 (list->string (reverse chars)))))
899
2876b989
LC
900\f
901;;;
902;;; Packages.
903;;;
904
1cd4027c 905(define %text-width
9703fef4 906 (make-parameter (terminal-columns)))
1cd4027c
ML
907
908(set! (@@ (texinfo plain-text) wrap*)
909 ;; XXX: Monkey patch this private procedure to let 'package->recutils'
910 ;; parameterize the fill of description field correctly.
911 (lambda strings
912 (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*))))
913 (fill-string (string-concatenate strings)
914 #:line-width (%text-width) #:initial-indent indent
915 #:subsequent-indent indent))))
916
917(define (texi->plain-text str)
918 "Return a plain-text representation of texinfo fragment STR."
08d7e359
LC
919 ;; 'texi-fragment->stexi' uses a string port so make sure it's a
920 ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.)
921 (with-fluids ((%default-port-encoding "UTF-8"))
922 (stexi->plain-text (texi-fragment->stexi str))))
1cd4027c 923
689db38e
AK
924(define (package-field-string package field-accessor)
925 "Return a plain-text representation of PACKAGE field."
926 (and=> (field-accessor package)
927 (compose texi->plain-text P_)))
928
1cd4027c
ML
929(define (package-description-string package)
930 "Return a plain-text representation of PACKAGE description field."
689db38e
AK
931 (package-field-string package package-description))
932
933(define (package-synopsis-string package)
934 "Return a plain-text representation of PACKAGE synopsis field."
935 (package-field-string package package-synopsis))
1cd4027c 936
299112d3
LC
937(define (string->recutils str)
938 "Return a version of STR where newlines have been replaced by newlines
939followed by \"+ \", which makes for a valid multi-line field value in the
940`recutils' syntax."
941 (list->string
942 (string-fold-right (lambda (chr result)
943 (if (eqv? chr #\newline)
944 (cons* chr #\+ #\space result)
945 (cons chr result)))
946 '()
947 str)))
948
4ee79f22
LC
949(define* (package->recutils p port #:optional (width (%text-width))
950 #:key (extra-fields '()))
299112d3 951 "Write to PORT a `recutils' record of package P, arranging to fit within
4ee79f22 952WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to emit."
069d43a7
LC
953 (define width*
954 ;; The available number of columns once we've taken into account space for
955 ;; the initial "+ " prefix.
956 (if (> width 2) (- width 2) width))
957
20ffce82
LC
958 (define (dependencies->recutils packages)
959 (let ((list (string-join (map package-full-name
960 (sort packages package<?)) " ")))
961 (string->recutils
069d43a7 962 (fill-paragraph list width*
20ffce82
LC
963 (string-length "dependencies: ")))))
964
9c0fc279
CR
965 (define (package<? p1 p2)
966 (string<? (package-full-name p1) (package-full-name p2)))
967
299112d3
LC
968 ;; Note: Don't i18n field names so that people can post-process it.
969 (format port "name: ~a~%" (package-name p))
970 (format port "version: ~a~%" (package-version p))
6bf99bac 971 (format port "outputs: ~a~%" (string-join (package-outputs p)))
5763ad92
LC
972 (format port "systems: ~a~%"
973 (string-join (package-transitive-supported-systems p)))
9c0fc279
CR
974 (format port "dependencies: ~a~%"
975 (match (package-direct-inputs p)
5e6feee6
EB
976 (((labels inputs . _) ...)
977 (dependencies->recutils (filter package? inputs)))))
299112d3
LC
978 (format port "location: ~a~%"
979 (or (and=> (package-location p) location->string)
69daee23 980 (G_ "unknown")))
8fa3e6b3
LC
981
982 ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in
983 ;; field identifiers.
984 (format port "homepage: ~a~%" (package-home-page p))
985
299112d3
LC
986 (format port "license: ~a~%"
987 (match (package-license p)
988 (((? license? licenses) ...)
989 (string-join (map license-name licenses)
990 ", "))
991 ((? license? license)
992 (license-name license))
993 (x
69daee23 994 (G_ "unknown"))))
299112d3
LC
995 (format port "synopsis: ~a~%"
996 (string-map (match-lambda
4ee79f22
LC
997 (#\newline #\space)
998 (chr chr))
689db38e 999 (or (and=> (package-synopsis-string p) P_)
299112d3 1000 "")))
4ee79f22 1001 (format port "~a~%"
1cd4027c
ML
1002 (string->recutils
1003 (string-trim-right
069d43a7 1004 (parameterize ((%text-width width*))
1cd4027c
ML
1005 (texi->plain-text
1006 (string-append "description: "
1007 (or (and=> (package-description p) P_)
1008 ""))))
4ee79f22
LC
1009 #\newline)))
1010 (for-each (match-lambda
1011 ((field . value)
1012 (let ((field (symbol->string field)))
1013 (format port "~a: ~a~%"
1014 field
1015 (fill-paragraph (object->string value) width*
1016 (string-length field))))))
1017 extra-fields)
1018 (newline port))
299112d3 1019
2cd09108
NK
1020(define (string->generations str)
1021 "Return the list of generations matching a pattern in STR. This function
1022accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
1023 (define (maybe-integer)
1024 (let ((x (string->number str)))
1025 (and (integer? x)
1026 x)))
1027
1028 (define (maybe-comma-separated-integers)
1029 (let ((lst (delete-duplicates
1030 (map string->number
1031 (string-split str #\,)))))
1032 (and (every integer? lst)
1033 lst)))
1034
1035 (cond ((maybe-integer)
1036 =>
1037 list)
1038 ((maybe-comma-separated-integers)
1039 =>
1040 identity)
1041 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
1042 =>
1043 (lambda (match)
1044 (let ((s (string->number (match:substring match 1)))
1045 (e (string->number (match:substring match 2))))
1046 (and (every integer? (list s e))
1047 (<= s e)
1048 (iota (1+ (- e s)) s)))))
1049 ((string-match "^([0-9]+)\\.\\.$" str)
1050 =>
1051 (lambda (match)
1052 (let ((s (string->number (match:substring match 1))))
1053 (and (integer? s)
1054 `(>= ,s)))))
1055 ((string-match "^\\.\\.([0-9]+)$" str)
1056 =>
1057 (lambda (match)
1058 (let ((e (string->number (match:substring match 1))))
1059 (and (integer? e)
1060 `(<= ,e)))))
1061 (else #f)))
1062
1063(define (string->duration str)
1064 "Return the duration matching a pattern in STR. This function accepts the
1065following patterns: \"1d\", \"1w\", \"1m\"."
1066 (define (hours->duration hours match)
1067 (make-time time-duration 0
1068 (* 3600 hours (string->number (match:substring match 1)))))
1069
638c5b79
LC
1070 (cond ((string-match "^([0-9]+)s$" str)
1071 =>
1072 (lambda (match)
1073 (make-time time-duration 0
1074 (string->number (match:substring match 1)))))
1075 ((string-match "^([0-9]+)h$" str)
30d2397f 1076 =>
638c5b79
LC
1077 (lambda (match)
1078 (hours->duration 1 match)))
1079 ((string-match "^([0-9]+)d$" str)
2cd09108
NK
1080 =>
1081 (lambda (match)
1082 (hours->duration 24 match)))
1083 ((string-match "^([0-9]+)w$" str)
1084 =>
1085 (lambda (match)
1086 (hours->duration (* 24 7) match)))
1087 ((string-match "^([0-9]+)m$" str)
1088 =>
1089 (lambda (match)
1090 (hours->duration (* 24 30) match)))
1091 (else #f)))
1092
e49de93a
LC
1093(define* (matching-generations str profile
1094 #:key (duration-relation <=))
1095 "Return the list of available generations matching a pattern in STR. See
1096'string->generations' and 'string->duration' for the list of valid patterns.
1097When STR is a duration pattern, return all the generations whose ctime has
1098DURATION-RELATION with the current time."
1099 (define (valid-generations lst)
1100 (define (valid-generation? n)
1101 (any (cut = n <>) (generation-numbers profile)))
1102
1103 (fold-right (lambda (x acc)
1104 (if (valid-generation? x)
1105 (cons x acc)
1106 acc))
1107 '()
1108 lst))
1109
1110 (define (filter-generations generations)
1111 (match generations
1112 (() '())
1113 (('>= n)
1114 (drop-while (cut > n <>)
1115 (generation-numbers profile)))
1116 (('<= n)
1117 (valid-generations (iota n 1)))
1118 ((lst ..1)
1119 (valid-generations lst))
30906825 1120 (x #f)))
e49de93a
LC
1121
1122 (define (filter-by-duration duration)
1123 (define (time-at-midnight time)
1124 ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and
1125 ;; hours to zeros.
1126 (let ((d (time-utc->date time)))
1127 (date->time-utc
1128 (make-date 0 0 0 0
1129 (date-day d) (date-month d)
1130 (date-year d) (date-zone-offset d)))))
1131
1132 (define generation-ctime-alist
1133 (map (lambda (number)
1134 (cons number
1135 (time-second
1136 (time-at-midnight
1137 (generation-time profile number)))))
1138 (generation-numbers profile)))
1139
1140 (match duration
1141 (#f #f)
1142 (res
1143 (let ((s (time-second
1144 (subtract-duration (time-at-midnight (current-time))
1145 duration))))
1146 (delete #f (map (lambda (x)
1147 (and (duration-relation s (cdr x))
1148 (first x)))
1149 generation-ctime-alist))))))
1150
1151 (cond ((string->generations str)
1152 =>
1153 filter-generations)
1154 ((string->duration str)
1155 =>
1156 filter-by-duration)
1157 (else #f)))
1158
ad18c7e6
LC
1159(define (display-generation profile number)
1160 "Display a one-line summary of generation NUMBER of PROFILE."
1161 (unless (zero? number)
69daee23 1162 (let ((header (format #f (G_ "Generation ~a\t~a") number
ad18c7e6
LC
1163 (date->string
1164 (time-utc->date
1165 (generation-time profile number))
1166 "~b ~d ~Y ~T")))
1167 (current (generation-number profile)))
1168 (if (= number current)
3bd9672c
LC
1169 ;; TRANSLATORS: The word "current" here is an adjective for
1170 ;; "Generation", as in "current generation". Use the appropriate
1171 ;; gender where applicable.
69daee23 1172 (format #t (G_ "~a\t(current)~%") header)
ad18c7e6
LC
1173 (format #t "~a~%" header)))))
1174
e95ae7c2
RJ
1175(define (display-profile-content-diff profile gen1 gen2)
1176 "Display the changed packages in PROFILE GEN2 compared to generation GEN2."
1177
1178 (define (equal-entry? first second)
1179 (string= (manifest-entry-item first) (manifest-entry-item second)))
1180
1181 (define (display-entry entry prefix)
1182 (match entry
1183 (($ <manifest-entry> name version output location _)
1184 (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location))))
1185
1186 (define (list-entries number)
1187 (manifest-entries (profile-manifest (generation-file-name profile number))))
1188
1189 (define (display-diff profile old new)
1190 (display-generation profile new)
1191 (let ((added (lset-difference
1192 equal-entry? (list-entries new) (list-entries old)))
1193 (removed (lset-difference
1194 equal-entry? (list-entries old) (list-entries new))))
1195 (for-each (cut display-entry <> "+") added)
88bdbb2a
RJ
1196 (for-each (cut display-entry <> "-") removed)
1197 (newline)))
e95ae7c2
RJ
1198
1199 (display-diff profile gen1 gen2))
1200
ad18c7e6
LC
1201(define (display-profile-content profile number)
1202 "Display the packages in PROFILE, generation NUMBER, in a human-readable
1203way."
1204 (for-each (match-lambda
1205 (($ <manifest-entry> name version output location _)
1206 (format #t " ~a\t~a\t~a\t~a~%"
1207 name version output location)))
1208
1209 ;; Show most recently installed packages last.
1210 (reverse
1211 (manifest-entries
1212 (profile-manifest (generation-file-name profile number))))))
1213
06d45f45 1214(define (display-generation-change previous current)
69daee23 1215 (format #t (G_ "switched from generation ~a to ~a~%") previous current))
06d45f45
LC
1216
1217(define (roll-back* store profile)
1218 "Like 'roll-back', but display what is happening."
1219 (call-with-values
1220 (lambda ()
1221 (roll-back store profile))
1222 display-generation-change))
1223
1224(define (switch-to-generation* profile number)
1225 "Like 'switch-generation', but display what is happening."
1226 (let ((previous (switch-to-generation profile number)))
1227 (display-generation-change previous number)))
1228
1229(define (delete-generation* store profile generation)
1230 "Like 'delete-generation', but display what is going on."
69daee23 1231 (format #t (G_ "deleting ~a~%")
06d45f45
LC
1232 (generation-file-name profile generation))
1233 (delete-generation store profile generation))
1234
2876b989
LC
1235(define* (package-specification->name+version+output spec
1236 #:optional (output "out"))
1237 "Parse package specification SPEC and return three value: the specified
1238package name, version number (or #f), and output name (or OUTPUT). SPEC may
1239optionally contain a version number and an output name, as in these examples:
1240
1241 guile
1b846da8 1242 guile@2.0.9
2876b989 1243 guile:debug
1b846da8 1244 guile@2.0.9:debug
2876b989
LC
1245"
1246 (let*-values (((name sub-drv)
1247 (match (string-rindex spec #\:)
1248 (#f (values spec output))
1249 (colon (values (substring spec 0 colon)
1250 (substring spec (+ 1 colon))))))
1251 ((name version)
1252 (package-name->name+version name)))
1253 (values name version sub-drv)))
1254
1255\f
1256;;;
1257;;; Command-line option processing.
1258;;;
1259
e49951eb 1260(define (show-guix-usage)
e49951eb 1261 (format (current-error-port)
69daee23 1262 (G_ "Try `guix --help' for more information.~%"))
25c93676 1263 (exit 1))
e49951eb 1264
e31ff8b8
LC
1265(define (command-files)
1266 "Return the list of source files that define Guix sub-commands."
1267 (define directory
1268 (and=> (search-path %load-path "guix.scm")
1269 (compose (cut string-append <> "/guix/scripts")
1270 dirname)))
1271
2b8cf44f
LC
1272 (define dot-scm?
1273 (cut string-suffix? ".scm" <>))
1274
e31ff8b8 1275 (if directory
09d809db 1276 (scandir directory dot-scm?)
e31ff8b8
LC
1277 '()))
1278
1279(define (commands)
1280 "Return the list of Guix command names."
1281 (map (compose (cut string-drop-right <> 4)
1282 basename)
1283 (command-files)))
1284
1285(define (show-guix-help)
59f734f3 1286 (define (internal? command)
94d92c77
LC
1287 (member command '("substitute" "authenticate" "offload"
1288 "perform-download")))
59f734f3 1289
69daee23 1290 (format #t (G_ "Usage: guix COMMAND ARGS...
e31ff8b8
LC
1291Run COMMAND with ARGS.\n"))
1292 (newline)
69daee23 1293 (format #t (G_ "COMMAND must be one of the sub-commands listed below:\n"))
e31ff8b8
LC
1294 (newline)
1295 ;; TODO: Display a synopsis of each command.
59f734f3
LC
1296 (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
1297 string<?))
e31ff8b8
LC
1298 (show-bug-report-information))
1299
a2011be5
LC
1300(define program-name
1301 ;; Name of the command-line program currently executing, or #f.
1302 (make-parameter #f))
1303
ec5d0a85
LC
1304(define (run-guix-command command . args)
1305 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
1306found."
1307 (define module
1308 (catch 'misc-error
1309 (lambda ()
1310 (resolve-interface `(guix scripts ,command)))
1311 (lambda -
25c93676 1312 (format (current-error-port)
69daee23 1313 (G_ "guix: ~a: command not found~%") command)
25c93676 1314 (show-guix-usage))))
ec5d0a85
LC
1315
1316 (let ((command-main (module-ref module
1317 (symbol-append 'guix- command))))
1318 (parameterize ((program-name command))
14d5ca2e
LC
1319 ;; Disable canonicalization so we don't don't stat unreasonably.
1320 (with-fluids ((%file-port-name-canonicalization #f))
f4453df9
LC
1321 (dynamic-wind
1322 (const #f)
1323 (lambda ()
1324 (apply command-main args))
1325 (lambda ()
1326 ;; Abuse 'exit-hook' (which is normally meant to be used by the
1327 ;; REPL) to run things like profiling hooks upon completion.
1328 (run-hook exit-hook)))))))
ec5d0a85 1329
caa6732e
AK
1330(define (run-guix . args)
1331 "Run the 'guix' command defined by command line ARGS.
1332Unlike 'guix-main', this procedure assumes that locale, i18n support,
1333and signal handling has already been set up."
1334 (define option? (cut string-prefix? "-" <>))
1335
cbee9559
LC
1336 ;; The default %LOAD-EXTENSIONS includes the empty string, which doubles the
1337 ;; number of 'stat' calls per entry in %LOAD-PATH. Shamelessly remove it.
1338 (set! %load-extensions '(".scm"))
1339
caa6732e
AK
1340 (match args
1341 (()
1342 (format (current-error-port)
69daee23 1343 (G_ "guix: missing command name~%"))
caa6732e
AK
1344 (show-guix-usage))
1345 ((or ("-h") ("--help"))
1346 (show-guix-help))
1347 (("--version")
1348 (show-version-and-exit "guix"))
1349 (((? option? o) args ...)
1350 (format (current-error-port)
69daee23 1351 (G_ "guix: unrecognized option '~a'~%") o)
caa6732e 1352 (show-guix-usage))
2ab5fdc4
LC
1353 (("help" command)
1354 (apply run-guix-command (string->symbol command)
1355 '("--help")))
caa6732e
AK
1356 (("help" args ...)
1357 (show-guix-help))
1358 ((command args ...)
1359 (apply run-guix-command
1360 (string->symbol command)
1361 args))))
1362
a2011be5
LC
1363(define guix-warning-port
1364 (make-parameter (current-warning-port)))
1365
e49951eb
MW
1366(define (guix-main arg0 . args)
1367 (initialize-guix)
caa6732e 1368 (apply run-guix args))
e49951eb 1369
073c34d7 1370;;; ui.scm ends here