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)
637 ;; TODO: This parameter is transitional; it's here
638 ;; to avoid a full rebuild. Remove it on the next
640 (pre-load-modules? #t)
643 (script-name (string-append name "-builder")))
644 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
645 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
646 TARGET is true, it is used as the cross-compilation target triplet for
647 packages referred to by EXP.
649 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
650 make MODULES available in the evaluation context of EXP; MODULES is a list of
651 names of Guile modules searched in MODULE-PATH to be copied in the store,
652 compiled, and made available in the load path during the execution of
653 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
655 EFFECTIVE-VERSION determines the string to use when adding extensions of
656 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
658 GRAFT? determines whether packages referred to by EXP should be grafted when
661 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
665 (FILE-NAME PACKAGE OUTPUT)
666 (FILE-NAME DERIVATION)
667 (FILE-NAME DERIVATION OUTPUT)
668 (FILE-NAME STORE-ITEM)
670 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
671 an input of the build process of EXP. In the build environment, each
672 FILE-NAME contains the reference graph of the corresponding item, in a simple
675 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
676 In the latter case, the list denotes store items that the result is allowed to
677 refer to. Any reference to another store item will lead to a build error.
678 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
679 referenced by the outputs.
681 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
682 compiling modules. It can be #f, #t, or 'detailed.
684 The other arguments are as for 'derivation'."
687 (append modules (gexp-modules exp))))
688 (define outputs (gexp-outputs exp))
690 (define (graphs-file-names graphs)
691 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
693 ;; TODO: Remove 'derivation?' special cases.
694 ((file-name (? derivation? drv))
695 (cons file-name (derivation->output-path drv)))
696 ((file-name (? derivation? drv) sub-drv)
697 (cons file-name (derivation->output-path drv sub-drv)))
699 (cons file-name thing)))
702 (define (extension-flags extension)
703 `("-L" ,(string-append (derivation->output-path extension)
704 "/share/guile/site/" effective-version)
705 "-C" ,(string-append (derivation->output-path extension)
706 "/lib/guile/" effective-version "/site-ccache")))
708 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
709 ;; '%current-target-system' to be looked up at >>=
711 (graft? (set-grafting graft?))
713 (system -> (or system (%current-system)))
714 (target -> (if (eq? target 'current)
715 (%current-target-system)
717 (normals (lower-inputs (gexp-inputs exp)
720 (natives (lower-inputs (gexp-native-inputs exp)
723 (inputs -> (append normals natives))
724 (sexp (gexp->sexp exp
727 (builder (text-file script-name
728 (object->string sexp)))
729 (extensions -> (gexp-extensions exp))
730 (exts (mapm %store-monad
732 (lower-object obj system))
734 (modules (if (pair? %modules)
735 (imported-modules %modules
737 #:module-path module-path
738 #:guile guile-for-build)
740 (compiled (if (pair? %modules)
741 (compiled-modules %modules
743 #:module-path module-path
744 #:extensions extensions
745 #:guile guile-for-build
748 #:deprecation-warnings
749 deprecation-warnings)
751 (graphs (if references-graphs
752 (lower-reference-graphs references-graphs
756 (allowed (if allowed-references
757 (lower-references allowed-references
761 (disallowed (if disallowed-references
762 (lower-references disallowed-references
766 (guile (if guile-for-build
767 (return guile-for-build)
768 (default-guile-derivation system))))
770 (set-grafting graft?) ;restore the initial setting
772 (string-append (derivation->output-path guile)
774 `("--no-auto-compile"
775 ,@(if (pair? %modules)
776 `("-L" ,(if (derivation? modules)
777 (derivation->output-path modules)
779 "-C" ,(derivation->output-path compiled))
781 ,@(append-map extension-flags exts)
789 `((,modules) (,compiled) ,@inputs)
793 (((_ . inputs) ...) inputs)
795 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
796 #:references-graphs (and=> graphs graphs-file-names)
797 #:allowed-references allowed
798 #:disallowed-references disallowed
799 #:leaked-env-vars leaked-env-vars
800 #:local-build? local-build?
801 #:substitutable? substitutable?
802 #:properties properties))))
804 (define* (gexp-inputs exp #:key native?)
805 "Return the input list for EXP. When NATIVE? is true, return only native
806 references; otherwise, return only non-native references."
807 (define (add-reference-inputs ref result)
809 (($ <gexp-input> (? gexp? exp) _ #t)
811 (append (gexp-inputs exp)
812 (gexp-inputs exp #:native? #t)
815 (($ <gexp-input> (? gexp? exp) _ #f)
816 (append (gexp-inputs exp #:native? native?)
818 (($ <gexp-input> (? string? str))
819 (if (direct-store-path? str)
820 (cons `(,str) result)
822 (($ <gexp-input> (? struct? thing) output n?)
823 (if (and (eqv? n? native?) (lookup-compiler thing))
824 ;; THING is a derivation, or a package, or an origin, etc.
825 (cons `(,thing ,output) result)
827 (($ <gexp-input> (lst ...) output n?)
828 (fold-right add-reference-inputs result
829 ;; XXX: For now, automatically convert LST to a list of
830 ;; gexp-inputs. Inherit N?.
833 (%gexp-input (gexp-input-thing x)
834 (gexp-input-output x)
837 (%gexp-input x "out" n?)))
840 ;; Ignore references to other kinds of objects.
843 (fold-right add-reference-inputs
845 (gexp-references exp)))
847 (define gexp-native-inputs
848 (cut gexp-inputs <> #:native? #t))
850 (define (gexp-outputs exp)
851 "Return the outputs referred to by EXP as a list of strings."
852 (define (add-reference-output ref result)
854 (($ <gexp-output> name)
856 (($ <gexp-input> (? gexp? exp))
857 (append (gexp-outputs exp) result))
858 (($ <gexp-input> (lst ...) output native?)
859 ;; XXX: Automatically convert LST.
860 (add-reference-output (map (match-lambda
861 ((? gexp-input? x) x)
862 (x (%gexp-input x "out" native?)))
866 (fold-right add-reference-output result lst))
871 (add-reference-output (gexp-references exp) '())))
873 (define* (gexp->sexp exp #:key
874 (system (%current-system))
875 (target (%current-target-system)))
876 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
877 and in the current monad setting (system type, etc.)"
878 (define* (reference->sexp ref #:optional native?)
879 (with-monad %store-monad
881 (($ <gexp-output> output)
882 ;; Output file names are not known in advance but the daemon defines
883 ;; an environment variable for each of them at build time, so use
885 (return `((@ (guile) getenv) ,output)))
886 (($ <gexp-input> (? gexp? exp) output n?)
889 #:target (if (or n? native?) #f target)))
890 (($ <gexp-input> (refs ...) output n?)
893 ;; XXX: Automatically convert REF to an gexp-input.
895 (if (gexp-input? ref)
897 (%gexp-input ref "out" n?))
900 (($ <gexp-input> (? struct? thing) output n?)
901 (let ((target (if (or n? native?) #f target))
902 (expand (lookup-expander thing)))
903 (mlet %store-monad ((obj (lower-object thing system
905 ;; OBJ must be either a derivation or a store file name.
906 (return (expand thing obj output)))))
913 ((args (mapm %store-monad
914 reference->sexp (gexp-references exp))))
915 (return (apply (gexp-proc exp) args))))
917 (define (syntax-location-string s)
918 "Return a string representing the source code location of S."
919 (let ((props (syntax-source s)))
921 (let ((file (assoc-ref props 'filename))
922 (line (and=> (assoc-ref props 'line) 1+))
923 (column (assoc-ref props 'column)))
925 (simple-format #f "~a:~a:~a"
927 (simple-format #f "~a:~a" line column)))
928 "<unknown location>")))
930 (define-syntax-rule (define-syntax-parameter-once name proc)
931 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
932 ;; does not get redefined. This works around a race condition in a
933 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
934 (eval-when (load eval expand compile)
936 (if (module-locally-bound? (current-module) 'name)
937 (module-ref (current-module) 'name)
938 (make-syntax-transformer 'name 'syntax-parameter
941 (define-syntax-parameter-once current-imported-modules
942 ;; Current list of imported modules.
943 (identifier-syntax '()))
945 (define-syntax-rule (with-imported-modules modules body ...)
946 "Mark the gexps defined in BODY... as requiring MODULES in their execution
948 (syntax-parameterize ((current-imported-modules
949 (identifier-syntax modules)))
952 (define-syntax-parameter-once current-imported-extensions
953 ;; Current list of extensions.
954 (identifier-syntax '()))
956 (define-syntax-rule (with-extensions extensions body ...)
957 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
958 execution environment."
959 (syntax-parameterize ((current-imported-extensions
960 (identifier-syntax extensions)))
965 (define (collect-escapes exp)
966 ;; Return all the 'ungexp' present in EXP.
969 (syntax-case exp (ungexp
972 ungexp-native-splicing)
977 ((ungexp-splicing _ ...)
979 ((ungexp-native _ ...)
981 ((ungexp-native-splicing _ ...)
984 (let ((result (loop #'exp0 result)))
985 (loop #'exp result)))
989 (define (escape->ref exp)
990 ;; Turn 'ungexp' form EXP into a "reference".
991 (syntax-case exp (ungexp ungexp-splicing
992 ungexp-native ungexp-native-splicing
995 #'(gexp-output "out"))
996 ((ungexp output name)
997 #'(gexp-output name))
999 #'(%gexp-input thing "out" #f))
1000 ((ungexp drv-or-pkg out)
1001 #'(%gexp-input drv-or-pkg out #f))
1002 ((ungexp-splicing lst)
1003 #'(%gexp-input lst "out" #f))
1004 ((ungexp-native thing)
1005 #'(%gexp-input thing "out" #t))
1006 ((ungexp-native drv-or-pkg out)
1007 #'(%gexp-input drv-or-pkg out #t))
1008 ((ungexp-native-splicing lst)
1009 #'(%gexp-input lst "out" #t))))
1011 (define (substitute-ungexp exp substs)
1012 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1013 ;; the corresponding form in SUBSTS.
1014 (match (assoc exp substs)
1018 (with-syntax ((exp exp))
1019 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1021 (define (substitute-ungexp-splicing exp substs)
1024 (match (assoc #'exp substs)
1026 (with-syntax ((id id))
1028 #,(substitute-references #'(rest ...) substs))))
1030 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1033 (define (substitute-references exp substs)
1034 ;; Return a variant of EXP where all the cars of SUBSTS have been
1035 ;; replaced by the corresponding cdr.
1036 (syntax-case exp (ungexp ungexp-native
1037 ungexp-splicing ungexp-native-splicing)
1039 (substitute-ungexp exp substs))
1040 ((ungexp-native _ ...)
1041 (substitute-ungexp exp substs))
1042 (((ungexp-splicing _ ...) rest ...)
1043 (substitute-ungexp-splicing exp substs))
1044 (((ungexp-native-splicing _ ...) rest ...)
1045 (substitute-ungexp-splicing exp substs))
1047 #`(cons #,(substitute-references #'exp0 substs)
1048 #,(substitute-references #'exp substs)))
1051 (syntax-case s (ungexp output)
1053 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1054 (formals (generate-temporaries escapes))
1055 (sexp (substitute-references #'exp (zip escapes formals)))
1056 (refs (map escape->ref escapes)))
1057 #`(make-gexp (list #,@refs)
1058 current-imported-modules
1059 current-imported-extensions
1065 ;;; Module handling.
1069 (char-set-complement (char-set #\/)))
1071 (define (file-mapping->tree mapping)
1072 "Convert MAPPING, an alist like:
1074 ((\"guix/build/utils.scm\" . \"…/utils.scm\"))
1076 to a tree suitable for 'interned-file-tree'."
1077 (let ((mapping (map (match-lambda
1078 ((destination . source)
1079 (cons (string-tokenize destination
1083 (fold (lambda (pair result)
1085 ((destination . source)
1086 (let loop ((destination destination)
1090 (let* ((mode (stat:mode (stat source)))
1091 (type (if (zero? (logand mode #o100))
1095 `(,type (file ,source))
1098 (let ((directory (assoc-ref result file)))
1103 (('directory . entries) entries)
1106 (alist-delete file result)
1111 (define %utils-module
1112 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1113 ;; other primitives below. Note: We give the file name relative to this
1114 ;; file you are currently reading; 'search-path' could return a file name
1115 ;; relative to the current working directory.
1116 (local-file "build/utils.scm"
1119 (define* (imported-files/derivation files
1120 #:key (name "file-import")
1122 (system (%current-system))
1123 (guile (%guile-for-build)))
1124 "Return a derivation that imports FILES into STORE. FILES must be a list
1125 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1126 resulting store path. FILE can be either a file name, or a file-like object,
1127 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1128 to the source files instead of copying them."
1131 ((final-path . (? string? file-name))
1132 (mlet %store-monad ((file (interned-file file-name
1133 (basename final-path))))
1134 (return (list final-path file))))
1135 ((final-path . file-like)
1136 (mlet %store-monad ((file (lower-object file-like system)))
1137 (return (list final-path file))))))
1139 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1143 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1144 (use-modules (ice-9 match))
1146 (mkdir (ungexp output)) (chdir (ungexp output))
1147 (for-each (match-lambda
1148 ((final-path store-path)
1149 (mkdir-p (dirname final-path))
1150 ((ungexp (if symlink? 'symlink 'copy-file))
1151 store-path final-path)))
1154 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1155 ;; exactly the same regardless of FILES: less disk space, and fewer
1156 ;; 'add-to-store' RPCs.
1157 (gexp->derivation name build
1159 #:guile-for-build guile
1162 ;; Avoid deprecation warnings about the use of the _IO*
1163 ;; constants in (guix build utils).
1165 '(("GUILE_WARN_DEPRECATED" . "no")))))
1167 (define* (imported-files files
1168 #:key (name "file-import")
1169 ;; The following parameters make sense when creating
1170 ;; an actual derivation.
1171 (system (%current-system))
1172 (guile (%guile-for-build)))
1173 "Import FILES into the store and return the resulting derivation or store
1174 file name (a derivation is created if and only if some elements of FILES are
1175 file-like objects and not local file names.) FILES must be a list
1176 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1177 resulting store path. FILE can be either a file name, or a file-like object,
1178 as returned by 'local-file' for example."
1179 (if (any (match-lambda
1180 ((_ . (? struct? source)) #t)
1183 (imported-files/derivation files #:name name
1184 #:symlink? derivation?
1185 #:system system #:guile guile)
1186 (interned-file-tree `(,name directory
1187 ,@(file-mapping->tree files)))))
1189 (define* (imported-modules modules
1190 #:key (name "module-import")
1191 (system (%current-system))
1192 (guile (%guile-for-build))
1193 (module-path %load-path))
1194 "Return a derivation that contains the source files of MODULES, a list of
1195 module names such as `(ice-9 q)'. All of MODULES must be either names of
1196 modules to be found in the MODULE-PATH search path, or a module name followed
1197 by an arrow followed by a file-like object. For example:
1199 (imported-modules `((guix build utils)
1201 ((guix config) => ,(scheme-file …))))
1203 In this example, the first two modules are taken from MODULE-PATH, and the
1204 last one is created from the given <scheme-file> object."
1205 (let ((files (map (match-lambda
1206 (((module ...) '=> file)
1207 (cons (module->source-file-name module)
1210 (let ((f (module->source-file-name module)))
1211 (cons f (search-path* module-path f)))))
1213 (imported-files files #:name name
1217 (define* (compiled-modules modules
1218 #:key (name "module-import-compiled")
1219 (system (%current-system))
1220 (guile (%guile-for-build))
1221 (module-path %load-path)
1223 (deprecation-warnings #f)
1225 ;; TODO: This flag is here to prevent a full
1226 ;; rebuild. Remove it on the next rebuild cycle.
1227 (pre-load-modules? #t))
1228 "Return a derivation that builds a tree containing the `.go' files
1229 corresponding to MODULES. All the MODULES are built in a context where
1230 they can refer to each other."
1231 (define total (length modules))
1233 (mlet %store-monad ((modules (imported-modules modules
1241 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1243 (use-modules (ice-9 ftw)
1247 (system base compile))
1249 (define (regular? file)
1250 (not (member file '("." ".."))))
1252 (define (process-entry entry output processed)
1253 (if (file-is-directory? entry)
1254 (let ((output (string-append output "/" (basename entry))))
1256 (process-directory entry output processed))
1257 (let* ((base (basename entry ".scm"))
1258 (output (string-append output "/" base ".go")))
1259 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1261 (ungexp-splicing (if pre-load-modules?
1262 (gexp ((ungexp total)))
1264 (ungexp (* total (if pre-load-modules? 2 1)))
1267 #:output-file output
1268 #:opts %auto-compilation-options)
1271 (define (process-directory directory output processed)
1272 (let ((entries (map (cut string-append directory "/" <>)
1273 (scandir directory regular?))))
1274 (fold (cut process-entry <> output <>)
1278 (setvbuf (current-output-port)
1279 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1282 ;; Capture 'mkdir-p'.
1283 (@ (guix build utils) mkdir-p))
1285 ;; Add EXTENSIONS to the search path.
1287 (append (map (lambda (extension)
1288 (string-append extension
1289 "/share/guile/site/"
1290 (effective-version)))
1291 '((ungexp-native-splicing extensions)))
1293 (set! %load-compiled-path
1294 (append (map (lambda (extension)
1295 (string-append extension "/lib/guile/"
1298 '((ungexp-native-splicing extensions)))
1299 %load-compiled-path))
1301 (set! %load-path (cons (ungexp modules) %load-path))
1303 ;; Above we loaded our own (guix build utils) but now we may need to
1304 ;; load a compile a different one. Thus, force a reload.
1305 (let ((utils (string-append (ungexp modules)
1306 "/guix/build/utils.scm")))
1307 (when (file-exists? utils)
1310 (mkdir (ungexp output))
1311 (chdir (ungexp modules))
1314 (if pre-load-modules?
1315 (gexp ((define* (load-from-directory directory
1316 #:optional (loaded 0))
1317 "Load all the source files found in DIRECTORY."
1318 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1319 (let ((entries (map (cut string-append directory "/" <>)
1320 (scandir directory regular?))))
1321 (fold (lambda (file loaded)
1322 (if (file-is-directory? file)
1323 (load-from-directory file loaded)
1325 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1327 (ungexp (* 2 total))
1329 (save-module-excursion
1331 (primitive-load file)))
1336 (load-from-directory ".")))
1339 (process-directory "." (ungexp output) 0))))
1341 ;; TODO: Pass MODULES as an environment variable.
1342 (gexp->derivation name build
1344 #:guile-for-build guile
1347 (case deprecation-warnings
1349 '(("GUILE_WARN_DEPRECATED" . "no")))
1351 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1357 ;;; Convenience procedures.
1360 (define (default-guile)
1361 ;; Lazily resolve 'guile-2.2' (not 'guile-final' because this is for
1362 ;; programs returned by 'program-file' and we don't want to keep references
1363 ;; to several Guile packages). This module must not refer to (gnu …)
1364 ;; modules directly, to avoid circular dependencies, hence this hack.
1365 (module-ref (resolve-interface '(gnu packages guile))
1368 (define* (load-path-expression modules #:optional (path %load-path)
1369 #:key (extensions '()))
1370 "Return as a monadic value a gexp that sets '%load-path' and
1371 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1372 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1373 (if (and (null? modules) (null? extensions))
1374 (with-monad %store-monad
1376 (mlet %store-monad ((modules (imported-modules modules
1377 #:module-path path))
1378 (compiled (compiled-modules modules
1379 #:extensions extensions
1380 #:module-path path)))
1381 (return (gexp (eval-when (expand load eval)
1383 (cons (ungexp modules)
1384 (append (map (lambda (extension)
1385 (string-append extension
1386 "/share/guile/site/"
1387 (effective-version)))
1388 '((ungexp-native-splicing extensions)))
1390 (set! %load-compiled-path
1391 (cons (ungexp compiled)
1392 (append (map (lambda (extension)
1393 (string-append extension
1397 '((ungexp-native-splicing extensions)))
1398 %load-compiled-path)))))))))
1400 (define* (gexp->script name exp
1401 #:key (guile (default-guile))
1402 (module-path %load-path))
1403 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1404 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1405 (mlet %store-monad ((set-load-path
1406 (load-path-expression (gexp-modules exp)
1409 (gexp-extensions exp))))
1410 (gexp->derivation name
1412 (call-with-output-file (ungexp output)
1414 ;; Note: that makes a long shebang. When the store
1415 ;; is /gnu/store, that fits within the 128-byte
1416 ;; limit imposed by Linux, but that may go beyond
1417 ;; when running tests.
1419 "#!~a/bin/guile --no-auto-compile~%!#~%"
1424 (gexp ((write '(ungexp set-load-path) port)))
1427 (write '(ungexp exp) port)
1428 (chmod port #o555))))
1429 #:module-path module-path)))
1431 (define* (gexp->file name exp #:key
1433 (module-path %load-path)
1435 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1436 is true, EXP is considered to be a list of expressions that will be spliced in
1439 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1440 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1441 Lookup EXP's modules in MODULE-PATH."
1442 (define modules (gexp-modules exp))
1443 (define extensions (gexp-extensions exp))
1445 (if (or (not set-load-path?)
1446 (and (null? modules) (null? extensions)))
1447 (gexp->derivation name
1449 (call-with-output-file (ungexp output)
1451 (for-each (lambda (exp)
1453 '(ungexp (if splice?
1455 (gexp ((ungexp exp)))))))))
1457 #:substitutable? #f)
1458 (mlet %store-monad ((set-load-path
1459 (load-path-expression modules module-path
1460 #:extensions extensions)))
1461 (gexp->derivation name
1463 (call-with-output-file (ungexp output)
1465 (write '(ungexp set-load-path) port)
1466 (for-each (lambda (exp)
1468 '(ungexp (if splice?
1470 (gexp ((ungexp exp)))))))))
1471 #:module-path module-path
1473 #:substitutable? #f))))
1475 (define* (text-file* name #:rest text)
1476 "Return as a monadic value a derivation that builds a text file containing
1477 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1478 can be used in a gexp: packages, derivations, local file objects, etc. The
1479 resulting store file holds references to all these."
1481 (gexp (call-with-output-file (ungexp output "out")
1483 (display (string-append (ungexp-splicing text)) port)))))
1485 (gexp->derivation name builder
1487 #:substitutable? #f))
1489 (define* (mixed-text-file name #:rest text)
1490 "Return an object representing store file NAME containing TEXT. TEXT is a
1491 sequence of strings and file-like objects, as in:
1493 (mixed-text-file \"profile\"
1494 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1496 This is the declarative counterpart of 'text-file*'."
1498 (gexp (call-with-output-file (ungexp output "out")
1500 (display (string-append (ungexp-splicing text)) port)))))
1502 (computed-file name build))
1504 (define (file-union name files)
1505 "Return a <computed-file> that builds a directory containing all of FILES.
1506 Each item in FILES must be a two-element list where the first element is the
1507 file name to use in the new directory, and the second element is a gexp
1508 denoting the target file. Here's an example:
1511 `((\"hosts\" ,(plain-file \"hosts\"
1512 \"127.0.0.1 localhost\"))
1513 (\"bashrc\" ,(plain-file \"bashrc\"
1514 \"alias ls='ls --color'\"))
1515 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1517 This yields an 'etc' directory containing these two files."
1519 (with-imported-modules '((guix build utils))
1522 (use-modules (guix build utils))
1524 (mkdir (ungexp output))
1525 (chdir (ungexp output))
1531 ;; Stat the source to abort early if it does
1533 (stat (ungexp source))
1535 (mkdir-p (dirname (ungexp target)))
1536 (symlink (ungexp source)
1537 (ungexp target))))))
1540 (define* (directory-union name things
1541 #:key (copy? #f) (quiet? #f)
1542 (resolve-collision 'warn-about-collision))
1543 "Return a directory that is the union of THINGS, where THINGS is a list of
1544 file-like objects denoting directories. For example:
1546 (directory-union \"guile+emacs\" (list guile emacs))
1548 yields a directory that is the union of the 'guile' and 'emacs' packages.
1550 Call RESOLVE-COLLISION when several files collide, passing it the list of
1551 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1552 which case the colliding entry is skipped altogether.
1554 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1555 is true, the derivation will not print anything."
1558 (gexp (lambda (old new)
1559 (if (file-is-directory? old)
1561 (copy-file old new))))
1566 (gexp (%make-void-port "w"))
1567 (gexp (current-error-port))))
1571 ;; Only one thing; return it.
1575 (with-imported-modules '((guix build union))
1577 (use-modules (guix build union)
1578 (srfi srfi-1)) ;for 'first' and 'last'
1580 (union-build (ungexp output)
1583 #:log-port (ungexp log-port)
1584 #:symlink (ungexp symlink)
1586 (ungexp resolve-collision)))))))))
1590 ;;; Syntactic sugar.
1593 (eval-when (expand load eval)
1594 (define* (read-ungexp chr port #:optional native?)
1595 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1596 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1597 (define unquote-symbol
1598 (match (peek-char port)
1602 'ungexp-native-splicing
1611 (let ((str (symbol->string symbol)))
1612 (match (string-index-right str #\:)
1614 `(,unquote-symbol ,symbol))
1616 (let ((name (string->symbol (substring str 0 colon)))
1617 (output (substring str (+ colon 1))))
1618 `(,unquote-symbol ,name ,output))))))
1620 `(,unquote-symbol ,x))))
1622 (define (read-gexp chr port)
1623 "Read a 'gexp' form from PORT."
1624 `(gexp ,(read port)))
1626 ;; Extend the reader
1627 (read-hash-extend #\~ read-gexp)
1628 (read-hash-extend #\$ read-ungexp)
1629 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1631 ;;; gexp.scm ends here