Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
a173f098 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org> |
cbdfa50d | 3 | ;;; Copyright © 2013, 2018 Mark H Weaver <mhw@netris.org> |
98eb8cbe | 4 | ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> |
88981dd3 AK |
5 | ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com> |
6 | ;;; Copyright © 2014 Cyrill Schenkel <cyrill.schenkel@gmail.com> | |
689db38e | 7 | ;;; Copyright © 2014, 2015, 2017 Alex Kost <alezost@gmail.com> |
88981dd3 | 8 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> |
cf5f2ad3 | 9 | ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org> |
e95ae7c2 RJ |
10 | ;;; Copyright © 2016 Roel Janssen <roel@gnu.org> |
11 | ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch> | |
85c3fbf5 | 12 | ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com> |
15cc7e6a | 13 | ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net> |
073c34d7 | 14 | ;;; |
233e7676 | 15 | ;;; This file is part of GNU Guix. |
073c34d7 | 16 | ;;; |
233e7676 | 17 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
073c34d7 LC |
18 | ;;; under the terms of the GNU General Public License as published by |
19 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
20 | ;;; your option) any later version. | |
21 | ;;; | |
233e7676 | 22 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
073c34d7 LC |
23 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | ;;; GNU General Public License for more details. | |
26 | ;;; | |
27 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 28 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
073c34d7 LC |
29 | |
30 | (define-module (guix ui) | |
37eed374 | 31 | #:use-module (guix i18n) |
3e43166f | 32 | #:use-module (guix gexp) |
b5bfa477 | 33 | #:use-module (guix sets) |
073c34d7 LC |
34 | #:use-module (guix utils) |
35 | #:use-module (guix store) | |
cdd5d6f9 | 36 | #:use-module (guix config) |
073c34d7 | 37 | #:use-module (guix packages) |
5d7a8584 | 38 | #:use-module (guix profiles) |
9bb2b96a | 39 | #:use-module (guix derivations) |
958dd3ce | 40 | #:use-module (guix combinators) |
b7071bc5 LC |
41 | #:use-module (guix build-system) |
42 | #:use-module (guix serialization) | |
299112d3 | 43 | #:use-module ((guix licenses) #:select (license? license-name)) |
dcfc6f21 LC |
44 | #:use-module ((guix build syscalls) |
45 | #:select (free-disk-space terminal-columns)) | |
cbdfa50d | 46 | #:use-module ((guix build utils) |
24cb66d1 LC |
47 | ;; XXX: All we need are the bindings related to |
48 | ;; '&invoke-error'. However, to work around the bug described | |
49 | ;; in 5d669883ecc104403c5d3ba7d172e9c02234577c, #:hide | |
50 | ;; unwanted bindings instead of #:select'ing the needed | |
51 | ;; bindings. | |
52 | #:hide (package-name->name+version)) | |
299112d3 LC |
53 | #:use-module (srfi srfi-1) |
54 | #:use-module (srfi srfi-11) | |
2cd09108 | 55 | #:use-module (srfi srfi-19) |
073c34d7 | 56 | #:use-module (srfi srfi-26) |
2abcc97f | 57 | #:use-module (srfi srfi-31) |
073c34d7 | 58 | #:use-module (srfi srfi-34) |
c1d52c71 | 59 | #:use-module (srfi srfi-35) |
e31ff8b8 | 60 | #:autoload (ice-9 ftw) (scandir) |
64fc89b6 | 61 | #:use-module (ice-9 match) |
9bb2b96a | 62 | #:use-module (ice-9 format) |
2cd09108 | 63 | #:use-module (ice-9 regex) |
f44c7aac | 64 | #:autoload (system base compile) (compile-file) |
db030303 LC |
65 | #:autoload (system repl repl) (start-repl) |
66 | #:autoload (system repl debug) (make-debug stack->vector) | |
1cd4027c ML |
67 | #:use-module (texinfo) |
68 | #:use-module (texinfo plain-text) | |
69 | #:use-module (texinfo string-utils) | |
37eed374 LC |
70 | #:re-export (G_ N_ P_) ;backward compatibility |
71 | #:export (report-error | |
6ddb5960 | 72 | display-hint |
073c34d7 | 73 | leave |
7ea1432e DT |
74 | make-user-module |
75 | load* | |
4ae7559f | 76 | warn-about-load-error |
cdd5d6f9 | 77 | show-version-and-exit |
3441e164 | 78 | show-bug-report-information |
fd688c82 | 79 | make-regexp* |
969e678e | 80 | string->number* |
1d6243cf | 81 | size->number |
fa394eb9 | 82 | show-derivation-outputs |
9bb2b96a | 83 | show-what-to-build |
4d043ab6 | 84 | show-what-to-build* |
5d7a8584 | 85 | show-manifest-transaction |
073c34d7 | 86 | call-with-error-handling |
64fc89b6 | 87 | with-error-handling |
2d2f98ef | 88 | with-unbound-variable-handling |
df36e629 | 89 | leave-on-EPIPE |
ac5de156 | 90 | read/eval |
eb0880e7 | 91 | read/eval-package-expression |
8120b23e | 92 | check-available-space |
299112d3 LC |
93 | location->string |
94 | fill-paragraph | |
0649321d | 95 | %text-width |
2748ee3b | 96 | texi->plain-text |
1cd4027c | 97 | package-description-string |
689db38e | 98 | package-synopsis-string |
299112d3 | 99 | string->recutils |
e49951eb | 100 | package->recutils |
2876b989 | 101 | package-specification->name+version+output |
c7ae219e LC |
102 | relevance |
103 | package-relevance | |
2cd09108 NK |
104 | string->generations |
105 | string->duration | |
e49de93a | 106 | matching-generations |
ad18c7e6 LC |
107 | display-generation |
108 | display-profile-content | |
e95ae7c2 | 109 | display-profile-content-diff |
06d45f45 LC |
110 | roll-back* |
111 | switch-to-generation* | |
112 | delete-generation* | |
e49951eb | 113 | run-guix-command |
caa6732e | 114 | run-guix |
a2011be5 LC |
115 | program-name |
116 | guix-warning-port | |
117 | warning | |
240b57f0 | 118 | info |
5d9f9ad6 | 119 | guix-main)) |
073c34d7 LC |
120 | |
121 | ;;; Commentary: | |
122 | ;;; | |
123 | ;;; User interface facilities for command-line tools. | |
124 | ;;; | |
125 | ;;; Code: | |
126 | ||
26a2021a LC |
127 | (define-syntax define-diagnostic |
128 | (syntax-rules () | |
129 | "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all | |
b2a886f6 | 130 | messages." |
26a2021a LC |
131 | ((_ name (G_ prefix)) |
132 | (define-syntax name | |
133 | (lambda (x) | |
134 | (syntax-case x () | |
135 | ((name (underscore fmt) args (... ...)) | |
136 | (and (string? (syntax->datum #'fmt)) | |
137 | (free-identifier=? #'underscore #'G_)) | |
138 | #'(begin | |
cc3697d5 | 139 | (print-diagnostic-prefix prefix) |
26a2021a LC |
140 | (format (guix-warning-port) (gettext fmt %gettext-domain) |
141 | args (... ...)))) | |
142 | ((name (N-underscore singular plural n) args (... ...)) | |
143 | (and (string? (syntax->datum #'singular)) | |
144 | (string? (syntax->datum #'plural)) | |
145 | (free-identifier=? #'N-underscore #'N_)) | |
146 | #'(begin | |
cc3697d5 | 147 | (print-diagnostic-prefix prefix) |
26a2021a LC |
148 | (format (guix-warning-port) |
149 | (ngettext singular plural n %gettext-domain) | |
150 | args (... ...)))))))))) | |
b2a886f6 | 151 | |
26a2021a LC |
152 | ;; XXX: This doesn't work well for right-to-left languages. |
153 | ;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase; | |
154 | ;; "~a" is a placeholder for that phrase. | |
155 | (define-diagnostic warning (G_ "warning: ")) ;emit a warning | |
156 | (define-diagnostic info (G_ "")) | |
b2a886f6 | 157 | |
26a2021a | 158 | (define-diagnostic report-error (G_ "error: ")) |
b2a886f6 LC |
159 | (define-syntax-rule (leave args ...) |
160 | "Emit an error message and exit." | |
161 | (begin | |
162 | (report-error args ...) | |
163 | (exit 1))) | |
164 | ||
cc3697d5 LC |
165 | (define (print-diagnostic-prefix prefix) |
166 | "Print PREFIX as a diagnostic line prefix." | |
167 | (format (guix-warning-port) "~:[~*~;guix ~a: ~]~a" | |
168 | (program-name) (program-name) | |
169 | (if (string-null? prefix) | |
170 | prefix | |
171 | (gettext prefix %gettext-domain)))) | |
172 | ||
dc856223 LC |
173 | (define (print-unbound-variable-error port key args default-printer) |
174 | ;; Print unbound variable errors more nicely, and in the right language. | |
175 | (match args | |
176 | ((proc message (variable) _ ...) | |
177 | ;; We can always omit PROC because when it's useful (i.e., different from | |
178 | ;; "module-lookup"), it gets displayed before. | |
2d2f98ef | 179 | (format port (G_ "error: ~a: unbound variable") variable)) |
dc856223 LC |
180 | (_ |
181 | (default-printer)))) | |
182 | ||
183 | (set-exception-printer! 'unbound-variable print-unbound-variable-error) | |
184 | ||
7ea1432e DT |
185 | (define (make-user-module modules) |
186 | "Return a new user module with the additional MODULES loaded." | |
187 | ;; Module in which the machine description file is loaded. | |
188 | (let ((module (make-fresh-user-module))) | |
189 | (for-each (lambda (iface) | |
190 | (module-use! module (resolve-interface iface))) | |
191 | modules) | |
192 | module)) | |
193 | ||
7f2f6a2c LC |
194 | (define (last-frame-with-source stack) |
195 | "Walk stack upwards and return the last frame that has source location | |
196 | information, or #f if it could not be found." | |
2abcc97f LC |
197 | (define (frame-with-source frame) |
198 | ;; Walk from FRAME upwards until source location information is found. | |
199 | (let loop ((frame frame) | |
200 | (previous frame)) | |
201 | (if (not frame) | |
202 | previous | |
203 | (if (frame-source frame) | |
204 | frame | |
205 | (loop (frame-previous frame) frame))))) | |
206 | ||
7f2f6a2c LC |
207 | (let* ((depth (stack-length stack)) |
208 | (last (and (> depth 0) (stack-ref stack 0)))) | |
209 | (frame-with-source (if (> depth 1) | |
210 | (stack-ref stack 1) ;skip the 'throw' frame | |
211 | last)))) | |
212 | ||
213 | (define* (load* file user-module | |
214 | #:key (on-error 'nothing-special)) | |
215 | "Load the user provided Scheme source code FILE." | |
db030303 LC |
216 | (define (error-string frame args) |
217 | (call-with-output-string | |
f44c7aac LC |
218 | (lambda (port) |
219 | (apply display-error frame port (cdr args))))) | |
db030303 LC |
220 | |
221 | (define tag | |
222 | (make-prompt-tag "user-code")) | |
223 | ||
7ea1432e DT |
224 | (catch #t |
225 | (lambda () | |
2abcc97f | 226 | ;; XXX: Force a recompilation to avoid ABI issues. |
92423868 LC |
227 | ;; |
228 | ;; In 2.2.3, the bogus answer to <https://bugs.gnu.org/29226> was to | |
229 | ;; ignore all available .go, not just those from ~/.cache, which in turn | |
230 | ;; meant that we had to rebuild *everything*. Since this is too costly, | |
f44c7aac LC |
231 | ;; we have to turn off '%fresh-auto-compile' with that version, so to |
232 | ;; avoid ABI breakage in the user's config file, we explicitly compile | |
233 | ;; it (the problem remains if the user's config is spread on several | |
234 | ;; modules.) See <https://bugs.gnu.org/29881>. | |
7949c140 LC |
235 | (unless (string=? (version) "2.2.3") |
236 | (set! %fresh-auto-compile #t)) | |
92423868 | 237 | |
2abcc97f | 238 | (set! %load-should-auto-compile #t) |
7ea1432e DT |
239 | |
240 | (save-module-excursion | |
241 | (lambda () | |
242 | (set-current-module user-module) | |
7ea1432e | 243 | |
2abcc97f LC |
244 | ;; Hide the "auto-compiling" messages. |
245 | (parameterize ((current-warning-port (%make-void-port "w"))) | |
db030303 LC |
246 | (call-with-prompt tag |
247 | (lambda () | |
7949c140 LC |
248 | (when (string=? (version) "2.2.3") |
249 | (catch 'system-error | |
250 | (lambda () | |
251 | (compile-file file #:env user-module)) | |
252 | (const #f))) ;EACCES maybe, let's interpret it | |
253 | ||
db030303 LC |
254 | ;; Give 'load' an absolute file name so that it doesn't try to |
255 | ;; search for FILE in %LOAD-PATH. Note: use 'load', not | |
256 | ;; 'primitive-load', so that FILE is compiled, which then allows us | |
257 | ;; to provide better error reporting with source line numbers. | |
258 | (load (canonicalize-path file))) | |
259 | (const #f)))))) | |
2abcc97f LC |
260 | (lambda _ |
261 | ;; XXX: Errors are reported from the pre-unwind handler below, but | |
262 | ;; calling 'exit' from there has no effect, so we call it here. | |
263 | (exit 1)) | |
264 | (rec (handle-error . args) | |
265 | ;; Capture the stack up to this procedure call, excluded, and pass | |
266 | ;; the faulty stack frame to 'report-load-error'. | |
db030303 | 267 | (let* ((stack (make-stack #t handle-error tag)) |
7f2f6a2c | 268 | (frame (last-frame-with-source stack))) |
db030303 LC |
269 | |
270 | (report-load-error file args frame) | |
271 | ||
272 | (case on-error | |
273 | ((debug) | |
274 | (newline) | |
69daee23 | 275 | (display (G_ "entering debugger; type ',bt' for a backtrace\n")) |
db030303 LC |
276 | (start-repl #:debug (make-debug (stack->vector stack) 0 |
277 | (error-string frame args) | |
278 | #f))) | |
279 | ((backtrace) | |
280 | (newline (current-error-port)) | |
281 | (display-backtrace stack (current-error-port))) | |
282 | (else | |
283 | #t)))))) | |
2abcc97f | 284 | |
a2985bb1 LC |
285 | (define (known-variable-definition variable) |
286 | "Search among the currently loaded modules one that defines a variable named | |
287 | VARIABLE and return it, or #f if none was found." | |
288 | (define (module<? m1 m2) | |
289 | (match (module-name m2) | |
290 | (('gnu _ ...) #t) | |
291 | (('guix _ ...) | |
292 | (match (module-name m1) | |
293 | (('gnu _ ...) #f) | |
294 | (_ #t))) | |
295 | (_ #f))) | |
296 | ||
b5bfa477 LC |
297 | (let loop ((modules (list (resolve-module '() #f #f #:ensure #f))) |
298 | (suggestions '()) | |
299 | (visited (setq))) | |
a2985bb1 LC |
300 | (match modules |
301 | (() | |
302 | ;; Pick the "best" suggestion. | |
303 | (match (sort suggestions module<?) | |
304 | (() #f) | |
305 | ((first _ ...) first))) | |
306 | ((head tail ...) | |
b5bfa477 LC |
307 | (if (set-contains? visited head) |
308 | (loop tail suggestions visited) | |
309 | (let ((visited (set-insert head visited)) | |
310 | (next (append tail | |
311 | (hash-map->list (lambda (name module) | |
312 | module) | |
313 | (module-submodules head))))) | |
314 | (match (module-local-variable head variable) | |
315 | (#f (loop next suggestions visited)) | |
316 | (_ | |
317 | (match (module-name head) | |
318 | (('gnu _ ...) head) ;must be that one | |
319 | (_ (loop next (cons head suggestions) visited))))))))))) | |
a2985bb1 | 320 | |
935542fb LC |
321 | (define* (display-hint message #:optional (port (current-error-port))) |
322 | "Display MESSAGE, a l10n message possibly containing Texinfo markup, to | |
323 | PORT." | |
324 | (format port (G_ "hint: ~a~%") | |
16de4555 LC |
325 | ;; XXX: We should arrange so that the initial indent is wider. |
326 | (parameterize ((%text-width (max 15 | |
327 | (- (terminal-columns) 5)))) | |
328 | (texi->plain-text message)))) | |
935542fb | 329 | |
2d2f98ef LC |
330 | (define* (report-unbound-variable-error args #:key frame) |
331 | "Return the given unbound-variable error, where ARGS is the list of 'throw' | |
332 | arguments." | |
333 | (match args | |
334 | ((key . args) | |
335 | (print-exception (current-error-port) frame key args))) | |
336 | (match args | |
337 | (('unbound-variable proc message (variable) _ ...) | |
338 | (match (known-variable-definition variable) | |
339 | (#f | |
340 | (display-hint (G_ "Did you forget a @code{use-modules} form?"))) | |
341 | ((? module? module) | |
342 | (display-hint (format #f (G_ "Did you forget @code{(use-modules ~a)}?") | |
343 | (module-name module)))))))) | |
344 | ||
2abcc97f | 345 | (define* (report-load-error file args #:optional frame) |
db030303 | 346 | "Report the failure to load FILE, a user-provided Scheme file. |
1151f6ae LC |
347 | ARGS is the list of arguments received by the 'throw' handler." |
348 | (match args | |
e465d9e1 | 349 | (('system-error . rest) |
1151f6ae | 350 | (let ((err (system-error-errno args))) |
69daee23 | 351 | (report-error (G_ "failed to load '~a': ~a~%") file (strerror err)))) |
a6e22d84 LC |
352 | (('read-error "scm_i_lreadparen" message _ ...) |
353 | ;; Guile's missing-paren messages are obscure so we make them more | |
354 | ;; intelligible here. | |
355 | (if (string-suffix? "end of file" message) | |
356 | (let ((location (string-drop-right message | |
357 | (string-length "end of file")))) | |
358 | (format (current-error-port) (G_ "~amissing closing parenthesis~%") | |
359 | location)) | |
360 | (apply throw args))) | |
1151f6ae LC |
361 | (('syntax-error proc message properties form . rest) |
362 | (let ((loc (source-properties->location properties))) | |
69daee23 | 363 | (format (current-error-port) (G_ "~a: error: ~a~%") |
db030303 | 364 | (location->string loc) message))) |
2d2f98ef LC |
365 | (('unbound-variable _ ...) |
366 | (report-unbound-variable-error args #:frame frame)) | |
23185cea | 367 | (('srfi-34 obj) |
f816dba6 | 368 | (if (message-condition? obj) |
23735137 LC |
369 | (if (error-location? obj) |
370 | (format (current-error-port) | |
371 | (G_ "~a: error: ~a~%") | |
372 | (location->string (error-location obj)) | |
373 | (gettext (condition-message obj) | |
374 | %gettext-domain)) | |
375 | (report-error (G_ "~a~%") | |
376 | (gettext (condition-message obj) | |
377 | %gettext-domain))) | |
378 | (report-error (G_ "exception thrown: ~s~%") obj)) | |
379 | (when (fix-hint? obj) | |
935542fb | 380 | (display-hint (condition-fix-hint obj)))) |
70a50305 | 381 | ((key args ...) |
69daee23 | 382 | (report-error (G_ "failed to load '~a':~%") file) |
70a50305 LC |
383 | (match args |
384 | (((? symbol? proc) (? string? message) (args ...) . rest) | |
385 | (display-error frame (current-error-port) proc message | |
386 | args rest)) | |
387 | (_ | |
388 | ;; Some exceptions like 'git-error' do not follow Guile's convention | |
389 | ;; above and need to be printed with 'print-exception'. | |
390 | (print-exception (current-error-port) frame key args)))))) | |
1151f6ae | 391 | |
4ae7559f LC |
392 | (define (warn-about-load-error file args) ;FIXME: factorize with ↑ |
393 | "Report the failure to load FILE, a user-provided Scheme file, without | |
394 | exiting. ARGS is the list of arguments received by the 'throw' handler." | |
395 | (match args | |
e465d9e1 | 396 | (('system-error . rest) |
4ae7559f | 397 | (let ((err (system-error-errno args))) |
69daee23 | 398 | (warning (G_ "failed to load '~a': ~a~%") file (strerror err)))) |
4ae7559f LC |
399 | (('syntax-error proc message properties form . rest) |
400 | (let ((loc (source-properties->location properties))) | |
69daee23 | 401 | (format (current-error-port) (G_ "~a: warning: ~a~%") |
4ae7559f | 402 | (location->string loc) message))) |
23185cea | 403 | (('srfi-34 obj) |
f816dba6 | 404 | (if (message-condition? obj) |
69daee23 | 405 | (warning (G_ "failed to load '~a': ~a~%") |
f816dba6 LC |
406 | file |
407 | (gettext (condition-message obj) %gettext-domain)) | |
69daee23 | 408 | (warning (G_ "failed to load '~a': exception thrown: ~s~%") |
f816dba6 | 409 | file obj))) |
4ae7559f | 410 | ((error args ...) |
69daee23 | 411 | (warning (G_ "failed to load '~a':~%") file) |
4ae7559f LC |
412 | (apply display-error #f (current-error-port) args)))) |
413 | ||
2d2f98ef LC |
414 | (define (call-with-unbound-variable-handling thunk) |
415 | (define tag | |
416 | (make-prompt-tag "user-code")) | |
417 | ||
418 | (catch 'unbound-variable | |
419 | (lambda () | |
420 | (call-with-prompt tag | |
421 | thunk | |
422 | (const #f))) | |
423 | (const #t) | |
424 | (rec (handle-error . args) | |
425 | (let* ((stack (make-stack #t handle-error tag)) | |
426 | (frame (and stack (last-frame-with-source stack)))) | |
427 | (report-unbound-variable-error args #:frame frame) | |
428 | (exit 1))))) | |
429 | ||
430 | (define-syntax-rule (with-unbound-variable-handling exp ...) | |
431 | "Capture 'unbound-variable' exceptions in the dynamic extent of EXP... and | |
432 | report them in a user-friendly way." | |
433 | (call-with-unbound-variable-handling (lambda () exp ...))) | |
434 | ||
b2a886f6 LC |
435 | (define (install-locale) |
436 | "Install the current locale settings." | |
437 | (catch 'system-error | |
438 | (lambda _ | |
439 | (setlocale LC_ALL "")) | |
440 | (lambda args | |
26db747a LC |
441 | (display-hint (G_ "Consider installing the @code{glibc-utf8-locales} or |
442 | @code{glibc-locales} package and defining @code{GUIX_LOCPATH}, along these | |
443 | lines: | |
444 | ||
445 | @example | |
446 | guix package -i glibc-utf8-locales | |
447 | export GUIX_LOCPATH=\"$HOME/.guix-profile/lib/locale\" | |
448 | @end example | |
449 | ||
450 | See the \"Application Setup\" section in the manual, for more info.\n"))))) | |
b2a886f6 | 451 | |
e49951eb | 452 | (define (initialize-guix) |
633f045f | 453 | "Perform the usual initialization for stand-alone Guix commands." |
ce92d269 LC |
454 | ;; By default don't annoy users with deprecation warnings. In practice, |
455 | ;; 'define-deprecated' in (ice-9 deprecated) arranges so that those warnings | |
456 | ;; are emitted at expansion-time only, but there are cases where they could | |
457 | ;; slip through, for instance when interpreting code. | |
458 | (unless (getenv "GUILE_WARN_DEPRECATED") | |
459 | (debug-disable 'warn-deprecated)) | |
460 | ||
e49951eb | 461 | (install-locale) |
39e9f95d | 462 | (textdomain %gettext-domain) |
e14c3929 LC |
463 | |
464 | ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be | |
465 | ;; notified via an EPIPE later. | |
466 | (sigaction SIGPIPE SIG_IGN) | |
467 | ||
76832d34 LC |
468 | (setvbuf (current-output-port) 'line) |
469 | (setvbuf (current-error-port) 'line)) | |
e49951eb | 470 | |
cdd5d6f9 LC |
471 | (define* (show-version-and-exit #:optional (command (car (command-line)))) |
472 | "Display version information for COMMAND and `(exit 0)'." | |
473 | (simple-format #t "~a (~a) ~a~%" | |
474 | command %guix-package-name %guix-version) | |
a173f098 | 475 | (format #t "Copyright ~a 2019 ~a" |
d925cdc3 LC |
476 | ;; TRANSLATORS: Translate "(C)" to the copyright symbol |
477 | ;; (C-in-a-circle), if this symbol is available in the user's | |
478 | ;; locale. Otherwise, do not translate "(C)"; leave it as-is. */ | |
69daee23 LC |
479 | (G_ "(C)") |
480 | (G_ "the Guix authors\n")) | |
481 | (display (G_"\ | |
64a967cc LC |
482 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
483 | This is free software: you are free to change and redistribute it. | |
484 | There is NO WARRANTY, to the extent permitted by law. | |
485 | ")) | |
cdd5d6f9 LC |
486 | (exit 0)) |
487 | ||
3441e164 | 488 | (define (show-bug-report-information) |
cf5f2ad3 ML |
489 | ;; TRANSLATORS: The placeholder indicates the bug-reporting address for this |
490 | ;; package. Please add another line saying "Report translation bugs to | |
491 | ;; ...\n" with the address for translation bugs (typically your translation | |
492 | ;; team's web or email address). | |
69daee23 | 493 | (format #t (G_ " |
3441e164 | 494 | Report bugs to: ~a.") %guix-bug-report-address) |
69daee23 | 495 | (format #t (G_ " |
3441e164 | 496 | ~a home page: <~a>") %guix-package-name %guix-home-page-url) |
69daee23 | 497 | (display (G_ " |
3441e164 LC |
498 | General help using GNU software: <http://www.gnu.org/gethelp/>")) |
499 | (newline)) | |
500 | ||
e7ff0543 LC |
501 | (define (augmented-system-error-handler file) |
502 | "Return a 'system-error' handler that mentions FILE in its message." | |
503 | (lambda (key proc fmt args errno) | |
504 | ;; Augment the FMT and ARGS with information about TARGET (this | |
505 | ;; information is missing as of Guile 2.0.11, making the exception | |
506 | ;; uninformative.) | |
507 | (apply throw key proc "~A: ~S" | |
508 | (list (strerror (car errno)) file) | |
509 | (list errno)))) | |
510 | ||
6616a352 TS |
511 | (define-syntax apply-formals |
512 | (syntax-rules () | |
513 | ((_ proc (args ...)) (proc args ...)) | |
514 | ((_ proc (arg1 args ... . rest)) (apply proc arg1 args ... rest)))) | |
515 | ||
516 | (define-syntax-rule (error-reporting-wrapper proc formals file) | |
e7ff0543 LC |
517 | "Wrap PROC such that its 'system-error' exceptions are augmented to mention |
518 | FILE." | |
519 | (let ((real-proc (@ (guile) proc))) | |
6616a352 | 520 | (lambda formals |
e7ff0543 LC |
521 | (catch 'system-error |
522 | (lambda () | |
6616a352 | 523 | (apply-formals real-proc formals)) |
e7ff0543 LC |
524 | (augmented-system-error-handler file))))) |
525 | ||
7522a016 LC |
526 | (set! symlink |
527 | ;; We 'set!' the global binding because (gnu build ...) modules and similar | |
528 | ;; typically don't use (guix ui). | |
e7ff0543 | 529 | (error-reporting-wrapper symlink (source target) target)) |
44fd6ef1 | 530 | |
9b14107f LC |
531 | (set! copy-file |
532 | ;; Note: here we use 'set!', not #:replace, because UIs typically use | |
533 | ;; 'copy-recursively', which doesn't use (guix ui). | |
e7ff0543 LC |
534 | (error-reporting-wrapper copy-file (source target) target)) |
535 | ||
6d30b1b2 LC |
536 | (set! canonicalize-path |
537 | (error-reporting-wrapper canonicalize-path (file) file)) | |
538 | ||
90b144d2 LC |
539 | (set! delete-file |
540 | (error-reporting-wrapper delete-file (file) file)) | |
541 | ||
6616a352 TS |
542 | (set! execlp |
543 | (error-reporting-wrapper execlp (filename . args) filename)) | |
9b14107f | 544 | |
fd688c82 LC |
545 | (define (make-regexp* regexp . flags) |
546 | "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error | |
547 | nicely." | |
548 | (catch 'regular-expression-syntax | |
549 | (lambda () | |
550 | (apply make-regexp regexp flags)) | |
551 | (lambda (key proc message . rest) | |
69daee23 | 552 | (leave (G_ "'~a' is not a valid regular expression: ~a~%") |
fd688c82 LC |
553 | regexp message)))) |
554 | ||
969e678e LC |
555 | (define (string->number* str) |
556 | "Like `string->number', but error out with an error message on failure." | |
557 | (or (string->number str) | |
69daee23 | 558 | (leave (G_ "~a: invalid number~%") str))) |
969e678e | 559 | |
1d6243cf LC |
560 | (define (size->number str) |
561 | "Convert STR, a storage measurement representation such as \"1024\" or | |
562 | \"1MiB\", to a number of bytes. Raise an error if STR could not be | |
563 | interpreted." | |
564 | (define unit-pos | |
565 | (string-rindex str char-set:digit)) | |
566 | ||
567 | (define unit | |
568 | (and unit-pos (substring str (+ 1 unit-pos)))) | |
569 | ||
570 | (let* ((numstr (if unit-pos | |
571 | (substring str 0 (+ 1 unit-pos)) | |
572 | str)) | |
573 | (num (string->number numstr))) | |
574 | (unless num | |
69daee23 | 575 | (leave (G_ "invalid number: ~a~%") numstr)) |
1d6243cf LC |
576 | |
577 | ((compose inexact->exact round) | |
578 | (* num | |
579 | (match unit | |
4a44d7bb LC |
580 | ((or "KiB" "K" "k") (expt 2 10)) |
581 | ((or "MiB" "M") (expt 2 20)) | |
582 | ((or "GiB" "G") (expt 2 30)) | |
583 | ((or "TiB" "T") (expt 2 40)) | |
584 | ((or "PiB" "P") (expt 2 50)) | |
585 | ((or "EiB" "E") (expt 2 60)) | |
586 | ((or "ZiB" "Z") (expt 2 70)) | |
587 | ((or "YiB" "Y") (expt 2 80)) | |
588 | ("kB" (expt 10 3)) | |
1d6243cf LC |
589 | ("MB" (expt 10 6)) |
590 | ("GB" (expt 10 9)) | |
591 | ("TB" (expt 10 12)) | |
4a44d7bb LC |
592 | ("PB" (expt 10 15)) |
593 | ("EB" (expt 10 18)) | |
594 | ("ZB" (expt 10 21)) | |
595 | ("YB" (expt 10 24)) | |
1d6243cf | 596 | ("" 1) |
e465d9e1 | 597 | (x |
69daee23 | 598 | (leave (G_ "unknown unit: ~a~%") unit))))))) |
1d6243cf | 599 | |
3b80b813 LC |
600 | (define (display-collision-resolution-hint collision) |
601 | "Display hints on how to resolve COLLISION, a &profile-collistion-error." | |
602 | (define (top-most-entry entry) | |
603 | (let loop ((entry entry)) | |
604 | (match (force (manifest-entry-parent entry)) | |
605 | (#f entry) | |
606 | (parent (loop parent))))) | |
607 | ||
608 | (let* ((first (profile-collision-error-entry collision)) | |
609 | (second (profile-collision-error-conflict collision)) | |
610 | (name1 (manifest-entry-name (top-most-entry first))) | |
611 | (name2 (manifest-entry-name (top-most-entry second)))) | |
612 | (if (string=? name1 name2) | |
613 | (display-hint (format #f (G_ "You cannot have two different versions | |
614 | or variants of @code{~a} in the same profile.") | |
615 | name1)) | |
616 | (display-hint (format #f (G_ "Try upgrading both @code{~a} and @code{~a}, | |
617 | or remove one of them from the profile.") | |
618 | name1 name2))))) | |
619 | ||
073c34d7 LC |
620 | (define (call-with-error-handling thunk) |
621 | "Call THUNK within a user-friendly error handler." | |
bec7f352 LC |
622 | (define (port-filename* port) |
623 | ;; 'port-filename' returns #f for non-file ports, but it raises an | |
624 | ;; exception for file ports that are closed. Work around that. | |
625 | (and (not (port-closed? port)) | |
626 | (port-filename port))) | |
627 | ||
073c34d7 LC |
628 | (guard (c ((package-input-error? c) |
629 | (let* ((package (package-error-package c)) | |
630 | (input (package-error-invalid-input c)) | |
631 | (location (package-location package)) | |
632 | (file (location-file location)) | |
633 | (line (location-line location)) | |
634 | (column (location-column location))) | |
69daee23 | 635 | (leave (G_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") |
073c34d7 LC |
636 | file line column |
637 | (package-full-name package) input))) | |
9b222abe LC |
638 | ((package-cross-build-system-error? c) |
639 | (let* ((package (package-error-package c)) | |
640 | (loc (package-location package)) | |
641 | (system (package-build-system package))) | |
69daee23 | 642 | (leave (G_ "~a: ~a: build system `~a' does not support cross builds~%") |
9b222abe LC |
643 | (location->string loc) |
644 | (package-full-name package) | |
645 | (build-system-name system)))) | |
3e43166f LC |
646 | ((gexp-input-error? c) |
647 | (let ((input (package-error-invalid-input c))) | |
69daee23 | 648 | (leave (G_ "~s: invalid G-expression input~%") |
3e43166f | 649 | (gexp-error-invalid-input c)))) |
c0c018f1 | 650 | ((profile-not-found-error? c) |
69daee23 | 651 | (leave (G_ "profile '~a' does not exist~%") |
c0c018f1 AK |
652 | (profile-error-profile c))) |
653 | ((missing-generation-error? c) | |
69daee23 | 654 | (leave (G_ "generation ~a of profile '~a' does not exist~%") |
c0c018f1 AK |
655 | (missing-generation-error-generation c) |
656 | (profile-error-profile c))) | |
487cbb01 LC |
657 | ((unmatched-pattern-error? c) |
658 | (let ((pattern (unmatched-pattern-error-pattern c))) | |
659 | (leave (G_ "package '~a~@[@~a~]~@[:~a~]' not found in profile~%") | |
660 | (manifest-pattern-name pattern) | |
661 | (manifest-pattern-version pattern) | |
662 | (match (manifest-pattern-output pattern) | |
663 | ("out" #f) | |
664 | (output output))))) | |
a654dc4b LC |
665 | ((profile-collision-error? c) |
666 | (let ((entry (profile-collision-error-entry c)) | |
667 | (conflict (profile-collision-error-conflict c))) | |
668 | (define (report-parent-entries entry) | |
669 | (let ((parent (force (manifest-entry-parent entry)))) | |
670 | (when (manifest-entry? parent) | |
671 | (report-error (G_ " ... propagated from ~a@~a~%") | |
672 | (manifest-entry-name parent) | |
673 | (manifest-entry-version parent)) | |
674 | (report-parent-entries parent)))) | |
675 | ||
0d2ecf8b LC |
676 | (define (manifest-entry-output* entry) |
677 | (match (manifest-entry-output entry) | |
678 | ("out" "") | |
679 | (output (string-append ":" output)))) | |
680 | ||
3fb6464b | 681 | (report-error (G_ "profile contains conflicting entries for ~a~a~%") |
a654dc4b | 682 | (manifest-entry-name entry) |
3fb6464b | 683 | (manifest-entry-output* entry)) |
0d2ecf8b | 684 | (report-error (G_ " first entry: ~a@~a~a ~a~%") |
a654dc4b LC |
685 | (manifest-entry-name entry) |
686 | (manifest-entry-version entry) | |
0d2ecf8b | 687 | (manifest-entry-output* entry) |
a654dc4b LC |
688 | (manifest-entry-item entry)) |
689 | (report-parent-entries entry) | |
0d2ecf8b | 690 | (report-error (G_ " second entry: ~a@~a~a ~a~%") |
a654dc4b LC |
691 | (manifest-entry-name conflict) |
692 | (manifest-entry-version conflict) | |
0d2ecf8b | 693 | (manifest-entry-output* conflict) |
a654dc4b LC |
694 | (manifest-entry-item conflict)) |
695 | (report-parent-entries conflict) | |
3b80b813 | 696 | (display-collision-resolution-hint c) |
a654dc4b | 697 | (exit 1))) |
b7071bc5 LC |
698 | ((nar-error? c) |
699 | (let ((file (nar-error-file c)) | |
700 | (port (nar-error-port c))) | |
701 | (if file | |
69daee23 | 702 | (leave (G_ "corrupt input while restoring '~a' from ~s~%") |
bec7f352 | 703 | file (or (port-filename* port) port)) |
69daee23 | 704 | (leave (G_ "corrupt input while restoring archive from ~s~%") |
bec7f352 | 705 | (or (port-filename* port) port))))) |
f9e8a123 | 706 | ((store-connection-error? c) |
69daee23 | 707 | (leave (G_ "failed to connect to `~a': ~a~%") |
f9e8a123 LC |
708 | (store-connection-error-file c) |
709 | (strerror (store-connection-error-code c)))) | |
710 | ((store-protocol-error? c) | |
073c34d7 | 711 | ;; FIXME: Server-provided error messages aren't i18n'd. |
e6b065b2 | 712 | (leave (G_ "~a~%") |
f9e8a123 | 713 | (store-protocol-error-message c))) |
f304c9c2 | 714 | ((derivation-missing-output-error? c) |
69daee23 | 715 | (leave (G_ "reference to invalid output '~a' of derivation '~a'~%") |
f304c9c2 LC |
716 | (derivation-missing-output c) |
717 | (derivation-file-name (derivation-error-derivation c)))) | |
d26e1967 | 718 | ((file-search-error? c) |
69daee23 | 719 | (leave (G_ "file '~a' could not be found in these \ |
d26e1967 LC |
720 | directories:~{ ~a~}~%") |
721 | (file-search-error-file-name c) | |
722 | (file-search-error-search-path c))) | |
cbdfa50d MW |
723 | ((invoke-error? c) |
724 | (leave (G_ "program exited\ | |
725 | ~@[ with non-zero exit status ~a~]\ | |
726 | ~@[ terminated by signal ~a~]\ | |
727 | ~@[ stopped by signal ~a~]: ~s~%") | |
728 | (invoke-error-exit-status c) | |
729 | (invoke-error-term-signal c) | |
730 | (invoke-error-stop-signal c) | |
731 | (cons (invoke-error-program c) | |
732 | (invoke-error-arguments c)))) | |
23735137 LC |
733 | ((and (error-location? c) (message-condition? c)) |
734 | (format (current-error-port) | |
735 | (G_ "~a: error: ~a~%") | |
736 | (location->string (error-location c)) | |
399993f8 | 737 | (gettext (condition-message c) %gettext-domain)) |
e1ba6d49 LC |
738 | (when (fix-hint? c) |
739 | (display-hint (condition-fix-hint c))) | |
399993f8 | 740 | (exit 1)) |
4a8d536f LC |
741 | ((and (message-condition? c) (fix-hint? c)) |
742 | (format (current-error-port) "~a: error: ~a~%" | |
743 | (program-name) | |
744 | (gettext (condition-message c) %gettext-domain)) | |
745 | (display-hint (condition-fix-hint c)) | |
746 | (exit 1)) | |
c1d52c71 LC |
747 | ((message-condition? c) |
748 | ;; Normally '&message' error conditions have an i18n'd message. | |
69daee23 | 749 | (leave (G_ "~a~%") |
12703d08 | 750 | (gettext (condition-message c) %gettext-domain)))) |
e14c3929 LC |
751 | ;; Catch EPIPE and the likes. |
752 | (catch 'system-error | |
753 | thunk | |
bde8c0e6 | 754 | (lambda (key proc format-string format-args . rest) |
69daee23 | 755 | (leave (G_ "~a: ~a~%") proc |
bde8c0e6 | 756 | (apply format #f format-string format-args)))))) |
073c34d7 | 757 | |
df36e629 LC |
758 | (define-syntax-rule (leave-on-EPIPE exp ...) |
759 | "Run EXP... in a context when EPIPE errors are caught and lead to 'exit' | |
760 | with successful exit code. This is useful when writing to the standard output | |
761 | may lead to EPIPE, because the standard output is piped through 'head' or | |
762 | similar." | |
763 | (catch 'system-error | |
764 | (lambda () | |
765 | exp ...) | |
766 | (lambda args | |
767 | ;; We really have to exit this brutally, otherwise Guile eventually | |
768 | ;; attempts to flush all the ports, leading to an uncaught EPIPE down | |
769 | ;; the path. | |
770 | (if (= EPIPE (system-error-errno args)) | |
771 | (primitive-_exit 0) | |
772 | (apply throw args))))) | |
773 | ||
56b82106 LC |
774 | (define %guix-user-module |
775 | ;; Module in which user expressions are evaluated. | |
07254feb LC |
776 | ;; Compute lazily to avoid circularity with (guix gexp). |
777 | (delay | |
778 | (let ((module (make-module))) | |
779 | (beautify-user-module! module) | |
780 | ;; Use (guix gexp) so that one can use #~ & co. | |
781 | (module-use! module (resolve-interface '(guix gexp))) | |
782 | module))) | |
56b82106 | 783 | |
ac5de156 LC |
784 | (define (read/eval str) |
785 | "Read and evaluate STR, raising an error if something goes wrong." | |
eb0880e7 LC |
786 | (let ((exp (catch #t |
787 | (lambda () | |
788 | (call-with-input-string str read)) | |
789 | (lambda args | |
69daee23 | 790 | (leave (G_ "failed to read expression ~s: ~s~%") |
eb0880e7 | 791 | str args))))) |
ac5de156 LC |
792 | (catch #t |
793 | (lambda () | |
07254feb | 794 | (eval exp (force %guix-user-module))) |
ac5de156 | 795 | (lambda args |
69daee23 | 796 | (report-error (G_ "failed to evaluate expression '~a':~%") exp) |
41766807 LC |
797 | (match args |
798 | (('syntax-error proc message properties form . rest) | |
69daee23 | 799 | (report-error (G_ "syntax error: ~a~%") message)) |
23185cea | 800 | (('srfi-34 obj) |
f816dba6 | 801 | (if (message-condition? obj) |
69daee23 | 802 | (report-error (G_ "~a~%") |
f816dba6 LC |
803 | (gettext (condition-message obj) |
804 | %gettext-domain)) | |
69daee23 | 805 | (report-error (G_ "exception thrown: ~s~%") obj))) |
41766807 LC |
806 | ((error args ...) |
807 | (apply display-error #f (current-error-port) args)) | |
808 | (what? #f)) | |
809 | (exit 1))))) | |
ac5de156 LC |
810 | |
811 | (define (read/eval-package-expression str) | |
812 | "Read and evaluate STR and return the package it refers to, or exit an | |
813 | error." | |
814 | (match (read/eval str) | |
815 | ((? package? p) p) | |
e465d9e1 | 816 | (x |
69daee23 | 817 | (leave (G_ "expression ~s does not evaluate to a package~%") |
ac5de156 | 818 | str)))) |
eb0880e7 | 819 | |
fa394eb9 AK |
820 | (define (show-derivation-outputs derivation) |
821 | "Show the output file names of DERIVATION." | |
822 | (format #t "~{~a~%~}" | |
823 | (map (match-lambda | |
824 | ((out-name . out) | |
825 | (derivation->output-path derivation out-name))) | |
826 | (derivation-outputs derivation)))) | |
827 | ||
8120b23e LC |
828 | (define* (check-available-space need |
829 | #:optional (directory (%store-prefix))) | |
830 | "Make sure at least NEED bytes are available in DIRECTORY. Otherwise emit a | |
dcfc6f21 LC |
831 | warning." |
832 | (let ((free (catch 'system-error | |
833 | (lambda () | |
8120b23e | 834 | (free-disk-space directory)) |
dcfc6f21 LC |
835 | (const #f)))) |
836 | (when (and free (>= need free)) | |
837 | (warning (G_ "at least ~,1h MB needed but only ~,1h MB available in ~a~%") | |
8120b23e | 838 | (/ need 1e6) (/ free 1e6) directory)))) |
dcfc6f21 | 839 | |
d4aa147e LC |
840 | (define (graft-derivation? drv) |
841 | "Return true if DRV is definitely a graft derivation, false otherwise." | |
842 | (match (assq-ref (derivation-properties drv) 'type) | |
843 | ('graft #t) | |
844 | (_ #f))) | |
845 | ||
80eebee9 RW |
846 | (define (profile-hook-derivation? drv) |
847 | "Return true if DRV is definitely a profile hook derivation, false otherwise." | |
848 | (match (assq-ref (derivation-properties drv) 'type) | |
849 | ('profile-hook #t) | |
850 | (_ #f))) | |
851 | ||
dd36b51b | 852 | (define* (show-what-to-build store drv |
58c08df0 LC |
853 | #:key dry-run? (use-substitutes? #t) |
854 | (mode (build-mode normal))) | |
9bb2b96a | 855 | "Show what will or would (depending on DRY-RUN?) be built in realizing the |
58c08df0 LC |
856 | derivations listed in DRV using MODE, a 'build-mode' value. Return #t if |
857 | there's something to build, #f otherwise. When USE-SUBSTITUTES?, check and | |
858 | report what is prerequisites are available for download." | |
2dc98729 | 859 | (define substitutable-info |
e9651e39 LC |
860 | ;; Call 'substitutation-oracle' upfront so we don't end up launching the |
861 | ;; substituter many times. This makes a big difference, especially when | |
862 | ;; DRV is a long list as is the case with 'guix environment'. | |
863 | (if use-substitutes? | |
58c08df0 | 864 | (substitution-oracle store drv #:mode mode) |
e9651e39 LC |
865 | (const #f))) |
866 | ||
52ddf2ae | 867 | (define (built-or-substitutable? drv) |
f304c9c2 LC |
868 | (or (null? (derivation-outputs drv)) |
869 | (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists | |
52ddf2ae | 870 | (or (valid-path? store out) |
2dc98729 | 871 | (substitutable-info out))))) |
52ddf2ae | 872 | |
dd36b51b | 873 | (let*-values (((build download) |
59688fc4 LC |
874 | (fold2 (lambda (drv build download) |
875 | (let-values (((b d) | |
876 | (derivation-prerequisites-to-build | |
877 | store drv | |
58c08df0 | 878 | #:mode mode |
2dc98729 LC |
879 | #:substitutable-info |
880 | substitutable-info))) | |
59688fc4 LC |
881 | (values (append b build) |
882 | (append d download)))) | |
dd36b51b LC |
883 | '() '() |
884 | drv)) | |
885 | ((build) ; add the DRV themselves | |
886 | (delete-duplicates | |
59688fc4 | 887 | (append (map derivation-file-name |
52ddf2ae | 888 | (remove built-or-substitutable? drv)) |
dd36b51b LC |
889 | (map derivation-input-path build)))) |
890 | ((download) ; add the references of DOWNLOAD | |
1a8b7834 LC |
891 | (if use-substitutes? |
892 | (delete-duplicates | |
893 | (append download | |
2dc98729 LC |
894 | (filter-map (lambda (item) |
895 | (if (valid-path? store item) | |
896 | #f | |
897 | (substitutable-info item))) | |
898 | (append-map | |
899 | substitutable-references | |
900 | download)))) | |
d4aa147e | 901 | download)) |
80eebee9 RW |
902 | ((graft hook build) |
903 | (match (fold (lambda (file acc) | |
904 | (let ((drv (read-derivation-from-file file))) | |
905 | (match acc | |
906 | ((#:graft graft #:hook hook #:build build) | |
907 | (cond | |
908 | ((graft-derivation? drv) | |
909 | `(#:graft ,(cons file graft) | |
910 | #:hook ,hook | |
911 | #:build ,build)) | |
912 | ((profile-hook-derivation? drv) | |
913 | `(#:graft ,graft | |
914 | #:hook ,(cons file hook) | |
915 | #:build ,build)) | |
916 | (else | |
917 | `(#:graft ,graft | |
918 | #:hook ,hook | |
919 | #:build ,(cons file build)))))))) | |
920 | '(#:graft () #:hook () #:build ()) | |
921 | build) | |
922 | ((#:graft graft #:hook hook #:build build) | |
923 | (values graft hook build))))) | |
dcfc6f21 LC |
924 | (define installed-size |
925 | (reduce + 0 (map substitutable-nar-size download))) | |
926 | ||
d9bad2f0 LC |
927 | (define download-size |
928 | (/ (reduce + 0 (map substitutable-download-size download)) | |
929 | 1e6)) | |
930 | ||
931 | (define display-download-size? | |
932 | ;; Sometimes narinfos lack information about the download size. Only | |
933 | ;; display when we have information for all of DOWNLOAD. | |
934 | (not (any (compose zero? substitutable-download-size) download))) | |
935 | ||
9bb2b96a | 936 | (if dry-run? |
dd36b51b LC |
937 | (begin |
938 | (format (current-error-port) | |
83e61a73 LC |
939 | (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]" |
940 | "~:[The following derivations would be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
941 | (length build)) |
942 | (null? build) build) | |
d9bad2f0 LC |
943 | (if display-download-size? |
944 | (format (current-error-port) | |
945 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
946 | ;; translated to the corresponding abbreviation. | |
947 | (G_ "~:[~,1h MB would be downloaded:~%~{ ~a~%~}~;~]") | |
948 | (null? download) | |
949 | download-size | |
950 | (map substitutable-path download)) | |
951 | (format (current-error-port) | |
952 | (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]" | |
953 | "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]" | |
954 | (length download)) | |
955 | (null? download) | |
d4aa147e LC |
956 | (map substitutable-path download))) |
957 | (format (current-error-port) | |
958 | (N_ "~:[The following graft would be made:~%~{ ~a~%~}~;~]" | |
959 | "~:[The following grafts would be made:~%~{ ~a~%~}~;~]" | |
960 | (length graft)) | |
80eebee9 RW |
961 | (null? graft) graft) |
962 | (format (current-error-port) | |
963 | (N_ "~:[The following profile hook would be built:~%~{ ~a~%~}~;~]" | |
964 | "~:[The following profile hooks would be built:~%~{ ~a~%~}~;~]" | |
965 | (length hook)) | |
966 | (null? hook) hook)) | |
dd36b51b LC |
967 | (begin |
968 | (format (current-error-port) | |
83e61a73 LC |
969 | (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]" |
970 | "~:[The following derivations will be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
971 | (length build)) |
972 | (null? build) build) | |
d9bad2f0 LC |
973 | (if display-download-size? |
974 | (format (current-error-port) | |
975 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
976 | ;; translated to the corresponding abbreviation. | |
977 | (G_ "~:[~,1h MB will be downloaded:~%~{ ~a~%~}~;~]") | |
978 | (null? download) | |
979 | download-size | |
980 | (map substitutable-path download)) | |
981 | (format (current-error-port) | |
982 | (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]" | |
983 | "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]" | |
984 | (length download)) | |
985 | (null? download) | |
d4aa147e LC |
986 | (map substitutable-path download))) |
987 | (format (current-error-port) | |
988 | (N_ "~:[The following graft will be made:~%~{ ~a~%~}~;~]" | |
989 | "~:[The following grafts will be made:~%~{ ~a~%~}~;~]" | |
990 | (length graft)) | |
80eebee9 RW |
991 | (null? graft) graft) |
992 | (format (current-error-port) | |
993 | (N_ "~:[The following profile hook will be built:~%~{ ~a~%~}~;~]" | |
994 | "~:[The following profile hooks will be built:~%~{ ~a~%~}~;~]" | |
995 | (length hook)) | |
996 | (null? hook) hook))) | |
dcfc6f21 LC |
997 | |
998 | (check-available-space installed-size) | |
999 | ||
dd36b51b | 1000 | (pair? build))) |
9bb2b96a | 1001 | |
4d043ab6 DT |
1002 | (define show-what-to-build* |
1003 | (store-lift show-what-to-build)) | |
1004 | ||
5d7a8584 AK |
1005 | (define (right-arrow port) |
1006 | "Return either a string containing the 'RIGHT ARROW' character, or an ASCII | |
1007 | replacement if PORT is not Unicode-capable." | |
1062063a LC |
1008 | (let ((encoding (port-encoding port)) |
1009 | (arrow "→")) | |
1010 | (catch 'encoding-error | |
1011 | (lambda () | |
1012 | (call-with-output-string | |
1013 | (lambda (port) | |
1014 | (set-port-encoding! port encoding) | |
1015 | (set-port-conversion-strategy! port 'error) | |
1016 | (display arrow port)))) | |
1017 | (lambda (key . args) | |
1018 | "->")))) | |
5d7a8584 AK |
1019 | |
1020 | (define* (show-manifest-transaction store manifest transaction | |
1021 | #:key dry-run?) | |
1022 | "Display what will/would be installed/removed from MANIFEST by TRANSACTION." | |
1023 | (define (package-strings name version output item) | |
1024 | (map (lambda (name version output item) | |
1025 | (format #f " ~a~:[:~a~;~*~]\t~a\t~a" | |
1026 | name | |
1027 | (equal? output "out") output version | |
1028 | (if (package? item) | |
1029 | (package-output store item output) | |
1030 | item))) | |
1031 | name version output item)) | |
1032 | ||
1033 | (define → ;an arrow that can be represented on stderr | |
1034 | (right-arrow (current-error-port))) | |
1035 | ||
1036 | (define (upgrade-string name old-version new-version output item) | |
1037 | (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a" | |
1038 | name (equal? output "out") output | |
1039 | old-version → new-version | |
1040 | (if (package? item) | |
1041 | (package-output store item output) | |
1042 | item))) | |
1043 | ||
46b23e1a | 1044 | (let-values (((remove install upgrade downgrade) |
5d7a8584 AK |
1045 | (manifest-transaction-effects manifest transaction))) |
1046 | (match remove | |
1047 | ((($ <manifest-entry> name version output item) ..1) | |
1048 | (let ((len (length name)) | |
1049 | (remove (package-strings name version output item))) | |
1050 | (if dry-run? | |
1051 | (format (current-error-port) | |
1052 | (N_ "The following package would be removed:~%~{~a~%~}~%" | |
1053 | "The following packages would be removed:~%~{~a~%~}~%" | |
1054 | len) | |
1055 | remove) | |
1056 | (format (current-error-port) | |
1057 | (N_ "The following package will be removed:~%~{~a~%~}~%" | |
1058 | "The following packages will be removed:~%~{~a~%~}~%" | |
1059 | len) | |
1060 | remove)))) | |
30906825 | 1061 | (x #f)) |
46b23e1a LC |
1062 | (match downgrade |
1063 | (((($ <manifest-entry> name old-version) | |
1064 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
1065 | (let ((len (length name)) | |
1066 | (downgrade (map upgrade-string | |
1067 | name old-version new-version output item))) | |
1068 | (if dry-run? | |
1069 | (format (current-error-port) | |
1070 | (N_ "The following package would be downgraded:~%~{~a~%~}~%" | |
1071 | "The following packages would be downgraded:~%~{~a~%~}~%" | |
1072 | len) | |
1073 | downgrade) | |
1074 | (format (current-error-port) | |
1075 | (N_ "The following package will be downgraded:~%~{~a~%~}~%" | |
1076 | "The following packages will be downgraded:~%~{~a~%~}~%" | |
1077 | len) | |
1078 | downgrade)))) | |
30906825 | 1079 | (x #f)) |
5d7a8584 AK |
1080 | (match upgrade |
1081 | (((($ <manifest-entry> name old-version) | |
1082 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
1083 | (let ((len (length name)) | |
1084 | (upgrade (map upgrade-string | |
1085 | name old-version new-version output item))) | |
1086 | (if dry-run? | |
1087 | (format (current-error-port) | |
1088 | (N_ "The following package would be upgraded:~%~{~a~%~}~%" | |
1089 | "The following packages would be upgraded:~%~{~a~%~}~%" | |
1090 | len) | |
1091 | upgrade) | |
1092 | (format (current-error-port) | |
1093 | (N_ "The following package will be upgraded:~%~{~a~%~}~%" | |
1094 | "The following packages will be upgraded:~%~{~a~%~}~%" | |
1095 | len) | |
1096 | upgrade)))) | |
30906825 | 1097 | (x #f)) |
5d7a8584 AK |
1098 | (match install |
1099 | ((($ <manifest-entry> name version output item _) ..1) | |
1100 | (let ((len (length name)) | |
1101 | (install (package-strings name version output item))) | |
1102 | (if dry-run? | |
1103 | (format (current-error-port) | |
1104 | (N_ "The following package would be installed:~%~{~a~%~}~%" | |
1105 | "The following packages would be installed:~%~{~a~%~}~%" | |
1106 | len) | |
1107 | install) | |
1108 | (format (current-error-port) | |
1109 | (N_ "The following package will be installed:~%~{~a~%~}~%" | |
1110 | "The following packages will be installed:~%~{~a~%~}~%" | |
1111 | len) | |
1112 | install)))) | |
30906825 | 1113 | (x #f)))) |
5d7a8584 | 1114 | |
073c34d7 LC |
1115 | (define-syntax with-error-handling |
1116 | (syntax-rules () | |
1117 | "Run BODY within a user-friendly error condition handler." | |
1118 | ((_ body ...) | |
1119 | (call-with-error-handling | |
1120 | (lambda () | |
1121 | body ...))))) | |
1122 | ||
64fc89b6 LC |
1123 | (define (location->string loc) |
1124 | "Return a human-friendly, GNU-standard representation of LOC." | |
1125 | (match loc | |
69daee23 | 1126 | (#f (G_ "<unknown location>")) |
64fc89b6 LC |
1127 | (($ <location> file line column) |
1128 | (format #f "~a:~a:~a" file line column)))) | |
1129 | ||
299112d3 LC |
1130 | (define* (fill-paragraph str width #:optional (column 0)) |
1131 | "Fill STR such that each line contains at most WIDTH characters, assuming | |
1132 | that the first character is at COLUMN. | |
1133 | ||
1134 | When STR contains a single line break surrounded by other characters, it is | |
1135 | converted to a space; sequences of more than one line break are preserved." | |
1136 | (define (maybe-break chr result) | |
1137 | (match result | |
1138 | ((column newlines chars) | |
1139 | (case chr | |
1140 | ((#\newline) | |
1141 | `(,column ,(+ 1 newlines) ,chars)) | |
1142 | (else | |
3a09e1d2 CS |
1143 | (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1)) |
1144 | (chars (case newlines | |
1145 | ((0) chars) | |
1146 | ((1) | |
1147 | (append (make-list spaces #\space) chars)) | |
1148 | (else | |
1149 | (append (make-list newlines #\newline) chars)))) | |
1150 | (column (case newlines | |
1151 | ((0) column) | |
1152 | ((1) (+ spaces column)) | |
1153 | (else 0)))) | |
299112d3 LC |
1154 | (let ((chars (cons chr chars)) |
1155 | (column (+ 1 column))) | |
1156 | (if (> column width) | |
1157 | (let*-values (((before after) | |
1158 | (break (cut eqv? #\space <>) chars)) | |
1159 | ((len) | |
1160 | (length before))) | |
1161 | (if (<= len width) | |
1162 | `(,len | |
1163 | 0 | |
1164 | ,(if (null? after) | |
1165 | before | |
3a09e1d2 CS |
1166 | (append before |
1167 | (cons #\newline | |
1168 | (drop-while (cut eqv? #\space <>) | |
1169 | after))))) | |
299112d3 LC |
1170 | `(,column 0 ,chars))) ; unbreakable |
1171 | `(,column 0 ,chars))))))))) | |
1172 | ||
1173 | (match (string-fold maybe-break | |
1174 | `(,column 0 ()) | |
1175 | str) | |
30906825 | 1176 | ((column newlines chars) |
299112d3 LC |
1177 | (list->string (reverse chars))))) |
1178 | ||
2876b989 LC |
1179 | \f |
1180 | ;;; | |
1181 | ;;; Packages. | |
1182 | ;;; | |
1183 | ||
1cd4027c | 1184 | (define %text-width |
9703fef4 | 1185 | (make-parameter (terminal-columns))) |
1cd4027c ML |
1186 | |
1187 | (set! (@@ (texinfo plain-text) wrap*) | |
1188 | ;; XXX: Monkey patch this private procedure to let 'package->recutils' | |
1189 | ;; parameterize the fill of description field correctly. | |
1190 | (lambda strings | |
1191 | (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*)))) | |
1192 | (fill-string (string-concatenate strings) | |
1193 | #:line-width (%text-width) #:initial-indent indent | |
1194 | #:subsequent-indent indent)))) | |
1195 | ||
1196 | (define (texi->plain-text str) | |
1197 | "Return a plain-text representation of texinfo fragment STR." | |
08d7e359 LC |
1198 | ;; 'texi-fragment->stexi' uses a string port so make sure it's a |
1199 | ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.) | |
1200 | (with-fluids ((%default-port-encoding "UTF-8")) | |
1201 | (stexi->plain-text (texi-fragment->stexi str)))) | |
1cd4027c | 1202 | |
689db38e AK |
1203 | (define (package-field-string package field-accessor) |
1204 | "Return a plain-text representation of PACKAGE field." | |
1205 | (and=> (field-accessor package) | |
1206 | (compose texi->plain-text P_))) | |
1207 | ||
1cd4027c ML |
1208 | (define (package-description-string package) |
1209 | "Return a plain-text representation of PACKAGE description field." | |
689db38e AK |
1210 | (package-field-string package package-description)) |
1211 | ||
1212 | (define (package-synopsis-string package) | |
1213 | "Return a plain-text representation of PACKAGE synopsis field." | |
1214 | (package-field-string package package-synopsis)) | |
1cd4027c | 1215 | |
299112d3 LC |
1216 | (define (string->recutils str) |
1217 | "Return a version of STR where newlines have been replaced by newlines | |
1218 | followed by \"+ \", which makes for a valid multi-line field value in the | |
1219 | `recutils' syntax." | |
1220 | (list->string | |
1221 | (string-fold-right (lambda (chr result) | |
1222 | (if (eqv? chr #\newline) | |
1223 | (cons* chr #\+ #\space result) | |
1224 | (cons chr result))) | |
1225 | '() | |
1226 | str))) | |
1227 | ||
4ee79f22 LC |
1228 | (define* (package->recutils p port #:optional (width (%text-width)) |
1229 | #:key (extra-fields '())) | |
299112d3 | 1230 | "Write to PORT a `recutils' record of package P, arranging to fit within |
4ee79f22 | 1231 | WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to emit." |
069d43a7 LC |
1232 | (define width* |
1233 | ;; The available number of columns once we've taken into account space for | |
1234 | ;; the initial "+ " prefix. | |
1235 | (if (> width 2) (- width 2) width)) | |
1236 | ||
20ffce82 | 1237 | (define (dependencies->recutils packages) |
dbde386e SB |
1238 | (let ((list (string-join (delete-duplicates |
1239 | (map package-full-name | |
1240 | (sort packages package<?))) " "))) | |
20ffce82 | 1241 | (string->recutils |
069d43a7 | 1242 | (fill-paragraph list width* |
20ffce82 LC |
1243 | (string-length "dependencies: "))))) |
1244 | ||
9c0fc279 CR |
1245 | (define (package<? p1 p2) |
1246 | (string<? (package-full-name p1) (package-full-name p2))) | |
1247 | ||
299112d3 LC |
1248 | ;; Note: Don't i18n field names so that people can post-process it. |
1249 | (format port "name: ~a~%" (package-name p)) | |
1250 | (format port "version: ~a~%" (package-version p)) | |
6bf99bac | 1251 | (format port "outputs: ~a~%" (string-join (package-outputs p))) |
5763ad92 LC |
1252 | (format port "systems: ~a~%" |
1253 | (string-join (package-transitive-supported-systems p))) | |
9c0fc279 CR |
1254 | (format port "dependencies: ~a~%" |
1255 | (match (package-direct-inputs p) | |
5e6feee6 EB |
1256 | (((labels inputs . _) ...) |
1257 | (dependencies->recutils (filter package? inputs))))) | |
299112d3 LC |
1258 | (format port "location: ~a~%" |
1259 | (or (and=> (package-location p) location->string) | |
69daee23 | 1260 | (G_ "unknown"))) |
8fa3e6b3 LC |
1261 | |
1262 | ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in | |
1263 | ;; field identifiers. | |
1264 | (format port "homepage: ~a~%" (package-home-page p)) | |
1265 | ||
299112d3 LC |
1266 | (format port "license: ~a~%" |
1267 | (match (package-license p) | |
1268 | (((? license? licenses) ...) | |
1269 | (string-join (map license-name licenses) | |
1270 | ", ")) | |
1271 | ((? license? license) | |
1272 | (license-name license)) | |
1273 | (x | |
69daee23 | 1274 | (G_ "unknown")))) |
299112d3 LC |
1275 | (format port "synopsis: ~a~%" |
1276 | (string-map (match-lambda | |
4ee79f22 LC |
1277 | (#\newline #\space) |
1278 | (chr chr)) | |
689db38e | 1279 | (or (and=> (package-synopsis-string p) P_) |
299112d3 | 1280 | ""))) |
4ee79f22 | 1281 | (format port "~a~%" |
1cd4027c ML |
1282 | (string->recutils |
1283 | (string-trim-right | |
069d43a7 | 1284 | (parameterize ((%text-width width*)) |
1cd4027c ML |
1285 | (texi->plain-text |
1286 | (string-append "description: " | |
1287 | (or (and=> (package-description p) P_) | |
1288 | "")))) | |
4ee79f22 LC |
1289 | #\newline))) |
1290 | (for-each (match-lambda | |
1291 | ((field . value) | |
1292 | (let ((field (symbol->string field))) | |
1293 | (format port "~a: ~a~%" | |
1294 | field | |
1295 | (fill-paragraph (object->string value) width* | |
1296 | (string-length field)))))) | |
1297 | extra-fields) | |
1298 | (newline port)) | |
299112d3 | 1299 | |
c7ae219e LC |
1300 | (define (relevance obj regexps metrics) |
1301 | "Compute a \"relevance score\" for OBJ as a function of its number of | |
1302 | matches of REGEXPS and accordingly to METRICS. METRICS is list of | |
1303 | field/weight pairs, where FIELD is a procedure that returns a string | |
1304 | describing OBJ, and WEIGHT is a positive integer denoting the weight of this | |
1305 | field in the final score. | |
1306 | ||
1307 | A score of zero means that OBJ does not match any of REGEXPS. The higher the | |
1308 | score, the more relevant OBJ is to REGEXPS." | |
1309 | (define (score str) | |
fd1395c4 LC |
1310 | (let ((counts (map (lambda (regexp) |
1311 | (match (fold-matches regexp str '() cons) | |
1312 | (() 0) | |
1313 | ((m) (if (string=? (match:substring m) str) | |
1314 | 5 ;exact match | |
1315 | 1)) | |
1316 | (lst (length lst)))) | |
1317 | regexps))) | |
c7ae219e LC |
1318 | ;; Compute a score that's proportional to the number of regexps matched |
1319 | ;; and to the number of matches for each regexp. | |
1320 | (* (length counts) (reduce + 0 counts)))) | |
1321 | ||
1322 | (fold (lambda (metric relevance) | |
1323 | (match metric | |
1324 | ((field . weight) | |
1325 | (match (field obj) | |
1326 | (#f relevance) | |
1327 | (str (+ relevance | |
1328 | (* (score str) weight))))))) | |
1329 | 0 | |
1330 | metrics)) | |
1331 | ||
1332 | (define %package-metrics | |
1333 | ;; Metrics used to compute the "relevance score" of a package against a set | |
1334 | ;; of regexps. | |
304a53f6 | 1335 | `((,package-name . 4) |
94aeec0a LC |
1336 | |
1337 | ;; Match regexps on the raw Texinfo since formatting it is quite expensive | |
1338 | ;; and doesn't have much of an effect on search results. | |
1339 | (,(lambda (package) | |
1340 | (and=> (package-synopsis package) P_)) . 3) | |
1341 | (,(lambda (package) | |
1342 | (and=> (package-description package) P_)) . 2) | |
1343 | ||
304a53f6 LC |
1344 | (,(lambda (type) |
1345 | (match (and=> (package-location type) location-file) | |
1346 | ((? string? file) (basename file ".scm")) | |
1347 | (#f ""))) | |
1348 | . 1))) | |
c7ae219e LC |
1349 | |
1350 | (define (package-relevance package regexps) | |
1351 | "Return a score denoting the relevance of PACKAGE for REGEXPS. A score of | |
1352 | zero means that PACKAGE does not match any of REGEXPS." | |
1353 | (relevance package regexps %package-metrics)) | |
1354 | ||
2cd09108 NK |
1355 | (define (string->generations str) |
1356 | "Return the list of generations matching a pattern in STR. This function | |
1357 | accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"." | |
1358 | (define (maybe-integer) | |
1359 | (let ((x (string->number str))) | |
1360 | (and (integer? x) | |
1361 | x))) | |
1362 | ||
1363 | (define (maybe-comma-separated-integers) | |
1364 | (let ((lst (delete-duplicates | |
1365 | (map string->number | |
1366 | (string-split str #\,))))) | |
1367 | (and (every integer? lst) | |
1368 | lst))) | |
1369 | ||
1370 | (cond ((maybe-integer) | |
1371 | => | |
1372 | list) | |
1373 | ((maybe-comma-separated-integers) | |
1374 | => | |
1375 | identity) | |
1376 | ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) | |
1377 | => | |
1378 | (lambda (match) | |
1379 | (let ((s (string->number (match:substring match 1))) | |
1380 | (e (string->number (match:substring match 2)))) | |
1381 | (and (every integer? (list s e)) | |
1382 | (<= s e) | |
1383 | (iota (1+ (- e s)) s))))) | |
1384 | ((string-match "^([0-9]+)\\.\\.$" str) | |
1385 | => | |
1386 | (lambda (match) | |
1387 | (let ((s (string->number (match:substring match 1)))) | |
1388 | (and (integer? s) | |
1389 | `(>= ,s))))) | |
1390 | ((string-match "^\\.\\.([0-9]+)$" str) | |
1391 | => | |
1392 | (lambda (match) | |
1393 | (let ((e (string->number (match:substring match 1)))) | |
1394 | (and (integer? e) | |
1395 | `(<= ,e))))) | |
1396 | (else #f))) | |
1397 | ||
1398 | (define (string->duration str) | |
1399 | "Return the duration matching a pattern in STR. This function accepts the | |
1400 | following patterns: \"1d\", \"1w\", \"1m\"." | |
1401 | (define (hours->duration hours match) | |
1402 | (make-time time-duration 0 | |
1403 | (* 3600 hours (string->number (match:substring match 1))))) | |
1404 | ||
638c5b79 LC |
1405 | (cond ((string-match "^([0-9]+)s$" str) |
1406 | => | |
1407 | (lambda (match) | |
1408 | (make-time time-duration 0 | |
1409 | (string->number (match:substring match 1))))) | |
1410 | ((string-match "^([0-9]+)h$" str) | |
30d2397f | 1411 | => |
638c5b79 LC |
1412 | (lambda (match) |
1413 | (hours->duration 1 match))) | |
1414 | ((string-match "^([0-9]+)d$" str) | |
2cd09108 NK |
1415 | => |
1416 | (lambda (match) | |
1417 | (hours->duration 24 match))) | |
1418 | ((string-match "^([0-9]+)w$" str) | |
1419 | => | |
1420 | (lambda (match) | |
1421 | (hours->duration (* 24 7) match))) | |
1422 | ((string-match "^([0-9]+)m$" str) | |
1423 | => | |
1424 | (lambda (match) | |
1425 | (hours->duration (* 24 30) match))) | |
1426 | (else #f))) | |
1427 | ||
e49de93a LC |
1428 | (define* (matching-generations str profile |
1429 | #:key (duration-relation <=)) | |
1430 | "Return the list of available generations matching a pattern in STR. See | |
1431 | 'string->generations' and 'string->duration' for the list of valid patterns. | |
1432 | When STR is a duration pattern, return all the generations whose ctime has | |
1433 | DURATION-RELATION with the current time." | |
1434 | (define (valid-generations lst) | |
1435 | (define (valid-generation? n) | |
1436 | (any (cut = n <>) (generation-numbers profile))) | |
1437 | ||
1438 | (fold-right (lambda (x acc) | |
1439 | (if (valid-generation? x) | |
1440 | (cons x acc) | |
1441 | acc)) | |
1442 | '() | |
1443 | lst)) | |
1444 | ||
1445 | (define (filter-generations generations) | |
1446 | (match generations | |
1447 | (() '()) | |
1448 | (('>= n) | |
1449 | (drop-while (cut > n <>) | |
1450 | (generation-numbers profile))) | |
1451 | (('<= n) | |
1452 | (valid-generations (iota n 1))) | |
1453 | ((lst ..1) | |
1454 | (valid-generations lst)) | |
30906825 | 1455 | (x #f))) |
e49de93a LC |
1456 | |
1457 | (define (filter-by-duration duration) | |
1458 | (define (time-at-midnight time) | |
1459 | ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and | |
1460 | ;; hours to zeros. | |
1461 | (let ((d (time-utc->date time))) | |
1462 | (date->time-utc | |
1463 | (make-date 0 0 0 0 | |
1464 | (date-day d) (date-month d) | |
1465 | (date-year d) (date-zone-offset d))))) | |
1466 | ||
1467 | (define generation-ctime-alist | |
1468 | (map (lambda (number) | |
1469 | (cons number | |
1470 | (time-second | |
1471 | (time-at-midnight | |
1472 | (generation-time profile number))))) | |
1473 | (generation-numbers profile))) | |
1474 | ||
1475 | (match duration | |
1476 | (#f #f) | |
1477 | (res | |
1478 | (let ((s (time-second | |
1479 | (subtract-duration (time-at-midnight (current-time)) | |
1480 | duration)))) | |
1481 | (delete #f (map (lambda (x) | |
1482 | (and (duration-relation s (cdr x)) | |
1483 | (first x))) | |
1484 | generation-ctime-alist)))))) | |
1485 | ||
1486 | (cond ((string->generations str) | |
1487 | => | |
1488 | filter-generations) | |
1489 | ((string->duration str) | |
1490 | => | |
1491 | filter-by-duration) | |
1492 | (else #f))) | |
1493 | ||
ad18c7e6 LC |
1494 | (define (display-generation profile number) |
1495 | "Display a one-line summary of generation NUMBER of PROFILE." | |
1496 | (unless (zero? number) | |
69daee23 | 1497 | (let ((header (format #f (G_ "Generation ~a\t~a") number |
ad18c7e6 LC |
1498 | (date->string |
1499 | (time-utc->date | |
1500 | (generation-time profile number)) | |
af09bed7 JL |
1501 | ;; TRANSLATORS: This is a format-string for date->string. |
1502 | ;; Please choose a format that corresponds to the | |
1503 | ;; usual way of presenting dates in your locale. | |
1504 | ;; See https://www.gnu.org/software/guile/manual/html_node/SRFI_002d19-Date-to-string.html | |
1505 | ;; for details. | |
1506 | (G_ "~b ~d ~Y ~T")))) | |
ad18c7e6 LC |
1507 | (current (generation-number profile))) |
1508 | (if (= number current) | |
3bd9672c LC |
1509 | ;; TRANSLATORS: The word "current" here is an adjective for |
1510 | ;; "Generation", as in "current generation". Use the appropriate | |
1511 | ;; gender where applicable. | |
69daee23 | 1512 | (format #t (G_ "~a\t(current)~%") header) |
ad18c7e6 LC |
1513 | (format #t "~a~%" header))))) |
1514 | ||
e95ae7c2 | 1515 | (define (display-profile-content-diff profile gen1 gen2) |
dc013aff | 1516 | "Display the changed packages in PROFILE GEN2 compared to generation GEN1." |
e95ae7c2 RJ |
1517 | |
1518 | (define (equal-entry? first second) | |
1519 | (string= (manifest-entry-item first) (manifest-entry-item second))) | |
1520 | ||
1521 | (define (display-entry entry prefix) | |
1522 | (match entry | |
1523 | (($ <manifest-entry> name version output location _) | |
1524 | (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location)))) | |
1525 | ||
1526 | (define (list-entries number) | |
1527 | (manifest-entries (profile-manifest (generation-file-name profile number)))) | |
1528 | ||
1529 | (define (display-diff profile old new) | |
1530 | (display-generation profile new) | |
1531 | (let ((added (lset-difference | |
1532 | equal-entry? (list-entries new) (list-entries old))) | |
1533 | (removed (lset-difference | |
1534 | equal-entry? (list-entries old) (list-entries new)))) | |
1535 | (for-each (cut display-entry <> "+") added) | |
88bdbb2a RJ |
1536 | (for-each (cut display-entry <> "-") removed) |
1537 | (newline))) | |
e95ae7c2 RJ |
1538 | |
1539 | (display-diff profile gen1 gen2)) | |
1540 | ||
ad18c7e6 LC |
1541 | (define (display-profile-content profile number) |
1542 | "Display the packages in PROFILE, generation NUMBER, in a human-readable | |
1543 | way." | |
1544 | (for-each (match-lambda | |
1545 | (($ <manifest-entry> name version output location _) | |
1546 | (format #t " ~a\t~a\t~a\t~a~%" | |
1547 | name version output location))) | |
1548 | ||
1549 | ;; Show most recently installed packages last. | |
1550 | (reverse | |
1551 | (manifest-entries | |
1552 | (profile-manifest (generation-file-name profile number)))))) | |
1553 | ||
06d45f45 | 1554 | (define (display-generation-change previous current) |
69daee23 | 1555 | (format #t (G_ "switched from generation ~a to ~a~%") previous current)) |
06d45f45 LC |
1556 | |
1557 | (define (roll-back* store profile) | |
1558 | "Like 'roll-back', but display what is happening." | |
1559 | (call-with-values | |
1560 | (lambda () | |
1561 | (roll-back store profile)) | |
1562 | display-generation-change)) | |
1563 | ||
1564 | (define (switch-to-generation* profile number) | |
1565 | "Like 'switch-generation', but display what is happening." | |
1566 | (let ((previous (switch-to-generation profile number))) | |
1567 | (display-generation-change previous number))) | |
1568 | ||
1569 | (define (delete-generation* store profile generation) | |
1570 | "Like 'delete-generation', but display what is going on." | |
69daee23 | 1571 | (format #t (G_ "deleting ~a~%") |
06d45f45 LC |
1572 | (generation-file-name profile generation)) |
1573 | (delete-generation store profile generation)) | |
1574 | ||
2876b989 LC |
1575 | (define* (package-specification->name+version+output spec |
1576 | #:optional (output "out")) | |
1577 | "Parse package specification SPEC and return three value: the specified | |
1578 | package name, version number (or #f), and output name (or OUTPUT). SPEC may | |
1579 | optionally contain a version number and an output name, as in these examples: | |
1580 | ||
1581 | guile | |
1b846da8 | 1582 | guile@2.0.9 |
2876b989 | 1583 | guile:debug |
1b846da8 | 1584 | guile@2.0.9:debug |
2876b989 LC |
1585 | " |
1586 | (let*-values (((name sub-drv) | |
1587 | (match (string-rindex spec #\:) | |
1588 | (#f (values spec output)) | |
1589 | (colon (values (substring spec 0 colon) | |
1590 | (substring spec (+ 1 colon)))))) | |
1591 | ((name version) | |
1592 | (package-name->name+version name))) | |
1593 | (values name version sub-drv))) | |
1594 | ||
1595 | \f | |
1596 | ;;; | |
1597 | ;;; Command-line option processing. | |
1598 | ;;; | |
1599 | ||
e49951eb | 1600 | (define (show-guix-usage) |
e49951eb | 1601 | (format (current-error-port) |
69daee23 | 1602 | (G_ "Try `guix --help' for more information.~%")) |
25c93676 | 1603 | (exit 1)) |
e49951eb | 1604 | |
e31ff8b8 LC |
1605 | (define (command-files) |
1606 | "Return the list of source files that define Guix sub-commands." | |
1607 | (define directory | |
1608 | (and=> (search-path %load-path "guix.scm") | |
1609 | (compose (cut string-append <> "/guix/scripts") | |
1610 | dirname))) | |
1611 | ||
2b8cf44f LC |
1612 | (define dot-scm? |
1613 | (cut string-suffix? ".scm" <>)) | |
1614 | ||
e31ff8b8 | 1615 | (if directory |
09d809db | 1616 | (scandir directory dot-scm?) |
e31ff8b8 LC |
1617 | '())) |
1618 | ||
1619 | (define (commands) | |
1620 | "Return the list of Guix command names." | |
1621 | (map (compose (cut string-drop-right <> 4) | |
1622 | basename) | |
1623 | (command-files))) | |
1624 | ||
1625 | (define (show-guix-help) | |
59f734f3 | 1626 | (define (internal? command) |
94d92c77 LC |
1627 | (member command '("substitute" "authenticate" "offload" |
1628 | "perform-download"))) | |
59f734f3 | 1629 | |
69daee23 | 1630 | (format #t (G_ "Usage: guix COMMAND ARGS... |
e31ff8b8 LC |
1631 | Run COMMAND with ARGS.\n")) |
1632 | (newline) | |
69daee23 | 1633 | (format #t (G_ "COMMAND must be one of the sub-commands listed below:\n")) |
e31ff8b8 LC |
1634 | (newline) |
1635 | ;; TODO: Display a synopsis of each command. | |
59f734f3 LC |
1636 | (format #t "~{ ~a~%~}" (sort (remove internal? (commands)) |
1637 | string<?)) | |
e31ff8b8 LC |
1638 | (show-bug-report-information)) |
1639 | ||
a2011be5 LC |
1640 | (define program-name |
1641 | ;; Name of the command-line program currently executing, or #f. | |
1642 | (make-parameter #f)) | |
1643 | ||
ec5d0a85 LC |
1644 | (define (run-guix-command command . args) |
1645 | "Run COMMAND with the given ARGS. Report an error when COMMAND is not | |
1646 | found." | |
1647 | (define module | |
1648 | (catch 'misc-error | |
1649 | (lambda () | |
1650 | (resolve-interface `(guix scripts ,command))) | |
1651 | (lambda - | |
25c93676 | 1652 | (format (current-error-port) |
69daee23 | 1653 | (G_ "guix: ~a: command not found~%") command) |
25c93676 | 1654 | (show-guix-usage)))) |
ec5d0a85 LC |
1655 | |
1656 | (let ((command-main (module-ref module | |
1657 | (symbol-append 'guix- command)))) | |
1658 | (parameterize ((program-name command)) | |
14d5ca2e LC |
1659 | ;; Disable canonicalization so we don't don't stat unreasonably. |
1660 | (with-fluids ((%file-port-name-canonicalization #f)) | |
f4453df9 LC |
1661 | (dynamic-wind |
1662 | (const #f) | |
1663 | (lambda () | |
1664 | (apply command-main args)) | |
1665 | (lambda () | |
1666 | ;; Abuse 'exit-hook' (which is normally meant to be used by the | |
1667 | ;; REPL) to run things like profiling hooks upon completion. | |
1668 | (run-hook exit-hook))))))) | |
ec5d0a85 | 1669 | |
caa6732e AK |
1670 | (define (run-guix . args) |
1671 | "Run the 'guix' command defined by command line ARGS. | |
1672 | Unlike 'guix-main', this procedure assumes that locale, i18n support, | |
1673 | and signal handling has already been set up." | |
1674 | (define option? (cut string-prefix? "-" <>)) | |
1675 | ||
cbee9559 LC |
1676 | ;; The default %LOAD-EXTENSIONS includes the empty string, which doubles the |
1677 | ;; number of 'stat' calls per entry in %LOAD-PATH. Shamelessly remove it. | |
1678 | (set! %load-extensions '(".scm")) | |
1679 | ||
caa6732e AK |
1680 | (match args |
1681 | (() | |
1682 | (format (current-error-port) | |
69daee23 | 1683 | (G_ "guix: missing command name~%")) |
caa6732e AK |
1684 | (show-guix-usage)) |
1685 | ((or ("-h") ("--help")) | |
1686 | (show-guix-help)) | |
85c3fbf5 | 1687 | ((or ("-V") ("--version")) |
caa6732e AK |
1688 | (show-version-and-exit "guix")) |
1689 | (((? option? o) args ...) | |
1690 | (format (current-error-port) | |
69daee23 | 1691 | (G_ "guix: unrecognized option '~a'~%") o) |
caa6732e | 1692 | (show-guix-usage)) |
2ab5fdc4 LC |
1693 | (("help" command) |
1694 | (apply run-guix-command (string->symbol command) | |
1695 | '("--help"))) | |
caa6732e AK |
1696 | (("help" args ...) |
1697 | (show-guix-help)) | |
1698 | ((command args ...) | |
1699 | (apply run-guix-command | |
1700 | (string->symbol command) | |
1701 | args)))) | |
1702 | ||
a2011be5 LC |
1703 | (define guix-warning-port |
1704 | (make-parameter (current-warning-port))) | |
1705 | ||
e49951eb MW |
1706 | (define (guix-main arg0 . args) |
1707 | (initialize-guix) | |
caa6732e | 1708 | (apply run-guix args)) |
e49951eb | 1709 | |
073c34d7 | 1710 | ;;; ui.scm ends here |