gnu: emacs-matrix-client: Update to a0623667.
[guix.git] / guix / gexp.scm
blobf7c064297b7e8101a5a84442fec708fbf755c276
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>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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)
35   #:export (gexp
36             gexp?
37             with-imported-modules
38             with-extensions
40             gexp-input
41             gexp-input?
43             local-file
44             local-file?
45             local-file-file
46             local-file-absolute-file-name
47             local-file-name
48             local-file-recursive?
50             plain-file
51             plain-file?
52             plain-file-name
53             plain-file-content
55             computed-file
56             computed-file?
57             computed-file-name
58             computed-file-gexp
59             computed-file-options
61             program-file
62             program-file?
63             program-file-name
64             program-file-gexp
65             program-file-guile
66             program-file-module-path
68             scheme-file
69             scheme-file?
70             scheme-file-name
71             scheme-file-gexp
73             file-append
74             file-append?
75             file-append-base
76             file-append-suffix
78             load-path-expression
79             gexp-modules
81             gexp->derivation
82             gexp->file
83             gexp->script
84             text-file*
85             mixed-text-file
86             file-union
87             directory-union
88             imported-files
89             imported-modules
90             compiled-modules
92             define-gexp-compiler
93             gexp-compiler?
94             file-like?
95             lower-object
97             lower-inputs
99             &gexp-error
100             gexp-error?
101             &gexp-input-error
102             gexp-input-error?
103             gexp-error-invalid-input))
105 ;;; Commentary:
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.
120 ;;; Code:
122 ;; "G expressions".
123 (define-record-type <gexp>
124   (make-gexp references modules extensions proc)
125   gexp?
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.
138   (false-if-exception
139    (write (apply (gexp-proc gexp)
140                  (gexp-references gexp))
141           port))
142   (format port " ~a>"
143           (number->string (object-address gexp) 16)))
145 (set-record-type-printer! <gexp> write-gexp)
149 ;;; Methods.
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)
155   gexp-compiler?
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
161   gexp-error?)
163 (define-condition-type &gexp-input-error &gexp-error
164   gexp-input-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."
175   (match obj
176     ((? derivation? drv)
177      (derivation->output-path drv output))
178     ((? string? file)
179      file)))
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))
205                        #:key target)
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
209 <package>."
210   (match (lookup-compiler obj)
211     (#f
212      (raise (condition (&gexp-input-error (input obj)))))
213     (lower
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))
218                 obj
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
224 gexps.
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))
241     ((_ name record-type
242         compiler => compile
243         expander => expand)
244      (begin
245        (define name
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
251   ;; compiler.
252   (with-monad %store-monad
253     (return drv)))
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?)
266   local-file?
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."
284   (canonicalize-path
285    (cond ((string-prefix? "/" file) file)
286          ((not directory) file)
287          ((string-prefix? "/" directory)
288           (string-append directory "/" file))
289          (else file))))
291 (define-syntax local-file
292   (lambda (s)
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
308 appears."
309     (syntax-case s ()
310       ((_ file rest ...)
311        #'(%local-file file
312                       (delay (absolute-file-name file (current-source-directory)))
313                       rest ...))
314       ((_)
315        #'(syntax-error "missing file name"))
316       (id
317        (identifier? #'id)
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.
321        #'(syntax-error
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.
331   (match file
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)
342   plain-file?
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.
358   (match file
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)
366   computed-file?
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>)
382                                               system target)
383   ;; Compile FILE by returning a derivation whose build expression is its
384   ;; gexp.
385   (match file
386     (($ <computed-file> name gexp guile options)
387      (if guile
388          (mlet %store-monad ((guile (lower-object guile system
389                                                   #:target target)))
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)
397   program-file?
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>)
412                                              system target)
413   ;; Compile FILE by returning a derivation that builds the script.
414   (match file
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?)
422   scheme-file?
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>)
434                                             system target)
435   ;; Compile FILE by returning a derivation that builds the file.
436   (match 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)
443   file-append?
444   (base   file-append-base)                    ;<package> | <derivation> | ...
445   (suffix file-append-suffix))                 ;list of strings
447 (define (write-file-append file port)
448   (match file
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
457 SUFFIX."
458   (%file-append base suffix))
460 (define-gexp-compiler file-append-compiler <file-append>
461   compiler => (lambda (obj system target)
462                 (match obj
463                   (($ <file-append> base _)
464                    (lower-object base system #:target target))))
465   expander => (lambda (obj lowered output)
466                 (match obj
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?)
480   gexp-input?
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)
486   (match input
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")
496                      #:key native?)
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
502 ;; derivations.
503 (define-record-type <gexp-output>
504   (gexp-output name)
505   gexp-output?
506   (name gexp-output-name))
508 (define (write-gexp-output output port)
509   (match output
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'."
519   (if (gexp? gexp)
520       (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)
527                                             (if (gexp? item)
528                                                 (gexp-attribute item
529                                                                 self-attribute)
530                                                 '()))
531                                           lst))
532                              (_
533                               '()))
534                            (gexp-references gexp)))
535        equal?)
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.
548     (match m1
549       (((name1 ...) '=> _)
550        (match m2
551          (((name2 ...) '=> _) (equal? name1 name2))
552          (_ #f)))
553       (_
554        (equal? m1 m2))))
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
561 list."
562   (gexp-attribute gexp gexp-self-extensions))
564 (define* (lower-inputs inputs
565                        #:key system target)
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
570     (mapm %store-monad
571           (match-lambda
572             (((? struct? thing) sub-drv ...)
573              (mlet %store-monad ((drv (lower-object
574                                        thing system #:target target)))
575                (return `(,drv ,@sub-drv))))
576             (input
577              (return input)))
578           inputs)))
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."
584   (match graphs
585     (((file-names . inputs) ...)
586      (mlet %store-monad ((inputs (lower-inputs inputs
587                                                #:system system
588                                                #:target target)))
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
594 'derivation'."
595   (with-monad %store-monad
596     (define lower
597       (match-lambda
598        ((? string? output)
599         (return output))
600        (($ <gexp-input> thing output native?)
601         (mlet %store-monad ((drv (lower-object thing system
602                                                #:target (if native?
603                                                             #f target))))
604           (return (derivation->output-path drv output))))
605        (thing
606         (mlet %store-monad ((drv (lower-object thing system
607                                                #:target target)))
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.
615   (let ((proc (delay
616                 (let ((iface (resolve-interface '(guix packages))))
617                   (module-ref iface 'default-guile-derivation)))))
618     (lambda (system)
619       ((force proc) system))))
621 (define* (gexp->derivation name exp
622                            #:key
623                            system (target 'current)
624                            hash hash-algo recursive?
625                            (env-vars '())
626                            (modules '())
627                            (module-path %load-path)
628                            (guile-for-build (%guile-for-build))
629                            (effective-version "2.2")
630                            (graft? (%graft?))
631                            references-graphs
632                            allowed-references disallowed-references
633                            leaked-env-vars
634                            local-build? (substitutable? #t)
635                            (properties '())
637                            deprecation-warnings
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
654 applicable.
656 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
657 following forms:
659   (FILE-NAME PACKAGE)
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
668 text format.
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'."
680   (define %modules
681     (delete-duplicates
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.
687     (map (match-lambda
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)))
693            ((file-name thing)
694             (cons file-name thing)))
695          graphs))
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 >>=
705                        ;; time.
706                        (graft?    (set-grafting graft?))
708                        (system -> (or system (%current-system)))
709                        (target -> (if (eq? target 'current)
710                                       (%current-target-system)
711                                       target))
712                        (normals  (lower-inputs (gexp-inputs exp)
713                                                #:system system
714                                                #:target target))
715                        (natives  (lower-inputs (gexp-native-inputs exp)
716                                                #:system system
717                                                #:target #f))
718                        (inputs -> (append normals natives))
719                        (sexp     (gexp->sexp exp
720                                              #:system system
721                                              #:target target))
722                        (builder  (text-file script-name
723                                             (object->string sexp)))
724                        (extensions -> (gexp-extensions exp))
725                        (exts     (mapm %store-monad
726                                        (lambda (obj)
727                                          (lower-object obj system))
728                                        extensions))
729                        (modules  (if (pair? %modules)
730                                      (imported-modules %modules
731                                                        #:system system
732                                                        #:module-path module-path
733                                                        #:guile guile-for-build)
734                                      (return #f)))
735                        (compiled (if (pair? %modules)
736                                      (compiled-modules %modules
737                                                        #:system system
738                                                        #:module-path module-path
739                                                        #:extensions extensions
740                                                        #:guile guile-for-build
741                                                        #:deprecation-warnings
742                                                        deprecation-warnings)
743                                      (return #f)))
744                        (graphs   (if references-graphs
745                                      (lower-reference-graphs references-graphs
746                                                              #:system system
747                                                              #:target target)
748                                      (return #f)))
749                        (allowed  (if allowed-references
750                                      (lower-references allowed-references
751                                                        #:system system
752                                                        #:target target)
753                                      (return #f)))
754                        (disallowed (if disallowed-references
755                                        (lower-references disallowed-references
756                                                          #:system system
757                                                          #:target target)
758                                        (return #f)))
759                        (guile    (if guile-for-build
760                                      (return guile-for-build)
761                                      (default-guile-derivation system))))
762     (mbegin %store-monad
763       (set-grafting graft?)                       ;restore the initial setting
764       (raw-derivation name
765                       (string-append (derivation->output-path guile)
766                                      "/bin/guile")
767                       `("--no-auto-compile"
768                         ,@(if (pair? %modules)
769                               `("-L" ,(if (derivation? modules)
770                                           (derivation->output-path modules)
771                                           modules)
772                                 "-C" ,(derivation->output-path compiled))
773                               '())
774                         ,@(append-map extension-flags exts)
775                         ,builder)
776                       #:outputs outputs
777                       #:env-vars env-vars
778                       #:system system
779                       #:inputs `((,guile)
780                                  (,builder)
781                                  ,@(if modules
782                                        `((,modules) (,compiled) ,@inputs)
783                                        inputs)
784                                  ,@(map list exts)
785                                  ,@(match graphs
786                                      (((_ . inputs) ...) inputs)
787                                      (_ '())))
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)
801     (match ref
802       (($ <gexp-input> (? gexp? exp) _ #t)
803        (if native?
804            (append (gexp-inputs exp)
805                    (gexp-inputs exp #:native? #t)
806                    result)
807            result))
808       (($ <gexp-input> (? gexp? exp) _ #f)
809        (append (gexp-inputs exp #:native? native?)
810                result))
811       (($ <gexp-input> (? string? str))
812        (if (direct-store-path? str)
813            (cons `(,str) result)
814            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)
819            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?.
824                    (map (match-lambda
825                           ((? gexp-input? x)
826                            (%gexp-input (gexp-input-thing x)
827                                         (gexp-input-output x)
828                                         n?))
829                           (x
830                            (%gexp-input x "out" n?)))
831                         lst)))
832       (_
833        ;; Ignore references to other kinds of objects.
834        result)))
836   (fold-right add-reference-inputs
837               '()
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)
846     (match ref
847       (($ <gexp-output> name)
848        (cons name result))
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?)))
856                                   lst)
857                              result))
858       ((lst ...)
859        (fold-right add-reference-output result lst))
860       (_
861        result)))
863   (delete-duplicates
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
873       (match ref
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
877          ;; that trick.
878          (return `((@ (guile) getenv) ,output)))
879         (($ <gexp-input> (? gexp? exp) output n?)
880          (gexp->sexp exp
881                      #:system system
882                      #:target (if (or n? native?) #f target)))
883         (($ <gexp-input> (refs ...) output n?)
884          (mapm %store-monad
885                (lambda (ref)
886                  ;; XXX: Automatically convert REF to an gexp-input.
887                  (reference->sexp
888                   (if (gexp-input? ref)
889                       ref
890                       (%gexp-input ref "out" n?))
891                   (or n? native?)))
892                refs))
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
897                                                   #:target target)))
898              ;; OBJ must be either a derivation or a store file name.
899              (return (expand thing obj output)))))
900         (($ <gexp-input> x)
901          (return x))
902         (x
903          (return x)))))
905   (mlet %store-monad
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)))
913     (if props
914         (let ((file   (assoc-ref props 'filename))
915               (line   (and=> (assoc-ref props 'line) 1+))
916               (column (assoc-ref props 'column)))
917           (if file
918               (simple-format #f "~a:~a:~a"
919                              file line column)
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
929 environment."
930   (syntax-parameterize ((current-imported-modules
931                          (identifier-syntax modules)))
932     body ...))
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)))
943     body ...))
945 (define-syntax gexp
946   (lambda (s)
947     (define (collect-escapes exp)
948       ;; Return all the 'ungexp' present in EXP.
949       (let loop ((exp    exp)
950                  (result '()))
951         (syntax-case exp (ungexp
952                           ungexp-splicing
953                           ungexp-native
954                           ungexp-native-splicing)
955           ((ungexp _)
956            (cons exp result))
957           ((ungexp _ _)
958            (cons exp result))
959           ((ungexp-splicing _ ...)
960            (cons exp result))
961           ((ungexp-native _ ...)
962            (cons exp result))
963           ((ungexp-native-splicing _ ...)
964            (cons exp result))
965           ((exp0 . exp)
966            (let ((result (loop #'exp0 result)))
967              (loop  #'exp result)))
968           (_
969            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
975                         output)
976         ((ungexp output)
977          #'(gexp-output "out"))
978         ((ungexp output name)
979          #'(gexp-output name))
980         ((ungexp thing)
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)
997         ((_ id)
998          id)
999         (_                                        ;internal error
1000          (with-syntax ((exp exp))
1001            #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1003     (define (substitute-ungexp-splicing exp substs)
1004       (syntax-case exp ()
1005         ((exp rest ...)
1006          (match (assoc #'exp substs)
1007            ((_ id)
1008             (with-syntax ((id id))
1009               #`(append id
1010                         #,(substitute-references #'(rest ...) substs))))
1011            (_
1012             #'(syntax-error "error: no 'ungexp-splicing' substitution"
1013                             exp))))))
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)
1020         ((ungexp _ ...)
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))
1028         ((exp0 . exp)
1029          #`(cons #,(substitute-references #'exp0 substs)
1030                  #,(substitute-references #'exp substs)))
1031         (x #''x)))
1033     (syntax-case s (ungexp output)
1034       ((_ exp)
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
1042                       (lambda #,formals
1043                         #,sexp)))))))
1047 ;;; Module handling.
1050 (define %not-slash
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
1062                                                 %not-slash)
1063                                source)))
1064                       mapping)))
1065     (fold (lambda (pair result)
1066             (match pair
1067               ((destination . source)
1068                (let loop ((destination destination)
1069                           (result result))
1070                  (match destination
1071                    ((file)
1072                     (let* ((mode (stat:mode (stat source)))
1073                            (type (if (zero? (logand mode #o100))
1074                                      'regular
1075                                      'executable)))
1076                       (alist-cons file
1077                                   `(,type (file ,source))
1078                                   result)))
1079                    ((file rest ...)
1080                     (let ((directory (assoc-ref result file)))
1081                       (alist-cons file
1082                                   `(directory
1083                                     ,@(loop rest
1084                                             (match directory
1085                                               (('directory . entries) entries)
1086                                               (#f '()))))
1087                                   (if directory
1088                                       (alist-delete file result)
1089                                       result)))))))))
1090           '()
1091           mapping)))
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"
1099               "build-utils.scm"))
1101 (define* (imported-files/derivation files
1102                                     #:key (name "file-import")
1103                                     (symlink? #f)
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."
1111   (define file-pair
1112     (match-lambda
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)))
1122     (define build
1123       (gexp
1124        (begin
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)))
1134                    '(ungexp files)))))
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
1140                       #:system system
1141                       #:guile-for-build guile
1142                       #:local-build? #t
1144                       ;; Avoid deprecation warnings about the use of the _IO*
1145                       ;; constants in (guix build utils).
1146                       #:env-vars
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)
1163              (_ #f))
1164            files)
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)
1182                       (guix gcrypt)
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)
1190                              file))
1191                       ((module ...)
1192                        (let ((f (module->source-file-name module)))
1193                          (cons f (search-path* module-path f)))))
1194                     modules)))
1195     (imported-files files #:name name
1196                     #:system system
1197                     #:guile guile)))
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)
1204                            (extensions '())
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
1212                                                  #:system system
1213                                                  #:guile guile
1214                                                  #:module-path
1215                                                  module-path)))
1216     (define build
1217       (gexp
1218        (begin
1219          (primitive-load (ungexp %utils-module))  ;for 'mkdir-p'
1221          (use-modules (ice-9 ftw)
1222                       (ice-9 format)
1223                       (srfi srfi-1)
1224                       (srfi srfi-26)
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))))
1233                  (mkdir-p output)
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)
1239                  (compile-file entry
1240                                #:output-file output
1241                                #:opts %auto-compilation-options)
1242                  (+ 1 processed))))
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 <>)
1248                    processed
1249                    entries)))
1251          (setvbuf (current-output-port)
1252                   (cond-expand (guile-2.2 'line) (else _IOLBF)))
1254          (define mkdir-p
1255            ;; Capture 'mkdir-p'.
1256            (@ (guix build utils) mkdir-p))
1258          ;; Add EXTENSIONS to the search path.
1259          (set! %load-path
1260            (append (map (lambda (extension)
1261                           (string-append extension
1262                                          "/share/guile/site/"
1263                                          (effective-version)))
1264                         '((ungexp-native-splicing extensions)))
1265                    %load-path))
1266          (set! %load-compiled-path
1267            (append (map (lambda (extension)
1268                           (string-append extension "/lib/guile/"
1269                                          (effective-version)
1270                                          "/site-ccache"))
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)
1281              (load 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
1289                       #:system system
1290                       #:guile-for-build guile
1291                       #:local-build? #t
1292                       #:env-vars
1293                       (case deprecation-warnings
1294                         ((#f)
1295                          '(("GUILE_WARN_DEPRECATED" . "no")))
1296                         ((detailed)
1297                          '(("GUILE_WARN_DEPRECATED" . "detailed")))
1298                         (else
1299                          '())))))
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))
1312               'guile-2.2))
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
1321         (return #f))
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)
1328                         (set! %load-path
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)))
1335                                         %load-path)))
1336                         (set! %load-compiled-path
1337                           (cons (ungexp compiled)
1338                                 (append (map (lambda (extension)
1339                                                (string-append extension
1340                                                               "/lib/guile/"
1341                                                               (effective-version)
1342                                                               "/site-ccache"))
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)
1353                                              module-path
1354                                              #:extensions
1355                                              (gexp-extensions exp))))
1356     (gexp->derivation name
1357                       (gexp
1358                        (call-with-output-file (ungexp output)
1359                          (lambda (port)
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.
1364                            (format port
1365                                    "#!~a/bin/guile --no-auto-compile~%!#~%"
1366                                    (ungexp guile))
1368                            (ungexp-splicing
1369                             (if set-load-path
1370                                 (gexp ((write '(ungexp set-load-path) port)))
1371                                 (gexp ())))
1373                            (write '(ungexp exp) port)
1374                            (chmod port #o555))))
1375                       #:module-path module-path)))
1377 (define* (gexp->file name exp #:key
1378                      (set-load-path? #t)
1379                      (module-path %load-path)
1380                      (splice? #f))
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
1383 the resulting file.
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
1394                         (gexp
1395                          (call-with-output-file (ungexp output)
1396                            (lambda (port)
1397                              (for-each (lambda (exp)
1398                                          (write exp port))
1399                                        '(ungexp (if splice?
1400                                                     exp
1401                                                     (gexp ((ungexp exp)))))))))
1402                         #:local-build? #t
1403                         #:substitutable? #f)
1404       (mlet %store-monad ((set-load-path
1405                            (load-path-expression modules module-path
1406                                                  #:extensions extensions)))
1407         (gexp->derivation name
1408                           (gexp
1409                            (call-with-output-file (ungexp output)
1410                              (lambda (port)
1411                                (write '(ungexp set-load-path) port)
1412                                (for-each (lambda (exp)
1413                                            (write exp port))
1414                                          '(ungexp (if splice?
1415                                                       exp
1416                                                       (gexp ((ungexp exp)))))))))
1417                           #:module-path module-path
1418                           #:local-build? #t
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."
1426   (define builder
1427     (gexp (call-with-output-file (ungexp output "out")
1428             (lambda (port)
1429               (display (string-append (ungexp-splicing text)) port)))))
1431   (gexp->derivation name builder
1432                     #:local-build? #t
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*'."
1443   (define build
1444     (gexp (call-with-output-file (ungexp output "out")
1445             (lambda (port)
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:
1456   (file-union \"etc\"
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."
1464   (computed-file name
1465                  (with-imported-modules '((guix build utils))
1466                    (gexp
1467                     (begin
1468                       (use-modules (guix build utils))
1470                       (mkdir (ungexp output))
1471                       (chdir (ungexp output))
1472                       (ungexp-splicing
1473                        (map (match-lambda
1474                               ((target source)
1475                                (gexp
1476                                 (begin
1477                                   ;; Stat the source to abort early if it does
1478                                   ;; not exist.
1479                                   (stat (ungexp source))
1481                                   (mkdir-p (dirname (ungexp target)))
1482                                   (symlink (ungexp source)
1483                                            (ungexp target))))))
1484                             files)))))))
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."
1502   (define symlink
1503     (if copy?
1504         (gexp (lambda (old new)
1505                 (if (file-is-directory? old)
1506                     (symlink old new)
1507                     (copy-file old new))))
1508         (gexp symlink)))
1510   (define log-port
1511     (if quiet?
1512         (gexp (%make-void-port "w"))
1513         (gexp (current-error-port))))
1515   (match things
1516     ((one)
1517      ;; Only one thing; return it.
1518      one)
1519     (_
1520      (computed-file name
1521                     (with-imported-modules '((guix build union))
1522                       (gexp (begin
1523                               (use-modules (guix build union)
1524                                            (srfi srfi-1)) ;for 'first' and 'last'
1526                               (union-build (ungexp output)
1527                                            '(ungexp things)
1529                                            #:log-port (ungexp log-port)
1530                                            #:symlink (ungexp symlink)
1531                                            #:resolve-collision
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)
1545         (#\@
1546          (read-char port)
1547          (if native?
1548              'ungexp-native-splicing
1549              'ungexp-splicing))
1550         (_
1551          (if native?
1552              'ungexp-native
1553              'ungexp))))
1555     (match (read port)
1556       ((? symbol? symbol)
1557        (let ((str (symbol->string symbol)))
1558          (match (string-index-right str #\:)
1559            (#f
1560             `(,unquote-symbol ,symbol))
1561            (colon
1562             (let ((name   (string->symbol (substring str 0 colon)))
1563                   (output (substring str (+ colon 1))))
1564               `(,unquote-symbol ,name ,output))))))
1565       (x
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