system: image: Fix disk-image name.
[jackhill/guix/guix.git] / guix / gexp.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
4 ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (guix gexp)
23 #:use-module (guix store)
24 #:use-module (guix monads)
25 #:use-module (guix derivations)
26 #:use-module (guix grafts)
27 #:use-module (guix utils)
28 #:use-module (rnrs bytevectors)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
31 #:use-module (srfi srfi-9 gnu)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
36 #:export (gexp
37 gexp?
38 with-imported-modules
39 with-extensions
40 let-system
41
42 gexp-input
43 gexp-input?
44 gexp-input-thing
45 gexp-input-output
46 gexp-input-native?
47
48 local-file
49 local-file?
50 local-file-file
51 local-file-absolute-file-name
52 local-file-name
53 local-file-recursive?
54 local-file-select?
55
56 plain-file
57 plain-file?
58 plain-file-name
59 plain-file-content
60
61 computed-file
62 computed-file?
63 computed-file-name
64 computed-file-gexp
65 computed-file-options
66
67 program-file
68 program-file?
69 program-file-name
70 program-file-gexp
71 program-file-guile
72 program-file-module-path
73
74 scheme-file
75 scheme-file?
76 scheme-file-name
77 scheme-file-gexp
78
79 file-append
80 file-append?
81 file-append-base
82 file-append-suffix
83
84 raw-derivation-file
85 raw-derivation-file?
86
87 with-parameters
88 parameterized?
89
90 load-path-expression
91 gexp-modules
92
93 lower-gexp
94 lowered-gexp?
95 lowered-gexp-sexp
96 lowered-gexp-inputs
97 lowered-gexp-sources
98 lowered-gexp-guile
99 lowered-gexp-load-path
100 lowered-gexp-load-compiled-path
101
102 gexp->derivation
103 gexp->file
104 gexp->script
105 text-file*
106 mixed-text-file
107 file-union
108 directory-union
109 imported-files
110 imported-modules
111 compiled-modules
112
113 define-gexp-compiler
114 gexp-compiler?
115 file-like?
116 lower-object
117
118 lower-inputs
119
120 &gexp-error
121 gexp-error?
122 &gexp-input-error
123 gexp-input-error?
124 gexp-error-invalid-input))
125
126 ;;; Commentary:
127 ;;;
128 ;;; This module implements "G-expressions", or "gexps". Gexps are like
129 ;;; S-expressions (sexps), with two differences:
130 ;;;
131 ;;; 1. References (un-quotations) to derivations or packages in a gexp are
132 ;;; replaced by the corresponding output file name; in addition, the
133 ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
134 ;;; the native code of a given package, in case of cross-compilation;
135 ;;;
136 ;;; 2. Gexps embed information about the derivations they refer to.
137 ;;;
138 ;;; Gexps make it easy to write to files Scheme code that refers to store
139 ;;; items, or to write Scheme code to build derivations.
140 ;;;
141 ;;; Code:
142
143 ;; "G expressions".
144 (define-record-type <gexp>
145 (make-gexp references modules extensions proc)
146 gexp?
147 (references gexp-references) ;list of <gexp-input>
148 (modules gexp-self-modules) ;list of module names
149 (extensions gexp-self-extensions) ;list of lowerable things
150 (proc gexp-proc)) ;procedure
151
152 (define (write-gexp gexp port)
153 "Write GEXP on PORT."
154 (display "#<gexp " port)
155
156 ;; Try to write the underlying sexp. Now, this trick doesn't work when
157 ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
158 ;; tries to use 'append' on that, which fails with wrong-type-arg.
159 (false-if-exception
160 (write (apply (gexp-proc gexp)
161 (gexp-references gexp))
162 port))
163 (format port " ~a>"
164 (number->string (object-address gexp) 16)))
165
166 (set-record-type-printer! <gexp> write-gexp)
167
168 \f
169 ;;;
170 ;;; Methods.
171 ;;;
172
173 ;; Compiler for a type of objects that may be introduced in a gexp.
174 (define-record-type <gexp-compiler>
175 (gexp-compiler type lower expand)
176 gexp-compiler?
177 (type gexp-compiler-type) ;record type descriptor
178 (lower gexp-compiler-lower)
179 (expand gexp-compiler-expand)) ;#f | DRV -> sexp
180
181 (define-condition-type &gexp-error &error
182 gexp-error?)
183
184 (define-condition-type &gexp-input-error &gexp-error
185 gexp-input-error?
186 (input gexp-error-invalid-input))
187
188
189 (define %gexp-compilers
190 ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
191 (make-hash-table 20))
192
193 (define (default-expander thing obj output)
194 "This is the default expander for \"things\" that appear in gexps. It
195 returns its output file name of OBJ's OUTPUT."
196 (match obj
197 ((? derivation? drv)
198 (derivation->output-path drv output))
199 ((? string? file)
200 file)
201 ((? self-quoting? obj)
202 obj)))
203
204 (define (register-compiler! compiler)
205 "Register COMPILER as a gexp compiler."
206 (hashq-set! %gexp-compilers
207 (gexp-compiler-type compiler) compiler))
208
209 (define (lookup-compiler object)
210 "Search for a compiler for OBJECT. Upon success, return the three argument
211 procedure to lower it; otherwise return #f."
212 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
213 gexp-compiler-lower))
214
215 (define (file-like? object)
216 "Return #t if OBJECT leads to a file in the store once unquoted in a
217 G-expression; otherwise return #f."
218 (and (struct? object) (->bool (lookup-compiler object))))
219
220 (define (lookup-expander object)
221 "Search for an expander for OBJECT. Upon success, return the three argument
222 procedure to expand it; otherwise return #f."
223 (and=> (hashq-ref %gexp-compilers (struct-vtable object))
224 gexp-compiler-expand))
225
226 (define* (lower-object obj
227 #:optional (system (%current-system))
228 #:key (target 'current))
229 "Return as a value in %STORE-MONAD the derivation or store item
230 corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
231 OBJ must be an object that has an associated gexp compiler, such as a
232 <package>."
233 (mlet %store-monad ((target (if (eq? target 'current)
234 (current-target-system)
235 (return target)))
236 (graft? (grafting?)))
237 (let loop ((obj obj))
238 (match (lookup-compiler obj)
239 (#f
240 (raise (condition (&gexp-input-error (input obj)))))
241 (lower
242 ;; Cache in STORE the result of lowering OBJ.
243 (mcached (mlet %store-monad ((lowered (lower obj system target)))
244 (if (and (struct? lowered)
245 (not (derivation? lowered)))
246 (loop lowered)
247 (return lowered)))
248 obj
249 system target graft?))))))
250
251 (define* (lower+expand-object obj
252 #:optional (system (%current-system))
253 #:key target (output "out"))
254 "Return as a value in %STORE-MONAD the output of object OBJ expands to for
255 SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
256 expand to file names, but it's possible to expand to a plain data type."
257 (let loop ((obj obj)
258 (expand (and (struct? obj) (lookup-expander obj))))
259 (match (lookup-compiler obj)
260 (#f
261 (raise (condition (&gexp-input-error (input obj)))))
262 (lower
263 (mlet* %store-monad ((graft? (grafting?))
264 (lowered (mcached (lower obj system target)
265 obj
266 system target graft?)))
267 ;; LOWER might return something that needs to be further
268 ;; lowered.
269 (if (struct? lowered)
270 ;; If we lack an expander, delegate to that of LOWERED.
271 (if (not expand)
272 (loop lowered (lookup-expander lowered))
273 (return (expand obj lowered output)))
274 (if (not expand) ;self-quoting
275 (return lowered)
276 (return (expand obj lowered output)))))))))
277
278 (define-syntax define-gexp-compiler
279 (syntax-rules (=> compiler expander)
280 "Define NAME as a compiler for objects matching PREDICATE encountered in
281 gexps.
282
283 In the simplest form of the macro, BODY must return (1) a derivation for
284 a record of the specified type, for SYSTEM and TARGET (the latter of which is
285 #f except when cross-compiling), (2) another record that can itself be
286 compiled down to a derivation, or (3) an object of a primitive data type.
287
288 The more elaborate form allows you to specify an expander:
289
290 (define-gexp-compiler something-compiler <something>
291 compiler => (lambda (param system target) ...)
292 expander => (lambda (param drv output) ...))
293
294 The expander specifies how an object is converted to its sexp representation."
295 ((_ (name (param record-type) system target) body ...)
296 (define-gexp-compiler name record-type
297 compiler => (lambda (param system target) body ...)
298 expander => default-expander))
299 ((_ name record-type
300 compiler => compile
301 expander => expand)
302 (begin
303 (define name
304 (gexp-compiler record-type compile expand))
305 (register-compiler! name)))))
306
307 (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
308 ;; Derivations are the lowest-level representation, so this is the identity
309 ;; compiler.
310 (with-monad %store-monad
311 (return drv)))
312
313 ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
314 ;; words, this gives the raw ".drv" file instead of its build result.
315 (define-record-type <raw-derivation-file>
316 (raw-derivation-file obj)
317 raw-derivation-file?
318 (obj raw-derivation-file-object)) ;lowerable object
319
320 (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
321 compiler => (lambda (obj system target)
322 (mlet %store-monad ((obj (lower-object
323 (raw-derivation-file-object obj)
324 system #:target target)))
325 ;; Returning the .drv file name instead of the <derivation>
326 ;; record ensures that 'lower-gexp' will classify it as a
327 ;; "source" and not as an "input".
328 (return (if (derivation? obj)
329 (derivation-file-name obj)
330 obj))))
331 expander => (lambda (obj lowered output)
332 (if (derivation? lowered)
333 (derivation-file-name lowered)
334 lowered)))
335
336 \f
337 ;;;
338 ;;; System dependencies.
339 ;;;
340
341 ;; Binding form for the current system and cross-compilation target.
342 (define-record-type <system-binding>
343 (system-binding proc)
344 system-binding?
345 (proc system-binding-proc))
346
347 (define-syntax let-system
348 (syntax-rules ()
349 "Introduce a system binding in a gexp. The simplest form is:
350
351 (let-system system
352 (cond ((string=? system \"x86_64-linux\") ...)
353 (else ...)))
354
355 which binds SYSTEM to the currently targeted system. The second form is
356 similar, but it also shows the cross-compilation target:
357
358 (let-system (system target)
359 ...)
360
361 Here TARGET is bound to the cross-compilation triplet or #f."
362 ((_ (system target) exp0 exp ...)
363 (system-binding (lambda (system target)
364 exp0 exp ...)))
365 ((_ system exp0 exp ...)
366 (system-binding (lambda (system target)
367 exp0 exp ...)))))
368
369 (define-gexp-compiler system-binding-compiler <system-binding>
370 compiler => (lambda (binding system target)
371 (match binding
372 (($ <system-binding> proc)
373 (with-monad %store-monad
374 ;; PROC is expected to return a lowerable object.
375 ;; 'lower-object' takes care of residualizing it to a
376 ;; derivation or similar.
377 (return (proc system target))))))
378
379 ;; Delegate to the expander of the object returned by PROC.
380 expander => #f)
381
382 \f
383 ;;;
384 ;;; File declarations.
385 ;;;
386
387 ;; A local file name. FILE is the file name the user entered, which can be a
388 ;; relative file name, and ABSOLUTE is a promise that computes its canonical
389 ;; absolute file name. We keep it in a promise to compute it lazily and avoid
390 ;; repeated 'stat' calls.
391 (define-record-type <local-file>
392 (%%local-file file absolute name recursive? select?)
393 local-file?
394 (file local-file-file) ;string
395 (absolute %local-file-absolute-file-name) ;promise string
396 (name local-file-name) ;string
397 (recursive? local-file-recursive?) ;Boolean
398 (select? local-file-select?)) ;string stat -> Boolean
399
400 (define (true file stat) #t)
401
402 (define* (%local-file file promise #:optional (name (basename file))
403 #:key recursive? (select? true))
404 ;; This intermediate procedure is part of our ABI, but the underlying
405 ;; %%LOCAL-FILE is not.
406 (%%local-file file promise name recursive? select?))
407
408 (define (absolute-file-name file directory)
409 "Return the canonical absolute file name for FILE, which lives in the
410 vicinity of DIRECTORY."
411 (canonicalize-path
412 (cond ((string-prefix? "/" file) file)
413 ((not directory) file)
414 ((string-prefix? "/" directory)
415 (string-append directory "/" file))
416 (else file))))
417
418 (define-syntax local-file
419 (lambda (s)
420 "Return an object representing local file FILE to add to the store; this
421 object can be used in a gexp. If FILE is a relative file name, it is looked
422 up relative to the source file where this form appears. FILE will be added to
423 the store under NAME--by default the base name of FILE.
424
425 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
426 designates a flat file and RECURSIVE? is true, its contents are added, and its
427 permission bits are kept.
428
429 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
430 where FILE is the entry's absolute file name and STAT is the result of
431 'lstat'; exclude entries for which SELECT? does not return true.
432
433 This is the declarative counterpart of the 'interned-file' monadic procedure.
434 It is implemented as a macro to capture the current source directory where it
435 appears."
436 (syntax-case s ()
437 ((_ file rest ...)
438 (string? (syntax->datum #'file))
439 ;; FILE is a literal, so resolve it relative to the source directory.
440 #'(%local-file file
441 (delay (absolute-file-name file (current-source-directory)))
442 rest ...))
443 ((_ file rest ...)
444 ;; Resolve FILE relative to the current directory.
445 #'(%local-file file
446 (delay (absolute-file-name file (getcwd)))
447 rest ...))
448 ((_)
449 #'(syntax-error "missing file name"))
450 (id
451 (identifier? #'id)
452 ;; XXX: We could return #'(lambda (file . rest) ...). However,
453 ;; (syntax-source #'id) is #f so (current-source-directory) would not
454 ;; work. Thus, simply forbid this form.
455 #'(syntax-error
456 "'local-file' is a macro and cannot be used like this")))))
457
458 (define (local-file-absolute-file-name file)
459 "Return the absolute file name for FILE, a <local-file> instance. A
460 'system-error' exception is raised if FILE could not be found."
461 (force (%local-file-absolute-file-name file)))
462
463 (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
464 ;; "Compile" FILE by adding it to the store.
465 (match file
466 (($ <local-file> file (= force absolute) name recursive? select?)
467 ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
468 ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
469 ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
470 ;; just throw an error, both of which are inconvenient.
471 (interned-file absolute name
472 #:recursive? recursive? #:select? select?))))
473
474 (define-record-type <plain-file>
475 (%plain-file name content references)
476 plain-file?
477 (name plain-file-name) ;string
478 (content plain-file-content) ;string or bytevector
479 (references plain-file-references)) ;list (currently unused)
480
481 (define (plain-file name content)
482 "Return an object representing a text file called NAME with the given
483 CONTENT (a string) to be added to the store.
484
485 This is the declarative counterpart of 'text-file'."
486 ;; XXX: For now just ignore 'references' because it's not clear how to use
487 ;; them in a declarative context.
488 (%plain-file name content '()))
489
490 (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
491 ;; "Compile" FILE by adding it to the store.
492 (match file
493 (($ <plain-file> name (and (? string?) content) references)
494 (text-file name content references))
495 (($ <plain-file> name (and (? bytevector?) content) references)
496 (binary-file name content references))))
497
498 (define-record-type <computed-file>
499 (%computed-file name gexp guile options)
500 computed-file?
501 (name computed-file-name) ;string
502 (gexp computed-file-gexp) ;gexp
503 (guile computed-file-guile) ;<package>
504 (options computed-file-options)) ;list of arguments
505
506 (define* (computed-file name gexp
507 #:key guile (options '(#:local-build? #t)))
508 "Return an object representing the store item NAME, a file or directory
509 computed by GEXP. OPTIONS is a list of additional arguments to pass
510 to 'gexp->derivation'.
511
512 This is the declarative counterpart of 'gexp->derivation'."
513 (%computed-file name gexp guile options))
514
515 (define-gexp-compiler (computed-file-compiler (file <computed-file>)
516 system target)
517 ;; Compile FILE by returning a derivation whose build expression is its
518 ;; gexp.
519 (match file
520 (($ <computed-file> name gexp guile options)
521 (if guile
522 (mlet %store-monad ((guile (lower-object guile system
523 #:target target)))
524 (apply gexp->derivation name gexp #:guile-for-build guile
525 #:system system #:target target options))
526 (apply gexp->derivation name gexp
527 #:system system #:target target options)))))
528
529 (define-record-type <program-file>
530 (%program-file name gexp guile path)
531 program-file?
532 (name program-file-name) ;string
533 (gexp program-file-gexp) ;gexp
534 (guile program-file-guile) ;package
535 (path program-file-module-path)) ;list of strings
536
537 (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
538 "Return an object representing the executable store item NAME that runs
539 GEXP. GUILE is the Guile package used to execute that script. Imported
540 modules of GEXP are looked up in MODULE-PATH.
541
542 This is the declarative counterpart of 'gexp->script'."
543 (%program-file name gexp guile module-path))
544
545 (define-gexp-compiler (program-file-compiler (file <program-file>)
546 system target)
547 ;; Compile FILE by returning a derivation that builds the script.
548 (match file
549 (($ <program-file> name gexp guile module-path)
550 (gexp->script name gexp
551 #:module-path module-path
552 #:guile (or guile (default-guile))
553 #:system system
554 #:target target))))
555
556 (define-record-type <scheme-file>
557 (%scheme-file name gexp splice? load-path?)
558 scheme-file?
559 (name scheme-file-name) ;string
560 (gexp scheme-file-gexp) ;gexp
561 (splice? scheme-file-splice?) ;Boolean
562 (load-path? scheme-file-set-load-path?)) ;Boolean
563
564 (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
565 "Return an object representing the Scheme file NAME that contains GEXP.
566
567 This is the declarative counterpart of 'gexp->file'."
568 (%scheme-file name gexp splice? set-load-path?))
569
570 (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
571 system target)
572 ;; Compile FILE by returning a derivation that builds the file.
573 (match file
574 (($ <scheme-file> name gexp splice? set-load-path?)
575 (gexp->file name gexp
576 #:set-load-path? set-load-path?
577 #:splice? splice?
578 #:system system
579 #:target target))))
580
581 ;; Appending SUFFIX to BASE's output file name.
582 (define-record-type <file-append>
583 (%file-append base suffix)
584 file-append?
585 (base file-append-base) ;<package> | <derivation> | ...
586 (suffix file-append-suffix)) ;list of strings
587
588 (define (write-file-append file port)
589 (match file
590 (($ <file-append> base suffix)
591 (format port "#<file-append ~s ~s>" base
592 (string-join suffix)))))
593
594 (set-record-type-printer! <file-append> write-file-append)
595
596 (define (file-append base . suffix)
597 "Return a <file-append> object that expands to the concatenation of BASE and
598 SUFFIX."
599 (%file-append base suffix))
600
601 (define-gexp-compiler file-append-compiler <file-append>
602 compiler => (lambda (obj system target)
603 (match obj
604 (($ <file-append> base _)
605 (lower-object base system #:target target))))
606 expander => (lambda (obj lowered output)
607 (match obj
608 (($ <file-append> base suffix)
609 (let* ((expand (lookup-expander base))
610 (base (expand base lowered output)))
611 (string-append base (string-concatenate suffix)))))))
612
613 ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
614 ;; object lowering.
615 (define-record-type <parameterized>
616 (parameterized bindings thunk)
617 parameterized?
618 (bindings parameterized-bindings) ;list of parameter/value pairs
619 (thunk parameterized-thunk)) ;thunk
620
621 (define-syntax-rule (with-parameters ((param value) ...) body ...)
622 "Bind each PARAM to the corresponding VALUE for the extent during which BODY
623 is lowered. Consider this example:
624
625 (with-parameters ((%current-system \"x86_64-linux\"))
626 coreutils)
627
628 It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
629 x86_64-linux when COREUTILS is lowered."
630 (parameterized (list (list param (lambda () value)) ...)
631 (lambda ()
632 body ...)))
633
634 (define-gexp-compiler compile-parameterized <parameterized>
635 compiler =>
636 (lambda (parameterized system target)
637 (match (parameterized-bindings parameterized)
638 (((parameters values) ...)
639 (let ((fluids (map parameter-fluid parameters))
640 (thunk (parameterized-thunk parameterized)))
641 ;; Install the PARAMETERS for the dynamic extent of THUNK.
642 (with-fluids* fluids
643 (map (lambda (thunk) (thunk)) values)
644 (lambda ()
645 ;; Special-case '%current-system' and '%current-target-system' to
646 ;; make sure we get the desired effect.
647 (let ((system (if (memq %current-system parameters)
648 (%current-system)
649 system))
650 (target (if (memq %current-target-system parameters)
651 (%current-target-system)
652 target)))
653 (lower-object (thunk) system #:target target))))))))
654
655 expander => (lambda (parameterized lowered output)
656 (match (parameterized-bindings parameterized)
657 (((parameters values) ...)
658 (let ((fluids (map parameter-fluid parameters))
659 (thunk (parameterized-thunk parameterized)))
660 ;; Install the PARAMETERS for the dynamic extent of THUNK.
661 (with-fluids* fluids
662 (map (lambda (thunk) (thunk)) values)
663 (lambda ()
664 ;; Delegate to the expander of the wrapped object.
665 (let* ((base (thunk))
666 (expand (lookup-expander base)))
667 (expand base lowered output)))))))))
668
669 \f
670 ;;;
671 ;;; Inputs & outputs.
672 ;;;
673
674 ;; The input of a gexp.
675 (define-record-type <gexp-input>
676 (%gexp-input thing output native?)
677 gexp-input?
678 (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
679 (output gexp-input-output) ;string
680 (native? gexp-input-native?)) ;Boolean
681
682 (define (write-gexp-input input port)
683 (match input
684 (($ <gexp-input> thing output #f)
685 (format port "#<gexp-input ~s:~a>" thing output))
686 (($ <gexp-input> thing output #t)
687 (format port "#<gexp-input native ~s:~a>" thing output))))
688
689 (set-record-type-printer! <gexp-input> write-gexp-input)
690
691 (define* (gexp-input thing ;convenience procedure
692 #:optional (output "out")
693 #:key native?)
694 "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
695 whether this should be considered a \"native\" input or not."
696 (%gexp-input thing output native?))
697
698 ;; Reference to one of the derivation's outputs, for gexps used in
699 ;; derivations.
700 (define-record-type <gexp-output>
701 (gexp-output name)
702 gexp-output?
703 (name gexp-output-name))
704
705 (define (write-gexp-output output port)
706 (match output
707 (($ <gexp-output> name)
708 (format port "#<gexp-output ~a>" name))))
709
710 (set-record-type-printer! <gexp-output> write-gexp-output)
711
712 (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?))
713 "Recurse on GEXP and the expressions it refers to, summing the items
714 returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
715 second argument to 'delete-duplicates'."
716 (if (gexp? gexp)
717 (delete-duplicates
718 (append (self-attribute gexp)
719 (append-map (match-lambda
720 (($ <gexp-input> (? gexp? exp))
721 (gexp-attribute exp self-attribute))
722 (($ <gexp-input> (lst ...))
723 (append-map (lambda (item)
724 (if (gexp? item)
725 (gexp-attribute item
726 self-attribute)
727 '()))
728 lst))
729 (_
730 '()))
731 (gexp-references gexp)))
732 equal?)
733 '())) ;plain Scheme data type
734
735 (define (gexp-modules gexp)
736 "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
737 false, meaning that GEXP is a plain Scheme object, return the empty list."
738 (define (module=? m1 m2)
739 ;; Return #t when M1 equals M2. Special-case '=>' specs because their
740 ;; right-hand side may not be comparable with 'equal?': it's typically a
741 ;; file-like object that embeds a gexp, which in turn embeds closure;
742 ;; those closures may be 'eq?' when running compiled code but are unlikely
743 ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
744 ;; avoid this discrepancy.
745 (match m1
746 (((name1 ...) '=> _)
747 (match m2
748 (((name2 ...) '=> _) (equal? name1 name2))
749 (_ #f)))
750 (_
751 (equal? m1 m2))))
752
753 (gexp-attribute gexp gexp-self-modules module=?))
754
755 (define (gexp-extensions gexp)
756 "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
757 GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
758 list."
759 (gexp-attribute gexp gexp-self-extensions))
760
761 (define (self-quoting? x)
762 (letrec-syntax ((one-of (syntax-rules ()
763 ((_) #f)
764 ((_ pred rest ...)
765 (or (pred x)
766 (one-of rest ...))))))
767 (one-of symbol? string? keyword? pair? null? array?
768 number? boolean? char?)))
769
770 (define* (lower-inputs inputs
771 #:key system target)
772 "Turn any object from INPUTS into a derivation input for SYSTEM or a store
773 item (a \"source\"); return the corresponding input list as a monadic value.
774 When TARGET is true, use it as the cross-compilation target triplet."
775 (define (store-item? obj)
776 (and (string? obj) (store-path? obj)))
777
778 (define filterm
779 (lift1 (cut filter ->bool <>) %store-monad))
780
781 (with-monad %store-monad
782 (>>= (mapm/accumulate-builds
783 (match-lambda
784 (((? struct? thing) sub-drv ...)
785 (mlet %store-monad ((obj (lower-object
786 thing system #:target target)))
787 (return (match obj
788 ((? derivation? drv)
789 (let ((outputs (if (null? sub-drv)
790 '("out")
791 sub-drv)))
792 (derivation-input drv outputs)))
793 ((? store-item? item)
794 item)
795 ((? self-quoting?)
796 ;; Some inputs such as <system-binding> can lower to
797 ;; a self-quoting object that FILTERM will filter
798 ;; out.
799 #f)))))
800 (((? store-item? item))
801 (return item)))
802 inputs)
803 filterm)))
804
805 (define* (lower-reference-graphs graphs #:key system target)
806 "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
807 #:reference-graphs argument, lower it such that each INPUT is replaced by the
808 corresponding <derivation-input> or store item."
809 (match graphs
810 (((file-names . inputs) ...)
811 (mlet %store-monad ((inputs (lower-inputs inputs
812 #:system system
813 #:target target)))
814 (return (map cons file-names inputs))))))
815
816 (define* (lower-references lst #:key system target)
817 "Based on LST, a list of output names and packages, return a list of output
818 names and file names suitable for the #:allowed-references argument to
819 'derivation'."
820 (with-monad %store-monad
821 (define lower
822 (match-lambda
823 ((? string? output)
824 (return output))
825 (($ <gexp-input> thing output native?)
826 (mlet %store-monad ((drv (lower-object thing system
827 #:target (if native?
828 #f target))))
829 (return (derivation->output-path drv output))))
830 (thing
831 (mlet %store-monad ((drv (lower-object thing system
832 #:target target)))
833 (return (derivation->output-path drv))))))
834
835 (mapm/accumulate-builds lower lst)))
836
837 (define default-guile-derivation
838 ;; Here we break the abstraction by talking to the higher-level layer.
839 ;; Thus, do the resolution lazily to hide the circular dependency.
840 (let ((proc (delay
841 (let ((iface (resolve-interface '(guix packages))))
842 (module-ref iface 'default-guile-derivation)))))
843 (lambda (system)
844 ((force proc) system))))
845
846 ;; Representation of a gexp instantiated for a given target and system.
847 ;; It's an intermediate representation between <gexp> and <derivation>.
848 (define-record-type <lowered-gexp>
849 (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
850 lowered-gexp?
851 (sexp lowered-gexp-sexp) ;sexp
852 (inputs lowered-gexp-inputs) ;list of <derivation-input>
853 (sources lowered-gexp-sources) ;list of store items
854 (guile lowered-gexp-guile) ;<derivation-input> | #f
855 (load-path lowered-gexp-load-path) ;list of store items
856 (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
857
858 (define* (imported+compiled-modules modules system
859 #:key (extensions '())
860 deprecation-warnings guile
861 (module-path %load-path))
862 "Return a pair where the first element is the imported MODULES and the
863 second element is the derivation to compile them."
864 (mcached equal?
865 (mlet %store-monad ((modules (if (pair? modules)
866 (imported-modules modules
867 #:system system
868 #:module-path module-path)
869 (return #f)))
870 (compiled (if (pair? modules)
871 (compiled-modules modules
872 #:system system
873 #:module-path module-path
874 #:extensions extensions
875 #:guile guile
876 #:deprecation-warnings
877 deprecation-warnings)
878 (return #f))))
879 (return (cons modules compiled)))
880 modules
881 system extensions guile deprecation-warnings module-path))
882
883 (define* (lower-gexp exp
884 #:key
885 (module-path %load-path)
886 (system (%current-system))
887 (target 'current)
888 (graft? (%graft?))
889 (guile-for-build (%guile-for-build))
890 (effective-version "3.0")
891
892 deprecation-warnings)
893 "*Note: This API is subject to change; use at your own risk!*
894
895 Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
896 <lowered-gexp> ready to be used.
897
898 Lowered gexps are an intermediate representation that's useful for
899 applications that deal with gexps outside in a way that is disconnected from
900 derivations--e.g., code evaluated for its side effects."
901 (define %modules
902 (delete-duplicates (gexp-modules exp)))
903
904 (define (search-path modules extensions suffix)
905 (append (match modules
906 ((? derivation? drv)
907 (list (derivation->output-path drv)))
908 (#f
909 '())
910 ((? store-path? item)
911 (list item)))
912 (map (lambda (extension)
913 (string-append (match extension
914 ((? derivation? drv)
915 (derivation->output-path drv))
916 ((? store-path? item)
917 item))
918 suffix))
919 extensions)))
920
921 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
922 ;; '%current-target-system' to be looked up at >>=
923 ;; time.
924 (graft? (set-grafting graft?))
925
926 (system -> (or system (%current-system)))
927 (target -> (if (eq? target 'current)
928 (%current-target-system)
929 target))
930 (guile (if guile-for-build
931 (return guile-for-build)
932 (default-guile-derivation system)))
933 (normals (lower-inputs (gexp-inputs exp)
934 #:system system
935 #:target target))
936 (natives (lower-inputs (gexp-native-inputs exp)
937 #:system system
938 #:target #f))
939 (inputs -> (append normals natives))
940 (sexp (gexp->sexp exp
941 #:system system
942 #:target target))
943 (extensions -> (gexp-extensions exp))
944 (exts (mapm %store-monad
945 (lambda (obj)
946 (lower-object obj system
947 #:target #f))
948 extensions))
949 (modules+compiled (imported+compiled-modules
950 %modules system
951 #:extensions extensions
952 #:deprecation-warnings
953 deprecation-warnings
954 #:guile guile
955 #:module-path module-path))
956 (modules -> (car modules+compiled))
957 (compiled -> (cdr modules+compiled)))
958 (define load-path
959 (search-path modules exts
960 (string-append "/share/guile/site/" effective-version)))
961
962 (define load-compiled-path
963 (search-path compiled exts
964 (string-append "/lib/guile/" effective-version
965 "/site-ccache")))
966
967 (mbegin %store-monad
968 (set-grafting graft?) ;restore the initial setting
969 (return (lowered-gexp sexp
970 `(,@(if (derivation? modules)
971 (list (derivation-input modules))
972 '())
973 ,@(if compiled
974 (list (derivation-input compiled))
975 '())
976 ,@(map derivation-input exts)
977 ,@(filter derivation-input? inputs))
978 (filter string? (cons modules inputs))
979 (derivation-input guile '("out"))
980 load-path
981 load-compiled-path)))))
982
983 (define* (gexp->derivation name exp
984 #:key
985 system (target 'current)
986 hash hash-algo recursive?
987 (env-vars '())
988 (modules '())
989 (module-path %load-path)
990 (guile-for-build (%guile-for-build))
991 (effective-version "3.0")
992 (graft? (%graft?))
993 references-graphs
994 allowed-references disallowed-references
995 leaked-env-vars
996 local-build? (substitutable? #t)
997 (properties '())
998 deprecation-warnings
999 (script-name (string-append name "-builder")))
1000 "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
1001 derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
1002 TARGET is true, it is used as the cross-compilation target triplet for
1003 packages referred to by EXP.
1004
1005 MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
1006 make MODULES available in the evaluation context of EXP; MODULES is a list of
1007 names of Guile modules searched in MODULE-PATH to be copied in the store,
1008 compiled, and made available in the load path during the execution of
1009 EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
1010
1011 EFFECTIVE-VERSION determines the string to use when adding extensions of
1012 EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
1013
1014 GRAFT? determines whether packages referred to by EXP should be grafted when
1015 applicable.
1016
1017 When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
1018 following forms:
1019
1020 (FILE-NAME PACKAGE)
1021 (FILE-NAME PACKAGE OUTPUT)
1022 (FILE-NAME DERIVATION)
1023 (FILE-NAME DERIVATION OUTPUT)
1024 (FILE-NAME STORE-ITEM)
1025
1026 The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
1027 an input of the build process of EXP. In the build environment, each
1028 FILE-NAME contains the reference graph of the corresponding item, in a simple
1029 text format.
1030
1031 ALLOWED-REFERENCES must be either #f or a list of output names and packages.
1032 In the latter case, the list denotes store items that the result is allowed to
1033 refer to. Any reference to another store item will lead to a build error.
1034 Similarly for DISALLOWED-REFERENCES, which can list items that must not be
1035 referenced by the outputs.
1036
1037 DEPRECATION-WARNINGS determines whether to show deprecation warnings while
1038 compiling modules. It can be #f, #t, or 'detailed.
1039
1040 The other arguments are as for 'derivation'."
1041 (define outputs (gexp-outputs exp))
1042 (define requested-graft? graft?)
1043
1044 (define (graphs-file-names graphs)
1045 ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
1046 (map (match-lambda
1047 ((file-name . (? derivation-input? input))
1048 (cons file-name (first (derivation-input-output-paths input))))
1049 ((file-name . (? string? item))
1050 (cons file-name item)))
1051 graphs))
1052
1053 (define (add-modules exp modules)
1054 (if (null? modules)
1055 exp
1056 (make-gexp (gexp-references exp)
1057 (append modules (gexp-self-modules exp))
1058 (gexp-self-extensions exp)
1059 (gexp-proc exp))))
1060
1061 (mlet* %store-monad ( ;; The following binding forces '%current-system' and
1062 ;; '%current-target-system' to be looked up at >>=
1063 ;; time.
1064 (graft? (set-grafting graft?))
1065
1066 (system -> (or system (%current-system)))
1067 (target -> (if (eq? target 'current)
1068 (%current-target-system)
1069 target))
1070 (exp -> (add-modules exp modules))
1071 (lowered (lower-gexp exp
1072 #:module-path module-path
1073 #:system system
1074 #:target target
1075 #:graft? requested-graft?
1076 #:guile-for-build
1077 guile-for-build
1078 #:effective-version
1079 effective-version
1080 #:deprecation-warnings
1081 deprecation-warnings))
1082
1083 (graphs (if references-graphs
1084 (lower-reference-graphs references-graphs
1085 #:system system
1086 #:target target)
1087 (return #f)))
1088 (allowed (if allowed-references
1089 (lower-references allowed-references
1090 #:system system
1091 #:target target)
1092 (return #f)))
1093 (disallowed (if disallowed-references
1094 (lower-references disallowed-references
1095 #:system system
1096 #:target target)
1097 (return #f)))
1098 (guile -> (lowered-gexp-guile lowered))
1099 (builder (text-file script-name
1100 (object->string
1101 (lowered-gexp-sexp lowered)))))
1102 (mbegin %store-monad
1103 (set-grafting graft?) ;restore the initial setting
1104 (raw-derivation name
1105 (string-append (derivation-input-output-path guile)
1106 "/bin/guile")
1107 `("--no-auto-compile"
1108 ,@(append-map (lambda (directory)
1109 `("-L" ,directory))
1110 (lowered-gexp-load-path lowered))
1111 ,@(append-map (lambda (directory)
1112 `("-C" ,directory))
1113 (lowered-gexp-load-compiled-path lowered))
1114 ,builder)
1115 #:outputs outputs
1116 #:env-vars env-vars
1117 #:system system
1118 #:inputs `(,guile
1119 ,@(lowered-gexp-inputs lowered)
1120 ,@(match graphs
1121 (((_ . inputs) ...)
1122 (filter derivation-input? inputs))
1123 (#f '())))
1124 #:sources `(,builder
1125 ,@(if (and (string? modules)
1126 (store-path? modules))
1127 (list modules)
1128 '())
1129 ,@(lowered-gexp-sources lowered)
1130 ,@(match graphs
1131 (((_ . inputs) ...)
1132 (filter string? inputs))
1133 (#f '())))
1134
1135 #:hash hash #:hash-algo hash-algo #:recursive? recursive?
1136 #:references-graphs (and=> graphs graphs-file-names)
1137 #:allowed-references allowed
1138 #:disallowed-references disallowed
1139 #:leaked-env-vars leaked-env-vars
1140 #:local-build? local-build?
1141 #:substitutable? substitutable?
1142 #:properties properties))))
1143
1144 (define* (gexp-inputs exp #:key native?)
1145 "Return the input list for EXP. When NATIVE? is true, return only native
1146 references; otherwise, return only non-native references."
1147 ;; TODO: Return <gexp-input> records instead of tuples.
1148 (define (add-reference-inputs ref result)
1149 (match ref
1150 (($ <gexp-input> (? gexp? exp) _ #t)
1151 (if native?
1152 (append (gexp-inputs exp)
1153 (gexp-inputs exp #:native? #t)
1154 result)
1155 result))
1156 (($ <gexp-input> (? gexp? exp) _ #f)
1157 (append (gexp-inputs exp #:native? native?)
1158 result))
1159 (($ <gexp-input> (? string? str))
1160 (if (direct-store-path? str)
1161 (cons `(,str) result)
1162 result))
1163 (($ <gexp-input> (? struct? thing) output n?)
1164 (if (and (eqv? n? native?) (lookup-compiler thing))
1165 ;; THING is a derivation, or a package, or an origin, etc.
1166 (cons `(,thing ,output) result)
1167 result))
1168 (($ <gexp-input> (lst ...) output n?)
1169 (fold-right add-reference-inputs result
1170 ;; XXX: For now, automatically convert LST to a list of
1171 ;; gexp-inputs. Inherit N?.
1172 (map (match-lambda
1173 ((? gexp-input? x)
1174 (%gexp-input (gexp-input-thing x)
1175 (gexp-input-output x)
1176 n?))
1177 (x
1178 (%gexp-input x "out" n?)))
1179 lst)))
1180 (_
1181 ;; Ignore references to other kinds of objects.
1182 result)))
1183
1184 (fold-right add-reference-inputs
1185 '()
1186 (gexp-references exp)))
1187
1188 (define gexp-native-inputs
1189 (cut gexp-inputs <> #:native? #t))
1190
1191 (define (gexp-outputs exp)
1192 "Return the outputs referred to by EXP as a list of strings."
1193 (define (add-reference-output ref result)
1194 (match ref
1195 (($ <gexp-output> name)
1196 (cons name result))
1197 (($ <gexp-input> (? gexp? exp))
1198 (append (gexp-outputs exp) result))
1199 (($ <gexp-input> (lst ...) output native?)
1200 ;; XXX: Automatically convert LST.
1201 (add-reference-output (map (match-lambda
1202 ((? gexp-input? x) x)
1203 (x (%gexp-input x "out" native?)))
1204 lst)
1205 result))
1206 ((lst ...)
1207 (fold-right add-reference-output result lst))
1208 (_
1209 result)))
1210
1211 (delete-duplicates
1212 (add-reference-output (gexp-references exp) '())))
1213
1214 (define* (gexp->sexp exp #:key
1215 (system (%current-system))
1216 (target (%current-target-system)))
1217 "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
1218 and in the current monad setting (system type, etc.)"
1219 (define* (reference->sexp ref #:optional native?)
1220 (with-monad %store-monad
1221 (match ref
1222 (($ <gexp-output> output)
1223 ;; Output file names are not known in advance but the daemon defines
1224 ;; an environment variable for each of them at build time, so use
1225 ;; that trick.
1226 (return `((@ (guile) getenv) ,output)))
1227 (($ <gexp-input> (? gexp? exp) output n?)
1228 (gexp->sexp exp
1229 #:system system
1230 #:target (if (or n? native?) #f target)))
1231 (($ <gexp-input> (refs ...) output n?)
1232 (mapm %store-monad
1233 (lambda (ref)
1234 ;; XXX: Automatically convert REF to an gexp-input.
1235 (reference->sexp
1236 (if (gexp-input? ref)
1237 ref
1238 (%gexp-input ref "out" n?))
1239 (or n? native?)))
1240 refs))
1241 (($ <gexp-input> (? struct? thing) output n?)
1242 (let ((target (if (or n? native?) #f target)))
1243 (lower+expand-object thing system
1244 #:target target
1245 #:output output)))
1246 (($ <gexp-input> (? self-quoting? x))
1247 (return x))
1248 (($ <gexp-input> x)
1249 (raise (condition (&gexp-input-error (input x)))))
1250 (x
1251 (return x)))))
1252
1253 (mlet %store-monad
1254 ((args (mapm %store-monad
1255 reference->sexp (gexp-references exp))))
1256 (return (apply (gexp-proc exp) args))))
1257
1258 (define-syntax-rule (define-syntax-parameter-once name proc)
1259 ;; Like 'define-syntax-parameter' but ensure the top-level binding for NAME
1260 ;; does not get redefined. This works around a race condition in a
1261 ;; multi-threaded context with Guile <= 2.2.4: <https://bugs.gnu.org/27476>.
1262 (eval-when (load eval expand compile)
1263 (define name
1264 (if (module-locally-bound? (current-module) 'name)
1265 (module-ref (current-module) 'name)
1266 (make-syntax-transformer 'name 'syntax-parameter
1267 (list proc))))))
1268
1269 (define-syntax-parameter-once current-imported-modules
1270 ;; Current list of imported modules.
1271 (identifier-syntax '()))
1272
1273 (define-syntax-rule (with-imported-modules modules body ...)
1274 "Mark the gexps defined in BODY... as requiring MODULES in their execution
1275 environment."
1276 (syntax-parameterize ((current-imported-modules
1277 (identifier-syntax modules)))
1278 body ...))
1279
1280 (define-syntax-parameter-once current-imported-extensions
1281 ;; Current list of extensions.
1282 (identifier-syntax '()))
1283
1284 (define-syntax-rule (with-extensions extensions body ...)
1285 "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
1286 execution environment."
1287 (syntax-parameterize ((current-imported-extensions
1288 (identifier-syntax extensions)))
1289 body ...))
1290
1291 (define-syntax gexp
1292 (lambda (s)
1293 (define (collect-escapes exp)
1294 ;; Return all the 'ungexp' present in EXP.
1295 (let loop ((exp exp)
1296 (result '()))
1297 (syntax-case exp (ungexp
1298 ungexp-splicing
1299 ungexp-native
1300 ungexp-native-splicing)
1301 ((ungexp _)
1302 (cons exp result))
1303 ((ungexp _ _)
1304 (cons exp result))
1305 ((ungexp-splicing _ ...)
1306 (cons exp result))
1307 ((ungexp-native _ ...)
1308 (cons exp result))
1309 ((ungexp-native-splicing _ ...)
1310 (cons exp result))
1311 ((exp0 . exp)
1312 (let ((result (loop #'exp0 result)))
1313 (loop #'exp result)))
1314 (_
1315 result))))
1316
1317 (define (escape->ref exp)
1318 ;; Turn 'ungexp' form EXP into a "reference".
1319 (syntax-case exp (ungexp ungexp-splicing
1320 ungexp-native ungexp-native-splicing
1321 output)
1322 ((ungexp output)
1323 #'(gexp-output "out"))
1324 ((ungexp output name)
1325 #'(gexp-output name))
1326 ((ungexp thing)
1327 #'(%gexp-input thing "out" #f))
1328 ((ungexp drv-or-pkg out)
1329 #'(%gexp-input drv-or-pkg out #f))
1330 ((ungexp-splicing lst)
1331 #'(%gexp-input lst "out" #f))
1332 ((ungexp-native thing)
1333 #'(%gexp-input thing "out" #t))
1334 ((ungexp-native drv-or-pkg out)
1335 #'(%gexp-input drv-or-pkg out #t))
1336 ((ungexp-native-splicing lst)
1337 #'(%gexp-input lst "out" #t))))
1338
1339 (define (substitute-ungexp exp substs)
1340 ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
1341 ;; the corresponding form in SUBSTS.
1342 (match (assoc exp substs)
1343 ((_ id)
1344 id)
1345 (_ ;internal error
1346 (with-syntax ((exp exp))
1347 #'(syntax-error "error: no 'ungexp' substitution" exp)))))
1348
1349 (define (substitute-ungexp-splicing exp substs)
1350 (syntax-case exp ()
1351 ((exp rest ...)
1352 (match (assoc #'exp substs)
1353 ((_ id)
1354 (with-syntax ((id id))
1355 #`(append id
1356 #,(substitute-references #'(rest ...) substs))))
1357 (_
1358 #'(syntax-error "error: no 'ungexp-splicing' substitution"
1359 exp))))))
1360
1361 (define (substitute-references exp substs)
1362 ;; Return a variant of EXP where all the cars of SUBSTS have been
1363 ;; replaced by the corresponding cdr.
1364 (syntax-case exp (ungexp ungexp-native
1365 ungexp-splicing ungexp-native-splicing)
1366 ((ungexp _ ...)
1367 (substitute-ungexp exp substs))
1368 ((ungexp-native _ ...)
1369 (substitute-ungexp exp substs))
1370 (((ungexp-splicing _ ...) rest ...)
1371 (substitute-ungexp-splicing exp substs))
1372 (((ungexp-native-splicing _ ...) rest ...)
1373 (substitute-ungexp-splicing exp substs))
1374 ((exp0 . exp)
1375 #`(cons #,(substitute-references #'exp0 substs)
1376 #,(substitute-references #'exp substs)))
1377 (x #''x)))
1378
1379 (syntax-case s (ungexp output)
1380 ((_ exp)
1381 (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
1382 (formals (generate-temporaries escapes))
1383 (sexp (substitute-references #'exp (zip escapes formals)))
1384 (refs (map escape->ref escapes)))
1385 #`(make-gexp (list #,@refs)
1386 current-imported-modules
1387 current-imported-extensions
1388 (lambda #,formals
1389 #,sexp)))))))
1390
1391 \f
1392 ;;;
1393 ;;; Module handling.
1394 ;;;
1395
1396 (define %utils-module
1397 ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
1398 ;; other primitives below. Note: We give the file name relative to this
1399 ;; file you are currently reading; 'search-path' could return a file name
1400 ;; relative to the current working directory.
1401 (local-file "build/utils.scm"
1402 "build-utils.scm"))
1403
1404 (define* (imported-files/derivation files
1405 #:key (name "file-import")
1406 (symlink? #f)
1407 (system (%current-system))
1408 (guile (%guile-for-build)))
1409 "Return a derivation that imports FILES into STORE. FILES must be a list
1410 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1411 resulting store path. FILE can be either a file name, or a file-like object,
1412 as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
1413 to the source files instead of copying them."
1414 (define file-pair
1415 (match-lambda
1416 ((final-path . (? string? file-name))
1417 (mlet %store-monad ((file (interned-file file-name
1418 (basename final-path))))
1419 (return (list final-path file))))
1420 ((final-path . file-like)
1421 (mlet %store-monad ((file (lower-object file-like system)))
1422 (return (list final-path file))))))
1423
1424 (mlet %store-monad ((files (mapm %store-monad file-pair files)))
1425 (define build
1426 (gexp
1427 (begin
1428 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1429 (use-modules (ice-9 match))
1430
1431 (mkdir (ungexp output)) (chdir (ungexp output))
1432 (for-each (match-lambda
1433 ((final-path store-path)
1434 (mkdir-p (dirname final-path))
1435 ((ungexp (if symlink? 'symlink 'copy-file))
1436 store-path final-path)))
1437 '(ungexp files)))))
1438
1439 ;; TODO: Pass FILES as an environment variable so that BUILD remains
1440 ;; exactly the same regardless of FILES: less disk space, and fewer
1441 ;; 'add-to-store' RPCs.
1442 (gexp->derivation name build
1443 #:system system
1444 #:guile-for-build guile
1445 #:local-build? #t
1446 #:substitutable? #f
1447
1448 ;; Avoid deprecation warnings about the use of the _IO*
1449 ;; constants in (guix build utils).
1450 #:env-vars
1451 '(("GUILE_WARN_DEPRECATED" . "no")))))
1452
1453 (define* (imported-files files
1454 #:key (name "file-import")
1455 ;; The following parameters make sense when creating
1456 ;; an actual derivation.
1457 (system (%current-system))
1458 (guile (%guile-for-build)))
1459 "Import FILES into the store and return the resulting derivation or store
1460 file name (a derivation is created if and only if some elements of FILES are
1461 file-like objects and not local file names.) FILES must be a list
1462 of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
1463 resulting store path. FILE can be either a file name, or a file-like object,
1464 as returned by 'local-file' for example."
1465 (if (any (match-lambda
1466 ((_ . (? struct? source)) #t)
1467 (_ #f))
1468 files)
1469 (imported-files/derivation files #:name name
1470 #:symlink? derivation?
1471 #:system system #:guile guile)
1472 (interned-file-tree `(,name directory
1473 ,@(file-mapping->tree files)))))
1474
1475 (define* (imported-modules modules
1476 #:key (name "module-import")
1477 (system (%current-system))
1478 (guile (%guile-for-build))
1479 (module-path %load-path))
1480 "Return a derivation that contains the source files of MODULES, a list of
1481 module names such as `(ice-9 q)'. All of MODULES must be either names of
1482 modules to be found in the MODULE-PATH search path, or a module name followed
1483 by an arrow followed by a file-like object. For example:
1484
1485 (imported-modules `((guix build utils)
1486 (guix gcrypt)
1487 ((guix config) => ,(scheme-file …))))
1488
1489 In this example, the first two modules are taken from MODULE-PATH, and the
1490 last one is created from the given <scheme-file> object."
1491 (let ((files (map (match-lambda
1492 (((module ...) '=> file)
1493 (cons (module->source-file-name module)
1494 file))
1495 ((module ...)
1496 (let ((f (module->source-file-name module)))
1497 (cons f (search-path* module-path f)))))
1498 modules)))
1499 (imported-files files #:name name
1500 #:system system
1501 #:guile guile)))
1502
1503 (define* (compiled-modules modules
1504 #:key (name "module-import-compiled")
1505 (system (%current-system))
1506 target
1507 (guile (%guile-for-build))
1508 (module-path %load-path)
1509 (extensions '())
1510 (deprecation-warnings #f))
1511 "Return a derivation that builds a tree containing the `.go' files
1512 corresponding to MODULES. All the MODULES are built in a context where
1513 they can refer to each other. When TARGET is true, cross-compile MODULES for
1514 TARGET, a GNU triplet."
1515 (define total (length modules))
1516
1517 (mlet %store-monad ((modules (imported-modules modules
1518 #:system system
1519 #:guile guile
1520 #:module-path
1521 module-path)))
1522 (define build
1523 (gexp
1524 (begin
1525 (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
1526
1527 (use-modules (ice-9 ftw)
1528 (ice-9 format)
1529 (srfi srfi-1)
1530 (srfi srfi-26)
1531 (system base target)
1532 (system base compile))
1533
1534 (define (regular? file)
1535 (not (member file '("." ".."))))
1536
1537 (define (process-entry entry output processed)
1538 (if (file-is-directory? entry)
1539 (let ((output (string-append output "/" (basename entry))))
1540 (mkdir-p output)
1541 (process-directory entry output processed))
1542 (let* ((base (basename entry ".scm"))
1543 (output (string-append output "/" base ".go")))
1544 (format #t "[~2@a/~2@a] Compiling '~a'...~%"
1545 (+ 1 processed (ungexp total))
1546 (ungexp (* total 2))
1547 entry)
1548
1549 (ungexp-splicing
1550 (if target
1551 (gexp ((with-target (ungexp target)
1552 (lambda ()
1553 (compile-file entry
1554 #:output-file output
1555 #:opts
1556 %auto-compilation-options)))))
1557 (gexp ((compile-file entry
1558 #:output-file output
1559 #:opts %auto-compilation-options)))))
1560
1561 (+ 1 processed))))
1562
1563 (define (process-directory directory output processed)
1564 (let ((entries (map (cut string-append directory "/" <>)
1565 (scandir directory regular?))))
1566 (fold (cut process-entry <> output <>)
1567 processed
1568 entries)))
1569
1570 (define* (load-from-directory directory
1571 #:optional (loaded 0))
1572 "Load all the source files found in DIRECTORY."
1573 ;; XXX: This works around <https://bugs.gnu.org/15602>.
1574 (let ((entries (map (cut string-append directory "/" <>)
1575 (scandir directory regular?))))
1576 (fold (lambda (file loaded)
1577 (if (file-is-directory? file)
1578 (load-from-directory file loaded)
1579 (begin
1580 (format #t "[~2@a/~2@a] Loading '~a'...~%"
1581 (+ 1 loaded) (ungexp (* 2 total))
1582 file)
1583 (save-module-excursion
1584 (lambda ()
1585 (primitive-load file)))
1586 (+ 1 loaded))))
1587 loaded
1588 entries)))
1589
1590 (setvbuf (current-output-port)
1591 (cond-expand (guile-2.2 'line) (else _IOLBF)))
1592
1593 (define mkdir-p
1594 ;; Capture 'mkdir-p'.
1595 (@ (guix build utils) mkdir-p))
1596
1597 ;; Add EXTENSIONS to the search path.
1598 (set! %load-path
1599 (append (map (lambda (extension)
1600 (string-append extension
1601 "/share/guile/site/"
1602 (effective-version)))
1603 '((ungexp-native-splicing extensions)))
1604 %load-path))
1605 (set! %load-compiled-path
1606 (append (map (lambda (extension)
1607 (string-append extension "/lib/guile/"
1608 (effective-version)
1609 "/site-ccache"))
1610 '((ungexp-native-splicing extensions)))
1611 %load-compiled-path))
1612
1613 (set! %load-path (cons (ungexp modules) %load-path))
1614
1615 ;; Above we loaded our own (guix build utils) but now we may need to
1616 ;; load a compile a different one. Thus, force a reload.
1617 (let ((utils (string-append (ungexp modules)
1618 "/guix/build/utils.scm")))
1619 (when (file-exists? utils)
1620 (load utils)))
1621
1622 (mkdir (ungexp output))
1623 (chdir (ungexp modules))
1624
1625 (load-from-directory ".")
1626 (process-directory "." (ungexp output) 0))))
1627
1628 ;; TODO: Pass MODULES as an environment variable.
1629 (gexp->derivation name build
1630 #:system system
1631 #:guile-for-build guile
1632 #:local-build? #t
1633 #:env-vars
1634 (case deprecation-warnings
1635 ((#f)
1636 '(("GUILE_WARN_DEPRECATED" . "no")))
1637 ((detailed)
1638 '(("GUILE_WARN_DEPRECATED" . "detailed")))
1639 (else
1640 '())))))
1641
1642 \f
1643 ;;;
1644 ;;; Convenience procedures.
1645 ;;;
1646
1647 (define (default-guile)
1648 ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
1649 ;; programs returned by 'program-file' and we don't want to keep references
1650 ;; to several Guile packages). This module must not refer to (gnu …)
1651 ;; modules directly, to avoid circular dependencies, hence this hack.
1652 (module-ref (resolve-interface '(gnu packages guile))
1653 'guile-3.0))
1654
1655 (define* (load-path-expression modules #:optional (path %load-path)
1656 #:key (extensions '()) system target)
1657 "Return as a monadic value a gexp that sets '%load-path' and
1658 '%load-compiled-path' to point to MODULES, a list of module names. MODULES
1659 are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
1660 (if (and (null? modules) (null? extensions))
1661 (with-monad %store-monad
1662 (return #f))
1663 (mlet %store-monad ((modules (imported-modules modules
1664 #:module-path path
1665 #:system system))
1666 (compiled (compiled-modules modules
1667 #:extensions extensions
1668 #:module-path path
1669 #:system system
1670 #:target target)))
1671 (return
1672 (gexp (eval-when (expand load eval)
1673 ;; Augment the load paths and delete duplicates. Do that
1674 ;; without loading (srfi srfi-1) or anything.
1675 (let ((extensions '((ungexp-splicing extensions)))
1676 (prepend (lambda (items lst)
1677 ;; This is O(N²) but N is typically small.
1678 (let loop ((items items)
1679 (lst lst))
1680 (if (null? items)
1681 lst
1682 (loop (cdr items)
1683 (cons (car items)
1684 (delete (car items) lst))))))))
1685 (set! %load-path
1686 (prepend (cons (ungexp modules)
1687 (map (lambda (extension)
1688 (string-append extension
1689 "/share/guile/site/"
1690 (effective-version)))
1691 extensions))
1692 %load-path))
1693 (set! %load-compiled-path
1694 (prepend (cons (ungexp compiled)
1695 (map (lambda (extension)
1696 (string-append extension
1697 "/lib/guile/"
1698 (effective-version)
1699 "/site-ccache"))
1700 extensions))
1701 %load-compiled-path)))))))))
1702
1703 (define* (gexp->script name exp
1704 #:key (guile (default-guile))
1705 (module-path %load-path)
1706 (system (%current-system))
1707 (target 'current))
1708 "Return an executable script NAME that runs EXP using GUILE, with EXP's
1709 imported modules in its search path. Look up EXP's modules in MODULE-PATH."
1710 (mlet* %store-monad ((target (if (eq? target 'current)
1711 (current-target-system)
1712 (return target)))
1713 (set-load-path
1714 (load-path-expression (gexp-modules exp)
1715 module-path
1716 #:extensions
1717 (gexp-extensions exp)
1718 #:system system
1719 #:target target)))
1720 (gexp->derivation name
1721 (gexp
1722 (call-with-output-file (ungexp output)
1723 (lambda (port)
1724 ;; Note: that makes a long shebang. When the store
1725 ;; is /gnu/store, that fits within the 128-byte
1726 ;; limit imposed by Linux, but that may go beyond
1727 ;; when running tests.
1728 (format port
1729 "#!~a/bin/guile --no-auto-compile~%!#~%"
1730 (ungexp guile))
1731
1732 (ungexp-splicing
1733 (if set-load-path
1734 (gexp ((write '(ungexp set-load-path) port)))
1735 (gexp ())))
1736
1737 (write '(ungexp exp) port)
1738 (chmod port #o555))))
1739 #:system system
1740 #:target target
1741 #:module-path module-path
1742
1743 ;; These derivations are not worth offloading or
1744 ;; substituting.
1745 #:local-build? #t
1746 #:substitutable? #f)))
1747
1748 (define* (gexp->file name exp #:key
1749 (set-load-path? #t)
1750 (module-path %load-path)
1751 (splice? #f)
1752 (system (%current-system))
1753 (target 'current))
1754 "Return a derivation that builds a file NAME containing EXP. When SPLICE?
1755 is true, EXP is considered to be a list of expressions that will be spliced in
1756 the resulting file.
1757
1758 When SET-LOAD-PATH? is true, emit code in the resulting file to set
1759 '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
1760 Lookup EXP's modules in MODULE-PATH."
1761 (define modules (gexp-modules exp))
1762 (define extensions (gexp-extensions exp))
1763
1764 (mlet* %store-monad
1765 ((target (if (eq? target 'current)
1766 (current-target-system)
1767 (return target)))
1768 (no-load-path? -> (or (not set-load-path?)
1769 (and (null? modules)
1770 (null? extensions))))
1771 (set-load-path
1772 (load-path-expression modules module-path
1773 #:extensions extensions
1774 #:system system
1775 #:target target)))
1776 (if no-load-path?
1777 (gexp->derivation name
1778 (gexp
1779 (call-with-output-file (ungexp output)
1780 (lambda (port)
1781 (for-each
1782 (lambda (exp)
1783 (write exp port))
1784 '(ungexp (if splice?
1785 exp
1786 (gexp ((ungexp exp)))))))))
1787 #:local-build? #t
1788 #:substitutable? #f
1789 #:system system
1790 #:target target)
1791 (gexp->derivation name
1792 (gexp
1793 (call-with-output-file (ungexp output)
1794 (lambda (port)
1795 (write '(ungexp set-load-path) port)
1796 (for-each
1797 (lambda (exp)
1798 (write exp port))
1799 '(ungexp (if splice?
1800 exp
1801 (gexp ((ungexp exp)))))))))
1802 #:module-path module-path
1803 #:local-build? #t
1804 #:substitutable? #f
1805 #:system system
1806 #:target target))))
1807
1808 (define* (text-file* name #:rest text)
1809 "Return as a monadic value a derivation that builds a text file containing
1810 all of TEXT. TEXT may list, in addition to strings, objects of any type that
1811 can be used in a gexp: packages, derivations, local file objects, etc. The
1812 resulting store file holds references to all these."
1813 (define builder
1814 (gexp (call-with-output-file (ungexp output "out")
1815 (lambda (port)
1816 (display (string-append (ungexp-splicing text)) port)))))
1817
1818 (gexp->derivation name builder
1819 #:local-build? #t
1820 #:substitutable? #f))
1821
1822 (define* (mixed-text-file name #:rest text)
1823 "Return an object representing store file NAME containing TEXT. TEXT is a
1824 sequence of strings and file-like objects, as in:
1825
1826 (mixed-text-file \"profile\"
1827 \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
1828
1829 This is the declarative counterpart of 'text-file*'."
1830 (define build
1831 (gexp (call-with-output-file (ungexp output "out")
1832 (lambda (port)
1833 (display (string-append (ungexp-splicing text)) port)))))
1834
1835 (computed-file name build))
1836
1837 (define (file-union name files)
1838 "Return a <computed-file> that builds a directory containing all of FILES.
1839 Each item in FILES must be a two-element list where the first element is the
1840 file name to use in the new directory, and the second element is a gexp
1841 denoting the target file. Here's an example:
1842
1843 (file-union \"etc\"
1844 `((\"hosts\" ,(plain-file \"hosts\"
1845 \"127.0.0.1 localhost\"))
1846 (\"bashrc\" ,(plain-file \"bashrc\"
1847 \"alias ls='ls --color'\"))
1848 (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
1849
1850 This yields an 'etc' directory containing these two files."
1851 (computed-file name
1852 (with-imported-modules '((guix build utils))
1853 (gexp
1854 (begin
1855 (use-modules (guix build utils))
1856
1857 (mkdir (ungexp output))
1858 (chdir (ungexp output))
1859 (ungexp-splicing
1860 (map (match-lambda
1861 ((target source)
1862 (gexp
1863 (begin
1864 ;; Stat the source to abort early if it does
1865 ;; not exist.
1866 (stat (ungexp source))
1867
1868 (mkdir-p (dirname (ungexp target)))
1869 (symlink (ungexp source)
1870 (ungexp target))))))
1871 files)))))))
1872
1873 (define* (directory-union name things
1874 #:key (copy? #f) (quiet? #f)
1875 (resolve-collision 'warn-about-collision))
1876 "Return a directory that is the union of THINGS, where THINGS is a list of
1877 file-like objects denoting directories. For example:
1878
1879 (directory-union \"guile+emacs\" (list guile emacs))
1880
1881 yields a directory that is the union of the 'guile' and 'emacs' packages.
1882
1883 Call RESOLVE-COLLISION when several files collide, passing it the list of
1884 colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
1885 which case the colliding entry is skipped altogether.
1886
1887 When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
1888 is true, the derivation will not print anything."
1889 (define symlink
1890 (if copy?
1891 (gexp (lambda (old new)
1892 (if (file-is-directory? old)
1893 (symlink old new)
1894 (copy-file old new))))
1895 (gexp symlink)))
1896
1897 (define log-port
1898 (if quiet?
1899 (gexp (%make-void-port "w"))
1900 (gexp (current-error-port))))
1901
1902 (match things
1903 ((one)
1904 ;; Only one thing; return it.
1905 one)
1906 (_
1907 (computed-file name
1908 (with-imported-modules '((guix build union))
1909 (gexp (begin
1910 (use-modules (guix build union)
1911 (srfi srfi-1)) ;for 'first' and 'last'
1912
1913 (union-build (ungexp output)
1914 '(ungexp things)
1915
1916 #:log-port (ungexp log-port)
1917 #:symlink (ungexp symlink)
1918 #:resolve-collision
1919 (ungexp resolve-collision)))))))))
1920
1921 \f
1922 ;;;
1923 ;;; Syntactic sugar.
1924 ;;;
1925
1926 (eval-when (expand load eval)
1927 (define* (read-ungexp chr port #:optional native?)
1928 "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
1929 true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
1930 (define unquote-symbol
1931 (match (peek-char port)
1932 (#\@
1933 (read-char port)
1934 (if native?
1935 'ungexp-native-splicing
1936 'ungexp-splicing))
1937 (_
1938 (if native?
1939 'ungexp-native
1940 'ungexp))))
1941
1942 (match (read port)
1943 ((? symbol? symbol)
1944 (let ((str (symbol->string symbol)))
1945 (match (string-index-right str #\:)
1946 (#f
1947 `(,unquote-symbol ,symbol))
1948 (colon
1949 (let ((name (string->symbol (substring str 0 colon)))
1950 (output (substring str (+ colon 1))))
1951 `(,unquote-symbol ,name ,output))))))
1952 (x
1953 `(,unquote-symbol ,x))))
1954
1955 (define (read-gexp chr port)
1956 "Read a 'gexp' form from PORT."
1957 `(gexp ,(read port)))
1958
1959 ;; Extend the reader
1960 (read-hash-extend #\~ read-gexp)
1961 (read-hash-extend #\$ read-ungexp)
1962 (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
1963
1964 ;;; gexp.scm ends here