1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-37)
34 #:autoload (ice-9 ftw) (scandir)
35 #:use-module (ice-9 match)
36 #:use-module (ice-9 format)
41 show-bug-report-information
44 call-with-error-handling
46 read/eval-package-expression
62 ;;; User interface facilities for command-line tools.
66 (define %gettext-domain
69 (define _ (cut gettext <> %gettext-domain))
70 (define N_ (cut ngettext <> <> <> %gettext-domain))
72 (define-syntax-rule (define-diagnostic name prefix)
73 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
77 (define (augmented-format-string fmt)
78 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
81 ((name (underscore fmt) args (... ...))
82 (and (string? (syntax->datum #'fmt))
83 (free-identifier=? #'underscore #'_))
84 (with-syntax ((fmt* (augmented-format-string #'fmt))
85 (prefix (datum->syntax x prefix)))
86 #'(format (guix-warning-port) (gettext fmt*)
87 (program-name) (program-name) prefix
89 ((name (N-underscore singular plural n) args (... ...))
90 (and (string? (syntax->datum #'singular))
91 (string? (syntax->datum #'plural))
92 (free-identifier=? #'N-underscore #'N_))
93 (with-syntax ((s (augmented-format-string #'singular))
94 (p (augmented-format-string #'plural))
95 (prefix (datum->syntax x prefix)))
96 #'(format (guix-warning-port)
97 (ngettext s p n %gettext-domain)
98 (program-name) (program-name) prefix
101 (define-diagnostic warning "warning: ") ; emit a warning
103 (define-diagnostic report-error "error: ")
104 (define-syntax-rule (leave args ...)
105 "Emit an error message and exit."
107 (report-error args ...)
110 (define (install-locale)
111 "Install the current locale settings."
114 (setlocale LC_ALL ""))
116 (warning (_ "failed to install locale: ~a~%")
117 (strerror (system-error-errno args))))))
119 (define (initialize-guix)
120 "Perform the usual initialization for stand-alone Guix commands."
124 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
125 ;; notified via an EPIPE later.
126 (sigaction SIGPIPE SIG_IGN)
128 (setvbuf (current-output-port) _IOLBF)
129 (setvbuf (current-error-port) _IOLBF))
131 (define* (show-version-and-exit #:optional (command (car (command-line))))
132 "Display version information for COMMAND and `(exit 0)'."
133 (simple-format #t "~a (~a) ~a~%"
134 command %guix-package-name %guix-version)
137 (define (show-bug-report-information)
139 Report bugs to: ~a.") %guix-bug-report-address)
141 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
143 General help using GNU software: <http://www.gnu.org/gethelp/>"))
146 (define (string->number* str)
147 "Like `string->number', but error out with an error message on failure."
148 (or (string->number str)
149 (leave (_ "~a: invalid number~%") str)))
151 (define (call-with-error-handling thunk)
152 "Call THUNK within a user-friendly error handler."
153 (guard (c ((package-input-error? c)
154 (let* ((package (package-error-package c))
155 (input (package-error-invalid-input c))
156 (location (package-location package))
157 (file (location-file location))
158 (line (location-line location))
159 (column (location-column location)))
160 (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
162 (package-full-name package) input)))
163 ((package-cross-build-system-error? c)
164 (let* ((package (package-error-package c))
165 (loc (package-location package))
166 (system (package-build-system package)))
167 (leave (_ "~a: ~a: build system `~a' does not support cross builds~%")
168 (location->string loc)
169 (package-full-name package)
170 (build-system-name system))))
171 ((nix-connection-error? c)
172 (leave (_ "failed to connect to `~a': ~a~%")
173 (nix-connection-error-file c)
174 (strerror (nix-connection-error-code c))))
175 ((nix-protocol-error? c)
176 ;; FIXME: Server-provided error messages aren't i18n'd.
177 (leave (_ "build failed: ~a~%")
178 (nix-protocol-error-message c))))
179 ;; Catch EPIPE and the likes.
184 (strerror (system-error-errno args)))))))
186 (define (read/eval-package-expression str)
187 "Read and evaluate STR and return the package it refers to, or exit an
191 (call-with-input-string str read))
193 (leave (_ "failed to read expression ~s: ~s~%")
197 (eval exp the-scm-module))
199 (leave (_ "failed to evaluate expression `~a': ~s~%")
203 (leave (_ "expression `~s' does not evaluate to a package~%")
206 (define* (show-what-to-build store drv
207 #:key dry-run? (use-substitutes? #t))
208 "Show what will or would (depending on DRY-RUN?) be built in realizing the
209 derivations listed in DRV. Return #t if there's something to build, #f
210 otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are
211 available for download."
212 (let*-values (((build download)
213 (fold2 (lambda (drv-path build download)
214 (let ((drv (call-with-input-file drv-path
217 (derivation-prerequisites-to-build
221 (values (append b build)
222 (append d download)))))
225 ((build) ; add the DRV themselves
227 (append (remove (compose (lambda (out)
228 (or (valid-path? store out)
229 (and use-substitutes?
230 (has-substitutes? store
232 derivation-path->output-path)
234 (map derivation-input-path build))))
235 ((download) ; add the references of DOWNLOAD
239 (remove (cut valid-path? store <>)
241 substitutable-references
242 (substitutable-path-info store
247 (format (current-error-port)
248 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
249 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
252 (format (current-error-port)
253 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
254 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
256 (null? download) download))
258 (format (current-error-port)
259 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
260 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
263 (format (current-error-port)
264 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
265 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
267 (null? download) download)))
270 (define-syntax with-error-handling
272 "Run BODY within a user-friendly error condition handler."
274 (call-with-error-handling
278 (define (location->string loc)
279 "Return a human-friendly, GNU-standard representation of LOC."
281 (#f (_ "<unknown location>"))
282 (($ <location> file line column)
283 (format #f "~a:~a:~a" file line column))))
285 (define (switch-symlinks link target)
286 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
287 both when LINK already exists and when it does not."
288 (let ((pivot (string-append link ".new")))
289 (symlink target pivot)
290 (rename-file pivot link)))
292 (define (config-directory)
293 "Return the name of the configuration directory, after making sure that it
294 exists. Honor the XDG specs,
295 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
296 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
297 (and=> (getenv "HOME")
298 (cut string-append <> "/.config")))
299 (cut string-append <> "/guix"))))
305 (match (system-error-errno args)
309 (leave (_ "failed to create configuration directory `~a': ~a~%")
310 dir (strerror err))))))))
312 (define* (fill-paragraph str width #:optional (column 0))
313 "Fill STR such that each line contains at most WIDTH characters, assuming
314 that the first character is at COLUMN.
316 When STR contains a single line break surrounded by other characters, it is
317 converted to a space; sequences of more than one line break are preserved."
318 (define (maybe-break chr result)
320 ((column newlines chars)
323 `(,column ,(+ 1 newlines) ,chars))
325 (let ((chars (case newlines
327 ((1) (cons #\space chars))
329 (append (make-list newlines #\newline) chars))))
330 (column (case newlines
334 (let ((chars (cons chr chars))
335 (column (+ 1 column)))
337 (let*-values (((before after)
338 (break (cut eqv? #\space <>) chars))
346 (append before (cons #\newline (cdr after)))))
347 `(,column 0 ,chars))) ; unbreakable
348 `(,column 0 ,chars)))))))))
350 (match (string-fold maybe-break
354 (list->string (reverse chars)))))
356 (define (string->recutils str)
357 "Return a version of STR where newlines have been replaced by newlines
358 followed by \"+ \", which makes for a valid multi-line field value in the
361 (string-fold-right (lambda (chr result)
362 (if (eqv? chr #\newline)
363 (cons* chr #\+ #\space result)
368 (define* (package->recutils p port
369 #:optional (width (or (and=> (getenv "WIDTH")
372 "Write to PORT a `recutils' record of package P, arranging to fit within
374 (define (description->recutils str)
377 (fill-paragraph str width
378 (string-length "description: ")))))
380 ;; Note: Don't i18n field names so that people can post-process it.
381 (format port "name: ~a~%" (package-name p))
382 (format port "version: ~a~%" (package-version p))
383 (format port "location: ~a~%"
384 (or (and=> (package-location p) location->string)
386 (format port "home-page: ~a~%" (package-home-page p))
387 (format port "license: ~a~%"
388 (match (package-license p)
389 (((? license? licenses) ...)
390 (string-join (map license-name licenses)
392 ((? license? license)
393 (license-name license))
396 (format port "synopsis: ~a~%"
397 (string-map (match-lambda
400 (or (and=> (package-synopsis p) _)
402 (format port "description: ~a~%"
403 (and=> (package-description p) description->recutils))
406 (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
407 "A wrapper on top of `args-fold' that does proper user-facing error
411 (apply args-fold options unrecognized-option-proc
413 (lambda (key proc msg args . rest)
414 ;; XXX: MSG is not i18n'd.
415 (leave (_ "invalid argument: ~a~%")
416 (apply format #f msg args)))))
418 (define (show-guix-usage)
419 (format (current-error-port)
420 (_ "Try `guix --help' for more information.~%"))
423 (define (command-files)
424 "Return the list of source files that define Guix sub-commands."
426 (and=> (search-path %load-path "guix.scm")
427 (compose (cut string-append <> "/guix/scripts")
431 (cut string-suffix? ".scm" <>))
433 ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though
434 ;; they don't match `dot-scm?'. Work around it by doing additional
437 (filter dot-scm? (scandir directory dot-scm?))
441 "Return the list of Guix command names."
442 (map (compose (cut string-drop-right <> 4)
446 (define (show-guix-help)
447 (format #t (_ "Usage: guix COMMAND ARGS...
448 Run COMMAND with ARGS.\n"))
450 (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
452 ;; TODO: Display a synopsis of each command.
453 (format #t "~{ ~a~%~}" (sort (commands) string<?))
454 (show-bug-report-information))
457 ;; Name of the command-line program currently executing, or #f.
460 (define (run-guix-command command . args)
461 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
466 (resolve-interface `(guix scripts ,command)))
468 (format (current-error-port)
469 (_ "guix: ~a: command not found~%") command)
472 (let ((command-main (module-ref module
473 (symbol-append 'guix- command))))
474 (parameterize ((program-name command))
475 (apply command-main args))))
477 (define guix-warning-port
478 (make-parameter (current-warning-port)))
480 (define (guix-main arg0 . args)
483 (define (option? str) (string-prefix? "-" str))
486 (format (current-error-port)
487 (_ "guix: missing command name~%"))
492 (show-version-and-exit "guix"))
493 (((? option? o) args ...)
494 (format (current-error-port)
495 (_ "guix: unrecognized option '~a'~%") o)
498 (apply run-guix-command
499 (string->symbol command)