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