1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix gexp)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix derivations)
25 #:use-module (guix grafts)
26 #:use-module (guix utils)
27 #:use-module (rnrs bytevectors)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (ice-9 match)
46 local-file-absolute-file-name
66 program-file-module-path
103 gexp-error-invalid-input))
107 ;;; This module implements "G-expressions", or "gexps". Gexps are like
108 ;;; S-expressions (sexps), with two differences:
110 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
111 ;;; replaced by the corresponding output file name; in addition, the
112 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
113 ;;; the native code of a given package, in case of cross-compilation;
115 ;;; 2. Gexps embed information about the derivations they refer to.
117 ;;; Gexps make it easy to write to files Scheme code that refers to store
118 ;;; items, or to write Scheme code to build derivations.
123 (define-record-type <gexp>
124 (make-gexp references modules extensions proc)
126 (references gexp-references) ;list of <gexp-input>
127 (modules gexp-self-modules) ;list of module names
128 (extensions gexp-self-extensions) ;list of lowerable things
129 (proc gexp-proc)) ;procedure
131 (define (write-gexp gexp port)
132 "Write GEXP on PORT."
133 (display "#<gexp " port)
135 ;; Try to write the underlying sexp. Now, this trick doesn't work when
136 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
137 ;; tries to use 'append' on that, which fails with wrong-type-arg.
139 (write (apply (gexp-proc gexp)
140 (gexp-references gexp))
143 (number->string (object-address gexp) 16)))
145 (set-record-type-printer! <gexp> write-gexp)
152 ;; Compiler for a type of objects that may be introduced in a gexp.
153 (define-record-type <gexp-compiler>
154 (gexp-compiler type lower expand)
156 (type gexp-compiler-type) ;record type descriptor
157 (lower gexp-compiler-lower)
158 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
160 (define-condition-type &gexp-error &error
163 (define-condition-type &gexp-input-error &gexp-error
165 (input gexp-error-invalid-input))
168 (define %gexp-compilers
169 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
170 (make-hash-table 20))
172 (define (default-expander thing obj output)
173 "This is the default expander for \"things\" that appear in gexps. It
174 returns its output file name of OBJ's OUTPUT."
177 (derivation->output-path drv output))
181 (define (register-compiler! compiler)
182 "Register COMPILER as a gexp compiler."
183 (hashq-set! %gexp-compilers
184 (gexp-compiler-type compiler) compiler))
186 (define (lookup-compiler object)
187 "Search for a compiler for OBJECT. Upon success, return the three argument
188 procedure to lower it; otherwise return #f."
189 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
190 gexp-compiler-lower))
192 (define (file-like? object)
193 "Return #t if OBJECT leads to a file in the store once unquoted in a
194 G-expression; otherwise return #f."
195 (and (struct? object) (->bool (lookup-compiler object))))
197 (define (lookup-expander object)
198 "Search for an expander for OBJECT. Upon success, return the three argument
199 procedure to expand it; otherwise return #f."
200 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
201 gexp-compiler-expand))
203 (define* (lower-object obj
204 #:optional (system (%current-system))
206 "Return as a value in %STORE-MONAD the derivation or store item
207 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
208 OBJ must be an object that has an associated gexp compiler, such as a
210 (match (lookup-compiler obj)
212 (raise (condition (&gexp-input-error (input obj)))))
214 ;; Cache in STORE the result of lowering OBJ.
215 (mlet %store-monad ((graft? (grafting?)))
216 (mcached (let ((lower (lookup-compiler obj)))
217 (lower obj system target))
219 system target graft?)))))
221 (define-syntax define-gexp-compiler
222 (syntax-rules (=> compiler expander)
223 "Define NAME as a compiler for objects matching PREDICATE encountered in
226 In the simplest form of the macro, BODY must return a derivation for PARAM, an
227 object that matches PREDICATE, for SYSTEM and TARGET (the latter of which is
228 #f except when cross-compiling.)
230 The more elaborate form allows you to specify an expander:
232 (define-gexp-compiler something something?
233 compiler => (lambda (param system target) ...)
234 expander => (lambda (param drv output) ...))
236 The expander specifies how an object is converted to its sexp representation."
237 ((_ (name (param record-type) system target) body ...)
238 (define-gexp-compiler name record-type
239 compiler => (lambda (param system target) body ...)
240 expander => default-expander))
246 (gexp-compiler record-type compile expand))
247 (register-compiler! name)))))
249 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
250 ;; Derivations are the lowest-level representation, so this is the identity
252 (with-monad %store-monad
257 ;;; File declarations.
260 ;; A local file name. FILE is the file name the user entered, which can be a
261 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
262 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
263 ;; repeated 'stat' calls.
264 (define-record-type <local-file>
265 (%%local-file file absolute name recursive? select?)
267 (file local-file-file) ;string
268 (absolute %local-file-absolute-file-name) ;promise string
269 (name local-file-name) ;string
270 (recursive? local-file-recursive?) ;Boolean
271 (select? local-file-select?)) ;string stat -> Boolean
273 (define (true file stat) #t)
275 (define* (%local-file file promise #:optional (name (basename file))
276 #:key recursive? (select? true))
277 ;; This intermediate procedure is part of our ABI, but the underlying
278 ;; %%LOCAL-FILE is not.
279 (%%local-file file promise name recursive? select?))
281 (define (absolute-file-name file directory)
282 "Return the canonical absolute file name for FILE, which lives in the
283 vicinity of DIRECTORY."
285 (cond ((string-prefix? "/" file) file)
286 ((not directory) file)
287 ((string-prefix? "/" directory)
288 (string-append directory "/" file))
291 (define-syntax local-file
293 "Return an object representing local file FILE to add to the store; this
294 object can be used in a gexp. If FILE is a relative file name, it is looked
295 up relative to the source file where this form appears. FILE will be added to
296 the store under NAME--by default the base name of FILE.
298 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
299 designates a flat file and RECURSIVE? is true, its contents are added, and its
300 permission bits are kept.
302 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
303 where FILE is the entry's absolute file name and STAT is the result of
304 'lstat'; exclude entries for which SELECT? does not return true.
306 This is the declarative counterpart of the 'interned-file' monadic procedure.
307 It is implemented as a macro to capture the current source directory where it
312 (delay (absolute-file-name file (current-source-directory)))
315 #'(syntax-error "missing file name"))
318 ;; XXX: We could return #'(lambda (file . rest) ...). However,
319 ;; (syntax-source #'id) is #f so (current-source-directory) would not
320 ;; work. Thus, simply forbid this form.
322 "'local-file' is a macro and cannot be used like this")))))
324 (define (local-file-absolute-file-name file)
325 "Return the absolute file name for FILE, a <local-file> instance. A
326 'system-error' exception is raised if FILE could not be found."
327 (force (%local-file-absolute-file-name file)))
329 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
330 ;; "Compile" FILE by adding it to the store.
332 (($ <local-file> file (= force absolute) name recursive? select?)
333 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
334 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
335 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
336 ;; just throw an error, both of which are inconvenient.
337 (interned-file absolute name
338 #:recursive? recursive? #:select? select?))))
340 (define-record-type <plain-file>
341 (%plain-file name content references)
343 (name plain-file-name) ;string
344 (content plain-file-content) ;string or bytevector
345 (references plain-file-references)) ;list (currently unused)
347 (define (plain-file name content)
348 "Return an object representing a text file called NAME with the given
349 CONTENT (a string) to be added to the store.
351 This is the declarative counterpart of 'text-file'."
352 ;; XXX: For now just ignore 'references' because it's not clear how to use
353 ;; them in a declarative context.
354 (%plain-file name content '()))
356 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
357 ;; "Compile" FILE by adding it to the store.
359 (($ <plain-file> name (and (? string?) content) references)
360 (text-file name content references))
361 (($ <plain-file> name (and (? bytevector?) content) references)
362 (binary-file name content references))))
364 (define-record-type <computed-file>
365 (%computed-file name gexp guile options)
367 (name computed-file-name) ;string
368 (gexp computed-file-gexp) ;gexp
369 (guile computed-file-guile) ;<package>
370 (options computed-file-options)) ;list of arguments
372 (define* (computed-file name gexp
373 #:key guile (options '(#:local-build? #t)))
374 "Return an object representing the store item NAME, a file or directory
375 computed by GEXP. OPTIONS is a list of additional arguments to pass
376 to 'gexp->derivation'.
378 This is the declarative counterpart of 'gexp->derivation'."
379 (%computed-file name gexp guile options))
381 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
383 ;; Compile FILE by returning a derivation whose build expression is its
386 (($ <computed-file> name gexp guile options)
388 (mlet %store-monad ((guile (lower-object guile system
390 (apply gexp->derivation name gexp #:guile-for-build guile
391 #:system system #:target target options))
392 (apply gexp->derivation name gexp
393 #:system system #:target target options)))))
395 (define-record-type <program-file>
396 (%program-file name gexp guile path)
398 (name program-file-name) ;string
399 (gexp program-file-gexp) ;gexp
400 (guile program-file-guile) ;package
401 (path program-file-module-path)) ;list of strings
403 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
404 "Return an object representing the executable store item NAME that runs
405 GEXP. GUILE is the Guile package used to execute that script. Imported
406 modules of GEXP are looked up in MODULE-PATH.
408 This is the declarative counterpart of 'gexp->script'."
409 (%program-file name gexp guile module-path))
411 (define-gexp-compiler (program-file-compiler (file <program-file>)
413 ;; Compile FILE by returning a derivation that builds the script.
415 (($ <program-file> name gexp guile module-path)
416 (gexp->script name gexp
417 #:module-path module-path
418 #:guile (or guile (default-guile))))))
420 (define-record-type <scheme-file>
421 (%scheme-file name gexp splice?)
423 (name scheme-file-name) ;string
424 (gexp scheme-file-gexp) ;gexp
425 (splice? scheme-file-splice?)) ;Boolean
427 (define* (scheme-file name gexp #:key splice?)
428 "Return an object representing the Scheme file NAME that contains GEXP.
430 This is the declarative counterpart of 'gexp->file'."
431 (%scheme-file name gexp splice?))
433 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
435 ;; Compile FILE by returning a derivation that builds the file.
437 (($ <scheme-file> name gexp splice?)
438 (gexp->file name gexp #:splice? splice?))))
440 ;; Appending SUFFIX to BASE's output file name.
441 (define-record-type <file-append>
442 (%file-append base suffix)
444 (base file-append-base) ;<package> | <derivation> | ...
445 (suffix file-append-suffix)) ;list of strings
447 (define (write-file-append file port)
449 (($ <file-append> base suffix)
450 (format port "#<file-append ~s ~s>" base
451 (string-join suffix)))))
453 (set-record-type-printer! <file-append> write-file-append)
455 (define (file-append base . suffix)
456 "Return a <file-append> object that expands to the concatenation of BASE and
458 (%file-append base suffix))
460 (define-gexp-compiler file-append-compiler <file-append>
461 compiler => (lambda (obj system target)
463 (($ <file-append> base _)
464 (lower-object base system #:target target))))
465 expander => (lambda (obj lowered output)
467 (($ <file-append> base suffix)
468 (let* ((expand (lookup-expander base))
469 (base (expand base lowered output)))
470 (string-append base (string-concatenate suffix)))))))
474 ;;; Inputs & outputs.
477 ;; The input of a gexp.
478 (define-record-type <gexp-input>
479 (%gexp-input thing output native?)
481 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
482 (output gexp-input-output) ;string
483 (native? gexp-input-native?)) ;Boolean
485 (define (write-gexp-input input port)
487 (($ <gexp-input> thing output #f)
488 (format port "#<gexp-input ~s:~a>" thing output))
489 (($ <gexp-input> thing output #t)
490 (format port "#<gexp-input native ~s:~a>" thing output))))
492 (set-record-type-printer! <gexp-input> write-gexp-input)
494 (define* (gexp-input thing ;convenience procedure
495 #:optional (output "out")
497 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
498 whether this should be considered a \"native\" input or not."
499 (%gexp-input thing output native?))
501 ;; Reference to one of the derivation's outputs, for gexps used in
503 (define-record-type <gexp-output>
506 (name gexp-output-name))
508 (define (write-gexp-output output port)
510 (($ <gexp-output> name)
511 (format port "#<gexp-output ~a>" name))))
513 (set-record-type-printer! <gexp-output> write-gexp-output)
515 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
516 "Recurse on GEXP and the expressions it refers to, summing the items
517 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
518 second argument to 'delete-duplicates'."
521 (append (self-attribute gexp)
522 (append-map (match-lambda
523 (($ <gexp-input> (? gexp? exp))
524 (gexp-attribute exp self-attribute))
525 (($ <gexp-input> (lst ...))
526 (append-map (lambda (item)
534 (gexp-references gexp)))
536 '())) ;plain Scheme data type
538 (define (gexp-modules gexp)
539 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
540 false, meaning that GEXP is a plain Scheme object, return the empty list."
541 (define (module=? m1 m2)
542 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
543 ;; right-hand side may not be comparable with 'equal?': it's typically a
544 ;; file-like object that embeds a gexp, which in turn embeds closure;
545 ;; those closures may be 'eq?' when running compiled code but are unlikely
546 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
547 ;; avoid this discrepancy.
551 (((name2 ...) '=> _) (equal? name1 name2))
556 (gexp-attribute gexp gexp-self-modules module=?))
558 (define (gexp-extensions gexp)
559 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
560 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
562 (gexp-attribute gexp gexp-self-extensions))
564 (define* (lower-inputs inputs
566 "Turn any package from INPUTS into a derivation for SYSTEM; return the
567 corresponding input list as a monadic value. When TARGET is true, use it as
568 the cross-compilation target triplet."
569 (with-monad %store-monad
572 (((? struct? thing) sub-drv ...)
573 (mlet %store-monad ((drv (lower-object
574 thing system #:target target)))
575 (return `(,drv ,@sub-drv))))
580 (define* (lower-reference-graphs graphs #:key system target)
581 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
582 #:reference-graphs argument, lower it such that each INPUT is replaced by the
583 corresponding derivation."
585 (((file-names . inputs) ...)
586 (mlet %store-monad ((inputs (lower-inputs inputs
589 (return (map cons file-names inputs))))))
591 (define* (lower-references lst #:key system target)
592 "Based on LST, a list of output names and packages, return a list of output
593 names and file names suitable for the #:allowed-references argument to
595 (with-monad %store-monad
600 (($ <gexp-input> thing output native?)
601 (mlet %store-monad ((drv (lower-object thing system
604 (return (derivation->output-path drv output))))
606 (mlet %store-monad ((drv (lower-object thing system
608 (return (derivation->output-path drv))))))
610 (mapm %store-monad lower lst)))
612 (define default-guile-derivation
613 ;; Here we break the abstraction by talking to the higher-level layer.
614 ;; Thus, do the resolution lazily to hide the circular dependency.
616 (let ((iface (resolve-interface '(guix packages))))
617 (module-ref iface 'default-guile-derivation)))))
619 ((force proc) system))))
621 (define* (gexp->derivation name exp
623 system (target 'current)
624 hash hash-algo recursive?
627 (module-path %load-path)
628 (guile-for-build (%guile-for-build))
629 (effective-version "2.2")
632 allowed-references disallowed-references
634 local-build? (substitutable? #t)
638 (script-name (string-append name "-builder")))
639 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
640 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
641 TARGET is true, it is used as the cross-compilation target triplet for
642 packages referred to by EXP.
644 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
645 make MODULES available in the evaluation context of EXP; MODULES is a list of
646 names of Guile modules searched in MODULE-PATH to be copied in the store,
647 compiled, and made available in the load path during the execution of
648 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
650 EFFECTIVE-VERSION determines the string to use when adding extensions of
651 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
653 GRAFT? determines whether packages referred to by EXP should be grafted when
656 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
660 (FILE-NAME PACKAGE OUTPUT)
661 (FILE-NAME DERIVATION)
662 (FILE-NAME DERIVATION OUTPUT)
663 (FILE-NAME STORE-ITEM)
665 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
666 an input of the build process of EXP. In the build environment, each
667 FILE-NAME contains the reference graph of the corresponding item, in a simple
670 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
671 In the latter case, the list denotes store items that the result is allowed to
672 refer to. Any reference to another store item will lead to a build error.
673 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
674 referenced by the outputs.
676 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
677 compiling modules. It can be #f, #t, or 'detailed.
679 The other arguments are as for 'derivation'."
682 (append modules (gexp-modules exp))))
683 (define outputs (gexp-outputs exp))
685 (define (graphs-file-names graphs)
686 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
688 ;; TODO: Remove 'derivation?' special cases.
689 ((file-name (? derivation? drv))
690 (cons file-name (derivation->output-path drv)))
691 ((file-name (? derivation? drv) sub-drv)
692 (cons file-name (derivation->output-path drv sub-drv)))
694 (cons file-name thing)))
697 (define (extension-flags extension)
698 `("-L" ,(string-append (derivation->output-path extension)
699 "/share/guile/site/" effective-version)
700 "-C" ,(string-append (derivation->output-path extension)
701 "/lib/guile/" effective-version "/site-ccache")))
703 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
704 ;; '%current-target-system' to be looked up at >>=
706 (graft? (set-grafting graft?))
708 (system -> (or system (%current-system)))
709 (target -> (if (eq? target 'current)
710 (%current-target-system)
712 (normals (lower-inputs (gexp-inputs exp)
715 (natives (lower-inputs (gexp-native-inputs exp)
718 (inputs -> (append normals natives))
719 (sexp (gexp->sexp exp
722 (builder (text-file script-name
723 (object->string sexp)))
724 (extensions -> (gexp-extensions exp))
725 (exts (mapm %store-monad
727 (lower-object obj system))
729 (modules (if (pair? %modules)
730 (imported-modules %modules
732 #:module-path module-path
733 #:guile guile-for-build)
735 (compiled (if (pair? %modules)
736 (compiled-modules %modules
738 #:module-path module-path
739 #:extensions extensions
740 #:guile guile-for-build
741 #:deprecation-warnings
742 deprecation-warnings)
744 (graphs (if references-graphs
745 (lower-reference-graphs references-graphs
749 (allowed (if allowed-references
750 (lower-references allowed-references
754 (disallowed (if disallowed-references
755 (lower-references disallowed-references
759 (guile (if guile-for-build
760 (return guile-for-build)
761 (default-guile-derivation system))))
763 (set-grafting graft?) ;restore the initial setting
765 (string-append (derivation->output-path guile)
767 `("--no-auto-compile"
768 ,@(if (pair? %modules)
769 `("-L" ,(if (derivation? modules)
770 (derivation->output-path modules)
772 "-C" ,(derivation->output-path compiled))
774 ,@(append-map extension-flags exts)
782 `((,modules) (,compiled) ,@inputs)
786 (((_ . inputs) ...) inputs)
788 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
789 #:references-graphs (and=> graphs graphs-file-names)
790 #:allowed-references allowed
791 #:disallowed-references disallowed
792 #:leaked-env-vars leaked-env-vars
793 #:local-build? local-build?
794 #:substitutable? substitutable?
795 #:properties properties))))
797 (define* (gexp-inputs exp #:key native?)
798 "Return the input list for EXP. When NATIVE? is true, return only native
799 references; otherwise, return only non-native references."
800 (define (add-reference-inputs ref result)
802 (($ <gexp-input> (? gexp? exp) _ #t)
804 (append (gexp-inputs exp)
805 (gexp-inputs exp #:native? #t)
808 (($ <gexp-input> (? gexp? exp) _ #f)
809 (append (gexp-inputs exp #:native? native?)
811 (($ <gexp-input> (? string? str))
812 (if (direct-store-path? str)
813 (cons `(,str) result)
815 (($ <gexp-input> (? struct? thing) output n?)
816 (if (and (eqv? n? native?) (lookup-compiler thing))
817 ;; THING is a derivation, or a package, or an origin, etc.
818 (cons `(,thing ,output) result)
820 (($ <gexp-input> (lst ...) output n?)
821 (fold-right add-reference-inputs result
822 ;; XXX: For now, automatically convert LST to a list of
823 ;; gexp-inputs. Inherit N?.
826 (%gexp-input (gexp-input-thing x)
827 (gexp-input-output x)
830 (%gexp-input x "out" n?)))
833 ;; Ignore references to other kinds of objects.
836 (fold-right add-reference-inputs
838 (gexp-references exp)))
840 (define gexp-native-inputs
841 (cut gexp-inputs <> #:native? #t))
843 (define (gexp-outputs exp)
844 "Return the outputs referred to by EXP as a list of strings."
845 (define (add-reference-output ref result)
847 (($ <gexp-output> name)
849 (($ <gexp-input> (? gexp? exp))
850 (append (gexp-outputs exp) result))
851 (($ <gexp-input> (lst ...) output native?)
852 ;; XXX: Automatically convert LST.
853 (add-reference-output (map (match-lambda
854 ((? gexp-input? x) x)
855 (x (%gexp-input x "out" native?)))
859 (fold-right add-reference-output result lst))
864 (add-reference-output (gexp-references exp) '())))
866 (define* (gexp->sexp exp #:key
867 (system (%current-system))
868 (target (%current-target-system)))
869 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
870 and in the current monad setting (system type, etc.)"
871 (define* (reference->sexp ref #:optional native?)
872 (with-monad %store-monad
874 (($ <gexp-output> output)
875 ;; Output file names are not known in advance but the daemon defines
876 ;; an environment variable for each of them at build time, so use
878 (return `((@ (guile) getenv) ,output)))
879 (($ <gexp-input> (? gexp? exp) output n?)
882 #:target (if (or n? native?) #f target)))
883 (($ <gexp-input> (refs ...) output n?)
886 ;; XXX: Automatically convert REF to an gexp-input.
888 (if (gexp-input? ref)
890 (%gexp-input ref "out" n?))
893 (($ <gexp-input> (? struct? thing) output n?)
894 (let ((target (if (or n? native?) #f target))
895 (expand (lookup-expander thing)))
896 (mlet %store-monad ((obj (lower-object thing system
898 ;; OBJ must be either a derivation or a store file name.
899 (return (expand thing obj output)))))
906 ((args (mapm %store-monad
907 reference->sexp (gexp-references exp))))
908 (return (apply (gexp-proc exp) args))))
910 (define (syntax-location-string s)
911 "Return a string representing the source code location of S."
912 (let ((props (syntax-source s)))
914 (let ((file (assoc-ref props 'filename))
915 (line (and=> (assoc-ref props 'line) 1+))
916 (column (assoc-ref props 'column)))
918 (simple-format #f "~a:~a:~a"
920 (simple-format #f "~a:~a" line column)))
921 "<unknown location>")))
923 (define-syntax-parameter current-imported-modules
924 ;; Current list of imported modules.
925 (identifier-syntax '()))
927 (define-syntax-rule (with-imported-modules modules body ...)
928 "Mark the gexps defined in BODY... as requiring MODULES in their execution
930 (syntax-parameterize ((current-imported-modules
931 (identifier-syntax modules)))
934 (define-syntax-parameter current-imported-extensions
935 ;; Current list of extensions.
936 (identifier-syntax '()))
938 (define-syntax-rule (with-extensions extensions body ...)
939 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
940 execution environment."
941 (syntax-parameterize ((current-imported-extensions
942 (identifier-syntax extensions)))
947 (define (collect-escapes exp)
948 ;; Return all the 'ungexp' present in EXP.
951 (syntax-case exp (ungexp
954 ungexp-native-splicing)
959 ((ungexp-splicing _ ...)
961 ((ungexp-native _ ...)
963 ((ungexp-native-splicing _ ...)
966 (let ((result (loop #'exp0 result)))
967 (loop #'exp result)))
971 (define (escape->ref exp)
972 ;; Turn 'ungexp' form EXP into a "reference".
973 (syntax-case exp (ungexp ungexp-splicing
974 ungexp-native ungexp-native-splicing
977 #'(gexp-output "out"))
978 ((ungexp output name)
979 #'(gexp-output name))
981 #'(%gexp-input thing "out" #f))
982 ((ungexp drv-or-pkg out)
983 #'(%gexp-input drv-or-pkg out #f))
984 ((ungexp-splicing lst)
985 #'(%gexp-input lst "out" #f))
986 ((ungexp-native thing)
987 #'(%gexp-input thing "out" #t))
988 ((ungexp-native drv-or-pkg out)
989 #'(%gexp-input drv-or-pkg out #t))
990 ((ungexp-native-splicing lst)
991 #'(%gexp-input lst "out" #t))))
993 (define (substitute-ungexp exp substs)
994 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
995 ;; the corresponding form in SUBSTS.
996 (match (assoc exp substs)
1000 (with-syntax ((exp exp))
1001 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1003 (define (substitute-ungexp-splicing exp substs)
1006 (match (assoc #'exp substs)
1008 (with-syntax ((id id))
1010 #,(substitute-references #'(rest ...) substs))))
1012 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1015 (define (substitute-references exp substs)
1016 ;; Return a variant of EXP where all the cars of SUBSTS have been
1017 ;; replaced by the corresponding cdr.
1018 (syntax-case exp (ungexp ungexp-native
1019 ungexp-splicing ungexp-native-splicing)
1021 (substitute-ungexp exp substs))
1022 ((ungexp-native _ ...)
1023 (substitute-ungexp exp substs))
1024 (((ungexp-splicing _ ...) rest ...)
1025 (substitute-ungexp-splicing exp substs))
1026 (((ungexp-native-splicing _ ...) rest ...)
1027 (substitute-ungexp-splicing exp substs))
1029 #`(cons #,(substitute-references #'exp0 substs)
1030 #,(substitute-references #'exp substs)))
1033 (syntax-case s (ungexp output)
1035 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1036 (formals (generate-temporaries escapes))
1037 (sexp (substitute-references #'exp (zip escapes formals)))
1038 (refs (map escape->ref escapes)))
1039 #`(make-gexp (list #,@refs)
1040 current-imported-modules
1041 current-imported-extensions
1047 ;;; Module handling.
1051 (char-set-complement (char-set #\/)))
1053 (define (file-mapping->tree mapping)
1054 "Convert MAPPING, an alist like:
1056 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1058 to a tree suitable for 'interned-file-tree'."
1059 (let ((mapping (map (match-lambda
1060 ((destination . source)
1061 (cons (string-tokenize destination
1065 (fold (lambda (pair result)
1067 ((destination . source)
1068 (let loop ((destination destination)
1072 (let* ((mode (stat:mode (stat source)))
1073 (type (if (zero? (logand mode #o100))
1077 `(,type (file ,source))
1080 (let ((directory (assoc-ref result file)))
1085 (('directory . entries) entries)
1088 (alist-delete file result)
1093 (define %utils-module
1094 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1095 ;; other primitives below. Note: We give the file name relative to this
1096 ;; file you are currently reading; 'search-path' could return a file name
1097 ;; relative to the current working directory.
1098 (local-file "build/utils.scm"
1101 (define* (imported-files/derivation files
1102 #:key (name "file-import")
1104 (system (%current-system))
1105 (guile (%guile-for-build)))
1106 "Return a derivation that imports FILES into STORE. FILES must be a list
1107 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1108 resulting store path. FILE can be either a file name, or a file-like object,
1109 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1110 to the source files instead of copying them."
1113 ((final-path . (? string? file-name))
1114 (mlet %store-monad ((file (interned-file file-name
1115 (basename final-path))))
1116 (return (list final-path file))))
1117 ((final-path . file-like)
1118 (mlet %store-monad ((file (lower-object file-like system)))
1119 (return (list final-path file))))))
1121 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1125 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1126 (use-modules (ice-9 match))
1128 (mkdir (ungexp output)) (chdir (ungexp output))
1129 (for-each (match-lambda
1130 ((final-path store-path)
1131 (mkdir-p (dirname final-path))
1132 ((ungexp (if symlink? 'symlink 'copy-file))
1133 store-path final-path)))
1136 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1137 ;; exactly the same regardless of FILES: less disk space, and fewer
1138 ;; 'add-to-store' RPCs.
1139 (gexp->derivation name build
1141 #:guile-for-build guile
1144 ;; Avoid deprecation warnings about the use of the _IO*
1145 ;; constants in (guix build utils).
1147 '(("GUILE_WARN_DEPRECATED" . "no")))))
1149 (define* (imported-files files
1150 #:key (name "file-import")
1151 ;; The following parameters make sense when creating
1152 ;; an actual derivation.
1153 (system (%current-system))
1154 (guile (%guile-for-build)))
1155 "Import FILES into the store and return the resulting derivation or store
1156 file name (a derivation is created if and only if some elements of FILES are
1157 file-like objects and not local file names.) FILES must be a list
1158 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1159 resulting store path. FILE can be either a file name, or a file-like object,
1160 as returned by 'local-file' for example."
1161 (if (any (match-lambda
1162 ((_ . (? struct? source)) #t)
1165 (imported-files/derivation files #:name name
1166 #:symlink? derivation?
1167 #:system system #:guile guile)
1168 (interned-file-tree `(,name directory
1169 ,@(file-mapping->tree files)))))
1171 (define* (imported-modules modules
1172 #:key (name "module-import")
1173 (system (%current-system))
1174 (guile (%guile-for-build))
1175 (module-path %load-path))
1176 "Return a derivation that contains the source files of MODULES, a list of
1177 module names such as `(ice-9 q)'. All of MODULES must be either names of
1178 modules to be found in the MODULE-PATH search path, or a module name followed
1179 by an arrow followed by a file-like object. For example:
1181 (imported-modules `((guix build utils)
1183 ((guix config) => ,(scheme-file …))))
1185 In this example, the first two modules are taken from MODULE-PATH, and the
1186 last one is created from the given <scheme-file> object."
1187 (let ((files (map (match-lambda
1188 (((module ...) '=> file)
1189 (cons (module->source-file-name module)
1192 (let ((f (module->source-file-name module)))
1193 (cons f (search-path* module-path f)))))
1195 (imported-files files #:name name
1199 (define* (compiled-modules modules
1200 #:key (name "module-import-compiled")
1201 (system (%current-system))
1202 (guile (%guile-for-build))
1203 (module-path %load-path)
1205 (deprecation-warnings #f))
1206 "Return a derivation that builds a tree containing the `.go' files
1207 corresponding to MODULES. All the MODULES are built in a context where
1208 they can refer to each other."
1209 (define total (length modules))
1211 (mlet %store-monad ((modules (imported-modules modules
1219 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1221 (use-modules (ice-9 ftw)
1225 (system base compile))
1227 (define (regular? file)
1228 (not (member file '("." ".."))))
1230 (define (process-entry entry output processed)
1231 (if (file-is-directory? entry)
1232 (let ((output (string-append output "/" (basename entry))))
1234 (process-directory entry output processed))
1235 (let* ((base (basename entry ".scm"))
1236 (output (string-append output "/" base ".go")))
1237 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1238 (+ 1 processed) (ungexp total) entry)
1240 #:output-file output
1241 #:opts %auto-compilation-options)
1244 (define (process-directory directory output processed)
1245 (let ((entries (map (cut string-append directory "/" <>)
1246 (scandir directory regular?))))
1247 (fold (cut process-entry <> output <>)
1251 (setvbuf (current-output-port)
1252 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1255 ;; Capture 'mkdir-p'.
1256 (@ (guix build utils) mkdir-p))
1258 ;; Add EXTENSIONS to the search path.
1260 (append (map (lambda (extension)
1261 (string-append extension
1262 "/share/guile/site/"
1263 (effective-version)))
1264 '((ungexp-native-splicing extensions)))
1266 (set! %load-compiled-path
1267 (append (map (lambda (extension)
1268 (string-append extension "/lib/guile/"
1271 '((ungexp-native-splicing extensions)))
1272 %load-compiled-path))
1274 (set! %load-path (cons (ungexp modules) %load-path))
1276 ;; Above we loaded our own (guix build utils) but now we may need to
1277 ;; load a compile a different one. Thus, force a reload.
1278 (let ((utils (string-append (ungexp modules)
1279 "/guix/build/utils.scm")))
1280 (when (file-exists? utils)
1283 (mkdir (ungexp output))
1284 (chdir (ungexp modules))
1285 (process-directory "." (ungexp output) 0))))
1287 ;; TODO: Pass MODULES as an environment variable.
1288 (gexp->derivation name build
1290 #:guile-for-build guile
1293 (case deprecation-warnings
1295 '(("GUILE_WARN_DEPRECATED" . "no")))
1297 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1303 ;;; Convenience procedures.
1306 (define (default-guile)
1307 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1308 ;; programs returned by 'program-file' and we don't want to keep references
1309 ;; to several Guile packages). This module must not refer to (gnu …)
1310 ;; modules directly, to avoid circular dependencies, hence this hack.
1311 (module-ref (resolve-interface '(gnu packages guile))
1314 (define* (load-path-expression modules #:optional (path %load-path)
1315 #:key (extensions '()))
1316 "Return as a monadic value a gexp that sets '%load-path' and
1317 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1318 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1319 (if (and (null? modules) (null? extensions))
1320 (with-monad %store-monad
1322 (mlet %store-monad ((modules (imported-modules modules
1323 #:module-path path))
1324 (compiled (compiled-modules modules
1325 #:extensions extensions
1326 #:module-path path)))
1327 (return (gexp (eval-when (expand load eval)
1329 (cons (ungexp modules)
1330 (append (map (lambda (extension)
1331 (string-append extension
1332 "/share/guile/site/"
1333 (effective-version)))
1334 '((ungexp-native-splicing extensions)))
1336 (set! %load-compiled-path
1337 (cons (ungexp compiled)
1338 (append (map (lambda (extension)
1339 (string-append extension
1343 '((ungexp-native-splicing extensions)))
1344 %load-compiled-path)))))))))
1346 (define* (gexp->script name exp
1347 #:key (guile (default-guile))
1348 (module-path %load-path))
1349 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1350 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1351 (mlet %store-monad ((set-load-path
1352 (load-path-expression (gexp-modules exp)
1355 (gexp-extensions exp))))
1356 (gexp->derivation name
1358 (call-with-output-file (ungexp output)
1360 ;; Note: that makes a long shebang. When the store
1361 ;; is /gnu/store, that fits within the 128-byte
1362 ;; limit imposed by Linux, but that may go beyond
1363 ;; when running tests.
1365 "#!~a/bin/guile --no-auto-compile~%!#~%"
1370 (gexp ((write '(ungexp set-load-path) port)))
1373 (write '(ungexp exp) port)
1374 (chmod port #o555))))
1375 #:module-path module-path)))
1377 (define* (gexp->file name exp #:key
1379 (module-path %load-path)
1381 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1382 is true, EXP is considered to be a list of expressions that will be spliced in
1385 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1386 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1387 Lookup EXP's modules in MODULE-PATH."
1388 (define modules (gexp-modules exp))
1389 (define extensions (gexp-extensions exp))
1391 (if (or (not set-load-path?)
1392 (and (null? modules) (null? extensions)))
1393 (gexp->derivation name
1395 (call-with-output-file (ungexp output)
1397 (for-each (lambda (exp)
1399 '(ungexp (if splice?
1401 (gexp ((ungexp exp)))))))))
1403 #:substitutable? #f)
1404 (mlet %store-monad ((set-load-path
1405 (load-path-expression modules module-path
1406 #:extensions extensions)))
1407 (gexp->derivation name
1409 (call-with-output-file (ungexp output)
1411 (write '(ungexp set-load-path) port)
1412 (for-each (lambda (exp)
1414 '(ungexp (if splice?
1416 (gexp ((ungexp exp)))))))))
1417 #:module-path module-path
1419 #:substitutable? #f))))
1421 (define* (text-file* name #:rest text)
1422 "Return as a monadic value a derivation that builds a text file containing
1423 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1424 can be used in a gexp: packages, derivations, local file objects, etc. The
1425 resulting store file holds references to all these."
1427 (gexp (call-with-output-file (ungexp output "out")
1429 (display (string-append (ungexp-splicing text)) port)))))
1431 (gexp->derivation name builder
1433 #:substitutable? #f))
1435 (define* (mixed-text-file name #:rest text)
1436 "Return an object representing store file NAME containing TEXT. TEXT is a
1437 sequence of strings and file-like objects, as in:
1439 (mixed-text-file \"profile\"
1440 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1442 This is the declarative counterpart of 'text-file*'."
1444 (gexp (call-with-output-file (ungexp output "out")
1446 (display (string-append (ungexp-splicing text)) port)))))
1448 (computed-file name build))
1450 (define (file-union name files)
1451 "Return a <computed-file> that builds a directory containing all of FILES.
1452 Each item in FILES must be a two-element list where the first element is the
1453 file name to use in the new directory, and the second element is a gexp
1454 denoting the target file. Here's an example:
1457 `((\"hosts\" ,(plain-file \"hosts\"
1458 \"127.0.0.1 localhost\"))
1459 (\"bashrc\" ,(plain-file \"bashrc\"
1460 \"alias ls='ls --color'\"))
1461 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1463 This yields an 'etc' directory containing these two files."
1465 (with-imported-modules '((guix build utils))
1468 (use-modules (guix build utils))
1470 (mkdir (ungexp output))
1471 (chdir (ungexp output))
1477 ;; Stat the source to abort early if it does
1479 (stat (ungexp source))
1481 (mkdir-p (dirname (ungexp target)))
1482 (symlink (ungexp source)
1483 (ungexp target))))))
1486 (define* (directory-union name things
1487 #:key (copy? #f) (quiet? #f)
1488 (resolve-collision 'warn-about-collision))
1489 "Return a directory that is the union of THINGS, where THINGS is a list of
1490 file-like objects denoting directories. For example:
1492 (directory-union \"guile+emacs\" (list guile emacs))
1494 yields a directory that is the union of the 'guile' and 'emacs' packages.
1496 Call RESOLVE-COLLISION when several files collide, passing it the list of
1497 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1498 which case the colliding entry is skipped altogether.
1500 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1501 is true, the derivation will not print anything."
1504 (gexp (lambda (old new)
1505 (if (file-is-directory? old)
1507 (copy-file old new))))
1512 (gexp (%make-void-port "w"))
1513 (gexp (current-error-port))))
1517 ;; Only one thing; return it.
1521 (with-imported-modules '((guix build union))
1523 (use-modules (guix build union)
1524 (srfi srfi-1)) ;for 'first' and 'last'
1526 (union-build (ungexp output)
1529 #:log-port (ungexp log-port)
1530 #:symlink (ungexp symlink)
1532 (ungexp resolve-collision)))))))))
1536 ;;; Syntactic sugar.
1539 (eval-when (expand load eval)
1540 (define* (read-ungexp chr port #:optional native?)
1541 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1542 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1543 (define unquote-symbol
1544 (match (peek-char port)
1548 'ungexp-native-splicing
1557 (let ((str (symbol->string symbol)))
1558 (match (string-index-right str #\:)
1560 `(,unquote-symbol ,symbol))
1562 (let ((name (string->symbol (substring str 0 colon)))
1563 (output (substring str (+ colon 1))))
1564 `(,unquote-symbol ,name ,output))))))
1566 `(,unquote-symbol ,x))))
1568 (define (read-gexp chr port)
1569 "Read a 'gexp' form from PORT."
1570 `(gexp ,(read port)))
1572 ;; Extend the reader
1573 (read-hash-extend #\~ read-gexp)
1574 (read-hash-extend #\$ read-ungexp)
1575 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1577 ;;; gexp.scm ends here