guix-play/guix/build/download-nar.scm
Christopher Baines b59f89cf18
guix: Improve download-nar.
Previously download-nar worked with gzipped nars and queried
berlin.guix.gnu.org (also known as ci.guix.gnu.org). ci.guix.gnu.org no longer
serves gzipped nars so this is of limited use.

This commit changes download-nar to query both the default substitute servers,
and queries for lzipped rather than gzipped nars, since those are available
from both.

* guix/build/download-nar.scm (urls-for-item): Return urls for lzip rather
than gzip compression, and from both default substitute servers. The comment
about CDN's is no longer relevant.
(restore-gzipped-nar): Rename to restore-lzipped-nar and reimplement
accordingly.
(download-nar): Add progress reporting and switch to use lzip rather than
gzip.
* guix/cvs-download.scm (cvs-fetch): Replace guile-zlib with guile-lzlib.
* guix/git-download.scm (git-fetch): Replace guile-zlib with guile-lzlib.
* guix/hg-download.scm (hg-fetch): Replace guile-zlib with guile-lzlib.
* guix/android-repo-download.scm (android-repo-fetch): Add guile-lzlib for
download-nar.

Signed-off-by: Christopher Baines <mail@cbaines.net>
2023-03-16 18:37:01 +00:00

102 lines
3.9 KiB
Scheme

;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;;
;;; 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
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; 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 build download-nar)
#:use-module (guix build download)
#:use-module ((guix serialization) #:hide (dump-port*))
#:autoload (lzlib) (call-with-lzip-input-port)
#:use-module (guix progress)
#:use-module (web uri)
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
#:use-module (ice-9 format)
#:use-module (ice-9 match)
#:export (download-nar))
;;; Commentary:
;;;
;;; Download a normalized archive or "nar", similar to what 'guix substitute'
;;; does. The intent here is to use substitute servers as content-addressed
;;; mirrors of VCS checkouts. This is mostly useful for users who have
;;; disabled substitutes.
;;;
;;; Code:
(define (urls-for-item item)
"Return the fallback nar URL for ITEM--e.g.,
\"/gnu/store/cabbag3…-foo-1.2-checkout\"."
;; Here we hard-code nar URLs without checking narinfos. That's probably OK
;; though.
;; TODO: Use HTTPS? The downside is the extra dependency.
(let ((bases '("http://bordeaux.guix.gnu.org"
"http://ci.guix.gnu.org"))
(item (basename item)))
(append (map (cut string-append <> "/nar/lzip/" item) bases)
(map (cut string-append <> "/nar/" item) bases))))
(define (restore-lzipped-nar port item size)
"Restore the lzipped nar read from PORT, of SIZE bytes (compressed), to
ITEM."
(call-with-lzip-input-port port
(lambda (decompressed-port)
(restore-file decompressed-port
item))))
(define (download-nar item)
"Download and extract the normalized archive for ITEM. Return #t on
success, #f otherwise."
;; Let progress reports go through.
(setvbuf (current-error-port) 'none)
(setvbuf (current-output-port) 'none)
(let loop ((urls (urls-for-item item)))
(match urls
((url rest ...)
(format #t "Trying content-addressed mirror at ~a...~%"
(uri-host (string->uri url)))
(let-values (((port size)
(catch #t
(lambda ()
(http-fetch (string->uri url)))
(lambda args
(values #f #f)))))
(if (not port)
(loop rest)
(let* ((reporter (progress-reporter/file
url
size
(current-error-port)
#:abbreviation nar-uri-abbreviation))
(port-with-progress
(progress-report-port reporter port
#:download-size size)))
(if size
(format #t "Downloading from ~a (~,2h MiB)...~%" url
(/ size (expt 2 20.)))
(format #t "Downloading from ~a...~%" url))
(if (string-contains url "/lzip")
(restore-lzipped-nar port-with-progress
item
size)
(begin
(restore-file port-with-progress
item)))
#t))))
(()
#f))))