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 derivations)
27 #:use-module ((guix licenses) #:select (license? license-name))
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-11)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-34)
32 #:use-module (srfi srfi-37)
33 #:autoload (ice-9 ftw) (scandir)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 format)
40 show-bug-report-information
42 call-with-error-handling
44 read/eval-package-expression
60 ;;; User interface facilities for command-line tools.
64 (define %gettext-domain
67 (define _ (cut gettext <> %gettext-domain))
68 (define N_ (cut ngettext <> <> <> %gettext-domain))
70 (define-syntax-rule (define-diagnostic name prefix)
71 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
75 (define (augmented-format-string fmt)
76 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
78 (syntax-case x (N_ _) ; these are literals, yeah...
79 ((name (_ fmt) args (... ...))
80 (string? (syntax->datum #'fmt))
81 (with-syntax ((fmt* (augmented-format-string #'fmt))
82 (prefix (datum->syntax x prefix)))
83 #'(format (guix-warning-port) (gettext fmt*)
84 (program-name) (program-name) prefix
86 ((name (N_ singular plural n) args (... ...))
87 (and (string? (syntax->datum #'singular))
88 (string? (syntax->datum #'plural)))
89 (with-syntax ((s (augmented-format-string #'singular))
90 (p (augmented-format-string #'plural))
91 (prefix (datum->syntax x prefix)))
92 #'(format (guix-warning-port)
93 (ngettext s p n %gettext-domain)
94 (program-name) (program-name) prefix
97 (define-diagnostic warning "warning: ") ; emit a warning
99 (define-diagnostic report-error "error: ")
100 (define-syntax-rule (leave args ...)
101 "Emit an error message and exit."
103 (report-error args ...)
106 (define (install-locale)
107 "Install the current locale settings."
110 (setlocale LC_ALL ""))
112 (warning (_ "failed to install locale: ~a~%")
113 (strerror (system-error-errno args))))))
115 (define (initialize-guix)
116 "Perform the usual initialization for stand-alone Guix commands."
119 (setvbuf (current-output-port) _IOLBF)
120 (setvbuf (current-error-port) _IOLBF))
122 (define* (show-version-and-exit #:optional (command (car (command-line))))
123 "Display version information for COMMAND and `(exit 0)'."
124 (simple-format #t "~a (~a) ~a~%"
125 command %guix-package-name %guix-version)
128 (define (show-bug-report-information)
130 Report bugs to: ~a.") %guix-bug-report-address)
132 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
134 General help using GNU software: <http://www.gnu.org/gethelp/>"))
137 (define (call-with-error-handling thunk)
138 "Call THUNK within a user-friendly error handler."
139 (guard (c ((package-input-error? c)
140 (let* ((package (package-error-package c))
141 (input (package-error-invalid-input c))
142 (location (package-location package))
143 (file (location-file location))
144 (line (location-line location))
145 (column (location-column location)))
146 (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
148 (package-full-name package) input)))
149 ((nix-connection-error? c)
150 (leave (_ "failed to connect to `~a': ~a~%")
151 (nix-connection-error-file c)
152 (strerror (nix-connection-error-code c))))
153 ((nix-protocol-error? c)
154 ;; FIXME: Server-provided error messages aren't i18n'd.
155 (leave (_ "build failed: ~a~%")
156 (nix-protocol-error-message c))))
159 (define (read/eval-package-expression str)
160 "Read and evaluate STR and return the package it refers to, or exit an
164 (call-with-input-string str read))
166 (leave (_ "failed to read expression ~s: ~s~%")
170 (eval exp the-scm-module))
172 (leave (_ "failed to evaluate expression `~a': ~s~%")
176 (leave (_ "expression `~s' does not evaluate to a package~%")
179 (define* (show-what-to-build store drv
180 #:key dry-run? (use-substitutes? #t))
181 "Show what will or would (depending on DRY-RUN?) be built in realizing the
182 derivations listed in DRV. Return #t if there's something to build, #f
183 otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are
184 available for download."
185 (let*-values (((build download)
186 (fold2 (lambda (drv-path build download)
187 (let ((drv (call-with-input-file drv-path
190 (derivation-prerequisites-to-build
194 (values (append b build)
195 (append d download)))))
198 ((build) ; add the DRV themselves
200 (append (remove (compose (lambda (out)
201 (or (valid-path? store out)
202 (and use-substitutes?
203 (has-substitutes? store
205 derivation-path->output-path)
207 (map derivation-input-path build))))
208 ((download) ; add the references of DOWNLOAD
211 (remove (cut valid-path? store <>)
213 substitutable-references
214 (substitutable-path-info store download)))))))
217 (format (current-error-port)
218 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
219 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
222 (format (current-error-port)
223 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
224 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
226 (null? download) download))
228 (format (current-error-port)
229 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
230 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
233 (format (current-error-port)
234 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
235 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
237 (null? download) download)))
240 (define-syntax with-error-handling
242 "Run BODY within a user-friendly error condition handler."
244 (call-with-error-handling
248 (define (location->string loc)
249 "Return a human-friendly, GNU-standard representation of LOC."
251 (#f (_ "<unknown location>"))
252 (($ <location> file line column)
253 (format #f "~a:~a:~a" file line column))))
255 (define (switch-symlinks link target)
256 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
257 both when LINK already exists and when it does not."
258 (let ((pivot (string-append link ".new")))
259 (symlink target pivot)
260 (rename-file pivot link)))
262 (define (config-directory)
263 "Return the name of the configuration directory, after making sure that it
264 exists. Honor the XDG specs,
265 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
266 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
267 (and=> (getenv "HOME")
268 (cut string-append <> "/.config")))
269 (cut string-append <> "/guix"))))
275 (match (system-error-errno args)
279 (leave (_ "failed to create configuration directory `~a': ~a~%")
280 dir (strerror err))))))))
282 (define* (fill-paragraph str width #:optional (column 0))
283 "Fill STR such that each line contains at most WIDTH characters, assuming
284 that the first character is at COLUMN.
286 When STR contains a single line break surrounded by other characters, it is
287 converted to a space; sequences of more than one line break are preserved."
288 (define (maybe-break chr result)
290 ((column newlines chars)
293 `(,column ,(+ 1 newlines) ,chars))
295 (let ((chars (case newlines
297 ((1) (cons #\space chars))
299 (append (make-list newlines #\newline) chars))))
300 (column (case newlines
304 (let ((chars (cons chr chars))
305 (column (+ 1 column)))
307 (let*-values (((before after)
308 (break (cut eqv? #\space <>) chars))
316 (append before (cons #\newline (cdr after)))))
317 `(,column 0 ,chars))) ; unbreakable
318 `(,column 0 ,chars)))))))))
320 (match (string-fold maybe-break
324 (list->string (reverse chars)))))
326 (define (string->recutils str)
327 "Return a version of STR where newlines have been replaced by newlines
328 followed by \"+ \", which makes for a valid multi-line field value in the
331 (string-fold-right (lambda (chr result)
332 (if (eqv? chr #\newline)
333 (cons* chr #\+ #\space result)
338 (define* (package->recutils p port
339 #:optional (width (or (and=> (getenv "WIDTH")
342 "Write to PORT a `recutils' record of package P, arranging to fit within
344 (define (description->recutils str)
347 (fill-paragraph str width
348 (string-length "description: ")))))
350 ;; Note: Don't i18n field names so that people can post-process it.
351 (format port "name: ~a~%" (package-name p))
352 (format port "version: ~a~%" (package-version p))
353 (format port "location: ~a~%"
354 (or (and=> (package-location p) location->string)
356 (format port "home-page: ~a~%" (package-home-page p))
357 (format port "license: ~a~%"
358 (match (package-license p)
359 (((? license? licenses) ...)
360 (string-join (map license-name licenses)
362 ((? license? license)
363 (license-name license))
366 (format port "synopsis: ~a~%"
367 (string-map (match-lambda
370 (or (and=> (package-synopsis p) _)
372 (format port "description: ~a~%"
373 (and=> (package-description p) description->recutils))
376 (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
377 "A wrapper on top of `args-fold' that does proper user-facing error
381 (apply args-fold options unrecognized-option-proc
383 (lambda (key proc msg args . rest)
384 ;; XXX: MSG is not i18n'd.
385 (leave (_ "invalid argument: ~a~%")
386 (apply format #f msg args)))))
388 (define (show-guix-usage)
389 (format (current-error-port)
390 (_ "Try `guix --help' for more information.~%"))
393 (define (command-files)
394 "Return the list of source files that define Guix sub-commands."
396 (and=> (search-path %load-path "guix.scm")
397 (compose (cut string-append <> "/guix/scripts")
401 (cut string-suffix? ".scm" <>))
403 ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though
404 ;; they don't match `dot-scm?'. Work around it by doing additional
407 (filter dot-scm? (scandir directory dot-scm?))
411 "Return the list of Guix command names."
412 (map (compose (cut string-drop-right <> 4)
416 (define (show-guix-help)
417 (format #t (_ "Usage: guix COMMAND ARGS...
418 Run COMMAND with ARGS.\n"))
420 (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
422 ;; TODO: Display a synopsis of each command.
423 (format #t "~{ ~a~%~}" (commands))
424 (show-bug-report-information))
427 ;; Name of the command-line program currently executing, or #f.
430 (define (run-guix-command command . args)
431 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
436 (resolve-interface `(guix scripts ,command)))
438 (format (current-error-port)
439 (_ "guix: ~a: command not found~%") command)
442 (let ((command-main (module-ref module
443 (symbol-append 'guix- command))))
444 (parameterize ((program-name command))
445 (apply command-main args))))
447 (define guix-warning-port
448 (make-parameter (current-warning-port)))
450 (define (guix-main arg0 . args)
453 (define (option? str) (string-prefix? "-" str))
456 (format (current-error-port)
457 (_ "guix: missing command name~%"))
462 (show-version-and-exit "guix"))
463 (((? option? o) args ...)
464 (format (current-error-port)
465 (_ "guix: unrecognized option '~a'~%") o)
468 (apply run-guix-command
469 (string->symbol command)