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)
51 local-file-absolute-file-name
72 program-file-module-path
99 lowered-gexp-load-path
100 lowered-gexp-load-compiled-path
124 gexp-error-invalid-input))
128 ;;; This module implements "G-expressions", or "gexps". Gexps are like
129 ;;; S-expressions (sexps), with two differences:
131 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
132 ;;; replaced by the corresponding output file name; in addition, the
133 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
134 ;;; the native code of a given package, in case of cross-compilation;
136 ;;; 2. Gexps embed information about the derivations they refer to.
138 ;;; Gexps make it easy to write to files Scheme code that refers to store
139 ;;; items, or to write Scheme code to build derivations.
144 (define-record-type <gexp>
145 (make-gexp references modules extensions proc)
147 (references gexp-references) ;list of <gexp-input>
148 (modules gexp-self-modules) ;list of module names
149 (extensions gexp-self-extensions) ;list of lowerable things
150 (proc gexp-proc)) ;procedure
152 (define (write-gexp gexp port)
153 "Write GEXP on PORT."
154 (display "#<gexp " port)
156 ;; Try to write the underlying sexp. Now, this trick doesn't work when
157 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
158 ;; tries to use 'append' on that, which fails with wrong-type-arg.
160 (write (apply (gexp-proc gexp)
161 (gexp-references gexp))
164 (number->string (object-address gexp) 16)))
166 (set-record-type-printer! <gexp> write-gexp)
173 ;; Compiler for a type of objects that may be introduced in a gexp.
174 (define-record-type <gexp-compiler>
175 (gexp-compiler type lower expand)
177 (type gexp-compiler-type) ;record type descriptor
178 (lower gexp-compiler-lower)
179 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
181 (define-condition-type &gexp-error &error
184 (define-condition-type &gexp-input-error &gexp-error
186 (input gexp-error-invalid-input))
189 (define %gexp-compilers
190 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
191 (make-hash-table 20))
193 (define (default-expander thing obj output)
194 "This is the default expander for \"things\" that appear in gexps. It
195 returns its output file name of OBJ's OUTPUT."
198 (derivation->output-path drv output))
201 ((? self-quoting? obj)
204 (define (register-compiler! compiler)
205 "Register COMPILER as a gexp compiler."
206 (hashq-set! %gexp-compilers
207 (gexp-compiler-type compiler) compiler))
209 (define (lookup-compiler object)
210 "Search for a compiler for OBJECT. Upon success, return the three argument
211 procedure to lower it; otherwise return #f."
212 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
213 gexp-compiler-lower))
215 (define (file-like? object)
216 "Return #t if OBJECT leads to a file in the store once unquoted in a
217 G-expression; otherwise return #f."
218 (and (struct? object) (->bool (lookup-compiler object))))
220 (define (lookup-expander object)
221 "Search for an expander for OBJECT. Upon success, return the three argument
222 procedure to expand it; otherwise return #f."
223 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
224 gexp-compiler-expand))
226 (define* (lower-object obj
227 #:optional (system (%current-system))
228 #:key (target 'current))
229 "Return as a value in %STORE-MONAD the derivation or store item
230 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
231 OBJ must be an object that has an associated gexp compiler, such as a
233 (mlet %store-monad ((target (if (eq? target 'current)
234 (current-target-system)
236 (graft? (grafting?)))
237 (let loop ((obj obj))
238 (match (lookup-compiler obj)
240 (raise (condition (&gexp-input-error (input obj)))))
242 ;; Cache in STORE the result of lowering OBJ.
243 (mcached (mlet %store-monad ((lowered (lower obj system target)))
244 (if (and (struct? lowered)
245 (not (derivation? lowered)))
249 system target graft?))))))
251 (define* (lower+expand-object obj
252 #:optional (system (%current-system))
253 #:key target (output "out"))
254 "Return as a value in %STORE-MONAD the output of object OBJ expands to for
255 SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
256 expand to file names, but it's possible to expand to a plain data type."
258 (expand (and (struct? obj) (lookup-expander obj))))
259 (match (lookup-compiler obj)
261 (raise (condition (&gexp-input-error (input obj)))))
263 (mlet* %store-monad ((graft? (grafting?))
264 (lowered (mcached (lower obj system target)
266 system target graft?)))
267 ;; LOWER might return something that needs to be further
269 (if (struct? lowered)
270 ;; If we lack an expander, delegate to that of LOWERED.
272 (loop lowered (lookup-expander lowered))
273 (return (expand obj lowered output)))
274 (if (not expand) ;self-quoting
276 (return (expand obj lowered output)))))))))
278 (define-syntax define-gexp-compiler
279 (syntax-rules (=> compiler expander)
280 "Define NAME as a compiler for objects matching PREDICATE encountered in
283 In the simplest form of the macro, BODY must return (1) a derivation for
284 a record of the specified type, for SYSTEM and TARGET (the latter of which is
285 #f except when cross-compiling), (2) another record that can itself be
286 compiled down to a derivation, or (3) an object of a primitive data type.
288 The more elaborate form allows you to specify an expander:
290 (define-gexp-compiler something-compiler <something>
291 compiler => (lambda (param system target) ...)
292 expander => (lambda (param drv output) ...))
294 The expander specifies how an object is converted to its sexp representation."
295 ((_ (name (param record-type) system target) body ...)
296 (define-gexp-compiler name record-type
297 compiler => (lambda (param system target) body ...)
298 expander => default-expander))
304 (gexp-compiler record-type compile expand))
305 (register-compiler! name)))))
307 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
308 ;; Derivations are the lowest-level representation, so this is the identity
310 (with-monad %store-monad
313 ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
314 ;; words, this gives the raw ".drv" file instead of its build result.
315 (define-record-type <raw-derivation-file>
316 (raw-derivation-file obj)
318 (obj raw-derivation-file-object)) ;lowerable object
320 (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
321 compiler => (lambda (obj system target)
322 (mlet %store-monad ((obj (lower-object
323 (raw-derivation-file-object obj)
324 system #:target target)))
325 ;; Returning the .drv file name instead of the <derivation>
326 ;; record ensures that 'lower-gexp' will classify it as a
327 ;; "source" and not as an "input".
328 (return (if (derivation? obj)
329 (derivation-file-name obj)
331 expander => (lambda (obj lowered output)
332 (if (derivation? lowered)
333 (derivation-file-name lowered)
338 ;;; System dependencies.
341 ;; Binding form for the current system and cross-compilation target.
342 (define-record-type <system-binding>
343 (system-binding proc)
345 (proc system-binding-proc))
347 (define-syntax let-system
349 "Introduce a system binding in a gexp. The simplest form is:
352 (cond ((string=? system \"x86_64-linux\") ...)
355 which binds SYSTEM to the currently targeted system. The second form is
356 similar, but it also shows the cross-compilation target:
358 (let-system (system target)
361 Here TARGET is bound to the cross-compilation triplet or #f."
362 ((_ (system target) exp0 exp ...)
363 (system-binding (lambda (system target)
365 ((_ system exp0 exp ...)
366 (system-binding (lambda (system target)
369 (define-gexp-compiler system-binding-compiler <system-binding>
370 compiler => (lambda (binding system target)
372 (($ <system-binding> proc)
373 (with-monad %store-monad
374 ;; PROC is expected to return a lowerable object.
375 ;; 'lower-object' takes care of residualizing it to a
376 ;; derivation or similar.
377 (return (proc system target))))))
379 ;; Delegate to the expander of the object returned by PROC.
384 ;;; File declarations.
387 ;; A local file name. FILE is the file name the user entered, which can be a
388 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
389 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
390 ;; repeated 'stat' calls.
391 (define-record-type <local-file>
392 (%%local-file file absolute name recursive? select?)
394 (file local-file-file) ;string
395 (absolute %local-file-absolute-file-name) ;promise string
396 (name local-file-name) ;string
397 (recursive? local-file-recursive?) ;Boolean
398 (select? local-file-select?)) ;string stat -> Boolean
400 (define (true file stat) #t)
402 (define* (%local-file file promise #:optional (name (basename file))
403 #:key recursive? (select? true))
404 ;; This intermediate procedure is part of our ABI, but the underlying
405 ;; %%LOCAL-FILE is not.
406 (%%local-file file promise name recursive? select?))
408 (define (absolute-file-name file directory)
409 "Return the canonical absolute file name for FILE, which lives in the
410 vicinity of DIRECTORY."
412 (cond ((string-prefix? "/" file) file)
413 ((not directory) file)
414 ((string-prefix? "/" directory)
415 (string-append directory "/" file))
418 (define-syntax local-file
420 "Return an object representing local file FILE to add to the store; this
421 object can be used in a gexp. If FILE is a relative file name, it is looked
422 up relative to the source file where this form appears. FILE will be added to
423 the store under NAME--by default the base name of FILE.
425 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
426 designates a flat file and RECURSIVE? is true, its contents are added, and its
427 permission bits are kept.
429 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
430 where FILE is the entry's absolute file name and STAT is the result of
431 'lstat'; exclude entries for which SELECT? does not return true.
433 This is the declarative counterpart of the 'interned-file' monadic procedure.
434 It is implemented as a macro to capture the current source directory where it
438 (string? (syntax->datum #'file))
439 ;; FILE is a literal, so resolve it relative to the source directory.
441 (delay (absolute-file-name file (current-source-directory)))
444 ;; Resolve FILE relative to the current directory.
446 (delay (absolute-file-name file (getcwd)))
449 #'(syntax-error "missing file name"))
452 ;; XXX: We could return #'(lambda (file . rest) ...). However,
453 ;; (syntax-source #'id) is #f so (current-source-directory) would not
454 ;; work. Thus, simply forbid this form.
456 "'local-file' is a macro and cannot be used like this")))))
458 (define (local-file-absolute-file-name file)
459 "Return the absolute file name for FILE, a <local-file> instance. A
460 'system-error' exception is raised if FILE could not be found."
461 (force (%local-file-absolute-file-name file)))
463 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
464 ;; "Compile" FILE by adding it to the store.
466 (($ <local-file> file (= force absolute) name recursive? select?)
467 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
468 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
469 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
470 ;; just throw an error, both of which are inconvenient.
471 (interned-file absolute name
472 #:recursive? recursive? #:select? select?))))
474 (define-record-type <plain-file>
475 (%plain-file name content references)
477 (name plain-file-name) ;string
478 (content plain-file-content) ;string or bytevector
479 (references plain-file-references)) ;list (currently unused)
481 (define (plain-file name content)
482 "Return an object representing a text file called NAME with the given
483 CONTENT (a string) to be added to the store.
485 This is the declarative counterpart of 'text-file'."
486 ;; XXX: For now just ignore 'references' because it's not clear how to use
487 ;; them in a declarative context.
488 (%plain-file name content '()))
490 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
491 ;; "Compile" FILE by adding it to the store.
493 (($ <plain-file> name (and (? string?) content) references)
494 (text-file name content references))
495 (($ <plain-file> name (and (? bytevector?) content) references)
496 (binary-file name content references))))
498 (define-record-type <computed-file>
499 (%computed-file name gexp guile options)
501 (name computed-file-name) ;string
502 (gexp computed-file-gexp) ;gexp
503 (guile computed-file-guile) ;<package>
504 (options computed-file-options)) ;list of arguments
506 (define* (computed-file name gexp
507 #:key guile (options '(#:local-build? #t)))
508 "Return an object representing the store item NAME, a file or directory
509 computed by GEXP. OPTIONS is a list of additional arguments to pass
510 to 'gexp->derivation'.
512 This is the declarative counterpart of 'gexp->derivation'."
513 (%computed-file name gexp guile options))
515 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
517 ;; Compile FILE by returning a derivation whose build expression is its
520 (($ <computed-file> name gexp guile options)
522 (mlet %store-monad ((guile (lower-object guile system
524 (apply gexp->derivation name gexp #:guile-for-build guile
525 #:system system #:target target options))
526 (apply gexp->derivation name gexp
527 #:system system #:target target options)))))
529 (define-record-type <program-file>
530 (%program-file name gexp guile path)
532 (name program-file-name) ;string
533 (gexp program-file-gexp) ;gexp
534 (guile program-file-guile) ;package
535 (path program-file-module-path)) ;list of strings
537 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
538 "Return an object representing the executable store item NAME that runs
539 GEXP. GUILE is the Guile package used to execute that script. Imported
540 modules of GEXP are looked up in MODULE-PATH.
542 This is the declarative counterpart of 'gexp->script'."
543 (%program-file name gexp guile module-path))
545 (define-gexp-compiler (program-file-compiler (file <program-file>)
547 ;; Compile FILE by returning a derivation that builds the script.
549 (($ <program-file> name gexp guile module-path)
550 (gexp->script name gexp
551 #:module-path module-path
552 #:guile (or guile (default-guile))
556 (define-record-type <scheme-file>
557 (%scheme-file name gexp splice? load-path?)
559 (name scheme-file-name) ;string
560 (gexp scheme-file-gexp) ;gexp
561 (splice? scheme-file-splice?) ;Boolean
562 (load-path? scheme-file-set-load-path?)) ;Boolean
564 (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
565 "Return an object representing the Scheme file NAME that contains GEXP.
567 This is the declarative counterpart of 'gexp->file'."
568 (%scheme-file name gexp splice? set-load-path?))
570 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
572 ;; Compile FILE by returning a derivation that builds the file.
574 (($ <scheme-file> name gexp splice? set-load-path?)
575 (gexp->file name gexp
576 #:set-load-path? set-load-path?
581 ;; Appending SUFFIX to BASE's output file name.
582 (define-record-type <file-append>
583 (%file-append base suffix)
585 (base file-append-base) ;<package> | <derivation> | ...
586 (suffix file-append-suffix)) ;list of strings
588 (define (write-file-append file port)
590 (($ <file-append> base suffix)
591 (format port "#<file-append ~s ~s>" base
592 (string-join suffix)))))
594 (set-record-type-printer! <file-append> write-file-append)
596 (define (file-append base . suffix)
597 "Return a <file-append> object that expands to the concatenation of BASE and
599 (%file-append base suffix))
601 (define-gexp-compiler file-append-compiler <file-append>
602 compiler => (lambda (obj system target)
604 (($ <file-append> base _)
605 (lower-object base system #:target target))))
606 expander => (lambda (obj lowered output)
608 (($ <file-append> base suffix)
609 (let* ((expand (lookup-expander base))
610 (base (expand base lowered output)))
611 (string-append base (string-concatenate suffix)))))))
613 ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
615 (define-record-type <parameterized>
616 (parameterized bindings thunk)
618 (bindings parameterized-bindings) ;list of parameter/value pairs
619 (thunk parameterized-thunk)) ;thunk
621 (define-syntax-rule (with-parameters ((param value) ...) body ...)
622 "Bind each PARAM to the corresponding VALUE for the extent during which BODY
623 is lowered. Consider this example:
625 (with-parameters ((%current-system \"x86_64-linux\"))
628 It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
629 x86_64-linux when COREUTILS is lowered."
630 (parameterized (list (list param (lambda () value)) ...)
634 (define-gexp-compiler compile-parameterized <parameterized>
636 (lambda (parameterized system target)
637 (match (parameterized-bindings parameterized)
638 (((parameters values) ...)
639 (let ((fluids (map parameter-fluid parameters))
640 (thunk (parameterized-thunk parameterized)))
641 ;; Install the PARAMETERS for the dynamic extent of THUNK.
643 (map (lambda (thunk) (thunk)) values)
645 ;; Special-case '%current-system' and '%current-target-system' to
646 ;; make sure we get the desired effect.
647 (let ((system (if (memq %current-system parameters)
650 (target (if (memq %current-target-system parameters)
651 (%current-target-system)
653 (lower-object (thunk) system #:target target))))))))
655 expander => (lambda (parameterized lowered output)
656 (match (parameterized-bindings parameterized)
657 (((parameters values) ...)
658 (let ((fluids (map parameter-fluid parameters))
659 (thunk (parameterized-thunk parameterized)))
660 ;; Install the PARAMETERS for the dynamic extent of THUNK.
662 (map (lambda (thunk) (thunk)) values)
664 ;; Delegate to the expander of the wrapped object.
665 (let* ((base (thunk))
666 (expand (lookup-expander base)))
667 (expand base lowered output)))))))))
671 ;;; Inputs & outputs.
674 ;; The input of a gexp.
675 (define-record-type <gexp-input>
676 (%gexp-input thing output native?)
678 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
679 (output gexp-input-output) ;string
680 (native? gexp-input-native?)) ;Boolean
682 (define (write-gexp-input input port)
684 (($ <gexp-input> thing output #f)
685 (format port "#<gexp-input ~s:~a>" thing output))
686 (($ <gexp-input> thing output #t)
687 (format port "#<gexp-input native ~s:~a>" thing output))))
689 (set-record-type-printer! <gexp-input> write-gexp-input)
691 (define* (gexp-input thing ;convenience procedure
692 #:optional (output "out")
694 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
695 whether this should be considered a \"native\" input or not."
696 (%gexp-input thing output native?))
698 ;; Reference to one of the derivation's outputs, for gexps used in
700 (define-record-type <gexp-output>
703 (name gexp-output-name))
705 (define (write-gexp-output output port)
707 (($ <gexp-output> name)
708 (format port "#<gexp-output ~a>" name))))
710 (set-record-type-printer! <gexp-output> write-gexp-output)
712 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
713 "Recurse on GEXP and the expressions it refers to, summing the items
714 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
715 second argument to 'delete-duplicates'."
718 (append (self-attribute gexp)
719 (append-map (match-lambda
720 (($ <gexp-input> (? gexp? exp))
721 (gexp-attribute exp self-attribute))
722 (($ <gexp-input> (lst ...))
723 (append-map (lambda (item)
731 (gexp-references gexp)))
733 '())) ;plain Scheme data type
735 (define (gexp-modules gexp)
736 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
737 false, meaning that GEXP is a plain Scheme object, return the empty list."
738 (define (module=? m1 m2)
739 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
740 ;; right-hand side may not be comparable with 'equal?': it's typically a
741 ;; file-like object that embeds a gexp, which in turn embeds closure;
742 ;; those closures may be 'eq?' when running compiled code but are unlikely
743 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
744 ;; avoid this discrepancy.
748 (((name2 ...) '=> _) (equal? name1 name2))
753 (gexp-attribute gexp gexp-self-modules module=?))
755 (define (gexp-extensions gexp)
756 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
757 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
759 (gexp-attribute gexp gexp-self-extensions))
761 (define (self-quoting? x)
762 (letrec-syntax ((one-of (syntax-rules ()
766 (one-of rest ...))))))
767 (one-of symbol? string? keyword? pair? null? array?
768 number? boolean? char?)))
770 (define* (lower-inputs inputs
772 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
773 item (a \"source\"); return the corresponding input list as a monadic value.
774 When TARGET is true, use it as the cross-compilation target triplet."
775 (define (store-item? obj)
776 (and (string? obj) (store-path? obj)))
779 (lift1 (cut filter ->bool <>) %store-monad))
781 (with-monad %store-monad
782 (>>= (mapm/accumulate-builds
784 (((? struct? thing) sub-drv ...)
785 (mlet %store-monad ((obj (lower-object
786 thing system #:target target)))
789 (let ((outputs (if (null? sub-drv)
792 (derivation-input drv outputs)))
793 ((? store-item? item)
796 ;; Some inputs such as <system-binding> can lower to
797 ;; a self-quoting object that FILTERM will filter
800 (((? store-item? item))
805 (define* (lower-reference-graphs graphs #:key system target)
806 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
807 #:reference-graphs argument, lower it such that each INPUT is replaced by the
808 corresponding <derivation-input> or store item."
810 (((file-names . inputs) ...)
811 (mlet %store-monad ((inputs (lower-inputs inputs
814 (return (map cons file-names inputs))))))
816 (define* (lower-references lst #:key system target)
817 "Based on LST, a list of output names and packages, return a list of output
818 names and file names suitable for the #:allowed-references argument to
820 (with-monad %store-monad
825 (($ <gexp-input> thing output native?)
826 (mlet %store-monad ((drv (lower-object thing system
829 (return (derivation->output-path drv output))))
831 (mlet %store-monad ((drv (lower-object thing system
833 (return (derivation->output-path drv))))))
835 (mapm/accumulate-builds lower lst)))
837 (define default-guile-derivation
838 ;; Here we break the abstraction by talking to the higher-level layer.
839 ;; Thus, do the resolution lazily to hide the circular dependency.
841 (let ((iface (resolve-interface '(guix packages))))
842 (module-ref iface 'default-guile-derivation)))))
844 ((force proc) system))))
846 ;; Representation of a gexp instantiated for a given target and system.
847 ;; It's an intermediate representation between <gexp> and <derivation>.
848 (define-record-type <lowered-gexp>
849 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
851 (sexp lowered-gexp-sexp) ;sexp
852 (inputs lowered-gexp-inputs) ;list of <derivation-input>
853 (sources lowered-gexp-sources) ;list of store items
854 (guile lowered-gexp-guile) ;<derivation-input> | #f
855 (load-path lowered-gexp-load-path) ;list of store items
856 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
858 (define* (imported+compiled-modules modules system
859 #:key (extensions '())
860 deprecation-warnings guile
861 (module-path %load-path))
862 "Return a pair where the first element is the imported MODULES and the
863 second element is the derivation to compile them."
865 (mlet %store-monad ((modules (if (pair? modules)
866 (imported-modules modules
868 #:module-path module-path)
870 (compiled (if (pair? modules)
871 (compiled-modules modules
873 #:module-path module-path
874 #:extensions extensions
876 #:deprecation-warnings
877 deprecation-warnings)
879 (return (cons modules compiled)))
881 system extensions guile deprecation-warnings module-path))
883 (define* (lower-gexp exp
885 (module-path %load-path)
886 (system (%current-system))
889 (guile-for-build (%guile-for-build))
890 (effective-version "3.0")
892 deprecation-warnings)
893 "*Note: This API is subject to change; use at your own risk!*
895 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
896 <lowered-gexp> ready to be used.
898 Lowered gexps are an intermediate representation that's useful for
899 applications that deal with gexps outside in a way that is disconnected from
900 derivations--e.g., code evaluated for its side effects."
902 (delete-duplicates (gexp-modules exp)))
904 (define (search-path modules extensions suffix)
905 (append (match modules
907 (list (derivation->output-path drv)))
910 ((? store-path? item)
912 (map (lambda (extension)
913 (string-append (match extension
915 (derivation->output-path drv))
916 ((? store-path? item)
921 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
922 ;; '%current-target-system' to be looked up at >>=
924 (graft? (set-grafting graft?))
926 (system -> (or system (%current-system)))
927 (target -> (if (eq? target 'current)
928 (%current-target-system)
930 (guile (if guile-for-build
931 (return guile-for-build)
932 (default-guile-derivation system)))
933 (normals (lower-inputs (gexp-inputs exp)
936 (natives (lower-inputs (gexp-native-inputs exp)
939 (inputs -> (append normals natives))
940 (sexp (gexp->sexp exp
943 (extensions -> (gexp-extensions exp))
944 (exts (mapm %store-monad
946 (lower-object obj system
949 (modules+compiled (imported+compiled-modules
951 #:extensions extensions
952 #:deprecation-warnings
955 #:module-path module-path))
956 (modules -> (car modules+compiled))
957 (compiled -> (cdr modules+compiled)))
959 (search-path modules exts
960 (string-append "/share/guile/site/" effective-version)))
962 (define load-compiled-path
963 (search-path compiled exts
964 (string-append "/lib/guile/" effective-version
968 (set-grafting graft?) ;restore the initial setting
969 (return (lowered-gexp sexp
970 `(,@(if (derivation? modules)
971 (list (derivation-input modules))
974 (list (derivation-input compiled))
976 ,@(map derivation-input exts)
977 ,@(filter derivation-input? inputs))
978 (filter string? (cons modules inputs))
979 (derivation-input guile '("out"))
981 load-compiled-path)))))
983 (define* (gexp->derivation name exp
985 system (target 'current)
986 hash hash-algo recursive?
989 (module-path %load-path)
990 (guile-for-build (%guile-for-build))
991 (effective-version "3.0")
994 allowed-references disallowed-references
996 local-build? (substitutable? #t)
999 (script-name (string-append name "-builder")))
1000 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
1001 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
1002 TARGET is true, it is used as the cross-compilation target triplet for
1003 packages referred to by EXP.
1005 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
1006 make MODULES available in the evaluation context of EXP; MODULES is a list of
1007 names of Guile modules searched in MODULE-PATH to be copied in the store,
1008 compiled, and made available in the load path during the execution of
1009 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
1011 EFFECTIVE-VERSION determines the string to use when adding extensions of
1012 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
1014 GRAFT? determines whether packages referred to by EXP should be grafted when
1017 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
1021 (FILE-NAME PACKAGE OUTPUT)
1022 (FILE-NAME DERIVATION)
1023 (FILE-NAME DERIVATION OUTPUT)
1024 (FILE-NAME STORE-ITEM)
1026 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
1027 an input of the build process of EXP. In the build environment, each
1028 FILE-NAME contains the reference graph of the corresponding item, in a simple
1031 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
1032 In the latter case, the list denotes store items that the result is allowed to
1033 refer to. Any reference to another store item will lead to a build error.
1034 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
1035 referenced by the outputs.
1037 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
1038 compiling modules. It can be #f, #t, or 'detailed.
1040 The other arguments are as for 'derivation'."
1041 (define outputs (gexp-outputs exp))
1042 (define requested-graft? graft?)
1044 (define (graphs-file-names graphs)
1045 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
1047 ((file-name . (? derivation-input? input))
1048 (cons file-name (first (derivation-input-output-paths input))))
1049 ((file-name . (? string? item))
1050 (cons file-name item)))
1053 (define (add-modules exp modules)
1056 (make-gexp (gexp-references exp)
1057 (append modules (gexp-self-modules exp))
1058 (gexp-self-extensions exp)
1061 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
1062 ;; '%current-target-system' to be looked up at >>=
1064 (graft? (set-grafting graft?))
1066 (system -> (or system (%current-system)))
1067 (target -> (if (eq? target 'current)
1068 (%current-target-system)
1070 (exp -> (add-modules exp modules))
1071 (lowered (lower-gexp exp
1072 #:module-path module-path
1075 #:graft? requested-graft?
1080 #:deprecation-warnings
1081 deprecation-warnings))
1083 (graphs (if references-graphs
1084 (lower-reference-graphs references-graphs
1088 (allowed (if allowed-references
1089 (lower-references allowed-references
1093 (disallowed (if disallowed-references
1094 (lower-references disallowed-references
1098 (guile -> (lowered-gexp-guile lowered))
1099 (builder (text-file script-name
1101 (lowered-gexp-sexp lowered)))))
1102 (mbegin %store-monad
1103 (set-grafting graft?) ;restore the initial setting
1104 (raw-derivation name
1105 (string-append (derivation-input-output-path guile)
1107 `("--no-auto-compile"
1108 ,@(append-map (lambda (directory)
1110 (lowered-gexp-load-path lowered))
1111 ,@(append-map (lambda (directory)
1113 (lowered-gexp-load-compiled-path lowered))
1119 ,@(lowered-gexp-inputs lowered)
1122 (filter derivation-input? inputs))
1124 #:sources `(,builder
1125 ,@(if (and (string? modules)
1126 (store-path? modules))
1129 ,@(lowered-gexp-sources lowered)
1132 (filter string? inputs))
1135 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
1136 #:references-graphs (and=> graphs graphs-file-names)
1137 #:allowed-references allowed
1138 #:disallowed-references disallowed
1139 #:leaked-env-vars leaked-env-vars
1140 #:local-build? local-build?
1141 #:substitutable? substitutable?
1142 #:properties properties))))
1144 (define* (gexp-inputs exp #:key native?)
1145 "Return the input list for EXP. When NATIVE? is true, return only native
1146 references; otherwise, return only non-native references."
1147 ;; TODO: Return <gexp-input> records instead of tuples.
1148 (define (add-reference-inputs ref result)
1150 (($ <gexp-input> (? gexp? exp) _ #t)
1152 (append (gexp-inputs exp)
1153 (gexp-inputs exp #:native? #t)
1156 (($ <gexp-input> (? gexp? exp) _ #f)
1157 (append (gexp-inputs exp #:native? native?)
1159 (($ <gexp-input> (? string? str))
1160 (if (direct-store-path? str)
1161 (cons `(,str) result)
1163 (($ <gexp-input> (? struct? thing) output n?)
1164 (if (and (eqv? n? native?) (lookup-compiler thing))
1165 ;; THING is a derivation, or a package, or an origin, etc.
1166 (cons `(,thing ,output) result)
1168 (($ <gexp-input> (lst ...) output n?)
1169 (fold-right add-reference-inputs result
1170 ;; XXX: For now, automatically convert LST to a list of
1171 ;; gexp-inputs. Inherit N?.
1174 (%gexp-input (gexp-input-thing x)
1175 (gexp-input-output x)
1178 (%gexp-input x "out" n?)))
1181 ;; Ignore references to other kinds of objects.
1184 (fold-right add-reference-inputs
1186 (gexp-references exp)))
1188 (define gexp-native-inputs
1189 (cut gexp-inputs <> #:native? #t))
1191 (define (gexp-outputs exp)
1192 "Return the outputs referred to by EXP as a list of strings."
1193 (define (add-reference-output ref result)
1195 (($ <gexp-output> name)
1197 (($ <gexp-input> (? gexp? exp))
1198 (append (gexp-outputs exp) result))
1199 (($ <gexp-input> (lst ...) output native?)
1200 ;; XXX: Automatically convert LST.
1201 (add-reference-output (map (match-lambda
1202 ((? gexp-input? x) x)
1203 (x (%gexp-input x "out" native?)))
1207 (fold-right add-reference-output result lst))
1212 (add-reference-output (gexp-references exp) '())))
1214 (define* (gexp->sexp exp #:key
1215 (system (%current-system))
1216 (target (%current-target-system)))
1217 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1218 and in the current monad setting (system type, etc.)"
1219 (define* (reference->sexp ref #:optional native?)
1220 (with-monad %store-monad
1222 (($ <gexp-output> output)
1223 ;; Output file names are not known in advance but the daemon defines
1224 ;; an environment variable for each of them at build time, so use
1226 (return `((@ (guile) getenv) ,output)))
1227 (($ <gexp-input> (? gexp? exp) output n?)
1230 #:target (if (or n? native?) #f target)))
1231 (($ <gexp-input> (refs ...) output n?)
1234 ;; XXX: Automatically convert REF to an gexp-input.
1236 (if (gexp-input? ref)
1238 (%gexp-input ref "out" n?))
1241 (($ <gexp-input> (? struct? thing) output n?)
1242 (let ((target (if (or n? native?) #f target)))
1243 (lower+expand-object thing system
1246 (($ <gexp-input> (? self-quoting? x))
1249 (raise (condition (&gexp-input-error (input x)))))
1254 ((args (mapm %store-monad
1255 reference->sexp (gexp-references exp))))
1256 (return (apply (gexp-proc exp) args))))
1258 (define-syntax-rule (define-syntax-parameter-once name proc)
1259 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1260 ;; does not get redefined. This works around a race condition in a
1261 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1262 (eval-when (load eval expand compile)
1264 (if (module-locally-bound? (current-module) 'name)
1265 (module-ref (current-module) 'name)
1266 (make-syntax-transformer 'name 'syntax-parameter
1269 (define-syntax-parameter-once current-imported-modules
1270 ;; Current list of imported modules.
1271 (identifier-syntax '()))
1273 (define-syntax-rule (with-imported-modules modules body ...)
1274 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1276 (syntax-parameterize ((current-imported-modules
1277 (identifier-syntax modules)))
1280 (define-syntax-parameter-once current-imported-extensions
1281 ;; Current list of extensions.
1282 (identifier-syntax '()))
1284 (define-syntax-rule (with-extensions extensions body ...)
1285 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1286 execution environment."
1287 (syntax-parameterize ((current-imported-extensions
1288 (identifier-syntax extensions)))
1293 (define (collect-escapes exp)
1294 ;; Return all the 'ungexp' present in EXP.
1295 (let loop ((exp exp)
1297 (syntax-case exp (ungexp
1300 ungexp-native-splicing)
1305 ((ungexp-splicing _ ...)
1307 ((ungexp-native _ ...)
1309 ((ungexp-native-splicing _ ...)
1312 (let ((result (loop #'exp0 result)))
1313 (loop #'exp result)))
1317 (define (escape->ref exp)
1318 ;; Turn 'ungexp' form EXP into a "reference".
1319 (syntax-case exp (ungexp ungexp-splicing
1320 ungexp-native ungexp-native-splicing
1323 #'(gexp-output "out"))
1324 ((ungexp output name)
1325 #'(gexp-output name))
1327 #'(%gexp-input thing "out" #f))
1328 ((ungexp drv-or-pkg out)
1329 #'(%gexp-input drv-or-pkg out #f))
1330 ((ungexp-splicing lst)
1331 #'(%gexp-input lst "out" #f))
1332 ((ungexp-native thing)
1333 #'(%gexp-input thing "out" #t))
1334 ((ungexp-native drv-or-pkg out)
1335 #'(%gexp-input drv-or-pkg out #t))
1336 ((ungexp-native-splicing lst)
1337 #'(%gexp-input lst "out" #t))))
1339 (define (substitute-ungexp exp substs)
1340 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1341 ;; the corresponding form in SUBSTS.
1342 (match (assoc exp substs)
1346 (with-syntax ((exp exp))
1347 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1349 (define (substitute-ungexp-splicing exp substs)
1352 (match (assoc #'exp substs)
1354 (with-syntax ((id id))
1356 #,(substitute-references #'(rest ...) substs))))
1358 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1361 (define (substitute-references exp substs)
1362 ;; Return a variant of EXP where all the cars of SUBSTS have been
1363 ;; replaced by the corresponding cdr.
1364 (syntax-case exp (ungexp ungexp-native
1365 ungexp-splicing ungexp-native-splicing)
1367 (substitute-ungexp exp substs))
1368 ((ungexp-native _ ...)
1369 (substitute-ungexp exp substs))
1370 (((ungexp-splicing _ ...) rest ...)
1371 (substitute-ungexp-splicing exp substs))
1372 (((ungexp-native-splicing _ ...) rest ...)
1373 (substitute-ungexp-splicing exp substs))
1375 #`(cons #,(substitute-references #'exp0 substs)
1376 #,(substitute-references #'exp substs)))
1379 (syntax-case s (ungexp output)
1381 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1382 (formals (generate-temporaries escapes))
1383 (sexp (substitute-references #'exp (zip escapes formals)))
1384 (refs (map escape->ref escapes)))
1385 #`(make-gexp (list #,@refs)
1386 current-imported-modules
1387 current-imported-extensions
1393 ;;; Module handling.
1396 (define %utils-module
1397 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1398 ;; other primitives below. Note: We give the file name relative to this
1399 ;; file you are currently reading; 'search-path' could return a file name
1400 ;; relative to the current working directory.
1401 (local-file "build/utils.scm"
1404 (define* (imported-files/derivation files
1405 #:key (name "file-import")
1407 (system (%current-system))
1408 (guile (%guile-for-build)))
1409 "Return a derivation that imports FILES into STORE. FILES must be a list
1410 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1411 resulting store path. FILE can be either a file name, or a file-like object,
1412 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1413 to the source files instead of copying them."
1416 ((final-path . (? string? file-name))
1417 (mlet %store-monad ((file (interned-file file-name
1418 (basename final-path))))
1419 (return (list final-path file))))
1420 ((final-path . file-like)
1421 (mlet %store-monad ((file (lower-object file-like system)))
1422 (return (list final-path file))))))
1424 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1428 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1429 (use-modules (ice-9 match))
1431 (mkdir (ungexp output)) (chdir (ungexp output))
1432 (for-each (match-lambda
1433 ((final-path store-path)
1434 (mkdir-p (dirname final-path))
1435 ((ungexp (if symlink? 'symlink 'copy-file))
1436 store-path final-path)))
1439 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1440 ;; exactly the same regardless of FILES: less disk space, and fewer
1441 ;; 'add-to-store' RPCs.
1442 (gexp->derivation name build
1444 #:guile-for-build guile
1448 ;; Avoid deprecation warnings about the use of the _IO*
1449 ;; constants in (guix build utils).
1451 '(("GUILE_WARN_DEPRECATED" . "no")))))
1453 (define* (imported-files files
1454 #:key (name "file-import")
1455 ;; The following parameters make sense when creating
1456 ;; an actual derivation.
1457 (system (%current-system))
1458 (guile (%guile-for-build)))
1459 "Import FILES into the store and return the resulting derivation or store
1460 file name (a derivation is created if and only if some elements of FILES are
1461 file-like objects and not local file names.) FILES must be a list
1462 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1463 resulting store path. FILE can be either a file name, or a file-like object,
1464 as returned by 'local-file' for example."
1465 (if (any (match-lambda
1466 ((_ . (? struct? source)) #t)
1469 (imported-files/derivation files #:name name
1470 #:symlink? derivation?
1471 #:system system #:guile guile)
1472 (interned-file-tree `(,name directory
1473 ,@(file-mapping->tree files)))))
1475 (define* (imported-modules modules
1476 #:key (name "module-import")
1477 (system (%current-system))
1478 (guile (%guile-for-build))
1479 (module-path %load-path))
1480 "Return a derivation that contains the source files of MODULES, a list of
1481 module names such as `(ice-9 q)'. All of MODULES must be either names of
1482 modules to be found in the MODULE-PATH search path, or a module name followed
1483 by an arrow followed by a file-like object. For example:
1485 (imported-modules `((guix build utils)
1487 ((guix config) => ,(scheme-file …))))
1489 In this example, the first two modules are taken from MODULE-PATH, and the
1490 last one is created from the given <scheme-file> object."
1491 (let ((files (map (match-lambda
1492 (((module ...) '=> file)
1493 (cons (module->source-file-name module)
1496 (let ((f (module->source-file-name module)))
1497 (cons f (search-path* module-path f)))))
1499 (imported-files files #:name name
1503 (define* (compiled-modules modules
1504 #:key (name "module-import-compiled")
1505 (system (%current-system))
1507 (guile (%guile-for-build))
1508 (module-path %load-path)
1510 (deprecation-warnings #f))
1511 "Return a derivation that builds a tree containing the `.go' files
1512 corresponding to MODULES. All the MODULES are built in a context where
1513 they can refer to each other. When TARGET is true, cross-compile MODULES for
1514 TARGET, a GNU triplet."
1515 (define total (length modules))
1517 (mlet %store-monad ((modules (imported-modules modules
1525 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1527 (use-modules (ice-9 ftw)
1531 (system base target)
1532 (system base compile))
1534 (define (regular? file)
1535 (not (member file '("." ".."))))
1537 (define (process-entry entry output processed)
1538 (if (file-is-directory? entry)
1539 (let ((output (string-append output "/" (basename entry))))
1541 (process-directory entry output processed))
1542 (let* ((base (basename entry ".scm"))
1543 (output (string-append output "/" base ".go")))
1544 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1545 (+ 1 processed (ungexp total))
1546 (ungexp (* total 2))
1551 (gexp ((with-target (ungexp target)
1554 #:output-file output
1556 %auto-compilation-options)))))
1557 (gexp ((compile-file entry
1558 #:output-file output
1559 #:opts %auto-compilation-options)))))
1563 (define (process-directory directory output processed)
1564 (let ((entries (map (cut string-append directory "/" <>)
1565 (scandir directory regular?))))
1566 (fold (cut process-entry <> output <>)
1570 (define* (load-from-directory directory
1571 #:optional (loaded 0))
1572 "Load all the source files found in DIRECTORY."
1573 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1574 (let ((entries (map (cut string-append directory "/" <>)
1575 (scandir directory regular?))))
1576 (fold (lambda (file loaded)
1577 (if (file-is-directory? file)
1578 (load-from-directory file loaded)
1580 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1581 (+ 1 loaded) (ungexp (* 2 total))
1583 (save-module-excursion
1585 (primitive-load file)))
1590 (setvbuf (current-output-port)
1591 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1594 ;; Capture 'mkdir-p'.
1595 (@ (guix build utils) mkdir-p))
1597 ;; Add EXTENSIONS to the search path.
1599 (append (map (lambda (extension)
1600 (string-append extension
1601 "/share/guile/site/"
1602 (effective-version)))
1603 '((ungexp-native-splicing extensions)))
1605 (set! %load-compiled-path
1606 (append (map (lambda (extension)
1607 (string-append extension "/lib/guile/"
1610 '((ungexp-native-splicing extensions)))
1611 %load-compiled-path))
1613 (set! %load-path (cons (ungexp modules) %load-path))
1615 ;; Above we loaded our own (guix build utils) but now we may need to
1616 ;; load a compile a different one. Thus, force a reload.
1617 (let ((utils (string-append (ungexp modules)
1618 "/guix/build/utils.scm")))
1619 (when (file-exists? utils)
1622 (mkdir (ungexp output))
1623 (chdir (ungexp modules))
1625 (load-from-directory ".")
1626 (process-directory "." (ungexp output) 0))))
1628 ;; TODO: Pass MODULES as an environment variable.
1629 (gexp->derivation name build
1631 #:guile-for-build guile
1634 (case deprecation-warnings
1636 '(("GUILE_WARN_DEPRECATED" . "no")))
1638 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1644 ;;; Convenience procedures.
1647 (define (default-guile)
1648 ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
1649 ;; programs returned by 'program-file' and we don't want to keep references
1650 ;; to several Guile packages). This module must not refer to (gnu …)
1651 ;; modules directly, to avoid circular dependencies, hence this hack.
1652 (module-ref (resolve-interface '(gnu packages guile))
1655 (define* (load-path-expression modules #:optional (path %load-path)
1656 #:key (extensions '()) system target)
1657 "Return as a monadic value a gexp that sets '%load-path' and
1658 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1659 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1660 (if (and (null? modules) (null? extensions))
1661 (with-monad %store-monad
1663 (mlet %store-monad ((modules (imported-modules modules
1666 (compiled (compiled-modules modules
1667 #:extensions extensions
1672 (gexp (eval-when (expand load eval)
1673 ;; Augment the load paths and delete duplicates. Do that
1674 ;; without loading (srfi srfi-1) or anything.
1675 (let ((extensions '((ungexp-splicing extensions)))
1676 (prepend (lambda (items lst)
1677 ;; This is O(N²) but N is typically small.
1678 (let loop ((items items)
1684 (delete (car items) lst))))))))
1686 (prepend (cons (ungexp modules)
1687 (map (lambda (extension)
1688 (string-append extension
1689 "/share/guile/site/"
1690 (effective-version)))
1693 (set! %load-compiled-path
1694 (prepend (cons (ungexp compiled)
1695 (map (lambda (extension)
1696 (string-append extension
1701 %load-compiled-path)))))))))
1703 (define* (gexp->script name exp
1704 #:key (guile (default-guile))
1705 (module-path %load-path)
1706 (system (%current-system))
1708 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1709 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1710 (mlet* %store-monad ((target (if (eq? target 'current)
1711 (current-target-system)
1714 (load-path-expression (gexp-modules exp)
1717 (gexp-extensions exp)
1720 (gexp->derivation name
1722 (call-with-output-file (ungexp output)
1724 ;; Note: that makes a long shebang. When the store
1725 ;; is /gnu/store, that fits within the 128-byte
1726 ;; limit imposed by Linux, but that may go beyond
1727 ;; when running tests.
1729 "#!~a/bin/guile --no-auto-compile~%!#~%"
1734 (gexp ((write '(ungexp set-load-path) port)))
1737 (write '(ungexp exp) port)
1738 (chmod port #o555))))
1741 #:module-path module-path
1743 ;; These derivations are not worth offloading or
1746 #:substitutable? #f)))
1748 (define* (gexp->file name exp #:key
1750 (module-path %load-path)
1752 (system (%current-system))
1754 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1755 is true, EXP is considered to be a list of expressions that will be spliced in
1758 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1759 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1760 Lookup EXP's modules in MODULE-PATH."
1761 (define modules (gexp-modules exp))
1762 (define extensions (gexp-extensions exp))
1765 ((target (if (eq? target 'current)
1766 (current-target-system)
1768 (no-load-path? -> (or (not set-load-path?)
1769 (and (null? modules)
1770 (null? extensions))))
1772 (load-path-expression modules module-path
1773 #:extensions extensions
1777 (gexp->derivation name
1779 (call-with-output-file (ungexp output)
1784 '(ungexp (if splice?
1786 (gexp ((ungexp exp)))))))))
1791 (gexp->derivation name
1793 (call-with-output-file (ungexp output)
1795 (write '(ungexp set-load-path) port)
1799 '(ungexp (if splice?
1801 (gexp ((ungexp exp)))))))))
1802 #:module-path module-path
1808 (define* (text-file* name #:rest text)
1809 "Return as a monadic value a derivation that builds a text file containing
1810 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1811 can be used in a gexp: packages, derivations, local file objects, etc. The
1812 resulting store file holds references to all these."
1814 (gexp (call-with-output-file (ungexp output "out")
1816 (display (string-append (ungexp-splicing text)) port)))))
1818 (gexp->derivation name builder
1820 #:substitutable? #f))
1822 (define* (mixed-text-file name #:rest text)
1823 "Return an object representing store file NAME containing TEXT. TEXT is a
1824 sequence of strings and file-like objects, as in:
1826 (mixed-text-file \"profile\"
1827 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1829 This is the declarative counterpart of 'text-file*'."
1831 (gexp (call-with-output-file (ungexp output "out")
1833 (display (string-append (ungexp-splicing text)) port)))))
1835 (computed-file name build))
1837 (define (file-union name files)
1838 "Return a <computed-file> that builds a directory containing all of FILES.
1839 Each item in FILES must be a two-element list where the first element is the
1840 file name to use in the new directory, and the second element is a gexp
1841 denoting the target file. Here's an example:
1844 `((\"hosts\" ,(plain-file \"hosts\"
1845 \"127.0.0.1 localhost\"))
1846 (\"bashrc\" ,(plain-file \"bashrc\"
1847 \"alias ls='ls --color'\"))
1848 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1850 This yields an 'etc' directory containing these two files."
1852 (with-imported-modules '((guix build utils))
1855 (use-modules (guix build utils))
1857 (mkdir (ungexp output))
1858 (chdir (ungexp output))
1864 ;; Stat the source to abort early if it does
1866 (stat (ungexp source))
1868 (mkdir-p (dirname (ungexp target)))
1869 (symlink (ungexp source)
1870 (ungexp target))))))
1873 (define* (directory-union name things
1874 #:key (copy? #f) (quiet? #f)
1875 (resolve-collision 'warn-about-collision))
1876 "Return a directory that is the union of THINGS, where THINGS is a list of
1877 file-like objects denoting directories. For example:
1879 (directory-union \"guile+emacs\" (list guile emacs))
1881 yields a directory that is the union of the 'guile' and 'emacs' packages.
1883 Call RESOLVE-COLLISION when several files collide, passing it the list of
1884 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1885 which case the colliding entry is skipped altogether.
1887 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1888 is true, the derivation will not print anything."
1891 (gexp (lambda (old new)
1892 (if (file-is-directory? old)
1894 (copy-file old new))))
1899 (gexp (%make-void-port "w"))
1900 (gexp (current-error-port))))
1904 ;; Only one thing; return it.
1908 (with-imported-modules '((guix build union))
1910 (use-modules (guix build union)
1911 (srfi srfi-1)) ;for 'first' and 'last'
1913 (union-build (ungexp output)
1916 #:log-port (ungexp log-port)
1917 #:symlink (ungexp symlink)
1919 (ungexp resolve-collision)))))))))
1923 ;;; Syntactic sugar.
1926 (eval-when (expand load eval)
1927 (define* (read-ungexp chr port #:optional native?)
1928 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1929 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1930 (define unquote-symbol
1931 (match (peek-char port)
1935 'ungexp-native-splicing
1944 (let ((str (symbol->string symbol)))
1945 (match (string-index-right str #\:)
1947 `(,unquote-symbol ,symbol))
1949 (let ((name (string->symbol (substring str 0 colon)))
1950 (output (substring str (+ colon 1))))
1951 `(,unquote-symbol ,name ,output))))))
1953 `(,unquote-symbol ,x))))
1955 (define (read-gexp chr port)
1956 "Read a 'gexp' form from PORT."
1957 `(gexp ,(read port)))
1959 ;; Extend the reader
1960 (read-hash-extend #\~ read-gexp)
1961 (read-hash-extend #\$ read-ungexp)
1962 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1964 ;;; gexp.scm ends here