gnu: Update python to 2.7.5.
[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"))
5b0c9d16
LC
504 hash hash-algo hash-mode
505 dependency-graphs)
26bbbb95 506 "Build a derivation with the given arguments. Return the resulting
fb3eec83 507store path and <derivation> object. When HASH, HASH-ALGO, and HASH-MODE
26bbbb95 508are given, a fixed-output derivation is created---i.e., one whose result is
5b0c9d16
LC
509known in advance, such as a file download.
510
511When DEPENDENCY-GRAPHS is true, it must be a list of file name/store path
512pairs. In that case, the reference graph of each store path is exported in
513the build environment in the corresponding file, in a simple text format."
200dc937
LC
514 (define direct-store-path?
515 (let ((len (+ 1 (string-length (%store-prefix)))))
516 (lambda (p)
517 ;; Return #t if P is a store path, and not a sub-directory of a
518 ;; store path. This predicate is needed because files *under* a
519 ;; store path are not valid inputs.
520 (and (store-path? p)
521 (not (string-index (substring p len) #\/))))))
522
26bbbb95
LC
523 (define (add-output-paths drv)
524 ;; Return DRV with an actual store path for each of its output and the
525 ;; corresponding environment variable.
526 (match drv
527 (($ <derivation> outputs inputs sources
528 system builder args env-vars)
529 (let* ((drv-hash (derivation-hash drv))
530 (outputs (map (match-lambda
d9085c23
LC
531 ((output-name . ($ <derivation-output>
532 _ algo hash))
533 (let ((path (output-path output-name
534 drv-hash name)))
535 (cons output-name
536 (make-derivation-output path algo
537 hash)))))
538 outputs)))
26bbbb95
LC
539 (make-derivation outputs inputs sources system builder args
540 (map (match-lambda
541 ((name . value)
542 (cons name
543 (or (and=> (assoc-ref outputs name)
544 derivation-output-path)
545 value))))
546 env-vars))))))
547
5b0c9d16
LC
548 (define (user+system-env-vars)
549 ;; Some options are passed to the build daemon via the env. vars of
550 ;; derivations (urgh!). We hide that from our API, but here is the place
551 ;; where we kludgify those options.
552 (match dependency-graphs
553 (((file . path) ...)
554 (let ((value (map (cut string-append <> " " <>)
555 file path)))
556 ;; XXX: This all breaks down if an element of FILE or PATH contains
557 ;; white space.
558 `(("exportReferencesGraph" . ,(string-join value " "))
559 ,@env-vars)))
560 (#f
561 env-vars)))
562
563 (define (env-vars-with-empty-outputs env-vars)
26bbbb95 564 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
561eaf71 565 ;; empty string, even outputs that do not appear in ENV-VARS.
26bbbb95
LC
566 (let ((e (map (match-lambda
567 ((name . val)
568 (if (member name outputs)
569 (cons name "")
570 (cons name val))))
571 env-vars)))
561eaf71
LC
572 (fold (lambda (output-name env-vars)
573 (if (assoc output-name env-vars)
574 env-vars
575 (append env-vars `((,output-name . "")))))
576 e
577 outputs)))
26bbbb95
LC
578
579 (let* ((outputs (map (lambda (name)
580 ;; Return outputs with an empty path.
581 (cons name
582 (make-derivation-output "" hash-algo hash)))
583 outputs))
584 (inputs (map (match-lambda
200dc937 585 (((? direct-store-path? input))
de4c3f26 586 (make-derivation-input input '("out")))
200dc937 587 (((? direct-store-path? input) sub-drvs ...)
26bbbb95
LC
588 (make-derivation-input input sub-drvs))
589 ((input . _)
590 (let ((path (add-to-store store
591 (basename input)
a9ebd9ef 592 #t "sha256" input)))
26bbbb95 593 (make-derivation-input path '()))))
d26ad5e4 594 (delete-duplicates inputs)))
5b0c9d16 595 (env-vars (env-vars-with-empty-outputs (user+system-env-vars)))
26bbbb95
LC
596 (drv-masked (make-derivation outputs
597 (filter (compose derivation-path?
598 derivation-input-path)
599 inputs)
600 (filter-map (lambda (i)
601 (let ((p (derivation-input-path i)))
602 (and (not (derivation-path? p))
603 p)))
604 inputs)
605 system builder args env-vars))
606 (drv (add-output-paths drv-masked)))
de4c3f26 607
d66ac374
LC
608 ;; (write-derivation drv-masked (current-error-port))
609 ;; (newline (current-error-port))
fb3eec83
LC
610 (values (add-text-to-store store (string-append name ".drv")
611 (call-with-output-string
612 (cut write-derivation drv <>))
613 (map derivation-input-path
614 inputs))
615 drv)))
d9085c23
LC
616
617\f
618;;;
619;;; Guile-based builders.
620;;;
621
622(define %guile-for-build
623 ;; The derivation of the Guile to be used within the build environment,
624 ;; when using `build-expression->derivation'.
b272c474 625 (make-parameter #f))
d9085c23 626
d9024884
LC
627(define (parent-directories file-name)
628 "Return the list of parent dirs of FILE-NAME, in the order in which an
629`mkdir -p' implementation would make them."
630 (let ((not-slash (char-set-complement (char-set #\/))))
631 (reverse
632 (fold (lambda (dir result)
633 (match result
634 (()
635 (list dir))
636 ((prev _ ...)
637 (cons (string-append prev "/" dir)
638 result))))
639 '()
640 (remove (cut string=? <> ".")
641 (string-tokenize (dirname file-name) not-slash))))))
642
99634e3f 643(define* (imported-files store files
b272c474
LC
644 #:key (name "file-import")
645 (system (%current-system))
646 (guile (%guile-for-build)))
99634e3f
LC
647 "Return a derivation that imports FILES into STORE. FILES must be a list
648of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
649system, imported, and appears under FINAL-PATH in the resulting store path."
99634e3f
LC
650 (let* ((files (map (match-lambda
651 ((final-path . file-name)
2acb2cb6 652 (list final-path
a9ebd9ef 653 (add-to-store store (basename final-path) #f
99634e3f
LC
654 "sha256" file-name))))
655 files))
656 (builder
657 `(begin
658 (mkdir %output) (chdir %output)
659 ,@(append-map (match-lambda
2acb2cb6 660 ((final-path store-path)
d9024884 661 (append (match (parent-directories final-path)
99634e3f
LC
662 (() '())
663 ((head ... tail)
664 (append (map (lambda (d)
665 `(false-if-exception
666 (mkdir ,d)))
667 head)
224f7ad6
LC
668 `((or (file-exists? ,tail)
669 (mkdir ,tail))))))
99634e3f
LC
670 `((symlink ,store-path ,final-path)))))
671 files))))
ae39d1b2 672 (build-expression->derivation store name system
b272c474
LC
673 builder files
674 #:guile-for-build guile)))
99634e3f 675
3eb98237
LC
676(define* (imported-modules store modules
677 #:key (name "module-import")
b272c474 678 (system (%current-system))
8dcb0c55
LC
679 (guile (%guile-for-build))
680 (module-path %load-path))
3eb98237 681 "Return a derivation that contains the source files of MODULES, a list of
8dcb0c55 682module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
3eb98237
LC
683search path."
684 ;; TODO: Determine the closure of MODULES, build the `.go' files,
685 ;; canonicalize the source files through read/write, etc.
686 (let ((files (map (lambda (m)
687 (let ((f (string-append
688 (string-join (map symbol->string m) "/")
689 ".scm")))
8dcb0c55 690 (cons f (search-path module-path f))))
3eb98237 691 modules)))
b272c474
LC
692 (imported-files store files #:name name #:system system
693 #:guile guile)))
3eb98237 694
d9024884
LC
695(define* (compiled-modules store modules
696 #:key (name "module-import-compiled")
b272c474 697 (system (%current-system))
8dcb0c55
LC
698 (guile (%guile-for-build))
699 (module-path %load-path))
d9024884
LC
700 "Return a derivation that builds a tree containing the `.go' files
701corresponding to MODULES. All the MODULES are built in a context where
702they can refer to each other."
703 (let* ((module-drv (imported-modules store modules
b272c474 704 #:system system
8dcb0c55
LC
705 #:guile guile
706 #:module-path module-path))
d9024884
LC
707 (module-dir (derivation-path->output-path module-drv))
708 (files (map (lambda (m)
709 (let ((f (string-join (map symbol->string m)
710 "/")))
711 (cons (string-append f ".go")
712 (string-append module-dir "/" f ".scm"))))
713 modules)))
714 (define builder
715 `(begin
716 (use-modules (system base compile))
717 (let ((out (assoc-ref %outputs "out")))
718 (mkdir out)
719 (chdir out))
720
721 (set! %load-path
722 (cons ,module-dir %load-path))
723
724 ,@(map (match-lambda
725 ((output . input)
726 (let ((make-parent-dirs (map (lambda (dir)
727 `(unless (file-exists? ,dir)
728 (mkdir ,dir)))
729 (parent-directories output))))
730 `(begin
731 ,@make-parent-dirs
732 (compile-file ,input
733 #:output-file ,output
734 #:opts %auto-compilation-options)))))
735 files)))
736
737 (build-expression->derivation store name system builder
b272c474
LC
738 `(("modules" ,module-drv))
739 #:guile-for-build guile)))
3eb98237 740
d9085c23 741(define* (build-expression->derivation store name system exp inputs
9bc07f4d 742 #:key (outputs '("out"))
3eb98237 743 hash hash-algo
4c1eddf7 744 (env-vars '())
6dd7787c 745 (modules '())
9c629a27
LC
746 guile-for-build
747 dependency-graphs)
874e6874
LC
748 "Return a derivation that executes Scheme expression EXP as a builder
749for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
750tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
751of names of Guile modules from the current search path to be copied in
752the store, compiled, and made available in the load path during the
753execution of EXP.
754
755EXP is evaluated in an environment where %OUTPUT is bound to the main
756output path, %OUTPUTS is bound to a list of output/path pairs, and where
757%BUILD-INPUTS is bound to an alist of string/output-path pairs made from
758INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
759name and value of environment variables visible to the builder. The
760builder terminates by passing the result of EXP to `exit'; thus, when
761EXP returns #f, the build is considered to have failed.
6dd7787c
LC
762
763EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
9c629a27
LC
764omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
765
766See the `derivation' procedure for the meaning of DEPENDENCY-GRAPHS."
b272c474
LC
767 (define guile-drv
768 (or guile-for-build (%guile-for-build)))
769
d9085c23 770 (define guile
b272c474 771 (string-append (derivation-path->output-path guile-drv)
d9085c23
LC
772 "/bin/guile"))
773
0d56a551
LC
774 (define module-form?
775 (match-lambda
a987d2c0
LC
776 (((or 'define-module 'use-modules) _ ...) #t)
777 (_ #f)))
0d56a551 778
7bdd1f0e
LC
779 (define source-path
780 ;; When passed an input that is a source, return its path; otherwise
781 ;; return #f.
782 (match-lambda
783 ((_ path _ ...)
784 (and (not (derivation-path? path))
785 path))))
786
d9085c23 787 (let* ((prologue `(begin
0d56a551
LC
788 ,@(match exp
789 ((_ ...)
790 ;; Module forms must appear at the top-level so
791 ;; that any macros they export can be expanded.
792 (filter module-form? exp))
793 (_ `(,exp)))
794
d9085c23 795 (define %output (getenv "out"))
9bc07f4d
LC
796 (define %outputs
797 (map (lambda (o)
798 (cons o (getenv o)))
799 ',outputs))
d9085c23
LC
800 (define %build-inputs
801 ',(map (match-lambda
2acb2cb6
LC
802 ((name drv . rest)
803 (let ((sub (match rest
804 (() "out")
805 ((x) x))))
806 (cons name
807 (if (derivation-path? drv)
808 (derivation-path->output-path drv
809 sub)
810 drv)))))
d44bc84b
LC
811 inputs))
812
d9024884
LC
813 ,@(if (null? modules)
814 '()
815 ;; Remove our own settings.
816 '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
817
d44bc84b
LC
818 ;; Guile sets it, but remove it to avoid conflicts when
819 ;; building Guile-using packages.
820 (unsetenv "LD_LIBRARY_PATH")))
d9085c23
LC
821 (builder (add-text-to-store store
822 (string-append name "-guile-builder")
0bb1aa9e
LC
823
824 ;; Explicitly use UTF-8 for determinism,
825 ;; and also because UTF-8 output is faster.
826 (with-fluids ((%default-port-encoding
827 "UTF-8"))
828 (call-with-output-string
829 (lambda (port)
830 (write prologue port)
831 (write
832 `(exit
833 ,(match exp
834 ((_ ...)
835 (remove module-form? exp))
836 (_ `(,exp))))
837 port))))
7bdd1f0e
LC
838
839 ;; The references don't really matter
840 ;; since the builder is always used in
841 ;; conjunction with the drv that needs
842 ;; it. For clarity, we add references
843 ;; to the subset of INPUTS that are
844 ;; sources, avoiding references to other
845 ;; .drv; otherwise, BUILDER's hash would
846 ;; depend on those, even if they are
847 ;; fixed-output.
848 (filter-map source-path inputs)))
849
d9024884 850 (mod-drv (and (pair? modules)
ae39d1b2
LC
851 (imported-modules store modules
852 #:guile guile-drv
853 #:system system)))
3eb98237 854 (mod-dir (and mod-drv
d9024884
LC
855 (derivation-path->output-path mod-drv)))
856 (go-drv (and (pair? modules)
ae39d1b2
LC
857 (compiled-modules store modules
858 #:guile guile-drv
859 #:system system)))
d9024884
LC
860 (go-dir (and go-drv
861 (derivation-path->output-path go-drv))))
a987d2c0 862 (derivation store name guile
3eb98237
LC
863 `("--no-auto-compile"
864 ,@(if mod-dir `("-L" ,mod-dir) '())
865 ,builder)
d9024884 866
a987d2c0
LC
867 #:system system
868
869 #:inputs `((,(or guile-for-build (%guile-for-build)))
870 (,builder)
871 ,@(map cdr inputs)
872 ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
873
d9024884
LC
874 ;; When MODULES is non-empty, shamelessly clobber
875 ;; $GUILE_LOAD_COMPILED_PATH.
a987d2c0
LC
876 #:env-vars (if go-dir
877 `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
878 ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
879 env-vars))
880 env-vars)
881
9bc07f4d 882 #:hash hash #:hash-algo hash-algo
9c629a27
LC
883 #:outputs outputs
884 #:dependency-graphs dependency-graphs)))