1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix ui)
22 #:use-module (guix utils)
23 #:use-module (guix store)
24 #:use-module (guix config)
25 #:use-module (guix packages)
26 #:use-module (guix build-system)
27 #:use-module (guix derivations)
28 #:use-module ((guix licenses) #:select (license? license-name))
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-11)
31 #:use-module (srfi srfi-19)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (srfi srfi-37)
36 #:autoload (ice-9 ftw) (scandir)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 format)
39 #:use-module (ice-9 regex)
44 show-bug-report-information
47 call-with-error-handling
50 read/eval-package-expression
57 package-specification->name+version+output
69 ;;; User interface facilities for command-line tools.
73 (define %gettext-domain
76 (define _ (cut gettext <> %gettext-domain))
77 (define N_ (cut ngettext <> <> <> %gettext-domain))
79 (define-syntax-rule (define-diagnostic name prefix)
80 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
84 (define (augmented-format-string fmt)
85 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
88 ((name (underscore fmt) args (... ...))
89 (and (string? (syntax->datum #'fmt))
90 (free-identifier=? #'underscore #'_))
91 (with-syntax ((fmt* (augmented-format-string #'fmt))
92 (prefix (datum->syntax x prefix)))
93 #'(format (guix-warning-port) (gettext fmt*)
94 (program-name) (program-name) prefix
96 ((name (N-underscore singular plural n) args (... ...))
97 (and (string? (syntax->datum #'singular))
98 (string? (syntax->datum #'plural))
99 (free-identifier=? #'N-underscore #'N_))
100 (with-syntax ((s (augmented-format-string #'singular))
101 (p (augmented-format-string #'plural))
102 (prefix (datum->syntax x prefix)))
103 #'(format (guix-warning-port)
104 (ngettext s p n %gettext-domain)
105 (program-name) (program-name) prefix
106 args (... ...))))))))
108 (define-diagnostic warning "warning: ") ; emit a warning
110 (define-diagnostic report-error "error: ")
111 (define-syntax-rule (leave args ...)
112 "Emit an error message and exit."
114 (report-error args ...)
117 (define (install-locale)
118 "Install the current locale settings."
121 (setlocale LC_ALL ""))
123 (warning (_ "failed to install locale: ~a~%")
124 (strerror (system-error-errno args))))))
126 (define (initialize-guix)
127 "Perform the usual initialization for stand-alone Guix commands."
129 (textdomain %gettext-domain)
131 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
132 ;; notified via an EPIPE later.
133 (sigaction SIGPIPE SIG_IGN)
135 (setvbuf (current-output-port) _IOLBF)
136 (setvbuf (current-error-port) _IOLBF))
138 (define* (show-version-and-exit #:optional (command (car (command-line))))
139 "Display version information for COMMAND and `(exit 0)'."
140 (simple-format #t "~a (~a) ~a~%"
141 command %guix-package-name %guix-version)
142 (display (_ "Copyright (C) 2014 the Guix authors
143 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
144 This is free software: you are free to change and redistribute it.
145 There is NO WARRANTY, to the extent permitted by law.
149 (define (show-bug-report-information)
151 Report bugs to: ~a.") %guix-bug-report-address)
153 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
155 General help using GNU software: <http://www.gnu.org/gethelp/>"))
158 (define (string->number* str)
159 "Like `string->number', but error out with an error message on failure."
160 (or (string->number str)
161 (leave (_ "~a: invalid number~%") str)))
163 (define (call-with-error-handling thunk)
164 "Call THUNK within a user-friendly error handler."
165 (guard (c ((package-input-error? c)
166 (let* ((package (package-error-package c))
167 (input (package-error-invalid-input c))
168 (location (package-location package))
169 (file (location-file location))
170 (line (location-line location))
171 (column (location-column location)))
172 (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
174 (package-full-name package) input)))
175 ((package-cross-build-system-error? c)
176 (let* ((package (package-error-package c))
177 (loc (package-location package))
178 (system (package-build-system package)))
179 (leave (_ "~a: ~a: build system `~a' does not support cross builds~%")
180 (location->string loc)
181 (package-full-name package)
182 (build-system-name system))))
183 ((nix-connection-error? c)
184 (leave (_ "failed to connect to `~a': ~a~%")
185 (nix-connection-error-file c)
186 (strerror (nix-connection-error-code c))))
187 ((nix-protocol-error? c)
188 ;; FIXME: Server-provided error messages aren't i18n'd.
189 (leave (_ "build failed: ~a~%")
190 (nix-protocol-error-message c)))
191 ((message-condition? c)
192 ;; Normally '&message' error conditions have an i18n'd message.
193 (leave (_ "~a~%") (gettext (condition-message c)))))
194 ;; Catch EPIPE and the likes.
197 (lambda (key proc format-string format-args . rest)
198 (leave (_ "~a: ~a~%") proc
199 (apply format #f format-string format-args))))))
201 (define (read/eval str)
202 "Read and evaluate STR, raising an error if something goes wrong."
205 (call-with-input-string str read))
207 (leave (_ "failed to read expression ~s: ~s~%")
211 (eval exp the-scm-module))
213 (leave (_ "failed to evaluate expression `~a': ~s~%")
216 (define (read/eval-package-expression str)
217 "Read and evaluate STR and return the package it refers to, or exit an
219 (match (read/eval str)
222 (leave (_ "expression ~s does not evaluate to a package~%")
225 (define* (show-what-to-build store drv
226 #:key dry-run? (use-substitutes? #t))
227 "Show what will or would (depending on DRY-RUN?) be built in realizing the
228 derivations listed in DRV. Return #t if there's something to build, #f
229 otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are
230 available for download."
231 (let*-values (((build download)
232 (fold2 (lambda (drv build download)
234 (derivation-prerequisites-to-build
238 (values (append b build)
239 (append d download))))
242 ((build) ; add the DRV themselves
244 (append (map derivation-file-name
245 (remove (lambda (drv)
246 (let ((out (derivation->output-path
248 (or (valid-path? store out)
249 (and use-substitutes?
250 (has-substitutes? store
253 (map derivation-input-path build))))
254 ((download) ; add the references of DOWNLOAD
258 (remove (cut valid-path? store <>)
260 substitutable-references
261 (substitutable-path-info store
264 ;; TODO: Show the installed size of DOWNLOAD.
267 (format (current-error-port)
268 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
269 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
272 (format (current-error-port)
273 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
274 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
276 (null? download) download))
278 (format (current-error-port)
279 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
280 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
283 (format (current-error-port)
284 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
285 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
287 (null? download) download)))
290 (define-syntax with-error-handling
292 "Run BODY within a user-friendly error condition handler."
294 (call-with-error-handling
298 (define (location->string loc)
299 "Return a human-friendly, GNU-standard representation of LOC."
301 (#f (_ "<unknown location>"))
302 (($ <location> file line column)
303 (format #f "~a:~a:~a" file line column))))
305 (define (switch-symlinks link target)
306 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
307 both when LINK already exists and when it does not."
308 (let ((pivot (string-append link ".new")))
309 (symlink target pivot)
310 (rename-file pivot link)))
312 (define (config-directory)
313 "Return the name of the configuration directory, after making sure that it
314 exists. Honor the XDG specs,
315 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
316 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
317 (and=> (getenv "HOME")
318 (cut string-append <> "/.config")))
319 (cut string-append <> "/guix"))))
325 (match (system-error-errno args)
329 (leave (_ "failed to create configuration directory `~a': ~a~%")
330 dir (strerror err))))))))
332 (define* (fill-paragraph str width #:optional (column 0))
333 "Fill STR such that each line contains at most WIDTH characters, assuming
334 that the first character is at COLUMN.
336 When STR contains a single line break surrounded by other characters, it is
337 converted to a space; sequences of more than one line break are preserved."
338 (define (maybe-break chr result)
340 ((column newlines chars)
343 `(,column ,(+ 1 newlines) ,chars))
345 (let ((chars (case newlines
347 ((1) (cons #\space chars))
349 (append (make-list newlines #\newline) chars))))
350 (column (case newlines
354 (let ((chars (cons chr chars))
355 (column (+ 1 column)))
357 (let*-values (((before after)
358 (break (cut eqv? #\space <>) chars))
366 (append before (cons #\newline (cdr after)))))
367 `(,column 0 ,chars))) ; unbreakable
368 `(,column 0 ,chars)))))))))
370 (match (string-fold maybe-break
374 (list->string (reverse chars)))))
381 (define (string->recutils str)
382 "Return a version of STR where newlines have been replaced by newlines
383 followed by \"+ \", which makes for a valid multi-line field value in the
386 (string-fold-right (lambda (chr result)
387 (if (eqv? chr #\newline)
388 (cons* chr #\+ #\space result)
393 (define* (package->recutils p port
394 #:optional (width (or (and=> (getenv "WIDTH")
397 "Write to PORT a `recutils' record of package P, arranging to fit within
399 (define (description->recutils str)
402 (fill-paragraph str width
403 (string-length "description: ")))))
405 ;; Note: Don't i18n field names so that people can post-process it.
406 (format port "name: ~a~%" (package-name p))
407 (format port "version: ~a~%" (package-version p))
408 (format port "location: ~a~%"
409 (or (and=> (package-location p) location->string)
412 ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in
413 ;; field identifiers.
414 (format port "homepage: ~a~%" (package-home-page p))
416 (format port "license: ~a~%"
417 (match (package-license p)
418 (((? license? licenses) ...)
419 (string-join (map license-name licenses)
421 ((? license? license)
422 (license-name license))
425 (format port "synopsis: ~a~%"
426 (string-map (match-lambda
429 (or (and=> (package-synopsis p) _)
431 (format port "description: ~a~%"
432 (and=> (package-description p) description->recutils))
435 (define (string->generations str)
436 "Return the list of generations matching a pattern in STR. This function
437 accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
438 (define (maybe-integer)
439 (let ((x (string->number str)))
443 (define (maybe-comma-separated-integers)
444 (let ((lst (delete-duplicates
446 (string-split str #\,)))))
447 (and (every integer? lst)
450 (cond ((maybe-integer)
453 ((maybe-comma-separated-integers)
456 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
459 (let ((s (string->number (match:substring match 1)))
460 (e (string->number (match:substring match 2))))
461 (and (every integer? (list s e))
463 (iota (1+ (- e s)) s)))))
464 ((string-match "^([0-9]+)\\.\\.$" str)
467 (let ((s (string->number (match:substring match 1))))
470 ((string-match "^\\.\\.([0-9]+)$" str)
473 (let ((e (string->number (match:substring match 1))))
478 (define (string->duration str)
479 "Return the duration matching a pattern in STR. This function accepts the
480 following patterns: \"1d\", \"1w\", \"1m\"."
481 (define (hours->duration hours match)
482 (make-time time-duration 0
483 (* 3600 hours (string->number (match:substring match 1)))))
485 (cond ((string-match "^([0-9]+)d$" str)
488 (hours->duration 24 match)))
489 ((string-match "^([0-9]+)w$" str)
492 (hours->duration (* 24 7) match)))
493 ((string-match "^([0-9]+)m$" str)
496 (hours->duration (* 24 30) match)))
499 (define* (package-specification->name+version+output spec
500 #:optional (output "out"))
501 "Parse package specification SPEC and return three value: the specified
502 package name, version number (or #f), and output name (or OUTPUT). SPEC may
503 optionally contain a version number and an output name, as in these examples:
510 (let*-values (((name sub-drv)
511 (match (string-rindex spec #\:)
512 (#f (values spec output))
513 (colon (values (substring spec 0 colon)
514 (substring spec (+ 1 colon))))))
516 (package-name->name+version name)))
517 (values name version sub-drv)))
521 ;;; Command-line option processing.
524 (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
525 "A wrapper on top of `args-fold' that does proper user-facing error
529 (apply args-fold options unrecognized-option-proc
531 (lambda (key proc msg args . rest)
532 ;; XXX: MSG is not i18n'd.
533 (leave (_ "invalid argument: ~a~%")
534 (apply format #f msg args)))))
536 (define (show-guix-usage)
537 (format (current-error-port)
538 (_ "Try `guix --help' for more information.~%"))
541 (define (command-files)
542 "Return the list of source files that define Guix sub-commands."
544 (and=> (search-path %load-path "guix.scm")
545 (compose (cut string-append <> "/guix/scripts")
549 (cut string-suffix? ".scm" <>))
551 ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though
552 ;; they don't match `dot-scm?'. Work around it by doing additional
555 (filter dot-scm? (scandir directory dot-scm?))
559 "Return the list of Guix command names."
560 (map (compose (cut string-drop-right <> 4)
564 (define (show-guix-help)
565 (define (internal? command)
566 (member command '("substitute-binary" "authenticate" "offload")))
568 (format #t (_ "Usage: guix COMMAND ARGS...
569 Run COMMAND with ARGS.\n"))
571 (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
573 ;; TODO: Display a synopsis of each command.
574 (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
576 (show-bug-report-information))
579 ;; Name of the command-line program currently executing, or #f.
582 (define (run-guix-command command . args)
583 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
588 (resolve-interface `(guix scripts ,command)))
590 (format (current-error-port)
591 (_ "guix: ~a: command not found~%") command)
594 (let ((command-main (module-ref module
595 (symbol-append 'guix- command))))
596 (parameterize ((program-name command))
597 (apply command-main args))))
599 (define guix-warning-port
600 (make-parameter (current-warning-port)))
602 (define (guix-main arg0 . args)
605 (define (option? str) (string-prefix? "-" str))
608 (format (current-error-port)
609 (_ "guix: missing command name~%"))
611 ((or ("-h") ("--help"))
614 (show-version-and-exit "guix"))
615 (((? option? o) args ...)
616 (format (current-error-port)
617 (_ "guix: unrecognized option '~a'~%") o)
620 (apply run-guix-command
621 (string->symbol command)