graph: Use absolute file name canonicalization.
[jackhill/guix/guix.git] / guix / derivations.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
35b5ca78 2;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
77d3cf08 3;;;
233e7676 4;;; This file is part of GNU Guix.
77d3cf08 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
77d3cf08
LC
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
77d3cf08
LC
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
77d3cf08
LC
18
19(define-module (guix derivations)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
07c86312 22 #:use-module (srfi srfi-9 gnu)
77d3cf08 23 #:use-module (srfi srfi-26)
f304c9c2
LC
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
77d3cf08
LC
26 #:use-module (rnrs io ports)
27 #:use-module (rnrs bytevectors)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 rdelim)
e387ab7c 30 #:use-module (ice-9 vlist)
69f90f5c 31 #:use-module (guix store)
26bbbb95 32 #:use-module (guix utils)
958dd3ce 33 #:use-module (guix combinators)
e87f0591 34 #:use-module (guix monads)
72626a71 35 #:use-module (guix hash)
ddc29a78 36 #:use-module (guix base32)
969df974 37 #:use-module (guix records)
ed3592a9 38 #:use-module (guix sets)
9a20830e
LC
39 #:export (<derivation>
40 derivation?
77d3cf08
LC
41 derivation-outputs
42 derivation-inputs
43 derivation-sources
44 derivation-system
7ce7361b 45 derivation-builder
77d3cf08
LC
46 derivation-builder-arguments
47 derivation-builder-environment-vars
6a446d56 48 derivation-file-name
9a20830e
LC
49 derivation-prerequisites
50 derivation-prerequisites-to-build
77d3cf08 51
9a20830e 52 <derivation-output>
77d3cf08
LC
53 derivation-output?
54 derivation-output-path
55 derivation-output-hash-algo
56 derivation-output-hash
36bbbbd1 57 derivation-output-recursive?
77d3cf08 58
9a20830e 59 <derivation-input>
77d3cf08
LC
60 derivation-input?
61 derivation-input-path
62 derivation-input-sub-derivations
dd36b51b 63 derivation-input-output-paths
3681db5d 64 valid-derivation-input?
77d3cf08 65
f304c9c2
LC
66 &derivation-error
67 derivation-error?
68 derivation-error-derivation
69 &derivation-missing-output-error
70 derivation-missing-output-error?
71 derivation-missing-output
72
e786293e 73 derivation-name
0b6af195 74 derivation-output-names
77d3cf08 75 fixed-output-derivation?
fc93e309
LC
76 offloadable-derivation?
77 substitutable-derivation?
e9651e39 78 substitution-oracle
341c6fdd
LC
79 derivation-hash
80
81 read-derivation
26bbbb95 82 write-derivation
59688fc4
LC
83 derivation->output-path
84 derivation->output-paths
de4c3f26 85 derivation-path->output-path
7244a5f7 86 derivation-path->output-paths
d9085c23 87 derivation
969df974 88
e387ab7c 89 map-derivation
d9085c23 90
01d8ac64 91 build-derivations
e87f0591 92 built-derivations
e87f0591 93
d26e1967
LC
94 file-search-error?
95 file-search-error-file-name
96 file-search-error-search-path
9d8100f4 97
d26e1967 98 search-path*
6985335f 99 module->source-file-name
aa72d9af 100 build-expression->derivation)
e87f0591
LC
101
102 ;; Re-export it from here for backward compatibility.
01d8ac64 103 #:re-export (%guile-for-build))
77d3cf08 104
f304c9c2
LC
105;;;
106;;; Error conditions.
107;;;
108
109(define-condition-type &derivation-error &nix-error
110 derivation-error?
111 (derivation derivation-error-derivation))
112
113(define-condition-type &derivation-missing-output-error &derivation-error
114 derivation-missing-output-error?
115 (output derivation-missing-output))
116
77d3cf08
LC
117;;;
118;;; Nix derivations, as implemented in Nix's `derivations.cc'.
119;;;
120
121(define-record-type <derivation>
6a446d56
LC
122 (make-derivation outputs inputs sources system builder args env-vars
123 file-name)
77d3cf08
LC
124 derivation?
125 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
126 (inputs derivation-inputs) ; list of <derivation-input>
127 (sources derivation-sources) ; list of store paths
128 (system derivation-system) ; string
129 (builder derivation-builder) ; store path
130 (args derivation-builder-arguments) ; list of strings
6a446d56
LC
131 (env-vars derivation-builder-environment-vars) ; list of name/value pairs
132 (file-name derivation-file-name)) ; the .drv file name
77d3cf08
LC
133
134(define-record-type <derivation-output>
36bbbbd1 135 (make-derivation-output path hash-algo hash recursive?)
77d3cf08
LC
136 derivation-output?
137 (path derivation-output-path) ; store path
138 (hash-algo derivation-output-hash-algo) ; symbol | #f
36bbbbd1
LC
139 (hash derivation-output-hash) ; bytevector | #f
140 (recursive? derivation-output-recursive?)) ; Boolean
77d3cf08
LC
141
142(define-record-type <derivation-input>
143 (make-derivation-input path sub-derivations)
144 derivation-input?
145 (path derivation-input-path) ; store path
146 (sub-derivations derivation-input-sub-derivations)) ; list of strings
147
07c86312
LC
148(set-record-type-printer! <derivation>
149 (lambda (drv port)
150 (format port "#<derivation ~a => ~a ~a>"
151 (derivation-file-name drv)
152 (string-join
153 (map (match-lambda
154 ((_ . output)
155 (derivation-output-path output)))
156 (derivation-outputs drv)))
157 (number->string (object-address drv) 16))))
158
e786293e
LC
159(define (derivation-name drv)
160 "Return the base name of DRV."
161 (let ((base (store-path-package-name (derivation-file-name drv))))
162 (string-drop-right base 4)))
163
0b6af195
LC
164(define (derivation-output-names drv)
165 "Return the names of the outputs of DRV."
166 (match (derivation-outputs drv)
167 (((names . _) ...)
168 names)))
169
77d3cf08
LC
170(define (fixed-output-derivation? drv)
171 "Return #t if DRV is a fixed-output derivation, such as the result of a
172download with a fixed hash (aka. `fetchurl')."
173 (match drv
174 (($ <derivation>
99e17dc9 175 (("out" . ($ <derivation-output> _ (? symbol?) (? bytevector?)))))
77d3cf08
LC
176 #t)
177 (_ #f)))
178
dd36b51b
LC
179(define (derivation-input-output-paths input)
180 "Return the list of output paths corresponding to INPUT, a
181<derivation-input>."
182 (match input
183 (($ <derivation-input> path sub-drvs)
184 (map (cut derivation-path->output-path path <>)
185 sub-drvs))))
186
3681db5d
LC
187(define (valid-derivation-input? store input)
188 "Return true if INPUT is valid--i.e., if all the outputs it requests are in
189the store."
190 (every (cut valid-path? store <>)
191 (derivation-input-output-paths input)))
192
193(define* (derivation-prerequisites drv #:optional (cut? (const #f)))
194 "Return the list of derivation-inputs required to build DRV, recursively.
195
196CUT? is a predicate that is passed a derivation-input and returns true to
197eliminate the given input and its dependencies from the search. An example of
198search a predicate is 'valid-derivation-input?'; when it is used as CUT?, the
199result is the set of prerequisites of DRV not already in valid."
ed3592a9
LC
200 (let loop ((drv drv)
201 (result '())
202 (input-set (set)))
3681db5d
LC
203 (let ((inputs (remove (lambda (input)
204 (or (set-contains? input-set input)
205 (cut? input)))
9a20830e 206 (derivation-inputs drv))))
ed3592a9
LC
207 (fold2 loop
208 (append inputs result)
209 (fold set-insert input-set inputs)
210 (map (lambda (i)
211 (call-with-input-file (derivation-input-path i)
212 read-derivation))
213 inputs)))))
9a20830e 214
fc93e309
LC
215(define (offloadable-derivation? drv)
216 "Return true if DRV can be offloaded, false otherwise."
217 (match (assoc "preferLocalBuild"
218 (derivation-builder-environment-vars drv))
219 (("preferLocalBuild" . "1") #f)
220 (_ #t)))
221
4a6aeb67
LC
222(define (substitutable-derivation? drv)
223 "Return #t if DRV can be substituted."
224 (match (assoc "allowSubstitutes"
225 (derivation-builder-environment-vars drv))
226 (("allowSubstitutes" . value)
227 (string=? value "1"))
228 (_ #t)))
fc93e309 229
e9651e39
LC
230(define (derivation-output-paths drv sub-drvs)
231 "Return the output paths of outputs SUB-DRVS of DRV."
232 (match drv
233 (($ <derivation> outputs)
234 (map (lambda (sub-drv)
235 (derivation-output-path (assoc-ref outputs sub-drv)))
236 sub-drvs))))
237
58c08df0
LC
238(define* (substitution-oracle store drv
239 #:key (mode (build-mode normal)))
e9651e39
LC
240 "Return a one-argument procedure that, when passed a store file name,
241returns #t if it's substitutable and #f otherwise. The returned procedure
1eabf4ec
LC
242knows about all substitutes for all the derivations listed in DRV, *except*
243those that are already valid (that is, it won't bother checking whether an
244item is substitutable if it's already on disk); it also knows about their
245prerequisites, unless they are themselves substitutable.
e9651e39
LC
246
247Creating a single oracle (thus making a single 'substitutable-paths' call) and
248reusing it is much more efficient than calling 'has-substitutes?' or similar
249repeatedly, because it avoids the costs associated with launching the
250substituter many times."
c7d1d88f
LC
251 (define valid?
252 (cut valid-path? store <>))
253
c3a450fb
LC
254 (define valid-input?
255 (cut valid-derivation-input? store <>))
256
257 (define (dependencies drv)
258 ;; Skip prerequisite sub-trees of DRV whose root is valid. This allows us
259 ;; to ask the substituter for just as much as needed, instead of asking it
260 ;; for the whole world, which can be significantly faster when substitute
261 ;; info is not already in cache.
262 (append-map derivation-input-output-paths
263 (derivation-prerequisites drv valid-input?)))
264
e9651e39 265 (let* ((paths (delete-duplicates
b59df243
LC
266 (concatenate
267 (fold (lambda (drv result)
268 (let ((self (match (derivation->output-paths drv)
269 (((names . paths) ...)
270 paths))))
58c08df0
LC
271 (cond ((eqv? mode (build-mode check))
272 (cons (dependencies drv) result))
273 ((every valid? self)
274 result)
275 (else
276 (cons* self (dependencies drv) result)))))
b59df243
LC
277 '()
278 drv))))
c06d140c
LC
279 (subst (list->set (substitutable-paths store paths))))
280 (cut set-contains? subst <>)))
e9651e39 281
784bb1f3 282(define* (derivation-prerequisites-to-build store drv
dd36b51b 283 #:key
58c08df0 284 (mode (build-mode normal))
dd36b51b 285 (outputs
0b6af195 286 (derivation-output-names drv))
e9651e39
LC
287 (substitutable?
288 (substitution-oracle store
58c08df0
LC
289 (list drv)
290 #:mode mode)))
dd36b51b
LC
291 "Return two values: the list of derivation-inputs required to build the
292OUTPUTS of DRV and not already available in STORE, recursively, and the list
e9651e39
LC
293of required store paths that can be substituted. SUBSTITUTABLE? must be a
294one-argument procedure similar to that returned by 'substitution-oracle'."
784bb1f3
LC
295 (define built?
296 (cut valid-path? store <>))
297
9a20830e 298 (define input-built?
dd36b51b
LC
299 (compose (cut any built? <>) derivation-input-output-paths))
300
301 (define input-substitutable?
302 ;; Return true if and only if all of SUB-DRVS are subsitutable. If at
303 ;; least one is missing, then everything must be rebuilt.
304 (compose (cut every substitutable? <>) derivation-input-output-paths))
784bb1f3 305
58c08df0
LC
306 (define (derivation-built? drv* sub-drvs)
307 ;; In 'check' mode, assume that DRV is not built.
308 (and (not (and (eqv? mode (build-mode check))
309 (eq? drv* drv)))
310 (every built? (derivation-output-paths drv* sub-drvs))))
dd36b51b
LC
311
312 (define (derivation-substitutable? drv sub-drvs)
d2d0514b
LC
313 (and (substitutable-derivation? drv)
314 (every substitutable? (derivation-output-paths drv sub-drvs))))
dd36b51b
LC
315
316 (let loop ((drv drv)
317 (sub-drvs outputs)
318 (build '())
319 (substitute '()))
320 (cond ((derivation-built? drv sub-drvs)
321 (values build substitute))
322 ((derivation-substitutable? drv sub-drvs)
323 (values build
324 (append (derivation-output-paths drv sub-drvs)
325 substitute)))
326 (else
d2d0514b
LC
327 (let ((build (if (substitutable-derivation? drv)
328 build
329 (cons (make-derivation-input
330 (derivation-file-name drv) sub-drvs)
331 build)))
332 (inputs (remove (lambda (i)
dd36b51b
LC
333 (or (member i build) ; XXX: quadratic
334 (input-built? i)
335 (input-substitutable? i)))
336 (derivation-inputs drv))))
337 (fold2 loop
338 (append inputs build)
339 (append (append-map (lambda (input)
340 (if (and (not (input-built? input))
341 (input-substitutable? input))
342 (derivation-input-output-paths
343 input)
344 '()))
345 (derivation-inputs drv))
346 substitute)
347 (map (lambda (i)
348 (call-with-input-file (derivation-input-path i)
349 read-derivation))
350 inputs)
351 (map derivation-input-sub-derivations inputs)))))))
9a20830e 352
d0840e4a
LC
353(define (%read-derivation drv-port)
354 ;; Actually read derivation from DRV-PORT.
77d3cf08
LC
355
356 (define comma (string->symbol ","))
357
358 (define (ununquote x)
359 (match x
360 (('unquote x) (ununquote x))
361 ((x ...) (map ununquote x))
362 (_ x)))
363
364 (define (outputs->alist x)
365 (fold-right (lambda (output result)
366 (match output
367 ((name path "" "")
368 (alist-cons name
36bbbbd1 369 (make-derivation-output path #f #f #f)
77d3cf08
LC
370 result))
371 ((name path hash-algo hash)
372 ;; fixed-output
36bbbbd1
LC
373 (let* ((rec? (string-prefix? "r:" hash-algo))
374 (algo (string->symbol
375 (if rec?
376 (string-drop hash-algo 2)
377 hash-algo)))
378 (hash (base16-string->bytevector hash)))
77d3cf08 379 (alist-cons name
36bbbbd1
LC
380 (make-derivation-output path algo
381 hash rec?)
77d3cf08
LC
382 result)))))
383 '()
384 x))
385
386 (define (make-input-drvs x)
387 (fold-right (lambda (input result)
388 (match input
389 ((path (sub-drvs ...))
390 (cons (make-derivation-input path sub-drvs)
391 result))))
392 '()
393 x))
394
df7bbd38
LC
395 ;; The contents of a derivation are typically ASCII, but choosing
396 ;; UTF-8 allows us to take the fast path for Guile's `scm_getc'.
397 (set-port-encoding! drv-port "UTF-8")
398
77d3cf08
LC
399 (let loop ((exp (read drv-port))
400 (result '()))
401 (match exp
402 ((? eof-object?)
403 (let ((result (reverse result)))
404 (match result
405 (('Derive ((outputs ...) (input-drvs ...)
406 (input-srcs ...)
407 (? string? system)
408 (? string? builder)
409 ((? string? args) ...)
410 ((var value) ...)))
411 (make-derivation (outputs->alist outputs)
412 (make-input-drvs input-drvs)
413 input-srcs
414 system builder args
6a446d56
LC
415 (fold-right alist-cons '() var value)
416 (port-filename drv-port)))
77d3cf08
LC
417 (_
418 (error "failed to parse derivation" drv-port result)))))
419 ((? (cut eq? <> comma))
420 (loop (read drv-port) result))
421 (_
422 (loop (read drv-port)
423 (cons (ununquote exp) result))))))
424
d0840e4a
LC
425(define read-derivation
426 (let ((cache (make-weak-value-hash-table 200)))
427 (lambda (drv-port)
428 "Read the derivation from DRV-PORT and return the corresponding
429<derivation> object."
430 ;; Memoize that operation because `%read-derivation' is quite expensive,
431 ;; and because the same argument is read more than 15 times on average
432 ;; during something like (package-derivation s gdb).
433 (let ((file (and=> (port-filename drv-port) basename)))
434 (or (and file (hash-ref cache file))
435 (let ((drv (%read-derivation drv-port)))
436 (hash-set! cache file drv)
437 drv))))))
438
d8085599
LC
439(define-inlinable (write-sequence lst write-item port)
440 ;; Write each element of LST with WRITE-ITEM to PORT, separating them with a
441 ;; comma.
442 (match lst
443 (()
444 #t)
445 ((prefix (... ...) last)
446 (for-each (lambda (item)
447 (write-item item port)
448 (display "," port))
449 prefix)
450 (write-item last port))))
451
452(define-inlinable (write-list lst write-item port)
453 ;; Write LST as a derivation list to PORT, using WRITE-ITEM to write each
454 ;; element.
455 (display "[" port)
456 (write-sequence lst write-item port)
457 (display "]" port))
458
459(define-inlinable (write-tuple lst write-item port)
460 ;; Same, but write LST as a tuple.
461 (display "(" port)
462 (write-sequence lst write-item port)
463 (display ")" port))
464
77d3cf08
LC
465(define (write-derivation drv port)
466 "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
467Eelco Dolstra's PhD dissertation for an overview of a previous version of
468that form."
aaa848f3
LC
469
470 ;; Make sure we're using the faster implementation.
471 (define format simple-format)
472
d8085599
LC
473 (define (write-string-list lst)
474 (write-list lst write port))
77d3cf08 475
d66ac374
LC
476 (define (coalesce-duplicate-inputs inputs)
477 ;; Return a list of inputs, such that when INPUTS contains the same DRV
478 ;; twice, they are coalesced, with their sub-derivations merged. This is
479 ;; needed because Nix itself keeps only one of them.
480 (fold (lambda (input result)
481 (match input
482 (($ <derivation-input> path sub-drvs)
483 ;; XXX: quadratic
484 (match (find (match-lambda
485 (($ <derivation-input> p s)
486 (string=? p path)))
487 result)
488 (#f
489 (cons input result))
490 ((and dup ($ <derivation-input> _ sub-drvs2))
491 ;; Merge DUP with INPUT.
492 (let ((sub-drvs (delete-duplicates
493 (append sub-drvs sub-drvs2))))
494 (cons (make-derivation-input path sub-drvs)
495 (delq dup result))))))))
496 '()
497 inputs))
498
d8085599
LC
499 (define (write-output output port)
500 (match output
36bbbbd1 501 ((name . ($ <derivation-output> path hash-algo hash recursive?))
d8085599 502 (write-tuple (list name path
36bbbbd1
LC
503 (if hash-algo
504 (string-append (if recursive? "r:" "")
505 (symbol->string hash-algo))
506 "")
d8085599
LC
507 (or (and=> hash bytevector->base16-string)
508 ""))
509 write
510 port))))
511
512 (define (write-input input port)
513 (match input
514 (($ <derivation-input> path sub-drvs)
515 (display "(" port)
516 (write path port)
517 (display "," port)
518 (write-string-list (sort sub-drvs string<?))
519 (display ")" port))))
520
521 (define (write-env-var env-var port)
522 (match env-var
523 ((name . value)
524 (display "(" port)
525 (write name port)
526 (display "," port)
527 (write value port)
528 (display ")" port))))
529
561eaf71
LC
530 ;; Note: lists are sorted alphabetically, to conform with the behavior of
531 ;; C++ `std::map' in Nix itself.
532
77d3cf08
LC
533 (match drv
534 (($ <derivation> outputs inputs sources
535 system builder args env-vars)
536 (display "Derive(" port)
d8085599
LC
537 (write-list (sort outputs
538 (lambda (o1 o2)
539 (string<? (car o1) (car o2))))
540 write-output
541 port)
77d3cf08 542 (display "," port)
d8085599
LC
543 (write-list (sort (coalesce-duplicate-inputs inputs)
544 (lambda (i1 i2)
545 (string<? (derivation-input-path i1)
546 (derivation-input-path i2))))
547 write-input
548 port)
77d3cf08 549 (display "," port)
d8085599 550 (write-string-list (sort sources string<?))
77d3cf08 551 (format port ",~s,~s," system builder)
d8085599 552 (write-string-list args)
77d3cf08 553 (display "," port)
d8085599
LC
554 (write-list (sort env-vars
555 (lambda (e1 e2)
556 (string<? (car e1) (car e2))))
557 write-env-var
558 port)
77d3cf08
LC
559 (display ")" port))))
560
be4e38fb
LC
561(define derivation->string
562 (memoize
563 (lambda (drv)
564 "Return the external representation of DRV as a string."
565 (with-fluids ((%default-port-encoding "UTF-8"))
566 (call-with-output-string
567 (cut write-derivation drv <>))))))
568
59688fc4 569(define* (derivation->output-path drv #:optional (output "out"))
f304c9c2
LC
570 "Return the store path of its output OUTPUT. Raise a
571'&derivation-missing-output-error' condition if OUTPUT is not an output of
572DRV."
573 (let ((output* (assoc-ref (derivation-outputs drv) output)))
574 (if output*
575 (derivation-output-path output*)
576 (raise (condition (&derivation-missing-output-error
577 (derivation drv)
578 (output output)))))))
59688fc4
LC
579
580(define (derivation->output-paths drv)
581 "Return the list of name/path pairs of the outputs of DRV."
582 (map (match-lambda
583 ((name . output)
584 (cons name (derivation-output-path output))))
585 (derivation-outputs drv)))
586
aaa848f3
LC
587(define derivation-path->output-path
588 ;; This procedure is called frequently, so memoize it.
589 (memoize
590 (lambda* (path #:optional (output "out"))
6f58d582 591 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the store
de4c3f26 592path of its output OUTPUT."
d0dc4907
LC
593 (derivation->output-path (call-with-input-file path read-derivation)
594 output))))
de4c3f26 595
7244a5f7 596(define (derivation-path->output-paths path)
6f58d582 597 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the
7244a5f7 598list of name/path pairs of its outputs."
59688fc4 599 (derivation->output-paths (call-with-input-file path read-derivation)))
7244a5f7 600
de4c3f26
LC
601\f
602;;;
603;;; Derivation primitive.
604;;;
605
26bbbb95
LC
606(define (compressed-hash bv size) ; `compressHash'
607 "Given the hash stored in BV, return a compressed version thereof that fits
608in SIZE bytes."
609 (define new (make-bytevector size 0))
610 (define old-size (bytevector-length bv))
611 (let loop ((i 0))
612 (if (= i old-size)
613 new
614 (let* ((j (modulo i size))
615 (o (bytevector-u8-ref new j)))
616 (bytevector-u8-set! new j
617 (logxor o (bytevector-u8-ref bv i)))
618 (loop (+ 1 i))))))
77d3cf08 619
1391dcb0
LC
620(define derivation-path->base16-hash
621 (memoize
622 (lambda (file)
623 "Return a string containing the base16 representation of the hash of the
624derivation at FILE."
625 (call-with-input-file file
626 (compose bytevector->base16-string
627 derivation-hash
628 read-derivation)))))
629
de4c3f26
LC
630(define derivation-hash ; `hashDerivationModulo' in derivations.cc
631 (memoize
632 (lambda (drv)
633 "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
634 (match drv
635 (($ <derivation> ((_ . ($ <derivation-output> path
36bbbbd1
LC
636 (? symbol? hash-algo) (? bytevector? hash)
637 (? boolean? recursive?)))))
de4c3f26 638 ;; A fixed-output derivation.
77d3cf08 639 (sha256
de4c3f26 640 (string->utf8
36bbbbd1
LC
641 (string-append "fixed:out:"
642 (if recursive? "r:" "")
643 (symbol->string hash-algo)
749c6567
LC
644 ":" (bytevector->base16-string hash)
645 ":" path))))
de4c3f26
LC
646 (($ <derivation> outputs inputs sources
647 system builder args env-vars)
648 ;; A regular derivation: replace the path of each input with that
649 ;; input's hash; return the hash of serialization of the resulting
561eaf71
LC
650 ;; derivation.
651 (let* ((inputs (map (match-lambda
652 (($ <derivation-input> path sub-drvs)
1391dcb0 653 (let ((hash (derivation-path->base16-hash path)))
561eaf71
LC
654 (make-derivation-input hash sub-drvs))))
655 inputs))
656 (drv (make-derivation outputs inputs sources
6a446d56
LC
657 system builder args env-vars
658 #f)))
b0fad8a2
LC
659
660 ;; XXX: At this point this remains faster than `port-sha256', because
661 ;; the SHA256 port's `write' method gets called for every single
662 ;; character.
de4c3f26 663 (sha256
be4e38fb 664 (string->utf8 (derivation->string drv)))))))))
77d3cf08 665
26bbbb95
LC
666(define (store-path type hash name) ; makeStorePath
667 "Return the store path for NAME/HASH/TYPE."
668 (let* ((s (string-append type ":sha256:"
669 (bytevector->base16-string hash) ":"
670 (%store-prefix) ":" name))
671 (h (sha256 (string->utf8 s)))
672 (c (compressed-hash h 20)))
673 (string-append (%store-prefix) "/"
674 (bytevector->nix-base32-string c) "-"
675 name)))
676
677(define (output-path output hash name) ; makeOutputPath
678 "Return an output path for OUTPUT (the name of the output as a string) of
679the derivation called NAME with hash HASH."
680 (store-path (string-append "output:" output) hash
681 (if (string=? output "out")
682 name
683 (string-append name "-" output))))
684
36bbbbd1
LC
685(define (fixed-output-path output hash-algo hash recursive? name)
686 "Return an output path for the fixed output OUTPUT defined by HASH of type
687HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
688'add-to-store'."
689 (if (and recursive? (eq? hash-algo 'sha256))
690 (store-path "source" hash name)
691 (let ((tag (string-append "fixed:" output ":"
692 (if recursive? "r:" "")
693 (symbol->string hash-algo) ":"
694 (bytevector->base16-string hash) ":")))
695 (store-path (string-append "output:" output)
696 (sha256 (string->utf8 tag))
697 name))))
698
a987d2c0
LC
699(define* (derivation store name builder args
700 #:key
701 (system (%current-system)) (env-vars '())
702 (inputs '()) (outputs '("out"))
2096ef47 703 hash hash-algo recursive?
35b5ca78
LC
704 references-graphs
705 allowed-references disallowed-references
4a6aeb67
LC
706 leaked-env-vars local-build?
707 (substitutable? #t))
59688fc4 708 "Build a derivation with the given arguments, and return the resulting
2096ef47 709<derivation> object. When HASH and HASH-ALGO are given, a
59688fc4 710fixed-output derivation is created---i.e., one whose result is known in
36bbbbd1
LC
711advance, such as a file download. If, in addition, RECURSIVE? is true, then
712that fixed output may be an executable file or a directory and HASH must be
713the hash of an archive containing this output.
5b0c9d16 714
858e9282 715When REFERENCES-GRAPHS is true, it must be a list of file name/store path
5b0c9d16 716pairs. In that case, the reference graph of each store path is exported in
1909431c
LC
717the build environment in the corresponding file, in a simple text format.
718
b53be755 719When ALLOWED-REFERENCES is true, it must be a list of store items or outputs
35b5ca78
LC
720that the derivation's outputs may refer to. Likewise, DISALLOWED-REFERENCES,
721if true, must be a list of things the outputs may not refer to.
b53be755 722
c0468155
LC
723When LEAKED-ENV-VARS is true, it must be a list of strings denoting
724environment variables that are allowed to \"leak\" from the daemon's
725environment to the build environment. This is only applicable to fixed-output
726derivations--i.e., when HASH is true. The main use is to allow variables such
727as \"http_proxy\" to be passed to derivations that download files.
728
1909431c
LC
729When LOCAL-BUILD? is true, declare that the derivation is not a good candidate
730for offloading and should rather be built locally. This is the case for small
4a6aeb67
LC
731derivations where the costs of data transfers would outweigh the benefits.
732
733When SUBSTITUTABLE? is false, declare that substitutes of the derivation's
734output should not be used."
26bbbb95
LC
735 (define (add-output-paths drv)
736 ;; Return DRV with an actual store path for each of its output and the
737 ;; corresponding environment variable.
738 (match drv
739 (($ <derivation> outputs inputs sources
740 system builder args env-vars)
741 (let* ((drv-hash (derivation-hash drv))
742 (outputs (map (match-lambda
d9085c23 743 ((output-name . ($ <derivation-output>
36bbbbd1
LC
744 _ algo hash rec?))
745 (let ((path (if hash
746 (fixed-output-path output-name
747 algo hash
748 rec? name)
749 (output-path output-name
750 drv-hash name))))
d9085c23
LC
751 (cons output-name
752 (make-derivation-output path algo
36bbbbd1 753 hash rec?)))))
d9085c23 754 outputs)))
26bbbb95
LC
755 (make-derivation outputs inputs sources system builder args
756 (map (match-lambda
757 ((name . value)
758 (cons name
759 (or (and=> (assoc-ref outputs name)
760 derivation-output-path)
761 value))))
6a446d56
LC
762 env-vars)
763 #f)))))
26bbbb95 764
5b0c9d16
LC
765 (define (user+system-env-vars)
766 ;; Some options are passed to the build daemon via the env. vars of
767 ;; derivations (urgh!). We hide that from our API, but here is the place
768 ;; where we kludgify those options.
b53be755
LC
769 (let ((env-vars `(,@(if local-build?
770 `(("preferLocalBuild" . "1"))
771 '())
4a6aeb67
LC
772 ,@(if (not substitutable?)
773 `(("allowSubstitutes" . "0"))
774 '())
b53be755
LC
775 ,@(if allowed-references
776 `(("allowedReferences"
777 . ,(string-join allowed-references)))
778 '())
35b5ca78
LC
779 ,@(if disallowed-references
780 `(("disallowedReferences"
781 . ,(string-join disallowed-references)))
782 '())
c0468155
LC
783 ,@(if leaked-env-vars
784 `(("impureEnvVars"
785 . ,(string-join leaked-env-vars)))
786 '())
b53be755 787 ,@env-vars)))
1909431c
LC
788 (match references-graphs
789 (((file . path) ...)
790 (let ((value (map (cut string-append <> " " <>)
791 file path)))
792 ;; XXX: This all breaks down if an element of FILE or PATH contains
793 ;; white space.
794 `(("exportReferencesGraph" . ,(string-join value " "))
795 ,@env-vars)))
796 (#f
797 env-vars))))
5b0c9d16
LC
798
799 (define (env-vars-with-empty-outputs env-vars)
26bbbb95 800 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
561eaf71 801 ;; empty string, even outputs that do not appear in ENV-VARS.
26bbbb95
LC
802 (let ((e (map (match-lambda
803 ((name . val)
804 (if (member name outputs)
805 (cons name "")
806 (cons name val))))
807 env-vars)))
561eaf71
LC
808 (fold (lambda (output-name env-vars)
809 (if (assoc output-name env-vars)
810 env-vars
811 (append env-vars `((,output-name . "")))))
812 e
813 outputs)))
26bbbb95 814
6a446d56
LC
815 (define (set-file-name drv file)
816 ;; Set FILE as the 'file-name' field of DRV.
817 (match drv
818 (($ <derivation> outputs inputs sources system builder
819 args env-vars)
820 (make-derivation outputs inputs sources system builder
821 args env-vars file))))
822
26bbbb95
LC
823 (let* ((outputs (map (lambda (name)
824 ;; Return outputs with an empty path.
825 (cons name
36bbbbd1
LC
826 (make-derivation-output "" hash-algo
827 hash recursive?)))
26bbbb95
LC
828 outputs))
829 (inputs (map (match-lambda
59688fc4
LC
830 (((? derivation? drv))
831 (make-derivation-input (derivation-file-name drv)
832 '("out")))
833 (((? derivation? drv) sub-drvs ...)
834 (make-derivation-input (derivation-file-name drv)
835 sub-drvs))
200dc937 836 (((? direct-store-path? input))
de4c3f26 837 (make-derivation-input input '("out")))
200dc937 838 (((? direct-store-path? input) sub-drvs ...)
26bbbb95
LC
839 (make-derivation-input input sub-drvs))
840 ((input . _)
841 (let ((path (add-to-store store
842 (basename input)
a9ebd9ef 843 #t "sha256" input)))
26bbbb95 844 (make-derivation-input path '()))))
d26ad5e4 845 (delete-duplicates inputs)))
5b0c9d16 846 (env-vars (env-vars-with-empty-outputs (user+system-env-vars)))
26bbbb95
LC
847 (drv-masked (make-derivation outputs
848 (filter (compose derivation-path?
849 derivation-input-path)
850 inputs)
851 (filter-map (lambda (i)
852 (let ((p (derivation-input-path i)))
853 (and (not (derivation-path? p))
854 p)))
855 inputs)
6a446d56 856 system builder args env-vars #f))
26bbbb95 857 (drv (add-output-paths drv-masked)))
de4c3f26 858
6a446d56 859 (let ((file (add-text-to-store store (string-append name ".drv")
be4e38fb 860 (derivation->string drv)
6a446d56
LC
861 (map derivation-input-path
862 inputs))))
59688fc4
LC
863 (set-file-name drv file))))
864
e387ab7c
LC
865(define* (map-derivation store drv mapping
866 #:key (system (%current-system)))
867 "Given MAPPING, a list of pairs of derivations, return a derivation based on
868DRV where all the 'car's of MAPPING have been replaced by its 'cdr's,
869recursively."
870 (define (substitute str initial replacements)
871 (fold (lambda (path replacement result)
872 (string-replace-substring result path
873 replacement))
874 str
875 initial replacements))
876
877 (define (substitute-file file initial replacements)
878 (define contents
879 (with-fluids ((%default-port-encoding #f))
880 (call-with-input-file file get-string-all)))
881
882 (let ((updated (substitute contents initial replacements)))
883 (if (string=? updated contents)
884 file
885 ;; XXX: permissions aren't preserved.
886 (add-text-to-store store (store-path-package-name file)
887 updated))))
888
889 (define input->output-paths
890 (match-lambda
a716e36d 891 (((? derivation? drv))
e387ab7c 892 (list (derivation->output-path drv)))
a716e36d 893 (((? derivation? drv) sub-drvs ...)
e387ab7c 894 (map (cut derivation->output-path drv <>)
a716e36d
LC
895 sub-drvs))
896 ((file)
897 (list file))))
e387ab7c
LC
898
899 (let ((mapping (fold (lambda (pair result)
900 (match pair
a716e36d 901 (((? derivation? orig) . replacement)
e387ab7c 902 (vhash-cons (derivation-file-name orig)
a716e36d
LC
903 replacement result))
904 ((file . replacement)
905 (vhash-cons file replacement result))))
e387ab7c
LC
906 vlist-null
907 mapping)))
908 (define rewritten-input
909 ;; Rewrite the given input according to MAPPING, and return an input
910 ;; in the format used in 'derivation' calls.
911 (memoize
912 (lambda (input loop)
913 (match input
914 (($ <derivation-input> path (sub-drvs ...))
915 (match (vhash-assoc path mapping)
a716e36d 916 ((_ . (? derivation? replacement))
e387ab7c 917 (cons replacement sub-drvs))
a716e36d
LC
918 ((_ . replacement)
919 (list replacement))
e387ab7c
LC
920 (#f
921 (let* ((drv (loop (call-with-input-file path read-derivation))))
922 (cons drv sub-drvs)))))))))
923
924 (let loop ((drv drv))
925 (let* ((inputs (map (cut rewritten-input <> loop)
926 (derivation-inputs drv)))
927 (initial (append-map derivation-input-output-paths
928 (derivation-inputs drv)))
929 (replacements (append-map input->output-paths inputs))
930
931 ;; Sources typically refer to the output directories of the
932 ;; original inputs, INITIAL. Rewrite them by substituting
933 ;; REPLACEMENTS.
a716e36d
LC
934 (sources (map (lambda (source)
935 (match (vhash-assoc source mapping)
936 ((_ . replacement)
937 replacement)
938 (#f
939 (substitute-file source
940 initial replacements))))
e387ab7c
LC
941 (derivation-sources drv)))
942
943 ;; Now augment the lists of initials and replacements.
944 (initial (append (derivation-sources drv) initial))
945 (replacements (append sources replacements))
946 (name (store-path-package-name
947 (string-drop-right (derivation-file-name drv)
948 4))))
949 (derivation store name
950 (substitute (derivation-builder drv)
951 initial replacements)
952 (map (cut substitute <> initial replacements)
953 (derivation-builder-arguments drv))
954 #:system system
955 #:env-vars (map (match-lambda
956 ((var . value)
957 `(,var
958 . ,(substitute value initial
959 replacements))))
960 (derivation-builder-environment-vars drv))
961 #:inputs (append (map list sources) inputs)
0b6af195 962 #:outputs (derivation-output-names drv)
e387ab7c
LC
963 #:hash (match (derivation-outputs drv)
964 ((($ <derivation-output> _ algo hash))
965 hash)
966 (_ #f))
967 #:hash-algo (match (derivation-outputs drv)
968 ((($ <derivation-output> _ algo hash))
969 algo)
970 (_ #f)))))))
971
59688fc4
LC
972\f
973;;;
974;;; Store compatibility layer.
975;;;
976
a8d65643
LC
977(define* (build-derivations store derivations
978 #:optional (mode (build-mode normal)))
979 "Build DERIVATIONS, a list of <derivation> objects or .drv file names, using
980the specified MODE."
01d8ac64
LC
981 (build-things store (map (match-lambda
982 ((? string? file) file)
983 ((and drv ($ <derivation>))
984 (derivation-file-name drv)))
a8d65643
LC
985 derivations)
986 mode))
d9085c23
LC
987
988\f
989;;;
990;;; Guile-based builders.
991;;;
992
d9024884
LC
993(define (parent-directories file-name)
994 "Return the list of parent dirs of FILE-NAME, in the order in which an
995`mkdir -p' implementation would make them."
996 (let ((not-slash (char-set-complement (char-set #\/))))
997 (reverse
998 (fold (lambda (dir result)
999 (match result
1000 (()
1001 (list dir))
1002 ((prev _ ...)
1003 (cons (string-append prev "/" dir)
1004 result))))
1005 '()
1006 (remove (cut string=? <> ".")
1007 (string-tokenize (dirname file-name) not-slash))))))
1008
aa72d9af 1009(define* (imported-files store files ;deprecated
b272c474
LC
1010 #:key (name "file-import")
1011 (system (%current-system))
1012 (guile (%guile-for-build)))
99634e3f
LC
1013 "Return a derivation that imports FILES into STORE. FILES must be a list
1014of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
1015system, imported, and appears under FINAL-PATH in the resulting store path."
99634e3f
LC
1016 (let* ((files (map (match-lambda
1017 ((final-path . file-name)
2acb2cb6 1018 (list final-path
a9ebd9ef 1019 (add-to-store store (basename final-path) #f
99634e3f
LC
1020 "sha256" file-name))))
1021 files))
1022 (builder
1023 `(begin
1024 (mkdir %output) (chdir %output)
1025 ,@(append-map (match-lambda
2acb2cb6 1026 ((final-path store-path)
d9024884 1027 (append (match (parent-directories final-path)
99634e3f
LC
1028 (() '())
1029 ((head ... tail)
1030 (append (map (lambda (d)
1031 `(false-if-exception
1032 (mkdir ,d)))
1033 head)
224f7ad6
LC
1034 `((or (file-exists? ,tail)
1035 (mkdir ,tail))))))
99634e3f
LC
1036 `((symlink ,store-path ,final-path)))))
1037 files))))
dd1a5a15
LC
1038 (build-expression->derivation store name builder
1039 #:system system
1040 #:inputs files
6ce206cb
LC
1041 #:guile-for-build guile
1042 #:local-build? #t)))
99634e3f 1043
d26e1967
LC
1044;; The "file not found" error condition.
1045(define-condition-type &file-search-error &error
1046 file-search-error?
1047 (file file-search-error-file-name)
1048 (path file-search-error-search-path))
1049
8601d0dd
LC
1050(define search-path*
1051 ;; A memoizing version of 'search-path' so 'imported-modules' does not end
1052 ;; up looking for the same files over and over again.
d26e1967
LC
1053 (memoize (lambda (path file)
1054 "Search for FILE in PATH and memoize the result. Raise a
1055'&file-search-error' condition if it could not be found."
1056 (or (search-path path file)
1057 (raise (condition
1058 (&file-search-error (file file)
1059 (path path))))))))
8601d0dd 1060
6985335f
LC
1061(define (module->source-file-name module)
1062 "Return the file name corresponding to MODULE, a Guile module name (a list
1063of symbols.)"
1064 (string-append (string-join (map symbol->string module) "/")
1065 ".scm"))
1066
aa72d9af 1067(define* (%imported-modules store modules ;deprecated
e87f0591
LC
1068 #:key (name "module-import")
1069 (system (%current-system))
1070 (guile (%guile-for-build))
1071 (module-path %load-path))
3eb98237 1072 "Return a derivation that contains the source files of MODULES, a list of
8dcb0c55 1073module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
3eb98237
LC
1074search path."
1075 ;; TODO: Determine the closure of MODULES, build the `.go' files,
1076 ;; canonicalize the source files through read/write, etc.
1077 (let ((files (map (lambda (m)
6985335f 1078 (let ((f (module->source-file-name m)))
8601d0dd 1079 (cons f (search-path* module-path f))))
3eb98237 1080 modules)))
b272c474
LC
1081 (imported-files store files #:name name #:system system
1082 #:guile guile)))
3eb98237 1083
aa72d9af 1084(define* (%compiled-modules store modules ;deprecated
e87f0591
LC
1085 #:key (name "module-import-compiled")
1086 (system (%current-system))
1087 (guile (%guile-for-build))
1088 (module-path %load-path))
d9024884
LC
1089 "Return a derivation that builds a tree containing the `.go' files
1090corresponding to MODULES. All the MODULES are built in a context where
1091they can refer to each other."
e87f0591
LC
1092 (let* ((module-drv (%imported-modules store modules
1093 #:system system
1094 #:guile guile
1095 #:module-path module-path))
59688fc4 1096 (module-dir (derivation->output-path module-drv))
d9024884
LC
1097 (files (map (lambda (m)
1098 (let ((f (string-join (map symbol->string m)
1099 "/")))
1100 (cons (string-append f ".go")
1101 (string-append module-dir "/" f ".scm"))))
1102 modules)))
1103 (define builder
1104 `(begin
1105 (use-modules (system base compile))
1106 (let ((out (assoc-ref %outputs "out")))
1107 (mkdir out)
1108 (chdir out))
1109
1110 (set! %load-path
1111 (cons ,module-dir %load-path))
1112
1113 ,@(map (match-lambda
1114 ((output . input)
1115 (let ((make-parent-dirs (map (lambda (dir)
1116 `(unless (file-exists? ,dir)
1117 (mkdir ,dir)))
1118 (parent-directories output))))
1119 `(begin
1120 ,@make-parent-dirs
1121 (compile-file ,input
1122 #:output-file ,output
1123 #:opts %auto-compilation-options)))))
1124 files)))
1125
dd1a5a15
LC
1126 (build-expression->derivation store name builder
1127 #:inputs `(("modules" ,module-drv))
1128 #:system system
6ce206cb
LC
1129 #:guile-for-build guile
1130 #:local-build? #t)))
3eb98237 1131
aa72d9af 1132(define* (build-expression->derivation store name exp ;deprecated
dd1a5a15
LC
1133 #:key
1134 (system (%current-system))
1135 (inputs '())
1136 (outputs '("out"))
36bbbbd1 1137 hash hash-algo recursive?
4c1eddf7 1138 (env-vars '())
6dd7787c 1139 (modules '())
9c629a27 1140 guile-for-build
1909431c 1141 references-graphs
63a42824 1142 allowed-references
35b5ca78 1143 disallowed-references
4a6aeb67 1144 local-build? (substitutable? #t))
874e6874
LC
1145 "Return a derivation that executes Scheme expression EXP as a builder
1146for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
1147tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
1148of names of Guile modules from the current search path to be copied in
1149the store, compiled, and made available in the load path during the
1150execution of EXP.
1151
1152EXP is evaluated in an environment where %OUTPUT is bound to the main
1153output path, %OUTPUTS is bound to a list of output/path pairs, and where
1154%BUILD-INPUTS is bound to an alist of string/output-path pairs made from
1155INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
1156name and value of environment variables visible to the builder. The
1157builder terminates by passing the result of EXP to `exit'; thus, when
1158EXP returns #f, the build is considered to have failed.
6dd7787c
LC
1159
1160EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
9c629a27
LC
1161omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
1162
63a42824 1163See the `derivation' procedure for the meaning of REFERENCES-GRAPHS,
35b5ca78 1164ALLOWED-REFERENCES, DISALLOWED-REFERENCES, LOCAL-BUILD?, and SUBSTITUTABLE?."
b272c474
LC
1165 (define guile-drv
1166 (or guile-for-build (%guile-for-build)))
1167
d9085c23 1168 (define guile
59688fc4 1169 (string-append (derivation->output-path guile-drv)
d9085c23
LC
1170 "/bin/guile"))
1171
0d56a551
LC
1172 (define module-form?
1173 (match-lambda
a987d2c0
LC
1174 (((or 'define-module 'use-modules) _ ...) #t)
1175 (_ #f)))
0d56a551 1176
7bdd1f0e
LC
1177 (define source-path
1178 ;; When passed an input that is a source, return its path; otherwise
1179 ;; return #f.
1180 (match-lambda
59688fc4
LC
1181 ((_ (? derivation?) _ ...)
1182 #f)
7bdd1f0e
LC
1183 ((_ path _ ...)
1184 (and (not (derivation-path? path))
1185 path))))
1186
d9085c23 1187 (let* ((prologue `(begin
0d56a551
LC
1188 ,@(match exp
1189 ((_ ...)
1190 ;; Module forms must appear at the top-level so
1191 ;; that any macros they export can be expanded.
1192 (filter module-form? exp))
1193 (_ `(,exp)))
1194
d9085c23 1195 (define %output (getenv "out"))
9bc07f4d
LC
1196 (define %outputs
1197 (map (lambda (o)
1198 (cons o (getenv o)))
1199 ',outputs))
d9085c23
LC
1200 (define %build-inputs
1201 ',(map (match-lambda
2acb2cb6
LC
1202 ((name drv . rest)
1203 (let ((sub (match rest
1204 (() "out")
1205 ((x) x))))
1206 (cons name
59688fc4
LC
1207 (cond
1208 ((derivation? drv)
1209 (derivation->output-path drv sub))
1210 ((derivation-path? drv)
1211 (derivation-path->output-path drv
1212 sub))
1213 (else drv))))))
d44bc84b
LC
1214 inputs))
1215
d9024884
LC
1216 ,@(if (null? modules)
1217 '()
1218 ;; Remove our own settings.
1219 '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
1220
d44bc84b
LC
1221 ;; Guile sets it, but remove it to avoid conflicts when
1222 ;; building Guile-using packages.
1223 (unsetenv "LD_LIBRARY_PATH")))
d9085c23
LC
1224 (builder (add-text-to-store store
1225 (string-append name "-guile-builder")
0bb1aa9e
LC
1226
1227 ;; Explicitly use UTF-8 for determinism,
1228 ;; and also because UTF-8 output is faster.
1229 (with-fluids ((%default-port-encoding
1230 "UTF-8"))
1231 (call-with-output-string
1232 (lambda (port)
1233 (write prologue port)
1234 (write
1235 `(exit
1236 ,(match exp
1237 ((_ ...)
1238 (remove module-form? exp))
1239 (_ `(,exp))))
1240 port))))
7bdd1f0e
LC
1241
1242 ;; The references don't really matter
1243 ;; since the builder is always used in
1244 ;; conjunction with the drv that needs
1245 ;; it. For clarity, we add references
1246 ;; to the subset of INPUTS that are
1247 ;; sources, avoiding references to other
1248 ;; .drv; otherwise, BUILDER's hash would
1249 ;; depend on those, even if they are
1250 ;; fixed-output.
1251 (filter-map source-path inputs)))
1252
d9024884 1253 (mod-drv (and (pair? modules)
e87f0591
LC
1254 (%imported-modules store modules
1255 #:guile guile-drv
1256 #:system system)))
3eb98237 1257 (mod-dir (and mod-drv
59688fc4 1258 (derivation->output-path mod-drv)))
d9024884 1259 (go-drv (and (pair? modules)
e87f0591
LC
1260 (%compiled-modules store modules
1261 #:guile guile-drv
1262 #:system system)))
d9024884 1263 (go-dir (and go-drv
59688fc4 1264 (derivation->output-path go-drv))))
a987d2c0 1265 (derivation store name guile
3eb98237
LC
1266 `("--no-auto-compile"
1267 ,@(if mod-dir `("-L" ,mod-dir) '())
1268 ,builder)
d9024884 1269
a987d2c0
LC
1270 #:system system
1271
1272 #:inputs `((,(or guile-for-build (%guile-for-build)))
1273 (,builder)
1274 ,@(map cdr inputs)
1275 ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
1276
d9024884
LC
1277 ;; When MODULES is non-empty, shamelessly clobber
1278 ;; $GUILE_LOAD_COMPILED_PATH.
a987d2c0
LC
1279 #:env-vars (if go-dir
1280 `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
1281 ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
1282 env-vars))
1283 env-vars)
1284
9bc07f4d 1285 #:hash hash #:hash-algo hash-algo
36bbbbd1 1286 #:recursive? recursive?
9c629a27 1287 #:outputs outputs
1909431c 1288 #:references-graphs references-graphs
63a42824 1289 #:allowed-references allowed-references
35b5ca78 1290 #:disallowed-references disallowed-references
4a6aeb67
LC
1291 #:local-build? local-build?
1292 #:substitutable? substitutable?)))
e87f0591
LC
1293
1294\f
1295;;;
1296;;; Monadic interface.
1297;;;
1298
1299(define built-derivations
1300 (store-lift build-derivations))