1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
5 ;;; Copyright © 2015 Alex Kost <alezost@gmail.com>
6 ;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
7 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
8 ;;; Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
9 ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
10 ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
12 ;;; This file is part of GNU Guix.
14 ;;; GNU Guix is free software; you can redistribute it and/or modify it
15 ;;; under the terms of the GNU General Public License as published by
16 ;;; the Free Software Foundation; either version 3 of the License, or (at
17 ;;; your option) any later version.
19 ;;; GNU Guix is distributed in the hope that it will be useful, but
20 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;;; GNU General Public License for more details.
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
27 (define-module (guix scripts refresh)
28 #:use-module (guix ui)
29 #:use-module (gcrypt hash)
30 #:use-module (guix scripts)
31 #:use-module ((guix scripts build) #:select (%standard-build-options))
32 #:use-module (guix store)
33 #:use-module (guix utils)
34 #:use-module (guix packages)
35 #:use-module (guix profiles)
36 #:use-module (guix upstream)
37 #:use-module (guix graph)
38 #:use-module (guix scripts graph)
39 #:use-module (guix monads)
40 #:use-module (guix gnupg)
41 #:use-module (gnu packages)
42 #:use-module ((gnu packages commencement) #:select (%final-inputs))
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 regex)
45 #:use-module (ice-9 vlist)
46 #:use-module (ice-9 format)
47 #:use-module (srfi srfi-1)
48 #:use-module (srfi srfi-11)
49 #:use-module (srfi srfi-26)
50 #:use-module (srfi srfi-37)
51 #:use-module (ice-9 binary-ports)
52 #:export (guix-refresh))
56 ;;; Command-line options.
59 (define %default-options
60 ;; Alist of default option values.
64 ;; Specification of the command-line options.
65 (list (option '(#\u "update") #f #f
66 (lambda (opt name arg result)
67 (alist-cons 'update? #t result)))
68 (option '(#\s "select") #t #f
69 (lambda (opt name arg result)
71 ((or "core" "non-core")
72 (alist-cons 'select (string->symbol arg)
75 (leave (G_ "~a: invalid selection; expected `core' or `non-core'~%")
77 (option '(#\t "type") #t #f
78 (lambda (opt name arg result)
79 (let* ((not-comma (char-set-complement (char-set #\,)))
80 (names (map string->symbol
81 (string-tokenize arg not-comma))))
82 (alist-cons 'updaters names result))))
83 (option '(#\L "list-updaters") #f #f
85 (list-updaters-and-exit)))
86 (option '(#\m "manifest") #t #f
87 (lambda (opt name arg result)
88 (alist-cons 'manifest arg result)))
89 (option '(#\e "expression") #t #f
90 (lambda (opt name arg result)
91 (alist-cons 'expression arg result)))
92 (option '(#\l "list-dependent") #f #f
93 (lambda (opt name arg result)
94 (alist-cons 'list-dependent? #t result)))
95 (option '(#\r "recursive") #f #f
96 (lambda (opt name arg result)
97 (alist-cons 'recursive? #t result)))
98 (option '("list-transitive") #f #f
99 (lambda (opt name arg result)
100 (alist-cons 'list-transitive? #t result)))
102 (option '("keyring") #t #f
103 (lambda (opt name arg result)
104 (alist-cons 'keyring arg result)))
105 (option '("key-server") #t #f
106 (lambda (opt name arg result)
107 (alist-cons 'key-server arg result)))
108 (option '("gpg") #t #f
109 (lambda (opt name arg result)
110 (alist-cons 'gpg-command arg result)))
111 (option '("key-download") #t #f
112 (lambda (opt name arg result)
114 ((or "interactive" "always" "never")
115 (alist-cons 'key-download (string->symbol arg)
118 (leave (G_ "unsupported policy: ~a~%")
121 ;; The short option -L is already used by --list-updaters, therefore
122 ;; it needs to be removed from %standard-build-options.
123 (let ((%load-path-option (find (lambda (option)
125 (option-names option)))
126 %standard-build-options)))
128 (filter (lambda (name) (not (equal? #\L name)))
129 (option-names %load-path-option))
130 (option-required-arg? %load-path-option)
131 (option-optional-arg? %load-path-option)
132 (option-processor %load-path-option)))
134 (option '(#\h "help") #f #f
138 (option '(#\V "version") #f #f
140 (show-version-and-exit "guix refresh")))))
143 (display (G_ "Usage: guix refresh [OPTION]... [PACKAGE]...
144 Update package definitions to match the latest upstream version.
146 When PACKAGE... is given, update only the specified packages. Otherwise
147 update all the packages of the distribution, or the subset thereof
148 specified with `--select'.\n"))
150 -e, --expression=EXPR consider the package EXPR evaluates to"))
152 -u, --update update source files in place"))
154 -s, --select=SUBSET select all the packages in SUBSET, one of
155 `core' or `non-core'"))
157 -m, --manifest=FILE select all the packages from the manifest in FILE"))
159 -t, --type=UPDATER,... restrict to updates from the specified updaters
162 -L, --list-updaters list available updaters and exit"))
164 -l, --list-dependent list top-level dependent packages that would need to
165 be rebuilt as a result of upgrading PACKAGE..."))
167 -r, --recursive check the PACKAGE and its inputs for upgrades"))
169 --list-transitive list all the packages that PACKAGE depends on"))
172 --keyring=FILE use FILE as the keyring of upstream OpenPGP keys"))
174 --key-server=HOST use HOST as the OpenPGP key server"))
176 --gpg=COMMAND use COMMAND as the GnuPG 2.x command"))
178 --key-download=POLICY
179 handle missing OpenPGP keys according to POLICY:
180 'always', 'never', and 'interactive', which is also
181 used when 'key-download' is not specified"))
184 --load-path=DIR prepend DIR to the package module search path"))
187 -h, --help display this help and exit"))
189 -V, --version display version information and exit"))
191 (show-bug-report-information))
193 (define (options->packages opts)
194 "Return the list of packages requested by OPTS, honoring options like
196 (define core-package?
197 (let* ((input->package (match-lambda
198 ((name (? package? package) _ ...) package)
200 (final-inputs (map input->package %final-inputs))
201 (core (append final-inputs
202 (append-map (compose (cut filter-map input->package <>)
203 package-transitive-inputs)
205 (names (delete-duplicates (map package-name core))))
207 "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
208 update would trigger a complete rebuild."
209 ;; Compare by name because packages in base.scm basically inherit
210 ;; other packages. So, even if those packages are not core packages
211 ;; themselves, updating them would also update those who inherit from
213 ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
214 (member (package-name package) names))))
216 (define (keep-newest package lst)
217 ;; If a newer version of PACKAGE is already in LST, return LST; otherwise
218 ;; return LST minus the other version of PACKAGE in it, plus PACKAGE.
219 (let ((name (package-name package)))
220 (match (find (lambda (p)
221 (string=? (package-name p) name))
224 (if (version>? (package-version other) (package-version package))
226 (cons package (delq other lst))))
228 (cons package lst)))))
230 (define args-packages
231 ;; Packages explicitly passed as command-line arguments.
232 (match (filter-map (match-lambda
234 ;; Take either the specified version or the
236 (specification->package spec))
238 (read/eval-package-expression exp))
241 (() ;default to all packages
242 (let ((select? (match (assoc-ref opts 'select)
243 ('core core-package?)
244 ('non-core (negate core-package?))
246 (fold-packages (lambda (package result)
247 (if (select? package)
248 (keep-newest package result)
251 (some ;user-specified packages
255 (match (assoc-ref opts 'manifest)
257 ((? string? file) (packages-from-manifest file))))
259 (if (assoc-ref opts 'recursive?)
260 (mlet %store-monad ((edges (node-edges %bag-node-type
262 (return (node-transitive-edges packages edges)))
263 (with-monad %store-monad
271 (define (lookup-updater-by-name name)
272 "Return the updater called NAME."
273 (or (find (lambda (updater)
274 (eq? name (upstream-updater-name updater)))
276 (leave (G_ "~a: no such updater~%") name)))
278 (define (list-updaters-and-exit)
279 "Display available updaters and exit."
280 (format #t (G_ "Available updaters:~%"))
283 (let* ((packages (fold-packages cons '()))
284 (total (length packages)))
286 (fold (lambda (updater uncovered)
287 (let ((matches (filter (upstream-updater-predicate updater)
289 ;; TRANSLATORS: The parenthetical expression here is rendered
290 ;; like "(42% coverage)" and denotes the fraction of packages
291 ;; covered by the given updater.
292 (format #t (G_ " - ~a: ~a (~2,1f% coverage)~%")
293 (upstream-updater-name updater)
294 (G_ (upstream-updater-description updater))
295 (* 100. (/ (length matches) total)))
296 (lset-difference eq? uncovered matches)))
301 (format #t (G_ "~2,1f% of the packages are covered by these updaters.~%")
302 (* 100. (/ (- total (length uncovered)) total))))
305 (define (warn-no-updater package)
306 (warning (package-location package)
307 (G_ "no updater for ~a~%")
308 (package-name package)))
310 (define* (update-package store package updaters
311 #:key (key-download 'interactive) warn?)
312 "Update the source file that defines PACKAGE with the new version.
313 KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
314 values: 'interactive' (default), 'always', and 'never'. When WARN? is true,
315 warn about packages that have no matching updater."
316 (if (lookup-updater package updaters)
317 (let-values (((version tarball source)
318 (package-update store package updaters
319 #:key-download key-download))
321 (or (package-field-location package 'version)
322 (package-location package))))
324 (if (and=> tarball file-exists?)
327 (G_ "~a: updating from version ~a to version ~a...~%")
328 (package-name package)
329 (package-version package) version)
332 (format (current-error-port)
333 (match (list (upstream-input-change-action change)
334 (upstream-input-change-type change))
336 (G_ "~a: consider adding this input: ~a~%"))
338 (G_ "~a: consider adding this native input: ~a~%"))
340 (G_ "~a: consider adding this propagated input: ~a~%"))
342 (G_ "~a: consider removing this input: ~a~%"))
344 (G_ "~a: consider removing this native input: ~a~%"))
345 (('remove 'propagated)
346 (G_ "~a: consider removing this propagated input: ~a~%")))
347 (package-name package)
348 (upstream-input-change-name change)))
349 (upstream-source-input-changes source))
350 (let ((hash (call-with-input-file tarball
352 (update-package-source package source hash)))
353 (warning (G_ "~a: version ~a could not be \
354 downloaded and authenticated; not updating~%")
355 (package-name package) version))))
357 (warn-no-updater package))))
359 (define* (check-for-package-update package updaters #:key warn?)
360 "Check whether an update is available for PACKAGE and print a message. When
361 WARN? is true and no updater exists for PACKAGE, print a warning."
362 (match (package-latest-release package updaters)
363 ((? upstream-source? source)
364 (let ((loc (or (package-field-location package 'version)
365 (package-location package))))
366 (case (version-compare (upstream-source-version source)
367 (package-version package))
370 (G_ "~a would be upgraded from ~a to ~a~%")
371 (package-name package) (package-version package)
372 (upstream-source-version source)))
376 (G_ "~a is already the latest version of ~a~%")
377 (package-version package)
378 (package-name package))))
382 (G_ "~a is greater than \
383 the latest known version of ~a (~a)~%")
384 (package-version package)
385 (package-name package)
386 (upstream-source-version source)))))))
389 ;; Distinguish between "no updater" and "failing updater."
390 (match (lookup-updater package updaters)
391 ((? upstream-updater? updater)
392 (warning (package-location package)
393 (G_ "'~a' updater failed to determine available \
395 (upstream-updater-name updater)
396 (package-name package)))
398 (warn-no-updater package)))))))
405 (define (all-packages)
406 "Return the list of all the distro's packages."
407 (fold-packages (lambda (package result)
408 ;; Ignore deprecated packages.
409 (if (package-superseded package)
411 (cons package result)))
413 #:select? (const #t))) ;include hidden packages
415 (define (list-dependents packages)
416 "List all the things that would need to be rebuilt if PACKAGES are changed."
417 ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
418 ;; because it includes implicit dependencies.
419 (define (full-name package)
420 (string-append (package-name package) "@"
421 (package-version package)))
423 (mlet %store-monad ((edges (node-back-edges %bag-node-type
424 (package-closure (all-packages)))))
425 (let* ((dependents (node-transitive-edges packages edges))
426 (covering (filter (lambda (node)
427 (null? (edges node)))
431 (format (current-output-port)
432 (N_ "No dependents other than itself: ~{~a~}~%"
433 "No dependents other than themselves: ~{~a~^ ~}~%"
435 (map full-name packages)))
438 (format (current-output-port)
439 (G_ "A single dependent package: ~a~%")
442 (format (current-output-port)
443 (N_ "Building the following ~*package would ensure ~d \
444 dependent packages are rebuilt: ~{~a~^ ~}~%"
445 "Building the following ~d packages would ensure ~d \
446 dependent packages are rebuilt: ~{~a~^ ~}~%"
448 (length covering) (length dependents)
449 (map full-name covering))))
452 (define (list-transitive packages)
453 "List all the packages that would cause PACKAGES to be rebuilt if they are changed."
454 ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
455 ;; because it includes implicit dependencies.
456 (define (full-name package)
457 (string-append (package-name package) "@"
458 (package-version package)))
460 (mlet %store-monad ((edges (node-edges %bag-node-type
461 ;; Here we don't want the -boot0 packages.
462 (fold-packages cons '()))))
463 (let ((dependent (node-transitive-edges packages edges)))
466 (format (current-output-port)
467 (G_ "~a depends on the following ~d packages: ~{~a~^ ~}~%.")
468 (full-name x) (length dependent) (map full-name dependent)))
470 (format (current-output-port)
471 (G_ "The following ~d packages \
472 all are dependent packages: ~{~a~^ ~}~%")
473 (length dependent) (map full-name dependent))))
481 (define (manifest->packages manifest)
482 "Return the list of packages in MANIFEST."
483 (filter-map (lambda (entry)
484 (let ((item (manifest-entry-item entry)))
485 (if (package? item) item #f)))
486 (manifest-entries manifest)))
488 (define (packages-from-manifest manifest)
489 "Return the list of packages in loaded MANIFEST."
490 (let* ((user-module (make-user-module '((guix profiles) (gnu))))
491 (manifest (load* manifest user-module)))
492 (manifest->packages manifest)))
499 (define (guix-refresh . args)
500 (define (parse-options)
501 ;; Return the alist of option values.
502 (parse-command-line args %options (list %default-options)
503 #:build-options? #f))
505 (define (options->updaters opts)
506 ;; Return the list of updaters to use.
507 (match (filter-map (match-lambda
509 (map lookup-updater-by-name names))
513 ;; Use the default updaters.
516 (concatenate lists))))
518 (let* ((opts (parse-options))
519 (update? (assoc-ref opts 'update?))
520 (updaters (options->updaters opts))
521 (recursive? (assoc-ref opts 'recursive?))
522 (list-dependent? (assoc-ref opts 'list-dependent?))
523 (list-transitive? (assoc-ref opts 'list-transitive?))
524 (key-download (assoc-ref opts 'key-download))
526 ;; Warn about missing updaters when a package is explicitly given on
528 (warn? (and (or (assoc-ref opts 'argument)
529 (assoc-ref opts 'expression)
530 (assoc-ref opts 'manifest))
534 (run-with-store store
535 (mlet %store-monad ((packages (options->packages opts)))
538 (list-dependents packages))
540 (list-transitive packages))
542 (parameterize ((%openpgp-key-server
543 (or (assoc-ref opts 'key-server)
544 (%openpgp-key-server)))
546 (or (assoc-ref opts 'gpg-command)
549 (or (assoc-ref opts 'keyring)
550 (string-append (config-directory)
551 "/upstream/trustedkeys.kbx"))))
553 (cut update-package store <> updaters
554 #:key-download key-download
559 (for-each (cut check-for-package-update <> updaters