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