Subject: [PATCH 3/4] guix: Add module aware 'guix import go'
From: Jørgen Kvalsvik <[email-masked]>
To: [email-masked]
Cc: Jørgen Kvalsvik <[email-masked]>
Date: Fri, 15 Nov 2024 22:11:05 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Emit module aware package go packages.  It does not compute the hash of the
full module yet, and only supports git, but goes a long way towards making it
easy to package new go programs using the module-aware build system.

* guix/import/go.scm (go-module->guix-package): Add go.mod awareness
* guix/scripts/import/go.scm (show-help): Document -m, --mod
(%options): Accept them.

Change-Id: I4efd7260d69276279940e21698ecc7eb57232a67
---
 guix/import/go.scm         | 88 ++++++++++++++++++++++++--------------
 guix/scripts/import/go.scm |  6 +++
 2 files changed, 61 insertions(+), 33 deletions(-)

diff --git a/guix/import/go.scm b/guix/import/go.scm
index dd9298808d..967aa54d58 100644
--- a/guix/import/go.scm
+++ b/guix/import/go.scm
@@ -615,6 +615,7 @@ (define (validate-version version available-versions module-path)
 (define* (go-module->guix-package module-path #:key
                                   (goproxy "https://proxy.golang.org")
                                   version
+                                  go-mod?
                                   pin-versions?
                                   #:allow-other-keys)
   "Return the package S-expression corresponding to MODULE-PATH at VERSION, a Go package.
@@ -641,43 +642,62 @@ (define* (go-module->guix-package module-path #:key
          (meta-data (fetch-module-meta-data root-module-path))
          (vcs-type (module-meta-vcs meta-data))
          (vcs-repo-url (module-meta-data-repo-url meta-data goproxy))
+         (home-page (format #f "https://~a" root-module-path))
          (synopsis (go-package-synopsis module-path))
-         (description (go-package-description module-path))
-         (licenses (go-package-licenses module-path)))
-    (values
-     `(package
-        (name ,guix-name)
-        (version ,(strip-v-prefix version*))
-        (source
-         ,(vcs->origin vcs-type vcs-repo-url version*))
-        (build-system go-build-system)
-        (arguments
-         (list ,@(if (version>? min-go-version (package-version (go-package)))
-                     `(#:go ,(string->number min-go-version))
-                     '())
-               #:import-path ,module-path
-               ,@(if (string=? module-path-sans-suffix root-module-path)
-                     '()
-                     `(#:unpack-path ,root-module-path))))
-        ,@(maybe-propagated-inputs
-           (map (match-lambda
-                  ((name version)
-                   (go-module->guix-package-name name (strip-v-prefix version)))
-                  (name
-                   (go-module->guix-package-name name)))
-                dependencies))
-        (home-page ,(format #f "https://~a" root-module-path))
-        (synopsis ,synopsis)
-        (description ,(and=> description beautify-description))
-        (license ,(match (list->licenses licenses)
+         (description (and=> (go-package-description module-path) beautify-description))
+         (licenses (go-package-licenses module-path))
+         (license (match (list->licenses licenses)
                     (() #f)                       ;unknown license
-                    ((license)                    ;a single license
-                     license)
+                    ((license) license)           ;a single license
                     ((license ...)                ;a list of licenses
                      `(list ,@license)))))
-     (if pin-versions?
-         dependencies+versions
-         dependencies))))
+    (if go-mod?
+        (values
+         `(package
+            (name ,guix-name)
+            (version ,(strip-v-prefix version*))
+            (source
+             (origin
+               (method go-mod-fetch)
+               (uri (go-mod-reference
+                     (source ,(vcs->origin vcs-type vcs-repo-url version*))))
+               (sha256
+                (base32
+                 ;; FIXME: fetch & compute checksum
+                 "0000000000000000000000000000000000000000000000000000"))))
+             (build-system go-mod-build-system)
+             (home-page ,home-page)
+             (synopsis ,synopsis)
+             (description ,description)
+             (license ,license)))
+        (values
+         `(package
+            (name ,guix-name)
+            (version ,(strip-v-prefix version*))
+            (source ,(vcs->origin vcs-type vcs-repo-url version*))
+            (build-system go-build-system)
+         (arguments
+          (list ,@(if (version>? min-go-version (package-version (go-package)))
+                      `(#:go ,(string->number min-go-version))
+                      '())
+                #:import-path ,module-path
+                ,@(if (string=? module-path-sans-suffix root-module-path)
+                      '()
+                      `(#:unpack-path ,root-module-path))))
+         ,@(maybe-propagated-inputs
+            (map (match-lambda
+                   ((name version)
+                    (go-module->guix-package-name name (strip-v-prefix version)))
+                   (name
+                    (go-module->guix-package-name name)))
+                 dependencies))
+         (home-page ,home-page)
+         (synopsis ,synopsis)
+         (description ,description)
+         (license ,license)
+        (if pin-versions?
+            dependencies+versions
+            dependencies))))))
 
 (define go-module->guix-package*
   (lambda args
@@ -699,6 +719,7 @@ (define go-module->guix-package*
 (define* (go-module-recursive-import package-name
                                      #:key (goproxy "https://proxy.golang.org")
                                      version
+                                     go-mod?
                                      pin-versions?)
 
   (recursive-import
@@ -709,6 +730,7 @@ (define* (go-module-recursive-import package-name
       (receive (package-sexp dependencies)
           (go-module->guix-package* name #:goproxy goproxy
                                     #:version version
+                                    #:go-mod? go-mod?
                                     #:pin-versions? pin-versions?)
         (values package-sexp dependencies))))
    #:guix-name go-module->guix-package-name
diff --git a/guix/scripts/import/go.scm b/guix/scripts/import/go.scm
index b90c6ac72f..a12c3a9b2f 100644
--- a/guix/scripts/import/go.scm
+++ b/guix/scripts/import/go.scm
@@ -50,6 +50,8 @@ (define (show-help)
   (display (G_ "
   -h, --help             display this help and exit"))
   (display (G_ "
+  -m, --mod              generate go-module based packages"))
+  (display (G_ "
   -r, --recursive        generate package expressions for all Go modules
                          that are not yet in Guix"))
   (display (G_ "
@@ -65,6 +67,9 @@ (define %options
                  (lambda args
                    (show-help)
                    (exit 0)))
+         (option '(#\m "mod") #f #f
+                 (lambda (opt name arg result)
+                   (alist-cons 'go-mod? #t result)))
          (option '(#\r "recursive") #f #f
                  (lambda (opt name arg result)
                    (alist-cons 'recursive #t result)))
@@ -106,6 +111,7 @@ (define (parse-options)
            (let ((arguments (list name
                                   #:goproxy (assoc-ref opts 'goproxy)
                                   #:version version
+                                  #:go-mod?  (assoc-ref opts 'go-mod?)
                                   #:pin-versions?
                                   (assoc-ref opts 'pin-versions?))))
              (if (assoc-ref opts 'recursive)
-- 
2.39.5