1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix gexp)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix derivations)
23 #:use-module (guix utils)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-9)
26 #:use-module (srfi srfi-9 gnu)
27 #:use-module (srfi srfi-26)
28 #:use-module (ice-9 match)
61 ;;; This module implements "G-expressions", or "gexps". Gexps are like
62 ;;; S-expressions (sexps), with two differences:
64 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
65 ;;; replaced by the corresponding output file name; in addition, the
66 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
67 ;;; the native code of a given package, in case of cross-compilation;
69 ;;; 2. Gexps embed information about the derivations they refer to.
71 ;;; Gexps make it easy to write to files Scheme code that refers to store
72 ;;; items, or to write Scheme code to build derivations.
77 (define-record-type <gexp>
78 (make-gexp references natives proc)
80 (references gexp-references) ; ((DRV-OR-PKG OUTPUT) ...)
81 (natives gexp-native-references) ; ((DRV-OR-PKG OUTPUT) ...)
82 (proc gexp-proc)) ; procedure
84 (define (write-gexp gexp port)
86 (display "#<gexp " port)
88 ;; Try to write the underlying sexp. Now, this trick doesn't work when
89 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
90 ;; tries to use 'append' on that, which fails with wrong-type-arg.
92 (write (apply (gexp-proc gexp)
93 (append (gexp-references gexp)
94 (gexp-native-references gexp)))
97 (number->string (object-address gexp) 16)))
99 (set-record-type-printer! <gexp> write-gexp)
106 ;; Compiler for a type of objects that may be introduced in a gexp.
107 (define-record-type <gexp-compiler>
108 (gexp-compiler predicate lower)
110 (predicate gexp-compiler-predicate)
111 (lower gexp-compiler-lower))
113 (define %gexp-compilers
114 ;; List of <gexp-compiler>.
117 (define (register-compiler! compiler)
118 "Register COMPILER as a gexp compiler."
119 (set! %gexp-compilers (cons compiler %gexp-compilers)))
121 (define (lookup-compiler object)
122 "Search a compiler for OBJECT. Upon success, return the three argument
123 procedure to lower it; otherwise return #f."
125 (($ <gexp-compiler> predicate lower)
126 (and (predicate object) lower)))
129 (define-syntax-rule (define-gexp-compiler (name (param predicate)
132 "Define NAME as a compiler for objects matching PREDICATE encountered in
133 gexps. BODY must return a derivation for PARAM, an object that matches
134 PREDICATE, for SYSTEM and TARGET (the latter of which is #f except when
138 (gexp-compiler predicate
139 (lambda (param system target)
141 (register-compiler! name)))
143 (define-gexp-compiler (derivation-compiler (drv derivation?) system target)
144 ;; Derivations are the lowest-level representation, so this is the identity
146 (with-monad %store-monad
151 ;;; File declarations.
154 (define-record-type <local-file>
155 (%local-file file name recursive?)
157 (file local-file-file) ;string
158 (name local-file-name) ;string
159 (recursive? local-file-recursive?)) ;Boolean
161 (define* (local-file file #:optional (name (basename file))
163 "Return an object representing local file FILE to add to the store; this
164 object can be used in a gexp. FILE will be added to the store under NAME--by
165 default the base name of FILE.
167 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
168 designates a flat file and RECURSIVE? is true, its contents are added, and its
169 permission bits are kept.
171 This is the declarative counterpart of the 'interned-file' monadic procedure."
172 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing to
173 ;; do that, when RECURSIVE? is #t, we could end up creating a dangling
174 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would just
175 ;; throw an error, both of which are inconvenient.
176 (%local-file (canonicalize-path file) name recursive?))
178 (define-gexp-compiler (local-file-compiler (file local-file?) system target)
179 ;; "Compile" FILE by adding it to the store.
181 (($ <local-file> file name recursive?)
182 (interned-file file name #:recursive? recursive?))))
184 (define-record-type <plain-file>
185 (%plain-file name content references)
187 (name plain-file-name) ;string
188 (content plain-file-content) ;string
189 (references plain-file-references)) ;list (currently unused)
191 (define (plain-file name content)
192 "Return an object representing a text file called NAME with the given
193 CONTENT (a string) to be added to the store.
195 This is the declarative counterpart of 'text-file'."
196 ;; XXX: For now just ignore 'references' because it's not clear how to use
197 ;; them in a declarative context.
198 (%plain-file name content '()))
200 (define-gexp-compiler (plain-file-compiler (file plain-file?) system target)
201 ;; "Compile" FILE by adding it to the store.
203 (($ <plain-file> name content references)
204 (text-file name content references))))
208 ;;; Inputs & outputs.
211 ;; The input of a gexp.
212 (define-record-type <gexp-input>
213 (%gexp-input thing output native?)
215 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
216 (output gexp-input-output) ;string
217 (native? gexp-input-native?)) ;Boolean
219 (define (write-gexp-input input port)
221 (($ <gexp-input> thing output #f)
222 (format port "#<gexp-input ~s:~a>" thing output))
223 (($ <gexp-input> thing output #t)
224 (format port "#<gexp-input native ~s:~a>" thing output))))
226 (set-record-type-printer! <gexp-input> write-gexp-input)
228 (define* (gexp-input thing ;convenience procedure
229 #:optional (output "out")
231 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
232 whether this should be considered a \"native\" input or not."
233 (%gexp-input thing output native?))
235 ;; Reference to one of the derivation's outputs, for gexps used in
237 (define-record-type <gexp-output>
240 (name gexp-output-name))
242 (define (write-gexp-output output port)
244 (($ <gexp-output> name)
245 (format port "#<gexp-output ~a>" name))))
247 (set-record-type-printer! <gexp-output> write-gexp-output)
249 (define raw-derivation
250 (store-lift derivation))
252 (define* (lower-inputs inputs
254 "Turn any package from INPUTS into a derivation for SYSTEM; return the
255 corresponding input list as a monadic value. When TARGET is true, use it as
256 the cross-compilation target triplet."
257 (with-monad %store-monad
258 (sequence %store-monad
260 (((? struct? thing) sub-drv ...)
261 (mlet* %store-monad ((lower -> (lookup-compiler thing))
262 (drv (lower thing system target)))
263 (return `(,drv ,@sub-drv))))
268 (define* (lower-reference-graphs graphs #:key system target)
269 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
270 #:reference-graphs argument, lower it such that each INPUT is replaced by the
271 corresponding derivation."
273 (((file-names . inputs) ...)
274 (mlet %store-monad ((inputs (lower-inputs inputs
277 (return (map cons file-names inputs))))))
279 (define* (lower-references lst #:key system target)
280 "Based on LST, a list of output names and packages, return a list of output
281 names and file names suitable for the #:allowed-references argument to
283 ;; XXX: Currently outputs other than "out" are not supported, and things
284 ;; other than packages aren't either.
285 (with-monad %store-monad
290 (($ <gexp-input> thing output native?)
291 (mlet* %store-monad ((lower -> (lookup-compiler thing))
292 (drv (lower thing system
293 (if native? #f target))))
294 (return (derivation->output-path drv output))))
296 (mlet* %store-monad ((lower -> (lookup-compiler thing))
297 (drv (lower thing system target)))
298 (return (derivation->output-path drv))))))
300 (sequence %store-monad (map lower lst))))
302 (define default-guile-derivation
303 ;; Here we break the abstraction by talking to the higher-level layer.
304 ;; Thus, do the resolution lazily to hide the circular dependency.
306 (let ((iface (resolve-interface '(guix packages))))
307 (module-ref iface 'default-guile-derivation)))))
309 ((force proc) system))))
311 (define* (gexp->derivation name exp
313 system (target 'current)
314 hash hash-algo recursive?
317 (module-path %load-path)
318 (guile-for-build (%guile-for-build))
323 local-build? (substitutable? #t))
324 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
325 derivation) on SYSTEM. When TARGET is true, it is used as the
326 cross-compilation target triplet for packages referred to by EXP.
328 Make MODULES available in the evaluation context of EXP; MODULES is a list of
329 names of Guile modules searched in MODULE-PATH to be copied in the store,
330 compiled, and made available in the load path during the execution of
331 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
333 GRAFT? determines whether packages referred to by EXP should be grafted when
336 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
340 (FILE-NAME PACKAGE OUTPUT)
341 (FILE-NAME DERIVATION)
342 (FILE-NAME DERIVATION OUTPUT)
343 (FILE-NAME STORE-ITEM)
345 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
346 an input of the build process of EXP. In the build environment, each
347 FILE-NAME contains the reference graph of the corresponding item, in a simple
350 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
351 In the latter case, the list denotes store items that the result is allowed to
352 refer to. Any reference to another store item will lead to a build error.
354 The other arguments are as for 'derivation'."
355 (define %modules modules)
356 (define outputs (gexp-outputs exp))
358 (define (graphs-file-names graphs)
359 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
361 ;; TODO: Remove 'derivation?' special cases.
362 ((file-name (? derivation? drv))
363 (cons file-name (derivation->output-path drv)))
364 ((file-name (? derivation? drv) sub-drv)
365 (cons file-name (derivation->output-path drv sub-drv)))
367 (cons file-name thing)))
370 (mlet* %store-monad (;; The following binding forces '%current-system' and
371 ;; '%current-target-system' to be looked up at >>=
373 (graft? (set-grafting graft?))
375 (system -> (or system (%current-system)))
376 (target -> (if (eq? target 'current)
377 (%current-target-system)
379 (normals (lower-inputs (gexp-inputs exp)
382 (natives (lower-inputs (gexp-native-inputs exp)
385 (inputs -> (append normals natives))
386 (sexp (gexp->sexp exp
389 (builder (text-file (string-append name "-builder")
390 (object->string sexp)))
391 (modules (if (pair? %modules)
392 (imported-modules %modules
394 #:module-path module-path
395 #:guile guile-for-build)
397 (compiled (if (pair? %modules)
398 (compiled-modules %modules
400 #:module-path module-path
401 #:guile guile-for-build)
403 (graphs (if references-graphs
404 (lower-reference-graphs references-graphs
408 (allowed (if allowed-references
409 (lower-references allowed-references
413 (guile (if guile-for-build
414 (return guile-for-build)
415 (default-guile-derivation system))))
417 (set-grafting graft?) ;restore the initial setting
419 (string-append (derivation->output-path guile)
421 `("--no-auto-compile"
422 ,@(if (pair? %modules)
423 `("-L" ,(derivation->output-path modules)
424 "-C" ,(derivation->output-path compiled))
433 `((,modules) (,compiled) ,@inputs)
436 (((_ . inputs) ...) inputs)
438 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
439 #:references-graphs (and=> graphs graphs-file-names)
440 #:allowed-references allowed
441 #:leaked-env-vars leaked-env-vars
442 #:local-build? local-build?
443 #:substitutable? substitutable?))))
445 (define* (gexp-inputs exp #:key native?)
446 "Return the input list for EXP. When NATIVE? is true, return only native
447 references; otherwise, return only non-native references."
448 (define (add-reference-inputs ref result)
450 (($ <gexp-input> (? gexp? exp) _ #t)
452 (append (gexp-inputs exp)
453 (gexp-inputs exp #:native? #t)
456 (($ <gexp-input> (? gexp? exp) _ #f)
458 (append (gexp-inputs exp #:native? #t)
460 (append (gexp-inputs exp)
462 (($ <gexp-input> (? string? str))
463 (if (direct-store-path? str)
464 (cons `(,str) result)
466 (($ <gexp-input> (? struct? thing) output)
467 (if (lookup-compiler thing)
468 ;; THING is a derivation, or a package, or an origin, etc.
469 (cons `(,thing ,output) result)
471 (($ <gexp-input> (lst ...) output n?)
472 (fold-right add-reference-inputs result
473 ;; XXX: For now, automatically convert LST to a list of
476 ((? gexp-input? x) x)
477 (x (%gexp-input x "out" (or n? native?))))
480 ;; Ignore references to other kinds of objects.
483 (fold-right add-reference-inputs
486 (gexp-native-references exp)
487 (gexp-references exp))))
489 (define gexp-native-inputs
490 (cut gexp-inputs <> #:native? #t))
492 (define (gexp-outputs exp)
493 "Return the outputs referred to by EXP as a list of strings."
494 (define (add-reference-output ref result)
496 (($ <gexp-output> name)
498 (($ <gexp-input> (? gexp? exp))
499 (append (gexp-outputs exp) result))
500 (($ <gexp-input> (lst ...) output native?)
501 ;; XXX: Automatically convert LST.
502 (add-reference-output (map (match-lambda
503 ((? gexp-input? x) x)
504 (x (%gexp-input x "out" native?)))
508 (fold-right add-reference-output result lst))
513 (add-reference-output (gexp-references exp) '())))
515 (define* (gexp->sexp exp #:key
516 (system (%current-system))
517 (target (%current-target-system)))
518 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
519 and in the current monad setting (system type, etc.)"
520 (define* (reference->sexp ref #:optional native?)
521 (with-monad %store-monad
523 (($ <gexp-output> output)
524 ;; Output file names are not known in advance but the daemon defines
525 ;; an environment variable for each of them at build time, so use
527 (return `((@ (guile) getenv) ,output)))
528 (($ <gexp-input> (? gexp? exp) output n?)
531 #:target (if (or n? native?) #f target)))
532 (($ <gexp-input> (refs ...) output n?)
533 (sequence %store-monad
535 ;; XXX: Automatically convert REF to an gexp-input.
537 (if (gexp-input? ref)
539 (%gexp-input ref "out" n?))
542 (($ <gexp-input> (? struct? thing) output n?)
543 (let ((lower (lookup-compiler thing))
544 (target (if (or n? native?) #f target)))
545 (mlet %store-monad ((obj (lower thing system target)))
546 ;; OBJ must be either a derivation or a store file name.
549 (derivation->output-path drv output))
558 ((args (sequence %store-monad
559 (append (map reference->sexp (gexp-references exp))
560 (map (cut reference->sexp <> #t)
561 (gexp-native-references exp))))))
562 (return (apply (gexp-proc exp) args))))
564 (define (syntax-location-string s)
565 "Return a string representing the source code location of S."
566 (let ((props (syntax-source s)))
568 (let ((file (assoc-ref props 'filename))
569 (line (and=> (assoc-ref props 'line) 1+))
570 (column (assoc-ref props 'column)))
572 (simple-format #f "~a:~a:~a"
574 (simple-format #f "~a:~a" line column)))
575 "<unknown location>")))
579 (define (collect-escapes exp)
580 ;; Return all the 'ungexp' present in EXP.
583 (syntax-case exp (ungexp
586 ungexp-native-splicing)
591 ((ungexp-splicing _ ...)
593 ((ungexp-native _ ...)
595 ((ungexp-native-splicing _ ...)
598 (let ((result (loop #'exp0 result)))
599 (fold loop result #'(exp ...))))
603 (define (collect-native-escapes exp)
604 ;; Return all the 'ungexp-native' forms present in EXP.
607 (syntax-case exp (ungexp
610 ungexp-native-splicing)
615 ((ungexp-native-splicing _ ...)
619 ((ungexp-splicing _ ...)
622 (let ((result (loop #'exp0 result)))
623 (fold loop result #'(exp ...))))
627 (define (escape->ref exp)
628 ;; Turn 'ungexp' form EXP into a "reference".
629 (syntax-case exp (ungexp ungexp-splicing
630 ungexp-native ungexp-native-splicing
633 #'(gexp-output "out"))
634 ((ungexp output name)
635 #'(gexp-output name))
637 #'(%gexp-input thing "out" #f))
638 ((ungexp drv-or-pkg out)
639 #'(%gexp-input drv-or-pkg out #f))
640 ((ungexp-splicing lst)
641 #'(%gexp-input lst "out" #f))
642 ((ungexp-native thing)
643 #'(%gexp-input thing "out" #t))
644 ((ungexp-native drv-or-pkg out)
645 #'(%gexp-input drv-or-pkg out #t))
646 ((ungexp-native-splicing lst)
647 #'(%gexp-input lst "out" #t))))
649 (define (substitute-ungexp exp substs)
650 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
651 ;; the corresponding form in SUBSTS.
652 (match (assoc exp substs)
656 #'(syntax-error "error: no 'ungexp' substitution"
659 (define (substitute-ungexp-splicing exp substs)
662 (match (assoc #'exp substs)
664 (with-syntax ((id id))
666 #,(substitute-references #'(rest ...) substs))))
668 #'(syntax-error "error: no 'ungexp-splicing' substitution"
671 (define (substitute-references exp substs)
672 ;; Return a variant of EXP where all the cars of SUBSTS have been
673 ;; replaced by the corresponding cdr.
674 (syntax-case exp (ungexp ungexp-native
675 ungexp-splicing ungexp-native-splicing)
677 (substitute-ungexp exp substs))
678 ((ungexp-native _ ...)
679 (substitute-ungexp exp substs))
680 (((ungexp-splicing _ ...) rest ...)
681 (substitute-ungexp-splicing exp substs))
682 (((ungexp-native-splicing _ ...) rest ...)
683 (substitute-ungexp-splicing exp substs))
685 #`(cons #,(substitute-references #'exp0 substs)
686 #,(substitute-references #'(exp ...) substs)))
689 (syntax-case s (ungexp output)
691 (let* ((normals (delete-duplicates (collect-escapes #'exp)))
692 (natives (delete-duplicates (collect-native-escapes #'exp)))
693 (escapes (append normals natives))
694 (formals (generate-temporaries escapes))
695 (sexp (substitute-references #'exp (zip escapes formals)))
696 (refs (map escape->ref normals))
697 (nrefs (map escape->ref natives)))
698 #`(make-gexp (list #,@refs) (list #,@nrefs)
707 (define %mkdir-p-definition
708 ;; The code for 'mkdir-p' is copied from (guix build utils). We use it in
709 ;; derivations that cannot use the #:modules argument of 'gexp->derivation'
710 ;; precisely because they implement that functionality.
712 (define (mkdir-p dir)
714 (string-prefix? "/" dir))
717 (char-set-complement (char-set #\/)))
719 (let loop ((components (string-tokenize dir not-slash))
720 (root (if absolute? "" ".")))
723 (let ((path (string-append root "/" head)))
729 (if (= EEXIST (system-error-errno args))
731 (apply throw args))))))
734 (define* (imported-files files
735 #:key (name "file-import")
736 (system (%current-system))
737 (guile (%guile-for-build)))
738 "Return a derivation that imports FILES into STORE. FILES must be a list
739 of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
740 system, imported, and appears under FINAL-PATH in the resulting store path."
743 ((final-path . file-name)
744 (mlet %store-monad ((file (interned-file file-name
745 (basename final-path))))
746 (return (list final-path file))))))
748 (mlet %store-monad ((files (sequence %store-monad
749 (map file-pair files))))
753 (use-modules (ice-9 match))
755 (ungexp %mkdir-p-definition)
757 (mkdir (ungexp output)) (chdir (ungexp output))
758 (for-each (match-lambda
759 ((final-path store-path)
760 (mkdir-p (dirname final-path))
761 (symlink store-path final-path)))
764 ;; TODO: Pass FILES as an environment variable so that BUILD remains
765 ;; exactly the same regardless of FILES: less disk space, and fewer
766 ;; 'add-to-store' RPCs.
767 (gexp->derivation name build
769 #:guile-for-build guile
773 ;; A memoizing version of 'search-path' so 'imported-modules' does not end
774 ;; up looking for the same files over and over again.
775 (memoize search-path))
777 (define* (imported-modules modules
778 #:key (name "module-import")
779 (system (%current-system))
780 (guile (%guile-for-build))
781 (module-path %load-path))
782 "Return a derivation that contains the source files of MODULES, a list of
783 module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
785 ;; TODO: Determine the closure of MODULES, build the `.go' files,
786 ;; canonicalize the source files through read/write, etc.
787 (let ((files (map (lambda (m)
788 (let ((f (string-append
789 (string-join (map symbol->string m) "/")
791 (cons f (search-path* module-path f))))
793 (imported-files files #:name name #:system system
796 (define* (compiled-modules modules
797 #:key (name "module-import-compiled")
798 (system (%current-system))
799 (guile (%guile-for-build))
800 (module-path %load-path))
801 "Return a derivation that builds a tree containing the `.go' files
802 corresponding to MODULES. All the MODULES are built in a context where
803 they can refer to each other."
804 (mlet %store-monad ((modules (imported-modules modules
812 (use-modules (ice-9 ftw)
815 (system base compile))
817 (ungexp %mkdir-p-definition)
819 (define (regular? file)
820 (not (member file '("." ".."))))
822 (define (process-directory directory output)
823 (let ((entries (map (cut string-append directory "/" <>)
824 (scandir directory regular?))))
825 (for-each (lambda (entry)
826 (if (file-is-directory? entry)
827 (let ((output (string-append output "/"
830 (process-directory entry output))
831 (let* ((base (string-drop-right
834 (output (string-append output "/" base
839 %auto-compilation-options))))
842 (set! %load-path (cons (ungexp modules) %load-path))
843 (mkdir (ungexp output))
844 (chdir (ungexp modules))
845 (process-directory "." (ungexp output)))))
847 ;; TODO: Pass MODULES as an environment variable.
848 (gexp->derivation name build
850 #:guile-for-build guile
855 ;;; Convenience procedures.
858 (define (default-guile)
859 ;; Lazily resolve 'guile-final'. This module must not refer to (gnu …)
860 ;; modules directly, to avoid circular dependencies, hence this hack.
861 (module-ref (resolve-interface '(gnu packages commencement))
864 (define* (gexp->script name exp
865 #:key (modules '()) (guile (default-guile)))
866 "Return an executable script NAME that runs EXP using GUILE with MODULES in
868 (mlet %store-monad ((modules (imported-modules modules))
869 (compiled (compiled-modules modules)))
870 (gexp->derivation name
872 (call-with-output-file (ungexp output)
874 ;; Note: that makes a long shebang. When the store
875 ;; is /gnu/store, that fits within the 128-byte
876 ;; limit imposed by Linux, but that may go beyond
877 ;; when running tests.
879 "#!~a/bin/guile --no-auto-compile~%!#~%"
882 ;; Write the 'eval-when' form so that it can be
885 '(eval-when (expand load eval)
887 (cons (ungexp modules) %load-path))
888 (set! %load-compiled-path
889 (cons (ungexp compiled)
890 %load-compiled-path)))
892 (write '(ungexp exp) port)
893 (chmod port #o555)))))))
895 (define (gexp->file name exp)
896 "Return a derivation that builds a file NAME containing EXP."
897 (gexp->derivation name
899 (call-with-output-file (ungexp output)
901 (write '(ungexp exp) port))))
904 (define* (text-file* name #:rest text)
905 "Return as a monadic value a derivation that builds a text file containing
906 all of TEXT. TEXT may list, in addition to strings, objects of any type that
907 can be used in a gexp: packages, derivations, local file objects, etc. The
908 resulting store file holds references to all these."
910 (gexp (call-with-output-file (ungexp output "out")
912 (display (string-append (ungexp-splicing text)) port)))))
914 (gexp->derivation name builder))
921 (eval-when (expand load eval)
922 (define* (read-ungexp chr port #:optional native?)
923 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
924 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
925 (define unquote-symbol
926 (match (peek-char port)
930 'ungexp-native-splicing
939 (let ((str (symbol->string symbol)))
940 (match (string-index-right str #\:)
942 `(,unquote-symbol ,symbol))
944 (let ((name (string->symbol (substring str 0 colon)))
945 (output (substring str (+ colon 1))))
946 `(,unquote-symbol ,name ,output))))))
948 `(,unquote-symbol ,x))))
950 (define (read-gexp chr port)
951 "Read a 'gexp' form from PORT."
952 `(gexp ,(read port)))
955 (read-hash-extend #\~ read-gexp)
956 (read-hash-extend #\$ read-ungexp)
957 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
959 ;;; gexp.scm ends here