pull.scm 33 KB

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