pull.scm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix scripts pull)
  20. #:use-module ((guix ui) #:hide (display-profile-content))
  21. #:use-module (guix colors)
  22. #:use-module (guix utils)
  23. #:use-module ((guix status) #:select (with-status-verbosity))
  24. #:use-module (guix scripts)
  25. #:use-module (guix store)
  26. #:use-module (guix config)
  27. #:use-module (guix packages)
  28. #:use-module (guix derivations)
  29. #:use-module (guix profiles)
  30. #:use-module (guix gexp)
  31. #:use-module (guix grafts)
  32. #:use-module (guix memoization)
  33. #:use-module (guix monads)
  34. #:use-module (guix channels)
  35. #:autoload (guix inferior) (open-inferior
  36. inferior-available-packages
  37. close-inferior)
  38. #:use-module (guix scripts build)
  39. #:use-module (guix scripts describe)
  40. #:autoload (guix build utils) (which)
  41. #:use-module ((guix build syscalls)
  42. #:select (with-file-lock/no-wait))
  43. #:use-module (guix git)
  44. #:use-module (git)
  45. #:use-module (gnu packages)
  46. #:use-module ((guix scripts package) #:select (build-and-use-profile
  47. delete-matching-generations))
  48. #:use-module ((gnu packages base) #:select (canonical-package))
  49. #:use-module (gnu packages guile)
  50. #:use-module ((gnu packages bootstrap)
  51. #:select (%bootstrap-guile))
  52. #:use-module ((gnu packages certs) #:select (le-certs))
  53. #:use-module (srfi srfi-1)
  54. #:use-module (srfi srfi-11)
  55. #:use-module (srfi srfi-26)
  56. #:use-module (srfi srfi-34)
  57. #:use-module (srfi srfi-35)
  58. #:use-module (srfi srfi-37)
  59. #:use-module (ice-9 match)
  60. #:use-module (ice-9 vlist)
  61. #:use-module (ice-9 format)
  62. #:re-export (display-profile-content
  63. channel-commit-hyperlink)
  64. #:export (channel-list
  65. guix-pull))
  66. ;;;
  67. ;;; Command-line options.
  68. ;;;
  69. (define %default-options
  70. ;; Alist of default option values.
  71. `((system . ,(%current-system))
  72. (substitutes? . #t)
  73. (offload? . #t)
  74. (print-build-trace? . #t)
  75. (print-extended-build-trace? . #t)
  76. (multiplexed-build-output? . #t)
  77. (graft? . #t)
  78. (debug . 0)
  79. (verbosity . 1)
  80. (authenticate-channels? . #t)
  81. (validate-pull . ,ensure-forward-channel-update)))
  82. (define (show-help)
  83. (display (G_ "Usage: guix pull [OPTION]...
  84. Download and deploy the latest version of Guix.\n"))
  85. (display (G_ "
  86. -C, --channels=FILE deploy the channels defined in FILE"))
  87. (display (G_ "
  88. --url=URL download from the Git repository at URL"))
  89. (display (G_ "
  90. --commit=COMMIT download the specified COMMIT"))
  91. (display (G_ "
  92. --branch=BRANCH download the tip of the specified BRANCH"))
  93. (display (G_ "
  94. --allow-downgrades allow downgrades to earlier channel revisions"))
  95. (display (G_ "
  96. --disable-authentication
  97. disable channel authentication"))
  98. (display (G_ "
  99. -N, --news display news compared to the previous generation"))
  100. (display (G_ "
  101. -l, --list-generations[=PATTERN]
  102. list generations matching PATTERN"))
  103. (display (G_ "
  104. --roll-back roll back to the previous generation"))
  105. (display (G_ "
  106. -d, --delete-generations[=PATTERN]
  107. delete generations matching PATTERN"))
  108. (display (G_ "
  109. -S, --switch-generation=PATTERN
  110. switch to a generation matching PATTERN"))
  111. (display (G_ "
  112. -p, --profile=PROFILE use PROFILE instead of ~/.config/guix/current"))
  113. (display (G_ "
  114. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  115. (display (G_ "
  116. -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
  117. (display (G_ "
  118. --bootstrap use the bootstrap Guile to build the new Guix"))
  119. (newline)
  120. (show-build-options-help)
  121. (display (G_ "
  122. -h, --help display this help and exit"))
  123. (display (G_ "
  124. -V, --version display version information and exit"))
  125. (newline)
  126. (show-bug-report-information))
  127. (define %options
  128. ;; Specifications of the command-line options.
  129. (cons* (option '(#\C "channels") #t #f
  130. (lambda (opt name arg result)
  131. (alist-cons 'channel-file arg result)))
  132. (option '(#\l "list-generations") #f #t
  133. (lambda (opt name arg result)
  134. (cons `(query list-generations ,arg)
  135. result)))
  136. (option '("roll-back") #f #f
  137. (lambda (opt name arg result)
  138. (cons '(generation roll-back)
  139. result)))
  140. (option '(#\S "switch-generation") #t #f
  141. (lambda (opt name arg result)
  142. (cons `(generation switch ,arg)
  143. result)))
  144. (option '(#\d "delete-generations") #f #t
  145. (lambda (opt name arg result)
  146. (cons `(generation delete ,arg)
  147. result)))
  148. (option '(#\N "news") #f #f
  149. (lambda (opt name arg result)
  150. (cons '(query display-news) result)))
  151. (option '("url") #t #f
  152. (lambda (opt name arg result)
  153. (alist-cons 'repository-url arg
  154. (alist-delete 'repository-url result))))
  155. (option '("commit") #t #f
  156. (lambda (opt name arg result)
  157. (alist-cons 'ref `(commit . ,arg) result)))
  158. (option '("branch") #t #f
  159. (lambda (opt name arg result)
  160. (alist-cons 'ref `(branch . ,arg) result)))
  161. (option '("allow-downgrades") #f #f
  162. (lambda (opt name arg result)
  163. (alist-cons 'validate-pull warn-about-backward-updates
  164. result)))
  165. (option '("disable-authentication") #f #f
  166. (lambda (opt name arg result)
  167. (alist-cons 'authenticate-channels? #f result)))
  168. (option '(#\p "profile") #t #f
  169. (lambda (opt name arg result)
  170. (alist-cons 'profile (canonicalize-profile arg)
  171. result)))
  172. (option '(#\s "system") #t #f
  173. (lambda (opt name arg result)
  174. (alist-cons 'system arg
  175. (alist-delete 'system result eq?))))
  176. (option '(#\n "dry-run") #f #f
  177. (lambda (opt name arg result)
  178. (alist-cons 'dry-run? #t result)))
  179. (option '(#\v "verbosity") #t #f
  180. (lambda (opt name arg result)
  181. (let ((level (string->number* arg)))
  182. (alist-cons 'verbosity level
  183. (alist-delete 'verbosity result)))))
  184. (option '("bootstrap") #f #f
  185. (lambda (opt name arg result)
  186. (alist-cons 'bootstrap? #t result)))
  187. (option '(#\h "help") #f #f
  188. (lambda args
  189. (show-help)
  190. (exit 0)))
  191. (option '(#\V "version") #f #f
  192. (lambda args
  193. (show-version-and-exit "guix pull")))
  194. %standard-build-options))
  195. (define (warn-about-backward-updates channel start commit relation)
  196. "Warn about non-forward updates of CHANNEL from START to COMMIT, without
  197. aborting."
  198. (match relation
  199. ((or 'ancestor 'self)
  200. #t)
  201. ('descendant
  202. (warning (G_ "rolling back channel '~a' from ~a to ~a~%")
  203. (channel-name channel) start commit))
  204. ('unrelated
  205. (warning (G_ "moving channel '~a' from ~a to unrelated commit ~a~%")
  206. (channel-name channel) start commit))))
  207. (define* (display-profile-news profile #:key concise?
  208. current-is-newer?)
  209. "Display what's up in PROFILE--new packages, and all that. If
  210. CURRENT-IS-NEWER? is true, assume that the current process represents the
  211. newest generation of PROFILE. Return true when there's more info to display."
  212. (match (memv (generation-number profile)
  213. (reverse (profile-generations profile)))
  214. ((current previous _ ...)
  215. (let ((these (fold-available-packages
  216. (lambda* (name version result
  217. #:key supported? deprecated?
  218. #:allow-other-keys)
  219. (if (and supported? (not deprecated?))
  220. (alist-cons name version result)
  221. result))
  222. '()))
  223. (those (profile-package-alist
  224. (generation-file-name profile
  225. (if current-is-newer?
  226. previous
  227. current)))))
  228. (let ((old (if current-is-newer? those these))
  229. (new (if current-is-newer? these those)))
  230. (display-new/upgraded-packages old new
  231. #:concise? concise?
  232. #:heading
  233. (G_ "New in this revision:\n")))))
  234. (_ #f)))
  235. (define (display-channel channel)
  236. "Display information about CHANNEL."
  237. (format (current-error-port)
  238. ;; TRANSLATORS: This describes a "channel"; the first placeholder is
  239. ;; the channel name (e.g., "guix") and the second placeholder is its
  240. ;; URL.
  241. (G_ " ~a at ~a~%")
  242. (channel-name channel)
  243. (channel-url channel)))
  244. (define (channel=? channel1 channel2)
  245. "Return true if CHANNEL1 and CHANNEL2 are the same for all practical
  246. purposes."
  247. ;; Assume that the URL matters less than the name.
  248. (eq? (channel-name channel1) (channel-name channel2)))
  249. (define (display-news-entry-title entry language port)
  250. "Display the title of ENTRY, a news entry, to PORT."
  251. (define title
  252. (channel-news-entry-title entry))
  253. (let ((title (or (assoc-ref title language)
  254. (assoc-ref title (%default-message-language))
  255. "")))
  256. (format port " ~a~%"
  257. (highlight
  258. (string-trim-right
  259. (catch 'parser-error
  260. (lambda ()
  261. (texi->plain-text title))
  262. ;; When Texinfo markup is invalid, display it as-is.
  263. (const title)))))))
  264. (define (display-news-entry entry channel language port)
  265. "Display ENTRY, a <channel-news-entry> from CHANNEL, in LANGUAGE, a language
  266. code, to PORT."
  267. (define body
  268. (channel-news-entry-body entry))
  269. (define commit
  270. (channel-news-entry-commit entry))
  271. (display-news-entry-title entry language port)
  272. (format port (dim (G_ " commit ~a~%"))
  273. (if (supports-hyperlinks?)
  274. (channel-commit-hyperlink channel commit)
  275. commit))
  276. (newline port)
  277. (let ((body (or (assoc-ref body language)
  278. (assoc-ref body (%default-message-language))
  279. "")))
  280. (format port "~a~%"
  281. (indented-string
  282. (parameterize ((%text-width (- (%text-width) 4)))
  283. (string-trim-right
  284. (catch 'parser-error
  285. (lambda ()
  286. (texi->plain-text body))
  287. (lambda _
  288. ;; When Texinfo markup is invalid, display it as-is.
  289. (fill-paragraph body (%text-width))))))
  290. 4))))
  291. (define* (display-channel-specific-news new old
  292. #:key (port (current-output-port))
  293. concise?)
  294. "Display channel news applicable the commits between OLD and NEW, where OLD
  295. and NEW are <channel> records with a proper 'commit' field. When CONCISE? is
  296. true, display nothing but the news titles. Return true if there are more news
  297. to display."
  298. (let ((channel new)
  299. (old (channel-commit old))
  300. (new (channel-commit new)))
  301. (when (and old new)
  302. (let ((language (current-message-language)))
  303. (match (channel-news-for-commit channel new old)
  304. (() ;no news is good news
  305. #f)
  306. ((entries ...)
  307. (newline port)
  308. (format port (G_ "News for channel '~a'~%")
  309. (channel-name channel))
  310. (for-each (if concise?
  311. (cut display-news-entry-title <> language port)
  312. (cut display-news-entry <> channel language port))
  313. entries)
  314. (newline port)
  315. #t))))))
  316. (define* (display-channel-news profile
  317. #:optional
  318. (previous
  319. (and=> (relative-generation profile -1)
  320. (cut generation-file-name profile <>))))
  321. "Display news about the channels of PROFILE compared to PREVIOUS."
  322. (when previous
  323. (let ((old-channels (profile-channels previous))
  324. (new-channels (profile-channels profile)))
  325. (and (pair? old-channels) (pair? new-channels)
  326. (begin
  327. (match (lset-difference channel=? new-channels old-channels)
  328. (()
  329. #t)
  330. (new
  331. (let ((count (length new)))
  332. (format (current-error-port)
  333. (N_ " ~a new channel:~%"
  334. " ~a new channels:~%" count)
  335. count)
  336. (for-each display-channel new))))
  337. (match (lset-difference channel=? old-channels new-channels)
  338. (()
  339. #t)
  340. (removed
  341. (let ((count (length removed)))
  342. (format (current-error-port)
  343. (N_ " ~a channel removed:~%"
  344. " ~a channels removed:~%" count)
  345. count)
  346. (for-each display-channel removed))))
  347. ;; Display channel-specific news for those channels that were
  348. ;; here before and are still around afterwards.
  349. (for-each (match-lambda
  350. ((new old)
  351. (display-channel-specific-news new old)))
  352. (filter-map (lambda (new)
  353. (define old
  354. (find (cut channel=? new <>)
  355. old-channels))
  356. (and old (list new old)))
  357. new-channels)))))))
  358. (define* (display-channel-news-headlines profile)
  359. "Display the titles of news about the channels of PROFILE compared to its
  360. previous generation. Return true if there are news to display."
  361. (define previous
  362. (and=> (relative-generation profile -1)
  363. (cut generation-file-name profile <>)))
  364. (when previous
  365. (let ((old-channels (profile-channels previous))
  366. (new-channels (profile-channels profile)))
  367. ;; Find the channels present in both PROFILE and PREVIOUS, and print
  368. ;; their news.
  369. (and (pair? old-channels) (pair? new-channels)
  370. (let ((channels (filter-map (lambda (new)
  371. (define old
  372. (find (cut channel=? new <>)
  373. old-channels))
  374. (and old (list new old)))
  375. new-channels)))
  376. (define more?
  377. (map (match-lambda
  378. ((new old)
  379. (display-channel-specific-news new old
  380. #:concise? #t)))
  381. channels))
  382. (any ->bool more?))))))
  383. (define (display-news profile)
  384. ;; Display profile news, with the understanding that this process represents
  385. ;; the newest generation.
  386. (display-profile-news profile
  387. #:current-is-newer? #t)
  388. (display-channel-news profile))
  389. (define* (build-and-install instances profile)
  390. "Build the tool from SOURCE, and install it in PROFILE. When DRY-RUN? is
  391. true, display what would be built without actually building it."
  392. (define update-profile
  393. (store-lift build-and-use-profile))
  394. (define guix-command
  395. ;; The 'guix' command before we've built the new profile.
  396. (which "guix"))
  397. (mlet %store-monad ((manifest (channel-instances->manifest instances)))
  398. (mbegin %store-monad
  399. (update-profile profile manifest
  400. #:hooks %channel-profile-hooks)
  401. (return
  402. (let ((more? (list (display-profile-news profile #:concise? #t)
  403. (display-channel-news-headlines profile))))
  404. (newline)
  405. (when (any ->bool more?)
  406. (display-hint
  407. (G_ "Run @command{guix pull --news} to read all the news.")))))
  408. (if guix-command
  409. (let ((new (map (cut string-append <> "/bin/guix")
  410. (list (user-friendly-profile profile)
  411. profile))))
  412. ;; Is the 'guix' command previously in $PATH the same as the new
  413. ;; one? If the answer is "no", then suggest 'hash guix'.
  414. (unless (member guix-command new)
  415. (display-hint (format #f (G_ "After setting @code{PATH}, run
  416. @command{hash guix} to make sure your shell refers to @file{~a}.")
  417. (first new))))
  418. (return #f))
  419. (return #f)))))
  420. (define (honor-lets-encrypt-certificates! store)
  421. "Tell Guile-Git to use the Let's Encrypt certificates."
  422. (let* ((drv (package-derivation store le-certs))
  423. (certs (string-append (derivation->output-path drv)
  424. "/etc/ssl/certs")))
  425. (build-derivations store (list drv))
  426. (set-tls-certificate-locations! certs)))
  427. (define (honor-x509-certificates store)
  428. "Use the right X.509 certificates for Git checkouts over HTTPS."
  429. (unless (honor-system-x509-certificates!)
  430. (honor-lets-encrypt-certificates! store)))
  431. ;;;
  432. ;;; Profile.
  433. ;;;
  434. (define %current-profile
  435. ;; The "real" profile under /var/guix.
  436. (string-append %profile-directory "/current-guix"))
  437. (define %user-profile-directory
  438. ;; The user-friendly name of %CURRENT-PROFILE.
  439. (string-append (config-directory #:ensure? #f) "/current"))
  440. (define (migrate-generations profile directory)
  441. "Migrate the generations of PROFILE to DIRECTORY."
  442. (format (current-error-port)
  443. (G_ "Migrating profile generations to '~a'...~%")
  444. %profile-directory)
  445. (let ((current (generation-number profile)))
  446. (for-each (lambda (generation)
  447. (let ((source (generation-file-name profile generation))
  448. (target (string-append directory "/current-guix-"
  449. (number->string generation)
  450. "-link")))
  451. ;; Note: Don't use 'rename-file' as SOURCE and TARGET might
  452. ;; live on different file systems.
  453. (symlink (readlink source) target)
  454. (delete-file source)))
  455. (profile-generations profile))
  456. (symlink (string-append "current-guix-"
  457. (number->string current) "-link")
  458. (string-append directory "/current-guix"))))
  459. (define (ensure-default-profile)
  460. (ensure-profile-directory)
  461. ;; In 0.15.0+ we'd create ~/.config/guix/current-[0-9]*-link symlinks. Move
  462. ;; them to %PROFILE-DIRECTORY.
  463. ;;
  464. ;; XXX: Ubuntu's 'sudo' preserves $HOME by default, and thus the second
  465. ;; condition below is always false when one runs "sudo guix pull". As a
  466. ;; workaround, skip this code when $SUDO_USER is set. See
  467. ;; <https://bugs.gnu.org/36785>.
  468. (unless (or (getenv "SUDO_USER")
  469. (not (file-exists? %user-profile-directory))
  470. (string=? %profile-directory
  471. (dirname
  472. (canonicalize-profile %user-profile-directory))))
  473. (migrate-generations %user-profile-directory %profile-directory))
  474. ;; Make sure ~/.config/guix/current points to /var/guix/profiles/….
  475. (let ((link %user-profile-directory))
  476. (unless (equal? (false-if-exception (readlink link))
  477. %current-profile)
  478. (catch 'system-error
  479. (lambda ()
  480. (false-if-exception (delete-file 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-values (((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. (with-error-handling
  688. (with-git-error-handling
  689. (let* ((opts (parse-command-line args %options
  690. (list %default-options)))
  691. (substitutes? (assoc-ref opts 'substitutes?))
  692. (dry-run? (assoc-ref opts 'dry-run?))
  693. (channels (channel-list opts))
  694. (profile (or (assoc-ref opts 'profile) %current-profile))
  695. (current-channels (profile-channels profile))
  696. (validate-pull (assoc-ref opts 'validate-pull))
  697. (authenticate? (assoc-ref opts 'authenticate-channels?)))
  698. (cond ((assoc-ref opts 'query)
  699. (process-query opts profile))
  700. ((assoc-ref opts 'generation)
  701. (process-generation-change opts profile))
  702. (else
  703. (with-store store
  704. (with-status-verbosity (assoc-ref opts 'verbosity)
  705. (parameterize ((%current-system (assoc-ref opts 'system))
  706. (%graft? (assoc-ref opts 'graft?)))
  707. (with-build-handler (build-notifier #:use-substitutes?
  708. substitutes?
  709. #:verbosity
  710. (assoc-ref opts 'verbosity)
  711. #:dry-run? dry-run?)
  712. (set-build-options-from-command-line store opts)
  713. (ensure-default-profile)
  714. (honor-x509-certificates store)
  715. (let ((instances
  716. (latest-channel-instances store channels
  717. #:current-channels
  718. current-channels
  719. #:validate-pull
  720. validate-pull
  721. #:authenticate?
  722. authenticate?)))
  723. (format (current-error-port)
  724. (N_ "Building from this channel:~%"
  725. "Building from these channels:~%"
  726. (length instances)))
  727. (for-each (lambda (instance)
  728. (let ((channel
  729. (channel-instance-channel instance)))
  730. (format (current-error-port)
  731. " ~10a~a\t~a~%"
  732. (channel-name channel)
  733. (channel-url channel)
  734. (string-take
  735. (channel-instance-commit instance)
  736. 7))))
  737. instances)
  738. (parameterize ((%guile-for-build
  739. (package-derivation
  740. store
  741. (if (assoc-ref opts 'bootstrap?)
  742. %bootstrap-guile
  743. (default-guile)))))
  744. (with-profile-lock profile
  745. (run-with-store store
  746. (build-and-install instances profile)))))))))))))))
  747. ;;; pull.scm ends here