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