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