refresh.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2021 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>
  11. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  12. ;;;
  13. ;;; This file is part of GNU Guix.
  14. ;;;
  15. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  16. ;;; under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 3 of the License, or (at
  18. ;;; your option) any later version.
  19. ;;;
  20. ;;; GNU Guix is distributed in the hope that it will be useful, but
  21. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  27. (define-module (guix scripts refresh)
  28. #:use-module (guix ui)
  29. #:use-module (guix scripts)
  30. #:use-module ((guix scripts build) #:select (%standard-build-options))
  31. #:use-module (guix store)
  32. #:use-module (guix utils)
  33. #:use-module (guix packages)
  34. #:use-module (guix profiles)
  35. #:use-module (guix upstream)
  36. #:use-module (guix graph)
  37. #:use-module (guix scripts graph)
  38. #:use-module (guix monads)
  39. #:use-module (guix gnupg)
  40. #:use-module (guix hash)
  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))
  53. ;;;
  54. ;;; Command-line options.
  55. ;;;
  56. (define %default-options
  57. ;; Alist of default option values.
  58. '())
  59. (define %options
  60. ;; Specification of the command-line options.
  61. (list (option '(#\u "update") #f #f
  62. (lambda (opt name arg result)
  63. (alist-cons 'update? #t result)))
  64. (option '(#\s "select") #t #f
  65. (lambda (opt name arg result)
  66. (match arg
  67. ((or "core" "non-core")
  68. (alist-cons 'select (string->symbol arg)
  69. result))
  70. (x
  71. (leave (G_ "~a: invalid selection; expected `core' or `non-core'~%")
  72. arg)))))
  73. (option '(#\t "type") #t #f
  74. (lambda (opt name arg result)
  75. (let* ((not-comma (char-set-complement (char-set #\,)))
  76. (names (map string->symbol
  77. (string-tokenize arg not-comma))))
  78. (alist-cons 'updaters names result))))
  79. (option '(#\L "list-updaters") #f #f
  80. (lambda args
  81. (list-updaters-and-exit)))
  82. (option '(#\m "manifest") #t #f
  83. (lambda (opt name arg result)
  84. (alist-cons 'manifest arg result)))
  85. (option '(#\e "expression") #t #f
  86. (lambda (opt name arg result)
  87. (alist-cons 'expression arg result)))
  88. (option '(#\l "list-dependent") #f #f
  89. (lambda (opt name arg result)
  90. (alist-cons 'list-dependent? #t result)))
  91. (option '(#\r "recursive") #f #f
  92. (lambda (opt name arg result)
  93. (alist-cons 'recursive? #t result)))
  94. (option '("list-transitive") #f #f
  95. (lambda (opt name arg result)
  96. (alist-cons 'list-transitive? #t result)))
  97. (option '("keyring") #t #f
  98. (lambda (opt name arg result)
  99. (alist-cons 'keyring arg result)))
  100. (option '("key-server") #t #f
  101. (lambda (opt name arg result)
  102. (alist-cons 'key-server arg result)))
  103. (option '("gpg") #t #f
  104. (lambda (opt name arg result)
  105. (alist-cons 'gpg-command arg result)))
  106. (option '("key-download") #t #f
  107. (lambda (opt name arg result)
  108. (match arg
  109. ((or "interactive" "always" "never")
  110. (alist-cons 'key-download (string->symbol arg)
  111. result))
  112. (x
  113. (leave (G_ "unsupported policy: ~a~%")
  114. arg)))))
  115. ;; The short option -L is already used by --list-updaters, therefore
  116. ;; it needs to be removed from %standard-build-options.
  117. (let ((load-path-option (find (lambda (option)
  118. (member "load-path"
  119. (option-names option)))
  120. %standard-build-options)))
  121. (option
  122. (filter (lambda (name) (not (equal? #\L name)))
  123. (option-names load-path-option))
  124. (option-required-arg? load-path-option)
  125. (option-optional-arg? load-path-option)
  126. (option-processor load-path-option)))
  127. (option '(#\h "help") #f #f
  128. (lambda args
  129. (show-help)
  130. (exit 0)))
  131. (option '(#\V "version") #f #f
  132. (lambda args
  133. (show-version-and-exit "guix refresh")))))
  134. (define (show-help)
  135. (display (G_ "Usage: guix refresh [OPTION]... [PACKAGE]...
  136. Update package definitions to match the latest upstream version.
  137. When PACKAGE... is given, update only the specified packages. Otherwise
  138. update all the packages of the distribution, or the subset thereof
  139. specified with `--select'.\n"))
  140. (display (G_ "
  141. -e, --expression=EXPR consider the package EXPR evaluates to"))
  142. (display (G_ "
  143. -u, --update update source files in place"))
  144. (display (G_ "
  145. -s, --select=SUBSET select all the packages in SUBSET, one of
  146. `core' or `non-core'"))
  147. (display (G_ "
  148. -m, --manifest=FILE select all the packages from the manifest in FILE"))
  149. (display (G_ "
  150. -t, --type=UPDATER,... restrict to updates from the specified updaters
  151. (e.g., 'gnu')"))
  152. (display (G_ "
  153. -L, --list-updaters list available updaters and exit"))
  154. (display (G_ "
  155. -l, --list-dependent list top-level dependent packages that would need to
  156. be rebuilt as a result of upgrading PACKAGE..."))
  157. (display (G_ "
  158. -r, --recursive check the PACKAGE and its inputs for upgrades"))
  159. (display (G_ "
  160. --list-transitive list all the packages that PACKAGE depends on"))
  161. (newline)
  162. (display (G_ "
  163. --keyring=FILE use FILE as the keyring of upstream OpenPGP keys"))
  164. (display (G_ "
  165. --key-server=HOST use HOST as the OpenPGP key server"))
  166. (display (G_ "
  167. --gpg=COMMAND use COMMAND as the GnuPG 2.x command"))
  168. (display (G_ "
  169. --key-download=POLICY
  170. handle missing OpenPGP keys according to POLICY:
  171. 'always', 'never', and 'interactive', which is also
  172. used when 'key-download' is not specified"))
  173. (newline)
  174. (display (G_ "
  175. --load-path=DIR prepend DIR to the package module search path"))
  176. (newline)
  177. (display (G_ "
  178. -h, --help display this help and exit"))
  179. (display (G_ "
  180. -V, --version display version information and exit"))
  181. (newline)
  182. (show-bug-report-information))
  183. (define (options->packages opts)
  184. "Return the list of packages requested by OPTS, honoring options like
  185. '--recursive'."
  186. (define core-package?
  187. (let* ((input->package (match-lambda
  188. ((name (? package? package) _ ...) package)
  189. (_ #f)))
  190. (final-inputs (map input->package %final-inputs))
  191. (core (append final-inputs
  192. (append-map (compose (cut filter-map input->package <>)
  193. package-transitive-inputs)
  194. final-inputs)))
  195. (names (delete-duplicates (map package-name core))))
  196. (lambda (package)
  197. "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
  198. update would trigger a complete rebuild."
  199. ;; Compare by name because packages in base.scm basically inherit
  200. ;; other packages. So, even if those packages are not core packages
  201. ;; themselves, updating them would also update those who inherit from
  202. ;; them.
  203. ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
  204. (member (package-name package) names))))
  205. (define (keep-newest package lst)
  206. ;; If a newer version of PACKAGE is already in LST, return LST; otherwise
  207. ;; return LST minus the other version of PACKAGE in it, plus PACKAGE.
  208. (let ((name (package-name package)))
  209. (match (find (lambda (p)
  210. (string=? (package-name p) name))
  211. lst)
  212. ((? package? other)
  213. (if (version>? (package-version other) (package-version package))
  214. lst
  215. (cons package (delq other lst))))
  216. (_
  217. (cons package lst)))))
  218. (define args-packages
  219. ;; Packages explicitly passed as command-line arguments.
  220. (match (filter-map (match-lambda
  221. (('argument . spec)
  222. ;; Take either the specified version or the
  223. ;; latest one.
  224. (specification->package spec))
  225. (('expression . exp)
  226. (read/eval-package-expression exp))
  227. (_ #f))
  228. opts)
  229. (() ;default to all packages
  230. (let ((select? (match (assoc-ref opts 'select)
  231. ('core core-package?)
  232. ('non-core (negate core-package?))
  233. (_ (const #t)))))
  234. (fold-packages (lambda (package result)
  235. (if (select? package)
  236. (keep-newest package result)
  237. result))
  238. '())))
  239. (some ;user-specified packages
  240. some)))
  241. (define packages
  242. (match (assoc-ref opts 'manifest)
  243. (#f args-packages)
  244. ((? string? file) (packages-from-manifest file))))
  245. (if (assoc-ref opts 'recursive?)
  246. (mlet %store-monad ((edges (node-edges %bag-node-type
  247. (all-packages))))
  248. (return (node-transitive-edges packages edges)))
  249. (with-monad %store-monad
  250. (return packages))))
  251. ;;;
  252. ;;; Updates.
  253. ;;;
  254. (define (lookup-updater-by-name name)
  255. "Return the updater called NAME."
  256. (or (find (lambda (updater)
  257. (eq? name (upstream-updater-name updater)))
  258. (force %updaters))
  259. (leave (G_ "~a: no such updater~%") name)))
  260. (define (list-updaters-and-exit)
  261. "Display available updaters and exit."
  262. (format #t (G_ "Available updaters:~%"))
  263. (newline)
  264. (let* ((packages (fold-packages cons '()))
  265. (total (length packages)))
  266. (define uncovered
  267. (fold (lambda (updater uncovered)
  268. (let ((matches (filter (upstream-updater-predicate updater)
  269. packages)))
  270. ;; TRANSLATORS: The parenthetical expression here is rendered
  271. ;; like "(42% coverage)" and denotes the fraction of packages
  272. ;; covered by the given updater.
  273. (format #t (G_ " - ~a: ~a (~2,1f% coverage)~%")
  274. (upstream-updater-name updater)
  275. (G_ (upstream-updater-description updater))
  276. (* 100. (/ (length matches) total)))
  277. (lset-difference eq? uncovered matches)))
  278. packages
  279. (force %updaters)))
  280. (newline)
  281. (format #t (G_ "~2,1f% of the packages are covered by these updaters.~%")
  282. (* 100. (/ (- total (length uncovered)) total))))
  283. (exit 0))
  284. (define (warn-no-updater package)
  285. (warning (package-location package)
  286. (G_ "no updater for ~a~%")
  287. (package-name package)))
  288. (define* (update-package store package updaters
  289. #:key (key-download 'interactive) warn?)
  290. "Update the source file that defines PACKAGE with the new version.
  291. KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
  292. values: 'interactive' (default), 'always', and 'never'. When WARN? is true,
  293. warn about packages that have no matching updater."
  294. (if (lookup-updater package updaters)
  295. (let-values (((version output source)
  296. (package-update store package updaters
  297. #:key-download key-download))
  298. ((loc)
  299. (or (package-field-location package 'version)
  300. (package-location package))))
  301. (when version
  302. (if (and=> output file-exists?)
  303. (begin
  304. (info loc
  305. (G_ "~a: updating from version ~a to version ~a...~%")
  306. (package-name package)
  307. (package-version package) version)
  308. (for-each
  309. (lambda (change)
  310. (define field
  311. (match (upstream-input-change-type change)
  312. ('native 'native-inputs)
  313. ('propagated 'propagated-inputs)
  314. (_ 'inputs)))
  315. (define name
  316. (package-name package))
  317. (define loc
  318. (package-field-location package field))
  319. (define change-name
  320. (upstream-input-change-name change))
  321. (match (list (upstream-input-change-action change)
  322. (upstream-input-change-type change))
  323. (('add 'regular)
  324. (info loc (G_ "~a: consider adding this input: ~a~%")
  325. name change-name))
  326. (('add 'native)
  327. (info loc (G_ "~a: consider adding this native input: ~a~%")
  328. name change-name))
  329. (('add 'propagated)
  330. (info loc (G_ "~a: consider adding this propagated input: ~a~%")
  331. name change-name))
  332. (('remove 'regular)
  333. (info loc (G_ "~a: consider removing this input: ~a~%")
  334. name change-name))
  335. (('remove 'native)
  336. (info loc (G_ "~a: consider removing this native input: ~a~%")
  337. name change-name))
  338. (('remove 'propagated)
  339. (info loc (G_ "~a: consider removing this propagated input: ~a~%")
  340. name change-name))))
  341. (upstream-source-input-changes source))
  342. (let ((hash (file-hash* output)))
  343. (update-package-source package source hash)))
  344. (warning (G_ "~a: version ~a could not be \
  345. downloaded and authenticated; not updating~%")
  346. (package-name package) version))))
  347. (when warn?
  348. (warn-no-updater package))))
  349. (define* (check-for-package-update package updaters #:key warn?)
  350. "Check whether an update is available for PACKAGE and print a message. When
  351. WARN? is true and no updater exists for PACKAGE, print a warning."
  352. (match (package-latest-release package updaters)
  353. ((? upstream-source? source)
  354. (let ((loc (or (package-field-location package 'version)
  355. (package-location package))))
  356. (case (version-compare (upstream-source-version source)
  357. (package-version package))
  358. ((>)
  359. (info loc
  360. (G_ "~a would be upgraded from ~a to ~a~%")
  361. (package-name package) (package-version package)
  362. (upstream-source-version source)))
  363. ((=)
  364. (when warn?
  365. (info loc
  366. (G_ "~a is already the latest version of ~a~%")
  367. (package-version package)
  368. (package-name package))))
  369. (else
  370. (when warn?
  371. (warning loc
  372. (G_ "~a is greater than \
  373. the latest known version of ~a (~a)~%")
  374. (package-version package)
  375. (package-name package)
  376. (upstream-source-version source)))))))
  377. (#f
  378. (when warn?
  379. ;; Distinguish between "no updater" and "failing updater."
  380. (match (lookup-updater package updaters)
  381. ((? upstream-updater? updater)
  382. (warning (package-location package)
  383. (G_ "'~a' updater failed to determine available \
  384. releases for ~a~%")
  385. (upstream-updater-name updater)
  386. (package-name package)))
  387. (#f
  388. (warn-no-updater package)))))))
  389. ;;;
  390. ;;; Dependents.
  391. ;;;
  392. (define (all-packages)
  393. "Return the list of all the distro's packages."
  394. (fold-packages (lambda (package result)
  395. ;; Ignore deprecated packages.
  396. (if (package-superseded package)
  397. result
  398. (cons package result)))
  399. '()
  400. #:select? (const #t))) ;include hidden packages
  401. (define (list-dependents packages)
  402. "List all the things that would need to be rebuilt if PACKAGES are changed."
  403. ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
  404. ;; because it includes implicit dependencies.
  405. (define (full-name package)
  406. (string-append (package-name package) "@"
  407. (package-version package)))
  408. (mlet %store-monad ((edges (node-back-edges %bag-node-type
  409. (package-closure (all-packages)))))
  410. (let* ((dependents (node-transitive-edges packages edges))
  411. (covering (filter (lambda (node)
  412. (null? (edges node)))
  413. dependents)))
  414. (match dependents
  415. (()
  416. (format (current-output-port)
  417. (N_ "No dependents other than itself: ~{~a~}~%"
  418. "No dependents other than themselves: ~{~a~^ ~}~%"
  419. (length packages))
  420. (map full-name packages)))
  421. ((x)
  422. (format (current-output-port)
  423. (G_ "A single dependent package: ~a~%")
  424. (full-name x)))
  425. (lst
  426. (format (current-output-port)
  427. (N_ "Building the following ~d package would ensure ~d \
  428. dependent packages are rebuilt: ~{~a~^ ~}~%"
  429. "Building the following ~d packages would ensure ~d \
  430. dependent packages are rebuilt: ~{~a~^ ~}~%"
  431. (length covering))
  432. (length covering) (length dependents)
  433. (map full-name covering))))
  434. (return #t))))
  435. (define (list-transitive packages)
  436. "List all the packages that would cause PACKAGES to be rebuilt if they are changed."
  437. ;; Using %BAG-NODE-TYPE is more accurate than using %PACKAGE-NODE-TYPE
  438. ;; because it includes implicit dependencies.
  439. (define (full-name package)
  440. (string-append (package-name package) "@"
  441. (package-version package)))
  442. (mlet %store-monad ((edges (node-edges %bag-node-type
  443. ;; Here we don't want the -boot0 packages.
  444. (fold-packages cons '()))))
  445. (let ((dependent (node-transitive-edges packages edges)))
  446. (match packages
  447. ((x)
  448. (format (current-output-port)
  449. (G_ "~a depends on the following ~d packages: ~{~a~^ ~}~%.")
  450. (full-name x) (length dependent) (map full-name dependent)))
  451. (lst
  452. (format (current-output-port)
  453. (G_ "The following ~d packages \
  454. all are dependent packages: ~{~a~^ ~}~%")
  455. (length dependent) (map full-name dependent))))
  456. (return #t))))
  457. ;;;
  458. ;;; Manifest.
  459. ;;;
  460. (define (manifest->packages manifest)
  461. "Return the list of packages in MANIFEST."
  462. (filter-map (lambda (entry)
  463. (let ((item (manifest-entry-item entry)))
  464. (if (package? item) item #f)))
  465. (manifest-entries manifest)))
  466. (define (packages-from-manifest manifest)
  467. "Return the list of packages in loaded MANIFEST."
  468. (let* ((user-module (make-user-module '((guix profiles) (gnu))))
  469. (manifest (load* manifest user-module)))
  470. (manifest->packages manifest)))
  471. ;;;
  472. ;;; Entry point.
  473. ;;;
  474. (define-command (guix-refresh . args)
  475. (category packaging)
  476. (synopsis "update existing package definitions")
  477. (define (parse-options)
  478. ;; Return the alist of option values.
  479. (parse-command-line args %options (list %default-options)
  480. #:build-options? #f))
  481. (define (options->updaters opts)
  482. ;; Return the list of updaters to use.
  483. (match (filter-map (match-lambda
  484. (('updaters . names)
  485. (map lookup-updater-by-name names))
  486. (_ #f))
  487. opts)
  488. (()
  489. ;; Use the default updaters.
  490. (force %updaters))
  491. (lists
  492. (concatenate lists))))
  493. (let* ((opts (parse-options))
  494. (update? (assoc-ref opts 'update?))
  495. (updaters (options->updaters opts))
  496. (recursive? (assoc-ref opts 'recursive?))
  497. (list-dependent? (assoc-ref opts 'list-dependent?))
  498. (list-transitive? (assoc-ref opts 'list-transitive?))
  499. (key-download (assoc-ref opts 'key-download))
  500. ;; Warn about missing updaters when a package is explicitly given on
  501. ;; the command line.
  502. (warn? (and (or (assoc-ref opts 'argument)
  503. (assoc-ref opts 'expression)
  504. (assoc-ref opts 'manifest))
  505. (not recursive?))))
  506. (with-error-handling
  507. (with-store store
  508. (run-with-store store
  509. (mlet %store-monad ((packages (options->packages opts)))
  510. (cond
  511. (list-dependent?
  512. (list-dependents packages))
  513. (list-transitive?
  514. (list-transitive packages))
  515. (update?
  516. (parameterize ((%openpgp-key-server
  517. (or (assoc-ref opts 'key-server)
  518. (%openpgp-key-server)))
  519. (%gpg-command
  520. (or (assoc-ref opts 'gpg-command)
  521. (%gpg-command)))
  522. (current-keyring
  523. (or (assoc-ref opts 'keyring)
  524. (string-append (config-directory)
  525. "/upstream/trustedkeys.kbx"))))
  526. (for-each
  527. (cut update-package store <> updaters
  528. #:key-download key-download
  529. #:warn? warn?)
  530. packages)
  531. (return #t)))
  532. (else
  533. (for-each (cut check-for-package-update <> updaters
  534. #:warn? warn?)
  535. packages)
  536. (return #t)))))))))