pull.scm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
  4. ;;; Copyright © 2020, 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix scripts pull)
  21. #:use-module ((guix ui) #:hide (display-profile-content))
  22. #:use-module (guix colors)
  23. #:use-module (guix utils)
  24. #:use-module ((guix status) #:select (with-status-verbosity))
  25. #:use-module (guix scripts)
  26. #:use-module (guix store)
  27. #:use-module (guix config)
  28. #:use-module (guix packages)
  29. #:use-module (guix derivations)
  30. #:use-module (guix profiles)
  31. #:use-module (guix gexp)
  32. #:use-module (guix grafts)
  33. #:use-module (guix memoization)
  34. #:use-module (guix monads)
  35. #:use-module (guix channels)
  36. #:autoload (guix inferior) (open-inferior
  37. inferior-available-packages
  38. close-inferior)
  39. #:use-module (guix scripts build)
  40. #:use-module (guix scripts describe)
  41. #:autoload (guix build utils) (which mkdir-p)
  42. #:use-module ((guix build syscalls)
  43. #:select (with-file-lock/no-wait))
  44. #:use-module (guix git)
  45. #:use-module (git)
  46. #:autoload (gnu packages) (fold-available-packages)
  47. #:autoload (guix scripts package) (build-and-use-profile
  48. delete-matching-generations)
  49. #:autoload (gnu packages base) (canonical-package)
  50. #:autoload (gnu packages bootstrap) (%bootstrap-guile)
  51. #:autoload (gnu packages certs) (le-certs)
  52. #:use-module (srfi srfi-1)
  53. #:use-module (srfi srfi-26)
  54. #:use-module (srfi srfi-34)
  55. #:use-module (srfi srfi-35)
  56. #:use-module (srfi srfi-37)
  57. #:use-module (srfi srfi-71)
  58. #:use-module (ice-9 match)
  59. #:use-module (ice-9 vlist)
  60. #:use-module (ice-9 format)
  61. #:re-export (display-profile-content
  62. channel-commit-hyperlink)
  63. #:export (channel-list
  64. guix-pull))
  65. ;;;
  66. ;;; Command-line options.
  67. ;;;
  68. (define %default-options
  69. ;; Alist of default option values.
  70. `((system . ,(%current-system))
  71. (substitutes? . #t)
  72. (offload? . #t)
  73. (print-build-trace? . #t)
  74. (print-extended-build-trace? . #t)
  75. (multiplexed-build-output? . #t)
  76. (graft? . #t)
  77. (debug . 0)
  78. (verbosity . 1)
  79. (authenticate-channels? . #t)
  80. (validate-pull . ,ensure-forward-channel-update)))
  81. (define (show-help)
  82. (display (G_ "Usage: guix pull [OPTION]...
  83. Download and deploy the latest version of Guix.\n"))
  84. (display (G_ "
  85. -C, --channels=FILE deploy the channels defined in FILE"))
  86. (display (G_ "
  87. --url=URL download \"guix\" channel from the Git repository at URL"))
  88. (display (G_ "
  89. --commit=COMMIT download the specified \"guix\" channel COMMIT"))
  90. (display (G_ "
  91. --branch=BRANCH download the tip of the specified \"guix\" channel BRANCH"))
  92. (display (G_ "
  93. --allow-downgrades allow downgrades to earlier channel revisions"))
  94. (display (G_ "
  95. --disable-authentication
  96. disable channel authentication"))
  97. (display (G_ "
  98. -N, --news display news compared to the previous generation"))
  99. (display (G_ "
  100. -l, --list-generations[=PATTERN]
  101. list generations matching PATTERN"))
  102. (display (G_ "
  103. --roll-back roll back to the previous generation"))
  104. (display (G_ "
  105. -d, --delete-generations[=PATTERN]
  106. delete generations matching PATTERN"))
  107. (display (G_ "
  108. -S, --switch-generation=PATTERN
  109. switch to a generation matching PATTERN"))
  110. (display (G_ "
  111. -p, --profile=PROFILE use PROFILE instead of ~/.config/guix/current"))
  112. (display (G_ "
  113. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  114. (display (G_ "
  115. -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
  116. (display (G_ "
  117. --bootstrap use the bootstrap Guile to build the new Guix"))
  118. (newline)
  119. (show-build-options-help)
  120. (display (G_ "
  121. -h, --help display this help and exit"))
  122. (display (G_ "
  123. -V, --version display version information and exit"))
  124. (newline)
  125. (show-bug-report-information))
  126. (define %options
  127. ;; Specifications of the command-line options.
  128. (cons* (option '(#\C "channels") #t #f
  129. (lambda (opt name arg result)
  130. (alist-cons 'channel-file arg result)))
  131. (option '(#\l "list-generations") #f #t
  132. (lambda (opt name arg result)
  133. (cons `(query list-generations ,arg)
  134. result)))
  135. (option '("roll-back") #f #f
  136. (lambda (opt name arg result)
  137. (cons '(generation roll-back)
  138. result)))
  139. (option '(#\S "switch-generation") #t #f
  140. (lambda (opt name arg result)
  141. (cons `(generation switch ,arg)
  142. result)))
  143. (option '(#\d "delete-generations") #f #t
  144. (lambda (opt name arg result)
  145. (cons `(generation delete ,arg)
  146. result)))
  147. (option '(#\N "news") #f #f
  148. (lambda (opt name arg result)
  149. (cons '(query display-news) result)))
  150. (option '("url") #t #f
  151. (lambda (opt name arg result)
  152. (alist-cons 'repository-url arg
  153. (alist-delete 'repository-url result))))
  154. (option '("commit") #t #f
  155. (lambda (opt name arg result)
  156. (alist-cons 'ref `(commit . ,arg) result)))
  157. (option '("branch") #t #f
  158. (lambda (opt name arg result)
  159. (alist-cons 'ref `(branch . ,arg) result)))
  160. (option '("allow-downgrades") #f #f
  161. (lambda (opt name arg result)
  162. (alist-cons 'validate-pull warn-about-backward-updates
  163. result)))
  164. (option '("disable-authentication") #f #f
  165. (lambda (opt name arg result)
  166. (alist-cons 'authenticate-channels? #f result)))
  167. (option '(#\p "profile") #t #f
  168. (lambda (opt name arg result)
  169. (alist-cons 'profile (canonicalize-profile arg)
  170. result)))
  171. (option '(#\s "system") #t #f
  172. (lambda (opt name arg result)
  173. (alist-cons 'system arg
  174. (alist-delete 'system result eq?))))
  175. (option '(#\n "dry-run") #f #f
  176. (lambda (opt name arg result)
  177. (alist-cons 'dry-run? #t result)))
  178. (option '(#\v "verbosity") #t #f
  179. (lambda (opt name arg result)
  180. (let ((level (string->number* arg)))
  181. (alist-cons 'verbosity level
  182. (alist-delete 'verbosity result)))))
  183. (option '("bootstrap") #f #f
  184. (lambda (opt name arg result)
  185. (alist-cons 'bootstrap? #t result)))
  186. (option '(#\h "help") #f #f
  187. (lambda args
  188. (show-help)
  189. (exit 0)))
  190. (option '(#\V "version") #f #f
  191. (lambda args
  192. (show-version-and-exit "guix pull")))
  193. %standard-build-options))
  194. (define (warn-about-backward-updates channel start commit relation)
  195. "Warn about non-forward updates of CHANNEL from START to COMMIT, without
  196. aborting."
  197. (match relation
  198. ((or 'ancestor 'self)
  199. #t)
  200. ('descendant
  201. (warning (G_ "rolling back channel '~a' from ~a to ~a~%")
  202. (channel-name channel) start commit))
  203. ('unrelated
  204. (warning (G_ "moving channel '~a' from ~a to unrelated commit ~a~%")
  205. (channel-name channel) start commit))))
  206. (define* (display-profile-news profile #:key concise?
  207. current-is-newer?)
  208. "Display what's up in PROFILE--new packages, and all that. If
  209. CURRENT-IS-NEWER? is true, assume that the current process represents the
  210. newest generation of PROFILE. Return true when there's more info to display."
  211. (match (memv (generation-number profile)
  212. (reverse (profile-generations profile)))
  213. ((current previous _ ...)
  214. (let ((these (fold-available-packages
  215. (lambda* (name version result
  216. #:key supported? deprecated?
  217. #:allow-other-keys)
  218. (if (and supported? (not deprecated?))
  219. (alist-cons name version result)
  220. result))
  221. '()))
  222. (those (profile-package-alist
  223. (generation-file-name profile
  224. (if current-is-newer?
  225. previous
  226. current)))))
  227. (let ((old (if current-is-newer? those these))
  228. (new (if current-is-newer? these those)))
  229. (display-new/upgraded-packages old new
  230. #:concise? concise?
  231. #:heading
  232. (G_ "New in this revision:\n")))))
  233. (_ #f)))
  234. (define (display-channel channel)
  235. "Display information about CHANNEL."
  236. (format (current-error-port)
  237. ;; TRANSLATORS: This describes a "channel"; the first placeholder is
  238. ;; the channel name (e.g., "guix") and the second placeholder is its
  239. ;; URL.
  240. (G_ " ~a at ~a~%")
  241. (channel-name channel)
  242. (channel-url channel)))
  243. (define (channel=? channel1 channel2)
  244. "Return true if CHANNEL1 and CHANNEL2 are the same for all practical
  245. purposes."
  246. ;; Assume that the URL matters less than the name.
  247. (eq? (channel-name channel1) (channel-name channel2)))
  248. (define (display-news-entry-title entry language port)
  249. "Display the title of ENTRY, a news entry, to PORT."
  250. (define title
  251. (channel-news-entry-title entry))
  252. (let ((title (or (assoc-ref title language)
  253. (assoc-ref title (%default-message-language))
  254. "")))
  255. (format port " ~a~%"
  256. (highlight
  257. (string-trim-right
  258. (catch 'parser-error
  259. (lambda ()
  260. (texi->plain-text title))
  261. ;; When Texinfo markup is invalid, display it as-is.
  262. (const title)))))))
  263. (define (display-news-entry entry channel language port)
  264. "Display ENTRY, a <channel-news-entry> from CHANNEL, in LANGUAGE, a language
  265. code, to PORT."
  266. (define body
  267. (channel-news-entry-body entry))
  268. (define commit
  269. (channel-news-entry-commit entry))
  270. (display-news-entry-title entry language port)
  271. (format port (dim (G_ " commit ~a~%"))
  272. (if (supports-hyperlinks?)
  273. (channel-commit-hyperlink channel commit)
  274. commit))
  275. (newline port)
  276. (let ((body (or (assoc-ref body language)
  277. (assoc-ref body (%default-message-language))
  278. "")))
  279. (format port "~a~%"
  280. (indented-string
  281. (parameterize ((%text-width (- (%text-width) 4)))
  282. (string-trim-right
  283. (catch 'parser-error
  284. (lambda ()
  285. (texi->plain-text body))
  286. (lambda _
  287. ;; When Texinfo markup is invalid, display it as-is.
  288. (fill-paragraph body (%text-width))))))
  289. 4))))
  290. (define* (display-channel-specific-news new old
  291. #:key (port (current-output-port))
  292. concise?)
  293. "Display channel news applicable the commits between OLD and NEW, where OLD
  294. and NEW are <channel> records with a proper 'commit' field. When CONCISE? is
  295. true, display nothing but the news titles. Return true if there are more news
  296. to display."
  297. (let ((channel new)
  298. (old (channel-commit old))
  299. (new (channel-commit new)))
  300. (when (and old new)
  301. (let ((language (current-message-language)))
  302. (match (channel-news-for-commit channel new old)
  303. (() ;no news is good news
  304. #f)
  305. ((entries ...)
  306. (newline port)
  307. (format port (G_ "News for channel '~a'~%")
  308. (channel-name channel))
  309. (for-each (if concise?
  310. (cut display-news-entry-title <> language port)
  311. (cut display-news-entry <> channel language port))
  312. entries)
  313. (newline port)
  314. #t))))))
  315. (define* (display-channel-news profile
  316. #:optional
  317. (previous
  318. (and=> (relative-generation profile -1)
  319. (cut generation-file-name profile <>))))
  320. "Display news about the channels of PROFILE compared to PREVIOUS."
  321. (when previous
  322. (let ((old-channels (profile-channels previous))
  323. (new-channels (profile-channels profile)))
  324. (and (pair? old-channels) (pair? new-channels)
  325. (begin
  326. (match (lset-difference channel=? new-channels old-channels)
  327. (()
  328. #t)
  329. (new
  330. (let ((count (length new)))
  331. (format (current-error-port)
  332. (N_ " ~a new channel:~%"
  333. " ~a new channels:~%" count)
  334. count)
  335. (for-each display-channel new))))
  336. (match (lset-difference channel=? old-channels new-channels)
  337. (()
  338. #t)
  339. (removed
  340. (let ((count (length removed)))
  341. (format (current-error-port)
  342. (N_ " ~a channel removed:~%"
  343. " ~a channels removed:~%" count)
  344. count)
  345. (for-each display-channel removed))))
  346. ;; Display channel-specific news for those channels that were
  347. ;; here before and are still around afterwards.
  348. (for-each (match-lambda
  349. ((new old)
  350. (display-channel-specific-news new old)))
  351. (filter-map (lambda (new)
  352. (define old
  353. (find (cut channel=? new <>)
  354. old-channels))
  355. (and old (list new old)))
  356. new-channels)))))))
  357. (define* (display-channel-news-headlines profile)
  358. "Display the titles of news about the channels of PROFILE compared to its
  359. previous generation. Return true if there are news to display."
  360. (define previous
  361. (and=> (relative-generation profile -1)
  362. (cut generation-file-name profile <>)))
  363. (and previous
  364. (let ((old-channels (profile-channels previous))
  365. (new-channels (profile-channels profile)))
  366. ;; Find the channels present in both PROFILE and PREVIOUS, and print
  367. ;; their news.
  368. (and (pair? old-channels) (pair? new-channels)
  369. (let ((channels (filter-map (lambda (new)
  370. (define old
  371. (find (cut channel=? new <>)
  372. old-channels))
  373. (and old (list new old)))
  374. new-channels)))
  375. (define more?
  376. (map (match-lambda
  377. ((new old)
  378. (display-channel-specific-news new old
  379. #:concise? #t)))
  380. channels))
  381. (any ->bool more?))))))
  382. (define (display-news profile)
  383. ;; Display profile news, with the understanding that this process represents
  384. ;; the newest generation.
  385. (display-profile-news profile
  386. #:current-is-newer? #t)
  387. (display-channel-news profile))
  388. (define* (build-and-install instances profile)
  389. "Build the tool from SOURCE, and install it in PROFILE. When DRY-RUN? is
  390. true, display what would be built without actually building it."
  391. (define update-profile
  392. (store-lift build-and-use-profile))
  393. (define guix-command
  394. ;; The 'guix' command before we've built the new profile.
  395. (which "guix"))
  396. (mlet %store-monad ((manifest (channel-instances->manifest instances)))
  397. (mbegin %store-monad
  398. (update-profile profile manifest
  399. #:hooks %channel-profile-hooks)
  400. (return
  401. (let ((more? (list (display-profile-news profile #:concise? #t)
  402. (display-channel-news-headlines profile))))
  403. (newline)
  404. (when (any ->bool more?)
  405. (display-hint
  406. (G_ "Run @command{guix pull --news} to read all the news.")))))
  407. (if guix-command
  408. (let ((new (map (cut string-append <> "/bin/guix")
  409. (list (user-friendly-profile profile)
  410. profile))))
  411. ;; Is the 'guix' command previously in $PATH the same as the new
  412. ;; one? If the answer is "no", then suggest 'hash guix'.
  413. (unless (member guix-command new)
  414. (display-hint (format #f (G_ "After setting @code{PATH}, run
  415. @command{hash guix} to make sure your shell refers to @file{~a}.")
  416. (first new))))
  417. (return #f))
  418. (return #f)))))
  419. (define (honor-lets-encrypt-certificates! store)
  420. "Tell Guile-Git to use the Let's Encrypt certificates."
  421. (let* ((drv (package-derivation store le-certs))
  422. (certs (string-append (derivation->output-path drv)
  423. "/etc/ssl/certs")))
  424. (build-derivations store (list drv))
  425. (set-tls-certificate-locations! certs)))
  426. (define (honor-x509-certificates store)
  427. "Use the right X.509 certificates for Git checkouts over HTTPS."
  428. (unless (honor-system-x509-certificates!)
  429. (honor-lets-encrypt-certificates! store)))
  430. ;;;
  431. ;;; Profile.
  432. ;;;
  433. (define %current-profile
  434. ;; The "real" profile under /var/guix.
  435. (string-append %profile-directory "/current-guix"))
  436. (define %user-profile-directory
  437. ;; The user-friendly name of %CURRENT-PROFILE.
  438. (string-append (config-directory #:ensure? #f) "/current"))
  439. (define (migrate-generations profile directory)
  440. "Migrate the generations of PROFILE to DIRECTORY."
  441. (format (current-error-port)
  442. (G_ "Migrating profile generations to '~a'...~%")
  443. %profile-directory)
  444. (let ((current (generation-number profile)))
  445. (for-each (lambda (generation)
  446. (let ((source (generation-file-name profile generation))
  447. (target (string-append directory "/current-guix-"
  448. (number->string generation)
  449. "-link")))
  450. ;; Note: Don't use 'rename-file' as SOURCE and TARGET might
  451. ;; live on different file systems.
  452. (symlink (readlink source) target)
  453. (delete-file source)))
  454. (profile-generations profile))
  455. (symlink (string-append "current-guix-"
  456. (number->string current) "-link")
  457. (string-append directory "/current-guix"))))
  458. (define (ensure-default-profile)
  459. (ensure-profile-directory)
  460. ;; In 0.15.0+ we'd create ~/.config/guix/current-[0-9]*-link symlinks. Move
  461. ;; them to %PROFILE-DIRECTORY.
  462. ;;
  463. ;; XXX: Ubuntu's 'sudo' preserves $HOME by default, and thus the second
  464. ;; condition below is always false when one runs "sudo guix pull". As a
  465. ;; workaround, skip this code when $SUDO_USER is set. See
  466. ;; <https://bugs.gnu.org/36785>.
  467. (unless (or (getenv "SUDO_USER")
  468. (not (file-exists? %user-profile-directory))
  469. (string=? %profile-directory
  470. (dirname
  471. (canonicalize-profile %user-profile-directory))))
  472. (migrate-generations %user-profile-directory %profile-directory))
  473. ;; Make sure ~/.config/guix/current points to /var/guix/profiles/….
  474. (let ((link %user-profile-directory))
  475. (unless (equal? (false-if-exception (readlink link))
  476. %current-profile)
  477. (catch 'system-error
  478. (lambda ()
  479. (false-if-exception (delete-file link))
  480. (mkdir-p (dirname link))
  481. (symlink %current-profile link))
  482. (lambda args
  483. (leave (G_ "while creating symlink '~a': ~a~%")
  484. link (strerror (system-error-errno args))))))))
  485. ;;;
  486. ;;; Queries.
  487. ;;;
  488. (define profile-package-alist
  489. (mlambda (profile)
  490. "Return a name/version alist representing the packages in PROFILE."
  491. (let* ((inferior (open-inferior profile))
  492. (packages (inferior-available-packages inferior)))
  493. (close-inferior inferior)
  494. packages)))
  495. (define (new/upgraded-packages alist1 alist2)
  496. "Compare ALIST1 and ALIST2, both of which are lists of package name/version
  497. pairs, and return two values: the list of packages new in ALIST2, and the list
  498. of packages upgraded in ALIST2."
  499. (let* ((old (fold (match-lambda*
  500. (((name . version) table)
  501. (match (vhash-assoc name table)
  502. (#f
  503. (vhash-cons name version table))
  504. ((_ . previous-version)
  505. (if (version>? version previous-version)
  506. (vhash-cons name version table)
  507. table)))))
  508. vlist-null
  509. alist1))
  510. (new (remove (match-lambda
  511. ((name . _)
  512. (vhash-assoc name old)))
  513. alist2))
  514. (upgraded (filter-map (match-lambda
  515. ((name . new-version)
  516. (match (vhash-assoc name old)
  517. (#f #f)
  518. ((_ . old-version)
  519. (and (version>? new-version old-version)
  520. (string-append name "@"
  521. new-version))))))
  522. alist2)))
  523. (values new upgraded)))
  524. (define* (ellipsis #:optional (port (current-output-port)))
  525. "Return HORIZONTAL ELLIPSIS three dots if PORT's encoding cannot represent
  526. it."
  527. (match (port-encoding port)
  528. ("UTF-8" "…")
  529. (_ "...")))
  530. (define* (display-new/upgraded-packages alist1 alist2
  531. #:key (heading "") concise?)
  532. "Given the two package name/version alists ALIST1 and ALIST2, display the
  533. list of new and upgraded packages going from ALIST1 to ALIST2. When ALIST1
  534. and ALIST2 differ, display HEADING upfront. When CONCISE? is true, do not
  535. display long package lists that would fill the user's screen.
  536. Return true when there is more package info to display."
  537. (define (pretty str column)
  538. (indented-string (fill-paragraph str (- (%text-width) 4)
  539. column)
  540. 4 #:initial-indent? #f))
  541. (define concise/max-item-count
  542. ;; Maximum number of items to display when CONCISE? is true.
  543. 12)
  544. (define list->enumeration
  545. (if concise?
  546. (lambda* (lst #:optional (max concise/max-item-count))
  547. (if (> (length lst) max)
  548. (string-append (string-join (take lst max) ", ")
  549. ", " (ellipsis))
  550. (string-join lst ", ")))
  551. (cut string-join <> ", ")))
  552. (let ((new upgraded (new/upgraded-packages alist1 alist2)))
  553. (define new-count (length new))
  554. (define upgraded-count (length upgraded))
  555. (unless (and (null? new) (null? upgraded))
  556. (display heading))
  557. (match new-count
  558. (0 #t)
  559. (count
  560. (format #t (N_ " ~h new package: ~a~%"
  561. " ~h new packages: ~a~%" count)
  562. count
  563. (pretty (list->enumeration (sort (map first new) string<?))
  564. 30))))
  565. (match upgraded-count
  566. (0 #t)
  567. (count
  568. (format #t (N_ " ~h package upgraded: ~a~%"
  569. " ~h packages upgraded: ~a~%" count)
  570. count
  571. (pretty (list->enumeration (sort upgraded string<?))
  572. 35))))
  573. (and concise?
  574. (or (> new-count concise/max-item-count)
  575. (> upgraded-count concise/max-item-count)))))
  576. (define (display-profile-content-diff profile gen1 gen2)
  577. "Display the changes in PROFILE GEN2 compared to generation GEN1."
  578. (define (package-alist generation)
  579. (profile-package-alist (generation-file-name profile generation)))
  580. (display-profile-content profile gen2)
  581. (display-new/upgraded-packages (package-alist gen1)
  582. (package-alist gen2)))
  583. (define (process-query opts profile)
  584. "Process any query on PROFILE specified by OPTS."
  585. (match (assoc-ref opts 'query)
  586. (('list-generations pattern)
  587. (define (list-generations profile numbers)
  588. (match numbers
  589. ((first rest ...)
  590. (display-profile-content profile first)
  591. (let loop ((numbers numbers))
  592. (match numbers
  593. ((first second rest ...)
  594. (display-profile-content-diff profile
  595. first second)
  596. (display-channel-news (generation-file-name profile second)
  597. (generation-file-name profile first))
  598. (loop (cons second rest)))
  599. ((_) #t)
  600. (() #t))))))
  601. (leave-on-EPIPE
  602. (cond ((not (file-exists? profile)) ; XXX: race condition
  603. (raise (condition (&profile-not-found-error
  604. (profile profile)))))
  605. ((not pattern)
  606. (list-generations profile (profile-generations profile)))
  607. ((matching-generations pattern profile)
  608. =>
  609. (match-lambda
  610. (()
  611. (exit 1))
  612. ((numbers ...)
  613. (list-generations profile numbers)))))))
  614. (('display-news)
  615. (display-news profile))))
  616. (define (process-generation-change opts profile)
  617. "Process a request to change the current generation (roll-back, switch, delete)."
  618. (unless (assoc-ref opts 'dry-run?)
  619. (match (assoc-ref opts 'generation)
  620. (('roll-back)
  621. (with-store store
  622. (roll-back* store profile)))
  623. (('switch pattern)
  624. (let ((number (relative-generation-spec->number profile pattern)))
  625. (if number
  626. (switch-to-generation* profile number)
  627. (leave (G_ "cannot switch to generation '~a'~%") pattern))))
  628. (('delete pattern)
  629. (with-store store
  630. (delete-matching-generations store profile pattern))))))
  631. (define (channel-list opts)
  632. "Return the list of channels to use. If OPTS specify a channel file,
  633. channels are read from there; otherwise, if ~/.config/guix/channels.scm
  634. exists, read it; otherwise %DEFAULT-CHANNELS is used. Apply channel
  635. transformations specified in OPTS (resulting from '--url', '--commit', or
  636. '--branch'), if any."
  637. (define file
  638. (assoc-ref opts 'channel-file))
  639. (define default-file
  640. (string-append (config-directory) "/channels.scm"))
  641. (define global-file
  642. (string-append %sysconfdir "/guix/channels.scm"))
  643. (define (load-channels file)
  644. (let ((result (load* file (make-user-module '((guix channels))))))
  645. (if (and (list? result) (every channel? result))
  646. result
  647. (leave (G_ "'~a' did not return a list of channels~%") file))))
  648. (define channels
  649. (cond (file
  650. (load-channels file))
  651. ((file-exists? default-file)
  652. (load-channels default-file))
  653. ((file-exists? global-file)
  654. (load-channels global-file))
  655. (else
  656. %default-channels)))
  657. (define (environment-variable)
  658. (match (getenv "GUIX_PULL_URL")
  659. (#f #f)
  660. (url
  661. (warning (G_ "The 'GUIX_PULL_URL' environment variable is deprecated.
  662. Use '~/.config/guix/channels.scm' instead."))
  663. url)))
  664. (let ((ref (assoc-ref opts 'ref))
  665. (url (or (assoc-ref opts 'repository-url)
  666. (environment-variable))))
  667. (if (or ref url)
  668. (match (find guix-channel? channels)
  669. ((? channel? guix)
  670. ;; Apply '--url', '--commit', and '--branch' to the 'guix' channel.
  671. (let ((url (or url (channel-url guix))))
  672. (cons (match ref
  673. (('commit . commit)
  674. (channel (inherit guix)
  675. (url url) (commit commit) (branch #f)))
  676. (('branch . branch)
  677. (channel (inherit guix)
  678. (url url) (commit #f) (branch branch)))
  679. (#f
  680. (channel (inherit guix) (url url))))
  681. (remove guix-channel? channels))))
  682. (#f ;no 'guix' channel, failure will ensue
  683. channels))
  684. channels)))
  685. (define-command (guix-pull . args)
  686. (synopsis "pull the latest revision of Guix")
  687. (define (no-arguments arg _‌)
  688. (leave (G_ "~A: extraneous argument~%") arg))
  689. (with-error-handling
  690. (with-git-error-handling
  691. (let* ((opts (parse-command-line args %options
  692. (list %default-options)
  693. #:argument-handler no-arguments))
  694. (substitutes? (assoc-ref opts 'substitutes?))
  695. (dry-run? (assoc-ref opts 'dry-run?))
  696. (profile (or (assoc-ref opts 'profile) %current-profile))
  697. (current-channels (profile-channels profile))
  698. (validate-pull (assoc-ref opts 'validate-pull))
  699. (authenticate? (assoc-ref opts 'authenticate-channels?)))
  700. (cond
  701. ((assoc-ref opts 'query)
  702. (process-query opts profile))
  703. ((assoc-ref opts 'generation)
  704. (process-generation-change opts profile))
  705. (else
  706. (with-store store
  707. (with-status-verbosity (assoc-ref opts 'verbosity)
  708. (parameterize ((%current-system (assoc-ref opts 'system))
  709. (%graft? (assoc-ref opts 'graft?)))
  710. (with-build-handler (build-notifier #:use-substitutes?
  711. substitutes?
  712. #:verbosity
  713. (assoc-ref opts 'verbosity)
  714. #:dry-run? dry-run?)
  715. (set-build-options-from-command-line store opts)
  716. (ensure-default-profile)
  717. (honor-x509-certificates store)
  718. (let* ((channels (channel-list opts))
  719. (instances
  720. (latest-channel-instances store channels
  721. #:current-channels
  722. current-channels
  723. #:validate-pull
  724. validate-pull
  725. #:authenticate?
  726. authenticate?)))
  727. (format (current-error-port)
  728. (N_ "Building from this channel:~%"
  729. "Building from these channels:~%"
  730. (length instances)))
  731. (for-each (lambda (instance)
  732. (let ((channel
  733. (channel-instance-channel instance)))
  734. (format (current-error-port)
  735. " ~10a~a\t~a~%"
  736. (channel-name channel)
  737. (channel-url channel)
  738. (string-take
  739. (channel-instance-commit instance)
  740. 7))))
  741. instances)
  742. (parameterize ((%guile-for-build
  743. (package-derivation
  744. store
  745. (if (assoc-ref opts 'bootstrap?)
  746. %bootstrap-guile
  747. (default-guile)))))
  748. (with-profile-lock profile
  749. (run-with-store store
  750. (build-and-install instances profile)))))))))))))))
  751. ;;; pull.scm ends here