You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1260 lines
48 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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 Cyril Roelandt <tipecaml@gmail.com>
  6. ;;; Copyright © 2014 Cyrill Schenkel <cyrill.schenkel@gmail.com>
  7. ;;; Copyright © 2014, 2015 Alex Kost <alezost@gmail.com>
  8. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  9. ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  10. ;;; Copyright © 2016 Roel Janssen <roel@gnu.org>
  11. ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch>
  12. ;;;
  13. ;;; This file is part of GNU Guix.
  14. ;;;
  15. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  16. ;;; under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 3 of the License, or (at
  18. ;;; your option) any later version.
  19. ;;;
  20. ;;; GNU Guix is distributed in the hope that it will be useful, but
  21. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  27. (define-module (guix ui)
  28. #:use-module (guix utils)
  29. #:use-module (guix store)
  30. #:use-module (guix config)
  31. #:use-module (guix packages)
  32. #:use-module (guix profiles)
  33. #:use-module (guix derivations)
  34. #:use-module (guix combinators)
  35. #:use-module (guix build-system)
  36. #:use-module (guix serialization)
  37. #:use-module ((guix build utils) #:select (mkdir-p))
  38. #:use-module ((guix licenses) #:select (license? license-name))
  39. #:use-module ((guix build syscalls) #:select (terminal-columns))
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-19)
  43. #:use-module (srfi srfi-26)
  44. #:use-module (srfi srfi-31)
  45. #:use-module (srfi srfi-34)
  46. #:use-module (srfi srfi-35)
  47. #:autoload (ice-9 ftw) (scandir)
  48. #:use-module (ice-9 match)
  49. #:use-module (ice-9 format)
  50. #:use-module (ice-9 regex)
  51. #:autoload (system repl repl) (start-repl)
  52. #:autoload (system repl debug) (make-debug stack->vector)
  53. #:use-module (texinfo)
  54. #:use-module (texinfo plain-text)
  55. #:use-module (texinfo string-utils)
  56. #:export (_
  57. N_
  58. P_
  59. report-error
  60. leave
  61. make-user-module
  62. load*
  63. warn-about-load-error
  64. show-version-and-exit
  65. show-bug-report-information
  66. make-regexp*
  67. string->number*
  68. size->number
  69. show-derivation-outputs
  70. show-what-to-build
  71. show-what-to-build*
  72. show-manifest-transaction
  73. call-with-error-handling
  74. with-error-handling
  75. leave-on-EPIPE
  76. read/eval
  77. read/eval-package-expression
  78. location->string
  79. config-directory
  80. fill-paragraph
  81. texi->plain-text
  82. package-description-string
  83. string->recutils
  84. package->recutils
  85. package-specification->name+version+output
  86. string->generations
  87. string->duration
  88. matching-generations
  89. display-generation
  90. display-profile-content
  91. display-profile-content-diff
  92. roll-back*
  93. switch-to-generation*
  94. delete-generation*
  95. run-guix-command
  96. run-guix
  97. program-name
  98. guix-warning-port
  99. warning
  100. info
  101. guix-main))
  102. ;;; Commentary:
  103. ;;;
  104. ;;; User interface facilities for command-line tools.
  105. ;;;
  106. ;;; Code:
  107. (define %gettext-domain
  108. ;; Text domain for strings used in the tools.
  109. "guix")
  110. (define %package-text-domain
  111. ;; Text domain for package synopses and descriptions.
  112. "guix-packages")
  113. (define _ (cut gettext <> %gettext-domain))
  114. (define N_ (cut ngettext <> <> <> %gettext-domain))
  115. (define (P_ msgid)
  116. "Return the translation of the package description or synopsis MSGID."
  117. ;; Descriptions/synopses might occasionally be empty strings, even if that
  118. ;; is something we try to avoid. Since (gettext "") can return a non-empty
  119. ;; string, explicitly check for that case.
  120. (if (string-null? msgid)
  121. msgid
  122. (gettext msgid %package-text-domain)))
  123. (define-syntax-rule (define-diagnostic name prefix)
  124. "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
  125. messages."
  126. (define-syntax name
  127. (lambda (x)
  128. (define (augmented-format-string fmt)
  129. (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt)))
  130. (syntax-case x ()
  131. ((name (underscore fmt) args (... ...))
  132. (and (string? (syntax->datum #'fmt))
  133. (free-identifier=? #'underscore #'_))
  134. (with-syntax ((fmt* (augmented-format-string #'fmt))
  135. (prefix (datum->syntax x prefix)))
  136. #'(format (guix-warning-port) (gettext fmt*)
  137. (program-name) (program-name) prefix
  138. args (... ...))))
  139. ((name (N-underscore singular plural n) args (... ...))
  140. (and (string? (syntax->datum #'singular))
  141. (string? (syntax->datum #'plural))
  142. (free-identifier=? #'N-underscore #'N_))
  143. (with-syntax ((s (augmented-format-string #'singular))
  144. (p (augmented-format-string #'plural))
  145. (prefix (datum->syntax x prefix)))
  146. #'(format (guix-warning-port)
  147. (ngettext s p n %gettext-domain)
  148. (program-name) (program-name) prefix
  149. args (... ...))))))))
  150. (define-diagnostic warning "warning: ") ; emit a warning
  151. (define-diagnostic info "")
  152. (define-diagnostic report-error "error: ")
  153. (define-syntax-rule (leave args ...)
  154. "Emit an error message and exit."
  155. (begin
  156. (report-error args ...)
  157. (exit 1)))
  158. (define (make-user-module modules)
  159. "Return a new user module with the additional MODULES loaded."
  160. ;; Module in which the machine description file is loaded.
  161. (let ((module (make-fresh-user-module)))
  162. (for-each (lambda (iface)
  163. (module-use! module (resolve-interface iface)))
  164. modules)
  165. module))
  166. (define* (load* file user-module
  167. #:key (on-error 'nothing-special))
  168. "Load the user provided Scheme source code FILE."
  169. (define (frame-with-source frame)
  170. ;; Walk from FRAME upwards until source location information is found.
  171. (let loop ((frame frame)
  172. (previous frame))
  173. (if (not frame)
  174. previous
  175. (if (frame-source frame)
  176. frame
  177. (loop (frame-previous frame) frame)))))
  178. (define (error-string frame args)
  179. (call-with-output-string
  180. (lambda (port)
  181. (apply display-error frame port (cdr args)))))
  182. (define tag
  183. (make-prompt-tag "user-code"))
  184. (catch #t
  185. (lambda ()
  186. ;; XXX: Force a recompilation to avoid ABI issues.
  187. (set! %fresh-auto-compile #t)
  188. (set! %load-should-auto-compile #t)
  189. (save-module-excursion
  190. (lambda ()
  191. (set-current-module user-module)
  192. ;; Hide the "auto-compiling" messages.
  193. (parameterize ((current-warning-port (%make-void-port "w")))
  194. (call-with-prompt tag
  195. (lambda ()
  196. ;; Give 'load' an absolute file name so that it doesn't try to
  197. ;; search for FILE in %LOAD-PATH. Note: use 'load', not
  198. ;; 'primitive-load', so that FILE is compiled, which then allows us
  199. ;; to provide better error reporting with source line numbers.
  200. (load (canonicalize-path file)))
  201. (const #f))))))
  202. (lambda _
  203. ;; XXX: Errors are reported from the pre-unwind handler below, but
  204. ;; calling 'exit' from there has no effect, so we call it here.
  205. (exit 1))
  206. (rec (handle-error . args)
  207. ;; Capture the stack up to this procedure call, excluded, and pass
  208. ;; the faulty stack frame to 'report-load-error'.
  209. (let* ((stack (make-stack #t handle-error tag))
  210. (depth (stack-length stack))
  211. (last (and (> depth 0) (stack-ref stack 0)))
  212. (frame (frame-with-source
  213. (if (> depth 1)
  214. (stack-ref stack 1) ;skip the 'throw' frame
  215. last))))
  216. (report-load-error file args frame)
  217. (case on-error
  218. ((debug)
  219. (newline)
  220. (display (_ "entering debugger; type ',bt' for a backtrace\n"))
  221. (start-repl #:debug (make-debug (stack->vector stack) 0
  222. (error-string frame args)
  223. #f)))
  224. ((backtrace)
  225. (newline (current-error-port))
  226. (display-backtrace stack (current-error-port)))
  227. (else
  228. #t))))))
  229. (define* (report-load-error file args #:optional frame)
  230. "Report the failure to load FILE, a user-provided Scheme file.
  231. ARGS is the list of arguments received by the 'throw' handler."
  232. (match args
  233. (('system-error . rest)
  234. (let ((err (system-error-errno args)))
  235. (report-error (_ "failed to load '~a': ~a~%") file (strerror err))))
  236. (('syntax-error proc message properties form . rest)
  237. (let ((loc (source-properties->location properties)))
  238. (format (current-error-port) (_ "~a: error: ~a~%")
  239. (location->string loc) message)))
  240. (('srfi-34 obj)
  241. (report-error (_ "exception thrown: ~s~%") obj))
  242. ((error args ...)
  243. (report-error (_ "failed to load '~a':~%") file)
  244. (apply display-error frame (current-error-port) args))))
  245. (define (warn-about-load-error file args) ;FIXME: factorize with ↑
  246. "Report the failure to load FILE, a user-provided Scheme file, without
  247. exiting. ARGS is the list of arguments received by the 'throw' handler."
  248. (match args
  249. (('system-error . rest)
  250. (let ((err (system-error-errno args)))
  251. (warning (_ "failed to load '~a': ~a~%") file (strerror err))))
  252. (('syntax-error proc message properties form . rest)
  253. (let ((loc (source-properties->location properties)))
  254. (format (current-error-port) (_ "~a: warning: ~a~%")
  255. (location->string loc) message)))
  256. (('srfi-34 obj)
  257. (warning (_ "failed to load '~a': exception thrown: ~s~%")
  258. file obj))
  259. ((error args ...)
  260. (warning (_ "failed to load '~a':~%") file)
  261. (apply display-error #f (current-error-port) args))))
  262. (define (install-locale)
  263. "Install the current locale settings."
  264. (catch 'system-error
  265. (lambda _
  266. (setlocale LC_ALL ""))
  267. (lambda args
  268. (warning (_ "failed to install locale: ~a~%")
  269. (strerror (system-error-errno args))))))
  270. (define (initialize-guix)
  271. "Perform the usual initialization for stand-alone Guix commands."
  272. (install-locale)
  273. (textdomain %gettext-domain)
  274. ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be
  275. ;; notified via an EPIPE later.
  276. (sigaction SIGPIPE SIG_IGN)
  277. (setvbuf (current-output-port) _IOLBF)
  278. (setvbuf (current-error-port) _IOLBF))
  279. (define* (show-version-and-exit #:optional (command (car (command-line))))
  280. "Display version information for COMMAND and `(exit 0)'."
  281. (simple-format #t "~a (~a) ~a~%"
  282. command %guix-package-name %guix-version)
  283. (display (_ "Copyright (C) 2016 the Guix authors
  284. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  285. This is free software: you are free to change and redistribute it.
  286. There is NO WARRANTY, to the extent permitted by law.
  287. "))
  288. (exit 0))
  289. (define (show-bug-report-information)
  290. ;; TRANSLATORS: The placeholder indicates the bug-reporting address for this
  291. ;; package. Please add another line saying "Report translation bugs to
  292. ;; ...\n" with the address for translation bugs (typically your translation
  293. ;; team's web or email address).
  294. (format #t (_ "
  295. Report bugs to: ~a.") %guix-bug-report-address)
  296. (format #t (_ "
  297. ~a home page: <~a>") %guix-package-name %guix-home-page-url)
  298. (display (_ "
  299. General help using GNU software: <http://www.gnu.org/gethelp/>"))
  300. (newline))
  301. (set! symlink
  302. ;; We 'set!' the global binding because (gnu build ...) modules and similar
  303. ;; typically don't use (guix ui).
  304. (let ((real-symlink (@ (guile) symlink)))
  305. (lambda (target link)
  306. "This is a 'symlink' replacement that provides proper error reporting."
  307. (catch 'system-error
  308. (lambda ()
  309. (real-symlink target link))
  310. (lambda (key proc fmt args errno)
  311. ;; Augment the FMT and ARGS with information about LINK (this
  312. ;; information is missing as of Guile 2.0.11, making the exception
  313. ;; uninformative.)
  314. (apply throw key proc "~A: ~S"
  315. (list (strerror (car errno)) link)
  316. (list errno)))))))
  317. (set! copy-file
  318. ;; Note: here we use 'set!', not #:replace, because UIs typically use
  319. ;; 'copy-recursively', which doesn't use (guix ui).
  320. (let ((real-copy-file (@ (guile) copy-file)))
  321. (lambda (source target)
  322. "This is a 'copy-file' replacement that provides proper error reporting."
  323. (catch 'system-error
  324. (lambda ()
  325. (real-copy-file source target))
  326. (lambda (key proc fmt args errno)
  327. ;; Augment the FMT and ARGS with information about TARGET (this
  328. ;; information is missing as of Guile 2.0.11, making the exception
  329. ;; uninformative.)
  330. (apply throw key proc "~A: ~S"
  331. (list (strerror (car errno)) target)
  332. (list errno)))))))
  333. (define (make-regexp* regexp . flags)
  334. "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error
  335. nicely."
  336. (catch 'regular-expression-syntax
  337. (lambda ()
  338. (apply make-regexp regexp flags))
  339. (lambda (key proc message . rest)
  340. (leave (_ "'~a' is not a valid regular expression: ~a~%")
  341. regexp message))))
  342. (define (string->number* str)
  343. "Like `string->number', but error out with an error message on failure."
  344. (or (string->number str)
  345. (leave (_ "~a: invalid number~%") str)))
  346. (define (size->number str)
  347. "Convert STR, a storage measurement representation such as \"1024\" or
  348. \"1MiB\", to a number of bytes. Raise an error if STR could not be
  349. interpreted."
  350. (define unit-pos
  351. (string-rindex str char-set:digit))
  352. (define unit
  353. (and unit-pos (substring str (+ 1 unit-pos))))
  354. (let* ((numstr (if unit-pos
  355. (substring str 0 (+ 1 unit-pos))
  356. str))
  357. (num (string->number numstr)))
  358. (unless num
  359. (leave (_ "invalid number: ~a~%") numstr))
  360. ((compose inexact->exact round)
  361. (* num
  362. (match unit
  363. ((or "KiB" "K" "k") (expt 2 10))
  364. ((or "MiB" "M") (expt 2 20))
  365. ((or "GiB" "G") (expt 2 30))
  366. ((or "TiB" "T") (expt 2 40))
  367. ((or "PiB" "P") (expt 2 50))
  368. ((or "EiB" "E") (expt 2 60))
  369. ((or "ZiB" "Z") (expt 2 70))
  370. ((or "YiB" "Y") (expt 2 80))
  371. ("kB" (expt 10 3))
  372. ("MB" (expt 10 6))
  373. ("GB" (expt 10 9))
  374. ("TB" (expt 10 12))
  375. ("PB" (expt 10 15))
  376. ("EB" (expt 10 18))
  377. ("ZB" (expt 10 21))
  378. ("YB" (expt 10 24))
  379. ("" 1)
  380. (x
  381. (leave (_ "unknown unit: ~a~%") unit)))))))
  382. (define (call-with-error-handling thunk)
  383. "Call THUNK within a user-friendly error handler."
  384. (define (port-filename* port)
  385. ;; 'port-filename' returns #f for non-file ports, but it raises an
  386. ;; exception for file ports that are closed. Work around that.
  387. (and (not (port-closed? port))
  388. (port-filename port)))
  389. (guard (c ((package-input-error? c)
  390. (let* ((package (package-error-package c))
  391. (input (package-error-invalid-input c))
  392. (location (package-location package))
  393. (file (location-file location))
  394. (line (location-line location))
  395. (column (location-column location)))
  396. (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%")
  397. file line column
  398. (package-full-name package) input)))
  399. ((package-cross-build-system-error? c)
  400. (let* ((package (package-error-package c))
  401. (loc (package-location package))
  402. (system (package-build-system package)))
  403. (leave (_ "~a: ~a: build system `~a' does not support cross builds~%")
  404. (location->string loc)
  405. (package-full-name package)
  406. (build-system-name system))))
  407. ((profile-not-found-error? c)
  408. (leave (_ "profile '~a' does not exist~%")
  409. (profile-error-profile c)))
  410. ((missing-generation-error? c)
  411. (leave (_ "generation ~a of profile '~a' does not exist~%")
  412. (missing-generation-error-generation c)
  413. (profile-error-profile c)))
  414. ((nar-error? c)
  415. (let ((file (nar-error-file c))
  416. (port (nar-error-port c)))
  417. (if file
  418. (leave (_ "corrupt input while restoring '~a' from ~s~%")
  419. file (or (port-filename* port) port))
  420. (leave (_ "corrupt input while restoring archive from ~s~%")
  421. (or (port-filename* port) port)))))
  422. ((nix-connection-error? c)
  423. (leave (_ "failed to connect to `~a': ~a~%")
  424. (nix-connection-error-file c)
  425. (strerror (nix-connection-error-code c))))
  426. ((nix-protocol-error? c)
  427. ;; FIXME: Server-provided error messages aren't i18n'd.
  428. (leave (_ "build failed: ~a~%")
  429. (nix-protocol-error-message c)))
  430. ((derivation-missing-output-error? c)
  431. (leave (_ "reference to invalid output '~a' of derivation '~a'~%")
  432. (derivation-missing-output c)
  433. (derivation-file-name (derivation-error-derivation c))))
  434. ((file-search-error? c)
  435. (leave (_ "file '~a' could not be found in these \
  436. directories:~{ ~a~}~%")
  437. (file-search-error-file-name c)
  438. (file-search-error-search-path c)))
  439. ((message-condition? c)
  440. ;; Normally '&message' error conditions have an i18n'd message.
  441. (leave (_ "~a~%")
  442. (gettext (condition-message c) %gettext-domain))))
  443. ;; Catch EPIPE and the likes.
  444. (catch 'system-error
  445. thunk
  446. (lambda (key proc format-string format-args . rest)
  447. (leave (_ "~a: ~a~%") proc
  448. (apply format #f format-string format-args))))))
  449. (define-syntax-rule (leave-on-EPIPE exp ...)
  450. "Run EXP... in a context when EPIPE errors are caught and lead to 'exit'
  451. with successful exit code. This is useful when writing to the standard output
  452. may lead to EPIPE, because the standard output is piped through 'head' or
  453. similar."
  454. (catch 'system-error
  455. (lambda ()
  456. exp ...)
  457. (lambda args
  458. ;; We really have to exit this brutally, otherwise Guile eventually
  459. ;; attempts to flush all the ports, leading to an uncaught EPIPE down
  460. ;; the path.
  461. (if (= EPIPE (system-error-errno args))
  462. (primitive-_exit 0)
  463. (apply throw args)))))
  464. (define %guix-user-module
  465. ;; Module in which user expressions are evaluated.
  466. ;; Compute lazily to avoid circularity with (guix gexp).
  467. (delay
  468. (let ((module (make-module)))
  469. (beautify-user-module! module)
  470. ;; Use (guix gexp) so that one can use #~ & co.
  471. (module-use! module (resolve-interface '(guix gexp)))
  472. module)))
  473. (define (read/eval str)
  474. "Read and evaluate STR, raising an error if something goes wrong."
  475. (let ((exp (catch #t
  476. (lambda ()
  477. (call-with-input-string str read))
  478. (lambda args
  479. (leave (_ "failed to read expression ~s: ~s~%")
  480. str args)))))
  481. (catch #t
  482. (lambda ()
  483. (eval exp (force %guix-user-module)))
  484. (lambda args
  485. (report-error (_ "failed to evaluate expression '~a':~%") exp)
  486. (match args
  487. (('syntax-error proc message properties form . rest)
  488. (report-error (_ "syntax error: ~a~%") message))
  489. (('srfi-34 obj)
  490. (report-error (_ "exception thrown: ~s~%") obj))
  491. ((error args ...)
  492. (apply display-error #f (current-error-port) args))
  493. (what? #f))
  494. (exit 1)))))
  495. (define (read/eval-package-expression str)
  496. "Read and evaluate STR and return the package it refers to, or exit an
  497. error."
  498. (match (read/eval str)
  499. ((? package? p) p)
  500. (x
  501. (leave (_ "expression ~s does not evaluate to a package~%")
  502. str))))
  503. (define (show-derivation-outputs derivation)
  504. "Show the output file names of DERIVATION."
  505. (format #t "~{~a~%~}"
  506. (map (match-lambda
  507. ((out-name . out)
  508. (derivation->output-path derivation out-name)))
  509. (derivation-outputs derivation))))
  510. (define* (show-what-to-build store drv
  511. #:key dry-run? (use-substitutes? #t)
  512. (mode (build-mode normal)))
  513. "Show what will or would (depending on DRY-RUN?) be built in realizing the
  514. derivations listed in DRV using MODE, a 'build-mode' value. Return #t if
  515. there's something to build, #f otherwise. When USE-SUBSTITUTES?, check and
  516. report what is prerequisites are available for download."
  517. (define substitutable?
  518. ;; Call 'substitutation-oracle' upfront so we don't end up launching the
  519. ;; substituter many times. This makes a big difference, especially when
  520. ;; DRV is a long list as is the case with 'guix environment'.
  521. (if use-substitutes?
  522. (substitution-oracle store drv #:mode mode)
  523. (const #f)))
  524. (define (built-or-substitutable? drv)
  525. (or (null? (derivation-outputs drv))
  526. (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists
  527. (or (valid-path? store out)
  528. (substitutable? out)))))
  529. (let*-values (((build download)
  530. (fold2 (lambda (drv build download)
  531. (let-values (((b d)
  532. (derivation-prerequisites-to-build
  533. store drv
  534. #:mode mode
  535. #:substitutable? substitutable?)))
  536. (values (append b build)
  537. (append d download))))
  538. '() '()
  539. drv))
  540. ((build) ; add the DRV themselves
  541. (delete-duplicates
  542. (append (map derivation-file-name
  543. (remove built-or-substitutable? drv))
  544. (map derivation-input-path build))))
  545. ((download) ; add the references of DOWNLOAD
  546. (if use-substitutes?
  547. (delete-duplicates
  548. (append download
  549. (remove (cut valid-path? store <>)
  550. (append-map
  551. substitutable-references
  552. (substitutable-path-info store
  553. download)))))
  554. download)))
  555. ;; TODO: Show the installed size of DOWNLOAD.
  556. (if dry-run?
  557. (begin
  558. (format (current-error-port)
  559. (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]"
  560. "~:[The following derivations would be built:~%~{ ~a~%~}~;~]"
  561. (length build))
  562. (null? build) build)
  563. (format (current-error-port)
  564. (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]"
  565. "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]"
  566. (length download))
  567. (null? download) download))
  568. (begin
  569. (format (current-error-port)
  570. (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]"
  571. "~:[The following derivations will be built:~%~{ ~a~%~}~;~]"
  572. (length build))
  573. (null? build) build)
  574. (format (current-error-port)
  575. (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]"
  576. "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]"
  577. (length download))
  578. (null? download) download)))
  579. (pair? build)))
  580. (define show-what-to-build*
  581. (store-lift show-what-to-build))
  582. (define (right-arrow port)
  583. "Return either a string containing the 'RIGHT ARROW' character, or an ASCII
  584. replacement if PORT is not Unicode-capable."
  585. (with-fluids ((%default-port-encoding (port-encoding port)))
  586. (let ((arrow "→"))
  587. (catch 'encoding-error
  588. (lambda ()
  589. (call-with-output-string
  590. (lambda (port)
  591. (set-port-conversion-strategy! port 'error)
  592. (display arrow port))))
  593. (lambda (key . args)
  594. "->")))))
  595. (define* (show-manifest-transaction store manifest transaction
  596. #:key dry-run?)
  597. "Display what will/would be installed/removed from MANIFEST by TRANSACTION."
  598. (define (package-strings name version output item)
  599. (map (lambda (name version output item)
  600. (format #f " ~a~:[:~a~;~*~]\t~a\t~a"
  601. name
  602. (equal? output "out") output version
  603. (if (package? item)
  604. (package-output store item output)
  605. item)))
  606. name version output item))
  607. (define → ;an arrow that can be represented on stderr
  608. (right-arrow (current-error-port)))
  609. (define (upgrade-string name old-version new-version output item)
  610. (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a"
  611. name (equal? output "out") output
  612. old-version → new-version
  613. (if (package? item)
  614. (package-output store item output)
  615. item)))
  616. (let-values (((remove install upgrade downgrade)
  617. (manifest-transaction-effects manifest transaction)))
  618. (match remove
  619. ((($ <manifest-entry> name version output item) ..1)
  620. (let ((len (length name))
  621. (remove (package-strings name version output item)))
  622. (if dry-run?
  623. (format (current-error-port)
  624. (N_ "The following package would be removed:~%~{~a~%~}~%"
  625. "The following packages would be removed:~%~{~a~%~}~%"
  626. len)
  627. remove)
  628. (format (current-error-port)
  629. (N_ "The following package will be removed:~%~{~a~%~}~%"
  630. "The following packages will be removed:~%~{~a~%~}~%"
  631. len)
  632. remove))))
  633. (_ #f))
  634. (match downgrade
  635. (((($ <manifest-entry> name old-version)
  636. . ($ <manifest-entry> _ new-version output item)) ..1)
  637. (let ((len (length name))
  638. (downgrade (map upgrade-string
  639. name old-version new-version output item)))
  640. (if dry-run?
  641. (format (current-error-port)
  642. (N_ "The following package would be downgraded:~%~{~a~%~}~%"
  643. "The following packages would be downgraded:~%~{~a~%~}~%"
  644. len)
  645. downgrade)
  646. (format (current-error-port)
  647. (N_ "The following package will be downgraded:~%~{~a~%~}~%"
  648. "The following packages will be downgraded:~%~{~a~%~}~%"
  649. len)
  650. downgrade))))
  651. (_ #f))
  652. (match upgrade
  653. (((($ <manifest-entry> name old-version)
  654. . ($ <manifest-entry> _ new-version output item)) ..1)
  655. (let ((len (length name))
  656. (upgrade (map upgrade-string
  657. name old-version new-version output item)))
  658. (if dry-run?
  659. (format (current-error-port)
  660. (N_ "The following package would be upgraded:~%~{~a~%~}~%"
  661. "The following packages would be upgraded:~%~{~a~%~}~%"
  662. len)
  663. upgrade)
  664. (format (current-error-port)
  665. (N_ "The following package will be upgraded:~%~{~a~%~}~%"
  666. "The following packages will be upgraded:~%~{~a~%~}~%"
  667. len)
  668. upgrade))))
  669. (_ #f))
  670. (match install
  671. ((($ <manifest-entry> name version output item _) ..1)
  672. (let ((len (length name))
  673. (install (package-strings name version output item)))
  674. (if dry-run?
  675. (format (current-error-port)
  676. (N_ "The following package would be installed:~%~{~a~%~}~%"
  677. "The following packages would be installed:~%~{~a~%~}~%"
  678. len)
  679. install)
  680. (format (current-error-port)
  681. (N_ "The following package will be installed:~%~{~a~%~}~%"
  682. "The following packages will be installed:~%~{~a~%~}~%"
  683. len)
  684. install))))
  685. (_ #f))))
  686. (define-syntax with-error-handling
  687. (syntax-rules ()
  688. "Run BODY within a user-friendly error condition handler."
  689. ((_ body ...)
  690. (call-with-error-handling
  691. (lambda ()
  692. body ...)))))
  693. (define (location->string loc)
  694. "Return a human-friendly, GNU-standard representation of LOC."
  695. (match loc
  696. (#f (_ "<unknown location>"))
  697. (($ <location> file line column)
  698. (format #f "~a:~a:~a" file line column))))
  699. (define (config-directory)
  700. "Return the name of the configuration directory, after making sure that it
  701. exists. Honor the XDG specs,
  702. <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
  703. (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME")
  704. (and=> (getenv "HOME")
  705. (cut string-append <> "/.config")))
  706. (cut string-append <> "/guix"))))
  707. (catch 'system-error
  708. (lambda ()
  709. (mkdir-p dir)
  710. dir)
  711. (lambda args
  712. (let ((err (system-error-errno args)))
  713. ;; ERR is necessarily different from EEXIST.
  714. (leave (_ "failed to create configuration directory `~a': ~a~%")
  715. dir (strerror err)))))))
  716. (define* (fill-paragraph str width #:optional (column 0))
  717. "Fill STR such that each line contains at most WIDTH characters, assuming
  718. that the first character is at COLUMN.
  719. When STR contains a single line break surrounded by other characters, it is
  720. converted to a space; sequences of more than one line break are preserved."
  721. (define (maybe-break chr result)
  722. (match result
  723. ((column newlines chars)
  724. (case chr
  725. ((#\newline)
  726. `(,column ,(+ 1 newlines) ,chars))
  727. (else
  728. (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1))
  729. (chars (case newlines
  730. ((0) chars)
  731. ((1)
  732. (append (make-list spaces #\space) chars))
  733. (else
  734. (append (make-list newlines #\newline) chars))))
  735. (column (case newlines
  736. ((0) column)
  737. ((1) (+ spaces column))
  738. (else 0))))
  739. (let ((chars (cons chr chars))
  740. (column (+ 1 column)))
  741. (if (> column width)
  742. (let*-values (((before after)
  743. (break (cut eqv? #\space <>) chars))
  744. ((len)
  745. (length before)))
  746. (if (<= len width)
  747. `(,len
  748. 0
  749. ,(if (null? after)
  750. before
  751. (append before
  752. (cons #\newline
  753. (drop-while (cut eqv? #\space <>)
  754. after)))))
  755. `(,column 0 ,chars))) ; unbreakable
  756. `(,column 0 ,chars)))))))))
  757. (match (string-fold maybe-break
  758. `(,column 0 ())
  759. str)
  760. ((_ _ chars)
  761. (list->string (reverse chars)))))
  762. ;;;
  763. ;;; Packages.
  764. ;;;
  765. (define %text-width
  766. (make-parameter (terminal-columns)))
  767. (set! (@@ (texinfo plain-text) wrap*)
  768. ;; XXX: Monkey patch this private procedure to let 'package->recutils'
  769. ;; parameterize the fill of description field correctly.
  770. (lambda strings
  771. (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*))))
  772. (fill-string (string-concatenate strings)
  773. #:line-width (%text-width) #:initial-indent indent
  774. #:subsequent-indent indent))))
  775. (define (texi->plain-text str)
  776. "Return a plain-text representation of texinfo fragment STR."
  777. ;; 'texi-fragment->stexi' uses a string port so make sure it's a
  778. ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.)
  779. (with-fluids ((%default-port-encoding "UTF-8"))
  780. (stexi->plain-text (texi-fragment->stexi str))))
  781. (define (package-description-string package)
  782. "Return a plain-text representation of PACKAGE description field."
  783. (and=> (package-description package)
  784. (compose texi->plain-text P_)))
  785. (define (string->recutils str)
  786. "Return a version of STR where newlines have been replaced by newlines
  787. followed by \"+ \", which makes for a valid multi-line field value in the
  788. `recutils' syntax."
  789. (list->string
  790. (string-fold-right (lambda (chr result)
  791. (if (eqv? chr #\newline)
  792. (cons* chr #\+ #\space result)
  793. (cons chr result)))
  794. '()
  795. str)))
  796. (define* (package->recutils p port #:optional (width (%text-width)))
  797. "Write to PORT a `recutils' record of package P, arranging to fit within
  798. WIDTH columns."
  799. (define width*
  800. ;; The available number of columns once we've taken into account space for
  801. ;; the initial "+ " prefix.
  802. (if (> width 2) (- width 2) width))
  803. (define (dependencies->recutils packages)
  804. (let ((list (string-join (map package-full-name
  805. (sort packages package<?)) " ")))
  806. (string->recutils
  807. (fill-paragraph list width*
  808. (string-length "dependencies: ")))))
  809. (define (package<? p1 p2)
  810. (string<? (package-full-name p1) (package-full-name p2)))
  811. ;; Note: Don't i18n field names so that people can post-process it.
  812. (format port "name: ~a~%" (package-name p))
  813. (format port "version: ~a~%" (package-version p))
  814. (format port "outputs: ~a~%" (string-join (package-outputs p)))
  815. (format port "systems: ~a~%"
  816. (string-join (package-transitive-supported-systems p)))
  817. (format port "dependencies: ~a~%"
  818. (match (package-direct-inputs p)
  819. (((labels inputs . _) ...)
  820. (dependencies->recutils (filter package? inputs)))))
  821. (format port "location: ~a~%"
  822. (or (and=> (package-location p) location->string)
  823. (_ "unknown")))
  824. ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in
  825. ;; field identifiers.
  826. (format port "homepage: ~a~%" (package-home-page p))
  827. (format port "license: ~a~%"
  828. (match (package-license p)
  829. (((? license? licenses) ...)
  830. (string-join (map license-name licenses)
  831. ", "))
  832. ((? license? license)
  833. (license-name license))
  834. (x
  835. (_ "unknown"))))
  836. (format port "synopsis: ~a~%"
  837. (string-map (match-lambda
  838. (#\newline #\space)
  839. (chr chr))
  840. (or (and=> (package-synopsis p) P_)
  841. "")))
  842. (format port "~a~2%"
  843. (string->recutils
  844. (string-trim-right
  845. (parameterize ((%text-width width*))
  846. (texi->plain-text
  847. (string-append "description: "
  848. (or (and=> (package-description p) P_)
  849. ""))))
  850. #\newline))))
  851. (define (string->generations str)
  852. "Return the list of generations matching a pattern in STR. This function
  853. accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"."
  854. (define (maybe-integer)
  855. (let ((x (string->number str)))
  856. (and (integer? x)
  857. x)))
  858. (define (maybe-comma-separated-integers)
  859. (let ((lst (delete-duplicates
  860. (map string->number
  861. (string-split str #\,)))))
  862. (and (every integer? lst)
  863. lst)))
  864. (cond ((maybe-integer)
  865. =>
  866. list)
  867. ((maybe-comma-separated-integers)
  868. =>
  869. identity)
  870. ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
  871. =>
  872. (lambda (match)
  873. (let ((s (string->number (match:substring match 1)))
  874. (e (string->number (match:substring match 2))))
  875. (and (every integer? (list s e))
  876. (<= s e)
  877. (iota (1+ (- e s)) s)))))
  878. ((string-match "^([0-9]+)\\.\\.$" str)
  879. =>
  880. (lambda (match)
  881. (let ((s (string->number (match:substring match 1))))
  882. (and (integer? s)
  883. `(>= ,s)))))
  884. ((string-match "^\\.\\.([0-9]+)$" str)
  885. =>
  886. (lambda (match)
  887. (let ((e (string->number (match:substring match 1))))
  888. (and (integer? e)
  889. `(<= ,e)))))
  890. (else #f)))
  891. (define (string->duration str)
  892. "Return the duration matching a pattern in STR. This function accepts the
  893. following patterns: \"1d\", \"1w\", \"1m\"."
  894. (define (hours->duration hours match)
  895. (make-time time-duration 0
  896. (* 3600 hours (string->number (match:substring match 1)))))
  897. (cond ((string-match "^([0-9]+)s$" str)
  898. =>
  899. (lambda (match)
  900. (make-time time-duration 0
  901. (string->number (match:substring match 1)))))
  902. ((string-match "^([0-9]+)h$" str)
  903. (lambda (match)
  904. (hours->duration 1 match)))
  905. ((string-match "^([0-9]+)d$" str)
  906. =>
  907. (lambda (match)
  908. (hours->duration 24 match)))
  909. ((string-match "^([0-9]+)w$" str)
  910. =>
  911. (lambda (match)
  912. (hours->duration (* 24 7) match)))
  913. ((string-match "^([0-9]+)m$" str)
  914. =>
  915. (lambda (match)
  916. (hours->duration (* 24 30) match)))
  917. (else #f)))
  918. (define* (matching-generations str profile
  919. #:key (duration-relation <=))
  920. "Return the list of available generations matching a pattern in STR. See
  921. 'string->generations' and 'string->duration' for the list of valid patterns.
  922. When STR is a duration pattern, return all the generations whose ctime has
  923. DURATION-RELATION with the current time."
  924. (define (valid-generations lst)
  925. (define (valid-generation? n)
  926. (any (cut = n <>) (generation-numbers profile)))
  927. (fold-right (lambda (x acc)
  928. (if (valid-generation? x)
  929. (cons x acc)
  930. acc))
  931. '()
  932. lst))
  933. (define (filter-generations generations)
  934. (match generations
  935. (() '())
  936. (('>= n)
  937. (drop-while (cut > n <>)
  938. (generation-numbers profile)))
  939. (('<= n)
  940. (valid-generations (iota n 1)))
  941. ((lst ..1)
  942. (valid-generations lst))
  943. (_ #f)))
  944. (define (filter-by-duration duration)
  945. (define (time-at-midnight time)
  946. ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and
  947. ;; hours to zeros.
  948. (let ((d (time-utc->date time)))
  949. (date->time-utc
  950. (make-date 0 0 0 0
  951. (date-day d) (date-month d)
  952. (date-year d) (date-zone-offset d)))))
  953. (define generation-ctime-alist
  954. (map (lambda (number)
  955. (cons number
  956. (time-second
  957. (time-at-midnight
  958. (generation-time profile number)))))
  959. (generation-numbers profile)))
  960. (match duration
  961. (#f #f)
  962. (res
  963. (let ((s (time-second
  964. (subtract-duration (time-at-midnight (current-time))
  965. duration))))
  966. (delete #f (map (lambda (x)
  967. (and (duration-relation s (cdr x))
  968. (first x)))
  969. generation-ctime-alist))))))
  970. (cond ((string->generations str)
  971. =>
  972. filter-generations)
  973. ((string->duration str)
  974. =>
  975. filter-by-duration)
  976. (else #f)))
  977. (define (display-generation profile number)
  978. "Display a one-line summary of generation NUMBER of PROFILE."
  979. (unless (zero? number)
  980. (let ((header (format #f (_ "Generation ~a\t~a") number
  981. (date->string
  982. (time-utc->date
  983. (generation-time profile number))
  984. "~b ~d ~Y ~T")))
  985. (current (generation-number profile)))
  986. (if (= number current)
  987. ;; TRANSLATORS: The word "current" here is an adjective for
  988. ;; "Generation", as in "current generation". Use the appropriate
  989. ;; gender where applicable.
  990. (format #t (_ "~a\t(current)~%") header)
  991. (format #t "~a~%" header)))))
  992. (define (display-profile-content-diff profile gen1 gen2)
  993. "Display the changed packages in PROFILE GEN2 compared to generation GEN2."
  994. (define (equal-entry? first second)
  995. (string= (manifest-entry-item first) (manifest-entry-item second)))
  996. (define (display-entry entry prefix)
  997. (match entry
  998. (($ <manifest-entry> name version output location _)
  999. (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location))))
  1000. (define (list-entries number)
  1001. (manifest-entries (profile-manifest (generation-file-name profile number))))
  1002. (define (display-diff profile old new)
  1003. (display-generation profile new)
  1004. (let ((added (lset-difference
  1005. equal-entry? (list-entries new) (list-entries old)))
  1006. (removed (lset-difference
  1007. equal-entry? (list-entries old) (list-entries new))))
  1008. (for-each (cut display-entry <> "+") added)
  1009. (for-each (cut display-entry <> "-") removed)))
  1010. (display-diff profile gen1 gen2))
  1011. (define (display-profile-content profile number)
  1012. "Display the packages in PROFILE, generation NUMBER, in a human-readable
  1013. way."
  1014. (for-each (match-lambda
  1015. (($ <manifest-entry> name version output location _)
  1016. (format #t " ~a\t~a\t~a\t~a~%"
  1017. name version output location)))
  1018. ;; Show most recently installed packages last.
  1019. (reverse
  1020. (manifest-entries
  1021. (profile-manifest (generation-file-name profile number))))))
  1022. (define (display-generation-change previous current)
  1023. (format #t (_ "switched from generation ~a to ~a~%") previous current))
  1024. (define (roll-back* store profile)
  1025. "Like 'roll-back', but display what is happening."
  1026. (call-with-values
  1027. (lambda ()
  1028. (roll-back store profile))
  1029. display-generation-change))
  1030. (define (switch-to-generation* profile number)
  1031. "Like 'switch-generation', but display what is happening."
  1032. (let ((previous (switch-to-generation profile number)))
  1033. (display-generation-change previous number)))
  1034. (define (delete-generation* store profile generation)
  1035. "Like 'delete-generation', but display what is going on."
  1036. (format #t (_ "deleting ~a~%")
  1037. (generation-file-name profile generation))
  1038. (delete-generation store profile generation))
  1039. (define* (package-specification->name+version+output spec
  1040. #:optional (output "out"))
  1041. "Parse package specification SPEC and return three value: the specified
  1042. package name, version number (or #f), and output name (or OUTPUT). SPEC may
  1043. optionally contain a version number and an output name, as in these examples:
  1044. guile
  1045. guile@2.0.9
  1046. guile:debug
  1047. guile@2.0.9:debug
  1048. "
  1049. (let*-values (((name sub-drv)
  1050. (match (string-rindex spec #\:)
  1051. (#f (values spec output))
  1052. (colon (values (substring spec 0 colon)
  1053. (substring spec (+ 1 colon))))))
  1054. ((name version)
  1055. (package-name->name+version name)))
  1056. (values name version sub-drv)))
  1057. ;;;
  1058. ;;; Command-line option processing.
  1059. ;;;
  1060. (define (show-guix-usage)
  1061. (format (current-error-port)
  1062. (_ "Try `guix --help' for more information.~%"))
  1063. (exit 1))
  1064. (define (command-files)
  1065. "Return the list of source files that define Guix sub-commands."
  1066. (define directory
  1067. (and=> (search-path %load-path "guix.scm")
  1068. (compose (cut string-append <> "/guix/scripts")
  1069. dirname)))
  1070. (define dot-scm?
  1071. (cut string-suffix? ".scm" <>))
  1072. (if directory
  1073. (scandir directory dot-scm?)
  1074. '()))
  1075. (define (commands)
  1076. "Return the list of Guix command names."
  1077. (map (compose (cut string-drop-right <> 4)
  1078. basename)
  1079. (command-files)))
  1080. (define (show-guix-help)
  1081. (define (internal? command)
  1082. (member command '("substitute" "authenticate" "offload"
  1083. "perform-download")))
  1084. (format #t (_ "Usage: guix COMMAND ARGS...
  1085. Run COMMAND with ARGS.\n"))
  1086. (newline)
  1087. (format #t (_ "COMMAND must be one of the sub-commands listed below:\n"))
  1088. (newline)
  1089. ;; TODO: Display a synopsis of each command.
  1090. (format #t "~{ ~a~%~}" (sort (remove internal? (commands))
  1091. string<?))
  1092. (show-bug-report-information))
  1093. (define program-name
  1094. ;; Name of the command-line program currently executing, or #f.
  1095. (make-parameter #f))
  1096. (define (run-guix-command command . args)
  1097. "Run COMMAND with the given ARGS. Report an error when COMMAND is not
  1098. found."
  1099. (define module
  1100. (catch 'misc-error
  1101. (lambda ()
  1102. (resolve-interface `(guix scripts ,command)))
  1103. (lambda -
  1104. (format (current-error-port)
  1105. (_ "guix: ~a: command not found~%") command)
  1106. (show-guix-usage))))
  1107. (let ((command-main (module-ref module
  1108. (symbol-append 'guix- command))))
  1109. (parameterize ((program-name command))
  1110. ;; Disable canonicalization so we don't don't stat unreasonably.
  1111. (with-fluids ((%file-port-name-canonicalization #f))
  1112. (apply command-main args)))))
  1113. (define (run-guix . args)
  1114. "Run the 'guix' command defined by command line ARGS.
  1115. Unlike 'guix-main', this procedure assumes that locale, i18n support,
  1116. and signal handling has already been set up."
  1117. (define option? (cut string-prefix? "-" <>))
  1118. (match args
  1119. (()
  1120. (format (current-error-port)
  1121. (_ "guix: missing command name~%"))
  1122. (show-guix-usage))
  1123. ((or ("-h") ("--help"))
  1124. (show-guix-help))
  1125. (("--version")
  1126. (show-version-and-exit "guix"))
  1127. (((? option? o) args ...)
  1128. (format (current-error-port)
  1129. (_ "guix: unrecognized option '~a'~%") o)
  1130. (show-guix-usage))
  1131. (("help" command)
  1132. (apply run-guix-command (string->symbol command)
  1133. '("--help")))
  1134. (("help" args ...)
  1135. (show-guix-help))
  1136. ((command args ...)
  1137. (apply run-guix-command
  1138. (string->symbol command)
  1139. args))))
  1140. (define guix-warning-port
  1141. (make-parameter (current-warning-port)))
  1142. (define (guix-main arg0 . args)
  1143. (initialize-guix)
  1144. (apply run-guix args))
  1145. ;;; ui.scm ends here