Add (guix profiles).
[jackhill/guix/guix.git] / guix / ui.scm
CommitLineData
233e7676
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012, 2013 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>
073c34d7 5;;;
233e7676 6;;; This file is part of GNU Guix.
073c34d7 7;;;
233e7676 8;;; GNU Guix is free software; you can redistribute it and/or modify it
073c34d7
LC
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.
12;;;
233e7676 13;;; GNU Guix is distributed in the hope that it will be useful, but
073c34d7
LC
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.
17;;;
18;;; You should have received a copy of the GNU General Public License
233e7676 19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
073c34d7
LC
20
21(define-module (guix ui)
22 #:use-module (guix utils)
23 #:use-module (guix store)
cdd5d6f9 24 #:use-module (guix config)
073c34d7 25 #:use-module (guix packages)
9b222abe 26 #:use-module (guix build-system)
9bb2b96a 27 #:use-module (guix derivations)
299112d3
LC
28 #:use-module ((guix licenses) #:select (license? license-name))
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-11)
2cd09108 31 #:use-module (srfi srfi-19)
073c34d7
LC
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
a5975ced 34 #:use-module (srfi srfi-37)
e31ff8b8 35 #:autoload (ice-9 ftw) (scandir)
64fc89b6 36 #:use-module (ice-9 match)
9bb2b96a 37 #:use-module (ice-9 format)
2cd09108 38 #:use-module (ice-9 regex)
073c34d7
LC
39 #:export (_
40 N_
41 leave
cdd5d6f9 42 show-version-and-exit
3441e164 43 show-bug-report-information
969e678e 44 string->number*
9bb2b96a 45 show-what-to-build
073c34d7 46 call-with-error-handling
64fc89b6 47 with-error-handling
eb0880e7 48 read/eval-package-expression
299112d3 49 location->string
c61b026e 50 switch-symlinks
f651b477 51 config-directory
299112d3
LC
52 fill-paragraph
53 string->recutils
e49951eb 54 package->recutils
2cd09108
NK
55 string->generations
56 string->duration
a5975ced 57 args-fold*
e49951eb 58 run-guix-command
a2011be5
LC
59 program-name
60 guix-warning-port
61 warning
e49951eb 62 guix-main))
073c34d7
LC
63
64;;; Commentary:
65;;;
66;;; User interface facilities for command-line tools.
67;;;
68;;; Code:
69
70(define %gettext-domain
71 "guix")
72
73(define _ (cut gettext <> %gettext-domain))
74(define N_ (cut ngettext <> <> <> %gettext-domain))
75
b2a886f6
LC
76(define-syntax-rule (define-diagnostic name prefix)
77 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
78messages."
79 (define-syntax name
80 (lambda (x)
81 (define (augmented-format-string fmt)
82 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
83
89d02b98
LC
84 (syntax-case x ()
85 ((name (underscore fmt) args (... ...))
86 (and (string? (syntax->datum #'fmt))
87 (free-identifier=? #'underscore #'_))
b2a886f6
LC
88 (with-syntax ((fmt* (augmented-format-string #'fmt))
89 (prefix (datum->syntax x prefix)))
90 #'(format (guix-warning-port) (gettext fmt*)
91 (program-name) (program-name) prefix
92 args (... ...))))
89d02b98 93 ((name (N-underscore singular plural n) args (... ...))
b2a886f6 94 (and (string? (syntax->datum #'singular))
89d02b98
LC
95 (string? (syntax->datum #'plural))
96 (free-identifier=? #'N-underscore #'N_))
b2a886f6
LC
97 (with-syntax ((s (augmented-format-string #'singular))
98 (p (augmented-format-string #'plural))
99 (prefix (datum->syntax x prefix)))
100 #'(format (guix-warning-port)
101 (ngettext s p n %gettext-domain)
102 (program-name) (program-name) prefix
103 args (... ...))))))))
104
105(define-diagnostic warning "warning: ") ; emit a warning
106
107(define-diagnostic report-error "error: ")
108(define-syntax-rule (leave args ...)
109 "Emit an error message and exit."
110 (begin
111 (report-error args ...)
112 (exit 1)))
113
114(define (install-locale)
115 "Install the current locale settings."
116 (catch 'system-error
117 (lambda _
118 (setlocale LC_ALL ""))
119 (lambda args
120 (warning (_ "failed to install locale: ~a~%")
121 (strerror (system-error-errno args))))))
122
e49951eb 123(define (initialize-guix)
633f045f 124 "Perform the usual initialization for stand-alone Guix commands."
e49951eb 125 (install-locale)
39e9f95d 126 (textdomain %gettext-domain)
e14c3929
LC
127
128 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
129 ;; notified via an EPIPE later.
130 (sigaction SIGPIPE SIG_IGN)
131
e49951eb
MW
132 (setvbuf (current-output-port) _IOLBF)
133 (setvbuf (current-error-port) _IOLBF))
134
cdd5d6f9
LC
135(define* (show-version-and-exit #:optional (command (car (command-line))))
136 "Display version information for COMMAND and `(exit 0)'."
137 (simple-format #t "~a (~a) ~a~%"
138 command %guix-package-name %guix-version)
139 (exit 0))
140
3441e164
LC
141(define (show-bug-report-information)
142 (format #t (_ "
143Report bugs to: ~a.") %guix-bug-report-address)
144 (format #t (_ "
145~a home page: <~a>") %guix-package-name %guix-home-page-url)
146 (display (_ "
147General help using GNU software: <http://www.gnu.org/gethelp/>"))
148 (newline))
149
969e678e
LC
150(define (string->number* str)
151 "Like `string->number', but error out with an error message on failure."
152 (or (string->number str)
153 (leave (_ "~a: invalid number~%") str)))
154
073c34d7
LC
155(define (call-with-error-handling thunk)
156 "Call THUNK within a user-friendly error handler."
157 (guard (c ((package-input-error? c)
158 (let* ((package (package-error-package c))
159 (input (package-error-invalid-input c))
160 (location (package-location package))
161 (file (location-file location))
162 (line (location-line location))
163 (column (location-column location)))
98eb8cbe 164 (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
073c34d7
LC
165 file line column
166 (package-full-name package) input)))
9b222abe
LC
167 ((package-cross-build-system-error? c)
168 (let* ((package (package-error-package c))
169 (loc (package-location package))
170 (system (package-build-system package)))
171 (leave (_ "~a: ~a: build system `~a' does not support cross builds~%")
172 (location->string loc)
173 (package-full-name package)
174 (build-system-name system))))
ef86c39f 175 ((nix-connection-error? c)
98eb8cbe 176 (leave (_ "failed to connect to `~a': ~a~%")
ef86c39f
LC
177 (nix-connection-error-file c)
178 (strerror (nix-connection-error-code c))))
073c34d7
LC
179 ((nix-protocol-error? c)
180 ;; FIXME: Server-provided error messages aren't i18n'd.
98eb8cbe 181 (leave (_ "build failed: ~a~%")
073c34d7 182 (nix-protocol-error-message c))))
e14c3929
LC
183 ;; Catch EPIPE and the likes.
184 (catch 'system-error
185 thunk
186 (lambda args
187 (leave (_ "~a~%")
188 (strerror (system-error-errno args)))))))
073c34d7 189
eb0880e7
LC
190(define (read/eval-package-expression str)
191 "Read and evaluate STR and return the package it refers to, or exit an
192error."
193 (let ((exp (catch #t
194 (lambda ()
195 (call-with-input-string str read))
196 (lambda args
197 (leave (_ "failed to read expression ~s: ~s~%")
198 str args)))))
199 (let ((p (catch #t
200 (lambda ()
201 (eval exp the-scm-module))
202 (lambda args
203 (leave (_ "failed to evaluate expression `~a': ~s~%")
204 exp args)))))
205 (if (package? p)
206 p
207 (leave (_ "expression `~s' does not evaluate to a package~%")
208 exp)))))
209
dd36b51b
LC
210(define* (show-what-to-build store drv
211 #:key dry-run? (use-substitutes? #t))
9bb2b96a 212 "Show what will or would (depending on DRY-RUN?) be built in realizing the
4d60610a 213derivations listed in DRV. Return #t if there's something to build, #f
dd36b51b
LC
214otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are
215available for download."
216 (let*-values (((build download)
59688fc4
LC
217 (fold2 (lambda (drv build download)
218 (let-values (((b d)
219 (derivation-prerequisites-to-build
220 store drv
221 #:use-substitutes?
222 use-substitutes?)))
223 (values (append b build)
224 (append d download))))
dd36b51b
LC
225 '() '()
226 drv))
227 ((build) ; add the DRV themselves
228 (delete-duplicates
59688fc4
LC
229 (append (map derivation-file-name
230 (remove (lambda (drv)
231 (let ((out (derivation->output-path
232 drv)))
233 (or (valid-path? store out)
234 (and use-substitutes?
235 (has-substitutes? store
236 out)))))
237 drv))
dd36b51b
LC
238 (map derivation-input-path build))))
239 ((download) ; add the references of DOWNLOAD
1a8b7834
LC
240 (if use-substitutes?
241 (delete-duplicates
242 (append download
243 (remove (cut valid-path? store <>)
244 (append-map
245 substitutable-references
246 (substitutable-path-info store
247 download)))))
248 download)))
00554b2a 249 ;; TODO: Show the installed size of DOWNLOAD.
9bb2b96a 250 (if dry-run?
dd36b51b
LC
251 (begin
252 (format (current-error-port)
83e61a73
LC
253 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
254 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
dd36b51b
LC
255 (length build))
256 (null? build) build)
257 (format (current-error-port)
83e61a73
LC
258 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
259 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
dd36b51b
LC
260 (length download))
261 (null? download) download))
262 (begin
263 (format (current-error-port)
83e61a73
LC
264 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
265 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
dd36b51b
LC
266 (length build))
267 (null? build) build)
268 (format (current-error-port)
83e61a73
LC
269 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
270 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
dd36b51b
LC
271 (length download))
272 (null? download) download)))
273 (pair? build)))
9bb2b96a 274
073c34d7
LC
275(define-syntax with-error-handling
276 (syntax-rules ()
277 "Run BODY within a user-friendly error condition handler."
278 ((_ body ...)
279 (call-with-error-handling
280 (lambda ()
281 body ...)))))
282
64fc89b6
LC
283(define (location->string loc)
284 "Return a human-friendly, GNU-standard representation of LOC."
285 (match loc
286 (#f (_ "<unknown location>"))
287 (($ <location> file line column)
288 (format #f "~a:~a:~a" file line column))))
289
c61b026e
LC
290(define (switch-symlinks link target)
291 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
292both when LINK already exists and when it does not."
293 (let ((pivot (string-append link ".new")))
294 (symlink target pivot)
295 (rename-file pivot link)))
296
f651b477
LC
297(define (config-directory)
298 "Return the name of the configuration directory, after making sure that it
299exists. Honor the XDG specs,
300<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
301 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
302 (and=> (getenv "HOME")
303 (cut string-append <> "/.config")))
304 (cut string-append <> "/guix"))))
305 (catch 'system-error
306 (lambda ()
307 (mkdir dir)
308 dir)
309 (lambda args
310 (match (system-error-errno args)
311 ((or EEXIST 0)
312 dir)
313 (err
314 (leave (_ "failed to create configuration directory `~a': ~a~%")
315 dir (strerror err))))))))
316
299112d3
LC
317(define* (fill-paragraph str width #:optional (column 0))
318 "Fill STR such that each line contains at most WIDTH characters, assuming
319that the first character is at COLUMN.
320
321When STR contains a single line break surrounded by other characters, it is
322converted to a space; sequences of more than one line break are preserved."
323 (define (maybe-break chr result)
324 (match result
325 ((column newlines chars)
326 (case chr
327 ((#\newline)
328 `(,column ,(+ 1 newlines) ,chars))
329 (else
330 (let ((chars (case newlines
331 ((0) chars)
332 ((1) (cons #\space chars))
333 (else
334 (append (make-list newlines #\newline) chars))))
335 (column (case newlines
336 ((0) column)
337 ((1) (+ 1 column))
338 (else 0))))
339 (let ((chars (cons chr chars))
340 (column (+ 1 column)))
341 (if (> column width)
342 (let*-values (((before after)
343 (break (cut eqv? #\space <>) chars))
344 ((len)
345 (length before)))
346 (if (<= len width)
347 `(,len
348 0
349 ,(if (null? after)
350 before
351 (append before (cons #\newline (cdr after)))))
352 `(,column 0 ,chars))) ; unbreakable
353 `(,column 0 ,chars)))))))))
354
355 (match (string-fold maybe-break
356 `(,column 0 ())
357 str)
358 ((_ _ chars)
359 (list->string (reverse chars)))))
360
361(define (string->recutils str)
362 "Return a version of STR where newlines have been replaced by newlines
363followed by \"+ \", which makes for a valid multi-line field value in the
364`recutils' syntax."
365 (list->string
366 (string-fold-right (lambda (chr result)
367 (if (eqv? chr #\newline)
368 (cons* chr #\+ #\space result)
369 (cons chr result)))
370 '()
371 str)))
372
373(define* (package->recutils p port
374 #:optional (width (or (and=> (getenv "WIDTH")
375 string->number)
376 80)))
377 "Write to PORT a `recutils' record of package P, arranging to fit within
378WIDTH columns."
379 (define (description->recutils str)
380 (let ((str (_ str)))
381 (string->recutils
382 (fill-paragraph str width
383 (string-length "description: ")))))
384
385 ;; Note: Don't i18n field names so that people can post-process it.
386 (format port "name: ~a~%" (package-name p))
387 (format port "version: ~a~%" (package-version p))
388 (format port "location: ~a~%"
389 (or (and=> (package-location p) location->string)
390 (_ "unknown")))
391 (format port "home-page: ~a~%" (package-home-page p))
392 (format port "license: ~a~%"
393 (match (package-license p)
394 (((? license? licenses) ...)
395 (string-join (map license-name licenses)
396 ", "))
397 ((? license? license)
398 (license-name license))
399 (x
400 (_ "unknown"))))
401 (format port "synopsis: ~a~%"
402 (string-map (match-lambda
403 (#\newline #\space)
404 (chr chr))
405 (or (and=> (package-synopsis p) _)
406 "")))
407 (format port "description: ~a~%"
408 (and=> (package-description p) description->recutils))
409 (newline port))
410
2cd09108
NK
411(define (string->generations str)
412 "Return the list of generations matching a pattern in STR. This function
413accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
414 (define (maybe-integer)
415 (let ((x (string->number str)))
416 (and (integer? x)
417 x)))
418
419 (define (maybe-comma-separated-integers)
420 (let ((lst (delete-duplicates
421 (map string->number
422 (string-split str #\,)))))
423 (and (every integer? lst)
424 lst)))
425
426 (cond ((maybe-integer)
427 =>
428 list)
429 ((maybe-comma-separated-integers)
430 =>
431 identity)
432 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
433 =>
434 (lambda (match)
435 (let ((s (string->number (match:substring match 1)))
436 (e (string->number (match:substring match 2))))
437 (and (every integer? (list s e))
438 (<= s e)
439 (iota (1+ (- e s)) s)))))
440 ((string-match "^([0-9]+)\\.\\.$" str)
441 =>
442 (lambda (match)
443 (let ((s (string->number (match:substring match 1))))
444 (and (integer? s)
445 `(>= ,s)))))
446 ((string-match "^\\.\\.([0-9]+)$" str)
447 =>
448 (lambda (match)
449 (let ((e (string->number (match:substring match 1))))
450 (and (integer? e)
451 `(<= ,e)))))
452 (else #f)))
453
454(define (string->duration str)
455 "Return the duration matching a pattern in STR. This function accepts the
456following patterns: \"1d\", \"1w\", \"1m\"."
457 (define (hours->duration hours match)
458 (make-time time-duration 0
459 (* 3600 hours (string->number (match:substring match 1)))))
460
461 (cond ((string-match "^([0-9]+)d$" str)
462 =>
463 (lambda (match)
464 (hours->duration 24 match)))
465 ((string-match "^([0-9]+)w$" str)
466 =>
467 (lambda (match)
468 (hours->duration (* 24 7) match)))
469 ((string-match "^([0-9]+)m$" str)
470 =>
471 (lambda (match)
472 (hours->duration (* 24 30) match)))
473 (else #f)))
474
a5975ced
LC
475(define (args-fold* options unrecognized-option-proc operand-proc . seeds)
476 "A wrapper on top of `args-fold' that does proper user-facing error
477reporting."
478 (catch 'misc-error
479 (lambda ()
480 (apply args-fold options unrecognized-option-proc
481 operand-proc seeds))
482 (lambda (key proc msg args . rest)
483 ;; XXX: MSG is not i18n'd.
484 (leave (_ "invalid argument: ~a~%")
485 (apply format #f msg args)))))
486
e49951eb 487(define (show-guix-usage)
e49951eb 488 (format (current-error-port)
25c93676
LC
489 (_ "Try `guix --help' for more information.~%"))
490 (exit 1))
e49951eb 491
e31ff8b8
LC
492(define (command-files)
493 "Return the list of source files that define Guix sub-commands."
494 (define directory
495 (and=> (search-path %load-path "guix.scm")
496 (compose (cut string-append <> "/guix/scripts")
497 dirname)))
498
2b8cf44f
LC
499 (define dot-scm?
500 (cut string-suffix? ".scm" <>))
501
502 ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though
503 ;; they don't match `dot-scm?'. Work around it by doing additional
504 ;; filtering.
e31ff8b8 505 (if directory
2b8cf44f 506 (filter dot-scm? (scandir directory dot-scm?))
e31ff8b8
LC
507 '()))
508
509(define (commands)
510 "Return the list of Guix command names."
511 (map (compose (cut string-drop-right <> 4)
512 basename)
513 (command-files)))
514
515(define (show-guix-help)
516 (format #t (_ "Usage: guix COMMAND ARGS...
517Run COMMAND with ARGS.\n"))
518 (newline)
519 (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
520 (newline)
521 ;; TODO: Display a synopsis of each command.
b30b13dc 522 (format #t "~{ ~a~%~}" (sort (commands) string<?))
e31ff8b8
LC
523 (show-bug-report-information))
524
a2011be5
LC
525(define program-name
526 ;; Name of the command-line program currently executing, or #f.
527 (make-parameter #f))
528
ec5d0a85
LC
529(define (run-guix-command command . args)
530 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
531found."
532 (define module
533 (catch 'misc-error
534 (lambda ()
535 (resolve-interface `(guix scripts ,command)))
536 (lambda -
25c93676
LC
537 (format (current-error-port)
538 (_ "guix: ~a: command not found~%") command)
539 (show-guix-usage))))
ec5d0a85
LC
540
541 (let ((command-main (module-ref module
542 (symbol-append 'guix- command))))
543 (parameterize ((program-name command))
544 (apply command-main args))))
545
a2011be5
LC
546(define guix-warning-port
547 (make-parameter (current-warning-port)))
548
e49951eb
MW
549(define (guix-main arg0 . args)
550 (initialize-guix)
551 (let ()
552 (define (option? str) (string-prefix? "-" str))
553 (match args
25c93676
LC
554 (()
555 (format (current-error-port)
556 (_ "guix: missing command name~%"))
557 (show-guix-usage))
e12b3eb9 558 ((or ("-h") ("--help"))
25c93676
LC
559 (show-guix-help))
560 (("--version")
561 (show-version-and-exit "guix"))
562 (((? option? o) args ...)
563 (format (current-error-port)
564 (_ "guix: unrecognized option '~a'~%") o)
565 (show-guix-usage))
e49951eb 566 ((command args ...)
ec5d0a85
LC
567 (apply run-guix-command
568 (string->symbol command)
569 args)))))
e49951eb 570
073c34d7 571;;; ui.scm ends here