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