pull.scm 36 KB

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