1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (guix gexp)
23 #:use-module (guix store)
24 #:use-module (guix monads)
25 #:use-module (guix derivations)
26 #:use-module (guix grafts)
27 #:use-module (guix utils)
28 #:use-module (rnrs bytevectors)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
31 #:use-module (srfi srfi-9 gnu)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
50 local-file-absolute-file-name
70 program-file-module-path
97 lowered-gexp-load-path
98 lowered-gexp-load-compiled-path
122 gexp-error-invalid-input))
126 ;;; This module implements "G-expressions", or "gexps". Gexps are like
127 ;;; S-expressions (sexps), with two differences:
129 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
130 ;;; replaced by the corresponding output file name; in addition, the
131 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
132 ;;; the native code of a given package, in case of cross-compilation;
134 ;;; 2. Gexps embed information about the derivations they refer to.
136 ;;; Gexps make it easy to write to files Scheme code that refers to store
137 ;;; items, or to write Scheme code to build derivations.
142 (define-record-type <gexp>
143 (make-gexp references modules extensions proc)
145 (references gexp-references) ;list of <gexp-input>
146 (modules gexp-self-modules) ;list of module names
147 (extensions gexp-self-extensions) ;list of lowerable things
148 (proc gexp-proc)) ;procedure
150 (define (write-gexp gexp port)
151 "Write GEXP on PORT."
152 (display "#<gexp " port)
154 ;; Try to write the underlying sexp. Now, this trick doesn't work when
155 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
156 ;; tries to use 'append' on that, which fails with wrong-type-arg.
158 (write (apply (gexp-proc gexp)
159 (gexp-references gexp))
162 (number->string (object-address gexp) 16)))
164 (set-record-type-printer! <gexp> write-gexp)
171 ;; Compiler for a type of objects that may be introduced in a gexp.
172 (define-record-type <gexp-compiler>
173 (gexp-compiler type lower expand)
175 (type gexp-compiler-type) ;record type descriptor
176 (lower gexp-compiler-lower)
177 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
179 (define-condition-type &gexp-error &error
182 (define-condition-type &gexp-input-error &gexp-error
184 (input gexp-error-invalid-input))
187 (define %gexp-compilers
188 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
189 (make-hash-table 20))
191 (define (default-expander thing obj output)
192 "This is the default expander for \"things\" that appear in gexps. It
193 returns its output file name of OBJ's OUTPUT."
196 (derivation->output-path drv output))
200 (define (register-compiler! compiler)
201 "Register COMPILER as a gexp compiler."
202 (hashq-set! %gexp-compilers
203 (gexp-compiler-type compiler) compiler))
205 (define (lookup-compiler object)
206 "Search for a compiler for OBJECT. Upon success, return the three argument
207 procedure to lower it; otherwise return #f."
208 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
209 gexp-compiler-lower))
211 (define (file-like? object)
212 "Return #t if OBJECT leads to a file in the store once unquoted in a
213 G-expression; otherwise return #f."
214 (and (struct? object) (->bool (lookup-compiler object))))
216 (define (lookup-expander object)
217 "Search for an expander for OBJECT. Upon success, return the three argument
218 procedure to expand it; otherwise return #f."
219 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
220 gexp-compiler-expand))
222 (define* (lower-object obj
223 #:optional (system (%current-system))
224 #:key (target 'current))
225 "Return as a value in %STORE-MONAD the derivation or store item
226 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
227 OBJ must be an object that has an associated gexp compiler, such as a
229 (match (lookup-compiler obj)
231 (raise (condition (&gexp-input-error (input obj)))))
233 ;; Cache in STORE the result of lowering OBJ.
234 (mlet %store-monad ((target (if (eq? target 'current)
235 (current-target-system)
237 (graft? (grafting?)))
238 (mcached (let ((lower (lookup-compiler obj)))
239 (lower obj system target))
241 system target graft?)))))
243 (define-syntax define-gexp-compiler
244 (syntax-rules (=> compiler expander)
245 "Define NAME as a compiler for objects matching PREDICATE encountered in
248 In the simplest form of the macro, BODY must return a derivation for PARAM, an
249 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
250 #f except when cross-compiling.)
252 The more elaborate form allows you to specify an expander:
254 (define-gexp-compiler something something?
255 compiler => (lambda (param system target) ...)
256 expander => (lambda (param drv output) ...))
258 The expander specifies how an object is converted to its sexp representation."
259 ((_ (name (param record-type) system target) body ...)
260 (define-gexp-compiler name record-type
261 compiler => (lambda (param system target) body ...)
262 expander => default-expander))
268 (gexp-compiler record-type compile expand))
269 (register-compiler! name)))))
271 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
272 ;; Derivations are the lowest-level representation, so this is the identity
274 (with-monad %store-monad
277 ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
278 ;; words, this gives the raw ".drv" file instead of its build result.
279 (define-record-type <raw-derivation-file>
280 (raw-derivation-file obj)
282 (obj raw-derivation-file-object)) ;lowerable object
284 (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
285 compiler => (lambda (obj system target)
286 (mlet %store-monad ((obj (lower-object
287 (raw-derivation-file-object obj)
288 system #:target target)))
289 ;; Returning the .drv file name instead of the <derivation>
290 ;; record ensures that 'lower-gexp' will classify it as a
291 ;; "source" and not as an "input".
292 (return (if (derivation? obj)
293 (derivation-file-name obj)
295 expander => (lambda (obj lowered output)
296 (if (derivation? lowered)
297 (derivation-file-name lowered)
302 ;;; File declarations.
305 ;; A local file name. FILE is the file name the user entered, which can be a
306 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
307 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
308 ;; repeated 'stat' calls.
309 (define-record-type <local-file>
310 (%%local-file file absolute name recursive? select?)
312 (file local-file-file) ;string
313 (absolute %local-file-absolute-file-name) ;promise string
314 (name local-file-name) ;string
315 (recursive? local-file-recursive?) ;Boolean
316 (select? local-file-select?)) ;string stat -> Boolean
318 (define (true file stat) #t)
320 (define* (%local-file file promise #:optional (name (basename file))
321 #:key recursive? (select? true))
322 ;; This intermediate procedure is part of our ABI, but the underlying
323 ;; %%LOCAL-FILE is not.
324 (%%local-file file promise name recursive? select?))
326 (define (absolute-file-name file directory)
327 "Return the canonical absolute file name for FILE, which lives in the
328 vicinity of DIRECTORY."
330 (cond ((string-prefix? "/" file) file)
331 ((not directory) file)
332 ((string-prefix? "/" directory)
333 (string-append directory "/" file))
336 (define-syntax local-file
338 "Return an object representing local file FILE to add to the store; this
339 object can be used in a gexp. If FILE is a relative file name, it is looked
340 up relative to the source file where this form appears. FILE will be added to
341 the store under NAME--by default the base name of FILE.
343 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
344 designates a flat file and RECURSIVE? is true, its contents are added, and its
345 permission bits are kept.
347 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
348 where FILE is the entry's absolute file name and STAT is the result of
349 'lstat'; exclude entries for which SELECT? does not return true.
351 This is the declarative counterpart of the 'interned-file' monadic procedure.
352 It is implemented as a macro to capture the current source directory where it
356 (string? (syntax->datum #'file))
357 ;; FILE is a literal, so resolve it relative to the source directory.
359 (delay (absolute-file-name file (current-source-directory)))
362 ;; Resolve FILE relative to the current directory.
364 (delay (absolute-file-name file (getcwd)))
367 #'(syntax-error "missing file name"))
370 ;; XXX: We could return #'(lambda (file . rest) ...). However,
371 ;; (syntax-source #'id) is #f so (current-source-directory) would not
372 ;; work. Thus, simply forbid this form.
374 "'local-file' is a macro and cannot be used like this")))))
376 (define (local-file-absolute-file-name file)
377 "Return the absolute file name for FILE, a <local-file> instance. A
378 'system-error' exception is raised if FILE could not be found."
379 (force (%local-file-absolute-file-name file)))
381 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
382 ;; "Compile" FILE by adding it to the store.
384 (($ <local-file> file (= force absolute) name recursive? select?)
385 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
386 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
387 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
388 ;; just throw an error, both of which are inconvenient.
389 (interned-file absolute name
390 #:recursive? recursive? #:select? select?))))
392 (define-record-type <plain-file>
393 (%plain-file name content references)
395 (name plain-file-name) ;string
396 (content plain-file-content) ;string or bytevector
397 (references plain-file-references)) ;list (currently unused)
399 (define (plain-file name content)
400 "Return an object representing a text file called NAME with the given
401 CONTENT (a string) to be added to the store.
403 This is the declarative counterpart of 'text-file'."
404 ;; XXX: For now just ignore 'references' because it's not clear how to use
405 ;; them in a declarative context.
406 (%plain-file name content '()))
408 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
409 ;; "Compile" FILE by adding it to the store.
411 (($ <plain-file> name (and (? string?) content) references)
412 (text-file name content references))
413 (($ <plain-file> name (and (? bytevector?) content) references)
414 (binary-file name content references))))
416 (define-record-type <computed-file>
417 (%computed-file name gexp guile options)
419 (name computed-file-name) ;string
420 (gexp computed-file-gexp) ;gexp
421 (guile computed-file-guile) ;<package>
422 (options computed-file-options)) ;list of arguments
424 (define* (computed-file name gexp
425 #:key guile (options '(#:local-build? #t)))
426 "Return an object representing the store item NAME, a file or directory
427 computed by GEXP. OPTIONS is a list of additional arguments to pass
428 to 'gexp->derivation'.
430 This is the declarative counterpart of 'gexp->derivation'."
431 (%computed-file name gexp guile options))
433 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
435 ;; Compile FILE by returning a derivation whose build expression is its
438 (($ <computed-file> name gexp guile options)
440 (mlet %store-monad ((guile (lower-object guile system
442 (apply gexp->derivation name gexp #:guile-for-build guile
443 #:system system #:target target options))
444 (apply gexp->derivation name gexp
445 #:system system #:target target options)))))
447 (define-record-type <program-file>
448 (%program-file name gexp guile path)
450 (name program-file-name) ;string
451 (gexp program-file-gexp) ;gexp
452 (guile program-file-guile) ;package
453 (path program-file-module-path)) ;list of strings
455 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
456 "Return an object representing the executable store item NAME that runs
457 GEXP. GUILE is the Guile package used to execute that script. Imported
458 modules of GEXP are looked up in MODULE-PATH.
460 This is the declarative counterpart of 'gexp->script'."
461 (%program-file name gexp guile module-path))
463 (define-gexp-compiler (program-file-compiler (file <program-file>)
465 ;; Compile FILE by returning a derivation that builds the script.
467 (($ <program-file> name gexp guile module-path)
468 (gexp->script name gexp
469 #:module-path module-path
470 #:guile (or guile (default-guile))
474 (define-record-type <scheme-file>
475 (%scheme-file name gexp splice? load-path?)
477 (name scheme-file-name) ;string
478 (gexp scheme-file-gexp) ;gexp
479 (splice? scheme-file-splice?) ;Boolean
480 (load-path? scheme-file-set-load-path?)) ;Boolean
482 (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
483 "Return an object representing the Scheme file NAME that contains GEXP.
485 This is the declarative counterpart of 'gexp->file'."
486 (%scheme-file name gexp splice? set-load-path?))
488 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
490 ;; Compile FILE by returning a derivation that builds the file.
492 (($ <scheme-file> name gexp splice? set-load-path?)
493 (gexp->file name gexp
494 #:set-load-path? set-load-path?
499 ;; Appending SUFFIX to BASE's output file name.
500 (define-record-type <file-append>
501 (%file-append base suffix)
503 (base file-append-base) ;<package> | <derivation> | ...
504 (suffix file-append-suffix)) ;list of strings
506 (define (write-file-append file port)
508 (($ <file-append> base suffix)
509 (format port "#<file-append ~s ~s>" base
510 (string-join suffix)))))
512 (set-record-type-printer! <file-append> write-file-append)
514 (define (file-append base . suffix)
515 "Return a <file-append> object that expands to the concatenation of BASE and
517 (%file-append base suffix))
519 (define-gexp-compiler file-append-compiler <file-append>
520 compiler => (lambda (obj system target)
522 (($ <file-append> base _)
523 (lower-object base system #:target target))))
524 expander => (lambda (obj lowered output)
526 (($ <file-append> base suffix)
527 (let* ((expand (lookup-expander base))
528 (base (expand base lowered output)))
529 (string-append base (string-concatenate suffix)))))))
531 ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
533 (define-record-type <parameterized>
534 (parameterized bindings thunk)
536 (bindings parameterized-bindings) ;list of parameter/value pairs
537 (thunk parameterized-thunk)) ;thunk
539 (define-syntax-rule (with-parameters ((param value) ...) body ...)
540 "Bind each PARAM to the corresponding VALUE for the extent during which BODY
541 is lowered. Consider this example:
543 (with-parameters ((%current-system \"x86_64-linux\"))
546 It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
547 x86_64-linux when COREUTILS is lowered."
548 (parameterized (list (list param (lambda () value)) ...)
552 (define-gexp-compiler compile-parameterized <parameterized>
554 (lambda (parameterized system target)
555 (match (parameterized-bindings parameterized)
556 (((parameters values) ...)
557 (let ((fluids (map parameter-fluid parameters))
558 (thunk (parameterized-thunk parameterized)))
559 ;; Install the PARAMETERS for the dynamic extent of THUNK.
561 (map (lambda (thunk) (thunk)) values)
563 ;; Special-case '%current-system' and '%current-target-system' to
564 ;; make sure we get the desired effect.
565 (let ((system (if (memq %current-system parameters)
568 (target (if (memq %current-target-system parameters)
569 (%current-target-system)
571 (lower-object (thunk) system #:target target))))))))
573 expander => (lambda (parameterized lowered output)
574 (match (parameterized-bindings parameterized)
575 (((parameters values) ...)
576 (let ((fluids (map parameter-fluid parameters))
577 (thunk (parameterized-thunk parameterized)))
578 ;; Install the PARAMETERS for the dynamic extent of THUNK.
580 (map (lambda (thunk) (thunk)) values)
582 ;; Delegate to the expander of the wrapped object.
583 (let* ((base (thunk))
584 (expand (lookup-expander base)))
585 (expand base lowered output)))))))))
589 ;;; Inputs & outputs.
592 ;; The input of a gexp.
593 (define-record-type <gexp-input>
594 (%gexp-input thing output native?)
596 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
597 (output gexp-input-output) ;string
598 (native? gexp-input-native?)) ;Boolean
600 (define (write-gexp-input input port)
602 (($ <gexp-input> thing output #f)
603 (format port "#<gexp-input ~s:~a>" thing output))
604 (($ <gexp-input> thing output #t)
605 (format port "#<gexp-input native ~s:~a>" thing output))))
607 (set-record-type-printer! <gexp-input> write-gexp-input)
609 (define* (gexp-input thing ;convenience procedure
610 #:optional (output "out")
612 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
613 whether this should be considered a \"native\" input or not."
614 (%gexp-input thing output native?))
616 ;; Reference to one of the derivation's outputs, for gexps used in
618 (define-record-type <gexp-output>
621 (name gexp-output-name))
623 (define (write-gexp-output output port)
625 (($ <gexp-output> name)
626 (format port "#<gexp-output ~a>" name))))
628 (set-record-type-printer! <gexp-output> write-gexp-output)
630 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
631 "Recurse on GEXP and the expressions it refers to, summing the items
632 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
633 second argument to 'delete-duplicates'."
636 (append (self-attribute gexp)
637 (append-map (match-lambda
638 (($ <gexp-input> (? gexp? exp))
639 (gexp-attribute exp self-attribute))
640 (($ <gexp-input> (lst ...))
641 (append-map (lambda (item)
649 (gexp-references gexp)))
651 '())) ;plain Scheme data type
653 (define (gexp-modules gexp)
654 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
655 false, meaning that GEXP is a plain Scheme object, return the empty list."
656 (define (module=? m1 m2)
657 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
658 ;; right-hand side may not be comparable with 'equal?': it's typically a
659 ;; file-like object that embeds a gexp, which in turn embeds closure;
660 ;; those closures may be 'eq?' when running compiled code but are unlikely
661 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
662 ;; avoid this discrepancy.
666 (((name2 ...) '=> _) (equal? name1 name2))
671 (gexp-attribute gexp gexp-self-modules module=?))
673 (define (gexp-extensions gexp)
674 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
675 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
677 (gexp-attribute gexp gexp-self-extensions))
679 (define* (lower-inputs inputs
681 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
682 item (a \"source\"); return the corresponding input list as a monadic value.
683 When TARGET is true, use it as the cross-compilation target triplet."
684 (define (store-item? obj)
685 (and (string? obj) (store-path? obj)))
687 (with-monad %store-monad
688 (mapm/accumulate-builds
690 (((? struct? thing) sub-drv ...)
691 (mlet %store-monad ((obj (lower-object
692 thing system #:target target)))
695 (let ((outputs (if (null? sub-drv)
698 (derivation-input drv outputs)))
699 ((? store-item? item)
701 (((? store-item? item))
705 (define* (lower-reference-graphs graphs #:key system target)
706 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
707 #:reference-graphs argument, lower it such that each INPUT is replaced by the
708 corresponding <derivation-input> or store item."
710 (((file-names . inputs) ...)
711 (mlet %store-monad ((inputs (lower-inputs inputs
714 (return (map cons file-names inputs))))))
716 (define* (lower-references lst #:key system target)
717 "Based on LST, a list of output names and packages, return a list of output
718 names and file names suitable for the #:allowed-references argument to
720 (with-monad %store-monad
725 (($ <gexp-input> thing output native?)
726 (mlet %store-monad ((drv (lower-object thing system
729 (return (derivation->output-path drv output))))
731 (mlet %store-monad ((drv (lower-object thing system
733 (return (derivation->output-path drv))))))
735 (mapm/accumulate-builds lower lst)))
737 (define default-guile-derivation
738 ;; Here we break the abstraction by talking to the higher-level layer.
739 ;; Thus, do the resolution lazily to hide the circular dependency.
741 (let ((iface (resolve-interface '(guix packages))))
742 (module-ref iface 'default-guile-derivation)))))
744 ((force proc) system))))
746 ;; Representation of a gexp instantiated for a given target and system.
747 ;; It's an intermediate representation between <gexp> and <derivation>.
748 (define-record-type <lowered-gexp>
749 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
751 (sexp lowered-gexp-sexp) ;sexp
752 (inputs lowered-gexp-inputs) ;list of <derivation-input>
753 (sources lowered-gexp-sources) ;list of store items
754 (guile lowered-gexp-guile) ;<derivation-input> | #f
755 (load-path lowered-gexp-load-path) ;list of store items
756 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
758 (define* (imported+compiled-modules modules system
759 #:key (extensions '())
760 deprecation-warnings guile
761 (module-path %load-path))
762 "Return a pair where the first element is the imported MODULES and the
763 second element is the derivation to compile them."
765 (mlet %store-monad ((modules (if (pair? modules)
766 (imported-modules modules
768 #:module-path module-path)
770 (compiled (if (pair? modules)
771 (compiled-modules modules
773 #:module-path module-path
774 #:extensions extensions
776 #:deprecation-warnings
777 deprecation-warnings)
779 (return (cons modules compiled)))
781 system extensions guile deprecation-warnings module-path))
783 (define* (lower-gexp exp
785 (module-path %load-path)
786 (system (%current-system))
789 (guile-for-build (%guile-for-build))
790 (effective-version "2.2")
792 deprecation-warnings)
793 "*Note: This API is subject to change; use at your own risk!*
795 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
796 <lowered-gexp> ready to be used.
798 Lowered gexps are an intermediate representation that's useful for
799 applications that deal with gexps outside in a way that is disconnected from
800 derivations--e.g., code evaluated for its side effects."
802 (delete-duplicates (gexp-modules exp)))
804 (define (search-path modules extensions suffix)
805 (append (match modules
807 (list (derivation->output-path drv)))
810 ((? store-path? item)
812 (map (lambda (extension)
813 (string-append (match extension
815 (derivation->output-path drv))
816 ((? store-path? item)
821 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
822 ;; '%current-target-system' to be looked up at >>=
824 (graft? (set-grafting graft?))
826 (system -> (or system (%current-system)))
827 (target -> (if (eq? target 'current)
828 (%current-target-system)
830 (guile (if guile-for-build
831 (return guile-for-build)
832 (default-guile-derivation system)))
833 (normals (lower-inputs (gexp-inputs exp)
836 (natives (lower-inputs (gexp-native-inputs exp)
839 (inputs -> (append normals natives))
840 (sexp (gexp->sexp exp
843 (extensions -> (gexp-extensions exp))
844 (exts (mapm %store-monad
846 (lower-object obj system
849 (modules+compiled (imported+compiled-modules
851 #:extensions extensions
852 #:deprecation-warnings
855 #:module-path module-path))
856 (modules -> (car modules+compiled))
857 (compiled -> (cdr modules+compiled)))
859 (search-path modules exts
860 (string-append "/share/guile/site/" effective-version)))
862 (define load-compiled-path
863 (search-path compiled exts
864 (string-append "/lib/guile/" effective-version
868 (set-grafting graft?) ;restore the initial setting
869 (return (lowered-gexp sexp
870 `(,@(if (derivation? modules)
871 (list (derivation-input modules))
874 (list (derivation-input compiled))
876 ,@(map derivation-input exts)
877 ,@(filter derivation-input? inputs))
878 (filter string? (cons modules inputs))
879 (derivation-input guile '("out"))
881 load-compiled-path)))))
883 (define* (gexp->derivation name exp
885 system (target 'current)
886 hash hash-algo recursive?
889 (module-path %load-path)
890 (guile-for-build (%guile-for-build))
891 (effective-version "2.2")
894 allowed-references disallowed-references
896 local-build? (substitutable? #t)
899 (script-name (string-append name "-builder")))
900 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
901 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
902 TARGET is true, it is used as the cross-compilation target triplet for
903 packages referred to by EXP.
905 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
906 make MODULES available in the evaluation context of EXP; MODULES is a list of
907 names of Guile modules searched in MODULE-PATH to be copied in the store,
908 compiled, and made available in the load path during the execution of
909 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
911 EFFECTIVE-VERSION determines the string to use when adding extensions of
912 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
914 GRAFT? determines whether packages referred to by EXP should be grafted when
917 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
921 (FILE-NAME PACKAGE OUTPUT)
922 (FILE-NAME DERIVATION)
923 (FILE-NAME DERIVATION OUTPUT)
924 (FILE-NAME STORE-ITEM)
926 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
927 an input of the build process of EXP. In the build environment, each
928 FILE-NAME contains the reference graph of the corresponding item, in a simple
931 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
932 In the latter case, the list denotes store items that the result is allowed to
933 refer to. Any reference to another store item will lead to a build error.
934 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
935 referenced by the outputs.
937 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
938 compiling modules. It can be #f, #t, or 'detailed.
940 The other arguments are as for 'derivation'."
941 (define outputs (gexp-outputs exp))
942 (define requested-graft? graft?)
944 (define (graphs-file-names graphs)
945 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
947 ((file-name . (? derivation-input? input))
948 (cons file-name (first (derivation-input-output-paths input))))
949 ((file-name . (? string? item))
950 (cons file-name item)))
953 (define (add-modules exp modules)
956 (make-gexp (gexp-references exp)
957 (append modules (gexp-self-modules exp))
958 (gexp-self-extensions exp)
961 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
962 ;; '%current-target-system' to be looked up at >>=
964 (graft? (set-grafting graft?))
966 (system -> (or system (%current-system)))
967 (target -> (if (eq? target 'current)
968 (%current-target-system)
970 (exp -> (add-modules exp modules))
971 (lowered (lower-gexp exp
972 #:module-path module-path
975 #:graft? requested-graft?
980 #:deprecation-warnings
981 deprecation-warnings))
983 (graphs (if references-graphs
984 (lower-reference-graphs references-graphs
988 (allowed (if allowed-references
989 (lower-references allowed-references
993 (disallowed (if disallowed-references
994 (lower-references disallowed-references
998 (guile -> (lowered-gexp-guile lowered))
999 (builder (text-file script-name
1001 (lowered-gexp-sexp lowered)))))
1002 (mbegin %store-monad
1003 (set-grafting graft?) ;restore the initial setting
1004 (raw-derivation name
1005 (string-append (derivation-input-output-path guile)
1007 `("--no-auto-compile"
1008 ,@(append-map (lambda (directory)
1010 (lowered-gexp-load-path lowered))
1011 ,@(append-map (lambda (directory)
1013 (lowered-gexp-load-compiled-path lowered))
1019 ,@(lowered-gexp-inputs lowered)
1022 (filter derivation-input? inputs))
1024 #:sources `(,builder
1025 ,@(if (and (string? modules)
1026 (store-path? modules))
1029 ,@(lowered-gexp-sources lowered)
1032 (filter string? inputs))
1035 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
1036 #:references-graphs (and=> graphs graphs-file-names)
1037 #:allowed-references allowed
1038 #:disallowed-references disallowed
1039 #:leaked-env-vars leaked-env-vars
1040 #:local-build? local-build?
1041 #:substitutable? substitutable?
1042 #:properties properties))))
1044 (define* (gexp-inputs exp #:key native?)
1045 "Return the input list for EXP. When NATIVE? is true, return only native
1046 references; otherwise, return only non-native references."
1047 ;; TODO: Return <gexp-input> records instead of tuples.
1048 (define (add-reference-inputs ref result)
1050 (($ <gexp-input> (? gexp? exp) _ #t)
1052 (append (gexp-inputs exp)
1053 (gexp-inputs exp #:native? #t)
1056 (($ <gexp-input> (? gexp? exp) _ #f)
1057 (append (gexp-inputs exp #:native? native?)
1059 (($ <gexp-input> (? string? str))
1060 (if (direct-store-path? str)
1061 (cons `(,str) result)
1063 (($ <gexp-input> (? struct? thing) output n?)
1064 (if (and (eqv? n? native?) (lookup-compiler thing))
1065 ;; THING is a derivation, or a package, or an origin, etc.
1066 (cons `(,thing ,output) result)
1068 (($ <gexp-input> (lst ...) output n?)
1069 (fold-right add-reference-inputs result
1070 ;; XXX: For now, automatically convert LST to a list of
1071 ;; gexp-inputs. Inherit N?.
1074 (%gexp-input (gexp-input-thing x)
1075 (gexp-input-output x)
1078 (%gexp-input x "out" n?)))
1081 ;; Ignore references to other kinds of objects.
1084 (fold-right add-reference-inputs
1086 (gexp-references exp)))
1088 (define gexp-native-inputs
1089 (cut gexp-inputs <> #:native? #t))
1091 (define (gexp-outputs exp)
1092 "Return the outputs referred to by EXP as a list of strings."
1093 (define (add-reference-output ref result)
1095 (($ <gexp-output> name)
1097 (($ <gexp-input> (? gexp? exp))
1098 (append (gexp-outputs exp) result))
1099 (($ <gexp-input> (lst ...) output native?)
1100 ;; XXX: Automatically convert LST.
1101 (add-reference-output (map (match-lambda
1102 ((? gexp-input? x) x)
1103 (x (%gexp-input x "out" native?)))
1107 (fold-right add-reference-output result lst))
1112 (add-reference-output (gexp-references exp) '())))
1114 (define* (gexp->sexp exp #:key
1115 (system (%current-system))
1116 (target (%current-target-system)))
1117 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1118 and in the current monad setting (system type, etc.)"
1119 (define (self-quoting? x)
1120 (letrec-syntax ((one-of (syntax-rules ()
1124 (one-of rest ...))))))
1125 (one-of symbol? string? keyword? pair? null? array?
1126 number? boolean? char?)))
1128 (define* (reference->sexp ref #:optional native?)
1129 (with-monad %store-monad
1131 (($ <gexp-output> output)
1132 ;; Output file names are not known in advance but the daemon defines
1133 ;; an environment variable for each of them at build time, so use
1135 (return `((@ (guile) getenv) ,output)))
1136 (($ <gexp-input> (? gexp? exp) output n?)
1139 #:target (if (or n? native?) #f target)))
1140 (($ <gexp-input> (refs ...) output n?)
1143 ;; XXX: Automatically convert REF to an gexp-input.
1145 (if (gexp-input? ref)
1147 (%gexp-input ref "out" n?))
1150 (($ <gexp-input> (? struct? thing) output n?)
1151 (let ((target (if (or n? native?) #f target))
1152 (expand (lookup-expander thing)))
1153 (mlet %store-monad ((obj (lower-object thing system
1155 ;; OBJ must be either a derivation or a store file name.
1156 (return (expand thing obj output)))))
1157 (($ <gexp-input> (? self-quoting? x))
1160 (raise (condition (&gexp-input-error (input x)))))
1165 ((args (mapm %store-monad
1166 reference->sexp (gexp-references exp))))
1167 (return (apply (gexp-proc exp) args))))
1169 (define-syntax-rule (define-syntax-parameter-once name proc)
1170 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1171 ;; does not get redefined. This works around a race condition in a
1172 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1173 (eval-when (load eval expand compile)
1175 (if (module-locally-bound? (current-module) 'name)
1176 (module-ref (current-module) 'name)
1177 (make-syntax-transformer 'name 'syntax-parameter
1180 (define-syntax-parameter-once current-imported-modules
1181 ;; Current list of imported modules.
1182 (identifier-syntax '()))
1184 (define-syntax-rule (with-imported-modules modules body ...)
1185 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1187 (syntax-parameterize ((current-imported-modules
1188 (identifier-syntax modules)))
1191 (define-syntax-parameter-once current-imported-extensions
1192 ;; Current list of extensions.
1193 (identifier-syntax '()))
1195 (define-syntax-rule (with-extensions extensions body ...)
1196 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1197 execution environment."
1198 (syntax-parameterize ((current-imported-extensions
1199 (identifier-syntax extensions)))
1204 (define (collect-escapes exp)
1205 ;; Return all the 'ungexp' present in EXP.
1206 (let loop ((exp exp)
1208 (syntax-case exp (ungexp
1211 ungexp-native-splicing)
1216 ((ungexp-splicing _ ...)
1218 ((ungexp-native _ ...)
1220 ((ungexp-native-splicing _ ...)
1223 (let ((result (loop #'exp0 result)))
1224 (loop #'exp result)))
1228 (define (escape->ref exp)
1229 ;; Turn 'ungexp' form EXP into a "reference".
1230 (syntax-case exp (ungexp ungexp-splicing
1231 ungexp-native ungexp-native-splicing
1234 #'(gexp-output "out"))
1235 ((ungexp output name)
1236 #'(gexp-output name))
1238 #'(%gexp-input thing "out" #f))
1239 ((ungexp drv-or-pkg out)
1240 #'(%gexp-input drv-or-pkg out #f))
1241 ((ungexp-splicing lst)
1242 #'(%gexp-input lst "out" #f))
1243 ((ungexp-native thing)
1244 #'(%gexp-input thing "out" #t))
1245 ((ungexp-native drv-or-pkg out)
1246 #'(%gexp-input drv-or-pkg out #t))
1247 ((ungexp-native-splicing lst)
1248 #'(%gexp-input lst "out" #t))))
1250 (define (substitute-ungexp exp substs)
1251 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1252 ;; the corresponding form in SUBSTS.
1253 (match (assoc exp substs)
1257 (with-syntax ((exp exp))
1258 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1260 (define (substitute-ungexp-splicing exp substs)
1263 (match (assoc #'exp substs)
1265 (with-syntax ((id id))
1267 #,(substitute-references #'(rest ...) substs))))
1269 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1272 (define (substitute-references exp substs)
1273 ;; Return a variant of EXP where all the cars of SUBSTS have been
1274 ;; replaced by the corresponding cdr.
1275 (syntax-case exp (ungexp ungexp-native
1276 ungexp-splicing ungexp-native-splicing)
1278 (substitute-ungexp exp substs))
1279 ((ungexp-native _ ...)
1280 (substitute-ungexp exp substs))
1281 (((ungexp-splicing _ ...) rest ...)
1282 (substitute-ungexp-splicing exp substs))
1283 (((ungexp-native-splicing _ ...) rest ...)
1284 (substitute-ungexp-splicing exp substs))
1286 #`(cons #,(substitute-references #'exp0 substs)
1287 #,(substitute-references #'exp substs)))
1290 (syntax-case s (ungexp output)
1292 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1293 (formals (generate-temporaries escapes))
1294 (sexp (substitute-references #'exp (zip escapes formals)))
1295 (refs (map escape->ref escapes)))
1296 #`(make-gexp (list #,@refs)
1297 current-imported-modules
1298 current-imported-extensions
1304 ;;; Module handling.
1308 (char-set-complement (char-set #\/)))
1310 (define (file-mapping->tree mapping)
1311 "Convert MAPPING, an alist like:
1313 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1315 to a tree suitable for 'interned-file-tree'."
1316 (let ((mapping (map (match-lambda
1317 ((destination . source)
1318 (cons (string-tokenize destination
1322 (fold (lambda (pair result)
1324 ((destination . source)
1325 (let loop ((destination destination)
1329 (let* ((mode (stat:mode (stat source)))
1330 (type (if (zero? (logand mode #o100))
1334 `(,type (file ,source))
1337 (let ((directory (assoc-ref result file)))
1342 (('directory . entries) entries)
1345 (alist-delete file result)
1350 (define %utils-module
1351 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1352 ;; other primitives below. Note: We give the file name relative to this
1353 ;; file you are currently reading; 'search-path' could return a file name
1354 ;; relative to the current working directory.
1355 (local-file "build/utils.scm"
1358 (define* (imported-files/derivation files
1359 #:key (name "file-import")
1361 (system (%current-system))
1362 (guile (%guile-for-build)))
1363 "Return a derivation that imports FILES into STORE. FILES must be a list
1364 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1365 resulting store path. FILE can be either a file name, or a file-like object,
1366 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1367 to the source files instead of copying them."
1370 ((final-path . (? string? file-name))
1371 (mlet %store-monad ((file (interned-file file-name
1372 (basename final-path))))
1373 (return (list final-path file))))
1374 ((final-path . file-like)
1375 (mlet %store-monad ((file (lower-object file-like system)))
1376 (return (list final-path file))))))
1378 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1382 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1383 (use-modules (ice-9 match))
1385 (mkdir (ungexp output)) (chdir (ungexp output))
1386 (for-each (match-lambda
1387 ((final-path store-path)
1388 (mkdir-p (dirname final-path))
1389 ((ungexp (if symlink? 'symlink 'copy-file))
1390 store-path final-path)))
1393 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1394 ;; exactly the same regardless of FILES: less disk space, and fewer
1395 ;; 'add-to-store' RPCs.
1396 (gexp->derivation name build
1398 #:guile-for-build guile
1401 ;; Avoid deprecation warnings about the use of the _IO*
1402 ;; constants in (guix build utils).
1404 '(("GUILE_WARN_DEPRECATED" . "no")))))
1406 (define* (imported-files files
1407 #:key (name "file-import")
1408 ;; The following parameters make sense when creating
1409 ;; an actual derivation.
1410 (system (%current-system))
1411 (guile (%guile-for-build)))
1412 "Import FILES into the store and return the resulting derivation or store
1413 file name (a derivation is created if and only if some elements of FILES are
1414 file-like objects and not local file names.) FILES must be a list
1415 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1416 resulting store path. FILE can be either a file name, or a file-like object,
1417 as returned by 'local-file' for example."
1418 (if (any (match-lambda
1419 ((_ . (? struct? source)) #t)
1422 (imported-files/derivation files #:name name
1423 #:symlink? derivation?
1424 #:system system #:guile guile)
1425 (interned-file-tree `(,name directory
1426 ,@(file-mapping->tree files)))))
1428 (define* (imported-modules modules
1429 #:key (name "module-import")
1430 (system (%current-system))
1431 (guile (%guile-for-build))
1432 (module-path %load-path))
1433 "Return a derivation that contains the source files of MODULES, a list of
1434 module names such as `(ice-9 q)'. All of MODULES must be either names of
1435 modules to be found in the MODULE-PATH search path, or a module name followed
1436 by an arrow followed by a file-like object. For example:
1438 (imported-modules `((guix build utils)
1440 ((guix config) => ,(scheme-file …))))
1442 In this example, the first two modules are taken from MODULE-PATH, and the
1443 last one is created from the given <scheme-file> object."
1444 (let ((files (map (match-lambda
1445 (((module ...) '=> file)
1446 (cons (module->source-file-name module)
1449 (let ((f (module->source-file-name module)))
1450 (cons f (search-path* module-path f)))))
1452 (imported-files files #:name name
1456 (define* (compiled-modules modules
1457 #:key (name "module-import-compiled")
1458 (system (%current-system))
1460 (guile (%guile-for-build))
1461 (module-path %load-path)
1463 (deprecation-warnings #f))
1464 "Return a derivation that builds a tree containing the `.go' files
1465 corresponding to MODULES. All the MODULES are built in a context where
1466 they can refer to each other. When TARGET is true, cross-compile MODULES for
1467 TARGET, a GNU triplet."
1468 (define total (length modules))
1470 (mlet %store-monad ((modules (imported-modules modules
1478 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1480 (use-modules (ice-9 ftw)
1484 (system base compile))
1486 ;; TODO: Inline this on the next rebuild cycle.
1489 (gexp ((use-modules (system base target))))
1492 (define (regular? file)
1493 (not (member file '("." ".."))))
1495 (define (process-entry entry output processed)
1496 (if (file-is-directory? entry)
1497 (let ((output (string-append output "/" (basename entry))))
1499 (process-directory entry output processed))
1500 (let* ((base (basename entry ".scm"))
1501 (output (string-append output "/" base ".go")))
1502 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1503 (+ 1 processed (ungexp total))
1504 (ungexp (* total 2))
1509 (gexp ((with-target (ungexp target)
1512 #:output-file output
1514 %auto-compilation-options)))))
1515 (gexp ((compile-file entry
1516 #:output-file output
1517 #:opts %auto-compilation-options)))))
1521 (define (process-directory directory output processed)
1522 (let ((entries (map (cut string-append directory "/" <>)
1523 (scandir directory regular?))))
1524 (fold (cut process-entry <> output <>)
1528 (define* (load-from-directory directory
1529 #:optional (loaded 0))
1530 "Load all the source files found in DIRECTORY."
1531 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1532 (let ((entries (map (cut string-append directory "/" <>)
1533 (scandir directory regular?))))
1534 (fold (lambda (file loaded)
1535 (if (file-is-directory? file)
1536 (load-from-directory file loaded)
1538 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1539 (+ 1 loaded) (ungexp (* 2 total))
1541 (save-module-excursion
1543 (primitive-load file)))
1548 (setvbuf (current-output-port)
1549 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1552 ;; Capture 'mkdir-p'.
1553 (@ (guix build utils) mkdir-p))
1555 ;; Add EXTENSIONS to the search path.
1557 (append (map (lambda (extension)
1558 (string-append extension
1559 "/share/guile/site/"
1560 (effective-version)))
1561 '((ungexp-native-splicing extensions)))
1563 (set! %load-compiled-path
1564 (append (map (lambda (extension)
1565 (string-append extension "/lib/guile/"
1568 '((ungexp-native-splicing extensions)))
1569 %load-compiled-path))
1571 (set! %load-path (cons (ungexp modules) %load-path))
1573 ;; Above we loaded our own (guix build utils) but now we may need to
1574 ;; load a compile a different one. Thus, force a reload.
1575 (let ((utils (string-append (ungexp modules)
1576 "/guix/build/utils.scm")))
1577 (when (file-exists? utils)
1580 (mkdir (ungexp output))
1581 (chdir (ungexp modules))
1583 (load-from-directory ".")
1584 (process-directory "." (ungexp output) 0))))
1586 ;; TODO: Pass MODULES as an environment variable.
1587 (gexp->derivation name build
1589 #:guile-for-build guile
1592 (case deprecation-warnings
1594 '(("GUILE_WARN_DEPRECATED" . "no")))
1596 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1602 ;;; Convenience procedures.
1605 (define (default-guile)
1606 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1607 ;; programs returned by 'program-file' and we don't want to keep references
1608 ;; to several Guile packages). This module must not refer to (gnu …)
1609 ;; modules directly, to avoid circular dependencies, hence this hack.
1610 (module-ref (resolve-interface '(gnu packages guile))
1613 (define* (load-path-expression modules #:optional (path %load-path)
1614 #:key (extensions '()) system target)
1615 "Return as a monadic value a gexp that sets '%load-path' and
1616 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1617 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1618 (if (and (null? modules) (null? extensions))
1619 (with-monad %store-monad
1621 (mlet %store-monad ((modules (imported-modules modules
1624 (compiled (compiled-modules modules
1625 #:extensions extensions
1630 (gexp (eval-when (expand load eval)
1631 ;; Augment the load paths and delete duplicates. Do that
1632 ;; without loading (srfi srfi-1) or anything.
1633 (let ((extensions '((ungexp-splicing extensions)))
1634 (prepend (lambda (items lst)
1635 ;; This is O(N²) but N is typically small.
1636 (let loop ((items items)
1642 (delete (car items) lst))))))))
1644 (prepend (cons (ungexp modules)
1645 (map (lambda (extension)
1646 (string-append extension
1647 "/share/guile/site/"
1648 (effective-version)))
1651 (set! %load-compiled-path
1652 (prepend (cons (ungexp compiled)
1653 (map (lambda (extension)
1654 (string-append extension
1659 %load-compiled-path)))))))))
1661 (define* (gexp->script name exp
1662 #:key (guile (default-guile))
1663 (module-path %load-path)
1664 (system (%current-system))
1666 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1667 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1668 (mlet* %store-monad ((target (if (eq? target 'current)
1669 (current-target-system)
1672 (load-path-expression (gexp-modules exp)
1675 (gexp-extensions exp)
1678 (gexp->derivation name
1680 (call-with-output-file (ungexp output)
1682 ;; Note: that makes a long shebang. When the store
1683 ;; is /gnu/store, that fits within the 128-byte
1684 ;; limit imposed by Linux, but that may go beyond
1685 ;; when running tests.
1687 "#!~a/bin/guile --no-auto-compile~%!#~%"
1692 (gexp ((write '(ungexp set-load-path) port)))
1695 (write '(ungexp exp) port)
1696 (chmod port #o555))))
1699 #:module-path module-path
1701 ;; These derivations are not worth offloading or
1704 #:substitutable? #f)))
1706 (define* (gexp->file name exp #:key
1708 (module-path %load-path)
1710 (system (%current-system))
1712 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1713 is true, EXP is considered to be a list of expressions that will be spliced in
1716 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1717 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1718 Lookup EXP's modules in MODULE-PATH."
1719 (define modules (gexp-modules exp))
1720 (define extensions (gexp-extensions exp))
1723 ((target (if (eq? target 'current)
1724 (current-target-system)
1726 (no-load-path? -> (or (not set-load-path?)
1727 (and (null? modules)
1728 (null? extensions))))
1730 (load-path-expression modules module-path
1731 #:extensions extensions
1735 (gexp->derivation name
1737 (call-with-output-file (ungexp output)
1742 '(ungexp (if splice?
1744 (gexp ((ungexp exp)))))))))
1749 (gexp->derivation name
1751 (call-with-output-file (ungexp output)
1753 (write '(ungexp set-load-path) port)
1757 '(ungexp (if splice?
1759 (gexp ((ungexp exp)))))))))
1760 #:module-path module-path
1766 (define* (text-file* name #:rest text)
1767 "Return as a monadic value a derivation that builds a text file containing
1768 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1769 can be used in a gexp: packages, derivations, local file objects, etc. The
1770 resulting store file holds references to all these."
1772 (gexp (call-with-output-file (ungexp output "out")
1774 (display (string-append (ungexp-splicing text)) port)))))
1776 (gexp->derivation name builder
1778 #:substitutable? #f))
1780 (define* (mixed-text-file name #:rest text)
1781 "Return an object representing store file NAME containing TEXT. TEXT is a
1782 sequence of strings and file-like objects, as in:
1784 (mixed-text-file \"profile\"
1785 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1787 This is the declarative counterpart of 'text-file*'."
1789 (gexp (call-with-output-file (ungexp output "out")
1791 (display (string-append (ungexp-splicing text)) port)))))
1793 (computed-file name build))
1795 (define (file-union name files)
1796 "Return a <computed-file> that builds a directory containing all of FILES.
1797 Each item in FILES must be a two-element list where the first element is the
1798 file name to use in the new directory, and the second element is a gexp
1799 denoting the target file. Here's an example:
1802 `((\"hosts\" ,(plain-file \"hosts\"
1803 \"127.0.0.1 localhost\"))
1804 (\"bashrc\" ,(plain-file \"bashrc\"
1805 \"alias ls='ls --color'\"))
1806 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1808 This yields an 'etc' directory containing these two files."
1810 (with-imported-modules '((guix build utils))
1813 (use-modules (guix build utils))
1815 (mkdir (ungexp output))
1816 (chdir (ungexp output))
1822 ;; Stat the source to abort early if it does
1824 (stat (ungexp source))
1826 (mkdir-p (dirname (ungexp target)))
1827 (symlink (ungexp source)
1828 (ungexp target))))))
1831 (define* (directory-union name things
1832 #:key (copy? #f) (quiet? #f)
1833 (resolve-collision 'warn-about-collision))
1834 "Return a directory that is the union of THINGS, where THINGS is a list of
1835 file-like objects denoting directories. For example:
1837 (directory-union \"guile+emacs\" (list guile emacs))
1839 yields a directory that is the union of the 'guile' and 'emacs' packages.
1841 Call RESOLVE-COLLISION when several files collide, passing it the list of
1842 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1843 which case the colliding entry is skipped altogether.
1845 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1846 is true, the derivation will not print anything."
1849 (gexp (lambda (old new)
1850 (if (file-is-directory? old)
1852 (copy-file old new))))
1857 (gexp (%make-void-port "w"))
1858 (gexp (current-error-port))))
1862 ;; Only one thing; return it.
1866 (with-imported-modules '((guix build union))
1868 (use-modules (guix build union)
1869 (srfi srfi-1)) ;for 'first' and 'last'
1871 (union-build (ungexp output)
1874 #:log-port (ungexp log-port)
1875 #:symlink (ungexp symlink)
1877 (ungexp resolve-collision)))))))))
1881 ;;; Syntactic sugar.
1884 (eval-when (expand load eval)
1885 (define* (read-ungexp chr port #:optional native?)
1886 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1887 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1888 (define unquote-symbol
1889 (match (peek-char port)
1893 'ungexp-native-splicing
1902 (let ((str (symbol->string symbol)))
1903 (match (string-index-right str #\:)
1905 `(,unquote-symbol ,symbol))
1907 (let ((name (string->symbol (substring str 0 colon)))
1908 (output (substring str (+ colon 1))))
1909 `(,unquote-symbol ,name ,output))))))
1911 `(,unquote-symbol ,x))))
1913 (define (read-gexp chr port)
1914 "Read a 'gexp' form from PORT."
1915 `(gexp ,(read port)))
1917 ;; Extend the reader
1918 (read-hash-extend #\~ read-gexp)
1919 (read-hash-extend #\$ read-ungexp)
1920 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1922 ;;; gexp.scm ends here