summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2024-05-11 17:40:41 +0100
committerChristopher Baines <mail@cbaines.net>2024-06-11 11:33:31 +0100
commit275f27989175f31e9feb364ed7deac2435b08f68 (patch)
tree25fbf5a51b3365306a3ed690071e8a25cac3391c
parentad8d386168c434b8149beae3838a8f27e487f441 (diff)
downloadguix-275f27989175f31e9feb364ed7deac2435b08f68.tar.gz
hg-download: Reduce builder duplication.
Rather than creating a different builder in the store for every different
download (by hash), remove the hash from the builder and pass it in via an
environment variable.  This means that when hg-fetch is used by two different
package sources, the derivations will still differ but the builder will be
shared.

Looking at the code, becuase the ref is also in the builder, the builders have
been duplicated for a while.  The overhead is probably limited though since
hg-reference isn't used much compared to say svn-multi-reference.

To try and make the effects of introducing variance in to the builder script
more obvious, separate it out in to it's own procedure, so that it's clearer
when there's new data going in that could cause variance.

* guix/hg-download.scm (hg-fetch): Extract out builder script and include
hash, hg ref url, and hg ref changeset in the derivation as an environment
variables.
(hg-fetch-builder): New procedure.

Change-Id: I3c3a0b4963ea1b208bf1d5137ef98666458ae2d7
-rw-r--r--guix/hg-download.scm127
1 files changed, 75 insertions, 52 deletions
diff --git a/guix/hg-download.scm b/guix/hg-download.scm
index 55d908817f..812017e73d 100644
--- a/guix/hg-download.scm
+++ b/guix/hg-download.scm
@@ -30,6 +30,7 @@
   #:use-module (ice-9 match)
   #:use-module (ice-9 popen)
   #:use-module (ice-9 rdelim)
+  #:use-module (rnrs bytevectors)
   #:export (hg-reference
             hg-reference?
             hg-reference-url
@@ -58,13 +59,7 @@
   (let ((distro (resolve-interface '(gnu packages version-control))))
     (module-ref distro 'mercurial)))
 
-(define* (hg-fetch ref hash-algo hash
-                   #:optional name
-                   #:key (system (%current-system)) (guile (default-guile))
-                   (hg (hg-package)))
-  "Return a fixed-output derivation that fetches REF, a <hg-reference>
-object.  The output is expected to have recursive hash HASH of type
-HASH-ALGO (a symbol).  Use NAME as the file name, or a generic name if #f."
+(define (hg-fetch-builder hg hash-algo)
   (define inputs
     ;; The 'swh-download' procedure requires tar and gzip.
     `(("gzip" ,(module-ref (resolve-interface '(gnu packages compression))
@@ -88,56 +83,84 @@ HASH-ALGO (a symbol).  Use NAME as the file name, or a generic name if #f."
                                      (guix build download-nar)
                                      (guix swh)))))
 
-  (define build
-    (with-imported-modules modules
-      (with-extensions (list guile-json gnutls ;for (guix swh)
-                             guile-lzlib)
-        #~(begin
-            (use-modules (guix build hg)
-                         (guix build utils) ;for `set-path-environment-variable'
-                         ((guix build download)
-                          #:select (download-method-enabled?))
-                         (guix build download-nar)
-                         (guix swh)
-                         (ice-9 match))
-
-            (set-path-environment-variable "PATH" '("bin")
-                                           (match '#+inputs
-                                             (((names dirs outputs ...) ...)
-                                              dirs)))
-
-            (setvbuf (current-output-port) 'line)
-            (setvbuf (current-error-port) 'line)
-
-            (or (and (download-method-enabled? 'upstream)
-                     (hg-fetch '#$(hg-reference-url ref)
-                               '#$(hg-reference-changeset ref)
-                               #$output
-                               #:hg-command (string-append #+hg "/bin/hg")))
-                (and (download-method-enabled? 'nar)
-                     (download-nar #$output))
-                ;; As a last resort, attempt to download from Software Heritage.
-                ;; Disable X.509 certificate verification to avoid depending
-                ;; on nss-certs--we're authenticating the checkout anyway.
-                (and (download-method-enabled? 'swh)
-                     (parameterize ((%verify-swh-certificate? #f))
-                       (format (current-error-port)
-                               "Trying to download from Software Heritage...~%")
-                       (or (swh-download-directory-by-nar-hash
-                            #$hash '#$hash-algo #$output)
-                           (swh-download #$(hg-reference-url ref)
-                                         #$(hg-reference-changeset ref)
-                                         #$output)))))))))
+  (with-imported-modules modules
+    (with-extensions (list guile-json gnutls ;for (guix swh)
+                           guile-lzlib)
+      #~(begin
+          (use-modules (guix build hg)
+                       (guix build utils) ;for `set-path-environment-variable'
+                       ((guix build download)
+                        #:select (download-method-enabled?))
+                       (guix build download-nar)
+                       (guix swh)
+                       (ice-9 match)
+                       (rnrs bytevectors))
+
+          (set-path-environment-variable "PATH" '("bin")
+                                         (match '#+inputs
+                                           (((names dirs outputs ...) ...)
+                                            dirs)))
+
+          (setvbuf (current-output-port) 'line)
+          (setvbuf (current-error-port) 'line)
+
+          (or (and (download-method-enabled? 'upstream)
+                   (hg-fetch (string->symbol (getenv "hg ref url"))
+                             (string->symbol (getenv "hg ref changeset"))
+                             #$output
+                             #:hg-command (string-append #+hg "/bin/hg")))
+              (and (download-method-enabled? 'nar)
+                   (download-nar #$output))
+              ;; As a last resort, attempt to download from Software Heritage.
+              ;; Disable X.509 certificate verification to avoid depending
+              ;; on nss-certs--we're authenticating the checkout anyway.
+              (and (download-method-enabled? 'swh)
+                   (parameterize ((%verify-swh-certificate? #f))
+                     (format (current-error-port)
+                             "Trying to download from Software Heritage...~%")
+                     (or (swh-download-directory-by-nar-hash
+                          (u8-list->bytevector
+                           (map string->number
+                                (string-split (getenv "hash") #\,)))
+                          '#$hash-algo
+                          #$output)
+                         (swh-download (getenv "hg ref url")
+                                       (getenv "hg ref changeset")
+                                       #$output)))))))))
 
+(define* (hg-fetch ref hash-algo hash
+                   #:optional name
+                   #:key (system (%current-system)) (guile (default-guile))
+                   (hg (hg-package)))
+  "Return a fixed-output derivation that fetches REF, a <hg-reference>
+object.  The output is expected to have recursive hash HASH of type
+HASH-ALGO (a symbol).  Use NAME as the file name, or a generic name if #f."
   (mlet %store-monad ((guile (package->derivation guile system)))
-    (gexp->derivation (or name "hg-checkout") build
+    (gexp->derivation (or name "hg-checkout")
+                      ;; Avoid the builder differing for every single use as
+                      ;; having less builder is more efficient for computing
+                      ;; derivations.
+                      ;;
+                      ;; Don't pass package specific data in to the following
+                      ;; procedure, use #:env-vars below instead.
+                      (hg-fetch-builder hg hash-algo)
                       #:leaked-env-vars '("http_proxy" "https_proxy"
                                           "LC_ALL" "LC_MESSAGES" "LANG"
                                           "COLUMNS")
-                      #:env-vars (match (getenv "GUIX_DOWNLOAD_METHODS")
-                                   (#f '())
-                                   (value
-                                    `(("GUIX_DOWNLOAD_METHODS" . ,value))))
+                      #:env-vars
+                      `(("hg ref url" . ,(hg-reference-url ref))
+                        ("hg ref changeset" . ,(hg-reference-changeset ref))
+                        ;; To avoid pulling in (guix base32) in the builder
+                        ;; script, use bytevector->u8-list from (rnrs
+                        ;; bytevectors)
+                        ("hash" . ,(string-join
+                                    (map number->string
+                                         (bytevector->u8-list hash))
+                                    ","))
+                        ,@(match (getenv "GUIX_DOWNLOAD_METHODS")
+                            (#f '())
+                            (value
+                             `(("GUIX_DOWNLOAD_METHODS" . ,value)))))
                       #:system system
                       #:local-build? #t           ;don't offload repo cloning
                       #:hash-algo hash-algo