[bug#74633,v2] ui: Search channels for guix extensions

Message ID 2c078699007e847611b7a20679621813691d9db3.1733695255.git.brian@kubisiak.com
State New
Headers
Series [bug#74633,v2] ui: Search channels for guix extensions |

Commit Message

Brian Kubisiak Dec. 8, 2024, 10:03 p.m. UTC
  * guix/describe.scm (add-channels-to-load-path!): New function.
* gnu/packages.scm (%package-module-path): Call new function. Remove
the code that the function call replaces.
* guix/ui.scm (extension-directories): Call new function. Search
channels for guix extensions.
* guix/self.scm (compiled-guix)[*core-modules*]: Add 'guile-git' to
the list of extensions.

Change-Id: I53af828dc554485ca28389c9e2653ea6b4fb6b7e
---
 gnu/packages.scm  |  7 ++++---
 guix/describe.scm | 17 +++++++++++++++++
 guix/self.scm     |  1 +
 guix/ui.scm       | 12 +++++++++---
 4 files changed, 31 insertions(+), 6 deletions(-)


base-commit: 9001514e242ad15c190588439930b0fa4f6782e3
prerequisite-patch-id: cd0707c90e1d321f3f16f2f861313dd330e9f0b4
--
2.46.0
  

Comments

Ludovic Courtès Dec. 26, 2024, 9:08 p.m. UTC | #1
Hi Brian,

Brian Kubisiak <brian@kubisiak.com> skribis:

> * guix/describe.scm (add-channels-to-load-path!): New function.
> * gnu/packages.scm (%package-module-path): Call new function. Remove
> the code that the function call replaces.
> * guix/ui.scm (extension-directories): Call new function. Search
> channels for guix extensions.
> * guix/self.scm (compiled-guix)[*core-modules*]: Add 'guile-git' to
> the list of extensions.
>
> Change-Id: I53af828dc554485ca28389c9e2653ea6b4fb6b7e

Overall LGTM.  I tested it with ‘make as-derivation’ and it works as
advertised; ‘strace -c’ shows that the number of syscalls is comparable
to that we currently have.

A couple of minor comments:

> +++ b/gnu/packages.scm
> @@ -148,15 +148,16 @@ (define %package-module-path
>    (let* ((not-colon   (char-set-complement (char-set #\:)))
>           (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
>                                         not-colon))
> -         (channels-scm channels-go (package-path-entries)))
> +         (channels-scm (package-path-entries)))

This variable is now unused; I think it can be removed.

> +(define add-channels-to-load-path!
> +  (let ((promise
> +         (delay
> +           (let-values (((channels-scm channels-go) (package-path-entries)))
> +             (set! %load-path
> +                   (append %load-path channels-scm))
> +             (set! %load-compiled-path
> +                   (append %load-compiled-path channels-go))))))
> +    (lambda ()
> +      "Automatically add channels to Guile's search path.  Channels are added
> +to the end of the path so they don't override Guix' own modules.  This
> +function ensures that channels are only added to the search path once even if
> +it is called multiple times."
> +      (force promise))))

For clarity, I would call this ‘append-channels-to-load-path!’.

Using a promise here works, but I find it slightly misleading (because
we’re using it for side effects) and a bit heavyweight (promises are
thread-safe, so there’s a mutex etc.).

How about this:

  (define (append-channels-to-load-path!)
    (let-values (…)
      …)
    (set! append-channels-to-load-path! (lambda () #t)))

?

Could you send a v3 along these lines, if you think that makes sense?

Thanks,
Ludo’.
  

Patch

diff --git a/gnu/packages.scm b/gnu/packages.scm
index 1af3b8d440..42f3546606 100644
--- a/gnu/packages.scm
+++ b/gnu/packages.scm
@@ -148,15 +148,16 @@  (define %package-module-path
   (let* ((not-colon   (char-set-complement (char-set #\:)))
          (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
                                        not-colon))
-         (channels-scm channels-go (package-path-entries)))
+         (channels-scm (package-path-entries)))
     ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
     ;; search path.  For historical reasons, $GUIX_PACKAGE_PATH goes to the
     ;; front; channels go to the back so that they don't override Guix' own
     ;; modules.
+    (add-channels-to-load-path!)
     (set! %load-path
-      (append environment %load-path channels-scm))
+      (append environment %load-path))
     (set! %load-compiled-path
-      (append environment %load-compiled-path channels-go))
+      (append environment %load-compiled-path))

     (make-parameter
      (append environment
diff --git a/guix/describe.scm b/guix/describe.scm
index a4ca2462f4..6694b1eaf7 100644
--- a/guix/describe.scm
+++ b/guix/describe.scm
@@ -27,6 +27,7 @@  (define-module (guix describe)
                                 sexp->channel
                                 manifest-entry-channel)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-34)
   #:use-module (ice-9 match)
   #:export (current-profile
@@ -34,6 +35,7 @@  (define-module (guix describe)
             current-profile-entries
             current-channels
             package-path-entries
+            add-channels-to-load-path!

             package-provenance
             package-channels
@@ -190,6 +192,21 @@  (define (package-path-entries)
                                       "/site-ccache")))
                (current-channel-entries))))

+(define add-channels-to-load-path!
+  (let ((promise
+         (delay
+           (let-values (((channels-scm channels-go) (package-path-entries)))
+             (set! %load-path
+                   (append %load-path channels-scm))
+             (set! %load-compiled-path
+                   (append %load-compiled-path channels-go))))))
+    (lambda ()
+      "Automatically add channels to Guile's search path.  Channels are added
+to the end of the path so they don't override Guix' own modules.  This
+function ensures that channels are only added to the search path once even if
+it is called multiple times."
+      (force promise))))
+
 (define (package-channels package)
   "Return the list of channels providing PACKAGE or an empty list if it could
 not be determined."
diff --git a/guix/self.scm b/guix/self.scm
index 2652688c71..28239d53f5 100644
--- a/guix/self.scm
+++ b/guix/self.scm
@@ -882,6 +882,7 @@  (define* (compiled-guix source #:key
                     ,(local-file "../guix/store/schema.sql")))

                  #:extensions (list guile-gcrypt
+                                    guile-git     ;for (guix git)
                                     guile-json)   ;for (guix swh)
                  #:guile-for-build guile-for-build))

diff --git a/guix/ui.scm b/guix/ui.scm
index eba12c8616..87ae24ea55 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -38,6 +38,7 @@ 
 (define-module (guix ui)                       ;import in user interfaces only
   #:use-module (guix i18n)
   #:use-module (guix colors)
+  #:use-module (guix describe)
   #:use-module (guix diagnostics)
   #:use-module (guix gexp)
   #:use-module (guix sets)
@@ -2192,9 +2193,14 @@  (define* (command-files #:optional directory)

 (define (extension-directories)
   "Return the list of directories containing Guix extensions."
-  (filter file-exists?
-          (parse-path
-           (getenv "GUIX_EXTENSIONS_PATH"))))
+  (add-channels-to-load-path!)
+  (let ((channels (package-path-entries)))
+    (filter file-exists?
+            (parse-path
+             (getenv "GUIX_EXTENSIONS_PATH")
+             (map
+              (cut string-append <> "/guix/extensions")
+              channels)))))

 (define (commands)
   "Return the list of commands, alphabetically sorted."