* guix/gexp.scm (<gexp>)[modules]: New field.
(gexp-modules): New procedure.
(gexp->derivation): Use it and append the result to %MODULES.
Update docstring to mark #:modules as deprecated.
(current-imported-modules, with-imported-modules): New macros.
(gexp): Pass CURRENT-IMPORTED-MODULES as second argument to 'gexp'.
(gexp->script): Use and honor 'gexp-modules'; define '%modules'.
* tests/gexp.scm ("gexp->derivation & with-imported-modules")
("gexp->derivation & nested with-imported-modules")
("gexp-modules & ungexp", "gexp-modules & ungexp-splicing"):
New tests.
("program-file"): Use 'with-imported-modules'. Remove #:modules
argument to 'program-file'.
* doc/guix.texi (G-Expressions): Document 'with-imported-modules'.
Mark #:modules of 'gexp->derivation' as deprecated.
* emacs/guix-devel.el: Add syntax for 'with-imported-modules'.
(guix-devel-keywords): Add it.
* .dir-locals.el: Likewise.
(eval . (put 'run-with-store 'scheme-indent-function 1))
(eval . (put 'run-with-state 'scheme-indent-function 1))
(eval . (put 'wrap-program 'scheme-indent-function 1))
+ (eval . (put 'with-imported-modules 'scheme-indent-function 1))
(eval . (put 'call-with-container 'scheme-indent-function 1))
(eval . (put 'container-excursion 'scheme-indent-function 1))
that @command{ln} can actually run on the host; but then the
cross-compiled build of @var{emacs} is referenced.
+@cindex imported modules, for gexps
+@findex with-imported-modules
+Another gexp feature is @dfn{imported modules}: sometimes you want to be
+able to use certain Guile modules from the ``host environment'' in the
+gexp, so those modules should be imported in the ``build environment''.
+The @code{with-imported-modules} form allows you to express that:
+
+@example
+(let ((build (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (guix build utils))
+ (mkdir-p (string-append #$output "/bin"))))))
+ (gexp->derivation "empty-dir"
+ #~(begin
+ #$build
+ (display "success!\n")
+ #t)))
+@end example
+
+@noindent
+In this example, the @code{(guix build utils)} module is automatically
+pulled into the isolated build environment of our gexp, such that
+@code{(use-modules (guix build utils))} works as expected.
+
The syntactic form to construct gexps is summarized below.
@deffn {Scheme Syntax} #~@var{exp}
of the @code{gexp?} type (see below.)
@end deffn
+@deffn {Scheme Syntax} with-imported-modules @var{modules} @var{body}@dots{}
+Mark the gexps defined in @var{body}@dots{} as requiring @var{modules}
+in their execution environment. @var{modules} must be a list of Guile
+module names, such as @code{'((guix build utils) (guix build gremlin))}.
+
+This form has @emph{lexical} scope: it has an effect on the gexps
+directly defined in @var{body}@dots{}, but not on those defined, say, in
+procedures called from @var{body}@dots{}.
+@end deffn
+
@deffn {Scheme Procedure} gexp? @var{obj}
Return @code{#t} if @var{obj} is a G-expression.
@end deffn
it is used as the cross-compilation target triplet for packages referred
to by @var{exp}.
-Make @var{modules} available in the evaluation context of @var{exp};
+@var{modules} is deprecated in favor of @code{with-imported-modules}.
+Its meaning is to
+make @var{modules} available in the evaluation context of @var{exp};
@var{modules} is a list of names of Guile modules searched in
@var{module-path} to be copied in the store, compiled, and made available in
the load path during the execution of @var{exp}---e.g., @code{((guix
"with-derivation-substitute"
"with-directory-excursion"
"with-error-handling"
+ "with-imported-modules"
"with-monad"
"with-mutex"
"with-store"))
(with-derivation-substitute 2)
(with-directory-excursion 1)
(with-error-handling 0)
+ (with-imported-modules 1)
(with-monad 1)
(with-mutex 1)
(with-store 1)
#:use-module (ice-9 match)
#:export (gexp
gexp?
+ with-imported-modules
gexp-input
gexp-input?
;; "G expressions".
(define-record-type <gexp>
- (make-gexp references proc)
+ (make-gexp references modules proc)
gexp?
(references gexp-references) ;list of <gexp-input>
+ (modules gexp-self-modules) ;list of module names
(proc gexp-proc)) ;procedure
(define (write-gexp gexp port)
(set-record-type-printer! <gexp-output> write-gexp-output)
+(define (gexp-modules gexp)
+ "Return the list of Guile module names GEXP relies on."
+ (delete-duplicates
+ (append (gexp-self-modules gexp)
+ (append-map (match-lambda
+ (($ <gexp-input> (? gexp? exp))
+ (gexp-modules exp))
+ (($ <gexp-input> (lst ...))
+ (append-map (lambda (item)
+ (if (gexp? item)
+ (gexp-modules item)
+ '()))
+ lst))
+ (_
+ '()))
+ (gexp-references gexp)))))
+
(define raw-derivation
(store-lift derivation))
TARGET is true, it is used as the cross-compilation target triplet for
packages referred to by EXP.
-Make MODULES available in the evaluation context of EXP; MODULES is a list of
+MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
+make MODULES available in the evaluation context of EXP; MODULES is a list of
names of Guile modules searched in MODULE-PATH to be copied in the store,
compiled, and made available in the load path during the execution of
EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
referenced by the outputs.
The other arguments are as for 'derivation'."
- (define %modules modules)
+ (define %modules
+ (delete-duplicates
+ (append modules (gexp-modules exp))))
(define outputs (gexp-outputs exp))
(define (graphs-file-names graphs)
(simple-format #f "~a:~a" line column)))
"<unknown location>")))
+(define-syntax-parameter current-imported-modules
+ ;; Current list of imported modules.
+ (identifier-syntax '()))
+
+(define-syntax-rule (with-imported-modules modules body ...)
+ "Mark the gexps defined in BODY... as requiring MODULES in their execution
+environment."
+ (syntax-parameterize ((current-imported-modules
+ (identifier-syntax modules)))
+ body ...))
+
(define-syntax gexp
(lambda (s)
(define (collect-escapes exp)
(sexp (substitute-references #'exp (zip escapes formals)))
(refs (map escape->ref escapes)))
#`(make-gexp (list #,@refs)
+ current-imported-modules
(lambda #,formals
#,sexp)))))))
#:key (modules '()) (guile (default-guile)))
"Return an executable script NAME that runs EXP using GUILE with MODULES in
its search path."
- (mlet %store-monad ((modules (imported-modules modules))
- (compiled (compiled-modules modules)))
+ (define %modules
+ (append (gexp-modules exp) modules))
+
+ (mlet %store-monad ((modules (imported-modules %modules))
+ (compiled (compiled-modules %modules)))
(gexp->derivation name
(gexp
(call-with-output-file (ungexp output)
get-bytevector-all))))
files))))))
+(test-equal "gexp-modules & ungexp"
+ '((bar) (foo))
+ ((@@ (guix gexp) gexp-modules)
+ #~(foo #$(with-imported-modules '((foo)) #~+)
+ #+(with-imported-modules '((bar)) #~-))))
+
+(test-equal "gexp-modules & ungexp-splicing"
+ '((foo) (bar))
+ ((@@ (guix gexp) gexp-modules)
+ #~(foo #$@(list (with-imported-modules '((foo)) #~+)
+ (with-imported-modules '((bar)) #~-)))))
+
(test-assertm "gexp->derivation #:modules"
(mlet* %store-monad
((build -> #~(begin
(s (stat (string-append p "/guile/guix/nix"))))
(return (eq? (stat:type s) 'directory))))))
+(test-assertm "gexp->derivation & with-imported-modules"
+ ;; Same test as above, but using 'with-imported-modules'.
+ (mlet* %store-monad
+ ((build -> (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (guix build utils))
+ (mkdir-p (string-append #$output "/guile/guix/nix"))
+ #t)))
+ (drv (gexp->derivation "test-with-modules" build)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (let* ((p (derivation->output-path drv))
+ (s (stat (string-append p "/guile/guix/nix"))))
+ (return (eq? (stat:type s) 'directory))))))
+
+(test-assertm "gexp->derivation & nested with-imported-modules"
+ (mlet* %store-monad
+ ((build1 -> (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (guix build utils))
+ (mkdir-p (string-append #$output "/guile/guix/nix"))
+ #t)))
+ (build2 -> (with-imported-modules '((guix build bournish))
+ #~(begin
+ (use-modules (guix build bournish)
+ (system base compile))
+ #+build1
+ (call-with-output-file (string-append #$output "/b")
+ (lambda (port)
+ (write
+ (read-and-compile (open-input-string "cd /foo")
+ #:from %bournish-language
+ #:to 'scheme)
+ port))))))
+ (drv (gexp->derivation "test-with-modules" build2)))
+ (mbegin %store-monad
+ (built-derivations (list drv))
+ (let* ((p (derivation->output-path drv))
+ (s (stat (string-append p "/guile/guix/nix")))
+ (b (string-append p "/b")))
+ (return (and (eq? (stat:type s) 'directory)
+ (equal? '(chdir "/foo")
+ (call-with-input-file b read))))))))
+
(test-assertm "gexp->derivation #:references-graphs"
(mlet* %store-monad
((one (text-file "one" (random-text)))
(test-assertm "program-file"
(let* ((n (random (expt 2 50)))
- (exp (gexp (begin
- (use-modules (guix build utils))
- (display (ungexp n)))))
+ (exp (with-imported-modules '((guix build utils))
+ (gexp (begin
+ (use-modules (guix build utils))
+ (display (ungexp n))))))
(file (program-file "program" exp
- #:modules '((guix build utils))
#:guile %bootstrap-guile)))
(mlet* %store-monad ((drv (lower-object file))
(out -> (derivation->output-path drv)))