guix_mirror_bot pushed a commit to branch master
in repository guix.

commit f2337457735d9a1f54083b1504c5b94bcdd54d1f
Author: Sergio Pastor Pérez <[email protected]>
AuthorDate: Mon Apr 20 13:11:45 2026 +0200

    gexp: Support 'environment-variable' as gexp inputs.
    
    * doc/guix.texi (G-Expressions): Document 'environment-variable'.
    * guix/gexp.scm (filterm): Define procedure top-level to reuse it.
    (&gexp-duplicate-environment-variable-error): New error condition.
    (lower-inputs): Adjust logic to ignore <environment-variable> inputs.
    (<environment-variable>): New record type.
    (<lowered-gexp>): Add environment field.
    (lower-gexp): Handle inputs containing <environment-variable> records.
    (gexp->derivation): Lower <environment-variable> inputs from gexp.
    (gexp-inputs): Collect <environment-variable> inputs and input references
    within them.
    (assert-no-duplicated-environment-variables): New procedure
    (lower-environment-variables): New procedure.
    (gexp->sexp): Expand <environment-variable> inputs.
    * guix/ui.scm (call-with-error-handling): Add case for 
'gexp-duplicate-environment-variable-error?'.
    * tests/gexp.scm ("gexp->derivation vs. grafts (environment-variable)",
    "gexp->derivation, composed gexps (environment-variable)",
    "gexp->derivation, file-append (environment-variable)",
    "gexp->derivation, local-file (environment-variable)",
    "gexp->derivation, cross-compilation (environment-variable)",
    "gexp->derivation, ungexp-native (environment-variable)",
    "gexp->derivation, ungexp + ungexp-native (environment-variable)",
    "gexp->derivation, ungexp-native + composed gexps (environment-variable)",
    "lower-gexp, sexp (environment-variable)",
    "lower-gexp, composed gexps (environment-variable)",
    "lower-gexp, composed gexp with package output (environment-variable)",
    "lower-gexp, duplicated (environment-variable)",
    "lower-object, with-transformation, list (environment-variable)"): New test.
    
    Signed-off-by: Ludovic Courtès <[email protected]>
---
 doc/guix.texi  |  55 +++++++++++++++
 guix/gexp.scm  | 131 +++++++++++++++++++++++++++++++++--
 guix/ui.scm    |   6 ++
 tests/gexp.scm | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 396 insertions(+), 7 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index a1dc98a2496..a0532e67a25 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -13283,6 +13283,14 @@ dependencies are added to those of the containing gexp.
 
 If @var{obj} is another kind of object, it is inserted as is.
 
+A special kind of object that can appear inside a gexp is
+@dfn{environment variables}.  Those are produced using the
+`environment-variable` procedure which does two things: define the
+environment variable and its value, and produce code to retrieve its
+value.  The value of an environment variable can be either a file-like
+object (as discussed above) or a gexp.  @xref{environment-variable, the
+@code{environment-variable} reference below}.
+
 @item #$@var{obj}:@var{output}
 @itemx (ungexp @var{obj} @var{output})
 This is like the form above, but referring explicitly to the
@@ -13700,6 +13708,53 @@ the second case, the resulting script contains a 
@code{(string-append
 @dots{})} expression to construct the file name @emph{at run time}.
 @end deffn
 
+@anchor{environment-variable}
+@deffn {Procedure} environment-variable name expression
+Return an object representing an environment variable definition and
+reference that retrieves the value of @var{expression} stored in an
+environment variable named @var{name}.
+
+As an example, consider this gexp:
+
+@lisp
+(use-modules (guix gexp) (gnu packages base))
+
+(gexp->script "run-uname"
+              #~(system* #$(environment-variable
+                            "UNAME" (file-append coreutils "/bin/uname"))))
+@end lisp
+
+Expands to:
+
+@lisp
+(run-with-store (open-connection)
+  (gexp->sexp #~(system* #$(environment-variable
+                            "UNAME" (file-append coreutils "/bin/uname")))
+              (%current-system) (%current-target-system)))
+=>
+
+(system* ((@ (guile) call-with-input-string)
+          ((@ (guile) getenv) "UNAME")
+          (@ (guile) read)))
+@end lisp
+
+One can reference a specific output of a package by using a
+@code{gexp-input}.  @xref{gexp-input, the @code{gexp-input} reference
+below}.
+
+For example, one could reference the bin output of @code{libevent} like
+this:
+
+@lisp
+(run-with-store (open-connection)
+  (gexp->script "run-event_rcpgen"
+                #~(system* #$(environment-variable
+                              "LIBEVENT:debug"
+                              (file-append (gexp-input libevent "bin")
+                                           "/bin/event_rpcgen.py")))))
+@end lisp
+@end deffn
+
 @defmac let-system system body@dots{}
 @defmacx let-system (system target) body@dots{}
 Bind @var{system} to the currently targeted system---e.g.,
diff --git a/guix/gexp.scm b/guix/gexp.scm
index f7eddcdd4d0..3d87026ddb2 100644
--- a/guix/gexp.scm
+++ b/guix/gexp.scm
@@ -7,6 +7,7 @@
 ;;; Copyright © 2021, 2022 Maxime Devos <[email protected]>
 ;;; Copyright © 2025 Tomas Volf <[email protected]>
 ;;; Copyright © 2026 David Elsing <[email protected]>
+;;; Copyright © 2026 Sergio Pastor Pérez <[email protected]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -122,9 +123,15 @@
             load-path-expression
             gexp-modules
 
+            environment-variable
+            environment-variable?
+            environment-variable-name
+            environment-variable-exp
+
             lower-gexp
             lowered-gexp?
             lowered-gexp-sexp
+            lowered-gexp-environment
             lowered-gexp-inputs
             lowered-gexp-sources
             lowered-gexp-guile
@@ -159,7 +166,10 @@
             gexp-error?
             &gexp-input-error
             gexp-input-error?
-            gexp-error-invalid-input))
+            gexp-error-invalid-input
+            &gexp-duplicate-environment-variable-error
+            gexp-duplicate-environment-variable-error?
+            gexp-duplicate-environment-variable-error-variable))
 
 ;; "G expressions".
 (define-record-type <gexp>
@@ -248,6 +258,11 @@ returned by 'gexp->sexp'."
   gexp-input-error?
   (input gexp-error-invalid-input))
 
+(define-condition-type &gexp-duplicate-environment-variable-error
+  &gexp-error
+  gexp-duplicate-environment-variable-error?
+  (variable gexp-duplicate-environment-variable-error-variable))
+
 
 (define %gexp-compilers
   ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
@@ -992,6 +1007,9 @@ list."
     (one-of symbol? string? keyword? pair? null? array?
             number? boolean? char?)))
 
+(define filterm
+  (lift1 (cut filter ->bool <>) %store-monad))
+
 (define (lower-inputs inputs system target)
   "Turn any object from INPUTS into a derivation input for SYSTEM or a store
 item (a \"source\"); return the corresponding input list as a monadic value.
@@ -999,14 +1017,13 @@ When TARGET is true, use it as the cross-compilation 
target triplet."
   (define (store-item? obj)
     (and (string? obj) (store-path? obj)))
 
-  (define filterm
-    (lift1 (cut filter ->bool <>) %store-monad))
-
   (with-monad %store-monad
     (>>= (mapm/accumulate-builds
           (match-lambda
             (($ <gexp-input> (? store-item? item))
              (return item))
+            (($ <gexp-input> (? environment-variable?))
+             (return #f))
             (($ <gexp-input> thing output native?)
              (mlet %store-monad ((obj (lower-object thing system
                                                     #:target
@@ -1077,12 +1094,44 @@ names and file names suitable for the 
#:allowed-references argument to
     (lambda (system)
       ((force proc) system))))
 
+;; Object that teaks an expression and extracts it as an environment variable.
+;; For example:
+;;
+;; #~#$(environment-variable "#:coreutils" coreutils)
+;;
+;; Expands to:
+;;
+;; ((@ (guile) call-with-input-string)
+;;  ((@ (guile) getenv) "#:coreutils")
+;;  (@ (guile) read))
+;;
+;; This is an abstraction that allows build-system builders to share a common
+;; script where the different expressions are retrieved from the '.drv' files
+;; environment field.
+(define-record-type <environment-variable>
+  (environment-variable name exp)
+  environment-variable?
+  (name   environment-variable-name)
+  (exp    environment-variable-exp))
+
+(define (write-environment-variable variable port)
+  (match variable
+    (($ <environment-variable> name (? file-like? file))
+     (format port "#<environment-variable ~s ~a ~x>"
+             name file (object-address file)))
+    (($ <environment-variable> name exp)
+     (format port "#<environment-variable ~s ~20@y ~x>"
+             name exp (object-address variable)))))
+
+(set-record-type-printer! <environment-variable> write-environment-variable)
+
 ;; Representation of a gexp instantiated for a given target and system.
 ;; It's an intermediate representation between <gexp> and <derivation>.
 (define-record-type <lowered-gexp>
-  (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
+  (lowered-gexp sexp environment inputs sources guile load-path 
load-compiled-path)
   lowered-gexp?
   (sexp                lowered-gexp-sexp)         ;sexp
+  (environment         lowered-gexp-environment)  ;environment
   (inputs              lowered-gexp-inputs)       ;list of <derivation-input>
   (sources             lowered-gexp-sources)      ;list of store items
   (guile               lowered-gexp-guile)        ;<derivation-input> | #f
@@ -1162,7 +1211,9 @@ derivations--e.g., code evaluated for its side effects."
                                   suffix))
                  extensions)))
 
-  (mlet* %store-monad ( ;; The following binding forces '%current-system' and
+  (assert-no-duplicated-environment-variables exp)
+
+  (mlet* %store-monad (;; The following binding forces '%current-system' and
                        ;; '%current-target-system' to be looked up at >>=
                        ;; time.
                        (graft?    (set-grafting graft?))
@@ -1174,6 +1225,8 @@ derivations--e.g., code evaluated for its side effects."
                        (guile     (if guile-for-build
                                       (return guile-for-build)
                                       (default-guile-derivation system)))
+                       (environment (lower-environment-variables
+                                     exp system target))
                        (inputs   (lower-inputs (gexp-inputs exp)
                                                system target))
                        (sexp     (gexp->sexp exp system target))
@@ -1204,6 +1257,7 @@ derivations--e.g., code evaluated for its side effects."
     (mbegin %store-monad
       (set-grafting graft?)                       ;restore the initial setting
       (return (lowered-gexp sexp
+                            environment
                             `(,@(if (derivation? modules)
                                     (list (derivation-input modules))
                                     '())
@@ -1333,6 +1387,7 @@ The other arguments are as for 'derivation'."
                                                          #:target target)
                                        (return #f)))
                        (guile -> (lowered-gexp-guile lowered))
+                       (environment -> (lowered-gexp-environment lowered))
                        (builder  (text-file script-name
                                             (sexp->string
                                              (lowered-gexp-sexp lowered)))))
@@ -1350,7 +1405,7 @@ The other arguments are as for 'derivation'."
                                       (lowered-gexp-load-compiled-path 
lowered))
                         ,builder)
                       #:outputs outputs
-                      #:env-vars env-vars
+                      #:env-vars (append environment env-vars)
                       #:system system
                       #:inputs `(,guile
                                  ,@(lowered-gexp-inputs lowered)
@@ -1410,6 +1465,25 @@ The other arguments are as for 'derivation'."
        (if (direct-store-path? str)
            (cons ref result)
            result))
+      ;; For <environment-variable>, return all the inputs it references, as
+      ;; well as itself.
+      (($ <gexp-input> ($ <environment-variable> name (? gexp? exp)) _ n?)
+       (cons ref
+             (append
+              (map (match-lambda
+                     (($ <gexp-input> thing output native?)
+                      (gexp-input thing output #:native? (or n? native?))))
+                   (gexp-inputs exp))
+              result)))
+      (($ <gexp-input> ($ <environment-variable> name (? file-like? file)) _ 
n?)
+       (cons* ref
+              (gexp-input file #:native? n?)
+              result))
+      ;; If this expression is reached, it means that 'thing' is a
+      ;; self-quoting object (number, string, etc.), therefore we just need to
+      ;; register <environment-variable> as an input.
+      (($ <gexp-input> ($ <environment-variable> name thing) _ n?)
+       (cons* ref result))
       (($ <gexp-input> (? struct? thing) output n?)
        (if (lookup-compiler thing)
            ;; THING is a derivation, or a package, or an origin, etc.
@@ -1463,6 +1537,46 @@ The other arguments are as for 'derivation'."
   (delete-duplicates
    (fold add-reference-output '() (gexp-references exp))))
 
+(define (assert-no-duplicated-environment-variables exp)
+  "Check that EXP does not contain two or more environment variable with the
+same name."
+  (fold-right
+   (lambda (input result)
+     (match input
+       (($ <gexp-input> (and variable ($ <environment-variable> name _)))
+        (if (member name result)
+            (raise (condition (&gexp-duplicate-environment-variable-error 
(variable variable))
+                              (&error-location (location (gexp-location 
exp)))))
+            (cons name result)))
+       (_ result)))
+   '()
+   (gexp-inputs exp)))
+
+(define (lower-environment-variables exp system target)
+  "Lower <environment-variable> from EXP into a <lowered-gexp> environment
+alist.
+
+The alist will be adjusted for SYSTEM, cross-compiling for TARGET."
+  (with-monad %store-monad
+    (>>=
+     (mapm %store-monad
+           (match-lambda
+             (($ <gexp-input> ($ <environment-variable> name exp) _ native?)
+              (mlet %store-monad
+                  ((sexp (cond
+                          ((gexp? exp)
+                           (gexp->sexp exp system (if native? #f target)))
+                          ((file-like? exp)
+                           (apply lower+expand-object exp system
+                                  (if native?
+                                      '() `(#:target ,target))))
+                          (else (return exp)))))
+                (return (cons name
+                              (sexp->string sexp)))))
+             (_ (return #f)))
+           (gexp-inputs exp))
+     filterm)))
+
 (define (gexp->sexp exp system target)
   "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
 and in the current monad setting (system type, etc.)"
@@ -1474,6 +1588,9 @@ and in the current monad setting (system type, etc.)"
          ;; an environment variable for each of them at build time, so use
          ;; that trick.
          (return `((@ (guile) getenv) ,output)))
+        (($ <gexp-input> ($ <environment-variable> name _))
+         (return `((@ (guile) call-with-input-string) ((@ (guile) getenv) 
,name)
+                   (@ (guile) read))))
         (($ <gexp-input> (? gexp? exp) output n?)
          (gexp->sexp exp
                      system (if (or n? native?) #f target)))
diff --git a/guix/ui.scm b/guix/ui.scm
index 758784a2d49..4a73e3e04bf 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -20,6 +20,7 @@
 ;;; Copyright © 2022 Taiju HIGASHI <[email protected]>
 ;;; Copyright © 2022 Liliana Marie Prikler <[email protected]>
 ;;; Copyright © 2025 Efraim Flashner <[email protected]>
+;;; Copyright © 2026 Sergio Pastor Pérez <[email protected]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -924,6 +925,11 @@ evaluating the tests and bodies of CLAUSES."
                 (leave (and (error-location? c) (error-location c))
                        (G_ "~s: invalid G-expression input~%")
                        (gexp-error-invalid-input c))))
+             ((gexp-duplicate-environment-variable-error? c)
+              (let ((variable 
(gexp-duplicate-environment-variable-error-variable c)))
+                (leave (and (error-location? c) (error-location c))
+                       (G_ "~s: duplicated environment variable name~%")
+                       variable)))
              ((profile-not-found-error? c)
               (leave (G_ "profile '~a' does not exist~%")
                      (profile-error-profile c)))
diff --git a/tests/gexp.scm b/tests/gexp.scm
index 3c2e90191d9..d81b9e25b9b 100644
--- a/tests/gexp.scm
+++ b/tests/gexp.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2014-2026 Ludovic Courtès <[email protected]>
 ;;; Copyright © 2021-2022 Maxime Devos <[email protected]>
 ;;; Copyright © 2026 David Elsing <[email protected]>
+;;; Copyright © 2026 Sergio Pastor Pérez <[email protected]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -868,6 +869,27 @@
                  (string=? (derivation->output-path drv0)
                            (derivation->output-path drv1*))))))
 
+(test-assertm "gexp->derivation vs. grafts (environment-variable)"
+  (mlet* %store-monad ((graft?  (set-grafting #f))
+                       (p0 ->   (dummy-package "dummy"
+                                               (arguments
+                                                '(#:implicit-inputs? #f))))
+                       (r  ->   (package (inherit p0) (name "DuMMY")))
+                       (p1 ->   (package (inherit p0) (replacement r)))
+                       (exp0 -> (gexp (frob (ungexp (environment-variable 
"var" p0))
+                                            (ungexp output))))
+                       (exp1 -> (gexp (frob (ungexp (environment-variable 
"var" p1))
+                                            (ungexp output))))
+                       (void    (set-guile-for-build %bootstrap-guile))
+                       (drv0    (gexp->derivation "t" exp0 #:graft? #t))
+                       (drv1    (gexp->derivation "t" exp1 #:graft? #t))
+                       (drv1*   (gexp->derivation "t" exp1 #:graft? #f))
+                       (_       (set-grafting graft?)))
+    (return (and (not (string=? (derivation->output-path drv0)
+                                (derivation->output-path drv1)))
+                 (string=? (derivation->output-path drv0)
+                           (derivation->output-path drv1*))))))
+
 (test-assertm "gexp->derivation, composed gexps"
   (mlet* %store-monad ((exp0 -> (gexp (begin
                                         (mkdir (ungexp output))
@@ -884,6 +906,24 @@
     (return (string=? (readlink (string-append out "/foo"))
                       guile))))
 
+(test-assertm "gexp->derivation, composed gexps (environment-variable)"
+  (mlet* %store-monad ((exp0 -> (gexp (begin
+                                        (mkdir (ungexp output))
+                                        (chdir (ungexp output)))))
+                       (exp1 -> (gexp (symlink
+                                       (string-append (ungexp 
(environment-variable
+                                                               
"%bootstrap-guile"
+                                                               
%bootstrap-guile))
+                                                      "/bin/guile")
+                                       "foo")))
+                       (exp  -> (gexp (begin (ungexp exp0) (ungexp exp1))))
+                       (drv     (gexp->derivation "foo" exp))
+                       (out ->  (derivation->output-path drv))
+                       (done    (built-derivations (list drv)))
+                       (guile   (package-file %bootstrap-guile "bin/guile")))
+    (return (string=? (readlink (string-append out "/foo"))
+                      guile))))
+
 (test-assertm "gexp->derivation, default system"
   ;; The default system should be the one at '>>=' time, not the one at
   ;; invocation time.  See <http://bugs.gnu.org/18002>.
@@ -909,6 +949,39 @@
       (return (string=? (readlink (derivation->output-path drv))
                         intd)))))
 
+(test-assertm "gexp->derivation, local-file (environment-variable)"
+  (mlet* %store-monad ((file ->  (search-path %load-path "guix.scm"))
+                       (intd     (interned-file file #:recursive? #f))
+                       (local -> (local-file file))
+                       (exp ->   (gexp (begin
+                                         (stat (ungexp (environment-variable
+                                                        "local" local)))
+                                         (symlink (ungexp (environment-variable
+                                                        "local" local))
+                                                  (ungexp output)))))
+                       (drv      (gexp->derivation "local-file" exp)))
+    (mbegin %store-monad
+      (built-derivations (list drv))
+      (return (string=? (readlink (derivation->output-path drv))
+                        intd)))))
+
+(test-assertm "gexp->derivation, file-append (environment-variable)"
+  (mlet* %store-monad ((name -> "UNAME")
+                       (relative-path -> "/bin/uname")
+                       (pkg  -> coreutils)
+                       (file -> (file-append pkg relative-path))
+                       (exp  -> (gexp (system* (ungexp
+                                                (environment-variable
+                                                 name file)))))
+                       (drv     (package->derivation pkg))
+                       (drv-out -> (derivation->output-path drv))
+                       (lexp    (lower-gexp
+                                 exp
+                                 #:effective-version
+                                 (bootstrap-guile-effective-version))))
+    (return (equal? `((,name . ,(string-append "\"" drv-out relative-path 
"\"")))
+                    (lowered-gexp-environment lexp)))))
+
 (test-assertm "gexp->derivation, cross-compilation"
   (mlet* %store-monad ((target -> "mips64el-linux-gnu")
                        (exp    -> (gexp (list (ungexp coreutils)
@@ -923,6 +996,20 @@
     (return (and (member (derivation-file-name xcu) refs)
                  (not (member (derivation-file-name cu) refs))))))
 
+(test-assertm "gexp->derivation, cross-compilation (environment-variable)"
+  (mlet* %store-monad ((target -> "mips64el-linux-gnu")
+                       (exp    -> (gexp (list (ungexp (environment-variable 
"coreutils" coreutils))
+                                              (ungexp output))))
+                       (xdrv      (gexp->derivation "foo" exp
+                                                    #:target target))
+                       (refs      (references*
+                                   (derivation-file-name xdrv)))
+                       (xcu       (package->cross-derivation coreutils
+                                                             target))
+                       (cu        (package->derivation coreutils)))
+    (return (and (member (derivation-file-name xcu) refs)
+                 (not (member (derivation-file-name cu) refs))))))
+
 (test-assertm "gexp->derivation, ungexp-native"
   (mlet* %store-monad ((target -> "mips64el-linux-gnu")
                        (exp    -> (gexp (list (ungexp-native coreutils)
@@ -933,6 +1020,18 @@
     (return (string=? (derivation-file-name drv)
                       (derivation-file-name xdrv)))))
 
+(test-assertm "gexp->derivation, ungexp-native (environment-variable)"
+  (mlet* %store-monad ((target -> "mips64el-linux-gnu")
+                       (exp    -> (gexp (list (ungexp-native
+                                               (environment-variable
+                                                "coreutils" coreutils))
+                                              (ungexp output))))
+                       (xdrv      (gexp->derivation "foo" exp
+                                                    #:target target))
+                       (drv       (gexp->derivation "foo" exp)))
+    (return (string=? (derivation-file-name drv)
+                      (derivation-file-name xdrv)))))
+
 (test-assertm "gexp->derivation, ungexp + ungexp-native"
   (mlet* %store-monad ((target -> "mips64el-linux-gnu")
                        (exp    -> (gexp (list (ungexp-native coreutils)
@@ -947,6 +1046,24 @@
     (return (and (member (derivation-file-name cu) refs)
                  (member (derivation-file-name xglibc) refs)))))
 
+(test-assertm "gexp->derivation, ungexp + ungexp-native (environment-variable)"
+  (mlet* %store-monad ((target -> "mips64el-linux-gnu")
+                       (exp    -> (gexp (list (ungexp-native
+                                               (environment-variable
+                                                "coreutils" coreutils))
+                                              (ungexp
+                                               (environment-variable
+                                                "glibc" glibc))
+                                              (ungexp output))))
+                       (xdrv      (gexp->derivation "foo" exp
+                                                    #:target target))
+                       (refs      (references*
+                                   (derivation-file-name xdrv)))
+                       (xglibc    (package->cross-derivation glibc target))
+                       (cu        (package->derivation coreutils)))
+    (return (and (member (derivation-file-name cu) refs)
+                 (member (derivation-file-name xglibc) refs)))))
+
 (test-assertm "gexp->derivation, ungexp-native + composed gexps"
   (mlet* %store-monad ((target -> "mips64el-linux-gnu")
                        (exp0   -> (gexp (list 1 2
@@ -958,6 +1075,18 @@
     (return (string=? (derivation-file-name drv)
                       (derivation-file-name xdrv)))))
 
+(test-assertm "gexp->derivation, ungexp-native + composed gexps 
(environment-variable)"
+  (mlet* %store-monad ((target -> "mips64el-linux-gnu")
+                       (exp0   -> (gexp (list 1 2
+                                              (ungexp (environment-variable
+                                                       "coreutils" 
coreutils)))))
+                       (exp    -> (gexp (list 0 (ungexp-native exp0))))
+                       (xdrv      (gexp->derivation "foo" exp
+                                                    #:target target))
+                       (drv       (gexp->derivation "foo" exp)))
+    (return (string=? (derivation-file-name drv)
+                      (derivation-file-name xdrv)))))
+
 (test-assertm "gexp->derivation, store copy"
   (let ((build-one #~(call-with-output-file #$output
                        (lambda (port)
@@ -1305,6 +1434,69 @@ importing.* \\(guix config\\) from the host"
                          (lowered-gexp-sources lexp))
                  (null? (lowered-gexp-inputs lexp))))))
 
+(test-assertm "lower-gexp, sexp (environment-variable)"
+  (mlet* %store-monad ((name -> "three")
+                       (sexp -> '(+ 1 2))
+                       (exp  -> #~#$(environment-variable name sexp))
+                       (lexp (lower-gexp
+                              exp
+                              #:effective-version
+                              (bootstrap-guile-effective-version))))
+    (return (and (equal? `((@ (guile) call-with-input-string)
+                           ((@ (guile) getenv) ,name)
+                           (@ (guile) read))
+                         (lowered-gexp-sexp lexp))
+                 (equal? `((,name . ,((@@ (guix gexp) sexp->string) sexp)))
+                         (lowered-gexp-environment lexp))))))
+
+(test-assertm "lower-gexp, composed gexps (environment-variable)"
+  (mlet* %store-monad ((name -> "three")
+                       (sexp -> '(+ 1 2))
+                       (exp  -> #~#$(environment-variable name #~#$sexp))
+                       (lexp (lower-gexp
+                              exp
+                              #:effective-version
+                              (bootstrap-guile-effective-version))))
+    (return (and (equal? `((@ (guile) call-with-input-string)
+                           ((@ (guile) getenv) ,name)
+                           (@ (guile) read))
+                         (lowered-gexp-sexp lexp))
+                 (equal? `((,name . ,((@@ (guix gexp) sexp->string) sexp)))
+                         (lowered-gexp-environment lexp))))))
+
+(test-assertm "lower-gexp, composed gexp with package output 
(environment-variable)"
+  (mlet* %store-monad ((pkg -> coreutils)
+                       (out -> "debug")
+                       (name -> (string-append (package-name pkg) ":" out))
+                       (pgexp -> (gexp (ungexp pkg out)))
+                       (drv (package->derivation pkg))
+                       (drv-debug-out -> (derivation->output-path drv out))
+                       (exp  -> #~#$(environment-variable name pgexp))
+                       (lexp (lower-gexp
+                              exp
+                              #:effective-version
+                              (bootstrap-guile-effective-version))))
+    (return (and (equal? `((@ (guile) call-with-input-string)
+                           ((@ (guile) getenv) ,name)
+                           (@ (guile) read))
+                         (lowered-gexp-sexp lexp))
+                 (equal? `((,name . ,(string-append "\"" drv-debug-out "\"")))
+                         (lowered-gexp-environment lexp))))))
+
+(test-equal "lower-gexp, duplicated (environment-variable)"
+  "duplicated"
+  (guard (c ((gexp-duplicate-environment-variable-error? c)
+             (environment-variable-name
+              (gexp-duplicate-environment-variable-error-variable c))))
+    (let* ((name "duplicated")
+           (sexp '(+ 1 2))
+           (exp  #~(#$(environment-variable name sexp)
+                    #$(environment-variable name coreutils))))
+      (lower-gexp
+       exp
+       #:effective-version
+       (bootstrap-guile-effective-version)))))
+
 (test-eq "lower-gexp, non-self-quoting input"
   +
   (guard (c ((gexp-input-error? c)
@@ -1855,6 +2047,25 @@ importing.* \\(guix config\\) from the host"
     (return (and (member (derivation-file-name good) refs)
                  (not (member (derivation-file-name bad) refs))))))
 
+(test-assertm "lower-object, with-transformation, list (environment-variable)"
+  (mlet* %store-monad ((file -> (computed-file
+                                 "coreutils-transformed"
+                                 #~(symlink
+                                    (car '#$(list #~#$(environment-variable
+                                                       "COREUTILS" coreutils)))
+                                    #$output)
+                                 #:guile %bootstrap-guile))
+                       (drv (with-transformation (lambda (obj)
+                                                   (if (eq? obj coreutils)
+                                                       grep
+                                                       obj))
+                              (lower-object file)))
+                       (bad (lower-object coreutils))
+                       (good (lower-object grep))
+                       (refs (references* (derivation-file-name drv))))
+    (return (and (member (derivation-file-name good) refs)
+                 (not (member (derivation-file-name bad) refs))))))
+
 (test-assertm "references-file"
   (let* ((exp      #~(symlink #$%bootstrap-guile #$output))
          (computed (computed-file "computed" exp

Reply via email to