1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix gexp)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix derivations)
25 #:use-module (guix grafts)
26 #:use-module (guix utils)
27 #:use-module (rnrs bytevectors)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 match)
49 local-file-absolute-file-name
69 program-file-module-path
89 lowered-gexp-load-path
90 lowered-gexp-load-compiled-path
114 gexp-error-invalid-input))
118 ;;; This module implements "G-expressions", or "gexps". Gexps are like
119 ;;; S-expressions (sexps), with two differences:
121 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
122 ;;; replaced by the corresponding output file name; in addition, the
123 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
124 ;;; the native code of a given package, in case of cross-compilation;
126 ;;; 2. Gexps embed information about the derivations they refer to.
128 ;;; Gexps make it easy to write to files Scheme code that refers to store
129 ;;; items, or to write Scheme code to build derivations.
134 (define-record-type <gexp>
135 (make-gexp references modules extensions proc)
137 (references gexp-references) ;list of <gexp-input>
138 (modules gexp-self-modules) ;list of module names
139 (extensions gexp-self-extensions) ;list of lowerable things
140 (proc gexp-proc)) ;procedure
142 (define (write-gexp gexp port)
143 "Write GEXP on PORT."
144 (display "#<gexp " port)
146 ;; Try to write the underlying sexp. Now, this trick doesn't work when
147 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
148 ;; tries to use 'append' on that, which fails with wrong-type-arg.
150 (write (apply (gexp-proc gexp)
151 (gexp-references gexp))
154 (number->string (object-address gexp) 16)))
156 (set-record-type-printer! <gexp> write-gexp)
163 ;; Compiler for a type of objects that may be introduced in a gexp.
164 (define-record-type <gexp-compiler>
165 (gexp-compiler type lower expand)
167 (type gexp-compiler-type) ;record type descriptor
168 (lower gexp-compiler-lower)
169 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
171 (define-condition-type &gexp-error &error
174 (define-condition-type &gexp-input-error &gexp-error
176 (input gexp-error-invalid-input))
179 (define %gexp-compilers
180 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
181 (make-hash-table 20))
183 (define (default-expander thing obj output)
184 "This is the default expander for \"things\" that appear in gexps. It
185 returns its output file name of OBJ's OUTPUT."
188 (derivation->output-path drv output))
192 (define (register-compiler! compiler)
193 "Register COMPILER as a gexp compiler."
194 (hashq-set! %gexp-compilers
195 (gexp-compiler-type compiler) compiler))
197 (define (lookup-compiler object)
198 "Search for a compiler for OBJECT. Upon success, return the three argument
199 procedure to lower it; otherwise return #f."
200 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
201 gexp-compiler-lower))
203 (define (file-like? object)
204 "Return #t if OBJECT leads to a file in the store once unquoted in a
205 G-expression; otherwise return #f."
206 (and (struct? object) (->bool (lookup-compiler object))))
208 (define (lookup-expander object)
209 "Search for an expander for OBJECT. Upon success, return the three argument
210 procedure to expand it; otherwise return #f."
211 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
212 gexp-compiler-expand))
214 (define* (lower-object obj
215 #:optional (system (%current-system))
217 "Return as a value in %STORE-MONAD the derivation or store item
218 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
219 OBJ must be an object that has an associated gexp compiler, such as a
221 (match (lookup-compiler obj)
223 (raise (condition (&gexp-input-error (input obj)))))
225 ;; Cache in STORE the result of lowering OBJ.
226 (mlet %store-monad ((graft? (grafting?)))
227 (mcached (let ((lower (lookup-compiler obj)))
228 (lower obj system target))
230 system target graft?)))))
232 (define-syntax define-gexp-compiler
233 (syntax-rules (=> compiler expander)
234 "Define NAME as a compiler for objects matching PREDICATE encountered in
237 In the simplest form of the macro, BODY must return a derivation for PARAM, an
238 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
239 #f except when cross-compiling.)
241 The more elaborate form allows you to specify an expander:
243 (define-gexp-compiler something something?
244 compiler => (lambda (param system target) ...)
245 expander => (lambda (param drv output) ...))
247 The expander specifies how an object is converted to its sexp representation."
248 ((_ (name (param record-type) system target) body ...)
249 (define-gexp-compiler name record-type
250 compiler => (lambda (param system target) body ...)
251 expander => default-expander))
257 (gexp-compiler record-type compile expand))
258 (register-compiler! name)))))
260 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
261 ;; Derivations are the lowest-level representation, so this is the identity
263 (with-monad %store-monad
268 ;;; File declarations.
271 ;; A local file name. FILE is the file name the user entered, which can be a
272 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
273 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
274 ;; repeated 'stat' calls.
275 (define-record-type <local-file>
276 (%%local-file file absolute name recursive? select?)
278 (file local-file-file) ;string
279 (absolute %local-file-absolute-file-name) ;promise string
280 (name local-file-name) ;string
281 (recursive? local-file-recursive?) ;Boolean
282 (select? local-file-select?)) ;string stat -> Boolean
284 (define (true file stat) #t)
286 (define* (%local-file file promise #:optional (name (basename file))
287 #:key recursive? (select? true))
288 ;; This intermediate procedure is part of our ABI, but the underlying
289 ;; %%LOCAL-FILE is not.
290 (%%local-file file promise name recursive? select?))
292 (define (absolute-file-name file directory)
293 "Return the canonical absolute file name for FILE, which lives in the
294 vicinity of DIRECTORY."
296 (cond ((string-prefix? "/" file) file)
297 ((not directory) file)
298 ((string-prefix? "/" directory)
299 (string-append directory "/" file))
302 (define-syntax local-file
304 "Return an object representing local file FILE to add to the store; this
305 object can be used in a gexp. If FILE is a relative file name, it is looked
306 up relative to the source file where this form appears. FILE will be added to
307 the store under NAME--by default the base name of FILE.
309 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
310 designates a flat file and RECURSIVE? is true, its contents are added, and its
311 permission bits are kept.
313 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
314 where FILE is the entry's absolute file name and STAT is the result of
315 'lstat'; exclude entries for which SELECT? does not return true.
317 This is the declarative counterpart of the 'interned-file' monadic procedure.
318 It is implemented as a macro to capture the current source directory where it
323 (delay (absolute-file-name file (current-source-directory)))
326 #'(syntax-error "missing file name"))
329 ;; XXX: We could return #'(lambda (file . rest) ...). However,
330 ;; (syntax-source #'id) is #f so (current-source-directory) would not
331 ;; work. Thus, simply forbid this form.
333 "'local-file' is a macro and cannot be used like this")))))
335 (define (local-file-absolute-file-name file)
336 "Return the absolute file name for FILE, a <local-file> instance. A
337 'system-error' exception is raised if FILE could not be found."
338 (force (%local-file-absolute-file-name file)))
340 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
341 ;; "Compile" FILE by adding it to the store.
343 (($ <local-file> file (= force absolute) name recursive? select?)
344 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
345 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
346 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
347 ;; just throw an error, both of which are inconvenient.
348 (interned-file absolute name
349 #:recursive? recursive? #:select? select?))))
351 (define-record-type <plain-file>
352 (%plain-file name content references)
354 (name plain-file-name) ;string
355 (content plain-file-content) ;string or bytevector
356 (references plain-file-references)) ;list (currently unused)
358 (define (plain-file name content)
359 "Return an object representing a text file called NAME with the given
360 CONTENT (a string) to be added to the store.
362 This is the declarative counterpart of 'text-file'."
363 ;; XXX: For now just ignore 'references' because it's not clear how to use
364 ;; them in a declarative context.
365 (%plain-file name content '()))
367 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
368 ;; "Compile" FILE by adding it to the store.
370 (($ <plain-file> name (and (? string?) content) references)
371 (text-file name content references))
372 (($ <plain-file> name (and (? bytevector?) content) references)
373 (binary-file name content references))))
375 (define-record-type <computed-file>
376 (%computed-file name gexp guile options)
378 (name computed-file-name) ;string
379 (gexp computed-file-gexp) ;gexp
380 (guile computed-file-guile) ;<package>
381 (options computed-file-options)) ;list of arguments
383 (define* (computed-file name gexp
384 #:key guile (options '(#:local-build? #t)))
385 "Return an object representing the store item NAME, a file or directory
386 computed by GEXP. OPTIONS is a list of additional arguments to pass
387 to 'gexp->derivation'.
389 This is the declarative counterpart of 'gexp->derivation'."
390 (%computed-file name gexp guile options))
392 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
394 ;; Compile FILE by returning a derivation whose build expression is its
397 (($ <computed-file> name gexp guile options)
399 (mlet %store-monad ((guile (lower-object guile system
401 (apply gexp->derivation name gexp #:guile-for-build guile
402 #:system system #:target target options))
403 (apply gexp->derivation name gexp
404 #:system system #:target target options)))))
406 (define-record-type <program-file>
407 (%program-file name gexp guile path)
409 (name program-file-name) ;string
410 (gexp program-file-gexp) ;gexp
411 (guile program-file-guile) ;package
412 (path program-file-module-path)) ;list of strings
414 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
415 "Return an object representing the executable store item NAME that runs
416 GEXP. GUILE is the Guile package used to execute that script. Imported
417 modules of GEXP are looked up in MODULE-PATH.
419 This is the declarative counterpart of 'gexp->script'."
420 (%program-file name gexp guile module-path))
422 (define-gexp-compiler (program-file-compiler (file <program-file>)
424 ;; Compile FILE by returning a derivation that builds the script.
426 (($ <program-file> name gexp guile module-path)
427 (gexp->script name gexp
428 #:module-path module-path
429 #:guile (or guile (default-guile))))))
431 (define-record-type <scheme-file>
432 (%scheme-file name gexp splice?)
434 (name scheme-file-name) ;string
435 (gexp scheme-file-gexp) ;gexp
436 (splice? scheme-file-splice?)) ;Boolean
438 (define* (scheme-file name gexp #:key splice?)
439 "Return an object representing the Scheme file NAME that contains GEXP.
441 This is the declarative counterpart of 'gexp->file'."
442 (%scheme-file name gexp splice?))
444 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
446 ;; Compile FILE by returning a derivation that builds the file.
448 (($ <scheme-file> name gexp splice?)
449 (gexp->file name gexp #:splice? splice?))))
451 ;; Appending SUFFIX to BASE's output file name.
452 (define-record-type <file-append>
453 (%file-append base suffix)
455 (base file-append-base) ;<package> | <derivation> | ...
456 (suffix file-append-suffix)) ;list of strings
458 (define (write-file-append file port)
460 (($ <file-append> base suffix)
461 (format port "#<file-append ~s ~s>" base
462 (string-join suffix)))))
464 (set-record-type-printer! <file-append> write-file-append)
466 (define (file-append base . suffix)
467 "Return a <file-append> object that expands to the concatenation of BASE and
469 (%file-append base suffix))
471 (define-gexp-compiler file-append-compiler <file-append>
472 compiler => (lambda (obj system target)
474 (($ <file-append> base _)
475 (lower-object base system #:target target))))
476 expander => (lambda (obj lowered output)
478 (($ <file-append> base suffix)
479 (let* ((expand (lookup-expander base))
480 (base (expand base lowered output)))
481 (string-append base (string-concatenate suffix)))))))
485 ;;; Inputs & outputs.
488 ;; The input of a gexp.
489 (define-record-type <gexp-input>
490 (%gexp-input thing output native?)
492 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
493 (output gexp-input-output) ;string
494 (native? gexp-input-native?)) ;Boolean
496 (define (write-gexp-input input port)
498 (($ <gexp-input> thing output #f)
499 (format port "#<gexp-input ~s:~a>" thing output))
500 (($ <gexp-input> thing output #t)
501 (format port "#<gexp-input native ~s:~a>" thing output))))
503 (set-record-type-printer! <gexp-input> write-gexp-input)
505 (define* (gexp-input thing ;convenience procedure
506 #:optional (output "out")
508 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
509 whether this should be considered a \"native\" input or not."
510 (%gexp-input thing output native?))
512 ;; Reference to one of the derivation's outputs, for gexps used in
514 (define-record-type <gexp-output>
517 (name gexp-output-name))
519 (define (write-gexp-output output port)
521 (($ <gexp-output> name)
522 (format port "#<gexp-output ~a>" name))))
524 (set-record-type-printer! <gexp-output> write-gexp-output)
526 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
527 "Recurse on GEXP and the expressions it refers to, summing the items
528 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
529 second argument to 'delete-duplicates'."
532 (append (self-attribute gexp)
533 (append-map (match-lambda
534 (($ <gexp-input> (? gexp? exp))
535 (gexp-attribute exp self-attribute))
536 (($ <gexp-input> (lst ...))
537 (append-map (lambda (item)
545 (gexp-references gexp)))
547 '())) ;plain Scheme data type
549 (define (gexp-modules gexp)
550 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
551 false, meaning that GEXP is a plain Scheme object, return the empty list."
552 (define (module=? m1 m2)
553 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
554 ;; right-hand side may not be comparable with 'equal?': it's typically a
555 ;; file-like object that embeds a gexp, which in turn embeds closure;
556 ;; those closures may be 'eq?' when running compiled code but are unlikely
557 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
558 ;; avoid this discrepancy.
562 (((name2 ...) '=> _) (equal? name1 name2))
567 (gexp-attribute gexp gexp-self-modules module=?))
569 (define (gexp-extensions gexp)
570 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
571 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
573 (gexp-attribute gexp gexp-self-extensions))
575 (define* (lower-inputs inputs
577 "Turn any package from INPUTS into a derivation for SYSTEM; return the
578 corresponding input list as a monadic value. When TARGET is true, use it as
579 the cross-compilation target triplet."
580 (define (store-item? obj)
581 (and (string? obj) (store-path? obj)))
583 (with-monad %store-monad
586 (((? struct? thing) sub-drv ...)
587 (mlet %store-monad ((drv (lower-object
588 thing system #:target target)))
589 (return (apply gexp-input drv sub-drv))))
590 (((? store-item? item))
591 (return (gexp-input item)))
593 (return (gexp-input input))))
596 (define* (lower-reference-graphs graphs #:key system target)
597 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
598 #:reference-graphs argument, lower it such that each INPUT is replaced by the
599 corresponding derivation."
601 (((file-names . inputs) ...)
602 (mlet %store-monad ((inputs (lower-inputs inputs
605 (return (map (lambda (file input)
606 (cons file (gexp-input->tuple input)))
607 file-names inputs))))))
609 (define* (lower-references lst #:key system target)
610 "Based on LST, a list of output names and packages, return a list of output
611 names and file names suitable for the #:allowed-references argument to
613 (with-monad %store-monad
618 (($ <gexp-input> thing output native?)
619 (mlet %store-monad ((drv (lower-object thing system
622 (return (derivation->output-path drv output))))
624 (mlet %store-monad ((drv (lower-object thing system
626 (return (derivation->output-path drv))))))
628 (mapm %store-monad lower lst)))
630 (define default-guile-derivation
631 ;; Here we break the abstraction by talking to the higher-level layer.
632 ;; Thus, do the resolution lazily to hide the circular dependency.
634 (let ((iface (resolve-interface '(guix packages))))
635 (module-ref iface 'default-guile-derivation)))))
637 ((force proc) system))))
639 ;; Representation of a gexp instantiated for a given target and system.
640 (define-record-type <lowered-gexp>
641 (lowered-gexp sexp inputs guile load-path load-compiled-path)
643 (sexp lowered-gexp-sexp) ;sexp
644 (inputs lowered-gexp-inputs) ;list of <gexp-input>
645 (guile lowered-gexp-guile) ;<derivation> | #f
646 (load-path lowered-gexp-load-path) ;list of store items
647 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
649 (define* (lower-gexp exp
651 (module-path %load-path)
652 (system (%current-system))
655 (guile-for-build (%guile-for-build))
656 (effective-version "2.2")
659 (pre-load-modules? #t)) ;transitional
660 "*Note: This API is subject to change; use at your own risk!*
662 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
663 <lowered-gexp> ready to be used.
665 Lowered gexps are an intermediate representation that's useful for
666 applications that deal with gexps outside in a way that is disconnected from
667 derivations--e.g., code evaluated for its side effects."
669 (delete-duplicates (gexp-modules exp)))
671 (define (search-path modules extensions suffix)
672 (append (match modules
674 (list (derivation->output-path drv)))
677 ((? store-path? item)
679 (map (lambda (extension)
680 (string-append (match extension
682 (derivation->output-path drv))
683 ((? store-path? item)
688 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
689 ;; '%current-target-system' to be looked up at >>=
691 (graft? (set-grafting graft?))
693 (system -> (or system (%current-system)))
694 (target -> (if (eq? target 'current)
695 (%current-target-system)
697 (guile (if guile-for-build
698 (return guile-for-build)
699 (default-guile-derivation system)))
700 (normals (lower-inputs (gexp-inputs exp)
703 (natives (lower-inputs (gexp-native-inputs exp)
706 (inputs -> (append normals natives))
707 (sexp (gexp->sexp exp
710 (extensions -> (gexp-extensions exp))
711 (exts (mapm %store-monad
713 (lower-object obj system))
715 (modules (if (pair? %modules)
716 (imported-modules %modules
718 #:module-path module-path)
720 (compiled (if (pair? %modules)
721 (compiled-modules %modules
723 #:module-path module-path
724 #:extensions extensions
728 #:deprecation-warnings
729 deprecation-warnings)
732 (search-path modules exts
733 (string-append "/share/guile/site/" effective-version)))
735 (define load-compiled-path
736 (search-path compiled exts
737 (string-append "/lib/guile/" effective-version
741 (set-grafting graft?) ;restore the initial setting
742 (return (lowered-gexp sexp
744 (list (gexp-input modules))
747 (list (gexp-input compiled))
749 ,@(map gexp-input exts)
753 load-compiled-path)))))
755 (define (gexp-input->tuple input)
756 "Given INPUT, a <gexp-input> record, return the corresponding input tuple
757 suitable for the 'derivation' procedure."
758 (match (gexp-input-output input)
759 ("out" `(,(gexp-input-thing input)))
760 (output `(,(gexp-input-thing input)
761 ,(gexp-input-output input)))))
763 (define* (gexp->derivation name exp
765 system (target 'current)
766 hash hash-algo recursive?
769 (module-path %load-path)
770 (guile-for-build (%guile-for-build))
771 (effective-version "2.2")
774 allowed-references disallowed-references
776 local-build? (substitutable? #t)
779 ;; TODO: This parameter is transitional; it's here
780 ;; to avoid a full rebuild. Remove it on the next
782 (pre-load-modules? #t)
785 (script-name (string-append name "-builder")))
786 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
787 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
788 TARGET is true, it is used as the cross-compilation target triplet for
789 packages referred to by EXP.
791 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
792 make MODULES available in the evaluation context of EXP; MODULES is a list of
793 names of Guile modules searched in MODULE-PATH to be copied in the store,
794 compiled, and made available in the load path during the execution of
795 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
797 EFFECTIVE-VERSION determines the string to use when adding extensions of
798 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
800 GRAFT? determines whether packages referred to by EXP should be grafted when
803 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
807 (FILE-NAME PACKAGE OUTPUT)
808 (FILE-NAME DERIVATION)
809 (FILE-NAME DERIVATION OUTPUT)
810 (FILE-NAME STORE-ITEM)
812 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
813 an input of the build process of EXP. In the build environment, each
814 FILE-NAME contains the reference graph of the corresponding item, in a simple
817 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
818 In the latter case, the list denotes store items that the result is allowed to
819 refer to. Any reference to another store item will lead to a build error.
820 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
821 referenced by the outputs.
823 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
824 compiling modules. It can be #f, #t, or 'detailed.
826 The other arguments are as for 'derivation'."
827 (define outputs (gexp-outputs exp))
828 (define requested-graft? graft?)
830 (define (graphs-file-names graphs)
831 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
833 ;; TODO: Remove 'derivation?' special cases.
834 ((file-name (? derivation? drv))
835 (cons file-name (derivation->output-path drv)))
836 ((file-name (? derivation? drv) sub-drv)
837 (cons file-name (derivation->output-path drv sub-drv)))
839 (cons file-name thing)))
842 (define (add-modules exp modules)
845 (make-gexp (gexp-references exp)
846 (append modules (gexp-self-modules exp))
847 (gexp-self-extensions exp)
850 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
851 ;; '%current-target-system' to be looked up at >>=
853 (graft? (set-grafting graft?))
855 (system -> (or system (%current-system)))
856 (target -> (if (eq? target 'current)
857 (%current-target-system)
859 (exp -> (add-modules exp modules))
860 (lowered (lower-gexp exp
861 #:module-path module-path
864 #:graft? requested-graft?
869 #:deprecation-warnings
874 (graphs (if references-graphs
875 (lower-reference-graphs references-graphs
879 (allowed (if allowed-references
880 (lower-references allowed-references
884 (disallowed (if disallowed-references
885 (lower-references disallowed-references
889 (guile -> (lowered-gexp-guile lowered))
890 (builder (text-file script-name
892 (lowered-gexp-sexp lowered)))))
894 (set-grafting graft?) ;restore the initial setting
896 (string-append (derivation->output-path guile)
898 `("--no-auto-compile"
899 ,@(append-map (lambda (directory)
901 (lowered-gexp-load-path lowered))
902 ,@(append-map (lambda (directory)
904 (lowered-gexp-load-compiled-path lowered))
911 ,@(map gexp-input->tuple
912 (lowered-gexp-inputs lowered))
914 (((_ . inputs) ...) inputs)
916 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
917 #:references-graphs (and=> graphs graphs-file-names)
918 #:allowed-references allowed
919 #:disallowed-references disallowed
920 #:leaked-env-vars leaked-env-vars
921 #:local-build? local-build?
922 #:substitutable? substitutable?
923 #:properties properties))))
925 (define* (gexp-inputs exp #:key native?)
926 "Return the input list for EXP. When NATIVE? is true, return only native
927 references; otherwise, return only non-native references."
928 ;; TODO: Return <gexp-input> records instead of tuples.
929 (define (add-reference-inputs ref result)
931 (($ <gexp-input> (? gexp? exp) _ #t)
933 (append (gexp-inputs exp)
934 (gexp-inputs exp #:native? #t)
937 (($ <gexp-input> (? gexp? exp) _ #f)
938 (append (gexp-inputs exp #:native? native?)
940 (($ <gexp-input> (? string? str))
941 (if (direct-store-path? str)
942 (cons `(,str) result)
944 (($ <gexp-input> (? struct? thing) output n?)
945 (if (and (eqv? n? native?) (lookup-compiler thing))
946 ;; THING is a derivation, or a package, or an origin, etc.
947 (cons `(,thing ,output) result)
949 (($ <gexp-input> (lst ...) output n?)
950 (fold-right add-reference-inputs result
951 ;; XXX: For now, automatically convert LST to a list of
952 ;; gexp-inputs. Inherit N?.
955 (%gexp-input (gexp-input-thing x)
956 (gexp-input-output x)
959 (%gexp-input x "out" n?)))
962 ;; Ignore references to other kinds of objects.
965 (fold-right add-reference-inputs
967 (gexp-references exp)))
969 (define gexp-native-inputs
970 (cut gexp-inputs <> #:native? #t))
972 (define (gexp-outputs exp)
973 "Return the outputs referred to by EXP as a list of strings."
974 (define (add-reference-output ref result)
976 (($ <gexp-output> name)
978 (($ <gexp-input> (? gexp? exp))
979 (append (gexp-outputs exp) result))
980 (($ <gexp-input> (lst ...) output native?)
981 ;; XXX: Automatically convert LST.
982 (add-reference-output (map (match-lambda
983 ((? gexp-input? x) x)
984 (x (%gexp-input x "out" native?)))
988 (fold-right add-reference-output result lst))
993 (add-reference-output (gexp-references exp) '())))
995 (define* (gexp->sexp exp #:key
996 (system (%current-system))
997 (target (%current-target-system)))
998 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
999 and in the current monad setting (system type, etc.)"
1000 (define* (reference->sexp ref #:optional native?)
1001 (with-monad %store-monad
1003 (($ <gexp-output> output)
1004 ;; Output file names are not known in advance but the daemon defines
1005 ;; an environment variable for each of them at build time, so use
1007 (return `((@ (guile) getenv) ,output)))
1008 (($ <gexp-input> (? gexp? exp) output n?)
1011 #:target (if (or n? native?) #f target)))
1012 (($ <gexp-input> (refs ...) output n?)
1015 ;; XXX: Automatically convert REF to an gexp-input.
1017 (if (gexp-input? ref)
1019 (%gexp-input ref "out" n?))
1022 (($ <gexp-input> (? struct? thing) output n?)
1023 (let ((target (if (or n? native?) #f target))
1024 (expand (lookup-expander thing)))
1025 (mlet %store-monad ((obj (lower-object thing system
1027 ;; OBJ must be either a derivation or a store file name.
1028 (return (expand thing obj output)))))
1035 ((args (mapm %store-monad
1036 reference->sexp (gexp-references exp))))
1037 (return (apply (gexp-proc exp) args))))
1039 (define (syntax-location-string s)
1040 "Return a string representing the source code location of S."
1041 (let ((props (syntax-source s)))
1043 (let ((file (assoc-ref props 'filename))
1044 (line (and=> (assoc-ref props 'line) 1+))
1045 (column (assoc-ref props 'column)))
1047 (simple-format #f "~a:~a:~a"
1049 (simple-format #f "~a:~a" line column)))
1050 "<unknown location>")))
1052 (define-syntax-rule (define-syntax-parameter-once name proc)
1053 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1054 ;; does not get redefined. This works around a race condition in a
1055 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1056 (eval-when (load eval expand compile)
1058 (if (module-locally-bound? (current-module) 'name)
1059 (module-ref (current-module) 'name)
1060 (make-syntax-transformer 'name 'syntax-parameter
1063 (define-syntax-parameter-once current-imported-modules
1064 ;; Current list of imported modules.
1065 (identifier-syntax '()))
1067 (define-syntax-rule (with-imported-modules modules body ...)
1068 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1070 (syntax-parameterize ((current-imported-modules
1071 (identifier-syntax modules)))
1074 (define-syntax-parameter-once current-imported-extensions
1075 ;; Current list of extensions.
1076 (identifier-syntax '()))
1078 (define-syntax-rule (with-extensions extensions body ...)
1079 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1080 execution environment."
1081 (syntax-parameterize ((current-imported-extensions
1082 (identifier-syntax extensions)))
1087 (define (collect-escapes exp)
1088 ;; Return all the 'ungexp' present in EXP.
1089 (let loop ((exp exp)
1091 (syntax-case exp (ungexp
1094 ungexp-native-splicing)
1099 ((ungexp-splicing _ ...)
1101 ((ungexp-native _ ...)
1103 ((ungexp-native-splicing _ ...)
1106 (let ((result (loop #'exp0 result)))
1107 (loop #'exp result)))
1111 (define (escape->ref exp)
1112 ;; Turn 'ungexp' form EXP into a "reference".
1113 (syntax-case exp (ungexp ungexp-splicing
1114 ungexp-native ungexp-native-splicing
1117 #'(gexp-output "out"))
1118 ((ungexp output name)
1119 #'(gexp-output name))
1121 #'(%gexp-input thing "out" #f))
1122 ((ungexp drv-or-pkg out)
1123 #'(%gexp-input drv-or-pkg out #f))
1124 ((ungexp-splicing lst)
1125 #'(%gexp-input lst "out" #f))
1126 ((ungexp-native thing)
1127 #'(%gexp-input thing "out" #t))
1128 ((ungexp-native drv-or-pkg out)
1129 #'(%gexp-input drv-or-pkg out #t))
1130 ((ungexp-native-splicing lst)
1131 #'(%gexp-input lst "out" #t))))
1133 (define (substitute-ungexp exp substs)
1134 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1135 ;; the corresponding form in SUBSTS.
1136 (match (assoc exp substs)
1140 (with-syntax ((exp exp))
1141 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1143 (define (substitute-ungexp-splicing exp substs)
1146 (match (assoc #'exp substs)
1148 (with-syntax ((id id))
1150 #,(substitute-references #'(rest ...) substs))))
1152 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1155 (define (substitute-references exp substs)
1156 ;; Return a variant of EXP where all the cars of SUBSTS have been
1157 ;; replaced by the corresponding cdr.
1158 (syntax-case exp (ungexp ungexp-native
1159 ungexp-splicing ungexp-native-splicing)
1161 (substitute-ungexp exp substs))
1162 ((ungexp-native _ ...)
1163 (substitute-ungexp exp substs))
1164 (((ungexp-splicing _ ...) rest ...)
1165 (substitute-ungexp-splicing exp substs))
1166 (((ungexp-native-splicing _ ...) rest ...)
1167 (substitute-ungexp-splicing exp substs))
1169 #`(cons #,(substitute-references #'exp0 substs)
1170 #,(substitute-references #'exp substs)))
1173 (syntax-case s (ungexp output)
1175 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1176 (formals (generate-temporaries escapes))
1177 (sexp (substitute-references #'exp (zip escapes formals)))
1178 (refs (map escape->ref escapes)))
1179 #`(make-gexp (list #,@refs)
1180 current-imported-modules
1181 current-imported-extensions
1187 ;;; Module handling.
1191 (char-set-complement (char-set #\/)))
1193 (define (file-mapping->tree mapping)
1194 "Convert MAPPING, an alist like:
1196 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1198 to a tree suitable for 'interned-file-tree'."
1199 (let ((mapping (map (match-lambda
1200 ((destination . source)
1201 (cons (string-tokenize destination
1205 (fold (lambda (pair result)
1207 ((destination . source)
1208 (let loop ((destination destination)
1212 (let* ((mode (stat:mode (stat source)))
1213 (type (if (zero? (logand mode #o100))
1217 `(,type (file ,source))
1220 (let ((directory (assoc-ref result file)))
1225 (('directory . entries) entries)
1228 (alist-delete file result)
1233 (define %utils-module
1234 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1235 ;; other primitives below. Note: We give the file name relative to this
1236 ;; file you are currently reading; 'search-path' could return a file name
1237 ;; relative to the current working directory.
1238 (local-file "build/utils.scm"
1241 (define* (imported-files/derivation files
1242 #:key (name "file-import")
1244 (system (%current-system))
1245 (guile (%guile-for-build)))
1246 "Return a derivation that imports FILES into STORE. FILES must be a list
1247 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1248 resulting store path. FILE can be either a file name, or a file-like object,
1249 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1250 to the source files instead of copying them."
1253 ((final-path . (? string? file-name))
1254 (mlet %store-monad ((file (interned-file file-name
1255 (basename final-path))))
1256 (return (list final-path file))))
1257 ((final-path . file-like)
1258 (mlet %store-monad ((file (lower-object file-like system)))
1259 (return (list final-path file))))))
1261 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1265 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1266 (use-modules (ice-9 match))
1268 (mkdir (ungexp output)) (chdir (ungexp output))
1269 (for-each (match-lambda
1270 ((final-path store-path)
1271 (mkdir-p (dirname final-path))
1272 ((ungexp (if symlink? 'symlink 'copy-file))
1273 store-path final-path)))
1276 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1277 ;; exactly the same regardless of FILES: less disk space, and fewer
1278 ;; 'add-to-store' RPCs.
1279 (gexp->derivation name build
1281 #:guile-for-build guile
1284 ;; Avoid deprecation warnings about the use of the _IO*
1285 ;; constants in (guix build utils).
1287 '(("GUILE_WARN_DEPRECATED" . "no")))))
1289 (define* (imported-files files
1290 #:key (name "file-import")
1291 ;; The following parameters make sense when creating
1292 ;; an actual derivation.
1293 (system (%current-system))
1294 (guile (%guile-for-build)))
1295 "Import FILES into the store and return the resulting derivation or store
1296 file name (a derivation is created if and only if some elements of FILES are
1297 file-like objects and not local file names.) FILES must be a list
1298 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1299 resulting store path. FILE can be either a file name, or a file-like object,
1300 as returned by 'local-file' for example."
1301 (if (any (match-lambda
1302 ((_ . (? struct? source)) #t)
1305 (imported-files/derivation files #:name name
1306 #:symlink? derivation?
1307 #:system system #:guile guile)
1308 (interned-file-tree `(,name directory
1309 ,@(file-mapping->tree files)))))
1311 (define* (imported-modules modules
1312 #:key (name "module-import")
1313 (system (%current-system))
1314 (guile (%guile-for-build))
1315 (module-path %load-path))
1316 "Return a derivation that contains the source files of MODULES, a list of
1317 module names such as `(ice-9 q)'. All of MODULES must be either names of
1318 modules to be found in the MODULE-PATH search path, or a module name followed
1319 by an arrow followed by a file-like object. For example:
1321 (imported-modules `((guix build utils)
1323 ((guix config) => ,(scheme-file …))))
1325 In this example, the first two modules are taken from MODULE-PATH, and the
1326 last one is created from the given <scheme-file> object."
1327 (let ((files (map (match-lambda
1328 (((module ...) '=> file)
1329 (cons (module->source-file-name module)
1332 (let ((f (module->source-file-name module)))
1333 (cons f (search-path* module-path f)))))
1335 (imported-files files #:name name
1339 (define* (compiled-modules modules
1340 #:key (name "module-import-compiled")
1341 (system (%current-system))
1342 (guile (%guile-for-build))
1343 (module-path %load-path)
1345 (deprecation-warnings #f)
1347 ;; TODO: This flag is here to prevent a full
1348 ;; rebuild. Remove it on the next rebuild cycle.
1349 (pre-load-modules? #t))
1350 "Return a derivation that builds a tree containing the `.go' files
1351 corresponding to MODULES. All the MODULES are built in a context where
1352 they can refer to each other."
1353 (define total (length modules))
1355 (mlet %store-monad ((modules (imported-modules modules
1363 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1365 (use-modules (ice-9 ftw)
1369 (system base compile))
1371 (define (regular? file)
1372 (not (member file '("." ".."))))
1374 (define (process-entry entry output processed)
1375 (if (file-is-directory? entry)
1376 (let ((output (string-append output "/" (basename entry))))
1378 (process-directory entry output processed))
1379 (let* ((base (basename entry ".scm"))
1380 (output (string-append output "/" base ".go")))
1381 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1383 (ungexp-splicing (if pre-load-modules?
1384 (gexp ((ungexp total)))
1386 (ungexp (* total (if pre-load-modules? 2 1)))
1389 #:output-file output
1390 #:opts %auto-compilation-options)
1393 (define (process-directory directory output processed)
1394 (let ((entries (map (cut string-append directory "/" <>)
1395 (scandir directory regular?))))
1396 (fold (cut process-entry <> output <>)
1400 (setvbuf (current-output-port)
1401 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1404 ;; Capture 'mkdir-p'.
1405 (@ (guix build utils) mkdir-p))
1407 ;; Add EXTENSIONS to the search path.
1409 (append (map (lambda (extension)
1410 (string-append extension
1411 "/share/guile/site/"
1412 (effective-version)))
1413 '((ungexp-native-splicing extensions)))
1415 (set! %load-compiled-path
1416 (append (map (lambda (extension)
1417 (string-append extension "/lib/guile/"
1420 '((ungexp-native-splicing extensions)))
1421 %load-compiled-path))
1423 (set! %load-path (cons (ungexp modules) %load-path))
1425 ;; Above we loaded our own (guix build utils) but now we may need to
1426 ;; load a compile a different one. Thus, force a reload.
1427 (let ((utils (string-append (ungexp modules)
1428 "/guix/build/utils.scm")))
1429 (when (file-exists? utils)
1432 (mkdir (ungexp output))
1433 (chdir (ungexp modules))
1436 (if pre-load-modules?
1437 (gexp ((define* (load-from-directory directory
1438 #:optional (loaded 0))
1439 "Load all the source files found in DIRECTORY."
1440 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1441 (let ((entries (map (cut string-append directory "/" <>)
1442 (scandir directory regular?))))
1443 (fold (lambda (file loaded)
1444 (if (file-is-directory? file)
1445 (load-from-directory file loaded)
1447 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1449 (ungexp (* 2 total))
1451 (save-module-excursion
1453 (primitive-load file)))
1458 (load-from-directory ".")))
1461 (process-directory "." (ungexp output) 0))))
1463 ;; TODO: Pass MODULES as an environment variable.
1464 (gexp->derivation name build
1466 #:guile-for-build guile
1469 (case deprecation-warnings
1471 '(("GUILE_WARN_DEPRECATED" . "no")))
1473 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1479 ;;; Convenience procedures.
1482 (define (default-guile)
1483 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1484 ;; programs returned by 'program-file' and we don't want to keep references
1485 ;; to several Guile packages). This module must not refer to (gnu …)
1486 ;; modules directly, to avoid circular dependencies, hence this hack.
1487 (module-ref (resolve-interface '(gnu packages guile))
1490 (define* (load-path-expression modules #:optional (path %load-path)
1491 #:key (extensions '()))
1492 "Return as a monadic value a gexp that sets '%load-path' and
1493 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1494 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1495 (if (and (null? modules) (null? extensions))
1496 (with-monad %store-monad
1498 (mlet %store-monad ((modules (imported-modules modules
1499 #:module-path path))
1500 (compiled (compiled-modules modules
1501 #:extensions extensions
1502 #:module-path path)))
1503 (return (gexp (eval-when (expand load eval)
1505 (cons (ungexp modules)
1506 (append (map (lambda (extension)
1507 (string-append extension
1508 "/share/guile/site/"
1509 (effective-version)))
1510 '((ungexp-native-splicing extensions)))
1512 (set! %load-compiled-path
1513 (cons (ungexp compiled)
1514 (append (map (lambda (extension)
1515 (string-append extension
1519 '((ungexp-native-splicing extensions)))
1520 %load-compiled-path)))))))))
1522 (define* (gexp->script name exp
1523 #:key (guile (default-guile))
1524 (module-path %load-path))
1525 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1526 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1527 (mlet %store-monad ((set-load-path
1528 (load-path-expression (gexp-modules exp)
1531 (gexp-extensions exp))))
1532 (gexp->derivation name
1534 (call-with-output-file (ungexp output)
1536 ;; Note: that makes a long shebang. When the store
1537 ;; is /gnu/store, that fits within the 128-byte
1538 ;; limit imposed by Linux, but that may go beyond
1539 ;; when running tests.
1541 "#!~a/bin/guile --no-auto-compile~%!#~%"
1546 (gexp ((write '(ungexp set-load-path) port)))
1549 (write '(ungexp exp) port)
1550 (chmod port #o555))))
1551 #:module-path module-path)))
1553 (define* (gexp->file name exp #:key
1555 (module-path %load-path)
1557 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1558 is true, EXP is considered to be a list of expressions that will be spliced in
1561 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1562 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1563 Lookup EXP's modules in MODULE-PATH."
1564 (define modules (gexp-modules exp))
1565 (define extensions (gexp-extensions exp))
1567 (if (or (not set-load-path?)
1568 (and (null? modules) (null? extensions)))
1569 (gexp->derivation name
1571 (call-with-output-file (ungexp output)
1573 (for-each (lambda (exp)
1575 '(ungexp (if splice?
1577 (gexp ((ungexp exp)))))))))
1579 #:substitutable? #f)
1580 (mlet %store-monad ((set-load-path
1581 (load-path-expression modules module-path
1582 #:extensions extensions)))
1583 (gexp->derivation name
1585 (call-with-output-file (ungexp output)
1587 (write '(ungexp set-load-path) port)
1588 (for-each (lambda (exp)
1590 '(ungexp (if splice?
1592 (gexp ((ungexp exp)))))))))
1593 #:module-path module-path
1595 #:substitutable? #f))))
1597 (define* (text-file* name #:rest text)
1598 "Return as a monadic value a derivation that builds a text file containing
1599 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1600 can be used in a gexp: packages, derivations, local file objects, etc. The
1601 resulting store file holds references to all these."
1603 (gexp (call-with-output-file (ungexp output "out")
1605 (display (string-append (ungexp-splicing text)) port)))))
1607 (gexp->derivation name builder
1609 #:substitutable? #f))
1611 (define* (mixed-text-file name #:rest text)
1612 "Return an object representing store file NAME containing TEXT. TEXT is a
1613 sequence of strings and file-like objects, as in:
1615 (mixed-text-file \"profile\"
1616 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1618 This is the declarative counterpart of 'text-file*'."
1620 (gexp (call-with-output-file (ungexp output "out")
1622 (display (string-append (ungexp-splicing text)) port)))))
1624 (computed-file name build))
1626 (define (file-union name files)
1627 "Return a <computed-file> that builds a directory containing all of FILES.
1628 Each item in FILES must be a two-element list where the first element is the
1629 file name to use in the new directory, and the second element is a gexp
1630 denoting the target file. Here's an example:
1633 `((\"hosts\" ,(plain-file \"hosts\"
1634 \"127.0.0.1 localhost\"))
1635 (\"bashrc\" ,(plain-file \"bashrc\"
1636 \"alias ls='ls --color'\"))
1637 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1639 This yields an 'etc' directory containing these two files."
1641 (with-imported-modules '((guix build utils))
1644 (use-modules (guix build utils))
1646 (mkdir (ungexp output))
1647 (chdir (ungexp output))
1653 ;; Stat the source to abort early if it does
1655 (stat (ungexp source))
1657 (mkdir-p (dirname (ungexp target)))
1658 (symlink (ungexp source)
1659 (ungexp target))))))
1662 (define* (directory-union name things
1663 #:key (copy? #f) (quiet? #f)
1664 (resolve-collision 'warn-about-collision))
1665 "Return a directory that is the union of THINGS, where THINGS is a list of
1666 file-like objects denoting directories. For example:
1668 (directory-union \"guile+emacs\" (list guile emacs))
1670 yields a directory that is the union of the 'guile' and 'emacs' packages.
1672 Call RESOLVE-COLLISION when several files collide, passing it the list of
1673 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1674 which case the colliding entry is skipped altogether.
1676 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1677 is true, the derivation will not print anything."
1680 (gexp (lambda (old new)
1681 (if (file-is-directory? old)
1683 (copy-file old new))))
1688 (gexp (%make-void-port "w"))
1689 (gexp (current-error-port))))
1693 ;; Only one thing; return it.
1697 (with-imported-modules '((guix build union))
1699 (use-modules (guix build union)
1700 (srfi srfi-1)) ;for 'first' and 'last'
1702 (union-build (ungexp output)
1705 #:log-port (ungexp log-port)
1706 #:symlink (ungexp symlink)
1708 (ungexp resolve-collision)))))))))
1712 ;;; Syntactic sugar.
1715 (eval-when (expand load eval)
1716 (define* (read-ungexp chr port #:optional native?)
1717 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1718 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1719 (define unquote-symbol
1720 (match (peek-char port)
1724 'ungexp-native-splicing
1733 (let ((str (symbol->string symbol)))
1734 (match (string-index-right str #\:)
1736 `(,unquote-symbol ,symbol))
1738 (let ((name (string->symbol (substring str 0 colon)))
1739 (output (substring str (+ colon 1))))
1740 `(,unquote-symbol ,name ,output))))))
1742 `(,unquote-symbol ,x))))
1744 (define (read-gexp chr port)
1745 "Read a 'gexp' form from PORT."
1746 `(gexp ,(read port)))
1748 ;; Extend the reader
1749 (read-hash-extend #\~ read-gexp)
1750 (read-hash-extend #\$ read-ungexp)
1751 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1753 ;;; gexp.scm ends here