Subject: [PATCH 01/43] guix: packages: Extend bag-build to support gexp.

From: Nicolas Graves <[email-masked]>
Date: Tue, 12 Aug 2025 11:04:09 +0200

Rationale: The goal of this proposition was to enable guix users to
edit build-system lower builders on-the-fly, which is currently not
possible.  This is done by keeping a higher-level representation of
the builder - a gexp rather than a derivation - as the bag-build
field.

* guix/build-system.scm (<bag>): Update comment.
* guix/packages.scm
(bag->derivation): Handle gexp bag-build. Rename current procedure to...
(bag-builder):... this procedure.
(bag->cross-derivation): Rename procedure to...
(bag-cross-builder):... this procedure.

Change-Id: I56c5a9dab9954307f95b29eab5e02ee058271684
---
 guix/build-system.scm |  2 +-
 guix/packages.scm     | 53 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/guix/build-system.scm b/guix/build-system.scm
index 76d670995ce..a4dcdc52d85 100644
--- a/guix/build-system.scm
+++ b/guix/build-system.scm
@@ -79,7 +79,7 @@ (define-record-type* <bag> bag %make-bag
                  (default '("out")))
   (arguments     bag-arguments           ;list
                  (default '()))
-  (build         bag-build))             ;bag -> derivation
+  (build         bag-build))             ;bag -> gexp or derivation
 
 (define* (make-bag build-system name
                    #:key source (inputs '()) (native-inputs '())
diff --git a/guix/packages.scm b/guix/packages.scm
index 1d5986c5f42..1df9cb67f02 100644
--- a/guix/packages.scm
+++ b/guix/packages.scm
@@ -12,6 +12,7 @@
 ;;; Copyright © 2023 Simon Tournier <[email-masked]>
 ;;; Copyright © 2024 Janneke Nieuwenhuizen <[email-masked]>
 ;;; Copyright © 2024 David Elsing <[email-masked]>
+;;; Copyright © 2024 Nicolas Graves <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -51,6 +52,7 @@ (define-module (guix packages)
   #:use-module (ice-9 match)
   #:use-module (ice-9 vlist)
   #:use-module (ice-9 regex)
+  #:use-module (ice-9 optargs)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9 gnu)
   #:use-module (srfi srfi-26)
@@ -1967,12 +1969,12 @@ (define (input=? input1 input2)
                       (derivation=? obj1 obj2))
                  (equal? obj1 obj2))))))))
 
-(define* (bag->derivation bag #:optional context)
-  "Return the derivation to build BAG for SYSTEM.  Optionally, CONTEXT can be
-a package object describing the context in which the call occurs, for improved
-error reporting."
+(define* (bag-builder bag #:optional context)
+  "Return the gexp or derivation to build BAG for SYSTEM.  Optionally, CONTEXT
+can be a package object describing the context in which the call occurs, for
+improved error reporting."
   (if (bag-target bag)
-      (bag->cross-derivation bag)
+      (bag-cross-builder bag)
       (mlet* %store-monad ((system ->  (bag-system bag))
                            (inputs ->  (bag-transitive-inputs bag))
                            (input-drvs (mapm %store-monad
@@ -1994,10 +1996,10 @@ (define* (bag->derivation bag #:optional context)
                #:outputs (bag-outputs bag) #:system system
                (bag-arguments bag)))))
 
-(define* (bag->cross-derivation bag #:optional context)
-  "Return the derivation to build BAG, which is actually a cross build.
-Optionally, CONTEXT can be a package object denoting the context of the call.
-This is an internal procedure."
+(define* (bag-cross-builder bag #:optional context)
+  "Return the gexp or derivation to build BAG, which is actually a cross
+build. Optionally, CONTEXT can be a package object denoting the context of the
+call. This is an internal procedure."
   (mlet* %store-monad ((system ->   (bag-system bag))
                        (target ->   (bag-target bag))
                        (host ->     (bag-transitive-host-inputs bag))
@@ -2038,6 +2040,39 @@ (define* (bag->cross-derivation bag #:optional context)
            #:system system #:target target
            (bag-arguments bag))))
 
+(define* (bag->derivation bag #:optional context)
+  "Return the derivation to build BAG for SYSTEM.  Optionally, CONTEXT can be
+a package object describing the context in which the call occurs, for improved
+error reporting."
+  (mlet %store-monad ((builder (bag-builder bag context)))
+    (match builder
+      ((? derivation? drv)
+       (return drv))
+      ((? gexp? gexp)
+       (let-keywords (bag-arguments bag) #t
+                     ((allowed-references    #f)
+                      (disallowed-references #f)
+                      (guile                 #f)
+                      (substitutable?        #t))
+         (mlet %store-monad
+             ((guile (package->derivation (or guile (default-guile))
+                                          (bag-system bag)
+                                          #:graft? #f)))
+           ;; Note: Always pass #:graft? #f.  Without it, ALLOWED-REFERENCES &
+           ;; co. would be interpreted as referring to grafted packages.
+           (gexp->derivation (bag-name bag) gexp
+                             #:system (bag-system bag)
+                             #:target (and (bag-target bag))
+                             #:graft? #f
+                             #:substitutable? substitutable?
+                             #:allowed-references allowed-references
+                             #:disallowed-references disallowed-references
+                             #:guile-for-build guile))))
+      ;; build-bag has to be drv or gexp, else raise.
+      (_
+       (raise (condition (&package-error
+                          (package context))))))))
+
 (define bag->derivation*
   (store-lower bag->derivation))
 
-- 
2.50.1