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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
39 #:use-module (guix scripts build)
40 #:use-module (guix scripts describe)
41 #:autoload (guix build utils) (which)
42 #:use-module ((guix build syscalls)
43 #:select (with-file-lock/no-wait))
44 #:use-module (guix 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
70 ;;; Command-line options.
73 (define %default-options
74 ;; Alist of default option values.
75 `((system . ,(%current-system))
78 (print-build-trace? . #t)
79 (print-extended-build-trace? . #t)
80 (multiplexed-build-output? . #t)
84 (authenticate-channels? . #t)
85 (validate-pull . ,ensure-forward-channel-update)))
88 (display (G_ "Usage: guix pull [OPTION]...
89 Download and deploy the latest version of Guix.\n"))
91 -C, --channels=FILE deploy the channels defined in FILE"))
93 --url=URL download from the Git repository at URL"))
95 --commit=COMMIT download the specified COMMIT"))
97 --branch=BRANCH download the tip of the specified BRANCH"))
99 --allow-downgrades allow downgrades to earlier channel revisions"))
101 --disable-authentication
102 disable channel authentication"))
104 -N, --news display news compared to the previous generation"))
106 -l, --list-generations[=PATTERN]
107 list generations matching PATTERN"))
109 --roll-back roll back to the previous generation"))
111 -d, --delete-generations[=PATTERN]
112 delete generations matching PATTERN"))
114 -S, --switch-generation=PATTERN
115 switch to a generation matching PATTERN"))
117 -p, --profile=PROFILE use PROFILE instead of ~/.config/guix/current"))
119 -v, --verbosity=LEVEL use the given verbosity LEVEL"))
121 -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
123 --bootstrap use the bootstrap Guile to build the new Guix"))
125 (show-build-options-help)
127 -h, --help display this help and exit"))
129 -V, --version display version information and exit"))
131 (show-bug-report-information))
134 ;; Specifications of the command-line options.
135 (cons* (option '(#\C "channels") #t #f
136 (lambda (opt name arg result)
137 (alist-cons 'channel-file arg result)))
138 (option '(#\l "list-generations") #f #t
139 (lambda (opt name arg result)
140 (cons `(query list-generations ,arg)
142 (option '("roll-back") #f #f
143 (lambda (opt name arg result)
144 (cons '(generation roll-back)
146 (option '(#\S "switch-generation") #t #f
147 (lambda (opt name arg result)
148 (cons `(generation switch ,arg)
150 (option '(#\d "delete-generations") #f #t
151 (lambda (opt name arg result)
152 (cons `(generation delete ,arg)
154 (option '(#\N "news") #f #f
155 (lambda (opt name arg result)
156 (cons '(query display-news) result)))
157 (option '("url") #t #f
158 (lambda (opt name arg result)
159 (alist-cons 'repository-url arg
160 (alist-delete 'repository-url result))))
161 (option '("commit") #t #f
162 (lambda (opt name arg result)
163 (alist-cons 'ref `(commit . ,arg) result)))
164 (option '("branch") #t #f
165 (lambda (opt name arg result)
166 (alist-cons 'ref `(branch . ,arg) result)))
167 (option '("allow-downgrades") #f #f
168 (lambda (opt name arg result)
169 (alist-cons 'validate-pull warn-about-backward-updates
171 (option '("disable-authentication") #f #f
172 (lambda (opt name arg result)
173 (alist-cons 'authenticate-channels? #f result)))
174 (option '(#\p "profile") #t #f
175 (lambda (opt name arg result)
176 (alist-cons 'profile (canonicalize-profile arg)
178 (option '(#\s "system") #t #f
179 (lambda (opt name arg result)
180 (alist-cons 'system arg
181 (alist-delete 'system result eq?))))
182 (option '(#\n "dry-run") #f #f
183 (lambda (opt name arg result)
184 (alist-cons 'dry-run? #t result)))
185 (option '(#\v "verbosity") #t #f
186 (lambda (opt name arg result)
187 (let ((level (string->number* arg)))
188 (alist-cons 'verbosity level
189 (alist-delete 'verbosity result)))))
190 (option '("bootstrap") #f #f
191 (lambda (opt name arg result)
192 (alist-cons 'bootstrap? #t result)))
194 (option '(#\h "help") #f #f
198 (option '(#\V "version") #f #f
200 (show-version-and-exit "guix pull")))
202 %standard-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
208 ((or 'ancestor 'self)
211 (warning (G_ "rolling back channel '~a' from ~a to ~a~%")
212 (channel-name channel) start commit))
214 (warning (G_ "moving channel '~a' from ~a to unrelated commit ~a~%")
215 (channel-name channel) start commit))))
217 (define* (display-profile-news profile #:key concise?
219 "Display what's up in PROFILE--new packages, and all that. If
220 CURRENT-IS-NEWER? is true, assume that the current process represents the
221 newest generation of PROFILE. Return true when there's more info to display."
222 (match (memv (generation-number profile)
223 (reverse (profile-generations profile)))
224 ((current previous _ ...)
225 (let ((these (fold-available-packages
226 (lambda* (name version result
227 #:key supported? deprecated?
229 (if (and supported? (not deprecated?))
230 (alist-cons name version result)
233 (those (profile-package-alist
234 (generation-file-name profile
235 (if current-is-newer?
238 (let ((old (if current-is-newer? those these))
239 (new (if current-is-newer? these those)))
240 (display-new/upgraded-packages old new
243 (G_ "New in this revision:\n")))))
246 (define (display-channel channel)
247 "Display information about CHANNEL."
248 (format (current-error-port)
249 ;; TRANSLATORS: This describes a "channel"; the first placeholder is
250 ;; the channel name (e.g., "guix") and the second placeholder is its
253 (channel-name channel)
254 (channel-url channel)))
256 (define (channel=? channel1 channel2)
257 "Return true if CHANNEL1 and CHANNEL2 are the same for all practical
259 ;; Assume that the URL matters less than the name.
260 (eq? (channel-name channel1) (channel-name channel2)))
262 (define (display-news-entry-title entry language port)
263 "Display the title of ENTRY, a news entry, to PORT."
265 (channel-news-entry-title entry))
267 (let ((title (or (assoc-ref title language)
268 (assoc-ref title (%default-message-language))
275 (texi->plain-text title))
277 ;; When Texinfo markup is invalid, display it as-is.
280 (define (display-news-entry entry channel language port)
281 "Display ENTRY, a <channel-news-entry> from CHANNEL, in LANGUAGE, a language
284 (channel-news-entry-body entry))
287 (channel-news-entry-commit entry))
289 (display-news-entry-title entry language port)
290 (format port (dim (G_ " commit ~a~%"))
291 (if (supports-hyperlinks?)
292 (channel-commit-hyperlink channel commit)
295 (let ((body (or (assoc-ref body language)
296 (assoc-ref body (%default-message-language))
300 (parameterize ((%text-width (- (%text-width) 4)))
304 (texi->plain-text body))
306 ;; When Texinfo markup is invalid, display it as-is.
307 (fill-paragraph body (%text-width))))))
310 (define* (display-channel-specific-news new old
311 #:key (port (current-output-port))
313 "Display channel news applicable the commits between OLD and NEW, where OLD
314 and NEW are <channel> records with a proper 'commit' field. When CONCISE? is
315 true, display nothing but the news titles. Return true if there are more news
318 (old (channel-commit old))
319 (new (channel-commit new)))
321 (let ((language (current-message-language)))
322 (match (channel-news-for-commit channel new old)
323 (() ;no news is good news
327 (format port (G_ "News for channel '~a'~%")
328 (channel-name channel))
329 (for-each (if concise?
330 (cut display-news-entry-title <> language port)
331 (cut display-news-entry <> channel language port))
336 (define* (display-channel-news profile
339 (and=> (relative-generation profile -1)
340 (cut generation-file-name profile <>))))
341 "Display news about the channels of PROFILE compared to PREVIOUS."
343 (let ((old-channels (profile-channels previous))
344 (new-channels (profile-channels profile)))
345 (and (pair? old-channels) (pair? new-channels)
347 (match (lset-difference channel=? new-channels old-channels)
351 (let ((count (length new)))
352 (format (current-error-port)
353 (N_ " ~a new channel:~%"
354 " ~a new channels:~%" count)
356 (for-each display-channel new))))
357 (match (lset-difference channel=? old-channels new-channels)
361 (let ((count (length removed)))
362 (format (current-error-port)
363 (N_ " ~a channel removed:~%"
364 " ~a channels removed:~%" count)
366 (for-each display-channel removed))))
368 ;; Display channel-specific news for those channels that were
369 ;; here before and are still around afterwards.
370 (for-each (match-lambda
372 (display-channel-specific-news new old)))
373 (filter-map (lambda (new)
375 (find (cut channel=? new <>)
378 (and old (list new old)))
381 (define* (display-channel-news-headlines profile)
382 "Display the titles of news about the channels of PROFILE compared to its
383 previous generation. Return true if there are news to display."
385 (and=> (relative-generation profile -1)
386 (cut generation-file-name profile <>)))
389 (let ((old-channels (profile-channels previous))
390 (new-channels (profile-channels profile)))
391 ;; Find the channels present in both PROFILE and PREVIOUS, and print
393 (and (pair? old-channels) (pair? new-channels)
394 (let ((channels (filter-map (lambda (new)
396 (find (cut channel=? new <>)
399 (and old (list new old)))
404 (display-channel-specific-news new old
408 (any ->bool more?))))))
410 (define (display-news profile)
411 ;; Display profile news, with the understanding that this process represents
412 ;; the newest generation.
413 (display-profile-news profile
414 #:current-is-newer? #t)
416 (display-channel-news profile))
418 (define* (build-and-install instances profile)
419 "Build the tool from SOURCE, and install it in PROFILE. When DRY-RUN? is
420 true, display what would be built without actually building it."
421 (define update-profile
422 (store-lift build-and-use-profile))
425 ;; The 'guix' command before we've built the new profile.
428 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
430 (update-profile profile manifest
431 #:hooks %channel-profile-hooks)
434 (let ((more? (list (display-profile-news profile #:concise? #t)
435 (display-channel-news-headlines profile))))
437 (when (any ->bool more?)
439 (G_ "Run @command{guix pull --news} to read all the news.")))))
441 (let ((new (map (cut string-append <> "/bin/guix")
442 (list (user-friendly-profile profile)
444 ;; Is the 'guix' command previously in $PATH the same as the new
445 ;; one? If the answer is "no", then suggest 'hash guix'.
446 (unless (member guix-command new)
447 (display-hint (format #f (G_ "After setting @code{PATH}, run
448 @command{hash guix} to make sure your shell refers to @file{~a}.")
453 (define (honor-lets-encrypt-certificates! store)
454 "Tell Guile-Git to use the Let's Encrypt certificates."
455 (let* ((drv (package-derivation store le-certs))
456 (certs (string-append (derivation->output-path drv)
458 (build-derivations store (list drv))
459 (set-tls-certificate-locations! certs)))
461 (define (honor-x509-certificates store)
462 "Use the right X.509 certificates for Git checkouts over HTTPS."
463 (unless (honor-system-x509-certificates!)
464 (honor-lets-encrypt-certificates! store)))
471 (define %current-profile
472 ;; The "real" profile under /var/guix.
473 (string-append %profile-directory "/current-guix"))
475 (define %user-profile-directory
476 ;; The user-friendly name of %CURRENT-PROFILE.
477 (string-append (config-directory #:ensure? #f) "/current"))
479 (define (migrate-generations profile directory)
480 "Migrate the generations of PROFILE to DIRECTORY."
481 (format (current-error-port)
482 (G_ "Migrating profile generations to '~a'...~%")
484 (let ((current (generation-number profile)))
485 (for-each (lambda (generation)
486 (let ((source (generation-file-name profile generation))
487 (target (string-append directory "/current-guix-"
488 (number->string generation)
490 ;; Note: Don't use 'rename-file' as SOURCE and TARGET might
491 ;; live on different file systems.
492 (symlink (readlink source) target)
493 (delete-file source)))
494 (profile-generations profile))
495 (symlink (string-append "current-guix-"
496 (number->string current) "-link")
497 (string-append directory "/current-guix"))))
499 (define (ensure-default-profile)
500 (ensure-profile-directory)
502 ;; In 0.15.0+ we'd create ~/.config/guix/current-[0-9]*-link symlinks. Move
503 ;; them to %PROFILE-DIRECTORY.
505 ;; XXX: Ubuntu's 'sudo' preserves $HOME by default, and thus the second
506 ;; condition below is always false when one runs "sudo guix pull". As a
507 ;; workaround, skip this code when $SUDO_USER is set. See
508 ;; <https://bugs.gnu.org/36785>.
509 (unless (or (getenv "SUDO_USER")
510 (not (file-exists? %user-profile-directory))
511 (string=? %profile-directory
513 (canonicalize-profile %user-profile-directory))))
514 (migrate-generations %user-profile-directory %profile-directory))
516 ;; Make sure ~/.config/guix/current points to /var/guix/profiles/….
517 (let ((link %user-profile-directory))
518 (unless (equal? (false-if-exception (readlink link))
522 (false-if-exception (delete-file link))
523 (symlink %current-profile link))
525 (leave (G_ "while creating symlink '~a': ~a~%")
526 link (strerror (system-error-errno args))))))))
533 (define profile-package-alist
535 "Return a name/version alist representing the packages in PROFILE."
536 (let* ((inferior (open-inferior profile))
537 (packages (inferior-available-packages inferior)))
538 (close-inferior inferior)
541 (define (new/upgraded-packages alist1 alist2)
542 "Compare ALIST1 and ALIST2, both of which are lists of package name/version
543 pairs, and return two values: the list of packages new in ALIST2, and the list
544 of packages upgraded in ALIST2."
545 (let* ((old (fold (match-lambda*
546 (((name . version) table)
547 (match (vhash-assoc name table)
549 (vhash-cons name version table))
550 ((_ . previous-version)
551 (if (version>? version previous-version)
552 (vhash-cons name version table)
556 (new (remove (match-lambda
558 (vhash-assoc name old)))
560 (upgraded (filter-map (match-lambda
561 ((name . new-version)
562 (match (vhash-assoc name old)
565 (and (version>? new-version old-version)
566 (string-append name "@"
569 (values new upgraded)))
571 (define* (ellipsis #:optional (port (current-output-port)))
572 "Return HORIZONTAL ELLIPSIS three dots if PORT's encoding cannot represent
574 (match (port-encoding port)
578 (define* (display-new/upgraded-packages alist1 alist2
579 #:key (heading "") concise?)
580 "Given the two package name/version alists ALIST1 and ALIST2, display the
581 list of new and upgraded packages going from ALIST1 to ALIST2. When ALIST1
582 and ALIST2 differ, display HEADING upfront. When CONCISE? is true, do not
583 display long package lists that would fill the user's screen.
585 Return true when there is more package info to display."
586 (define (pretty str column)
587 (indented-string (fill-paragraph str (- (%text-width) 4)
589 4 #:initial-indent? #f))
591 (define concise/max-item-count
592 ;; Maximum number of items to display when CONCISE? is true.
595 (define list->enumeration
597 (lambda* (lst #:optional (max concise/max-item-count))
598 (if (> (length lst) max)
599 (string-append (string-join (take lst max) ", ")
601 (string-join lst ", ")))
602 (cut string-join <> ", ")))
604 (let-values (((new upgraded) (new/upgraded-packages alist1 alist2)))
605 (define new-count (length new))
606 (define upgraded-count (length upgraded))
608 (unless (and (null? new) (null? upgraded))
614 (format #t (N_ " ~h new package: ~a~%"
615 " ~h new packages: ~a~%" count)
617 (pretty (list->enumeration (sort (map first new) string<?))
619 (match upgraded-count
622 (format #t (N_ " ~h package upgraded: ~a~%"
623 " ~h packages upgraded: ~a~%" count)
625 (pretty (list->enumeration (sort upgraded string<?))
629 (or (> new-count concise/max-item-count)
630 (> upgraded-count concise/max-item-count)))))
632 (define (display-profile-content-diff profile gen1 gen2)
633 "Display the changes in PROFILE GEN2 compared to generation GEN1."
634 (define (package-alist generation)
635 (profile-package-alist (generation-file-name profile generation)))
637 (display-profile-content profile gen2)
638 (display-new/upgraded-packages (package-alist gen1)
639 (package-alist gen2)))
641 (define (process-query opts profile)
642 "Process any query on PROFILE specified by OPTS."
643 (match (assoc-ref opts 'query)
644 (('list-generations pattern)
645 (define (list-generations profile numbers)
648 (display-profile-content profile first)
649 (let loop ((numbers numbers))
651 ((first second rest ...)
652 (display-profile-content-diff profile
654 (display-channel-news (generation-file-name profile second)
655 (generation-file-name profile first))
656 (loop (cons second rest)))
661 (cond ((not (file-exists? profile)) ; XXX: race condition
662 (raise (condition (&profile-not-found-error
663 (profile profile)))))
665 (list-generations profile (profile-generations profile)))
666 ((matching-generations pattern profile)
672 (list-generations profile numbers)))))))
674 (display-news profile))))
676 (define (process-generation-change opts profile)
677 "Process a request to change the current generation (roll-back, switch, delete)."
678 (unless (assoc-ref opts 'dry-run?)
679 (match (assoc-ref opts 'generation)
682 (roll-back* store profile)))
684 (let ((number (relative-generation-spec->number profile pattern)))
686 (switch-to-generation* profile number)
687 (leave (G_ "cannot switch to generation '~a'~%") pattern))))
690 (delete-matching-generations store profile pattern))))))
692 (define (channel-list opts)
693 "Return the list of channels to use. If OPTS specify a channel file,
694 channels are read from there; otherwise, if ~/.config/guix/channels.scm
695 exists, read it; otherwise %DEFAULT-CHANNELS is used. Apply channel
696 transformations specified in OPTS (resulting from '--url', '--commit', or
697 '--branch'), if any."
699 (assoc-ref opts 'channel-file))
702 (string-append (config-directory) "/channels.scm"))
705 (string-append %sysconfdir "/guix/channels.scm"))
707 (define (load-channels file)
708 (let ((result (load* file (make-user-module '((guix channels))))))
709 (if (and (list? result) (every channel? result))
711 (leave (G_ "'~a' did not return a list of channels~%") file))))
715 (load-channels file))
716 ((file-exists? default-file)
717 (load-channels default-file))
718 ((file-exists? global-file)
719 (load-channels global-file))
723 (define (environment-variable)
724 (match (getenv "GUIX_PULL_URL")
727 (warning (G_ "The 'GUIX_PULL_URL' environment variable is deprecated.
728 Use '~/.config/guix/channels.scm' instead."))
731 (let ((ref (assoc-ref opts 'ref))
732 (url (or (assoc-ref opts 'repository-url)
733 (environment-variable))))
735 (match (find guix-channel? channels)
737 ;; Apply '--url', '--commit', and '--branch' to the 'guix' channel.
738 (let ((url (or url (channel-url guix))))
741 (channel (inherit guix)
742 (url url) (commit commit) (branch #f)))
744 (channel (inherit guix)
745 (url url) (commit #f) (branch branch)))
747 (channel (inherit guix) (url url))))
748 (remove guix-channel? channels))))
749 (#f ;no 'guix' channel, failure will ensue
754 (define-command (guix-pull . args)
755 (synopsis "pull the latest revision of Guix")
758 (with-git-error-handling
759 (let* ((opts (parse-command-line args %options
760 (list %default-options)))
761 (substitutes? (assoc-ref opts 'substitutes?))
762 (dry-run? (assoc-ref opts 'dry-run?))
763 (channels (channel-list opts))
764 (profile (or (assoc-ref opts 'profile) %current-profile))
765 (current-channels (profile-channels profile))
766 (validate-pull (assoc-ref opts 'validate-pull))
767 (authenticate? (assoc-ref opts 'authenticate-channels?)))
768 (cond ((assoc-ref opts 'query)
769 (process-query opts profile))
770 ((assoc-ref opts 'generation)
771 (process-generation-change opts profile))
774 (with-status-verbosity (assoc-ref opts 'verbosity)
775 (parameterize ((%current-system (assoc-ref opts 'system))
776 (%graft? (assoc-ref opts 'graft?)))
777 (with-build-handler (build-notifier #:use-substitutes?
780 (assoc-ref opts 'verbosity)
782 (set-build-options-from-command-line store opts)
783 (ensure-default-profile)
784 (honor-x509-certificates store)
787 (latest-channel-instances store channels
794 (format (current-error-port)
795 (N_ "Building from this channel:~%"
796 "Building from these channels:~%"
798 (for-each (lambda (instance)
800 (channel-instance-channel instance)))
801 (format (current-error-port)
803 (channel-name channel)
804 (channel-url channel)
806 (channel-instance-commit instance)
809 (parameterize ((%guile-for-build
812 (if (assoc-ref opts 'bootstrap?)
815 (with-profile-lock profile
816 (run-with-store store
817 (build-and-install instances profile)))))))))))))))
819 ;;; pull.scm ends here