1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
5 ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
6 ;;; Copyright © 2014 Deck Pickard <deck.r.pickard@gmail.com>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (guix ui)
24 #:use-module (guix utils)
25 #:use-module (guix store)
26 #:use-module (guix config)
27 #:use-module (guix packages)
28 #:use-module (guix profiles)
29 #:use-module (guix derivations)
30 #:use-module (guix build-system)
31 #:use-module (guix serialization)
32 #:use-module ((guix build utils) #:select (mkdir-p))
33 #:use-module ((guix licenses) #:select (license? license-name))
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
36 #:use-module (srfi srfi-19)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-31)
39 #:use-module (srfi srfi-34)
40 #:use-module (srfi srfi-35)
41 #:use-module (srfi srfi-37)
42 #:autoload (ice-9 ftw) (scandir)
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 format)
45 #:use-module (ice-9 regex)
46 #:autoload (system repl repl) (start-repl)
47 #:autoload (system repl debug) (make-debug stack->vector)
57 show-bug-report-information
62 show-manifest-transaction
63 call-with-error-handling
66 read/eval-package-expression
73 package-specification->name+version+output
86 ;;; User interface facilities for command-line tools.
90 (define %gettext-domain
91 ;; Text domain for strings used in the tools.
94 (define %package-text-domain
95 ;; Text domain for package synopses and descriptions.
98 (define _ (cut gettext <> %gettext-domain))
99 (define N_ (cut ngettext <> <> <> %gettext-domain))
100 (define P_ (cut gettext <> %package-text-domain))
102 (define-syntax-rule (define-diagnostic name prefix)
103 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
107 (define (augmented-format-string fmt)
108 (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
111 ((name (underscore fmt) args (... ...))
112 (and (string? (syntax->datum #'fmt))
113 (free-identifier=? #'underscore #'_))
114 (with-syntax ((fmt* (augmented-format-string #'fmt))
115 (prefix (datum->syntax x prefix)))
116 #'(format (guix-warning-port) (gettext fmt*)
117 (program-name) (program-name) prefix
119 ((name (N-underscore singular plural n) args (... ...))
120 (and (string? (syntax->datum #'singular))
121 (string? (syntax->datum #'plural))
122 (free-identifier=? #'N-underscore #'N_))
123 (with-syntax ((s (augmented-format-string #'singular))
124 (p (augmented-format-string #'plural))
125 (prefix (datum->syntax x prefix)))
126 #'(format (guix-warning-port)
127 (ngettext s p n %gettext-domain)
128 (program-name) (program-name) prefix
129 args (... ...))))))))
131 (define-diagnostic warning "warning: ") ; emit a warning
133 (define-diagnostic report-error "error: ")
134 (define-syntax-rule (leave args ...)
135 "Emit an error message and exit."
137 (report-error args ...)
140 (define (make-user-module modules)
141 "Return a new user module with the additional MODULES loaded."
142 ;; Module in which the machine description file is loaded.
143 (let ((module (make-fresh-user-module)))
144 (for-each (lambda (iface)
145 (module-use! module (resolve-interface iface)))
149 (define* (load* file user-module
150 #:key (on-error 'nothing-special))
151 "Load the user provided Scheme source code FILE."
152 (define (frame-with-source frame)
153 ;; Walk from FRAME upwards until source location information is found.
154 (let loop ((frame frame)
158 (if (frame-source frame)
160 (loop (frame-previous frame) frame)))))
162 (define (error-string frame args)
163 (call-with-output-string
165 (apply display-error frame port (cdr args)))))
168 (make-prompt-tag "user-code"))
172 ;; XXX: Force a recompilation to avoid ABI issues.
173 (set! %fresh-auto-compile #t)
174 (set! %load-should-auto-compile #t)
176 (save-module-excursion
178 (set-current-module user-module)
180 ;; Hide the "auto-compiling" messages.
181 (parameterize ((current-warning-port (%make-void-port "w")))
182 (call-with-prompt tag
184 ;; Give 'load' an absolute file name so that it doesn't try to
185 ;; search for FILE in %LOAD-PATH. Note: use 'load', not
186 ;; 'primitive-load', so that FILE is compiled, which then allows us
187 ;; to provide better error reporting with source line numbers.
188 (load (canonicalize-path file)))
191 ;; XXX: Errors are reported from the pre-unwind handler below, but
192 ;; calling 'exit' from there has no effect, so we call it here.
194 (rec (handle-error . args)
195 ;; Capture the stack up to this procedure call, excluded, and pass
196 ;; the faulty stack frame to 'report-load-error'.
197 (let* ((stack (make-stack #t handle-error tag))
198 (depth (stack-length stack))
199 (last (and (> depth 0) (stack-ref stack 0)))
200 (frame (frame-with-source
202 (stack-ref stack 1) ;skip the 'throw' frame
205 (report-load-error file args frame)
210 (display (_ "entering debugger; type ',bt' for a backtrace\n"))
211 (start-repl #:debug (make-debug (stack->vector stack) 0
212 (error-string frame args)
215 (newline (current-error-port))
216 (display-backtrace stack (current-error-port)))
220 (define* (report-load-error file args #:optional frame)
221 "Report the failure to load FILE, a user-provided Scheme file.
222 ARGS is the list of arguments received by the 'throw' handler."
225 (let ((err (system-error-errno args)))
226 (report-error (_ "failed to load '~a': ~a~%") file (strerror err))))
227 (('syntax-error proc message properties form . rest)
228 (let ((loc (source-properties->location properties)))
229 (format (current-error-port) (_ "~a: error: ~a~%")
230 (location->string loc) message)))
232 (report-error (_ "exception thrown: ~s~%") obj))
234 (report-error (_ "failed to load '~a':~%") file)
235 (apply display-error frame (current-error-port) args))))
237 (define (warn-about-load-error file args) ;FIXME: factorize with ↑
238 "Report the failure to load FILE, a user-provided Scheme file, without
239 exiting. ARGS is the list of arguments received by the 'throw' handler."
242 (let ((err (system-error-errno args)))
243 (warning (_ "failed to load '~a': ~a~%") file (strerror err))))
244 (('syntax-error proc message properties form . rest)
245 (let ((loc (source-properties->location properties)))
246 (format (current-error-port) (_ "~a: warning: ~a~%")
247 (location->string loc) message)))
249 (warning (_ "failed to load '~a': exception thrown: ~s~%")
252 (warning (_ "failed to load '~a':~%") file)
253 (apply display-error #f (current-error-port) args))))
255 (define (install-locale)
256 "Install the current locale settings."
259 (setlocale LC_ALL ""))
261 (warning (_ "failed to install locale: ~a~%")
262 (strerror (system-error-errno args))))))
264 (define (initialize-guix)
265 "Perform the usual initialization for stand-alone Guix commands."
267 (textdomain %gettext-domain)
269 ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
270 ;; notified via an EPIPE later.
271 (sigaction SIGPIPE SIG_IGN)
273 (setvbuf (current-output-port) _IOLBF)
274 (setvbuf (current-error-port) _IOLBF))
276 (define* (show-version-and-exit #:optional (command (car (command-line))))
277 "Display version information for COMMAND and `(exit 0)'."
278 (simple-format #t "~a (~a) ~a~%"
279 command %guix-package-name %guix-version)
280 (display (_ "Copyright (C) 2015 the Guix authors
281 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
282 This is free software: you are free to change and redistribute it.
283 There is NO WARRANTY, to the extent permitted by law.
287 (define (show-bug-report-information)
289 Report bugs to: ~a.") %guix-bug-report-address)
291 ~a home page: <~a>") %guix-package-name %guix-home-page-url)
293 General help using GNU software: <http://www.gnu.org/gethelp/>"))
297 ;; We 'set!' the global binding because (gnu build ...) modules and similar
298 ;; typically don't use (guix ui).
299 (let ((real-symlink (@ (guile) symlink)))
300 (lambda (target link)
301 "This is a 'symlink' replacement that provides proper error reporting."
304 (real-symlink target link))
305 (lambda (key proc fmt args errno)
306 ;; Augment the FMT and ARGS with information about LINK (this
307 ;; information is missing as of Guile 2.0.11, making the exception
309 (apply throw key proc "~A: ~S"
310 (list (strerror (car errno)) link)
314 ;; Note: here we use 'set!', not #:replace, because UIs typically use
315 ;; 'copy-recursively', which doesn't use (guix ui).
316 (let ((real-copy-file (@ (guile) copy-file)))
317 (lambda (source target)
318 "This is a 'copy-file' replacement that provides proper error reporting."
321 (real-copy-file source target))
322 (lambda (key proc fmt args errno)
323 ;; Augment the FMT and ARGS with information about TARGET (this
324 ;; information is missing as of Guile 2.0.11, making the exception
326 (apply throw key proc "~A: ~S"
327 (list (strerror (car errno)) target)
330 (define (string->number* str)
331 "Like `string->number', but error out with an error message on failure."
332 (or (string->number str)
333 (leave (_ "~a: invalid number~%") str)))
335 (define (size->number str)
336 "Convert STR, a storage measurement representation such as \"1024\" or
337 \"1MiB\", to a number of bytes. Raise an error if STR could not be
340 (string-rindex str char-set:digit))
343 (and unit-pos (substring str (+ 1 unit-pos))))
345 (let* ((numstr (if unit-pos
346 (substring str 0 (+ 1 unit-pos))
348 (num (string->number numstr)))
350 (leave (_ "invalid number: ~a~%") numstr))
352 ((compose inexact->exact round)
355 ((or "KiB" "K" "k") (expt 2 10))
356 ((or "MiB" "M") (expt 2 20))
357 ((or "GiB" "G") (expt 2 30))
358 ((or "TiB" "T") (expt 2 40))
359 ((or "PiB" "P") (expt 2 50))
360 ((or "EiB" "E") (expt 2 60))
361 ((or "ZiB" "Z") (expt 2 70))
362 ((or "YiB" "Y") (expt 2 80))
373 (leave (_ "unknown unit: ~a~%") unit)))))))
375 (define (call-with-error-handling thunk)
376 "Call THUNK within a user-friendly error handler."
377 (guard (c ((package-input-error? c)
378 (let* ((package (package-error-package c))
379 (input (package-error-invalid-input c))
380 (location (package-location package))
381 (file (location-file location))
382 (line (location-line location))
383 (column (location-column location)))
384 (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
386 (package-full-name package) input)))
387 ((package-cross-build-system-error? c)
388 (let* ((package (package-error-package c))
389 (loc (package-location package))
390 (system (package-build-system package)))
391 (leave (_ "~a: ~a: build system `~a' does not support cross builds~%")
392 (location->string loc)
393 (package-full-name package)
394 (build-system-name system))))
395 ((profile-not-found-error? c)
396 (leave (_ "profile '~a' does not exist~%")
397 (profile-error-profile c)))
398 ((missing-generation-error? c)
399 (leave (_ "generation ~a of profile '~a' does not exist~%")
400 (missing-generation-error-generation c)
401 (profile-error-profile c)))
403 (let ((file (nar-error-file c))
404 (port (nar-error-port c)))
406 (leave (_ "corrupt input while restoring '~a' from ~s~%")
407 file (or (port-filename port) port))
408 (leave (_ "corrupt input while restoring archive from ~s~%")
409 (or (port-filename port) port)))))
410 ((nix-connection-error? c)
411 (leave (_ "failed to connect to `~a': ~a~%")
412 (nix-connection-error-file c)
413 (strerror (nix-connection-error-code c))))
414 ((nix-protocol-error? c)
415 ;; FIXME: Server-provided error messages aren't i18n'd.
416 (leave (_ "build failed: ~a~%")
417 (nix-protocol-error-message c)))
418 ((derivation-missing-output-error? c)
419 (leave (_ "reference to invalid output '~a' of derivation '~a'~%")
420 (derivation-missing-output c)
421 (derivation-file-name (derivation-error-derivation c))))
422 ((message-condition? c)
423 ;; Normally '&message' error conditions have an i18n'd message.
425 (gettext (condition-message c) %gettext-domain))))
426 ;; Catch EPIPE and the likes.
429 (lambda (key proc format-string format-args . rest)
430 (leave (_ "~a: ~a~%") proc
431 (apply format #f format-string format-args))))))
433 (define %guix-user-module
434 ;; Module in which user expressions are evaluated.
435 ;; Compute lazily to avoid circularity with (guix gexp).
437 (let ((module (make-module)))
438 (beautify-user-module! module)
439 ;; Use (guix gexp) so that one can use #~ & co.
440 (module-use! module (resolve-interface '(guix gexp)))
443 (define (read/eval str)
444 "Read and evaluate STR, raising an error if something goes wrong."
447 (call-with-input-string str read))
449 (leave (_ "failed to read expression ~s: ~s~%")
453 (eval exp (force %guix-user-module)))
455 (report-error (_ "failed to evaluate expression '~a':~%") exp)
457 (('syntax-error proc message properties form . rest)
458 (report-error (_ "syntax error: ~a~%") message))
460 (report-error (_ "exception thrown: ~s~%") obj))
462 (apply display-error #f (current-error-port) args))
466 (define (read/eval-package-expression str)
467 "Read and evaluate STR and return the package it refers to, or exit an
469 (match (read/eval str)
472 (leave (_ "expression ~s does not evaluate to a package~%")
475 (define* (show-what-to-build store drv
476 #:key dry-run? (use-substitutes? #t))
477 "Show what will or would (depending on DRY-RUN?) be built in realizing the
478 derivations listed in DRV. Return #t if there's something to build, #f
479 otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are
480 available for download."
481 (define substitutable?
482 ;; Call 'substitutation-oracle' upfront so we don't end up launching the
483 ;; substituter many times. This makes a big difference, especially when
484 ;; DRV is a long list as is the case with 'guix environment'.
486 (substitution-oracle store drv)
489 (define (built-or-substitutable? drv)
490 (or (null? (derivation-outputs drv))
491 (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists
492 (or (valid-path? store out)
493 (substitutable? out)))))
495 (let*-values (((build download)
496 (fold2 (lambda (drv build download)
498 (derivation-prerequisites-to-build
500 #:substitutable? substitutable?)))
501 (values (append b build)
502 (append d download))))
505 ((build) ; add the DRV themselves
507 (append (map derivation-file-name
508 (remove built-or-substitutable? drv))
509 (map derivation-input-path build))))
510 ((download) ; add the references of DOWNLOAD
514 (remove (cut valid-path? store <>)
516 substitutable-references
517 (substitutable-path-info store
520 ;; TODO: Show the installed size of DOWNLOAD.
523 (format (current-error-port)
524 (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
525 "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
528 (format (current-error-port)
529 (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
530 "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
532 (null? download) download))
534 (format (current-error-port)
535 (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
536 "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
539 (format (current-error-port)
540 (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
541 "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
543 (null? download) download)))
546 (define show-what-to-build*
547 (store-lift show-what-to-build))
549 (define (right-arrow port)
550 "Return either a string containing the 'RIGHT ARROW' character, or an ASCII
551 replacement if PORT is not Unicode-capable."
552 (with-fluids ((%default-port-encoding (port-encoding port)))
554 (catch 'encoding-error
556 (call-with-output-string
558 (set-port-conversion-strategy! port 'error)
559 (display arrow port))))
563 (define* (show-manifest-transaction store manifest transaction
565 "Display what will/would be installed/removed from MANIFEST by TRANSACTION."
566 (define (package-strings name version output item)
567 (map (lambda (name version output item)
568 (format #f " ~a~:[:~a~;~*~]\t~a\t~a"
570 (equal? output "out") output version
572 (package-output store item output)
574 name version output item))
576 (define → ;an arrow that can be represented on stderr
577 (right-arrow (current-error-port)))
579 (define (upgrade-string name old-version new-version output item)
580 (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a"
581 name (equal? output "out") output
582 old-version → new-version
584 (package-output store item output)
587 (let-values (((remove install upgrade downgrade)
588 (manifest-transaction-effects manifest transaction)))
590 ((($ <manifest-entry> name version output item) ..1)
591 (let ((len (length name))
592 (remove (package-strings name version output item)))
594 (format (current-error-port)
595 (N_ "The following package would be removed:~%~{~a~%~}~%"
596 "The following packages would be removed:~%~{~a~%~}~%"
599 (format (current-error-port)
600 (N_ "The following package will be removed:~%~{~a~%~}~%"
601 "The following packages will be removed:~%~{~a~%~}~%"
606 (((($ <manifest-entry> name old-version)
607 . ($ <manifest-entry> _ new-version output item)) ..1)
608 (let ((len (length name))
609 (downgrade (map upgrade-string
610 name old-version new-version output item)))
612 (format (current-error-port)
613 (N_ "The following package would be downgraded:~%~{~a~%~}~%"
614 "The following packages would be downgraded:~%~{~a~%~}~%"
617 (format (current-error-port)
618 (N_ "The following package will be downgraded:~%~{~a~%~}~%"
619 "The following packages will be downgraded:~%~{~a~%~}~%"
624 (((($ <manifest-entry> name old-version)
625 . ($ <manifest-entry> _ new-version output item)) ..1)
626 (let ((len (length name))
627 (upgrade (map upgrade-string
628 name old-version new-version output item)))
630 (format (current-error-port)
631 (N_ "The following package would be upgraded:~%~{~a~%~}~%"
632 "The following packages would be upgraded:~%~{~a~%~}~%"
635 (format (current-error-port)
636 (N_ "The following package will be upgraded:~%~{~a~%~}~%"
637 "The following packages will be upgraded:~%~{~a~%~}~%"
642 ((($ <manifest-entry> name version output item _) ..1)
643 (let ((len (length name))
644 (install (package-strings name version output item)))
646 (format (current-error-port)
647 (N_ "The following package would be installed:~%~{~a~%~}~%"
648 "The following packages would be installed:~%~{~a~%~}~%"
651 (format (current-error-port)
652 (N_ "The following package will be installed:~%~{~a~%~}~%"
653 "The following packages will be installed:~%~{~a~%~}~%"
658 (define-syntax with-error-handling
660 "Run BODY within a user-friendly error condition handler."
662 (call-with-error-handling
666 (define (location->string loc)
667 "Return a human-friendly, GNU-standard representation of LOC."
669 (#f (_ "<unknown location>"))
670 (($ <location> file line column)
671 (format #f "~a:~a:~a" file line column))))
673 (define (switch-symlinks link target)
674 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
675 both when LINK already exists and when it does not."
676 (let ((pivot (string-append link ".new")))
677 (symlink target pivot)
678 (rename-file pivot link)))
680 (define (config-directory)
681 "Return the name of the configuration directory, after making sure that it
682 exists. Honor the XDG specs,
683 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
684 (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
685 (and=> (getenv "HOME")
686 (cut string-append <> "/.config")))
687 (cut string-append <> "/guix"))))
693 (let ((err (system-error-errno args)))
694 ;; ERR is necessarily different from EEXIST.
695 (leave (_ "failed to create configuration directory `~a': ~a~%")
696 dir (strerror err)))))))
698 (define* (fill-paragraph str width #:optional (column 0))
699 "Fill STR such that each line contains at most WIDTH characters, assuming
700 that the first character is at COLUMN.
702 When STR contains a single line break surrounded by other characters, it is
703 converted to a space; sequences of more than one line break are preserved."
704 (define (maybe-break chr result)
706 ((column newlines chars)
709 `(,column ,(+ 1 newlines) ,chars))
711 (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1))
712 (chars (case newlines
715 (append (make-list spaces #\space) chars))
717 (append (make-list newlines #\newline) chars))))
718 (column (case newlines
720 ((1) (+ spaces column))
722 (let ((chars (cons chr chars))
723 (column (+ 1 column)))
725 (let*-values (((before after)
726 (break (cut eqv? #\space <>) chars))
736 (drop-while (cut eqv? #\space <>)
738 `(,column 0 ,chars))) ; unbreakable
739 `(,column 0 ,chars)))))))))
741 (match (string-fold maybe-break
745 (list->string (reverse chars)))))
752 (define (string->recutils str)
753 "Return a version of STR where newlines have been replaced by newlines
754 followed by \"+ \", which makes for a valid multi-line field value in the
757 (string-fold-right (lambda (chr result)
758 (if (eqv? chr #\newline)
759 (cons* chr #\+ #\space result)
764 (define* (package->recutils p port
765 #:optional (width (or (and=> (getenv "WIDTH")
768 "Write to PORT a `recutils' record of package P, arranging to fit within
770 (define (description->recutils str)
771 (let ((str (P_ str)))
773 (fill-paragraph str width
774 (string-length "description: ")))))
776 (define (dependencies->recutils packages)
777 (let ((list (string-join (map package-full-name
778 (sort packages package<?)) " ")))
780 (fill-paragraph list width
781 (string-length "dependencies: ")))))
783 (define (package<? p1 p2)
784 (string<? (package-full-name p1) (package-full-name p2)))
786 ;; Note: Don't i18n field names so that people can post-process it.
787 (format port "name: ~a~%" (package-name p))
788 (format port "version: ~a~%" (package-version p))
789 (format port "systems: ~a~%"
790 (string-join (package-transitive-supported-systems p)))
791 (format port "dependencies: ~a~%"
792 (match (package-direct-inputs p)
793 (((labels inputs . _) ...)
794 (dependencies->recutils (filter package? inputs)))))
795 (format port "location: ~a~%"
796 (or (and=> (package-location p) location->string)
799 ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in
800 ;; field identifiers.
801 (format port "homepage: ~a~%" (package-home-page p))
803 (format port "license: ~a~%"
804 (match (package-license p)
805 (((? license? licenses) ...)
806 (string-join (map license-name licenses)
808 ((? license? license)
809 (license-name license))
812 (format port "synopsis: ~a~%"
813 (string-map (match-lambda
816 (or (and=> (package-synopsis p) P_)
818 (format port "description: ~a~%"
819 (and=> (package-description p) description->recutils))
822 (define (string->generations str)
823 "Return the list of generations matching a pattern in STR. This function
824 accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
825 (define (maybe-integer)
826 (let ((x (string->number str)))
830 (define (maybe-comma-separated-integers)
831 (let ((lst (delete-duplicates
833 (string-split str #\,)))))
834 (and (every integer? lst)
837 (cond ((maybe-integer)
840 ((maybe-comma-separated-integers)
843 ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
846 (let ((s (string->number (match:substring match 1)))
847 (e (string->number (match:substring match 2))))
848 (and (every integer? (list s e))
850 (iota (1+ (- e s)) s)))))
851 ((string-match "^([0-9]+)\\.\\.$" str)
854 (let ((s (string->number (match:substring match 1))))
857 ((string-match "^\\.\\.([0-9]+)$" str)
860 (let ((e (string->number (match:substring match 1))))
865 (define (string->duration str)
866 "Return the duration matching a pattern in STR. This function accepts the
867 following patterns: \"1d\", \"1w\", \"1m\"."
868 (define (hours->duration hours match)
869 (make-time time-duration 0
870 (* 3600 hours (string->number (match:substring match 1)))))
872 (cond ((string-match "^([0-9]+)d$" str)
875 (hours->duration 24 match)))
876 ((string-match "^([0-9]+)w$" str)
879 (hours->duration (* 24 7) match)))
880 ((string-match "^([0-9]+)m$" str)
883 (hours->duration (* 24 30) match)))
886 (define* (package-specification->name+version+output spec
887 #:optional (output "out"))
888 "Parse package specification SPEC and return three value: the specified
889 package name, version number (or #f), and output name (or OUTPUT). SPEC may
890 optionally contain a version number and an output name, as in these examples:
897 (let*-values (((name sub-drv)
898 (match (string-rindex spec #\:)
899 (#f (values spec output))
900 (colon (values (substring spec 0 colon)
901 (substring spec (+ 1 colon))))))
903 (package-name->name+version name)))
904 (values name version sub-drv)))
908 ;;; Command-line option processing.
911 (define (args-fold* options unrecognized-option-proc operand-proc . seeds)
912 "A wrapper on top of `args-fold' that does proper user-facing error
916 (apply args-fold options unrecognized-option-proc
918 (lambda (key proc msg args . rest)
919 ;; XXX: MSG is not i18n'd.
920 (leave (_ "invalid argument: ~a~%")
921 (apply format #f msg args)))))
923 (define (environment-build-options)
924 "Return additional build options passed as environment variables."
925 (arguments-from-environment-variable "GUIX_BUILD_OPTIONS"))
927 (define %default-argument-handler
928 ;; The default handler for non-option command-line arguments.
930 (alist-cons 'argument arg result)))
932 (define* (parse-command-line args options seeds
934 (argument-handler %default-argument-handler))
935 "Parse the command-line arguments ARGS as well as arguments passed via the
936 'GUIX_BUILD_OPTIONS' environment variable according to OPTIONS (a list of
937 SRFI-37 options) and return the result, seeded by SEEDS.
938 Command-line options take precedence those passed via 'GUIX_BUILD_OPTIONS'.
940 ARGUMENT-HANDLER is called for non-option arguments, like the 'operand-proc'
941 parameter of 'args-fold'."
942 (define (parse-options-from args seeds)
943 ;; Actual parsing takes place here.
944 (apply args-fold* args options
945 (lambda (opt name arg . rest)
946 (leave (_ "~A: unrecognized option~%") name))
952 (parse-options-from (environment-build-options) seeds))
954 ;; ARGS take precedence over what the environment variable specifies.
955 (parse-options-from args seeds))))
957 (define (show-guix-usage)
958 (format (current-error-port)
959 (_ "Try `guix --help' for more information.~%"))
962 (define (command-files)
963 "Return the list of source files that define Guix sub-commands."
965 (and=> (search-path %load-path "guix.scm")
966 (compose (cut string-append <> "/guix/scripts")
970 (cut string-suffix? ".scm" <>))
973 (scandir directory dot-scm?)
977 "Return the list of Guix command names."
978 (map (compose (cut string-drop-right <> 4)
982 (define (show-guix-help)
983 (define (internal? command)
984 (member command '("substitute" "authenticate" "offload")))
986 (format #t (_ "Usage: guix COMMAND ARGS...
987 Run COMMAND with ARGS.\n"))
989 (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
991 ;; TODO: Display a synopsis of each command.
992 (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
994 (show-bug-report-information))
997 ;; Name of the command-line program currently executing, or #f.
1000 (define (run-guix-command command . args)
1001 "Run COMMAND with the given ARGS. Report an error when COMMAND is not
1006 (resolve-interface `(guix scripts ,command)))
1008 (format (current-error-port)
1009 (_ "guix: ~a: command not found~%") command)
1010 (show-guix-usage))))
1012 (let ((command-main (module-ref module
1013 (symbol-append 'guix- command))))
1014 (parameterize ((program-name command))
1015 (apply command-main args))))
1017 (define guix-warning-port
1018 (make-parameter (current-warning-port)))
1020 (define (guix-main arg0 . args)
1023 (define (option? str) (string-prefix? "-" str))
1026 (format (current-error-port)
1027 (_ "guix: missing command name~%"))
1029 ((or ("-h") ("--help"))
1032 (show-version-and-exit "guix"))
1033 (((? option? o) args ...)
1034 (format (current-error-port)
1035 (_ "guix: unrecognized option '~a'~%") o)
1040 (apply run-guix-command
1041 (string->symbol command)
1044 ;;; ui.scm ends here