1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
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 substitute)
21 #:use-module (guix ui)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix config)
25 #:use-module (guix records)
26 #:use-module (guix serialization)
27 #:use-module (guix hash)
28 #:use-module (guix base64)
29 #:use-module (guix pk-crypto)
30 #:use-module (guix pki)
31 #:use-module ((guix build utils) #:select (mkdir-p dump-port))
32 #:use-module ((guix build download)
33 #:select (progress-proc uri-abbreviation))
34 #:use-module (ice-9 rdelim)
35 #:use-module (ice-9 regex)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (ice-9 ftw)
39 #:use-module (ice-9 binary-ports)
40 #:use-module (rnrs io ports)
41 #:use-module (rnrs bytevectors)
42 #:use-module (srfi srfi-1)
43 #:use-module (srfi srfi-9)
44 #:use-module (srfi srfi-11)
45 #:use-module (srfi srfi-19)
46 #:use-module (srfi srfi-26)
47 #:use-module (srfi srfi-34)
48 #:use-module (srfi srfi-35)
49 #:use-module (web uri)
50 #:use-module (web request)
51 #:use-module (web response)
52 #:use-module (guix http-client)
53 #:export (narinfo-signature->canonical-sexp
60 ;;; This is the "binary substituter". It is invoked by the daemon do check
61 ;;; for the existence of available "substitutes" (pre-built binaries), and to
62 ;;; actually use them as a substitute to building things locally.
64 ;;; If possible, substitute a binary for the requested store path, using a Nix
65 ;;; "binary cache". This program implements the Nix "substituter" protocol.
69 (define %narinfo-cache-directory
70 ;; A local cache of narinfos, to avoid going to the network.
71 (or (and=> (getenv "XDG_CACHE_HOME")
72 (cut string-append <> "/guix/substitute"))
73 (string-append %state-directory "/substitute/cache")))
75 (define %allow-unauthenticated-substitutes?
76 ;; Whether to allow unchecked substitutes. This is useful for testing
77 ;; purposes, and should be avoided otherwise.
78 (and (and=> (getenv "GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES")
79 (cut string-ci=? <> "yes"))
81 (warning (_ "authentication and authorization of substitutes \
86 ;; Number of seconds during which cached narinfo lookups are considered
87 ;; valid. This is a reasonable default value (corresponds to the TTL for
88 ;; nginx's .nar cache on hydra.gnu.org) but we'd rather want publishers to
89 ;; state what their TTL is in /nix-cache-info. (XXX)
92 (define %narinfo-negative-ttl
93 ;; Likewise, but for negative lookups---i.e., cached lookup failures.
96 (define %narinfo-expired-cache-entry-removal-delay
97 ;; How often we want to remove files corresponding to expired cache entries.
100 (define fields->alist
101 ;; The narinfo format is really just like recutils.
104 (define %fetch-timeout
105 ;; Number of seconds after which networking is considered "slow".
108 (define %random-state
109 (seed->random-state (+ (ash (cdr (gettimeofday)) 32) (getpid))))
111 (define-syntax-rule (with-timeout duration handler body ...)
112 "Run BODY; when DURATION seconds have expired, call HANDLER, and run BODY
117 (sigaction SIGALRM SIG_DFL)
127 ;; Before Guile v2.0.9-39-gfe51c7b, the SIGALRM triggers EINTR
128 ;; because of the bug at
129 ;; <http://lists.gnu.org/archive/html/guile-devel/2013-06/msg00050.html>.
130 ;; When that happens, try again. Note: SA_RESTART cannot be
131 ;; used because of <http://bugs.gnu.org/14640>.
132 (if (= EINTR (system-error-errno args))
134 ;; Wait a little to avoid bursts.
135 (usleep (random 3000000 %random-state))
137 (apply throw args))))))
140 (sigaction SIGALRM SIG_DFL)
141 (apply values result)))))
143 (define* (fetch uri #:key (buffered? #t) (timeout? #t) (quiet-404? #f))
144 "Return a binary input port to URI and the number of bytes it's expected to
145 provide. If QUIET-404? is true, HTTP 404 error conditions are passed through
146 to the caller without emitting an error message."
147 (case (uri-scheme uri)
149 (let ((port (open-file (uri-path uri)
150 (if buffered? "rb" "r0b"))))
151 (values port (stat:size (stat port)))))
153 (guard (c ((http-get-error? c)
154 (let ((code (http-get-error-code c)))
155 (if (and (= code 404) quiet-404?)
157 (leave (_ "download from '~a' failed: ~a, ~s~%")
158 (uri->string (http-get-error-uri c))
159 code (http-get-error-reason c))))))
161 ;; sudo tc qdisc add dev eth0 root netem delay 1500ms
162 ;; and then cancel with:
163 ;; sudo tc qdisc del dev eth0 root
165 (with-timeout (if timeout?
169 (warning (_ "while fetching ~a: server is somewhat slow~%")
171 (warning (_ "try `--no-substitutes' if the problem persists~%"))
173 ;; Before Guile v2.0.9-39-gfe51c7b, EINTR was reported to the user,
174 ;; and thus PORT had to be closed and re-opened. This is not the
176 (unless (or (guile-version>? "2.0.9")
177 (version>? (version) "2.0.9.39"))
181 (when (or (not port) (port-closed? port))
182 (set! port (open-socket-for-uri uri))
184 (setvbuf port _IONBF)))
185 (http-fetch uri #:text? #f #:port port))))))))
187 (define-record-type <cache>
188 (%make-cache url store-directory wants-mass-query?)
191 (store-directory cache-store-directory)
192 (wants-mass-query? cache-wants-mass-query?))
194 (define (open-cache url)
195 "Open the binary cache at URL. Return a <cache> object on success, or #f on
197 (define (download-cache-info url)
198 ;; Download the `nix-cache-info' from URL, and return its contents as an
199 ;; list of key/value pairs.
200 (and=> (false-if-exception (fetch (string->uri url)))
203 (and=> (download-cache-info (string-append url "/nix-cache-info"))
205 (alist->record properties
206 (cut %make-cache url <...>)
207 '("StoreDir" "WantMassQuery")))))
209 (define-syntax-rule (open-cache* url)
210 "Delayed variant of 'open-cache' that also lets the user know that they're
213 (format (current-error-port)
214 (_ "updating list of substitutes from '~a'...\r")
218 (define-record-type <narinfo>
219 (%make-narinfo path uri uri-base compression file-hash file-size nar-hash nar-size
220 references deriver system signature contents)
224 (uri-base narinfo-uri-base) ; URI of the cache it originates from
225 (compression narinfo-compression)
226 (file-hash narinfo-file-hash)
227 (file-size narinfo-file-size)
228 (nar-hash narinfo-hash)
229 (nar-size narinfo-size)
230 (references narinfo-references)
231 (deriver narinfo-deriver)
232 (system narinfo-system)
233 (signature narinfo-signature) ; canonical sexp
234 ;; The original contents of a narinfo file. This field is needed because we
235 ;; want to preserve the exact textual representation for verification purposes.
236 ;; See <https://lists.gnu.org/archive/html/guix-devel/2014-02/msg00340.html>
237 ;; for more information.
238 (contents narinfo-contents))
240 (define (narinfo-signature->canonical-sexp str)
241 "Return the value of a narinfo's 'Signature' field as a canonical sexp."
242 (match (string-split str #\;)
244 (let ((maybe-number (string->number version)))
245 (cond ((not (number? maybe-number))
246 (leave (_ "signature version must be a number: ~s~%")
248 ;; Currently, there are no other versions.
249 ((not (= 1 maybe-number))
250 (leave (_ "unsupported signature version: ~a~%")
253 (let ((signature (utf8->string (base64-decode sig))))
256 (string->canonical-sexp signature))
257 (lambda (key proc err)
258 (leave (_ "signature is not a valid \
262 (leave (_ "invalid format of the signature field: ~a~%") x))))
264 (define (narinfo-maker str cache-url)
265 "Return a narinfo constructor for narinfos originating from CACHE-URL. STR
266 must contain the original contents of a narinfo file."
267 (lambda (path url compression file-hash file-size nar-hash nar-size
268 references deriver system signature)
269 "Return a new <narinfo> object."
271 ;; Handle the case where URL is a relative URL.
272 (or (string->uri url)
273 (string->uri (string-append cache-url "/" url)))
276 compression file-hash
277 (and=> file-size string->number)
279 (and=> nar-size string->number)
280 (string-tokenize references)
286 (and=> signature narinfo-signature->canonical-sexp))
289 (define* (assert-valid-signature narinfo signature hash
290 #:optional (acl (current-acl)))
291 "Bail out if SIGNATURE, a canonical sexp representing the signature of
292 NARINFO, doesn't match HASH, a bytevector containing the hash of NARINFO."
293 (let ((uri (uri->string (narinfo-uri narinfo))))
294 (signature-case (signature hash acl)
297 (leave (_ "invalid signature for '~a'~%") uri))
299 (leave (_ "hash mismatch for '~a'~%") uri))
301 (leave (_ "'~a' is signed with an unauthorized key~%") uri))
303 (leave (_ "signature on '~a' is corrupt~%") uri)))))
305 (define* (read-narinfo port #:optional url
307 "Read a narinfo from PORT. If URL is true, it must be a string used to
308 build full URIs from relative URIs found while reading PORT. When SIZE is
309 true, read at most SIZE bytes from PORT; otherwise, read as much as possible.
311 No authentication and authorization checks are performed here!"
312 (let ((str (utf8->string (if size
313 (get-bytevector-n port size)
314 (get-bytevector-all port)))))
315 (alist->record (call-with-input-string str fields->alist)
316 (narinfo-maker str url)
317 '("StorePath" "URL" "Compression"
318 "FileHash" "FileSize" "NarHash" "NarSize"
319 "References" "Deriver" "System"
322 (define (narinfo-sha256 narinfo)
323 "Return the sha256 hash of NARINFO as a bytevector, or #f if NARINFO lacks a
325 (let ((contents (narinfo-contents narinfo)))
326 (match (string-contains contents "Signature:")
329 (let ((above-signature (string-take contents index)))
330 (sha256 (string->utf8 above-signature)))))))
332 (define* (assert-valid-narinfo narinfo
333 #:optional (acl (current-acl))
335 "Raise an exception if NARINFO lacks a signature, has an invalid signature,
336 or is signed by an unauthorized key."
337 (let ((hash (narinfo-sha256 narinfo)))
339 (if %allow-unauthenticated-substitutes?
341 (leave (_ "substitute at '~a' lacks a signature~%")
342 (uri->string (narinfo-uri narinfo))))
343 (let ((signature (narinfo-signature narinfo)))
344 (unless %allow-unauthenticated-substitutes?
345 (assert-valid-signature narinfo signature hash acl)
347 (format (current-error-port)
348 "found valid signature for '~a', from '~a'~%"
349 (narinfo-path narinfo)
350 (uri->string (narinfo-uri narinfo)))))
353 (define* (valid-narinfo? narinfo #:optional (acl (current-acl)))
354 "Return #t if NARINFO's signature is not valid."
355 (or %allow-unauthenticated-substitutes?
356 (let ((hash (narinfo-sha256 narinfo))
357 (signature (narinfo-signature narinfo)))
359 (signature-case (signature hash acl)
363 (define (write-narinfo narinfo port)
364 "Write NARINFO to PORT."
365 (put-bytevector port (string->utf8 (narinfo-contents narinfo))))
367 (define (narinfo->string narinfo)
368 "Return the external representation of NARINFO."
369 (call-with-output-string (cut write-narinfo narinfo <>)))
371 (define (string->narinfo str cache-uri)
372 "Return the narinfo represented by STR. Assume CACHE-URI as the base URI of
373 the cache STR originates form."
374 (call-with-input-string str (cut read-narinfo <> cache-uri)))
376 (define (obsolete? date now ttl)
377 "Return #t if DATE is obsolete compared to NOW + TTL seconds."
378 (time>? (subtract-duration now (make-time time-duration 0 ttl))
379 (make-time time-monotonic 0 date)))
382 (define (narinfo-cache-file path)
383 "Return the name of the local file that contains an entry for PATH."
384 (string-append %narinfo-cache-directory "/"
385 (store-path-hash-part path)))
387 (define (cached-narinfo path)
388 "Check locally if we have valid info about PATH. Return two values: a
389 Boolean indicating whether we have valid cached info, and that info, which may
390 be either #f (when PATH is unavailable) or the narinfo for PATH."
392 (current-time time-monotonic))
395 (narinfo-cache-file path))
399 (call-with-input-file cache-file
402 (('narinfo ('version 1)
403 ('cache-uri cache-uri)
404 ('date date) ('value #f))
405 ;; A cached negative lookup.
406 (if (obsolete? date now %narinfo-negative-ttl)
409 (('narinfo ('version 1)
410 ('cache-uri cache-uri)
411 ('date date) ('value value))
412 ;; A cached positive lookup
413 (if (obsolete? date now %narinfo-ttl)
415 (values #t (string->narinfo value cache-uri))))
416 (('narinfo ('version v) _ ...)
421 (define (cache-narinfo! cache path narinfo)
422 "Cache locally NARNIFO for PATH, which originates from CACHE. NARINFO may
423 be #f, in which case it indicates that PATH is unavailable at CACHE."
425 (current-time time-monotonic))
427 (define (cache-entry cache-uri narinfo)
428 `(narinfo (version 1)
429 (cache-uri ,cache-uri)
430 (date ,(time-second now))
431 (value ,(and=> narinfo narinfo->string))))
433 (with-atomic-file-output (narinfo-cache-file path)
435 (write (cache-entry (cache-url cache) narinfo) out)))
438 (define (narinfo-request cache-url path)
439 "Return an HTTP request for the narinfo of PATH at CACHE-URL."
440 (let ((url (string-append cache-url "/" (store-path-hash-part path)
442 (build-request (string->uri url) #:method 'GET)))
444 (define (http-multiple-get base-url requests proc)
445 "Send all of REQUESTS to the server at BASE-URL. Call PROC for each
446 response, passing it the request object, the response, and a port from which
447 to read the response body. Return the list of results."
448 (let connect ((requests requests)
450 ;; (format (current-error-port) "connecting (~a requests left)..."
451 ;; (length requests))
452 (let ((p (open-socket-for-uri base-url)))
453 ;; Send all of REQUESTS in a row.
454 (setvbuf p _IOFBF (expt 2 16))
455 (for-each (cut write-request <> p) requests)
458 ;; Now start processing responses.
459 (let loop ((requests requests)
465 (let* ((resp (read-response p))
466 (body (response-body-port resp))
467 (result (cons (proc head resp body) result)))
468 ;; The server can choose to stop responding at any time, in which
469 ;; case we have to try again. Check whether that is the case.
470 ;; Note that even upon "Connection: close", we can read from BODY.
471 (match (assq 'connection (response-headers resp))
472 (('connection 'close)
474 (connect tail result)) ;try again
476 (loop tail result)))))))))) ;keep going
478 (define (read-to-eof port)
479 "Read from PORT until EOF is reached. The data are discarded."
480 (dump-port port (%make-void-port "w")))
482 (define (narinfo-from-file file url)
483 "Attempt to read a narinfo from FILE, using URL as the cache URL. Return #f
484 if file doesn't exist, and the narinfo otherwise."
487 (call-with-input-file file
488 (cut read-narinfo <> url)))
490 (if (= ENOENT (system-error-errno args))
492 (apply throw args)))))
494 (define (fetch-narinfos cache paths)
495 "Retrieve all the narinfos for PATHS from CACHE and return them."
499 (define update-progress!
502 (display #\cr (current-error-port))
503 (force-output (current-error-port))
504 (format (current-error-port)
505 (_ "updating list of substitutes from '~a'... ~5,1f%")
506 url (* 100. (/ done (length paths))))
507 (set! done (+ 1 done)))))
509 (define (handle-narinfo-response request response port)
510 (let ((len (response-content-length response)))
511 ;; Make sure to read no more than LEN bytes since subsequent bytes may
512 ;; belong to the next response.
513 (case (response-code response)
515 (let ((narinfo (read-narinfo port url #:size len)))
516 (cache-narinfo! cache (narinfo-path narinfo) narinfo)
520 (let* ((path (uri-path (request-uri request)))
521 (hash-part (string-drop-right path 8))) ; drop ".narinfo"
523 (get-bytevector-n port len)
525 (cache-narinfo! cache
526 (find (cut string-contains <> hash-part) paths)
530 (else ; transient failure
532 (get-bytevector-n port len)
536 (and (string=? (cache-store-directory cache) (%store-prefix))
537 (let ((uri (string->uri url)))
538 (case (and=> uri uri-scheme)
540 (let ((requests (map (cut narinfo-request url <>) paths)))
542 (let ((result (http-multiple-get url requests
543 handle-narinfo-response)))
544 (newline (current-error-port))
547 (let* ((base (string-append (uri-path uri) "/"))
548 (files (map (compose (cut string-append base <> ".narinfo")
549 store-path-hash-part)
551 (filter-map (cut narinfo-from-file <> url) files)))
553 (leave (_ "~s: unsupported server URI scheme~%")
554 (if uri (uri-scheme uri) url)))))))
556 (define (lookup-narinfos cache paths)
557 "Return the narinfos for PATHS, invoking the server at CACHE when no
558 information is available locally."
559 (let-values (((cached missing)
560 (fold2 (lambda (path cached missing)
561 (let-values (((valid? value)
562 (cached-narinfo path)))
564 (values (cons value cached) missing)
565 (values cached (cons path missing)))))
571 (let* ((cache (force cache))
573 (fetch-narinfos cache missing)
575 (append cached missing)))))
577 (define (lookup-narinfo cache path)
578 "Return the narinfo for PATH in CACHE, or #f when no substitute for PATH was
580 (match (lookup-narinfos cache (list path))
583 (define (remove-expired-cached-narinfos)
584 "Remove expired narinfo entries from the cache. The sole purpose of this
585 function is to make sure `%narinfo-cache-directory' doesn't grow
588 (current-time time-monotonic))
590 (define (expired? file)
593 (call-with-input-file file
596 (('narinfo ('version 1) ('cache-uri _) ('date date)
598 (obsolete? date now %narinfo-negative-ttl))
599 (('narinfo ('version 1) ('cache-uri _) ('date date)
601 (obsolete? date now %narinfo-ttl))
604 ;; FILE may have been deleted.
607 (for-each (lambda (file)
608 (let ((file (string-append %narinfo-cache-directory
610 (when (expired? file)
611 ;; Wrap in `false-if-exception' because FILE might have been
612 ;; deleted in the meantime (TOCTTOU).
613 (false-if-exception (delete-file file)))))
614 (scandir %narinfo-cache-directory
616 (= (string-length file) 32)))))
618 (define (maybe-remove-expired-cached-narinfo)
619 "Remove expired narinfo entries from the cache if deemed necessary."
621 (current-time time-monotonic))
624 (string-append %narinfo-cache-directory "/last-expiry-cleanup"))
626 (define last-expiry-date
627 (or (false-if-exception
628 (call-with-input-file expiry-file read))
631 (when (obsolete? last-expiry-date now %narinfo-expired-cache-entry-removal-delay)
632 (remove-expired-cached-narinfos)
633 (call-with-output-file expiry-file
634 (cute write (time-second now) <>))))
636 (define (progress-report-port report-progress port)
637 "Return a port that calls REPORT-PROGRESS every time something is read from
638 PORT. REPORT-PROGRESS is a two-argument procedure such as that returned by
641 (define (read! bv start count)
642 (let ((n (match (get-bytevector-n! port bv start count)
645 (set! total (+ total n))
646 (report-progress total (const n))
647 ;; XXX: We're not in control, so we always return anyway.
650 (make-custom-binary-input-port "progress-port-proc"
652 (cut close-port port)))
654 (define-syntax with-networking
656 "Catch DNS lookup errors and gracefully exit."
657 ;; Note: no attempt is made to catch other networking errors, because DNS
658 ;; lookup errors are typically the first one, and because other errors are
659 ;; a subset of `system-error', which is harder to filter.
661 (catch 'getaddrinfo-error
664 (leave (_ "host name lookup error: ~a~%")
665 (gai-strerror error)))))))
673 (display (_ "Usage: guix substitute [OPTION]...
674 Internal tool to substitute a pre-built binary to a local build.\n"))
676 --query report on the availability of substitutes for the
677 store file names passed on the standard input"))
679 --substitute STORE-FILE DESTINATION
680 download STORE-FILE and store it as a Nar in file
684 -h, --help display this help and exit"))
686 -V, --version display version information and exit"))
688 (show-bug-report-information))
696 (define (check-acl-initialized)
697 "Warn if the ACL is uninitialized."
698 (define (singleton? acl)
699 ;; True if ACL contains just the user's public key.
700 (and (file-exists? %public-key-file)
701 (let ((key (call-with-input-file %public-key-file
702 (compose string->canonical-sexp
706 (equal? (canonical-sexp->string thing)
707 (canonical-sexp->string key)))
711 (let ((acl (acl->public-keys (current-acl))))
712 (when (or (null? acl) (singleton? acl))
713 (warning (_ "ACL for archive imports seems to be uninitialized, \
714 substitutes may be unavailable\n")))))
716 (define (daemon-options)
717 "Return a list of name/value pairs denoting build daemon options."
719 (char-set-complement (char-set #\newline)))
721 (match (getenv "_NIX_OPTIONS")
722 (#f ;should not happen when called by the daemon
725 ;; Here we get something of the form "OPTION1=VALUE1\nOPTION2=VALUE2\n".
726 (filter-map (lambda (option=value)
727 (match (string-index option=value #\=)
728 (#f ;invalid option setting
731 (cons (string-take option=value equal-sign)
732 (string-drop option=value (+ 1 equal-sign))))))
733 (string-tokenize newline-separated %not-newline)))))
735 (define (find-daemon-option option)
736 "Return the value of build daemon option OPTION, or #f if it could not be
738 (assoc-ref (daemon-options) option))
741 (match (and=> ;; TODO: Uncomment the following lines when multiple
742 ;; substitute sources are supported.
743 ;; (find-daemon-option "untrusted-substitute-urls") ;client
745 (find-daemon-option "substitute-urls") ;admin
750 ;; Currently we don't handle multiple substitute URLs.
751 (warning (_ "these substitute URLs will not be used:~{ ~a~}~%")
755 ;; This can only happen when this script is not invoked by the
757 "http://hydra.gnu.org")))
759 (define (guix-substitute . args)
760 "Implement the build daemon's substituter protocol."
761 (mkdir-p %narinfo-cache-directory)
762 (maybe-remove-expired-cached-narinfo)
763 (check-acl-initialized)
765 ;; Starting from commit 22144afa in Nix, we are allowed to bail out directly
766 ;; when we know we cannot substitute, but we must emit a newline on stdout
767 ;; when everything is alright.
768 (let ((uri (string->uri %cache-url)))
769 (case (uri-scheme uri)
771 ;; Exit gracefully if there's no network access.
772 (let ((host (uri-host uri)))
773 (catch 'getaddrinfo-error
777 (warning (_ "failed to look up host '~a' (~a), \
778 substituter disabled~%")
779 host (gai-strerror error))
783 ;; Say hello (see above.)
785 (force-output (current-output-port))
788 (with-error-handling ; for signature errors
791 (let ((cache (open-cache* %cache-url))
794 (and (narinfo? obj) (valid-narinfo? obj acl)))
796 (let loop ((command (read-line)))
797 (or (eof-object? command)
799 (match (string-tokenize command)
801 ;; Return the subset of PATHS available in CACHE.
804 (lookup-narinfos cache paths)
806 (for-each (lambda (narinfo)
807 (format #t "~a~%" (narinfo-path narinfo)))
808 (filter valid? substitutable))
811 ;; Reply info about PATHS if it's in CACHE.
814 (lookup-narinfos cache paths)
816 (for-each (lambda (narinfo)
817 (format #t "~a\n~a\n~a\n"
818 (narinfo-path narinfo)
819 (or (and=> (narinfo-deriver narinfo)
824 (length (narinfo-references narinfo)))
825 (for-each (cute format #t "~a/~a~%"
827 (narinfo-references narinfo))
828 (format #t "~a\n~a\n"
829 (or (narinfo-file-size narinfo) 0)
830 (or (narinfo-size narinfo) 0)))
831 (filter valid? substitutable))
834 (error "unknown `--query' command" wtf)))
835 (loop (read-line)))))))
836 (("--substitute" store-path destination)
837 ;; Download STORE-PATH and add store it as a Nar in file DESTINATION.
838 (let* ((cache (open-cache* %cache-url))
839 (narinfo (lookup-narinfo cache store-path))
840 (uri (narinfo-uri narinfo)))
841 ;; Make sure it is signed and everything.
842 (assert-valid-narinfo narinfo)
844 ;; Tell the daemon what the expected hash of the Nar itself is.
845 (format #t "~a~%" (narinfo-hash narinfo))
847 (format (current-error-port) "downloading `~a'~:[~*~; (~,1f MiB installed)~]...~%"
850 ;; Use the Nar size as an estimate of the installed size.
851 (narinfo-size narinfo)
852 (and=> (narinfo-size narinfo)
853 (cute / <> (expt 2. 20))))
854 (let*-values (((raw download-size)
855 ;; Note that Hydra currently generates Nars on the fly
856 ;; and doesn't specify a Content-Length, so
857 ;; DOWNLOAD-SIZE is #f in practice.
858 (fetch uri #:buffered? #f #:timeout? #f))
860 (let* ((comp (narinfo-compression narinfo))
861 (dl-size (or download-size
862 (and (equal? comp "none")
863 (narinfo-size narinfo))))
864 (progress (progress-proc (uri-abbreviation uri)
866 (current-error-port))))
867 (progress-report-port progress raw)))
869 (decompressed-port (and=> (narinfo-compression narinfo)
872 ;; Unpack the Nar at INPUT into DESTINATION.
873 (restore-file input destination)
875 ;; Skip a line after what 'progress-proc' printed.
876 (newline (current-error-port))
878 (every (compose zero? cdr waitpid) pids))))
880 (show-version-and-exit "guix substitute"))
884 (leave (_ "~a: unrecognized options~%") opts))))))
888 ;;; eval: (put 'with-timeout 'scheme-indent-function 1)
891 ;;; substitute.scm ends here