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
90 lowered-gexp-load-path
91 lowered-gexp-load-compiled-path
115 gexp-error-invalid-input))
119 ;;; This module implements "G-expressions", or "gexps". Gexps are like
120 ;;; S-expressions (sexps), with two differences:
122 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
123 ;;; replaced by the corresponding output file name; in addition, the
124 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
125 ;;; the native code of a given package, in case of cross-compilation;
127 ;;; 2. Gexps embed information about the derivations they refer to.
129 ;;; Gexps make it easy to write to files Scheme code that refers to store
130 ;;; items, or to write Scheme code to build derivations.
135 (define-record-type <gexp>
136 (make-gexp references modules extensions proc)
138 (references gexp-references) ;list of <gexp-input>
139 (modules gexp-self-modules) ;list of module names
140 (extensions gexp-self-extensions) ;list of lowerable things
141 (proc gexp-proc)) ;procedure
143 (define (write-gexp gexp port)
144 "Write GEXP on PORT."
145 (display "#<gexp " port)
147 ;; Try to write the underlying sexp. Now, this trick doesn't work when
148 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
149 ;; tries to use 'append' on that, which fails with wrong-type-arg.
151 (write (apply (gexp-proc gexp)
152 (gexp-references gexp))
155 (number->string (object-address gexp) 16)))
157 (set-record-type-printer! <gexp> write-gexp)
164 ;; Compiler for a type of objects that may be introduced in a gexp.
165 (define-record-type <gexp-compiler>
166 (gexp-compiler type lower expand)
168 (type gexp-compiler-type) ;record type descriptor
169 (lower gexp-compiler-lower)
170 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
172 (define-condition-type &gexp-error &error
175 (define-condition-type &gexp-input-error &gexp-error
177 (input gexp-error-invalid-input))
180 (define %gexp-compilers
181 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
182 (make-hash-table 20))
184 (define (default-expander thing obj output)
185 "This is the default expander for \"things\" that appear in gexps. It
186 returns its output file name of OBJ's OUTPUT."
189 (derivation->output-path drv output))
193 (define (register-compiler! compiler)
194 "Register COMPILER as a gexp compiler."
195 (hashq-set! %gexp-compilers
196 (gexp-compiler-type compiler) compiler))
198 (define (lookup-compiler object)
199 "Search for a compiler for OBJECT. Upon success, return the three argument
200 procedure to lower it; otherwise return #f."
201 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
202 gexp-compiler-lower))
204 (define (file-like? object)
205 "Return #t if OBJECT leads to a file in the store once unquoted in a
206 G-expression; otherwise return #f."
207 (and (struct? object) (->bool (lookup-compiler object))))
209 (define (lookup-expander object)
210 "Search for an expander for OBJECT. Upon success, return the three argument
211 procedure to expand it; otherwise return #f."
212 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
213 gexp-compiler-expand))
215 (define* (lower-object obj
216 #:optional (system (%current-system))
218 "Return as a value in %STORE-MONAD the derivation or store item
219 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
220 OBJ must be an object that has an associated gexp compiler, such as a
222 (match (lookup-compiler obj)
224 (raise (condition (&gexp-input-error (input obj)))))
226 ;; Cache in STORE the result of lowering OBJ.
227 (mlet %store-monad ((graft? (grafting?)))
228 (mcached (let ((lower (lookup-compiler obj)))
229 (lower obj system target))
231 system target graft?)))))
233 (define-syntax define-gexp-compiler
234 (syntax-rules (=> compiler expander)
235 "Define NAME as a compiler for objects matching PREDICATE encountered in
238 In the simplest form of the macro, BODY must return a derivation for PARAM, an
239 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
240 #f except when cross-compiling.)
242 The more elaborate form allows you to specify an expander:
244 (define-gexp-compiler something something?
245 compiler => (lambda (param system target) ...)
246 expander => (lambda (param drv output) ...))
248 The expander specifies how an object is converted to its sexp representation."
249 ((_ (name (param record-type) system target) body ...)
250 (define-gexp-compiler name record-type
251 compiler => (lambda (param system target) body ...)
252 expander => default-expander))
258 (gexp-compiler record-type compile expand))
259 (register-compiler! name)))))
261 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
262 ;; Derivations are the lowest-level representation, so this is the identity
264 (with-monad %store-monad
269 ;;; File declarations.
272 ;; A local file name. FILE is the file name the user entered, which can be a
273 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
274 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
275 ;; repeated 'stat' calls.
276 (define-record-type <local-file>
277 (%%local-file file absolute name recursive? select?)
279 (file local-file-file) ;string
280 (absolute %local-file-absolute-file-name) ;promise string
281 (name local-file-name) ;string
282 (recursive? local-file-recursive?) ;Boolean
283 (select? local-file-select?)) ;string stat -> Boolean
285 (define (true file stat) #t)
287 (define* (%local-file file promise #:optional (name (basename file))
288 #:key recursive? (select? true))
289 ;; This intermediate procedure is part of our ABI, but the underlying
290 ;; %%LOCAL-FILE is not.
291 (%%local-file file promise name recursive? select?))
293 (define (absolute-file-name file directory)
294 "Return the canonical absolute file name for FILE, which lives in the
295 vicinity of DIRECTORY."
297 (cond ((string-prefix? "/" file) file)
298 ((not directory) file)
299 ((string-prefix? "/" directory)
300 (string-append directory "/" file))
303 (define-syntax local-file
305 "Return an object representing local file FILE to add to the store; this
306 object can be used in a gexp. If FILE is a relative file name, it is looked
307 up relative to the source file where this form appears. FILE will be added to
308 the store under NAME--by default the base name of FILE.
310 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
311 designates a flat file and RECURSIVE? is true, its contents are added, and its
312 permission bits are kept.
314 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
315 where FILE is the entry's absolute file name and STAT is the result of
316 'lstat'; exclude entries for which SELECT? does not return true.
318 This is the declarative counterpart of the 'interned-file' monadic procedure.
319 It is implemented as a macro to capture the current source directory where it
323 (string? (syntax->datum #'file))
324 ;; FILE is a literal, so resolve it relative to the source directory.
326 (delay (absolute-file-name file (current-source-directory)))
329 ;; Resolve FILE relative to the current directory.
331 (delay (absolute-file-name file (getcwd)))
334 #'(syntax-error "missing file name"))
337 ;; XXX: We could return #'(lambda (file . rest) ...). However,
338 ;; (syntax-source #'id) is #f so (current-source-directory) would not
339 ;; work. Thus, simply forbid this form.
341 "'local-file' is a macro and cannot be used like this")))))
343 (define (local-file-absolute-file-name file)
344 "Return the absolute file name for FILE, a <local-file> instance. A
345 'system-error' exception is raised if FILE could not be found."
346 (force (%local-file-absolute-file-name file)))
348 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
349 ;; "Compile" FILE by adding it to the store.
351 (($ <local-file> file (= force absolute) name recursive? select?)
352 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
353 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
354 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
355 ;; just throw an error, both of which are inconvenient.
356 (interned-file absolute name
357 #:recursive? recursive? #:select? select?))))
359 (define-record-type <plain-file>
360 (%plain-file name content references)
362 (name plain-file-name) ;string
363 (content plain-file-content) ;string or bytevector
364 (references plain-file-references)) ;list (currently unused)
366 (define (plain-file name content)
367 "Return an object representing a text file called NAME with the given
368 CONTENT (a string) to be added to the store.
370 This is the declarative counterpart of 'text-file'."
371 ;; XXX: For now just ignore 'references' because it's not clear how to use
372 ;; them in a declarative context.
373 (%plain-file name content '()))
375 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
376 ;; "Compile" FILE by adding it to the store.
378 (($ <plain-file> name (and (? string?) content) references)
379 (text-file name content references))
380 (($ <plain-file> name (and (? bytevector?) content) references)
381 (binary-file name content references))))
383 (define-record-type <computed-file>
384 (%computed-file name gexp guile options)
386 (name computed-file-name) ;string
387 (gexp computed-file-gexp) ;gexp
388 (guile computed-file-guile) ;<package>
389 (options computed-file-options)) ;list of arguments
391 (define* (computed-file name gexp
392 #:key guile (options '(#:local-build? #t)))
393 "Return an object representing the store item NAME, a file or directory
394 computed by GEXP. OPTIONS is a list of additional arguments to pass
395 to 'gexp->derivation'.
397 This is the declarative counterpart of 'gexp->derivation'."
398 (%computed-file name gexp guile options))
400 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
402 ;; Compile FILE by returning a derivation whose build expression is its
405 (($ <computed-file> name gexp guile options)
407 (mlet %store-monad ((guile (lower-object guile system
409 (apply gexp->derivation name gexp #:guile-for-build guile
410 #:system system #:target target options))
411 (apply gexp->derivation name gexp
412 #:system system #:target target options)))))
414 (define-record-type <program-file>
415 (%program-file name gexp guile path)
417 (name program-file-name) ;string
418 (gexp program-file-gexp) ;gexp
419 (guile program-file-guile) ;package
420 (path program-file-module-path)) ;list of strings
422 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
423 "Return an object representing the executable store item NAME that runs
424 GEXP. GUILE is the Guile package used to execute that script. Imported
425 modules of GEXP are looked up in MODULE-PATH.
427 This is the declarative counterpart of 'gexp->script'."
428 (%program-file name gexp guile module-path))
430 (define-gexp-compiler (program-file-compiler (file <program-file>)
432 ;; Compile FILE by returning a derivation that builds the script.
434 (($ <program-file> name gexp guile module-path)
435 (gexp->script name gexp
436 #:module-path module-path
437 #:guile (or guile (default-guile))
441 (define-record-type <scheme-file>
442 (%scheme-file name gexp splice?)
444 (name scheme-file-name) ;string
445 (gexp scheme-file-gexp) ;gexp
446 (splice? scheme-file-splice?)) ;Boolean
448 (define* (scheme-file name gexp #:key splice?)
449 "Return an object representing the Scheme file NAME that contains GEXP.
451 This is the declarative counterpart of 'gexp->file'."
452 (%scheme-file name gexp splice?))
454 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
456 ;; Compile FILE by returning a derivation that builds the file.
458 (($ <scheme-file> name gexp splice?)
459 (gexp->file name gexp #:splice? splice?))))
461 ;; Appending SUFFIX to BASE's output file name.
462 (define-record-type <file-append>
463 (%file-append base suffix)
465 (base file-append-base) ;<package> | <derivation> | ...
466 (suffix file-append-suffix)) ;list of strings
468 (define (write-file-append file port)
470 (($ <file-append> base suffix)
471 (format port "#<file-append ~s ~s>" base
472 (string-join suffix)))))
474 (set-record-type-printer! <file-append> write-file-append)
476 (define (file-append base . suffix)
477 "Return a <file-append> object that expands to the concatenation of BASE and
479 (%file-append base suffix))
481 (define-gexp-compiler file-append-compiler <file-append>
482 compiler => (lambda (obj system target)
484 (($ <file-append> base _)
485 (lower-object base system #:target target))))
486 expander => (lambda (obj lowered output)
488 (($ <file-append> base suffix)
489 (let* ((expand (lookup-expander base))
490 (base (expand base lowered output)))
491 (string-append base (string-concatenate suffix)))))))
495 ;;; Inputs & outputs.
498 ;; The input of a gexp.
499 (define-record-type <gexp-input>
500 (%gexp-input thing output native?)
502 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
503 (output gexp-input-output) ;string
504 (native? gexp-input-native?)) ;Boolean
506 (define (write-gexp-input input port)
508 (($ <gexp-input> thing output #f)
509 (format port "#<gexp-input ~s:~a>" thing output))
510 (($ <gexp-input> thing output #t)
511 (format port "#<gexp-input native ~s:~a>" thing output))))
513 (set-record-type-printer! <gexp-input> write-gexp-input)
515 (define* (gexp-input thing ;convenience procedure
516 #:optional (output "out")
518 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
519 whether this should be considered a \"native\" input or not."
520 (%gexp-input thing output native?))
522 ;; Reference to one of the derivation's outputs, for gexps used in
524 (define-record-type <gexp-output>
527 (name gexp-output-name))
529 (define (write-gexp-output output port)
531 (($ <gexp-output> name)
532 (format port "#<gexp-output ~a>" name))))
534 (set-record-type-printer! <gexp-output> write-gexp-output)
536 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
537 "Recurse on GEXP and the expressions it refers to, summing the items
538 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
539 second argument to 'delete-duplicates'."
542 (append (self-attribute gexp)
543 (append-map (match-lambda
544 (($ <gexp-input> (? gexp? exp))
545 (gexp-attribute exp self-attribute))
546 (($ <gexp-input> (lst ...))
547 (append-map (lambda (item)
555 (gexp-references gexp)))
557 '())) ;plain Scheme data type
559 (define (gexp-modules gexp)
560 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
561 false, meaning that GEXP is a plain Scheme object, return the empty list."
562 (define (module=? m1 m2)
563 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
564 ;; right-hand side may not be comparable with 'equal?': it's typically a
565 ;; file-like object that embeds a gexp, which in turn embeds closure;
566 ;; those closures may be 'eq?' when running compiled code but are unlikely
567 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
568 ;; avoid this discrepancy.
572 (((name2 ...) '=> _) (equal? name1 name2))
577 (gexp-attribute gexp gexp-self-modules module=?))
579 (define (gexp-extensions gexp)
580 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
581 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
583 (gexp-attribute gexp gexp-self-extensions))
585 (define* (lower-inputs inputs
587 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
588 item (a \"source\"); return the corresponding input list as a monadic value.
589 When TARGET is true, use it as the cross-compilation target triplet."
590 (define (store-item? obj)
591 (and (string? obj) (store-path? obj)))
593 (with-monad %store-monad
596 (((? struct? thing) sub-drv ...)
597 (mlet %store-monad ((obj (lower-object
598 thing system #:target target)))
601 (let ((outputs (if (null? sub-drv)
604 (derivation-input drv outputs)))
605 ((? store-item? item)
607 (((? store-item? item))
611 (define* (lower-reference-graphs graphs #:key system target)
612 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
613 #:reference-graphs argument, lower it such that each INPUT is replaced by the
614 corresponding <derivation-input> or store item."
616 (((file-names . inputs) ...)
617 (mlet %store-monad ((inputs (lower-inputs inputs
620 (return (map cons file-names inputs))))))
622 (define* (lower-references lst #:key system target)
623 "Based on LST, a list of output names and packages, return a list of output
624 names and file names suitable for the #:allowed-references argument to
626 (with-monad %store-monad
631 (($ <gexp-input> thing output native?)
632 (mlet %store-monad ((drv (lower-object thing system
635 (return (derivation->output-path drv output))))
637 (mlet %store-monad ((drv (lower-object thing system
639 (return (derivation->output-path drv))))))
641 (mapm %store-monad lower lst)))
643 (define default-guile-derivation
644 ;; Here we break the abstraction by talking to the higher-level layer.
645 ;; Thus, do the resolution lazily to hide the circular dependency.
647 (let ((iface (resolve-interface '(guix packages))))
648 (module-ref iface 'default-guile-derivation)))))
650 ((force proc) system))))
652 ;; Representation of a gexp instantiated for a given target and system.
653 ;; It's an intermediate representation between <gexp> and <derivation>.
654 (define-record-type <lowered-gexp>
655 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
657 (sexp lowered-gexp-sexp) ;sexp
658 (inputs lowered-gexp-inputs) ;list of <derivation-input>
659 (sources lowered-gexp-sources) ;list of store items
660 (guile lowered-gexp-guile) ;<derivation-input> | #f
661 (load-path lowered-gexp-load-path) ;list of store items
662 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
664 (define* (imported+compiled-modules modules system
665 #:key (extensions '())
666 deprecation-warnings guile
667 (module-path %load-path))
668 "Return a pair where the first element is the imported MODULES and the
669 second element is the derivation to compile them."
671 (mlet %store-monad ((modules (if (pair? modules)
672 (imported-modules modules
674 #:module-path module-path)
676 (compiled (if (pair? modules)
677 (compiled-modules modules
679 #:module-path module-path
680 #:extensions extensions
682 #:deprecation-warnings
683 deprecation-warnings)
685 (return (cons modules compiled)))
687 system extensions guile deprecation-warnings module-path))
689 (define* (lower-gexp exp
691 (module-path %load-path)
692 (system (%current-system))
695 (guile-for-build (%guile-for-build))
696 (effective-version "2.2")
698 deprecation-warnings)
699 "*Note: This API is subject to change; use at your own risk!*
701 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
702 <lowered-gexp> ready to be used.
704 Lowered gexps are an intermediate representation that's useful for
705 applications that deal with gexps outside in a way that is disconnected from
706 derivations--e.g., code evaluated for its side effects."
708 (delete-duplicates (gexp-modules exp)))
710 (define (search-path modules extensions suffix)
711 (append (match modules
713 (list (derivation->output-path drv)))
716 ((? store-path? item)
718 (map (lambda (extension)
719 (string-append (match extension
721 (derivation->output-path drv))
722 ((? store-path? item)
727 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
728 ;; '%current-target-system' to be looked up at >>=
730 (graft? (set-grafting graft?))
732 (system -> (or system (%current-system)))
733 (target -> (if (eq? target 'current)
734 (%current-target-system)
736 (guile (if guile-for-build
737 (return guile-for-build)
738 (default-guile-derivation system)))
739 (normals (lower-inputs (gexp-inputs exp)
742 (natives (lower-inputs (gexp-native-inputs exp)
745 (inputs -> (append normals natives))
746 (sexp (gexp->sexp exp
749 (extensions -> (gexp-extensions exp))
750 (exts (mapm %store-monad
752 (lower-object obj system))
754 (modules+compiled (imported+compiled-modules
756 #:extensions extensions
757 #:deprecation-warnings
760 #:module-path module-path))
761 (modules -> (car modules+compiled))
762 (compiled -> (cdr modules+compiled)))
764 (search-path modules exts
765 (string-append "/share/guile/site/" effective-version)))
767 (define load-compiled-path
768 (search-path compiled exts
769 (string-append "/lib/guile/" effective-version
773 (set-grafting graft?) ;restore the initial setting
774 (return (lowered-gexp sexp
775 `(,@(if (derivation? modules)
776 (list (derivation-input modules))
779 (list (derivation-input compiled))
781 ,@(map derivation-input exts)
782 ,@(filter derivation-input? inputs))
783 (filter string? (cons modules inputs))
784 (derivation-input guile '("out"))
786 load-compiled-path)))))
788 (define* (gexp->derivation name exp
790 system (target 'current)
791 hash hash-algo recursive?
794 (module-path %load-path)
795 (guile-for-build (%guile-for-build))
796 (effective-version "2.2")
799 allowed-references disallowed-references
801 local-build? (substitutable? #t)
804 (script-name (string-append name "-builder")))
805 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
806 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
807 TARGET is true, it is used as the cross-compilation target triplet for
808 packages referred to by EXP.
810 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
811 make MODULES available in the evaluation context of EXP; MODULES is a list of
812 names of Guile modules searched in MODULE-PATH to be copied in the store,
813 compiled, and made available in the load path during the execution of
814 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
816 EFFECTIVE-VERSION determines the string to use when adding extensions of
817 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
819 GRAFT? determines whether packages referred to by EXP should be grafted when
822 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
826 (FILE-NAME PACKAGE OUTPUT)
827 (FILE-NAME DERIVATION)
828 (FILE-NAME DERIVATION OUTPUT)
829 (FILE-NAME STORE-ITEM)
831 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
832 an input of the build process of EXP. In the build environment, each
833 FILE-NAME contains the reference graph of the corresponding item, in a simple
836 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
837 In the latter case, the list denotes store items that the result is allowed to
838 refer to. Any reference to another store item will lead to a build error.
839 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
840 referenced by the outputs.
842 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
843 compiling modules. It can be #f, #t, or 'detailed.
845 The other arguments are as for 'derivation'."
846 (define outputs (gexp-outputs exp))
847 (define requested-graft? graft?)
849 (define (graphs-file-names graphs)
850 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
852 ((file-name . (? derivation-input? input))
853 (cons file-name (first (derivation-input-output-paths input))))
854 ((file-name . (? string? item))
855 (cons file-name item)))
858 (define (add-modules exp modules)
861 (make-gexp (gexp-references exp)
862 (append modules (gexp-self-modules exp))
863 (gexp-self-extensions exp)
866 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
867 ;; '%current-target-system' to be looked up at >>=
869 (graft? (set-grafting graft?))
871 (system -> (or system (%current-system)))
872 (target -> (if (eq? target 'current)
873 (%current-target-system)
875 (exp -> (add-modules exp modules))
876 (lowered (lower-gexp exp
877 #:module-path module-path
880 #:graft? requested-graft?
885 #:deprecation-warnings
886 deprecation-warnings))
888 (graphs (if references-graphs
889 (lower-reference-graphs references-graphs
893 (allowed (if allowed-references
894 (lower-references allowed-references
898 (disallowed (if disallowed-references
899 (lower-references disallowed-references
903 (guile -> (lowered-gexp-guile lowered))
904 (builder (text-file script-name
906 (lowered-gexp-sexp lowered)))))
908 (set-grafting graft?) ;restore the initial setting
910 (string-append (derivation-input-output-path guile)
912 `("--no-auto-compile"
913 ,@(append-map (lambda (directory)
915 (lowered-gexp-load-path lowered))
916 ,@(append-map (lambda (directory)
918 (lowered-gexp-load-compiled-path lowered))
924 ,@(lowered-gexp-inputs lowered)
927 (filter derivation-input? inputs))
930 ,@(if (and (string? modules)
931 (store-path? modules))
934 ,@(lowered-gexp-sources lowered)
937 (filter string? inputs))
940 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
941 #:references-graphs (and=> graphs graphs-file-names)
942 #:allowed-references allowed
943 #:disallowed-references disallowed
944 #:leaked-env-vars leaked-env-vars
945 #:local-build? local-build?
946 #:substitutable? substitutable?
947 #:properties properties))))
949 (define* (gexp-inputs exp #:key native?)
950 "Return the input list for EXP. When NATIVE? is true, return only native
951 references; otherwise, return only non-native references."
952 ;; TODO: Return <gexp-input> records instead of tuples.
953 (define (add-reference-inputs ref result)
955 (($ <gexp-input> (? gexp? exp) _ #t)
957 (append (gexp-inputs exp)
958 (gexp-inputs exp #:native? #t)
961 (($ <gexp-input> (? gexp? exp) _ #f)
962 (append (gexp-inputs exp #:native? native?)
964 (($ <gexp-input> (? string? str))
965 (if (direct-store-path? str)
966 (cons `(,str) result)
968 (($ <gexp-input> (? struct? thing) output n?)
969 (if (and (eqv? n? native?) (lookup-compiler thing))
970 ;; THING is a derivation, or a package, or an origin, etc.
971 (cons `(,thing ,output) result)
973 (($ <gexp-input> (lst ...) output n?)
974 (fold-right add-reference-inputs result
975 ;; XXX: For now, automatically convert LST to a list of
976 ;; gexp-inputs. Inherit N?.
979 (%gexp-input (gexp-input-thing x)
980 (gexp-input-output x)
983 (%gexp-input x "out" n?)))
986 ;; Ignore references to other kinds of objects.
989 (fold-right add-reference-inputs
991 (gexp-references exp)))
993 (define gexp-native-inputs
994 (cut gexp-inputs <> #:native? #t))
996 (define (gexp-outputs exp)
997 "Return the outputs referred to by EXP as a list of strings."
998 (define (add-reference-output ref result)
1000 (($ <gexp-output> name)
1002 (($ <gexp-input> (? gexp? exp))
1003 (append (gexp-outputs exp) result))
1004 (($ <gexp-input> (lst ...) output native?)
1005 ;; XXX: Automatically convert LST.
1006 (add-reference-output (map (match-lambda
1007 ((? gexp-input? x) x)
1008 (x (%gexp-input x "out" native?)))
1012 (fold-right add-reference-output result lst))
1017 (add-reference-output (gexp-references exp) '())))
1019 (define* (gexp->sexp exp #:key
1020 (system (%current-system))
1021 (target (%current-target-system)))
1022 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1023 and in the current monad setting (system type, etc.)"
1024 (define (self-quoting? x)
1025 (letrec-syntax ((one-of (syntax-rules ()
1029 (one-of rest ...))))))
1030 (one-of symbol? string? keyword? pair? null? array?
1033 (define* (reference->sexp ref #:optional native?)
1034 (with-monad %store-monad
1036 (($ <gexp-output> output)
1037 ;; Output file names are not known in advance but the daemon defines
1038 ;; an environment variable for each of them at build time, so use
1040 (return `((@ (guile) getenv) ,output)))
1041 (($ <gexp-input> (? gexp? exp) output n?)
1044 #:target (if (or n? native?) #f target)))
1045 (($ <gexp-input> (refs ...) output n?)
1048 ;; XXX: Automatically convert REF to an gexp-input.
1050 (if (gexp-input? ref)
1052 (%gexp-input ref "out" n?))
1055 (($ <gexp-input> (? struct? thing) output n?)
1056 (let ((target (if (or n? native?) #f target))
1057 (expand (lookup-expander thing)))
1058 (mlet %store-monad ((obj (lower-object thing system
1060 ;; OBJ must be either a derivation or a store file name.
1061 (return (expand thing obj output)))))
1062 (($ <gexp-input> (? self-quoting? x))
1065 (raise (condition (&gexp-input-error (input x)))))
1070 ((args (mapm %store-monad
1071 reference->sexp (gexp-references exp))))
1072 (return (apply (gexp-proc exp) args))))
1074 (define-syntax-rule (define-syntax-parameter-once name proc)
1075 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1076 ;; does not get redefined. This works around a race condition in a
1077 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1078 (eval-when (load eval expand compile)
1080 (if (module-locally-bound? (current-module) 'name)
1081 (module-ref (current-module) 'name)
1082 (make-syntax-transformer 'name 'syntax-parameter
1085 (define-syntax-parameter-once current-imported-modules
1086 ;; Current list of imported modules.
1087 (identifier-syntax '()))
1089 (define-syntax-rule (with-imported-modules modules body ...)
1090 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1092 (syntax-parameterize ((current-imported-modules
1093 (identifier-syntax modules)))
1096 (define-syntax-parameter-once current-imported-extensions
1097 ;; Current list of extensions.
1098 (identifier-syntax '()))
1100 (define-syntax-rule (with-extensions extensions body ...)
1101 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1102 execution environment."
1103 (syntax-parameterize ((current-imported-extensions
1104 (identifier-syntax extensions)))
1109 (define (collect-escapes exp)
1110 ;; Return all the 'ungexp' present in EXP.
1111 (let loop ((exp exp)
1113 (syntax-case exp (ungexp
1116 ungexp-native-splicing)
1121 ((ungexp-splicing _ ...)
1123 ((ungexp-native _ ...)
1125 ((ungexp-native-splicing _ ...)
1128 (let ((result (loop #'exp0 result)))
1129 (loop #'exp result)))
1133 (define (escape->ref exp)
1134 ;; Turn 'ungexp' form EXP into a "reference".
1135 (syntax-case exp (ungexp ungexp-splicing
1136 ungexp-native ungexp-native-splicing
1139 #'(gexp-output "out"))
1140 ((ungexp output name)
1141 #'(gexp-output name))
1143 #'(%gexp-input thing "out" #f))
1144 ((ungexp drv-or-pkg out)
1145 #'(%gexp-input drv-or-pkg out #f))
1146 ((ungexp-splicing lst)
1147 #'(%gexp-input lst "out" #f))
1148 ((ungexp-native thing)
1149 #'(%gexp-input thing "out" #t))
1150 ((ungexp-native drv-or-pkg out)
1151 #'(%gexp-input drv-or-pkg out #t))
1152 ((ungexp-native-splicing lst)
1153 #'(%gexp-input lst "out" #t))))
1155 (define (substitute-ungexp exp substs)
1156 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1157 ;; the corresponding form in SUBSTS.
1158 (match (assoc exp substs)
1162 (with-syntax ((exp exp))
1163 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1165 (define (substitute-ungexp-splicing exp substs)
1168 (match (assoc #'exp substs)
1170 (with-syntax ((id id))
1172 #,(substitute-references #'(rest ...) substs))))
1174 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1177 (define (substitute-references exp substs)
1178 ;; Return a variant of EXP where all the cars of SUBSTS have been
1179 ;; replaced by the corresponding cdr.
1180 (syntax-case exp (ungexp ungexp-native
1181 ungexp-splicing ungexp-native-splicing)
1183 (substitute-ungexp exp substs))
1184 ((ungexp-native _ ...)
1185 (substitute-ungexp exp substs))
1186 (((ungexp-splicing _ ...) rest ...)
1187 (substitute-ungexp-splicing exp substs))
1188 (((ungexp-native-splicing _ ...) rest ...)
1189 (substitute-ungexp-splicing exp substs))
1191 #`(cons #,(substitute-references #'exp0 substs)
1192 #,(substitute-references #'exp substs)))
1195 (syntax-case s (ungexp output)
1197 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1198 (formals (generate-temporaries escapes))
1199 (sexp (substitute-references #'exp (zip escapes formals)))
1200 (refs (map escape->ref escapes)))
1201 #`(make-gexp (list #,@refs)
1202 current-imported-modules
1203 current-imported-extensions
1209 ;;; Module handling.
1213 (char-set-complement (char-set #\/)))
1215 (define (file-mapping->tree mapping)
1216 "Convert MAPPING, an alist like:
1218 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1220 to a tree suitable for 'interned-file-tree'."
1221 (let ((mapping (map (match-lambda
1222 ((destination . source)
1223 (cons (string-tokenize destination
1227 (fold (lambda (pair result)
1229 ((destination . source)
1230 (let loop ((destination destination)
1234 (let* ((mode (stat:mode (stat source)))
1235 (type (if (zero? (logand mode #o100))
1239 `(,type (file ,source))
1242 (let ((directory (assoc-ref result file)))
1247 (('directory . entries) entries)
1250 (alist-delete file result)
1255 (define %utils-module
1256 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1257 ;; other primitives below. Note: We give the file name relative to this
1258 ;; file you are currently reading; 'search-path' could return a file name
1259 ;; relative to the current working directory.
1260 (local-file "build/utils.scm"
1263 (define* (imported-files/derivation files
1264 #:key (name "file-import")
1266 (system (%current-system))
1267 (guile (%guile-for-build)))
1268 "Return a derivation that imports FILES into STORE. FILES must be a list
1269 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1270 resulting store path. FILE can be either a file name, or a file-like object,
1271 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1272 to the source files instead of copying them."
1275 ((final-path . (? string? file-name))
1276 (mlet %store-monad ((file (interned-file file-name
1277 (basename final-path))))
1278 (return (list final-path file))))
1279 ((final-path . file-like)
1280 (mlet %store-monad ((file (lower-object file-like system)))
1281 (return (list final-path file))))))
1283 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1287 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1288 (use-modules (ice-9 match))
1290 (mkdir (ungexp output)) (chdir (ungexp output))
1291 (for-each (match-lambda
1292 ((final-path store-path)
1293 (mkdir-p (dirname final-path))
1294 ((ungexp (if symlink? 'symlink 'copy-file))
1295 store-path final-path)))
1298 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1299 ;; exactly the same regardless of FILES: less disk space, and fewer
1300 ;; 'add-to-store' RPCs.
1301 (gexp->derivation name build
1303 #:guile-for-build guile
1306 ;; Avoid deprecation warnings about the use of the _IO*
1307 ;; constants in (guix build utils).
1309 '(("GUILE_WARN_DEPRECATED" . "no")))))
1311 (define* (imported-files files
1312 #:key (name "file-import")
1313 ;; The following parameters make sense when creating
1314 ;; an actual derivation.
1315 (system (%current-system))
1316 (guile (%guile-for-build)))
1317 "Import FILES into the store and return the resulting derivation or store
1318 file name (a derivation is created if and only if some elements of FILES are
1319 file-like objects and not local file names.) FILES must be a list
1320 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1321 resulting store path. FILE can be either a file name, or a file-like object,
1322 as returned by 'local-file' for example."
1323 (if (any (match-lambda
1324 ((_ . (? struct? source)) #t)
1327 (imported-files/derivation files #:name name
1328 #:symlink? derivation?
1329 #:system system #:guile guile)
1330 (interned-file-tree `(,name directory
1331 ,@(file-mapping->tree files)))))
1333 (define* (imported-modules modules
1334 #:key (name "module-import")
1335 (system (%current-system))
1336 (guile (%guile-for-build))
1337 (module-path %load-path))
1338 "Return a derivation that contains the source files of MODULES, a list of
1339 module names such as `(ice-9 q)'. All of MODULES must be either names of
1340 modules to be found in the MODULE-PATH search path, or a module name followed
1341 by an arrow followed by a file-like object. For example:
1343 (imported-modules `((guix build utils)
1345 ((guix config) => ,(scheme-file …))))
1347 In this example, the first two modules are taken from MODULE-PATH, and the
1348 last one is created from the given <scheme-file> object."
1349 (let ((files (map (match-lambda
1350 (((module ...) '=> file)
1351 (cons (module->source-file-name module)
1354 (let ((f (module->source-file-name module)))
1355 (cons f (search-path* module-path f)))))
1357 (imported-files files #:name name
1361 (define* (compiled-modules modules
1362 #:key (name "module-import-compiled")
1363 (system (%current-system))
1365 (guile (%guile-for-build))
1366 (module-path %load-path)
1368 (deprecation-warnings #f))
1369 "Return a derivation that builds a tree containing the `.go' files
1370 corresponding to MODULES. All the MODULES are built in a context where
1371 they can refer to each other. When TARGET is true, cross-compile MODULES for
1372 TARGET, a GNU triplet."
1373 (define total (length modules))
1375 (mlet %store-monad ((modules (imported-modules modules
1383 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1385 (use-modules (ice-9 ftw)
1389 (system base compile))
1391 ;; TODO: Inline this on the next rebuild cycle.
1394 (gexp ((use-modules (system base target))))
1397 (define (regular? file)
1398 (not (member file '("." ".."))))
1400 (define (process-entry entry output processed)
1401 (if (file-is-directory? entry)
1402 (let ((output (string-append output "/" (basename entry))))
1404 (process-directory entry output processed))
1405 (let* ((base (basename entry ".scm"))
1406 (output (string-append output "/" base ".go")))
1407 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1408 (+ 1 processed (ungexp total))
1409 (ungexp (* total 2))
1414 (gexp ((with-target (ungexp target)
1417 #:output-file output
1419 %auto-compilation-options)))))
1420 (gexp ((compile-file entry
1421 #:output-file output
1422 #:opts %auto-compilation-options)))))
1426 (define (process-directory directory output processed)
1427 (let ((entries (map (cut string-append directory "/" <>)
1428 (scandir directory regular?))))
1429 (fold (cut process-entry <> output <>)
1433 (define* (load-from-directory directory
1434 #:optional (loaded 0))
1435 "Load all the source files found in DIRECTORY."
1436 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1437 (let ((entries (map (cut string-append directory "/" <>)
1438 (scandir directory regular?))))
1439 (fold (lambda (file loaded)
1440 (if (file-is-directory? file)
1441 (load-from-directory file loaded)
1443 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1444 (+ 1 loaded) (ungexp (* 2 total))
1446 (save-module-excursion
1448 (primitive-load file)))
1453 (setvbuf (current-output-port)
1454 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1457 ;; Capture 'mkdir-p'.
1458 (@ (guix build utils) mkdir-p))
1460 ;; Add EXTENSIONS to the search path.
1462 (append (map (lambda (extension)
1463 (string-append extension
1464 "/share/guile/site/"
1465 (effective-version)))
1466 '((ungexp-native-splicing extensions)))
1468 (set! %load-compiled-path
1469 (append (map (lambda (extension)
1470 (string-append extension "/lib/guile/"
1473 '((ungexp-native-splicing extensions)))
1474 %load-compiled-path))
1476 (set! %load-path (cons (ungexp modules) %load-path))
1478 ;; Above we loaded our own (guix build utils) but now we may need to
1479 ;; load a compile a different one. Thus, force a reload.
1480 (let ((utils (string-append (ungexp modules)
1481 "/guix/build/utils.scm")))
1482 (when (file-exists? utils)
1485 (mkdir (ungexp output))
1486 (chdir (ungexp modules))
1488 (load-from-directory ".")
1489 (process-directory "." (ungexp output) 0))))
1491 ;; TODO: Pass MODULES as an environment variable.
1492 (gexp->derivation name build
1494 #:guile-for-build guile
1497 (case deprecation-warnings
1499 '(("GUILE_WARN_DEPRECATED" . "no")))
1501 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1507 ;;; Convenience procedures.
1510 (define (default-guile)
1511 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1512 ;; programs returned by 'program-file' and we don't want to keep references
1513 ;; to several Guile packages). This module must not refer to (gnu …)
1514 ;; modules directly, to avoid circular dependencies, hence this hack.
1515 (module-ref (resolve-interface '(gnu packages guile))
1518 (define* (load-path-expression modules #:optional (path %load-path)
1519 #:key (extensions '()) system target)
1520 "Return as a monadic value a gexp that sets '%load-path' and
1521 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1522 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1523 (if (and (null? modules) (null? extensions))
1524 (with-monad %store-monad
1526 (mlet %store-monad ((modules (imported-modules modules
1529 (compiled (compiled-modules modules
1530 #:extensions extensions
1535 (gexp (eval-when (expand load eval)
1536 ;; Augment the load paths and delete duplicates. Do that
1537 ;; without loading (srfi srfi-1) or anything.
1538 (let ((extensions '((ungexp-splicing extensions)))
1539 (prepend (lambda (items lst)
1540 ;; This is O(N²) but N is typically small.
1541 (let loop ((items items)
1547 (delete (car items) lst))))))))
1549 (prepend (cons (ungexp modules)
1550 (map (lambda (extension)
1551 (string-append extension
1552 "/share/guile/site/"
1553 (effective-version)))
1556 (set! %load-compiled-path
1557 (prepend (cons (ungexp compiled)
1558 (map (lambda (extension)
1559 (string-append extension
1564 %load-compiled-path)))))))))
1566 (define* (gexp->script name exp
1567 #:key (guile (default-guile))
1568 (module-path %load-path)
1569 (system (%current-system))
1571 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1572 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1573 (mlet %store-monad ((set-load-path
1574 (load-path-expression (gexp-modules exp)
1577 (gexp-extensions exp)
1580 (gexp->derivation name
1582 (call-with-output-file (ungexp output)
1584 ;; Note: that makes a long shebang. When the store
1585 ;; is /gnu/store, that fits within the 128-byte
1586 ;; limit imposed by Linux, but that may go beyond
1587 ;; when running tests.
1589 "#!~a/bin/guile --no-auto-compile~%!#~%"
1594 (gexp ((write '(ungexp set-load-path) port)))
1597 (write '(ungexp exp) port)
1598 (chmod port #o555))))
1601 #:module-path module-path)))
1603 (define* (gexp->file name exp #:key
1605 (module-path %load-path)
1607 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1608 is true, EXP is considered to be a list of expressions that will be spliced in
1611 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1612 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1613 Lookup EXP's modules in MODULE-PATH."
1614 (define modules (gexp-modules exp))
1615 (define extensions (gexp-extensions exp))
1617 (if (or (not set-load-path?)
1618 (and (null? modules) (null? extensions)))
1619 (gexp->derivation name
1621 (call-with-output-file (ungexp output)
1623 (for-each (lambda (exp)
1625 '(ungexp (if splice?
1627 (gexp ((ungexp exp)))))))))
1629 #:substitutable? #f)
1630 (mlet %store-monad ((set-load-path
1631 (load-path-expression modules module-path
1632 #:extensions extensions)))
1633 (gexp->derivation name
1635 (call-with-output-file (ungexp output)
1637 (write '(ungexp set-load-path) port)
1638 (for-each (lambda (exp)
1640 '(ungexp (if splice?
1642 (gexp ((ungexp exp)))))))))
1643 #:module-path module-path
1645 #:substitutable? #f))))
1647 (define* (text-file* name #:rest text)
1648 "Return as a monadic value a derivation that builds a text file containing
1649 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1650 can be used in a gexp: packages, derivations, local file objects, etc. The
1651 resulting store file holds references to all these."
1653 (gexp (call-with-output-file (ungexp output "out")
1655 (display (string-append (ungexp-splicing text)) port)))))
1657 (gexp->derivation name builder
1659 #:substitutable? #f))
1661 (define* (mixed-text-file name #:rest text)
1662 "Return an object representing store file NAME containing TEXT. TEXT is a
1663 sequence of strings and file-like objects, as in:
1665 (mixed-text-file \"profile\"
1666 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1668 This is the declarative counterpart of 'text-file*'."
1670 (gexp (call-with-output-file (ungexp output "out")
1672 (display (string-append (ungexp-splicing text)) port)))))
1674 (computed-file name build))
1676 (define (file-union name files)
1677 "Return a <computed-file> that builds a directory containing all of FILES.
1678 Each item in FILES must be a two-element list where the first element is the
1679 file name to use in the new directory, and the second element is a gexp
1680 denoting the target file. Here's an example:
1683 `((\"hosts\" ,(plain-file \"hosts\"
1684 \"127.0.0.1 localhost\"))
1685 (\"bashrc\" ,(plain-file \"bashrc\"
1686 \"alias ls='ls --color'\"))
1687 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1689 This yields an 'etc' directory containing these two files."
1691 (with-imported-modules '((guix build utils))
1694 (use-modules (guix build utils))
1696 (mkdir (ungexp output))
1697 (chdir (ungexp output))
1703 ;; Stat the source to abort early if it does
1705 (stat (ungexp source))
1707 (mkdir-p (dirname (ungexp target)))
1708 (symlink (ungexp source)
1709 (ungexp target))))))
1712 (define* (directory-union name things
1713 #:key (copy? #f) (quiet? #f)
1714 (resolve-collision 'warn-about-collision))
1715 "Return a directory that is the union of THINGS, where THINGS is a list of
1716 file-like objects denoting directories. For example:
1718 (directory-union \"guile+emacs\" (list guile emacs))
1720 yields a directory that is the union of the 'guile' and 'emacs' packages.
1722 Call RESOLVE-COLLISION when several files collide, passing it the list of
1723 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1724 which case the colliding entry is skipped altogether.
1726 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1727 is true, the derivation will not print anything."
1730 (gexp (lambda (old new)
1731 (if (file-is-directory? old)
1733 (copy-file old new))))
1738 (gexp (%make-void-port "w"))
1739 (gexp (current-error-port))))
1743 ;; Only one thing; return it.
1747 (with-imported-modules '((guix build union))
1749 (use-modules (guix build union)
1750 (srfi srfi-1)) ;for 'first' and 'last'
1752 (union-build (ungexp output)
1755 #:log-port (ungexp log-port)
1756 #:symlink (ungexp symlink)
1758 (ungexp resolve-collision)))))))))
1762 ;;; Syntactic sugar.
1765 (eval-when (expand load eval)
1766 (define* (read-ungexp chr port #:optional native?)
1767 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1768 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1769 (define unquote-symbol
1770 (match (peek-char port)
1774 'ungexp-native-splicing
1783 (let ((str (symbol->string symbol)))
1784 (match (string-index-right str #\:)
1786 `(,unquote-symbol ,symbol))
1788 (let ((name (string->symbol (substring str 0 colon)))
1789 (output (substring str (+ colon 1))))
1790 `(,unquote-symbol ,name ,output))))))
1792 `(,unquote-symbol ,x))))
1794 (define (read-gexp chr port)
1795 "Read a 'gexp' form from PORT."
1796 `(gexp ,(read port)))
1798 ;; Extend the reader
1799 (read-hash-extend #\~ read-gexp)
1800 (read-hash-extend #\$ read-ungexp)
1801 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1803 ;;; gexp.scm ends here