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
71 program-file-module-path
98 lowered-gexp-load-path
99 lowered-gexp-load-compiled-path
123 gexp-error-invalid-input))
127 ;;; This module implements "G-expressions", or "gexps". Gexps are like
128 ;;; S-expressions (sexps), with two differences:
130 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
131 ;;; replaced by the corresponding output file name; in addition, the
132 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
133 ;;; the native code of a given package, in case of cross-compilation;
135 ;;; 2. Gexps embed information about the derivations they refer to.
137 ;;; Gexps make it easy to write to files Scheme code that refers to store
138 ;;; items, or to write Scheme code to build derivations.
143 (define-record-type <gexp>
144 (make-gexp references modules extensions proc)
146 (references gexp-references) ;list of <gexp-input>
147 (modules gexp-self-modules) ;list of module names
148 (extensions gexp-self-extensions) ;list of lowerable things
149 (proc gexp-proc)) ;procedure
151 (define (write-gexp gexp port)
152 "Write GEXP on PORT."
153 (display "#<gexp " port)
155 ;; Try to write the underlying sexp. Now, this trick doesn't work when
156 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
157 ;; tries to use 'append' on that, which fails with wrong-type-arg.
159 (write (apply (gexp-proc gexp)
160 (gexp-references gexp))
163 (number->string (object-address gexp) 16)))
165 (set-record-type-printer! <gexp> write-gexp)
172 ;; Compiler for a type of objects that may be introduced in a gexp.
173 (define-record-type <gexp-compiler>
174 (gexp-compiler type lower expand)
176 (type gexp-compiler-type) ;record type descriptor
177 (lower gexp-compiler-lower)
178 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
180 (define-condition-type &gexp-error &error
183 (define-condition-type &gexp-input-error &gexp-error
185 (input gexp-error-invalid-input))
188 (define %gexp-compilers
189 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
190 (make-hash-table 20))
192 (define (default-expander thing obj output)
193 "This is the default expander for \"things\" that appear in gexps. It
194 returns its output file name of OBJ's OUTPUT."
197 (derivation->output-path drv output))
200 ((? self-quoting? obj)
203 (define (register-compiler! compiler)
204 "Register COMPILER as a gexp compiler."
205 (hashq-set! %gexp-compilers
206 (gexp-compiler-type compiler) compiler))
208 (define (lookup-compiler object)
209 "Search for a compiler for OBJECT. Upon success, return the three argument
210 procedure to lower it; otherwise return #f."
211 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
212 gexp-compiler-lower))
214 (define (file-like? object)
215 "Return #t if OBJECT leads to a file in the store once unquoted in a
216 G-expression; otherwise return #f."
217 (and (struct? object) (->bool (lookup-compiler object))))
219 (define (lookup-expander object)
220 "Search for an expander for OBJECT. Upon success, return the three argument
221 procedure to expand it; otherwise return #f."
222 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
223 gexp-compiler-expand))
225 (define* (lower-object obj
226 #:optional (system (%current-system))
227 #:key (target 'current))
228 "Return as a value in %STORE-MONAD the derivation or store item
229 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
230 OBJ must be an object that has an associated gexp compiler, such as a
232 (mlet %store-monad ((target (if (eq? target 'current)
233 (current-target-system)
235 (graft? (grafting?)))
236 (let loop ((obj obj))
237 (match (lookup-compiler obj)
239 (raise (condition (&gexp-input-error (input obj)))))
241 ;; Cache in STORE the result of lowering OBJ.
242 (mcached (mlet %store-monad ((lowered (lower obj system target)))
243 (if (and (struct? lowered)
244 (not (derivation? lowered)))
248 system target graft?))))))
250 (define* (lower+expand-object obj
251 #:optional (system (%current-system))
252 #:key target (output "out"))
253 "Return as a value in %STORE-MONAD the output of object OBJ expands to for
254 SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
255 expand to file names, but it's possible to expand to a plain data type."
257 (expand (and (struct? obj) (lookup-expander obj))))
258 (match (lookup-compiler obj)
260 (raise (condition (&gexp-input-error (input obj)))))
262 (mlet* %store-monad ((graft? (grafting?))
263 (lowered (mcached (lower obj system target)
265 system target graft?)))
266 ;; LOWER might return something that needs to be further
268 (if (struct? lowered)
269 ;; If we lack an expander, delegate to that of LOWERED.
271 (loop lowered (lookup-expander lowered))
272 (return (expand obj lowered output)))
273 (if (not expand) ;self-quoting
275 (return (expand obj lowered output)))))))))
277 (define-syntax define-gexp-compiler
278 (syntax-rules (=> compiler expander)
279 "Define NAME as a compiler for objects matching PREDICATE encountered in
282 In the simplest form of the macro, BODY must return (1) a derivation for
283 a record of the specified type, for SYSTEM and TARGET (the latter of which is
284 #f except when cross-compiling), (2) another record that can itself be
285 compiled down to a derivation, or (3) an object of a primitive data type.
287 The more elaborate form allows you to specify an expander:
289 (define-gexp-compiler something-compiler <something>
290 compiler => (lambda (param system target) ...)
291 expander => (lambda (param drv output) ...))
293 The expander specifies how an object is converted to its sexp representation."
294 ((_ (name (param record-type) system target) body ...)
295 (define-gexp-compiler name record-type
296 compiler => (lambda (param system target) body ...)
297 expander => default-expander))
303 (gexp-compiler record-type compile expand))
304 (register-compiler! name)))))
306 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
307 ;; Derivations are the lowest-level representation, so this is the identity
309 (with-monad %store-monad
312 ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
313 ;; words, this gives the raw ".drv" file instead of its build result.
314 (define-record-type <raw-derivation-file>
315 (raw-derivation-file obj)
317 (obj raw-derivation-file-object)) ;lowerable object
319 (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
320 compiler => (lambda (obj system target)
321 (mlet %store-monad ((obj (lower-object
322 (raw-derivation-file-object obj)
323 system #:target target)))
324 ;; Returning the .drv file name instead of the <derivation>
325 ;; record ensures that 'lower-gexp' will classify it as a
326 ;; "source" and not as an "input".
327 (return (if (derivation? obj)
328 (derivation-file-name obj)
330 expander => (lambda (obj lowered output)
331 (if (derivation? lowered)
332 (derivation-file-name lowered)
337 ;;; System dependencies.
340 ;; Binding form for the current system and cross-compilation target.
341 (define-record-type <system-binding>
342 (system-binding proc)
344 (proc system-binding-proc))
346 (define-syntax let-system
348 "Introduce a system binding in a gexp. The simplest form is:
351 (cond ((string=? system \"x86_64-linux\") ...)
354 which binds SYSTEM to the currently targeted system. The second form is
355 similar, but it also shows the cross-compilation target:
357 (let-system (system target)
360 Here TARGET is bound to the cross-compilation triplet or #f."
361 ((_ (system target) exp0 exp ...)
362 (system-binding (lambda (system target)
364 ((_ system exp0 exp ...)
365 (system-binding (lambda (system target)
368 (define-gexp-compiler system-binding-compiler <system-binding>
369 compiler => (lambda (binding system target)
371 (($ <system-binding> proc)
372 (with-monad %store-monad
373 ;; PROC is expected to return a lowerable object.
374 ;; 'lower-object' takes care of residualizing it to a
375 ;; derivation or similar.
376 (return (proc system target))))))
378 ;; Delegate to the expander of the object returned by PROC.
383 ;;; File declarations.
386 ;; A local file name. FILE is the file name the user entered, which can be a
387 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
388 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
389 ;; repeated 'stat' calls.
390 (define-record-type <local-file>
391 (%%local-file file absolute name recursive? select?)
393 (file local-file-file) ;string
394 (absolute %local-file-absolute-file-name) ;promise string
395 (name local-file-name) ;string
396 (recursive? local-file-recursive?) ;Boolean
397 (select? local-file-select?)) ;string stat -> Boolean
399 (define (true file stat) #t)
401 (define* (%local-file file promise #:optional (name (basename file))
402 #:key recursive? (select? true))
403 ;; This intermediate procedure is part of our ABI, but the underlying
404 ;; %%LOCAL-FILE is not.
405 (%%local-file file promise name recursive? select?))
407 (define (absolute-file-name file directory)
408 "Return the canonical absolute file name for FILE, which lives in the
409 vicinity of DIRECTORY."
411 (cond ((string-prefix? "/" file) file)
412 ((not directory) file)
413 ((string-prefix? "/" directory)
414 (string-append directory "/" file))
417 (define-syntax local-file
419 "Return an object representing local file FILE to add to the store; this
420 object can be used in a gexp. If FILE is a relative file name, it is looked
421 up relative to the source file where this form appears. FILE will be added to
422 the store under NAME--by default the base name of FILE.
424 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
425 designates a flat file and RECURSIVE? is true, its contents are added, and its
426 permission bits are kept.
428 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
429 where FILE is the entry's absolute file name and STAT is the result of
430 'lstat'; exclude entries for which SELECT? does not return true.
432 This is the declarative counterpart of the 'interned-file' monadic procedure.
433 It is implemented as a macro to capture the current source directory where it
437 (string? (syntax->datum #'file))
438 ;; FILE is a literal, so resolve it relative to the source directory.
440 (delay (absolute-file-name file (current-source-directory)))
443 ;; Resolve FILE relative to the current directory.
445 (delay (absolute-file-name file (getcwd)))
448 #'(syntax-error "missing file name"))
451 ;; XXX: We could return #'(lambda (file . rest) ...). However,
452 ;; (syntax-source #'id) is #f so (current-source-directory) would not
453 ;; work. Thus, simply forbid this form.
455 "'local-file' is a macro and cannot be used like this")))))
457 (define (local-file-absolute-file-name file)
458 "Return the absolute file name for FILE, a <local-file> instance. A
459 'system-error' exception is raised if FILE could not be found."
460 (force (%local-file-absolute-file-name file)))
462 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
463 ;; "Compile" FILE by adding it to the store.
465 (($ <local-file> file (= force absolute) name recursive? select?)
466 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
467 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
468 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
469 ;; just throw an error, both of which are inconvenient.
470 (interned-file absolute name
471 #:recursive? recursive? #:select? select?))))
473 (define-record-type <plain-file>
474 (%plain-file name content references)
476 (name plain-file-name) ;string
477 (content plain-file-content) ;string or bytevector
478 (references plain-file-references)) ;list (currently unused)
480 (define (plain-file name content)
481 "Return an object representing a text file called NAME with the given
482 CONTENT (a string) to be added to the store.
484 This is the declarative counterpart of 'text-file'."
485 ;; XXX: For now just ignore 'references' because it's not clear how to use
486 ;; them in a declarative context.
487 (%plain-file name content '()))
489 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
490 ;; "Compile" FILE by adding it to the store.
492 (($ <plain-file> name (and (? string?) content) references)
493 (text-file name content references))
494 (($ <plain-file> name (and (? bytevector?) content) references)
495 (binary-file name content references))))
497 (define-record-type <computed-file>
498 (%computed-file name gexp guile options)
500 (name computed-file-name) ;string
501 (gexp computed-file-gexp) ;gexp
502 (guile computed-file-guile) ;<package>
503 (options computed-file-options)) ;list of arguments
505 (define* (computed-file name gexp
506 #:key guile (options '(#:local-build? #t)))
507 "Return an object representing the store item NAME, a file or directory
508 computed by GEXP. OPTIONS is a list of additional arguments to pass
509 to 'gexp->derivation'.
511 This is the declarative counterpart of 'gexp->derivation'."
512 (%computed-file name gexp guile options))
514 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
516 ;; Compile FILE by returning a derivation whose build expression is its
519 (($ <computed-file> name gexp guile options)
521 (mlet %store-monad ((guile (lower-object guile system
523 (apply gexp->derivation name gexp #:guile-for-build guile
524 #:system system #:target target options))
525 (apply gexp->derivation name gexp
526 #:system system #:target target options)))))
528 (define-record-type <program-file>
529 (%program-file name gexp guile path)
531 (name program-file-name) ;string
532 (gexp program-file-gexp) ;gexp
533 (guile program-file-guile) ;package
534 (path program-file-module-path)) ;list of strings
536 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
537 "Return an object representing the executable store item NAME that runs
538 GEXP. GUILE is the Guile package used to execute that script. Imported
539 modules of GEXP are looked up in MODULE-PATH.
541 This is the declarative counterpart of 'gexp->script'."
542 (%program-file name gexp guile module-path))
544 (define-gexp-compiler (program-file-compiler (file <program-file>)
546 ;; Compile FILE by returning a derivation that builds the script.
548 (($ <program-file> name gexp guile module-path)
549 (gexp->script name gexp
550 #:module-path module-path
551 #:guile (or guile (default-guile))
555 (define-record-type <scheme-file>
556 (%scheme-file name gexp splice? load-path?)
558 (name scheme-file-name) ;string
559 (gexp scheme-file-gexp) ;gexp
560 (splice? scheme-file-splice?) ;Boolean
561 (load-path? scheme-file-set-load-path?)) ;Boolean
563 (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
564 "Return an object representing the Scheme file NAME that contains GEXP.
566 This is the declarative counterpart of 'gexp->file'."
567 (%scheme-file name gexp splice? set-load-path?))
569 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
571 ;; Compile FILE by returning a derivation that builds the file.
573 (($ <scheme-file> name gexp splice? set-load-path?)
574 (gexp->file name gexp
575 #:set-load-path? set-load-path?
580 ;; Appending SUFFIX to BASE's output file name.
581 (define-record-type <file-append>
582 (%file-append base suffix)
584 (base file-append-base) ;<package> | <derivation> | ...
585 (suffix file-append-suffix)) ;list of strings
587 (define (write-file-append file port)
589 (($ <file-append> base suffix)
590 (format port "#<file-append ~s ~s>" base
591 (string-join suffix)))))
593 (set-record-type-printer! <file-append> write-file-append)
595 (define (file-append base . suffix)
596 "Return a <file-append> object that expands to the concatenation of BASE and
598 (%file-append base suffix))
600 (define-gexp-compiler file-append-compiler <file-append>
601 compiler => (lambda (obj system target)
603 (($ <file-append> base _)
604 (lower-object base system #:target target))))
605 expander => (lambda (obj lowered output)
607 (($ <file-append> base suffix)
608 (let* ((expand (lookup-expander base))
609 (base (expand base lowered output)))
610 (string-append base (string-concatenate suffix)))))))
612 ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
614 (define-record-type <parameterized>
615 (parameterized bindings thunk)
617 (bindings parameterized-bindings) ;list of parameter/value pairs
618 (thunk parameterized-thunk)) ;thunk
620 (define-syntax-rule (with-parameters ((param value) ...) body ...)
621 "Bind each PARAM to the corresponding VALUE for the extent during which BODY
622 is lowered. Consider this example:
624 (with-parameters ((%current-system \"x86_64-linux\"))
627 It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
628 x86_64-linux when COREUTILS is lowered."
629 (parameterized (list (list param (lambda () value)) ...)
633 (define-gexp-compiler compile-parameterized <parameterized>
635 (lambda (parameterized system target)
636 (match (parameterized-bindings parameterized)
637 (((parameters values) ...)
638 (let ((fluids (map parameter-fluid parameters))
639 (thunk (parameterized-thunk parameterized)))
640 ;; Install the PARAMETERS for the dynamic extent of THUNK.
642 (map (lambda (thunk) (thunk)) values)
644 ;; Special-case '%current-system' and '%current-target-system' to
645 ;; make sure we get the desired effect.
646 (let ((system (if (memq %current-system parameters)
649 (target (if (memq %current-target-system parameters)
650 (%current-target-system)
652 (lower-object (thunk) system #:target target))))))))
654 expander => (lambda (parameterized lowered output)
655 (match (parameterized-bindings parameterized)
656 (((parameters values) ...)
657 (let ((fluids (map parameter-fluid parameters))
658 (thunk (parameterized-thunk parameterized)))
659 ;; Install the PARAMETERS for the dynamic extent of THUNK.
661 (map (lambda (thunk) (thunk)) values)
663 ;; Delegate to the expander of the wrapped object.
664 (let* ((base (thunk))
665 (expand (lookup-expander base)))
666 (expand base lowered output)))))))))
670 ;;; Inputs & outputs.
673 ;; The input of a gexp.
674 (define-record-type <gexp-input>
675 (%gexp-input thing output native?)
677 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
678 (output gexp-input-output) ;string
679 (native? gexp-input-native?)) ;Boolean
681 (define (write-gexp-input input port)
683 (($ <gexp-input> thing output #f)
684 (format port "#<gexp-input ~s:~a>" thing output))
685 (($ <gexp-input> thing output #t)
686 (format port "#<gexp-input native ~s:~a>" thing output))))
688 (set-record-type-printer! <gexp-input> write-gexp-input)
690 (define* (gexp-input thing ;convenience procedure
691 #:optional (output "out")
693 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
694 whether this should be considered a \"native\" input or not."
695 (%gexp-input thing output native?))
697 ;; Reference to one of the derivation's outputs, for gexps used in
699 (define-record-type <gexp-output>
702 (name gexp-output-name))
704 (define (write-gexp-output output port)
706 (($ <gexp-output> name)
707 (format port "#<gexp-output ~a>" name))))
709 (set-record-type-printer! <gexp-output> write-gexp-output)
711 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
712 "Recurse on GEXP and the expressions it refers to, summing the items
713 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
714 second argument to 'delete-duplicates'."
717 (append (self-attribute gexp)
718 (append-map (match-lambda
719 (($ <gexp-input> (? gexp? exp))
720 (gexp-attribute exp self-attribute))
721 (($ <gexp-input> (lst ...))
722 (append-map (lambda (item)
730 (gexp-references gexp)))
732 '())) ;plain Scheme data type
734 (define (gexp-modules gexp)
735 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
736 false, meaning that GEXP is a plain Scheme object, return the empty list."
737 (define (module=? m1 m2)
738 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
739 ;; right-hand side may not be comparable with 'equal?': it's typically a
740 ;; file-like object that embeds a gexp, which in turn embeds closure;
741 ;; those closures may be 'eq?' when running compiled code but are unlikely
742 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
743 ;; avoid this discrepancy.
747 (((name2 ...) '=> _) (equal? name1 name2))
752 (gexp-attribute gexp gexp-self-modules module=?))
754 (define (gexp-extensions gexp)
755 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
756 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
758 (gexp-attribute gexp gexp-self-extensions))
760 (define (self-quoting? x)
761 (letrec-syntax ((one-of (syntax-rules ()
765 (one-of rest ...))))))
766 (one-of symbol? string? keyword? pair? null? array?
767 number? boolean? char?)))
769 (define* (lower-inputs inputs
771 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
772 item (a \"source\"); return the corresponding input list as a monadic value.
773 When TARGET is true, use it as the cross-compilation target triplet."
774 (define (store-item? obj)
775 (and (string? obj) (store-path? obj)))
778 (lift1 (cut filter ->bool <>) %store-monad))
780 (with-monad %store-monad
781 (>>= (mapm/accumulate-builds
783 (((? struct? thing) sub-drv ...)
784 (mlet %store-monad ((obj (lower-object
785 thing system #:target target)))
788 (let ((outputs (if (null? sub-drv)
791 (derivation-input drv outputs)))
792 ((? store-item? item)
795 ;; Some inputs such as <system-binding> can lower to
796 ;; a self-quoting object that FILTERM will filter
799 (((? store-item? item))
804 (define* (lower-reference-graphs graphs #:key system target)
805 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
806 #:reference-graphs argument, lower it such that each INPUT is replaced by the
807 corresponding <derivation-input> or store item."
809 (((file-names . inputs) ...)
810 (mlet %store-monad ((inputs (lower-inputs inputs
813 (return (map cons file-names inputs))))))
815 (define* (lower-references lst #:key system target)
816 "Based on LST, a list of output names and packages, return a list of output
817 names and file names suitable for the #:allowed-references argument to
819 (with-monad %store-monad
824 (($ <gexp-input> thing output native?)
825 (mlet %store-monad ((drv (lower-object thing system
828 (return (derivation->output-path drv output))))
830 (mlet %store-monad ((drv (lower-object thing system
832 (return (derivation->output-path drv))))))
834 (mapm/accumulate-builds lower lst)))
836 (define default-guile-derivation
837 ;; Here we break the abstraction by talking to the higher-level layer.
838 ;; Thus, do the resolution lazily to hide the circular dependency.
840 (let ((iface (resolve-interface '(guix packages))))
841 (module-ref iface 'default-guile-derivation)))))
843 ((force proc) system))))
845 ;; Representation of a gexp instantiated for a given target and system.
846 ;; It's an intermediate representation between <gexp> and <derivation>.
847 (define-record-type <lowered-gexp>
848 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
850 (sexp lowered-gexp-sexp) ;sexp
851 (inputs lowered-gexp-inputs) ;list of <derivation-input>
852 (sources lowered-gexp-sources) ;list of store items
853 (guile lowered-gexp-guile) ;<derivation-input> | #f
854 (load-path lowered-gexp-load-path) ;list of store items
855 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
857 (define* (imported+compiled-modules modules system
858 #:key (extensions '())
859 deprecation-warnings guile
860 (module-path %load-path))
861 "Return a pair where the first element is the imported MODULES and the
862 second element is the derivation to compile them."
864 (mlet %store-monad ((modules (if (pair? modules)
865 (imported-modules modules
867 #:module-path module-path)
869 (compiled (if (pair? modules)
870 (compiled-modules modules
872 #:module-path module-path
873 #:extensions extensions
875 #:deprecation-warnings
876 deprecation-warnings)
878 (return (cons modules compiled)))
880 system extensions guile deprecation-warnings module-path))
882 (define* (lower-gexp exp
884 (module-path %load-path)
885 (system (%current-system))
888 (guile-for-build (%guile-for-build))
889 (effective-version "3.0")
891 deprecation-warnings)
892 "*Note: This API is subject to change; use at your own risk!*
894 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
895 <lowered-gexp> ready to be used.
897 Lowered gexps are an intermediate representation that's useful for
898 applications that deal with gexps outside in a way that is disconnected from
899 derivations--e.g., code evaluated for its side effects."
901 (delete-duplicates (gexp-modules exp)))
903 (define (search-path modules extensions suffix)
904 (append (match modules
906 (list (derivation->output-path drv)))
909 ((? store-path? item)
911 (map (lambda (extension)
912 (string-append (match extension
914 (derivation->output-path drv))
915 ((? store-path? item)
920 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
921 ;; '%current-target-system' to be looked up at >>=
923 (graft? (set-grafting graft?))
925 (system -> (or system (%current-system)))
926 (target -> (if (eq? target 'current)
927 (%current-target-system)
929 (guile (if guile-for-build
930 (return guile-for-build)
931 (default-guile-derivation system)))
932 (normals (lower-inputs (gexp-inputs exp)
935 (natives (lower-inputs (gexp-native-inputs exp)
938 (inputs -> (append normals natives))
939 (sexp (gexp->sexp exp
942 (extensions -> (gexp-extensions exp))
943 (exts (mapm %store-monad
945 (lower-object obj system
948 (modules+compiled (imported+compiled-modules
950 #:extensions extensions
951 #:deprecation-warnings
954 #:module-path module-path))
955 (modules -> (car modules+compiled))
956 (compiled -> (cdr modules+compiled)))
958 (search-path modules exts
959 (string-append "/share/guile/site/" effective-version)))
961 (define load-compiled-path
962 (search-path compiled exts
963 (string-append "/lib/guile/" effective-version
967 (set-grafting graft?) ;restore the initial setting
968 (return (lowered-gexp sexp
969 `(,@(if (derivation? modules)
970 (list (derivation-input modules))
973 (list (derivation-input compiled))
975 ,@(map derivation-input exts)
976 ,@(filter derivation-input? inputs))
977 (filter string? (cons modules inputs))
978 (derivation-input guile '("out"))
980 load-compiled-path)))))
982 (define* (gexp->derivation name exp
984 system (target 'current)
985 hash hash-algo recursive?
988 (module-path %load-path)
989 (guile-for-build (%guile-for-build))
990 (effective-version "3.0")
993 allowed-references disallowed-references
995 local-build? (substitutable? #t)
998 (script-name (string-append name "-builder")))
999 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
1000 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
1001 TARGET is true, it is used as the cross-compilation target triplet for
1002 packages referred to by EXP.
1004 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
1005 make MODULES available in the evaluation context of EXP; MODULES is a list of
1006 names of Guile modules searched in MODULE-PATH to be copied in the store,
1007 compiled, and made available in the load path during the execution of
1008 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
1010 EFFECTIVE-VERSION determines the string to use when adding extensions of
1011 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
1013 GRAFT? determines whether packages referred to by EXP should be grafted when
1016 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
1020 (FILE-NAME PACKAGE OUTPUT)
1021 (FILE-NAME DERIVATION)
1022 (FILE-NAME DERIVATION OUTPUT)
1023 (FILE-NAME STORE-ITEM)
1025 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
1026 an input of the build process of EXP. In the build environment, each
1027 FILE-NAME contains the reference graph of the corresponding item, in a simple
1030 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
1031 In the latter case, the list denotes store items that the result is allowed to
1032 refer to. Any reference to another store item will lead to a build error.
1033 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
1034 referenced by the outputs.
1036 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
1037 compiling modules. It can be #f, #t, or 'detailed.
1039 The other arguments are as for 'derivation'."
1040 (define outputs (gexp-outputs exp))
1041 (define requested-graft? graft?)
1043 (define (graphs-file-names graphs)
1044 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
1046 ((file-name . (? derivation-input? input))
1047 (cons file-name (first (derivation-input-output-paths input))))
1048 ((file-name . (? string? item))
1049 (cons file-name item)))
1052 (define (add-modules exp modules)
1055 (make-gexp (gexp-references exp)
1056 (append modules (gexp-self-modules exp))
1057 (gexp-self-extensions exp)
1060 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
1061 ;; '%current-target-system' to be looked up at >>=
1063 (graft? (set-grafting graft?))
1065 (system -> (or system (%current-system)))
1066 (target -> (if (eq? target 'current)
1067 (%current-target-system)
1069 (exp -> (add-modules exp modules))
1070 (lowered (lower-gexp exp
1071 #:module-path module-path
1074 #:graft? requested-graft?
1079 #:deprecation-warnings
1080 deprecation-warnings))
1082 (graphs (if references-graphs
1083 (lower-reference-graphs references-graphs
1087 (allowed (if allowed-references
1088 (lower-references allowed-references
1092 (disallowed (if disallowed-references
1093 (lower-references disallowed-references
1097 (guile -> (lowered-gexp-guile lowered))
1098 (builder (text-file script-name
1100 (lowered-gexp-sexp lowered)))))
1101 (mbegin %store-monad
1102 (set-grafting graft?) ;restore the initial setting
1103 (raw-derivation name
1104 (string-append (derivation-input-output-path guile)
1106 `("--no-auto-compile"
1107 ,@(append-map (lambda (directory)
1109 (lowered-gexp-load-path lowered))
1110 ,@(append-map (lambda (directory)
1112 (lowered-gexp-load-compiled-path lowered))
1118 ,@(lowered-gexp-inputs lowered)
1121 (filter derivation-input? inputs))
1123 #:sources `(,builder
1124 ,@(if (and (string? modules)
1125 (store-path? modules))
1128 ,@(lowered-gexp-sources lowered)
1131 (filter string? inputs))
1134 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
1135 #:references-graphs (and=> graphs graphs-file-names)
1136 #:allowed-references allowed
1137 #:disallowed-references disallowed
1138 #:leaked-env-vars leaked-env-vars
1139 #:local-build? local-build?
1140 #:substitutable? substitutable?
1141 #:properties properties))))
1143 (define* (gexp-inputs exp #:key native?)
1144 "Return the input list for EXP. When NATIVE? is true, return only native
1145 references; otherwise, return only non-native references."
1146 ;; TODO: Return <gexp-input> records instead of tuples.
1147 (define (add-reference-inputs ref result)
1149 (($ <gexp-input> (? gexp? exp) _ #t)
1151 (append (gexp-inputs exp)
1152 (gexp-inputs exp #:native? #t)
1155 (($ <gexp-input> (? gexp? exp) _ #f)
1156 (append (gexp-inputs exp #:native? native?)
1158 (($ <gexp-input> (? string? str))
1159 (if (direct-store-path? str)
1160 (cons `(,str) result)
1162 (($ <gexp-input> (? struct? thing) output n?)
1163 (if (and (eqv? n? native?) (lookup-compiler thing))
1164 ;; THING is a derivation, or a package, or an origin, etc.
1165 (cons `(,thing ,output) result)
1167 (($ <gexp-input> (lst ...) output n?)
1168 (fold-right add-reference-inputs result
1169 ;; XXX: For now, automatically convert LST to a list of
1170 ;; gexp-inputs. Inherit N?.
1173 (%gexp-input (gexp-input-thing x)
1174 (gexp-input-output x)
1177 (%gexp-input x "out" n?)))
1180 ;; Ignore references to other kinds of objects.
1183 (fold-right add-reference-inputs
1185 (gexp-references exp)))
1187 (define gexp-native-inputs
1188 (cut gexp-inputs <> #:native? #t))
1190 (define (gexp-outputs exp)
1191 "Return the outputs referred to by EXP as a list of strings."
1192 (define (add-reference-output ref result)
1194 (($ <gexp-output> name)
1196 (($ <gexp-input> (? gexp? exp))
1197 (append (gexp-outputs exp) result))
1198 (($ <gexp-input> (lst ...) output native?)
1199 ;; XXX: Automatically convert LST.
1200 (add-reference-output (map (match-lambda
1201 ((? gexp-input? x) x)
1202 (x (%gexp-input x "out" native?)))
1206 (fold-right add-reference-output result lst))
1211 (add-reference-output (gexp-references exp) '())))
1213 (define* (gexp->sexp exp #:key
1214 (system (%current-system))
1215 (target (%current-target-system)))
1216 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1217 and in the current monad setting (system type, etc.)"
1218 (define* (reference->sexp ref #:optional native?)
1219 (with-monad %store-monad
1221 (($ <gexp-output> output)
1222 ;; Output file names are not known in advance but the daemon defines
1223 ;; an environment variable for each of them at build time, so use
1225 (return `((@ (guile) getenv) ,output)))
1226 (($ <gexp-input> (? gexp? exp) output n?)
1229 #:target (if (or n? native?) #f target)))
1230 (($ <gexp-input> (refs ...) output n?)
1233 ;; XXX: Automatically convert REF to an gexp-input.
1235 (if (gexp-input? ref)
1237 (%gexp-input ref "out" n?))
1240 (($ <gexp-input> (? struct? thing) output n?)
1241 (let ((target (if (or n? native?) #f target)))
1242 (lower+expand-object thing system
1245 (($ <gexp-input> (? self-quoting? x))
1248 (raise (condition (&gexp-input-error (input x)))))
1253 ((args (mapm %store-monad
1254 reference->sexp (gexp-references exp))))
1255 (return (apply (gexp-proc exp) args))))
1257 (define-syntax-rule (define-syntax-parameter-once name proc)
1258 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1259 ;; does not get redefined. This works around a race condition in a
1260 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1261 (eval-when (load eval expand compile)
1263 (if (module-locally-bound? (current-module) 'name)
1264 (module-ref (current-module) 'name)
1265 (make-syntax-transformer 'name 'syntax-parameter
1268 (define-syntax-parameter-once current-imported-modules
1269 ;; Current list of imported modules.
1270 (identifier-syntax '()))
1272 (define-syntax-rule (with-imported-modules modules body ...)
1273 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1275 (syntax-parameterize ((current-imported-modules
1276 (identifier-syntax modules)))
1279 (define-syntax-parameter-once current-imported-extensions
1280 ;; Current list of extensions.
1281 (identifier-syntax '()))
1283 (define-syntax-rule (with-extensions extensions body ...)
1284 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1285 execution environment."
1286 (syntax-parameterize ((current-imported-extensions
1287 (identifier-syntax extensions)))
1292 (define (collect-escapes exp)
1293 ;; Return all the 'ungexp' present in EXP.
1294 (let loop ((exp exp)
1296 (syntax-case exp (ungexp
1299 ungexp-native-splicing)
1304 ((ungexp-splicing _ ...)
1306 ((ungexp-native _ ...)
1308 ((ungexp-native-splicing _ ...)
1311 (let ((result (loop #'exp0 result)))
1312 (loop #'exp result)))
1316 (define (escape->ref exp)
1317 ;; Turn 'ungexp' form EXP into a "reference".
1318 (syntax-case exp (ungexp ungexp-splicing
1319 ungexp-native ungexp-native-splicing
1322 #'(gexp-output "out"))
1323 ((ungexp output name)
1324 #'(gexp-output name))
1326 #'(%gexp-input thing "out" #f))
1327 ((ungexp drv-or-pkg out)
1328 #'(%gexp-input drv-or-pkg out #f))
1329 ((ungexp-splicing lst)
1330 #'(%gexp-input lst "out" #f))
1331 ((ungexp-native thing)
1332 #'(%gexp-input thing "out" #t))
1333 ((ungexp-native drv-or-pkg out)
1334 #'(%gexp-input drv-or-pkg out #t))
1335 ((ungexp-native-splicing lst)
1336 #'(%gexp-input lst "out" #t))))
1338 (define (substitute-ungexp exp substs)
1339 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1340 ;; the corresponding form in SUBSTS.
1341 (match (assoc exp substs)
1345 (with-syntax ((exp exp))
1346 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1348 (define (substitute-ungexp-splicing exp substs)
1351 (match (assoc #'exp substs)
1353 (with-syntax ((id id))
1355 #,(substitute-references #'(rest ...) substs))))
1357 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1360 (define (substitute-references exp substs)
1361 ;; Return a variant of EXP where all the cars of SUBSTS have been
1362 ;; replaced by the corresponding cdr.
1363 (syntax-case exp (ungexp ungexp-native
1364 ungexp-splicing ungexp-native-splicing)
1366 (substitute-ungexp exp substs))
1367 ((ungexp-native _ ...)
1368 (substitute-ungexp exp substs))
1369 (((ungexp-splicing _ ...) rest ...)
1370 (substitute-ungexp-splicing exp substs))
1371 (((ungexp-native-splicing _ ...) rest ...)
1372 (substitute-ungexp-splicing exp substs))
1374 #`(cons #,(substitute-references #'exp0 substs)
1375 #,(substitute-references #'exp substs)))
1378 (syntax-case s (ungexp output)
1380 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1381 (formals (generate-temporaries escapes))
1382 (sexp (substitute-references #'exp (zip escapes formals)))
1383 (refs (map escape->ref escapes)))
1384 #`(make-gexp (list #,@refs)
1385 current-imported-modules
1386 current-imported-extensions
1392 ;;; Module handling.
1395 (define %utils-module
1396 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1397 ;; other primitives below. Note: We give the file name relative to this
1398 ;; file you are currently reading; 'search-path' could return a file name
1399 ;; relative to the current working directory.
1400 (local-file "build/utils.scm"
1403 (define* (imported-files/derivation files
1404 #:key (name "file-import")
1406 (system (%current-system))
1407 (guile (%guile-for-build)))
1408 "Return a derivation that imports FILES into STORE. FILES must be a list
1409 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1410 resulting store path. FILE can be either a file name, or a file-like object,
1411 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1412 to the source files instead of copying them."
1415 ((final-path . (? string? file-name))
1416 (mlet %store-monad ((file (interned-file file-name
1417 (basename final-path))))
1418 (return (list final-path file))))
1419 ((final-path . file-like)
1420 (mlet %store-monad ((file (lower-object file-like system)))
1421 (return (list final-path file))))))
1423 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1427 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1428 (use-modules (ice-9 match))
1430 (mkdir (ungexp output)) (chdir (ungexp output))
1431 (for-each (match-lambda
1432 ((final-path store-path)
1433 (mkdir-p (dirname final-path))
1434 ((ungexp (if symlink? 'symlink 'copy-file))
1435 store-path final-path)))
1438 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1439 ;; exactly the same regardless of FILES: less disk space, and fewer
1440 ;; 'add-to-store' RPCs.
1441 (gexp->derivation name build
1443 #:guile-for-build guile
1446 ;; Avoid deprecation warnings about the use of the _IO*
1447 ;; constants in (guix build utils).
1449 '(("GUILE_WARN_DEPRECATED" . "no")))))
1451 (define* (imported-files files
1452 #:key (name "file-import")
1453 ;; The following parameters make sense when creating
1454 ;; an actual derivation.
1455 (system (%current-system))
1456 (guile (%guile-for-build)))
1457 "Import FILES into the store and return the resulting derivation or store
1458 file name (a derivation is created if and only if some elements of FILES are
1459 file-like objects and not local file names.) FILES must be a list
1460 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1461 resulting store path. FILE can be either a file name, or a file-like object,
1462 as returned by 'local-file' for example."
1463 (if (any (match-lambda
1464 ((_ . (? struct? source)) #t)
1467 (imported-files/derivation files #:name name
1468 #:symlink? derivation?
1469 #:system system #:guile guile)
1470 (interned-file-tree `(,name directory
1471 ,@(file-mapping->tree files)))))
1473 (define* (imported-modules modules
1474 #:key (name "module-import")
1475 (system (%current-system))
1476 (guile (%guile-for-build))
1477 (module-path %load-path))
1478 "Return a derivation that contains the source files of MODULES, a list of
1479 module names such as `(ice-9 q)'. All of MODULES must be either names of
1480 modules to be found in the MODULE-PATH search path, or a module name followed
1481 by an arrow followed by a file-like object. For example:
1483 (imported-modules `((guix build utils)
1485 ((guix config) => ,(scheme-file …))))
1487 In this example, the first two modules are taken from MODULE-PATH, and the
1488 last one is created from the given <scheme-file> object."
1489 (let ((files (map (match-lambda
1490 (((module ...) '=> file)
1491 (cons (module->source-file-name module)
1494 (let ((f (module->source-file-name module)))
1495 (cons f (search-path* module-path f)))))
1497 (imported-files files #:name name
1501 (define* (compiled-modules modules
1502 #:key (name "module-import-compiled")
1503 (system (%current-system))
1505 (guile (%guile-for-build))
1506 (module-path %load-path)
1508 (deprecation-warnings #f))
1509 "Return a derivation that builds a tree containing the `.go' files
1510 corresponding to MODULES. All the MODULES are built in a context where
1511 they can refer to each other. When TARGET is true, cross-compile MODULES for
1512 TARGET, a GNU triplet."
1513 (define total (length modules))
1515 (mlet %store-monad ((modules (imported-modules modules
1523 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1525 (use-modules (ice-9 ftw)
1529 (system base target)
1530 (system base compile))
1532 (define (regular? file)
1533 (not (member file '("." ".."))))
1535 (define (process-entry entry output processed)
1536 (if (file-is-directory? entry)
1537 (let ((output (string-append output "/" (basename entry))))
1539 (process-directory entry output processed))
1540 (let* ((base (basename entry ".scm"))
1541 (output (string-append output "/" base ".go")))
1542 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1543 (+ 1 processed (ungexp total))
1544 (ungexp (* total 2))
1549 (gexp ((with-target (ungexp target)
1552 #:output-file output
1554 %auto-compilation-options)))))
1555 (gexp ((compile-file entry
1556 #:output-file output
1557 #:opts %auto-compilation-options)))))
1561 (define (process-directory directory output processed)
1562 (let ((entries (map (cut string-append directory "/" <>)
1563 (scandir directory regular?))))
1564 (fold (cut process-entry <> output <>)
1568 (define* (load-from-directory directory
1569 #:optional (loaded 0))
1570 "Load all the source files found in DIRECTORY."
1571 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1572 (let ((entries (map (cut string-append directory "/" <>)
1573 (scandir directory regular?))))
1574 (fold (lambda (file loaded)
1575 (if (file-is-directory? file)
1576 (load-from-directory file loaded)
1578 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1579 (+ 1 loaded) (ungexp (* 2 total))
1581 (save-module-excursion
1583 (primitive-load file)))
1588 (setvbuf (current-output-port)
1589 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1592 ;; Capture 'mkdir-p'.
1593 (@ (guix build utils) mkdir-p))
1595 ;; Add EXTENSIONS to the search path.
1597 (append (map (lambda (extension)
1598 (string-append extension
1599 "/share/guile/site/"
1600 (effective-version)))
1601 '((ungexp-native-splicing extensions)))
1603 (set! %load-compiled-path
1604 (append (map (lambda (extension)
1605 (string-append extension "/lib/guile/"
1608 '((ungexp-native-splicing extensions)))
1609 %load-compiled-path))
1611 (set! %load-path (cons (ungexp modules) %load-path))
1613 ;; Above we loaded our own (guix build utils) but now we may need to
1614 ;; load a compile a different one. Thus, force a reload.
1615 (let ((utils (string-append (ungexp modules)
1616 "/guix/build/utils.scm")))
1617 (when (file-exists? utils)
1620 (mkdir (ungexp output))
1621 (chdir (ungexp modules))
1623 (load-from-directory ".")
1624 (process-directory "." (ungexp output) 0))))
1626 ;; TODO: Pass MODULES as an environment variable.
1627 (gexp->derivation name build
1629 #:guile-for-build guile
1632 (case deprecation-warnings
1634 '(("GUILE_WARN_DEPRECATED" . "no")))
1636 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1642 ;;; Convenience procedures.
1645 (define (default-guile)
1646 ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
1647 ;; programs returned by 'program-file' and we don't want to keep references
1648 ;; to several Guile packages). This module must not refer to (gnu …)
1649 ;; modules directly, to avoid circular dependencies, hence this hack.
1650 (module-ref (resolve-interface '(gnu packages guile))
1653 (define* (load-path-expression modules #:optional (path %load-path)
1654 #:key (extensions '()) system target)
1655 "Return as a monadic value a gexp that sets '%load-path' and
1656 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1657 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1658 (if (and (null? modules) (null? extensions))
1659 (with-monad %store-monad
1661 (mlet %store-monad ((modules (imported-modules modules
1664 (compiled (compiled-modules modules
1665 #:extensions extensions
1670 (gexp (eval-when (expand load eval)
1671 ;; Augment the load paths and delete duplicates. Do that
1672 ;; without loading (srfi srfi-1) or anything.
1673 (let ((extensions '((ungexp-splicing extensions)))
1674 (prepend (lambda (items lst)
1675 ;; This is O(N²) but N is typically small.
1676 (let loop ((items items)
1682 (delete (car items) lst))))))))
1684 (prepend (cons (ungexp modules)
1685 (map (lambda (extension)
1686 (string-append extension
1687 "/share/guile/site/"
1688 (effective-version)))
1691 (set! %load-compiled-path
1692 (prepend (cons (ungexp compiled)
1693 (map (lambda (extension)
1694 (string-append extension
1699 %load-compiled-path)))))))))
1701 (define* (gexp->script name exp
1702 #:key (guile (default-guile))
1703 (module-path %load-path)
1704 (system (%current-system))
1706 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1707 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1708 (mlet* %store-monad ((target (if (eq? target 'current)
1709 (current-target-system)
1712 (load-path-expression (gexp-modules exp)
1715 (gexp-extensions exp)
1718 (gexp->derivation name
1720 (call-with-output-file (ungexp output)
1722 ;; Note: that makes a long shebang. When the store
1723 ;; is /gnu/store, that fits within the 128-byte
1724 ;; limit imposed by Linux, but that may go beyond
1725 ;; when running tests.
1727 "#!~a/bin/guile --no-auto-compile~%!#~%"
1732 (gexp ((write '(ungexp set-load-path) port)))
1735 (write '(ungexp exp) port)
1736 (chmod port #o555))))
1739 #:module-path module-path
1741 ;; These derivations are not worth offloading or
1744 #:substitutable? #f)))
1746 (define* (gexp->file name exp #:key
1748 (module-path %load-path)
1750 (system (%current-system))
1752 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1753 is true, EXP is considered to be a list of expressions that will be spliced in
1756 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1757 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1758 Lookup EXP's modules in MODULE-PATH."
1759 (define modules (gexp-modules exp))
1760 (define extensions (gexp-extensions exp))
1763 ((target (if (eq? target 'current)
1764 (current-target-system)
1766 (no-load-path? -> (or (not set-load-path?)
1767 (and (null? modules)
1768 (null? extensions))))
1770 (load-path-expression modules module-path
1771 #:extensions extensions
1775 (gexp->derivation name
1777 (call-with-output-file (ungexp output)
1782 '(ungexp (if splice?
1784 (gexp ((ungexp exp)))))))))
1789 (gexp->derivation name
1791 (call-with-output-file (ungexp output)
1793 (write '(ungexp set-load-path) port)
1797 '(ungexp (if splice?
1799 (gexp ((ungexp exp)))))))))
1800 #:module-path module-path
1806 (define* (text-file* name #:rest text)
1807 "Return as a monadic value a derivation that builds a text file containing
1808 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1809 can be used in a gexp: packages, derivations, local file objects, etc. The
1810 resulting store file holds references to all these."
1812 (gexp (call-with-output-file (ungexp output "out")
1814 (display (string-append (ungexp-splicing text)) port)))))
1816 (gexp->derivation name builder
1818 #:substitutable? #f))
1820 (define* (mixed-text-file name #:rest text)
1821 "Return an object representing store file NAME containing TEXT. TEXT is a
1822 sequence of strings and file-like objects, as in:
1824 (mixed-text-file \"profile\"
1825 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1827 This is the declarative counterpart of 'text-file*'."
1829 (gexp (call-with-output-file (ungexp output "out")
1831 (display (string-append (ungexp-splicing text)) port)))))
1833 (computed-file name build))
1835 (define (file-union name files)
1836 "Return a <computed-file> that builds a directory containing all of FILES.
1837 Each item in FILES must be a two-element list where the first element is the
1838 file name to use in the new directory, and the second element is a gexp
1839 denoting the target file. Here's an example:
1842 `((\"hosts\" ,(plain-file \"hosts\"
1843 \"127.0.0.1 localhost\"))
1844 (\"bashrc\" ,(plain-file \"bashrc\"
1845 \"alias ls='ls --color'\"))
1846 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1848 This yields an 'etc' directory containing these two files."
1850 (with-imported-modules '((guix build utils))
1853 (use-modules (guix build utils))
1855 (mkdir (ungexp output))
1856 (chdir (ungexp output))
1862 ;; Stat the source to abort early if it does
1864 (stat (ungexp source))
1866 (mkdir-p (dirname (ungexp target)))
1867 (symlink (ungexp source)
1868 (ungexp target))))))
1871 (define* (directory-union name things
1872 #:key (copy? #f) (quiet? #f)
1873 (resolve-collision 'warn-about-collision))
1874 "Return a directory that is the union of THINGS, where THINGS is a list of
1875 file-like objects denoting directories. For example:
1877 (directory-union \"guile+emacs\" (list guile emacs))
1879 yields a directory that is the union of the 'guile' and 'emacs' packages.
1881 Call RESOLVE-COLLISION when several files collide, passing it the list of
1882 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1883 which case the colliding entry is skipped altogether.
1885 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1886 is true, the derivation will not print anything."
1889 (gexp (lambda (old new)
1890 (if (file-is-directory? old)
1892 (copy-file old new))))
1897 (gexp (%make-void-port "w"))
1898 (gexp (current-error-port))))
1902 ;; Only one thing; return it.
1906 (with-imported-modules '((guix build union))
1908 (use-modules (guix build union)
1909 (srfi srfi-1)) ;for 'first' and 'last'
1911 (union-build (ungexp output)
1914 #:log-port (ungexp log-port)
1915 #:symlink (ungexp symlink)
1917 (ungexp resolve-collision)))))))))
1921 ;;; Syntactic sugar.
1924 (eval-when (expand load eval)
1925 (define* (read-ungexp chr port #:optional native?)
1926 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1927 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1928 (define unquote-symbol
1929 (match (peek-char port)
1933 'ungexp-native-splicing
1942 (let ((str (symbol->string symbol)))
1943 (match (string-index-right str #\:)
1945 `(,unquote-symbol ,symbol))
1947 (let ((name (string->symbol (substring str 0 colon)))
1948 (output (substring str (+ colon 1))))
1949 `(,unquote-symbol ,name ,output))))))
1951 `(,unquote-symbol ,x))))
1953 (define (read-gexp chr port)
1954 "Read a 'gexp' form from PORT."
1955 `(gexp ,(read port)))
1957 ;; Extend the reader
1958 (read-hash-extend #\~ read-gexp)
1959 (read-hash-extend #\$ read-ungexp)
1960 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1962 ;;; gexp.scm ends here