[PATCH] guix: opam: Allow importing local files.

OpenSubmitted by Julien Lepiller.
Details
3 participants
  • Julien Lepiller
  • Maxime Devos
  • Xinglu Chen
Owner
unassigned
Severity
normal
J
J
Julien Lepiller wrote on 6 Feb 22:41 +0100
(address . guix-patches@gnu.org)
20220206224130.2220aae9@tachikoma.lepiller.eu
Hi Guix!

This patch lets you import opam packages from a local file. This is a
feature that was requested a few times, and there it is :)

Now, if you have an OCaml package sources that provides an opam file
(usually called opam, or with the .opam extension), you can import the
package, recursively or not with something like:

guix import opam foo -f foo.opam -r

instead of fetching foo from the repositories, it will fetch the data
directly from the given file. WDYT?
From b027391357eef4d6fa1bb8741528cf9650aef568 Mon Sep 17 00:00:00 2001
Message-Id: <b027391357eef4d6fa1bb8741528cf9650aef568.1644183523.git.julien@lepiller.eu>
From: Julien Lepiller <julien@lepiller.eu>
Date: Sun, 6 Feb 2022 22:35:14 +0100
Subject: [PATCH] guix: opam: Allow importing local files.

* guix/scripts/import/opam.scm (guix-import-opam): Support `--file` flag.
* guix/import/utils.scm (recursive-import): Add optional file argument.
Pass it to the first iteration when available.
* guix/import/opam.scm (opam-fetch): Fetch from file when available.
(opam->guix-source): Factorize source out of opam->guix-package.
(opam->guix-package): Add file argument. Ensure it does not fail when
name, version and url are not available.
---
guix/import/opam.scm | 101 +++++++++++++++++++----------------
guix/import/utils.scm | 17 +++---
guix/scripts/import/opam.scm | 11 +++-
3 files changed, 75 insertions(+), 54 deletions(-)

Toggle diff (215 lines)
diff --git a/guix/import/opam.scm b/guix/import/opam.scm
index a6f6fe8c9f..05f79110f8 100644
--- a/guix/import/opam.scm
+++ b/guix/import/opam.scm
@@ -310,8 +310,11 @@ (define (dependency-list->inputs lst)
   (map string->symbol
        (ocaml-names->guix-names lst)))
 
-(define* (opam-fetch name #:optional (repositories-specs '("opam")))
-  (or (fold (lambda (repository others)
+(define* (opam-fetch name #:optional (repositories-specs '("opam")) (file #f))
+  (or (and file (let ((metadata (get-metadata file)))
+                  `(("metadata" . ,metadata)
+                    ("version" . #f))))
+      (fold (lambda (repository others)
               (match (find-latest-version name repository)
                 ((_ version file) `(("metadata" ,@(get-metadata file))
                                     ("version" . ,version)))
@@ -320,17 +323,29 @@ (define* (opam-fetch name #:optional (repositories-specs '("opam")))
             (filter-map get-opam-repository repositories-specs))
       (warning (G_ "opam: package '~a' not found~%") name)))
 
-(define* (opam->guix-package name #:key (repo 'opam) version)
+(define (opam->guix-source url-dict)
+  (let ((source-url (and url-dict
+                         (or (metadata-ref url-dict "src")
+                             (metadata-ref url-dict "archive")))))
+    (if source-url
+        (call-with-temporary-output-file
+          (lambda (temp port)
+            (and (url-fetch source-url temp)
+                 `(origin
+                    (method url-fetch)
+                    (uri ,source-url)
+                    (sha256 (base32 ,(guix-hash-url temp)))))))
+        'no-source-information)))
+
+(define* (opam->guix-package name #:key (repo '("opam")) (file #f) version)
   "Import OPAM package NAME from REPOSITORY (a directory name) or, if
-REPOSITORY is #f, from the official OPAM repository.  Return a 'package' sexp
+REPOSITORY is #f, from the official OPAM repository.  When FILE is defined,
+import the packaged defined in the opam FILE instead.  Return a 'package' sexp
 or #f on failure."
   (and-let* ((with-opam (if (member "opam" repo) repo (cons "opam" repo)))
-             (opam-file (opam-fetch name with-opam))
-             (version (assoc-ref opam-file "version"))
+             (opam-file (opam-fetch name with-opam file))
              (opam-content (assoc-ref opam-file "metadata"))
-             (url-dict (metadata-ref opam-content "url"))
-             (source-url (or (metadata-ref url-dict "src")
-                             (metadata-ref url-dict "archive")))
+             (source (opam->guix-source (metadata-ref opam-content "url")))
              (requirements (metadata-ref opam-content "depends"))
              (names (dependency-list->names requirements))
              (dependencies (filter-dependencies names))
@@ -344,47 +359,41 @@ (define* (opam->guix-package name #:key (repo 'opam) version)
                                   (not (member name '("dune" "jbuilder"))))
                                 native-dependencies))))
         (let ((use-dune? (member "dune" names)))
-          (call-with-temporary-output-file
-            (lambda (temp port)
-              (and (url-fetch source-url temp)
-                   (values
-                    `(package
-                       (name ,(ocaml-name->guix-name name))
-                       (version ,version)
-                       (source
-                         (origin
-                           (method url-fetch)
-                           (uri ,source-url)
-                           (sha256 (base32 ,(guix-hash-url temp)))))
-                       (build-system ,(if use-dune?
-                                          'dune-build-system
-                                          'ocaml-build-system))
-                       ,@(if (null? inputs)
-                           '()
-                           `((propagated-inputs (list ,@inputs))))
-                       ,@(if (null? native-inputs)
-                           '()
-                           `((native-inputs (list ,@native-inputs))))
-                       ,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
-                           '()
-                           `((properties
-                               ,(list 'quasiquote `((upstream-name . ,name))))))
-                       (home-page ,(metadata-ref opam-content "homepage"))
-                       (synopsis ,(metadata-ref opam-content "synopsis"))
-                       (description ,(beautify-description
-                                      (metadata-ref opam-content "description")))
-                       (license ,(spdx-string->license
-                                  (metadata-ref opam-content "license"))))
-                    (filter
-                      (lambda (name)
-                        (not (member name '("dune" "jbuilder"))))
-                      dependencies))))))))
+          (values
+           `(package
+              (name ,(and name (ocaml-name->guix-name name)))
+              (version ,(assoc-ref opam-file "version"))
+              (source ,source)
+              (build-system ,(if use-dune?
+                                 'dune-build-system
+                                 'ocaml-build-system))
+              ,@(if (null? inputs)
+                  '()
+                  `((propagated-inputs (list ,@inputs))))
+              ,@(if (null? native-inputs)
+                  '()
+                  `((native-inputs (list ,@native-inputs))))
+              ,@(if (and name (equal? name (guix-name->opam-name (ocaml-name->guix-name name))))
+                  '()
+                  `((properties
+                      ,(list 'quasiquote `((upstream-name . ,name))))))
+              (home-page ,(metadata-ref opam-content "homepage"))
+              (synopsis ,(metadata-ref opam-content "synopsis"))
+              (description ,(beautify-description
+                             (metadata-ref opam-content "description")))
+              (license ,(spdx-string->license
+                         (metadata-ref opam-content "license"))))
+           (filter
+             (lambda (name)
+               (not (member name '("dune" "jbuilder"))))
+             dependencies)))))
 
-(define* (opam-recursive-import package-name #:key repo)
+(define* (opam-recursive-import package-name #:key repo file)
   (recursive-import package-name
                     #:repo->guix-package opam->guix-package
                     #:guix-name ocaml-name->guix-name
-                    #:repo repo))
+                    #:repo repo
+                    #:file file))
 
 (define (guix-name->opam-name name)
   (if (string-prefix? "ocaml-" name)
diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index 1c3cfa3e0b..ab35b8a4fc 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -470,7 +470,7 @@ (define (topological-sort nodes
                    (set-insert (node-name head) visited))))))))
 
 (define* (recursive-import package-name
-                           #:key repo->guix-package guix-name version repo
+                           #:key repo->guix-package guix-name version repo file
                            #:allow-other-keys)
   "Return a list of package expressions for PACKAGE-NAME and all its
 dependencies, sorted in topological order.  For each package,
@@ -488,10 +488,15 @@ (define* (recursive-import package-name
   (define (exists? name version)
     (not (null? (find-packages-by-name (guix-name name) version))))
 
-  (define (lookup-node name version)
-    (let* ((package dependencies (repo->guix-package name
-                                                     #:version version
-                                                     #:repo repo))
+  (define* (lookup-node name version #:optional file)
+    (let* ((package dependencies (if file
+                                     (repo->guix-package name
+                                                       #:version version
+                                                       #:repo repo
+                                                       #:file file)
+                                     (repo->guix-package name
+                                                       #:version version
+                                                       #:repo repo)))
            (normalized-deps (map (match-lambda
                                    ((name version) (list name version))
                                    (name (list name #f))) dependencies)))
@@ -499,7 +504,7 @@ (define* (recursive-import package-name
 
   (filter-map
    node-package
-   (topological-sort (list (lookup-node package-name version))
+   (topological-sort (list (lookup-node package-name version file))
                      (lambda (node)
                        (map (lambda (name-version)
                               (apply lookup-node name-version))
diff --git a/guix/scripts/import/opam.scm b/guix/scripts/import/opam.scm
index 834ac34cb0..b58c6eab3d 100644
--- a/guix/scripts/import/opam.scm
+++ b/guix/scripts/import/opam.scm
@@ -50,6 +50,8 @@ (define (show-help)
       --repo             import packages from this opam repository (name, URL or local path)
                          can be used more than once"))
   (display (G_ "
+  -f, --file             import the package defined from a .opam file"))
+  (display (G_ "
   -V, --version          display version information and exit"))
   (newline)
   (show-bug-report-information))
@@ -66,6 +68,9 @@ (define %options
          (option '(#f "repo") #t #f
                  (lambda (opt name arg result)
                    (alist-cons 'repo arg result)))
+         (option '(#\f "file") #t #f
+                 (lambda (opt name arg result)
+                   (alist-cons 'file arg result)))
          (option '(#\r "recursive") #f #f
                  (lambda (opt name arg result)
                    (alist-cons 'recursive #t result)))
@@ -100,9 +105,11 @@ (define (guix-import-opam . args)
                    `(define-public ,(string->symbol name)
                       ,pkg))
                   (_ #f))
-                (opam-recursive-import package-name #:repo repo))
+                (opam-recursive-import package-name #:repo repo
+                                       #:file (assoc-ref opts 'file)))
            ;; Single import
-           (let ((sexp (opam->guix-package package-name #:repo repo)))
+           (let ((sexp (opam->guix-package package-name #:repo repo
+                                           #:file (assoc-ref opts 'file))))
              (unless sexp
                (leave (G_ "failed to download meta-data for package '~a'~%")
                       package-name))
-- 
2.34.0
M
M
Maxime Devos wrote on 6 Feb 22:58 +0100
c7f328ba29b68bcf0f4555095a864738a63be2f3.camel@telenet.be
Julien Lepiller schreef op zo 06-02-2022 om 22:41 [+0100]:
Toggle quote (10 lines)
> Now, if you have an OCaml package sources that provides an opam file
> (usually called opam, or with the .opam extension), you can import
> the
> package, recursively or not with something like:
>
> guix import opam foo -f foo.opam -r
>
> instead of fetching foo from the repositories, it will fetch the data
> directly from the given file. WDYT?

I don't know OCaml at all, but that seems potentially useful.
Can we have some tests for this in tests/opam.scm?

Greetings,
Maxime.
-----BEGIN PGP SIGNATURE-----

iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYgBEnBccbWF4aW1lZGV2
b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7knRAQCi2ZjhnPN1LWFPJDAfi9HCBN+h
uM+77YR+GNec2y6zYwD8CYIcFLIqXa2nDdLV2SPvBMu+jbaOSHQgLPeOb3s+OAU=
=xYaa
-----END PGP SIGNATURE-----


X
X
Xinglu Chen wrote on 7 Feb 09:27 +0100
87k0e713pg.fsf@yoctocell.xyz
Julien schrieb am Sonntag der 06. Februar 2022 um 22:41 +01:

Toggle quote (14 lines)
> Hi Guix!
>
> This patch lets you import opam packages from a local file. This is a
> feature that was requested a few times, and there it is :)
>
> Now, if you have an OCaml package sources that provides an opam file
> (usually called opam, or with the .opam extension), you can import the
> package, recursively or not with something like:
>
> guix import opam foo -f foo.opam -r
>
> instead of fetching foo from the repositories, it will fetch the data
> directly from the given file. WDYT?

Why just limit it to local files? What about accepting a tarball on the
internet or any VCS repository? This could generally be useful for any
importer.

Also, since we already know the name of the package, maybe specifying
the directory that contains the OCaml project would be enough?

guix import opam foo --type=local /path/to/directory
guix import opam foo --type=tarball https://example.org/foo.tar.gz
guix import opam foo --type=git https://git.example.org/foo

Or am I asking for too much? :-)

Toggle quote (6 lines)
> From b027391357eef4d6fa1bb8741528cf9650aef568 Mon Sep 17 00:00:00 2001
> Message-Id: <b027391357eef4d6fa1bb8741528cf9650aef568.1644183523.git.julien@lepiller.eu>
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Sun, 6 Feb 2022 22:35:14 +0100
> Subject: [PATCH] guix: opam: Allow importing local files.

It should be “import: opam: …”.

Toggle quote (50 lines)
> * guix/scripts/import/opam.scm (guix-import-opam): Support `--file` flag.
> * guix/import/utils.scm (recursive-import): Add optional file argument.
> Pass it to the first iteration when available.
> * guix/import/opam.scm (opam-fetch): Fetch from file when available.
> (opam->guix-source): Factorize source out of opam->guix-package.
> (opam->guix-package): Add file argument. Ensure it does not fail when
> name, version and url are not available.
> ---
> guix/import/opam.scm | 101 +++++++++++++++++++----------------
> guix/import/utils.scm | 17 +++---
> guix/scripts/import/opam.scm | 11 +++-
> 3 files changed, 75 insertions(+), 54 deletions(-)
>
> diff --git a/guix/import/opam.scm b/guix/import/opam.scm
> index a6f6fe8c9f..05f79110f8 100644
> --- a/guix/import/opam.scm
> +++ b/guix/import/opam.scm
> @@ -310,8 +310,11 @@ (define (dependency-list->inputs lst)
> (map string->symbol
> (ocaml-names->guix-names lst)))
>
> -(define* (opam-fetch name #:optional (repositories-specs '("opam")))
> - (or (fold (lambda (repository others)
> +(define* (opam-fetch name #:optional (repositories-specs '("opam")) (file #f))
> + (or (and file (let ((metadata (get-metadata file)))
> + `(("metadata" . ,metadata)
> + ("version" . #f))))
> + (fold (lambda (repository others)
> (match (find-latest-version name repository)
> ((_ version file) `(("metadata" ,@(get-metadata file))
> ("version" . ,version)))
> @@ -320,17 +323,29 @@ (define* (opam-fetch name #:optional (repositories-specs '("opam")))
> (filter-map get-opam-repository repositories-specs))
> (warning (G_ "opam: package '~a' not found~%") name)))
>
> -(define* (opam->guix-package name #:key (repo 'opam) version)
> +(define (opam->guix-source url-dict)
> + (let ((source-url (and url-dict
> + (or (metadata-ref url-dict "src")
> + (metadata-ref url-dict "archive")))))
> + (if source-url
> + (call-with-temporary-output-file
> + (lambda (temp port)
> + (and (url-fetch source-url temp)
> + `(origin
> + (method url-fetch)
> + (uri ,source-url)
> + (sha256 (base32 ,(guix-hash-url temp)))))))
> + 'no-source-information)))

I would use ‘and-let*’ instead of ‘let’, and drop the ‘if’ form.

The ability to import from a local file seems to mostly be useful when
the package isn’t meant to be submitted to Guix, right? Wouldn’t it
make more sense to make the source a ‘local-file’ object instead of just
setting it to #f?

Toggle quote (6 lines)
> +(define* (opam->guix-package name #:key (repo '("opam")) (file #f) version)
> "Import OPAM package NAME from REPOSITORY (a directory name) or, if
> -REPOSITORY is #f, from the official OPAM repository. Return a 'package' sexp
> +REPOSITORY is #f, from the official OPAM repository. When FILE is defined,
> +import the packaged defined in the opam FILE instead. Return a 'package' sexp

s/packaged/package/

Toggle quote (105 lines)
> (and-let* ((with-opam (if (member "opam" repo) repo (cons "opam" repo)))
> - (opam-file (opam-fetch name with-opam))
> - (version (assoc-ref opam-file "version"))
> + (opam-file (opam-fetch name with-opam file))
> (opam-content (assoc-ref opam-file "metadata"))
> - (url-dict (metadata-ref opam-content "url"))
> - (source-url (or (metadata-ref url-dict "src")
> - (metadata-ref url-dict "archive")))
> + (source (opam->guix-source (metadata-ref opam-content "url")))
> (requirements (metadata-ref opam-content "depends"))
> (names (dependency-list->names requirements))
> (dependencies (filter-dependencies names))
> @@ -344,47 +359,41 @@ (define* (opam->guix-package name #:key (repo 'opam) version)
> (not (member name '("dune" "jbuilder"))))
> native-dependencies))))
> (let ((use-dune? (member "dune" names)))
> - (call-with-temporary-output-file
> - (lambda (temp port)
> - (and (url-fetch source-url temp)
> - (values
> - `(package
> - (name ,(ocaml-name->guix-name name))
> - (version ,version)
> - (source
> - (origin
> - (method url-fetch)
> - (uri ,source-url)
> - (sha256 (base32 ,(guix-hash-url temp)))))
> - (build-system ,(if use-dune?
> - 'dune-build-system
> - 'ocaml-build-system))
> - ,@(if (null? inputs)
> - '()
> - `((propagated-inputs (list ,@inputs))))
> - ,@(if (null? native-inputs)
> - '()
> - `((native-inputs (list ,@native-inputs))))
> - ,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
> - '()
> - `((properties
> - ,(list 'quasiquote `((upstream-name . ,name))))))
> - (home-page ,(metadata-ref opam-content "homepage"))
> - (synopsis ,(metadata-ref opam-content "synopsis"))
> - (description ,(beautify-description
> - (metadata-ref opam-content "description")))
> - (license ,(spdx-string->license
> - (metadata-ref opam-content "license"))))
> - (filter
> - (lambda (name)
> - (not (member name '("dune" "jbuilder"))))
> - dependencies))))))))
> + (values
> + `(package
> + (name ,(and name (ocaml-name->guix-name name)))
> + (version ,(assoc-ref opam-file "version"))
> + (source ,source)
> + (build-system ,(if use-dune?
> + 'dune-build-system
> + 'ocaml-build-system))
> + ,@(if (null? inputs)
> + '()
> + `((propagated-inputs (list ,@inputs))))
> + ,@(if (null? native-inputs)
> + '()
> + `((native-inputs (list ,@native-inputs))))
> + ,@(if (and name (equal? name (guix-name->opam-name (ocaml-name->guix-name name))))
> + '()
> + `((properties
> + ,(list 'quasiquote `((upstream-name . ,name))))))
> + (home-page ,(metadata-ref opam-content "homepage"))
> + (synopsis ,(metadata-ref opam-content "synopsis"))
> + (description ,(beautify-description
> + (metadata-ref opam-content "description")))
> + (license ,(spdx-string->license
> + (metadata-ref opam-content "license"))))
> + (filter
> + (lambda (name)
> + (not (member name '("dune" "jbuilder"))))
> + dependencies)))))
>
> -(define* (opam-recursive-import package-name #:key repo)
> +(define* (opam-recursive-import package-name #:key repo file)
> (recursive-import package-name
> #:repo->guix-package opam->guix-package
> #:guix-name ocaml-name->guix-name
> - #:repo repo))
> + #:repo repo
> + #:file file))
>
> (define (guix-name->opam-name name)
> (if (string-prefix? "ocaml-" name)
> diff --git a/guix/import/utils.scm b/guix/import/utils.scm
> index 1c3cfa3e0b..ab35b8a4fc 100644
> --- a/guix/import/utils.scm
> +++ b/guix/import/utils.scm
> @@ -470,7 +470,7 @@ (define (topological-sort nodes
> (set-insert (node-name head) visited))))))))
>
> (define* (recursive-import package-name
> - #:key repo->guix-package guix-name version repo
> + #:key repo->guix-package guix-name version repo file
> #:allow-other-keys)
> "Return a list of package expressions for PACKAGE-NAME and all its
> dependencies, sorted in topological order. For each package,

The docstrings should also be updated.
-----BEGIN PGP SIGNATURE-----

iQJJBAEBCAAzFiEEAVhh4yyK5+SEykIzrPUJmaL7XHkFAmIA1/sVHHB1YmxpY0B5
b2N0b2NlbGwueHl6AAoJEKz1CZmi+1x5i1oP/1TZbQVKhanj9SBwv1P4b1Fp6/0e
PQAVN2Koms7eSLsSmApR3Fw6k5HBPWMbBlwcY7UaRBg1eZai0Nbzc7IfEWG3WDNO
XjRTT5DNK3ek74Ibq5LZVv9HLyLg59/E3fjr91JVKHesdDCTvSJOz3Zi+u2FXd/7
TWfJfeZIP/L8Y5L819IKyKcnH6oQYocSgnhXUPrbVATWvvd6wk6a7clOZN30pPmK
rwruQyfSwe12gCIxYx9bDELeRLTUz7tVw7Lm0MA5xDnBy+RwS4xv0WLOobO+vL8K
qCTWloJL3hKRYSu56P24tALQgdfBRAzrXRIKi4dHeMVhRr9AwYLo6DOcNDm9Q6M3
g4KF9l8fqUwuiRCzeKj3V9gs6db76LhsugJPfxCBeaVNg3rX6oo09Q/mKENrR4a2
A/cso8cR7wgViwtD9vUqN9W/YQCuKHFnT8lwb7lRi93TOn59r7Q+Mr7dS8ZgPmL2
ZA2r3+OcVyMLioyPxhQ2OPfHiayY1zAmJl7M5C/pJ0xYy3aZdWqMrbHDePCvNy3K
uYTvUSQPWLox37kMAXqAWgN+ElQnQb/d7U72erGlgF3imh1JQTcH4boWgJSVSvZN
6IOEcqG+Vm4iJkKWoJafS8rmyS8FP19sZCQ4TYf6oPFCApw3cHpYlYHfnELNu4J7
mbsgpexm6KbIwJhC
=1m+M
-----END PGP SIGNATURE-----

J
J
Julien Lepiller wrote on 7 Feb 09:46 +0100
E7325AF8-C9EE-4A89-9AE5-390CBD49ABAA@lepiller.eu
On February 7, 2022 9:27:39 AM GMT+01:00, Xinglu Chen <public@yoctocell.xyz> wrote:
Toggle quote (20 lines)
>Julien schrieb am Sonntag der 06. Februar 2022 um 22:41 +01:
>
>> Hi Guix!
>>
>> This patch lets you import opam packages from a local file. This is a
>> feature that was requested a few times, and there it is :)
>>
>> Now, if you have an OCaml package sources that provides an opam file
>> (usually called opam, or with the .opam extension), you can import the
>> package, recursively or not with something like:
>>
>> guix import opam foo -f foo.opam -r
>>
>> instead of fetching foo from the repositories, it will fetch the data
>> directly from the given file. WDYT?
>
>Why just limit it to local files? What about accepting a tarball on the
>internet or any VCS repository? This could generally be useful for any
>importer.

That's a good idea, but I'm not sure how to recognize these cases.

Toggle quote (4 lines)
>
>Also, since we already know the name of the package, maybe specifying
>the directory that contains the OCaml project would be enough?

Sure, that could be useful for your suggestion below to use the local directory as the source.

Toggle quote (68 lines)
>
> guix import opam foo --type=local /path/to/directory
> guix import opam foo --type=tarball https://example.org/foo.tar.gz
> guix import opam foo --type=git https://git.example.org/foo
> …
>
>Or am I asking for too much? :-)
>
>> From b027391357eef4d6fa1bb8741528cf9650aef568 Mon Sep 17 00:00:00 2001
>> Message-Id: <b027391357eef4d6fa1bb8741528cf9650aef568.1644183523.git.julien@lepiller.eu>
>> From: Julien Lepiller <julien@lepiller.eu>
>> Date: Sun, 6 Feb 2022 22:35:14 +0100
>> Subject: [PATCH] guix: opam: Allow importing local files.
>
>It should be “import: opam: …”.
>
>> * guix/scripts/import/opam.scm (guix-import-opam): Support `--file` flag.
>> * guix/import/utils.scm (recursive-import): Add optional file argument.
>> Pass it to the first iteration when available.
>> * guix/import/opam.scm (opam-fetch): Fetch from file when available.
>> (opam->guix-source): Factorize source out of opam->guix-package.
>> (opam->guix-package): Add file argument. Ensure it does not fail when
>> name, version and url are not available.
>> ---
>> guix/import/opam.scm | 101 +++++++++++++++++++----------------
>> guix/import/utils.scm | 17 +++---
>> guix/scripts/import/opam.scm | 11 +++-
>> 3 files changed, 75 insertions(+), 54 deletions(-)
>>
>> diff --git a/guix/import/opam.scm b/guix/import/opam.scm
>> index a6f6fe8c9f..05f79110f8 100644
>> --- a/guix/import/opam.scm
>> +++ b/guix/import/opam.scm
>> @@ -310,8 +310,11 @@ (define (dependency-list->inputs lst)
>> (map string->symbol
>> (ocaml-names->guix-names lst)))
>>
>> -(define* (opam-fetch name #:optional (repositories-specs '("opam")))
>> - (or (fold (lambda (repository others)
>> +(define* (opam-fetch name #:optional (repositories-specs '("opam")) (file #f))
>> + (or (and file (let ((metadata (get-metadata file)))
>> + `(("metadata" . ,metadata)
>> + ("version" . #f))))
>> + (fold (lambda (repository others)
>> (match (find-latest-version name repository)
>> ((_ version file) `(("metadata" ,@(get-metadata file))
>> ("version" . ,version)))
>> @@ -320,17 +323,29 @@ (define* (opam-fetch name #:optional (repositories-specs '("opam")))
>> (filter-map get-opam-repository repositories-specs))
>> (warning (G_ "opam: package '~a' not found~%") name)))
>>
>> -(define* (opam->guix-package name #:key (repo 'opam) version)
>> +(define (opam->guix-source url-dict)
>> + (let ((source-url (and url-dict
>> + (or (metadata-ref url-dict "src")
>> + (metadata-ref url-dict "archive")))))
>> + (if source-url
>> + (call-with-temporary-output-file
>> + (lambda (temp port)
>> + (and (url-fetch source-url temp)
>> + `(origin
>> + (method url-fetch)
>> + (uri ,source-url)
>> + (sha256 (base32 ,(guix-hash-url temp)))))))
>> + 'no-source-information)))
>
>I would use ‘and-let*’ instead of ‘let’, and drop the ‘if’ form.

That would return #f and make the ocher and-let* in opam->guix-package fail.

Toggle quote (6 lines)
>
>The ability to import from a local file seems to mostly be useful when
>the package isn’t meant to be submitted to Guix, right? Wouldn’t it
>make more sense to make the source a ‘local-file’ object instead of just
>setting it to #f?

Excellent idea.

Toggle quote (115 lines)
>
>> +(define* (opam->guix-package name #:key (repo '("opam")) (file #f) version)
>> "Import OPAM package NAME from REPOSITORY (a directory name) or, if
>> -REPOSITORY is #f, from the official OPAM repository. Return a 'package' sexp
>> +REPOSITORY is #f, from the official OPAM repository. When FILE is defined,
>> +import the packaged defined in the opam FILE instead. Return a 'package' sexp
>
>s/packaged/package/
>
>> (and-let* ((with-opam (if (member "opam" repo) repo (cons "opam" repo)))
>> - (opam-file (opam-fetch name with-opam))
>> - (version (assoc-ref opam-file "version"))
>> + (opam-file (opam-fetch name with-opam file))
>> (opam-content (assoc-ref opam-file "metadata"))
>> - (url-dict (metadata-ref opam-content "url"))
>> - (source-url (or (metadata-ref url-dict "src")
>> - (metadata-ref url-dict "archive")))
>> + (source (opam->guix-source (metadata-ref opam-content "url")))
>> (requirements (metadata-ref opam-content "depends"))
>> (names (dependency-list->names requirements))
>> (dependencies (filter-dependencies names))
>> @@ -344,47 +359,41 @@ (define* (opam->guix-package name #:key (repo 'opam) version)
>> (not (member name '("dune" "jbuilder"))))
>> native-dependencies))))
>> (let ((use-dune? (member "dune" names)))
>> - (call-with-temporary-output-file
>> - (lambda (temp port)
>> - (and (url-fetch source-url temp)
>> - (values
>> - `(package
>> - (name ,(ocaml-name->guix-name name))
>> - (version ,version)
>> - (source
>> - (origin
>> - (method url-fetch)
>> - (uri ,source-url)
>> - (sha256 (base32 ,(guix-hash-url temp)))))
>> - (build-system ,(if use-dune?
>> - 'dune-build-system
>> - 'ocaml-build-system))
>> - ,@(if (null? inputs)
>> - '()
>> - `((propagated-inputs (list ,@inputs))))
>> - ,@(if (null? native-inputs)
>> - '()
>> - `((native-inputs (list ,@native-inputs))))
>> - ,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
>> - '()
>> - `((properties
>> - ,(list 'quasiquote `((upstream-name . ,name))))))
>> - (home-page ,(metadata-ref opam-content "homepage"))
>> - (synopsis ,(metadata-ref opam-content "synopsis"))
>> - (description ,(beautify-description
>> - (metadata-ref opam-content "description")))
>> - (license ,(spdx-string->license
>> - (metadata-ref opam-content "license"))))
>> - (filter
>> - (lambda (name)
>> - (not (member name '("dune" "jbuilder"))))
>> - dependencies))))))))
>> + (values
>> + `(package
>> + (name ,(and name (ocaml-name->guix-name name)))
>> + (version ,(assoc-ref opam-file "version"))
>> + (source ,source)
>> + (build-system ,(if use-dune?
>> + 'dune-build-system
>> + 'ocaml-build-system))
>> + ,@(if (null? inputs)
>> + '()
>> + `((propagated-inputs (list ,@inputs))))
>> + ,@(if (null? native-inputs)
>> + '()
>> + `((native-inputs (list ,@native-inputs))))
>> + ,@(if (and name (equal? name (guix-name->opam-name (ocaml-name->guix-name name))))
>> + '()
>> + `((properties
>> + ,(list 'quasiquote `((upstream-name . ,name))))))
>> + (home-page ,(metadata-ref opam-content "homepage"))
>> + (synopsis ,(metadata-ref opam-content "synopsis"))
>> + (description ,(beautify-description
>> + (metadata-ref opam-content "description")))
>> + (license ,(spdx-string->license
>> + (metadata-ref opam-content "license"))))
>> + (filter
>> + (lambda (name)
>> + (not (member name '("dune" "jbuilder"))))
>> + dependencies)))))
>>
>> -(define* (opam-recursive-import package-name #:key repo)
>> +(define* (opam-recursive-import package-name #:key repo file)
>> (recursive-import package-name
>> #:repo->guix-package opam->guix-package
>> #:guix-name ocaml-name->guix-name
>> - #:repo repo))
>> + #:repo repo
>> + #:file file))
>>
>> (define (guix-name->opam-name name)
>> (if (string-prefix? "ocaml-" name)
>> diff --git a/guix/import/utils.scm b/guix/import/utils.scm
>> index 1c3cfa3e0b..ab35b8a4fc 100644
>> --- a/guix/import/utils.scm
>> +++ b/guix/import/utils.scm
>> @@ -470,7 +470,7 @@ (define (topological-sort nodes
>> (set-insert (node-name head) visited))))))))
>>
>> (define* (recursive-import package-name
>> - #:key repo->guix-package guix-name version repo
>> + #:key repo->guix-package guix-name version repo file
>> #:allow-other-keys)
>> "Return a list of package expressions for PACKAGE-NAME and all its
>> dependencies, sorted in topological order. For each package,
>
>The docstrings should also be updated.
X
X
Xinglu Chen wrote on 9 Feb 14:34 +0100
87tud8w4da.fsf@yoctocell.xyz
Hi,

Julien schrieb am Montag der 07. Februar 2022 um 09:46 +01:

Toggle quote (19 lines)
>>> +(define (opam->guix-source url-dict)
>>> + (let ((source-url (and url-dict
>>> + (or (metadata-ref url-dict "src")
>>> + (metadata-ref url-dict "archive")))))
>>> + (if source-url
>>> + (call-with-temporary-output-file
>>> + (lambda (temp port)
>>> + (and (url-fetch source-url temp)
>>> + `(origin
>>> + (method url-fetch)
>>> + (uri ,source-url)
>>> + (sha256 (base32 ,(guix-hash-url temp)))))))
>>> + 'no-source-information)))
>>
>>I would use ‘and-let*’ instead of ‘let’, and drop the ‘if’ form.
>
> That would return #f and make the ocher and-let* in opam->guix-package
> fail.

Right, I missed the ‘and-let*’ in ‘opam->guix-package’. However, seeing
as the ‘source’ variable is only used in the body of the ‘and-let*’, I
think it would make sense to not even bind the result of
‘opam->guix-source’ to any variable, instead using it directly in the
‘package’ form.

`(package
(source ,source)
…)

becomes

`(package
(source ,(opam->guix-source (metadata-ref opam-content "url")))
…)

WDYT?
-----BEGIN PGP SIGNATURE-----

iQJJBAEBCAAzFiEEAVhh4yyK5+SEykIzrPUJmaL7XHkFAmIDwuEVHHB1YmxpY0B5
b2N0b2NlbGwueHl6AAoJEKz1CZmi+1x5yJYQALpJEYSpJUAkAn7NtkD2v7qvC2q/
VOXdrxDv6T9hMUE62OVe+Tqfy1PIwCVCFeHDldaHg+GrGONig5bFfSeqN/QpsjGp
K4qU2HiA5XUwoDtpXtSBaNgAiJtFQ8FWc95EwPAM1sM1FDDwq31uQDIcaBp8xIiS
f8bvOm6RaOJFYvYecWpqhOkfEjkm6hjBKW5BKnhQGis/ItTPlT17m7TFZ5E/BOSu
Wi699NBbaXPqk65OSYozMt514cKwED5zNEtDmCKHtmAJX7CQ//0+Zb/cciqysbgH
FcjhnaR2/CPfg2lI2xiQrVEaAWHG8Ymzw2bMFtvmMVPxk/OTTt6e570QUuQvXr1l
JZV6p1Jn3UB93NpE4EDPhmsKrtMlCPva64bBMdaRwG65+/i/UL82sr909TDZPKM5
FruC+l918rIESBJcEuOOYGHJRb6yomeI6UmkH30++PgwIvJWIxpdwBgUN8XgHEb3
MWWGzsrurbaO4zatPNFC68b5/pkwONfadFXSQyn2osaVXDLzrwRBkjN190YMkJbw
gL/KKi99BxWtxd3VE0WVzWhhU8i4dDlRZLtpPfriaSt+W/bwc9ewd6j3+EtrOjlo
eDnPUvlk7veRROLHjSkA3a4P7u0eeEQxD/kuICzg915yDmJ04Cee92zJiUgaXFnO
7EjqITOt2RX/hQdK
=RitB
-----END PGP SIGNATURE-----

J
J
Julien Lepiller wrote on 12 Jun 08:31 +0200
[PATCH v2] import: opam: Allow importing local files.
(address . 53828@debbugs.gnu.org)
20220612063121.26914-1-julien@lepiller.eu
* guix/scripts/import/opam.scm (guix-import-opam): Support `--scan-project` flag.
* guix/import/opam.scm (opam-scan-projects): New procedure.
---
guix/import/opam.scm | 32 ++++++++++++++++++++++++++++++--
guix/scripts/import/opam.scm | 11 +++++++++++
2 files changed, 41 insertions(+), 2 deletions(-)

Toggle diff (97 lines)
diff --git a/guix/import/opam.scm b/guix/import/opam.scm
index b4b5a6eaad..3989dff58e 100644
--- a/guix/import/opam.scm
+++ b/guix/import/opam.scm
@@ -32,9 +32,11 @@ (define-module (guix import opam)
   #:use-module (srfi srfi-2)
   #:use-module ((srfi srfi-26) #:select (cut))
   #:use-module ((web uri) #:select (string->uri uri->string))
-  #:use-module ((guix build utils) #:select (dump-port find-files mkdir-p))
+  #:use-module ((guix build utils) #:select (dump-port find-files mkdir-p
+                                             delete-file-recursively))
   #:use-module (guix build-system)
   #:use-module (guix build-system ocaml)
+  #:use-module (guix git)
   #:use-module (guix http-client)
   #:use-module (guix ui)
   #:use-module (guix packages)
@@ -48,7 +50,8 @@ (define-module (guix import opam)
                                               spdx-string->license
                                               url-fetch))
   #:use-module ((guix licenses) #:prefix license:)
-  #:export (opam->guix-package
+  #:export (opam-scan-projects
+            opam->guix-package
             opam-recursive-import
             %opam-updater
 
@@ -178,6 +181,31 @@ (define* (get-opam-repository #:optional (repo "opam"))
 ;; Prevent Guile 3 from inlining this procedure so we can mock it in tests.
 (set! get-opam-repository get-opam-repository)
 
+(define (opam-scan-dir dir)
+  (let* ((opam-files (find-files dir "\\.opam$"))
+         (dir (opam-cache-directory dir))
+         (packages (string-append dir "/packages")))
+    (when (file-exists? dir)
+      (delete-file-recursively dir))
+    (mkdir-p packages)
+    (for-each
+      (lambda (package)
+        (let* ((name (basename package ".opam"))
+               (version (metadata-ref (get-metadata package) "version"))
+               (file (string-append packages "/" name "." version "/opam")))
+          (mkdir-p (dirname file))
+          (copy-file package file)))
+      opam-files)
+    dir))
+
+(define (opam-scan-project project)
+  (if (file-exists? project)
+    (opam-scan-dir project)
+    (opam-scan-dir (update-cached-checkout project))))
+
+(define (opam-scan-projects projects)
+  (map opam-scan-project projects))
+
 (define (get-version-and-file path)
   "Analyse a candidate path and return an list containing information for proper
   version comparison as well as the source path for metadata."
diff --git a/guix/scripts/import/opam.scm b/guix/scripts/import/opam.scm
index 834ac34cb0..0b15a81541 100644
--- a/guix/scripts/import/opam.scm
+++ b/guix/scripts/import/opam.scm
@@ -50,6 +50,10 @@ (define (show-help)
       --repo             import packages from this opam repository (name, URL or local path)
                          can be used more than once"))
   (display (G_ "
+  -p, --scan-project     import packages from this OCaml project (URL of a
+                         Git repository or local path).  Can be used more
+                         than once."))
+  (display (G_ "
   -V, --version          display version information and exit"))
   (newline)
   (show-bug-report-information))
@@ -69,6 +73,9 @@ (define %options
          (option '(#\r "recursive") #f #f
                  (lambda (opt name arg result)
                    (alist-cons 'recursive #t result)))
+         (option '(#\p "scan-project") #t #f
+                 (lambda (opt name arg result)
+                   (alist-cons 'project arg result)))
          %standard-import-options))
 
 
@@ -86,6 +93,10 @@ (define (parse-options)
          (repo (filter-map (match-lambda
                              (('repo . name) name)
                              (_ #f)) opts))
+         (projects (filter-map (match-lambda
+                                 (('project . name) name)
+                                 (_ #f)) opts))
+         (repo (append repo (opam-scan-projects projects)))
          (args (filter-map (match-lambda
                             (('argument . value)
                              value)
-- 
2.35.1
J
J
Julien Lepiller wrote on 12 Jun 08:37 +0200
(address . 53828@debbugs.gnu.org)
20220612083728.4f6984e7@sybil.lepiller.eu
As you can see, I've reworked the patch a little. Instead of a special
#:file argument to the importer, I simply create an opam repository out
of a given "project" (if you have a better name...). A "project" is an
upstream source repository, either a local checkout or a remote git
repository, that contain .opam files.

The new version of the patch scans the project for opam packages and
adds them to a local repository. When importing recursively, they
become available, so countrary to the previous patch, you can use this
for recursive import even when multiple packages are not in the opam
repository but in the project (some projects split their code into
multiple opam packages, like coq for instance). You can specify
--scan-project multiple times, so you can import from dependent repos
too.

WDYT? :)
?