Mirror of GNU Guix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
18 KiB

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013, 2014, 2015, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
;;; This file is part of GNU Guix.
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; GNU General Public License for more details.
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (guix scripts pull)
#:use-module (guix ui)
#:use-module (guix utils)
#:use-module (guix scripts)
#:use-module (guix store)
#:use-module (guix config)
#:use-module (guix packages)
#:use-module (guix derivations)
#:use-module (guix profiles)
#:use-module (guix gexp)
#:use-module (guix grafts)
#:use-module (guix memoization)
#:use-module (guix monads)
#:use-module (guix channels)
#:autoload (guix inferior) (open-inferior)
#:use-module (guix scripts build)
#:use-module (guix git)
#:use-module (git)
#:use-module (gnu packages)
#:use-module ((guix scripts package) #:select (build-and-use-profile))
#:use-module (gnu packages base)
#:use-module (gnu packages guile)
#:use-module ((gnu packages bootstrap)
#:select (%bootstrap-guile))
#:use-module ((gnu packages certs) #:select (le-certs))
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-35)
#:use-module (srfi srfi-37)
#:use-module (ice-9 match)
#:use-module (ice-9 vlist)
#:export (guix-pull))
;;; Command-line options.
(define %default-options
;; Alist of default option values.
`((system . ,(%current-system))
(substitutes? . #t)
(build-hook? . #t)
(graft? . #t)
(verbosity . 0)))
(define (show-help)
(display (G_ "Usage: guix pull [OPTION]...
Download and deploy the latest version of Guix.\n"))
(display (G_ "
--verbose produce verbose output"))
(display (G_ "
-C, --channels=FILE deploy the channels defined in FILE"))
(display (G_ "
--url=URL download from the Git repository at URL"))
(display (G_ "
--commit=COMMIT download the specified COMMIT"))
(display (G_ "
--branch=BRANCH download the tip of the specified BRANCH"))
(display (G_ "
-l, --list-generations[=PATTERN]
list generations matching PATTERN"))
(display (G_ "
--bootstrap use the bootstrap Guile to build the new Guix"))
(display (G_ "
-h, --help display this help and exit"))
(display (G_ "
-V, --version display version information and exit"))
(define %options
;; Specifications of the command-line options.
(cons* (option '("verbose") #f #f
(lambda (opt name arg result)
(alist-cons 'verbose? #t result)))
(option '(#\C "channels") #t #f
(lambda (opt name arg result)
(alist-cons 'channel-file arg result)))
(option '(#\l "list-generations") #f #t
(lambda (opt name arg result)
(cons `(query list-generations ,(or arg ""))
(option '("url") #t #f
(lambda (opt name arg result)
(alist-cons 'repository-url arg
(alist-delete 'repository-url result))))
(option '("commit") #t #f
(lambda (opt name arg result)
(alist-cons 'ref `(commit . ,arg) result)))
(option '("branch") #t #f
(lambda (opt name arg result)
(alist-cons 'ref `(branch . ,(string-append "origin/" arg))
(option '(#\n "dry-run") #f #f
(lambda (opt name arg result)
(alist-cons 'dry-run? #t (alist-cons 'graft? #f result))))
(option '("bootstrap") #f #f
(lambda (opt name arg result)
(alist-cons 'bootstrap? #t result)))
(option '(#\h "help") #f #f
(lambda args
(exit 0)))
(option '(#\V "version") #f #f
(lambda args
(show-version-and-exit "guix pull")))
(define what-to-build
(store-lift show-what-to-build))
(define indirect-root-added
(store-lift add-indirect-root))
(define (display-profile-news profile)
"Display what's up in PROFILE--new packages, and all that."
(match (memv (generation-number profile)
(reverse (profile-generations profile)))
((current previous _ ...)
(let ((old (fold-packages (lambda (package result)
(alist-cons (package-name package)
(package-version package)
(new (profile-package-alist
(generation-file-name profile current))))
(display-new/upgraded-packages old new
#:heading (G_ "New in this revision:\n"))))
(_ #t)))
(define* (build-and-install instances config-dir
#:key verbose?)
"Build the tool from SOURCE, and install it in CONFIG-DIR."
(define update-profile
(store-lift build-and-use-profile))
(define profile
(string-append config-dir "/current"))
(mlet %store-monad ((manifest (channel-instances->manifest instances)))
(mbegin %store-monad
(update-profile profile manifest)
(return (display-profile-news profile)))))
(define (honor-lets-encrypt-certificates! store)
"Tell Guile-Git to use the Let's Encrypt certificates."
(let* ((drv (package-derivation store le-certs))
(certs (string-append (derivation->output-path drv)
(build-derivations store (list drv))
;; In the past Guile-Git would not provide this procedure.
(if (module-defined? (resolve-interface '(git))
(set-tls-certificate-locations! certs)
;; In this case we end up using whichever certificates OpenSSL
;; chooses to use: $SSL_CERT_FILE, $SSL_CERT_DIR, or /etc/ssl/certs.
(warning (G_ "cannot enforce use of the Let's Encrypt \
(warning (G_ "please upgrade Guile-Git~%"))))))
(define (report-git-error error)
"Report the given Guile-Git error."
;; Prior to Guile-Git commit b6b2760c2fd6dfaa5c0fedb43eeaff06166b3134,
;; errors would be represented by integers.
(match error
((? integer? error) ;old Guile-Git
(leave (G_ "Git error ~a~%") error))
((? git-error? error) ;new Guile-Git
(leave (G_ "Git error: ~a~%") (git-error-message error)))))
(define-syntax-rule (with-git-error-handling body ...)
(catch 'git-error
(lambda ()
body ...)
(lambda (key err)
(report-git-error err))))
;;; Queries.
(define (display-profile-content profile number)
"Display the packages in PROFILE, generation NUMBER, in a human-readable
way and displaying details about the channel's source code."
(display-generation profile number)
(for-each (lambda (entry)
(format #t " ~a ~a~%"
(manifest-entry-name entry)
(manifest-entry-version entry))
(match (assq 'source (manifest-entry-properties entry))
(('source ('repository ('version 0)
('url url)
('branch branch)
('commit commit)
_ ...))
(format #t (G_ " repository URL: ~a~%") url)
(when branch
(format #t (G_ " branch: ~a~%") branch))
(format #t (G_ " commit: ~a~%") commit))
(_ #f)))
;; Show most recently installed packages last.
(profile-manifest (generation-file-name profile number))))))
(define (indented-string str indent)
"Return STR with each newline preceded by IDENT spaces."
(define indent-string
(make-list indent #\space))
(string-fold-right (lambda (chr result)
(if (eqv? chr #\newline)
(cons chr (append indent-string result))
(cons chr result)))
(define profile-package-alist
(mlambda (profile)
"Return a name/version alist representing the packages in PROFILE."
(fold (lambda (package lst)
(alist-cons (inferior-package-name package)
(inferior-package-version package)
(let* ((inferior (open-inferior profile))
(packages (inferior-packages inferior)))
(close-inferior inferior)
(define* (display-new/upgraded-packages alist1 alist2
#:key (heading ""))
"Given the two package name/version alists ALIST1 and ALIST2, display the
list of new and upgraded packages going from ALIST1 to ALIST2. When ALIST1
and ALIST2 differ, display HEADING upfront."
(let* ((old (fold (match-lambda*
(((name . version) table)
(vhash-cons name version table)))
(new (remove (match-lambda
((name . _)
(vhash-assoc name old)))
(upgraded (filter-map (match-lambda
((name . new-version)
(match (vhash-fold* cons '() name old)
(() #f)
((= (cut sort <> version>?) old-versions)
(and (version>? new-version
(first old-versions))
(string-append name "@"
(unless (and (null? new) (null? upgraded))
(display heading))
(match (length new)
(0 #t)
(format #t (N_ " ~h new package: ~a~%"
" ~h new packages: ~a~%" count)
(fill-paragraph (string-join (sort (map first new) string<?)
", ")
(- (%text-width) 4) 30)
(match (length upgraded)
(0 #t)
(format #t (N_ " ~h package upgraded: ~a~%"
" ~h packages upgraded: ~a~%" count)
(fill-paragraph (string-join (sort upgraded string<?) ", ")
(- (%text-width) 4) 35)
(define (display-profile-content-diff profile gen1 gen2)
"Display the changes in PROFILE GEN2 compared to generation GEN1."
(define (package-alist generation)
(profile-package-alist (generation-file-name profile generation)))
(display-profile-content profile gen2)
(display-new/upgraded-packages (package-alist gen1)
(package-alist gen2)))
(define (process-query opts)
"Process any query specified by OPTS."
(define profile
(string-append (config-directory) "/current"))
(match (assoc-ref opts 'query)
(('list-generations pattern)
(define (list-generations profile numbers)
(match numbers
((first rest ...)
(display-profile-content profile first)
(let loop ((numbers numbers))
(match numbers
((first second rest ...)
(display-profile-content-diff profile
first second)
(loop (cons second rest)))
((_) #t)
(() #t))))))
(cond ((not (file-exists? profile)) ; XXX: race condition
(raise (condition (&profile-not-found-error
(profile profile)))))
((string-null? pattern)
(list-generations profile (profile-generations profile)))
((matching-generations pattern profile)
(exit 1))
((numbers ...)
(list-generations profile numbers)))))))))
(define (channel-list opts)
"Return the list of channels to use. If OPTS specify a channel file,
channels are read from there; otherwise, if ~/.config/guix/channels.scm
exists, read it; otherwise %DEFAULT-CHANNELS is used. Apply channel
transformations specified in OPTS (resulting from '--url', '--commit', or
'--branch'), if any."
(define file
(assoc-ref opts 'channel-file))
(define default-file
(string-append (config-directory) "/channels.scm"))
(define (load-channels file)
(let ((result (load* file (make-user-module '((guix channels))))))
(if (and (list? result) (every channel? result))
(leave (G_ "'~a' did not return a list of channels~%") file))))
(define channels
(cond (file
(load-channels file))
((file-exists? default-file)
(load-channels default-file))
(define (environment-variable)
(match (getenv "GUIX_PULL_URL")
(#f #f)
(warning (G_ "The 'GUIX_PULL_URL' environment variable is deprecated.
Use '~/.config/guix/channels.scm' instead."))
(let ((ref (assoc-ref opts 'ref))
(url (or (assoc-ref opts 'repository-url)
(if (or ref url)
(match channels
;; When there's only one channel, apply '--url', '--commit', and
;; '--branch' to this specific channel.
(let ((url (or url (channel-url one))))
(list (match ref
(('commit . commit)
(channel (inherit one)
(url url) (commit commit) (branch #f)))
(('branch . branch)
(channel (inherit one)
(url url) (commit #f) (branch branch)))
(channel (inherit one) (url url)))))))
;; Otherwise bail out.
(G_ "'--url', '--commit', and '--branch' are not applicable~%"))))
(define (guix-pull . args)
(let* ((opts (parse-command-line args %options
(list %default-options)))
(cache (string-append (cache-directory) "/pull"))
(channels (channel-list opts)))
(cond ((assoc-ref opts 'query)
(process-query opts))
((assoc-ref opts 'dry-run?)
#t) ;XXX: not very useful
(with-store store
(parameterize ((%graft? (assoc-ref opts 'graft?))
(%repository-cache-directory cache))
(set-build-options-from-command-line store opts)
;; When certificates are already installed, use them.
;; Otherwise, use the Let's Encrypt certificates, which we
;; know Savannah uses.
(let ((certs (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
(unless (file-exists? certs)
(honor-lets-encrypt-certificates! store)))
(let ((instances (latest-channel-instances store channels)))
(format (current-error-port)
(N_ "Building from this channel:~%"
"Building from these channels:~%"
(length instances)))
(for-each (lambda (instance)
(let ((channel
(channel-instance-channel instance)))
(format (current-error-port)
" ~10a~a\t~a~%"
(channel-name channel)
(channel-url channel)
(channel-instance-commit instance)
(parameterize ((%guile-for-build
(if (assoc-ref opts 'bootstrap?)
(canonical-package guile-2.2)))))
(run-with-store store
(build-and-install instances (config-directory)
(assoc-ref opts 'verbose?)))))))))))))
;;; pull.scm ends here