Subject: [bug#74633] [PATCH v4] ui: Search channels for guix extensions

From: Brian Kubisiak <[email-masked]>
Date: Mon, 17 Feb 2025 07:41:46 -0800

* guix/describe.scm (append-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 | 15 +++++++++++++++
 guix/self.scm     |  1 +
 guix/ui.scm       | 27 +++++++++++++++++++++++----
 4 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/gnu/packages.scm b/gnu/packages.scm
index bdd5d21940..ee99dea2ca 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.
+    (append-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..819f0fef74 100644
--- a/guix/describe.scm
+++ b/guix/describe.scm
@@ -28,12 +28,14 @@ (define-module (guix describe)
                                 manifest-entry-channel)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-34)
+  #:use-module (srfi srfi-71)
   #:use-module (ice-9 match)
   #:export (current-profile
             current-profile-date
             current-profile-entries
             current-channels
             package-path-entries
+            append-channels-to-load-path!
 
             package-provenance
             package-channels
@@ -190,6 +192,19 @@ (define (package-path-entries)
                                       "/site-ccache")))
                (current-channel-entries))))
 
+(define (append-channels-to-load-path!)
+  "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 procedure ensures that channels are only added to the search path once
+even if it is called multiple times."
+  (let ((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)))
+  (set! append-channels-to-load-path! (lambda () #t)))
+
 (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 87a448bf72..3cc15b05fc 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -2194,9 +2194,24 @@ (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"))))
+  ;; We need to resolve these lazily, because even using an #:autoload is too
+  ;; much and breaks compilation during "guix pull".
+  (define append-channels-to-load-path!
+    (module-ref (resolve-module `(guix describe))
+                (symbol-append 'append-channels-to-load-path!)))
+  (define package-path-entries
+    (module-ref (resolve-module `(guix describe))
+                (symbol-append 'package-path-entries)))
+  (append-channels-to-load-path!)
+  (let ((channels (package-path-entries)))
+    (filter file-exists?
+            (parse-path
+             (getenv "GUIX_EXTENSIONS_PATH")
+             (append
+              (map (cut string-append <> "/guix/scripts")
+                   channels)
+              (map (cut string-append <> "/guix/extensions")
+                   channels))))))
 
 (define (commands)
   "Return the list of commands, alphabetically sorted."
@@ -2284,7 +2299,11 @@ (define (run-guix-command command . args)
              (show-guix-usage)))))
       (file
        (load file)
-       (resolve-interface `(guix extensions ,command)))))
+       (let ((maybe-extension-path
+              (format #f "/guix/extensions/~a.scm" command)))
+         (if (string-suffix? maybe-extension-path file)
+             (resolve-interface `(guix extensions ,command))
+             (resolve-interface `(guix scripts ,command)))))))
 
   (let ((command-main (module-ref module
                                   (symbol-append 'guix- command))))

base-commit: 0cd02f5e083abac280f6478cb8fda16a0a02e789
-- 
2.48.1