Subject: [bug#52555] [RFC PATCH v2 5/5] substitute: Fetch substitutes using ERIS.

From: pukkamustard <[email-masked]>
Date: Tue, 25 Jan 2022 19:22:01 +0000

* guix/scripts/substitute.scm (process-substitution): Fetch substitutes using ERIS.
* guix/eris.scm (guix-eris-block-ref): New procedure.
---
 guix/eris.scm               |  7 ++++++-
 guix/scripts/substitute.scm | 21 ++++++++++++++++-----
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/guix/eris.scm b/guix/eris.scm
index 163bbe05ac..0999564c1f 100644
--- a/guix/eris.scm
+++ b/guix/eris.scm
@@ -23,7 +23,8 @@ (define-module (guix eris)
   #:use-module (web response)
   #:use-module (srfi srfi-71)
 
-  #:export (guix-eris-block-reducer))
+  #:export (guix-eris-block-reducer
+            guix-eris-block-ref))
 
 (define (ipfs-daemon-alive?)
   "Attempt to connect to the IPFS daemon. Returns #t if the daemon is alive
@@ -53,3 +54,7 @@ (define guix-eris-block-reducer
      (if ipfs
          (eris-blocks-ipfs-reducer ipfs ref-block)
          ipfs))))
+
+(define (guix-eris-block-ref ref)
+  "Dereference a block for decoding ERIS content"
+  (eris-blocks-ipfs-ref ref))
diff --git a/guix/scripts/substitute.scm b/guix/scripts/substitute.scm
index 908a8334a8..852264976e 100755
--- a/guix/scripts/substitute.scm
+++ b/guix/scripts/substitute.scm
@@ -62,6 +62,8 @@ (define-module (guix scripts substitute)
   #:use-module (srfi srfi-35)
   #:use-module (web uri)
   #:use-module (guix http-client)
+  #:use-module (guix eris)
+  #:use-module (eris)
   #:export (%allow-unauthenticated-substitutes?
             %reply-file-descriptor
 
@@ -486,18 +488,27 @@ (define (fetch uri)
                          #:port port
                          #:keep-alive? #t
                          #:buffered? #f)))))
+
       (else
-       (leave (G_ "unsupported substitute URI scheme: ~a~%")
-              (uri->string uri)))))
+       (if (and (eris-read-capability? uri))
+           (values (eris-decode->port uri
+                                      #:block-ref
+                                      guix-eris-block-ref) #f)
+           (leave (G_ "unsupported substitute URI scheme: ~a~%")
+                  (uri->string uri))))))
+
+  (define* (best-uri narinfo #:key (eris? #f))
+    (if (and eris? (narinfo-eris-urn narinfo))
+        (values (narinfo-eris-urn narinfo) "zstd" #f)
+        (narinfo-best-uri narinfo #:fast-decompression?
+                          %prefer-fast-decompression?)))
 
   (unless narinfo
     (leave (G_ "no valid substitute for '~a'~%")
            store-item))
 
   (let-values (((uri compression file-size)
-                (narinfo-best-uri narinfo
-                                  #:fast-decompression?
-                                  %prefer-fast-decompression?)))
+                (best-uri narinfo #:eris? #t)))
     (unless print-build-trace?
       (format (current-error-port)
               (G_ "Downloading ~a...~%") (uri->string uri)))
-- 
2.34.0