derivations: Move 3 positional parameters into keyword parameters.
[jackhill/guix/guix.git] / guix / derivations.scm
CommitLineData
233e7676
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012, 2013 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)
22 #:use-module (srfi srfi-26)
23 #:use-module (rnrs io ports)
24 #:use-module (rnrs bytevectors)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 rdelim)
69f90f5c 27 #:use-module (guix store)
26bbbb95 28 #:use-module (guix utils)
72626a71 29 #:use-module (guix hash)
ddc29a78 30 #:use-module (guix base32)
9a20830e
LC
31 #:export (<derivation>
32 derivation?
77d3cf08
LC
33 derivation-outputs
34 derivation-inputs
35 derivation-sources
36 derivation-system
37 derivation-builder-arguments
38 derivation-builder-environment-vars
9a20830e
LC
39 derivation-prerequisites
40 derivation-prerequisites-to-build
77d3cf08 41
9a20830e 42 <derivation-output>
77d3cf08
LC
43 derivation-output?
44 derivation-output-path
45 derivation-output-hash-algo
46 derivation-output-hash
47
9a20830e 48 <derivation-input>
77d3cf08
LC
49 derivation-input?
50 derivation-input-path
51 derivation-input-sub-derivations
dd36b51b 52 derivation-input-output-paths
77d3cf08
LC
53
54 fixed-output-derivation?
341c6fdd
LC
55 derivation-hash
56
57 read-derivation
26bbbb95 58 write-derivation
de4c3f26 59 derivation-path->output-path
7244a5f7 60 derivation-path->output-paths
d9085c23
LC
61 derivation
62
63 %guile-for-build
99634e3f
LC
64 build-expression->derivation
65 imported-files))
77d3cf08
LC
66
67;;;
68;;; Nix derivations, as implemented in Nix's `derivations.cc'.
69;;;
70
71(define-record-type <derivation>
72 (make-derivation outputs inputs sources system builder args env-vars)
73 derivation?
74 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
75 (inputs derivation-inputs) ; list of <derivation-input>
76 (sources derivation-sources) ; list of store paths
77 (system derivation-system) ; string
78 (builder derivation-builder) ; store path
79 (args derivation-builder-arguments) ; list of strings
80 (env-vars derivation-builder-environment-vars)) ; list of name/value pairs
81
82(define-record-type <derivation-output>
83 (make-derivation-output path hash-algo hash)
84 derivation-output?
85 (path derivation-output-path) ; store path
86 (hash-algo derivation-output-hash-algo) ; symbol | #f
749c6567 87 (hash derivation-output-hash)) ; bytevector | #f
77d3cf08
LC
88
89(define-record-type <derivation-input>
90 (make-derivation-input path sub-derivations)
91 derivation-input?
92 (path derivation-input-path) ; store path
93 (sub-derivations derivation-input-sub-derivations)) ; list of strings
94
95(define (fixed-output-derivation? drv)
96 "Return #t if DRV is a fixed-output derivation, such as the result of a
97download with a fixed hash (aka. `fetchurl')."
98 (match drv
99 (($ <derivation>
100 (($ <derivation-output> _ (? symbol?) (? string?))))
101 #t)
102 (_ #f)))
103
dd36b51b
LC
104(define (derivation-input-output-paths input)
105 "Return the list of output paths corresponding to INPUT, a
106<derivation-input>."
107 (match input
108 (($ <derivation-input> path sub-drvs)
109 (map (cut derivation-path->output-path path <>)
110 sub-drvs))))
111
9a20830e
LC
112(define (derivation-prerequisites drv)
113 "Return the list of derivation-inputs required to build DRV, recursively."
114 (let loop ((drv drv)
115 (result '()))
116 (let ((inputs (remove (cut member <> result) ; XXX: quadratic
117 (derivation-inputs drv))))
118 (fold loop
119 (append inputs result)
120 (map (lambda (i)
121 (call-with-input-file (derivation-input-path i)
122 read-derivation))
123 inputs)))))
124
784bb1f3 125(define* (derivation-prerequisites-to-build store drv
dd36b51b
LC
126 #:key
127 (outputs
128 (map
129 car
130 (derivation-outputs drv)))
131 (use-substitutes? #t))
132 "Return two values: the list of derivation-inputs required to build the
133OUTPUTS of DRV and not already available in STORE, recursively, and the list
134of required store paths that can be substituted. When USE-SUBSTITUTES? is #f,
135that second value is the empty list."
136 (define (derivation-output-paths drv sub-drvs)
137 (match drv
138 (($ <derivation> outputs)
139 (map (lambda (sub-drv)
140 (derivation-output-path (assoc-ref outputs sub-drv)))
141 sub-drvs))))
142
784bb1f3
LC
143 (define built?
144 (cut valid-path? store <>))
145
dd36b51b
LC
146 (define substitutable?
147 ;; Return true if the given path is substitutable. Call
148 ;; `substitutable-paths' upfront, to benefit from parallelism in the
149 ;; substituter.
150 (if use-substitutes?
151 (let ((s (substitutable-paths store
152 (append
153 (derivation-output-paths drv outputs)
154 (append-map
155 derivation-input-output-paths
156 (derivation-prerequisites drv))))))
157 (cut member <> s))
158 (const #f)))
159
9a20830e 160 (define input-built?
dd36b51b
LC
161 (compose (cut any built? <>) derivation-input-output-paths))
162
163 (define input-substitutable?
164 ;; Return true if and only if all of SUB-DRVS are subsitutable. If at
165 ;; least one is missing, then everything must be rebuilt.
166 (compose (cut every substitutable? <>) derivation-input-output-paths))
784bb1f3
LC
167
168 (define (derivation-built? drv sub-drvs)
dd36b51b
LC
169 (every built? (derivation-output-paths drv sub-drvs)))
170
171 (define (derivation-substitutable? drv sub-drvs)
172 (every substitutable? (derivation-output-paths drv sub-drvs)))
173
174 (let loop ((drv drv)
175 (sub-drvs outputs)
176 (build '())
177 (substitute '()))
178 (cond ((derivation-built? drv sub-drvs)
179 (values build substitute))
180 ((derivation-substitutable? drv sub-drvs)
181 (values build
182 (append (derivation-output-paths drv sub-drvs)
183 substitute)))
184 (else
185 (let ((inputs (remove (lambda (i)
186 (or (member i build) ; XXX: quadratic
187 (input-built? i)
188 (input-substitutable? i)))
189 (derivation-inputs drv))))
190 (fold2 loop
191 (append inputs build)
192 (append (append-map (lambda (input)
193 (if (and (not (input-built? input))
194 (input-substitutable? input))
195 (derivation-input-output-paths
196 input)
197 '()))
198 (derivation-inputs drv))
199 substitute)
200 (map (lambda (i)
201 (call-with-input-file (derivation-input-path i)
202 read-derivation))
203 inputs)
204 (map derivation-input-sub-derivations inputs)))))))
9a20830e 205
d0840e4a
LC
206(define (%read-derivation drv-port)
207 ;; Actually read derivation from DRV-PORT.
77d3cf08
LC
208
209 (define comma (string->symbol ","))
210
211 (define (ununquote x)
212 (match x
213 (('unquote x) (ununquote x))
214 ((x ...) (map ununquote x))
215 (_ x)))
216
217 (define (outputs->alist x)
218 (fold-right (lambda (output result)
219 (match output
220 ((name path "" "")
221 (alist-cons name
222 (make-derivation-output path #f #f)
223 result))
224 ((name path hash-algo hash)
225 ;; fixed-output
749c6567
LC
226 (let ((algo (string->symbol hash-algo))
227 (hash (base16-string->bytevector hash)))
77d3cf08
LC
228 (alist-cons name
229 (make-derivation-output path algo hash)
230 result)))))
231 '()
232 x))
233
234 (define (make-input-drvs x)
235 (fold-right (lambda (input result)
236 (match input
237 ((path (sub-drvs ...))
238 (cons (make-derivation-input path sub-drvs)
239 result))))
240 '()
241 x))
242
df7bbd38
LC
243 ;; The contents of a derivation are typically ASCII, but choosing
244 ;; UTF-8 allows us to take the fast path for Guile's `scm_getc'.
245 (set-port-encoding! drv-port "UTF-8")
246
77d3cf08
LC
247 (let loop ((exp (read drv-port))
248 (result '()))
249 (match exp
250 ((? eof-object?)
251 (let ((result (reverse result)))
252 (match result
253 (('Derive ((outputs ...) (input-drvs ...)
254 (input-srcs ...)
255 (? string? system)
256 (? string? builder)
257 ((? string? args) ...)
258 ((var value) ...)))
259 (make-derivation (outputs->alist outputs)
260 (make-input-drvs input-drvs)
261 input-srcs
262 system builder args
263 (fold-right alist-cons '() var value)))
264 (_
265 (error "failed to parse derivation" drv-port result)))))
266 ((? (cut eq? <> comma))
267 (loop (read drv-port) result))
268 (_
269 (loop (read drv-port)
270 (cons (ununquote exp) result))))))
271
d0840e4a
LC
272(define read-derivation
273 (let ((cache (make-weak-value-hash-table 200)))
274 (lambda (drv-port)
275 "Read the derivation from DRV-PORT and return the corresponding
276<derivation> object."
277 ;; Memoize that operation because `%read-derivation' is quite expensive,
278 ;; and because the same argument is read more than 15 times on average
279 ;; during something like (package-derivation s gdb).
280 (let ((file (and=> (port-filename drv-port) basename)))
281 (or (and file (hash-ref cache file))
282 (let ((drv (%read-derivation drv-port)))
283 (hash-set! cache file drv)
284 drv))))))
285
d8085599
LC
286(define-inlinable (write-sequence lst write-item port)
287 ;; Write each element of LST with WRITE-ITEM to PORT, separating them with a
288 ;; comma.
289 (match lst
290 (()
291 #t)
292 ((prefix (... ...) last)
293 (for-each (lambda (item)
294 (write-item item port)
295 (display "," port))
296 prefix)
297 (write-item last port))))
298
299(define-inlinable (write-list lst write-item port)
300 ;; Write LST as a derivation list to PORT, using WRITE-ITEM to write each
301 ;; element.
302 (display "[" port)
303 (write-sequence lst write-item port)
304 (display "]" port))
305
306(define-inlinable (write-tuple lst write-item port)
307 ;; Same, but write LST as a tuple.
308 (display "(" port)
309 (write-sequence lst write-item port)
310 (display ")" port))
311
77d3cf08
LC
312(define (write-derivation drv port)
313 "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
314Eelco Dolstra's PhD dissertation for an overview of a previous version of
315that form."
aaa848f3
LC
316
317 ;; Make sure we're using the faster implementation.
318 (define format simple-format)
319
d8085599
LC
320 (define (write-string-list lst)
321 (write-list lst write port))
77d3cf08 322
d66ac374
LC
323 (define (coalesce-duplicate-inputs inputs)
324 ;; Return a list of inputs, such that when INPUTS contains the same DRV
325 ;; twice, they are coalesced, with their sub-derivations merged. This is
326 ;; needed because Nix itself keeps only one of them.
327 (fold (lambda (input result)
328 (match input
329 (($ <derivation-input> path sub-drvs)
330 ;; XXX: quadratic
331 (match (find (match-lambda
332 (($ <derivation-input> p s)
333 (string=? p path)))
334 result)
335 (#f
336 (cons input result))
337 ((and dup ($ <derivation-input> _ sub-drvs2))
338 ;; Merge DUP with INPUT.
339 (let ((sub-drvs (delete-duplicates
340 (append sub-drvs sub-drvs2))))
341 (cons (make-derivation-input path sub-drvs)
342 (delq dup result))))))))
343 '()
344 inputs))
345
d8085599
LC
346 (define (write-output output port)
347 (match output
348 ((name . ($ <derivation-output> path hash-algo hash))
349 (write-tuple (list name path
350 (or (and=> hash-algo symbol->string) "")
351 (or (and=> hash bytevector->base16-string)
352 ""))
353 write
354 port))))
355
356 (define (write-input input port)
357 (match input
358 (($ <derivation-input> path sub-drvs)
359 (display "(" port)
360 (write path port)
361 (display "," port)
362 (write-string-list (sort sub-drvs string<?))
363 (display ")" port))))
364
365 (define (write-env-var env-var port)
366 (match env-var
367 ((name . value)
368 (display "(" port)
369 (write name port)
370 (display "," port)
371 (write value port)
372 (display ")" port))))
373
561eaf71
LC
374 ;; Note: lists are sorted alphabetically, to conform with the behavior of
375 ;; C++ `std::map' in Nix itself.
376
77d3cf08
LC
377 (match drv
378 (($ <derivation> outputs inputs sources
379 system builder args env-vars)
380 (display "Derive(" port)
d8085599
LC
381 (write-list (sort outputs
382 (lambda (o1 o2)
383 (string<? (car o1) (car o2))))
384 write-output
385 port)
77d3cf08 386 (display "," port)
d8085599
LC
387 (write-list (sort (coalesce-duplicate-inputs inputs)
388 (lambda (i1 i2)
389 (string<? (derivation-input-path i1)
390 (derivation-input-path i2))))
391 write-input
392 port)
77d3cf08 393 (display "," port)
d8085599 394 (write-string-list (sort sources string<?))
77d3cf08 395 (format port ",~s,~s," system builder)
d8085599 396 (write-string-list args)
77d3cf08 397 (display "," port)
d8085599
LC
398 (write-list (sort env-vars
399 (lambda (e1 e2)
400 (string<? (car e1) (car e2))))
401 write-env-var
402 port)
77d3cf08
LC
403 (display ")" port))))
404
aaa848f3
LC
405(define derivation-path->output-path
406 ;; This procedure is called frequently, so memoize it.
407 (memoize
408 (lambda* (path #:optional (output "out"))
409 "Read the derivation from PATH (`/nix/store/xxx.drv'), and return the store
de4c3f26 410path of its output OUTPUT."
aaa848f3
LC
411 (let* ((drv (call-with-input-file path read-derivation))
412 (outputs (derivation-outputs drv)))
413 (and=> (assoc-ref outputs output) derivation-output-path)))))
de4c3f26 414
7244a5f7
LC
415(define (derivation-path->output-paths path)
416 "Read the derivation from PATH (`/nix/store/xxx.drv'), and return the
417list of name/path pairs of its outputs."
418 (let* ((drv (call-with-input-file path read-derivation))
419 (outputs (derivation-outputs drv)))
420 (map (match-lambda
421 ((name . output)
422 (cons name (derivation-output-path output))))
423 outputs)))
424
de4c3f26
LC
425\f
426;;;
427;;; Derivation primitive.
428;;;
429
26bbbb95
LC
430(define (compressed-hash bv size) ; `compressHash'
431 "Given the hash stored in BV, return a compressed version thereof that fits
432in SIZE bytes."
433 (define new (make-bytevector size 0))
434 (define old-size (bytevector-length bv))
435 (let loop ((i 0))
436 (if (= i old-size)
437 new
438 (let* ((j (modulo i size))
439 (o (bytevector-u8-ref new j)))
440 (bytevector-u8-set! new j
441 (logxor o (bytevector-u8-ref bv i)))
442 (loop (+ 1 i))))))
77d3cf08 443
de4c3f26
LC
444(define derivation-hash ; `hashDerivationModulo' in derivations.cc
445 (memoize
446 (lambda (drv)
447 "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
448 (match drv
449 (($ <derivation> ((_ . ($ <derivation-output> path
749c6567 450 (? symbol? hash-algo) (? bytevector? hash)))))
de4c3f26 451 ;; A fixed-output derivation.
77d3cf08 452 (sha256
de4c3f26
LC
453 (string->utf8
454 (string-append "fixed:out:" (symbol->string hash-algo)
749c6567
LC
455 ":" (bytevector->base16-string hash)
456 ":" path))))
de4c3f26
LC
457 (($ <derivation> outputs inputs sources
458 system builder args env-vars)
459 ;; A regular derivation: replace the path of each input with that
460 ;; input's hash; return the hash of serialization of the resulting
561eaf71
LC
461 ;; derivation.
462 (let* ((inputs (map (match-lambda
463 (($ <derivation-input> path sub-drvs)
464 (let ((hash (call-with-input-file path
465 (compose bytevector->base16-string
466 derivation-hash
467 read-derivation))))
468 (make-derivation-input hash sub-drvs))))
469 inputs))
470 (drv (make-derivation outputs inputs sources
471 system builder args env-vars)))
b0fad8a2
LC
472
473 ;; XXX: At this point this remains faster than `port-sha256', because
474 ;; the SHA256 port's `write' method gets called for every single
475 ;; character.
de4c3f26 476 (sha256
0bd31a21
LC
477 (with-fluids ((%default-port-encoding "UTF-8"))
478 (string->utf8 (call-with-output-string
479 (cut write-derivation drv <>)))))))))))
77d3cf08 480
26bbbb95
LC
481(define (store-path type hash name) ; makeStorePath
482 "Return the store path for NAME/HASH/TYPE."
483 (let* ((s (string-append type ":sha256:"
484 (bytevector->base16-string hash) ":"
485 (%store-prefix) ":" name))
486 (h (sha256 (string->utf8 s)))
487 (c (compressed-hash h 20)))
488 (string-append (%store-prefix) "/"
489 (bytevector->nix-base32-string c) "-"
490 name)))
491
492(define (output-path output hash name) ; makeOutputPath
493 "Return an output path for OUTPUT (the name of the output as a string) of
494the derivation called NAME with hash HASH."
495 (store-path (string-append "output:" output) hash
496 (if (string=? output "out")
497 name
498 (string-append name "-" output))))
499
a987d2c0
LC
500(define* (derivation store name builder args
501 #:key
502 (system (%current-system)) (env-vars '())
503 (inputs '()) (outputs '("out"))
504 hash hash-algo hash-mode)
26bbbb95 505 "Build a derivation with the given arguments. Return the resulting
fb3eec83 506store path and <derivation> object. When HASH, HASH-ALGO, and HASH-MODE
26bbbb95
LC
507are given, a fixed-output derivation is created---i.e., one whose result is
508known in advance, such as a file download."
200dc937
LC
509 (define direct-store-path?
510 (let ((len (+ 1 (string-length (%store-prefix)))))
511 (lambda (p)
512 ;; Return #t if P is a store path, and not a sub-directory of a
513 ;; store path. This predicate is needed because files *under* a
514 ;; store path are not valid inputs.
515 (and (store-path? p)
516 (not (string-index (substring p len) #\/))))))
517
26bbbb95
LC
518 (define (add-output-paths drv)
519 ;; Return DRV with an actual store path for each of its output and the
520 ;; corresponding environment variable.
521 (match drv
522 (($ <derivation> outputs inputs sources
523 system builder args env-vars)
524 (let* ((drv-hash (derivation-hash drv))
525 (outputs (map (match-lambda
d9085c23
LC
526 ((output-name . ($ <derivation-output>
527 _ algo hash))
528 (let ((path (output-path output-name
529 drv-hash name)))
530 (cons output-name
531 (make-derivation-output path algo
532 hash)))))
533 outputs)))
26bbbb95
LC
534 (make-derivation outputs inputs sources system builder args
535 (map (match-lambda
536 ((name . value)
537 (cons name
538 (or (and=> (assoc-ref outputs name)
539 derivation-output-path)
540 value))))
541 env-vars))))))
542
543 (define (env-vars-with-empty-outputs)
544 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
561eaf71 545 ;; empty string, even outputs that do not appear in ENV-VARS.
26bbbb95
LC
546 (let ((e (map (match-lambda
547 ((name . val)
548 (if (member name outputs)
549 (cons name "")
550 (cons name val))))
551 env-vars)))
561eaf71
LC
552 (fold (lambda (output-name env-vars)
553 (if (assoc output-name env-vars)
554 env-vars
555 (append env-vars `((,output-name . "")))))
556 e
557 outputs)))
26bbbb95
LC
558
559 (let* ((outputs (map (lambda (name)
560 ;; Return outputs with an empty path.
561 (cons name
562 (make-derivation-output "" hash-algo hash)))
563 outputs))
564 (inputs (map (match-lambda
200dc937 565 (((? direct-store-path? input))
de4c3f26 566 (make-derivation-input input '("out")))
200dc937 567 (((? direct-store-path? input) sub-drvs ...)
26bbbb95
LC
568 (make-derivation-input input sub-drvs))
569 ((input . _)
570 (let ((path (add-to-store store
571 (basename input)
a9ebd9ef 572 #t "sha256" input)))
26bbbb95 573 (make-derivation-input path '()))))
d26ad5e4 574 (delete-duplicates inputs)))
26bbbb95
LC
575 (env-vars (env-vars-with-empty-outputs))
576 (drv-masked (make-derivation outputs
577 (filter (compose derivation-path?
578 derivation-input-path)
579 inputs)
580 (filter-map (lambda (i)
581 (let ((p (derivation-input-path i)))
582 (and (not (derivation-path? p))
583 p)))
584 inputs)
585 system builder args env-vars))
586 (drv (add-output-paths drv-masked)))
de4c3f26 587
d66ac374
LC
588 ;; (write-derivation drv-masked (current-error-port))
589 ;; (newline (current-error-port))
fb3eec83
LC
590 (values (add-text-to-store store (string-append name ".drv")
591 (call-with-output-string
592 (cut write-derivation drv <>))
593 (map derivation-input-path
594 inputs))
595 drv)))
d9085c23
LC
596
597\f
598;;;
599;;; Guile-based builders.
600;;;
601
602(define %guile-for-build
603 ;; The derivation of the Guile to be used within the build environment,
604 ;; when using `build-expression->derivation'.
b272c474 605 (make-parameter #f))
d9085c23 606
d9024884
LC
607(define (parent-directories file-name)
608 "Return the list of parent dirs of FILE-NAME, in the order in which an
609`mkdir -p' implementation would make them."
610 (let ((not-slash (char-set-complement (char-set #\/))))
611 (reverse
612 (fold (lambda (dir result)
613 (match result
614 (()
615 (list dir))
616 ((prev _ ...)
617 (cons (string-append prev "/" dir)
618 result))))
619 '()
620 (remove (cut string=? <> ".")
621 (string-tokenize (dirname file-name) not-slash))))))
622
99634e3f 623(define* (imported-files store files
b272c474
LC
624 #:key (name "file-import")
625 (system (%current-system))
626 (guile (%guile-for-build)))
99634e3f
LC
627 "Return a derivation that imports FILES into STORE. FILES must be a list
628of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
629system, imported, and appears under FINAL-PATH in the resulting store path."
99634e3f
LC
630 (let* ((files (map (match-lambda
631 ((final-path . file-name)
2acb2cb6 632 (list final-path
a9ebd9ef 633 (add-to-store store (basename final-path) #f
99634e3f
LC
634 "sha256" file-name))))
635 files))
636 (builder
637 `(begin
638 (mkdir %output) (chdir %output)
639 ,@(append-map (match-lambda
2acb2cb6 640 ((final-path store-path)
d9024884 641 (append (match (parent-directories final-path)
99634e3f
LC
642 (() '())
643 ((head ... tail)
644 (append (map (lambda (d)
645 `(false-if-exception
646 (mkdir ,d)))
647 head)
224f7ad6
LC
648 `((or (file-exists? ,tail)
649 (mkdir ,tail))))))
99634e3f
LC
650 `((symlink ,store-path ,final-path)))))
651 files))))
ae39d1b2 652 (build-expression->derivation store name system
b272c474
LC
653 builder files
654 #:guile-for-build guile)))
99634e3f 655
3eb98237
LC
656(define* (imported-modules store modules
657 #:key (name "module-import")
b272c474 658 (system (%current-system))
8dcb0c55
LC
659 (guile (%guile-for-build))
660 (module-path %load-path))
3eb98237 661 "Return a derivation that contains the source files of MODULES, a list of
8dcb0c55 662module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
3eb98237
LC
663search path."
664 ;; TODO: Determine the closure of MODULES, build the `.go' files,
665 ;; canonicalize the source files through read/write, etc.
666 (let ((files (map (lambda (m)
667 (let ((f (string-append
668 (string-join (map symbol->string m) "/")
669 ".scm")))
8dcb0c55 670 (cons f (search-path module-path f))))
3eb98237 671 modules)))
b272c474
LC
672 (imported-files store files #:name name #:system system
673 #:guile guile)))
3eb98237 674
d9024884
LC
675(define* (compiled-modules store modules
676 #:key (name "module-import-compiled")
b272c474 677 (system (%current-system))
8dcb0c55
LC
678 (guile (%guile-for-build))
679 (module-path %load-path))
d9024884
LC
680 "Return a derivation that builds a tree containing the `.go' files
681corresponding to MODULES. All the MODULES are built in a context where
682they can refer to each other."
683 (let* ((module-drv (imported-modules store modules
b272c474 684 #:system system
8dcb0c55
LC
685 #:guile guile
686 #:module-path module-path))
d9024884
LC
687 (module-dir (derivation-path->output-path module-drv))
688 (files (map (lambda (m)
689 (let ((f (string-join (map symbol->string m)
690 "/")))
691 (cons (string-append f ".go")
692 (string-append module-dir "/" f ".scm"))))
693 modules)))
694 (define builder
695 `(begin
696 (use-modules (system base compile))
697 (let ((out (assoc-ref %outputs "out")))
698 (mkdir out)
699 (chdir out))
700
701 (set! %load-path
702 (cons ,module-dir %load-path))
703
704 ,@(map (match-lambda
705 ((output . input)
706 (let ((make-parent-dirs (map (lambda (dir)
707 `(unless (file-exists? ,dir)
708 (mkdir ,dir)))
709 (parent-directories output))))
710 `(begin
711 ,@make-parent-dirs
712 (compile-file ,input
713 #:output-file ,output
714 #:opts %auto-compilation-options)))))
715 files)))
716
717 (build-expression->derivation store name system builder
b272c474
LC
718 `(("modules" ,module-drv))
719 #:guile-for-build guile)))
3eb98237 720
d9085c23 721(define* (build-expression->derivation store name system exp inputs
9bc07f4d 722 #:key (outputs '("out"))
3eb98237 723 hash hash-algo
4c1eddf7 724 (env-vars '())
6dd7787c
LC
725 (modules '())
726 guile-for-build)
874e6874
LC
727 "Return a derivation that executes Scheme expression EXP as a builder
728for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
729tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
730of names of Guile modules from the current search path to be copied in
731the store, compiled, and made available in the load path during the
732execution of EXP.
733
734EXP is evaluated in an environment where %OUTPUT is bound to the main
735output path, %OUTPUTS is bound to a list of output/path pairs, and where
736%BUILD-INPUTS is bound to an alist of string/output-path pairs made from
737INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
738name and value of environment variables visible to the builder. The
739builder terminates by passing the result of EXP to `exit'; thus, when
740EXP returns #f, the build is considered to have failed.
6dd7787c
LC
741
742EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
743omitted or is #f, the value of the `%guile-for-build' fluid is used instead."
b272c474
LC
744 (define guile-drv
745 (or guile-for-build (%guile-for-build)))
746
d9085c23 747 (define guile
b272c474 748 (string-append (derivation-path->output-path guile-drv)
d9085c23
LC
749 "/bin/guile"))
750
0d56a551
LC
751 (define module-form?
752 (match-lambda
a987d2c0
LC
753 (((or 'define-module 'use-modules) _ ...) #t)
754 (_ #f)))
0d56a551 755
7bdd1f0e
LC
756 (define source-path
757 ;; When passed an input that is a source, return its path; otherwise
758 ;; return #f.
759 (match-lambda
760 ((_ path _ ...)
761 (and (not (derivation-path? path))
762 path))))
763
d9085c23 764 (let* ((prologue `(begin
0d56a551
LC
765 ,@(match exp
766 ((_ ...)
767 ;; Module forms must appear at the top-level so
768 ;; that any macros they export can be expanded.
769 (filter module-form? exp))
770 (_ `(,exp)))
771
d9085c23 772 (define %output (getenv "out"))
9bc07f4d
LC
773 (define %outputs
774 (map (lambda (o)
775 (cons o (getenv o)))
776 ',outputs))
d9085c23
LC
777 (define %build-inputs
778 ',(map (match-lambda
2acb2cb6
LC
779 ((name drv . rest)
780 (let ((sub (match rest
781 (() "out")
782 ((x) x))))
783 (cons name
784 (if (derivation-path? drv)
785 (derivation-path->output-path drv
786 sub)
787 drv)))))
d44bc84b
LC
788 inputs))
789
d9024884
LC
790 ,@(if (null? modules)
791 '()
792 ;; Remove our own settings.
793 '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
794
d44bc84b
LC
795 ;; Guile sets it, but remove it to avoid conflicts when
796 ;; building Guile-using packages.
797 (unsetenv "LD_LIBRARY_PATH")))
d9085c23
LC
798 (builder (add-text-to-store store
799 (string-append name "-guile-builder")
0bb1aa9e
LC
800
801 ;; Explicitly use UTF-8 for determinism,
802 ;; and also because UTF-8 output is faster.
803 (with-fluids ((%default-port-encoding
804 "UTF-8"))
805 (call-with-output-string
806 (lambda (port)
807 (write prologue port)
808 (write
809 `(exit
810 ,(match exp
811 ((_ ...)
812 (remove module-form? exp))
813 (_ `(,exp))))
814 port))))
7bdd1f0e
LC
815
816 ;; The references don't really matter
817 ;; since the builder is always used in
818 ;; conjunction with the drv that needs
819 ;; it. For clarity, we add references
820 ;; to the subset of INPUTS that are
821 ;; sources, avoiding references to other
822 ;; .drv; otherwise, BUILDER's hash would
823 ;; depend on those, even if they are
824 ;; fixed-output.
825 (filter-map source-path inputs)))
826
d9024884 827 (mod-drv (and (pair? modules)
ae39d1b2
LC
828 (imported-modules store modules
829 #:guile guile-drv
830 #:system system)))
3eb98237 831 (mod-dir (and mod-drv
d9024884
LC
832 (derivation-path->output-path mod-drv)))
833 (go-drv (and (pair? modules)
ae39d1b2
LC
834 (compiled-modules store modules
835 #:guile guile-drv
836 #:system system)))
d9024884
LC
837 (go-dir (and go-drv
838 (derivation-path->output-path go-drv))))
a987d2c0 839 (derivation store name guile
3eb98237
LC
840 `("--no-auto-compile"
841 ,@(if mod-dir `("-L" ,mod-dir) '())
842 ,builder)
d9024884 843
a987d2c0
LC
844 #:system system
845
846 #:inputs `((,(or guile-for-build (%guile-for-build)))
847 (,builder)
848 ,@(map cdr inputs)
849 ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
850
d9024884
LC
851 ;; When MODULES is non-empty, shamelessly clobber
852 ;; $GUILE_LOAD_COMPILED_PATH.
a987d2c0
LC
853 #:env-vars (if go-dir
854 `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
855 ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
856 env-vars))
857 env-vars)
858
9bc07f4d
LC
859 #:hash hash #:hash-algo hash-algo
860 #:outputs outputs)))