derivations: Introduce 'graft' record type.
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
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 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
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
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (test-derivations)
20 #:use-module (guix derivations)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix hash)
24 #:use-module (guix base32)
25 #:use-module (guix tests)
26 #:use-module ((guix packages) #:select (package-derivation base32))
27 #:use-module ((guix build utils) #:select (executable-file?))
28 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
29 #:use-module (gnu packages bootstrap)
30 #:use-module ((gnu packages guile) #:select (guile-1.8))
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-11)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-64)
36 #:use-module (rnrs io ports)
37 #:use-module (rnrs bytevectors)
38 #:use-module (web uri)
39 #:use-module (ice-9 rdelim)
40 #:use-module (ice-9 regex)
41 #:use-module (ice-9 ftw)
42 #:use-module (ice-9 match))
43
44 (define %store
45 (open-connection-for-tests))
46
47 (define (bootstrap-binary name)
48 (let ((bin (search-bootstrap-binary name (%current-system))))
49 (and %store
50 (add-to-store %store name #t "sha256" bin))))
51
52 (define %bash
53 (bootstrap-binary "bash"))
54 (define %mkdir
55 (bootstrap-binary "mkdir"))
56
57 (define* (directory-contents dir #:optional (slurp get-bytevector-all))
58 "Return an alist representing the contents of DIR."
59 (define prefix-len (string-length dir))
60 (sort (file-system-fold (const #t) ; enter?
61 (lambda (path stat result) ; leaf
62 (alist-cons (string-drop path prefix-len)
63 (call-with-input-file path slurp)
64 result))
65 (lambda (path stat result) result) ; down
66 (lambda (path stat result) result) ; up
67 (lambda (path stat result) result) ; skip
68 (lambda (path stat errno result) result) ; error
69 '()
70 dir)
71 (lambda (e1 e2)
72 (string<? (car e1) (car e2)))))
73
74 (test-begin "derivations")
75
76 (test-assert "parse & export"
77 (let* ((f (search-path %load-path "tests/test.drv"))
78 (b1 (call-with-input-file f get-bytevector-all))
79 (d1 (read-derivation (open-bytevector-input-port b1)))
80 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
81 (d2 (read-derivation (open-bytevector-input-port b2))))
82 (and (equal? b1 b2)
83 (equal? d1 d2))))
84
85 (test-skip (if %store 0 12))
86
87 (test-assert "add-to-store, flat"
88 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
89 (drv (add-to-store %store "flat-test" #f "sha256" file)))
90 (and (eq? 'regular (stat:type (stat drv)))
91 (valid-path? %store drv)
92 (equal? (call-with-input-file file get-bytevector-all)
93 (call-with-input-file drv get-bytevector-all)))))
94
95 (test-assert "add-to-store, recursive"
96 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
97 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
98 (and (eq? 'directory (stat:type (stat drv)))
99 (valid-path? %store drv)
100 (equal? (directory-contents dir)
101 (directory-contents drv)))))
102
103 (test-assert "derivation with no inputs"
104 (let* ((builder (add-text-to-store %store "my-builder.sh"
105 "echo hello, world\n"
106 '()))
107 (drv (derivation %store "foo"
108 %bash `("-e" ,builder)
109 #:env-vars '(("HOME" . "/homeless")))))
110 (and (store-path? (derivation-file-name drv))
111 (valid-path? %store (derivation-file-name drv)))))
112
113 (test-assert "build derivation with 1 source"
114 (let* ((builder (add-text-to-store %store "my-builder.sh"
115 "echo hello, world > \"$out\"\n"
116 '()))
117 (drv (derivation %store "foo"
118 %bash `(,builder)
119 #:env-vars '(("HOME" . "/homeless")
120 ("zzz" . "Z!")
121 ("AAA" . "A!"))
122 #:inputs `((,%bash) (,builder))))
123 (succeeded?
124 (build-derivations %store (list drv))))
125 (and succeeded?
126 (let ((path (derivation->output-path drv)))
127 (and (valid-path? %store path)
128 (string=? (call-with-input-file path read-line)
129 "hello, world"))))))
130
131 (test-assert "derivation with local file as input"
132 (let* ((builder (add-text-to-store
133 %store "my-builder.sh"
134 "(while read line ; do echo \"$line\" ; done) < $in > $out"
135 '()))
136 (input (search-path %load-path "ice-9/boot-9.scm"))
137 (input* (add-to-store %store (basename input)
138 #t "sha256" input))
139 (drv (derivation %store "derivation-with-input-file"
140 %bash `(,builder)
141
142 ;; Cheat to pass the actual file name to the
143 ;; builder.
144 #:env-vars `(("in" . ,input*))
145
146 #:inputs `((,%bash)
147 (,builder)
148 (,input))))) ; ← local file name
149 (and (build-derivations %store (list drv))
150 ;; Note: we can't compare the files because the above trick alters
151 ;; the contents.
152 (valid-path? %store (derivation->output-path drv)))))
153
154 (test-assert "identical files are deduplicated"
155 (let* ((build1 (add-text-to-store %store "one.sh"
156 "echo hello, world > \"$out\"\n"
157 '()))
158 (build2 (add-text-to-store %store "two.sh"
159 "# Hey!\necho hello, world > \"$out\"\n"
160 '()))
161 (drv1 (derivation %store "foo"
162 %bash `(,build1)
163 #:inputs `((,%bash) (,build1))))
164 (drv2 (derivation %store "bar"
165 %bash `(,build2)
166 #:inputs `((,%bash) (,build2)))))
167 (and (build-derivations %store (list drv1 drv2))
168 (let ((file1 (derivation->output-path drv1))
169 (file2 (derivation->output-path drv2)))
170 (and (valid-path? %store file1) (valid-path? %store file2)
171 (string=? (call-with-input-file file1 get-string-all)
172 "hello, world\n")
173 (= (stat:ino (lstat file1))
174 (stat:ino (lstat file2))))))))
175
176 (test-assert "fixed-output-derivation?"
177 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
178 "echo -n hello > $out" '()))
179 (hash (sha256 (string->utf8 "hello")))
180 (drv (derivation %store "fixed"
181 %bash `(,builder)
182 #:inputs `((,builder))
183 #:hash hash #:hash-algo 'sha256)))
184 (fixed-output-derivation? drv)))
185
186 (test-assert "fixed-output derivation"
187 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
188 "echo -n hello > $out" '()))
189 (hash (sha256 (string->utf8 "hello")))
190 (drv (derivation %store "fixed"
191 %bash `(,builder)
192 #:inputs `((,builder)) ; optional
193 #:hash hash #:hash-algo 'sha256))
194 (succeeded? (build-derivations %store (list drv))))
195 (and succeeded?
196 (let ((p (derivation->output-path drv)))
197 (and (equal? (string->utf8 "hello")
198 (call-with-input-file p get-bytevector-all))
199 (bytevector? (query-path-hash %store p)))))))
200
201 (test-assert "fixed-output derivation: output paths are equal"
202 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
203 "echo -n hello > $out" '()))
204 (builder2 (add-text-to-store %store "fixed-builder2.sh"
205 "echo hey; echo -n hello > $out" '()))
206 (hash (sha256 (string->utf8 "hello")))
207 (drv1 (derivation %store "fixed"
208 %bash `(,builder1)
209 #:hash hash #:hash-algo 'sha256))
210 (drv2 (derivation %store "fixed"
211 %bash `(,builder2)
212 #:hash hash #:hash-algo 'sha256))
213 (succeeded? (build-derivations %store (list drv1 drv2))))
214 (and succeeded?
215 (equal? (derivation->output-path drv1)
216 (derivation->output-path drv2)))))
217
218 (test-assert "fixed-output derivation, recursive"
219 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
220 "echo -n hello > $out" '()))
221 (hash (sha256 (string->utf8 "hello")))
222 (drv (derivation %store "fixed-rec"
223 %bash `(,builder)
224 #:inputs `((,builder))
225 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
226 #:hash-algo 'sha256
227 #:recursive? #t))
228 (succeeded? (build-derivations %store (list drv))))
229 (and succeeded?
230 (let ((p (derivation->output-path drv)))
231 (and (equal? (string->utf8 "hello")
232 (call-with-input-file p get-bytevector-all))
233 (bytevector? (query-path-hash %store p)))))))
234
235 (test-assert "derivation with a fixed-output input"
236 ;; A derivation D using a fixed-output derivation F doesn't has the same
237 ;; output path when passed F or F', as long as F and F' have the same output
238 ;; path.
239 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
240 "echo -n hello > $out" '()))
241 (builder2 (add-text-to-store %store "fixed-builder2.sh"
242 "echo hey; echo -n hello > $out" '()))
243 (hash (sha256 (string->utf8 "hello")))
244 (fixed1 (derivation %store "fixed"
245 %bash `(,builder1)
246 #:hash hash #:hash-algo 'sha256))
247 (fixed2 (derivation %store "fixed"
248 %bash `(,builder2)
249 #:hash hash #:hash-algo 'sha256))
250 (fixed-out (derivation->output-path fixed1))
251 (builder3 (add-text-to-store
252 %store "final-builder.sh"
253 ;; Use Bash hackery to avoid Coreutils.
254 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
255 (final1 (derivation %store "final"
256 %bash `(,builder3)
257 #:env-vars `(("in" . ,fixed-out))
258 #:inputs `((,%bash) (,builder3) (,fixed1))))
259 (final2 (derivation %store "final"
260 %bash `(,builder3)
261 #:env-vars `(("in" . ,fixed-out))
262 #:inputs `((,%bash) (,builder3) (,fixed2))))
263 (succeeded? (build-derivations %store
264 (list final1 final2))))
265 (and succeeded?
266 (equal? (derivation->output-path final1)
267 (derivation->output-path final2)))))
268
269 (test-assert "multiple-output derivation"
270 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
271 "echo one > $out ; echo two > $second"
272 '()))
273 (drv (derivation %store "fixed"
274 %bash `(,builder)
275 #:env-vars '(("HOME" . "/homeless")
276 ("zzz" . "Z!")
277 ("AAA" . "A!"))
278 #:inputs `((,%bash) (,builder))
279 #:outputs '("out" "second")))
280 (succeeded? (build-derivations %store (list drv))))
281 (and succeeded?
282 (let ((one (derivation->output-path drv "out"))
283 (two (derivation->output-path drv "second")))
284 (and (lset= equal?
285 (derivation->output-paths drv)
286 `(("out" . ,one) ("second" . ,two)))
287 (eq? 'one (call-with-input-file one read))
288 (eq? 'two (call-with-input-file two read)))))))
289
290 (test-assert "multiple-output derivation, non-alphabetic order"
291 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
292 ;; path computation must reorder them first.
293 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
294 "echo one > $out ; echo two > $AAA"
295 '()))
296 (drv (derivation %store "fixed"
297 %bash `(,builder)
298 #:inputs `((,%bash) (,builder))
299 #:outputs '("out" "AAA")))
300 (succeeded? (build-derivations %store (list drv))))
301 (and succeeded?
302 (let ((one (derivation->output-path drv "out"))
303 (two (derivation->output-path drv "AAA")))
304 (and (eq? 'one (call-with-input-file one read))
305 (eq? 'two (call-with-input-file two read)))))))
306
307 (test-assert "multiple-output derivation, derivation-path->output-path"
308 (let* ((builder (add-text-to-store %store "builder.sh"
309 "echo one > $out ; echo two > $second"
310 '()))
311 (drv (derivation %store "multiple"
312 %bash `(,builder)
313 #:outputs '("out" "second")))
314 (drv-file (derivation-file-name drv))
315 (one (derivation->output-path drv "out"))
316 (two (derivation->output-path drv "second"))
317 (first (derivation-path->output-path drv-file "out"))
318 (second (derivation-path->output-path drv-file "second")))
319 (and (not (string=? one two))
320 (string-suffix? "-second" two)
321 (string=? first one)
322 (string=? second two))))
323
324 (test-assert "user of multiple-output derivation"
325 ;; Check whether specifying several inputs coming from the same
326 ;; multiple-output derivation works.
327 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
328 "echo one > $out ; echo two > $two"
329 '()))
330 (mdrv (derivation %store "multiple-output"
331 %bash `(,builder1)
332 #:inputs `((,%bash) (,builder1))
333 #:outputs '("out" "two")))
334 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
335 "read x < $one;
336 read y < $two;
337 echo \"($x $y)\" > $out"
338 '()))
339 (udrv (derivation %store "multiple-output-user"
340 %bash `(,builder2)
341 #:env-vars `(("one"
342 . ,(derivation->output-path
343 mdrv "out"))
344 ("two"
345 . ,(derivation->output-path
346 mdrv "two")))
347 #:inputs `((,%bash)
348 (,builder2)
349 ;; two occurrences of MDRV:
350 (,mdrv)
351 (,mdrv "two")))))
352 (and (build-derivations %store (list (pk 'udrv udrv)))
353 (let ((p (derivation->output-path udrv)))
354 (and (valid-path? %store p)
355 (equal? '(one two) (call-with-input-file p read)))))))
356
357 (test-assert "derivation with #:references-graphs"
358 (let* ((input1 (add-text-to-store %store "foo" "hello"
359 (list %bash)))
360 (input2 (add-text-to-store %store "bar"
361 (number->string (random 7777))
362 (list input1)))
363 (builder (add-text-to-store %store "build-graph"
364 (format #f "
365 ~a $out
366 (while read l ; do echo $l ; done) < bash > $out/bash
367 (while read l ; do echo $l ; done) < input1 > $out/input1
368 (while read l ; do echo $l ; done) < input2 > $out/input2"
369 %mkdir)
370 (list %mkdir)))
371 (drv (derivation %store "closure-graphs"
372 %bash `(,builder)
373 #:references-graphs
374 `(("bash" . ,%bash)
375 ("input1" . ,input1)
376 ("input2" . ,input2))
377 #:inputs `((,%bash) (,builder))))
378 (out (derivation->output-path drv)))
379 (define (deps path . deps)
380 (let ((count (length deps)))
381 (string-append path "\n\n" (number->string count) "\n"
382 (string-join (sort deps string<?) "\n")
383 (if (zero? count) "" "\n"))))
384
385 (and (build-derivations %store (list drv))
386 (equal? (directory-contents out get-string-all)
387 `(("/bash" . ,(string-append %bash "\n\n0\n"))
388 ("/input1" . ,(if (string>? input1 %bash)
389 (string-append (deps %bash)
390 (deps input1 %bash))
391 (string-append (deps input1 %bash)
392 (deps %bash))))
393 ("/input2" . ,(string-concatenate
394 (map cdr
395 (sort
396 (map (lambda (p d)
397 (cons p (apply deps p d)))
398 (list %bash input1 input2)
399 (list '() (list %bash) (list input1)))
400 (lambda (x y)
401 (match x
402 ((p1 . _)
403 (match y
404 ((p2 . _)
405 (string<? p1 p2)))))))))))))))
406
407 (test-assert "derivation #:allowed-references, ok"
408 (let ((drv (derivation %store "allowed" %bash
409 '("-c" "echo hello > $out")
410 #:inputs `((,%bash))
411 #:allowed-references '())))
412 (build-derivations %store (list drv))))
413
414 (test-assert "derivation #:allowed-references, not allowed"
415 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
416 (drv (derivation %store "disallowed" %bash
417 `("-c" ,(string-append "echo " txt "> $out"))
418 #:inputs `((,%bash) (,txt))
419 #:allowed-references '())))
420 (guard (c ((nix-protocol-error? c)
421 ;; There's no specific error message to check for.
422 #t))
423 (build-derivations %store (list drv))
424 #f)))
425
426 (test-assert "derivation #:allowed-references, self allowed"
427 (let ((drv (derivation %store "allowed" %bash
428 '("-c" "echo $out > $out")
429 #:inputs `((,%bash))
430 #:allowed-references '("out"))))
431 (build-derivations %store (list drv))))
432
433 (test-assert "derivation #:allowed-references, self not allowed"
434 (let ((drv (derivation %store "disallowed" %bash
435 `("-c" ,"echo $out > $out")
436 #:inputs `((,%bash))
437 #:allowed-references '())))
438 (guard (c ((nix-protocol-error? c)
439 ;; There's no specific error message to check for.
440 #t))
441 (build-derivations %store (list drv))
442 #f)))
443
444 \f
445 (define %coreutils
446 (false-if-exception
447 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
448 (or (package-derivation %store %bootstrap-coreutils&co)
449 (nixpkgs-derivation "coreutils")))))
450
451 (test-skip (if %coreutils 0 1))
452
453 (test-assert "build derivation with coreutils"
454 (let* ((builder
455 (add-text-to-store %store "build-with-coreutils.sh"
456 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
457 '()))
458 (drv
459 (derivation %store "foo"
460 %bash `(,builder)
461 #:env-vars `(("PATH" .
462 ,(string-append
463 (derivation->output-path %coreutils)
464 "/bin")))
465 #:inputs `((,builder)
466 (,%coreutils))))
467 (succeeded?
468 (build-derivations %store (list drv))))
469 (and succeeded?
470 (let ((p (derivation->output-path drv)))
471 (and (valid-path? %store p)
472 (file-exists? (string-append p "/good")))))))
473
474 (test-skip (if (%guile-for-build) 0 8))
475
476 (test-assert "build-expression->derivation and derivation-prerequisites"
477 (let ((drv (build-expression->derivation %store "fail" #f)))
478 (any (match-lambda
479 (($ <derivation-input> path)
480 (string=? path (derivation-file-name (%guile-for-build)))))
481 (derivation-prerequisites drv))))
482
483 (test-assert "build-expression->derivation without inputs"
484 (let* ((builder '(begin
485 (mkdir %output)
486 (call-with-output-file (string-append %output "/test")
487 (lambda (p)
488 (display '(hello guix) p)))))
489 (drv (build-expression->derivation %store "goo" builder))
490 (succeeded? (build-derivations %store (list drv))))
491 (and succeeded?
492 (let ((p (derivation->output-path drv)))
493 (equal? '(hello guix)
494 (call-with-input-file (string-append p "/test") read))))))
495
496 (test-assert "build-expression->derivation and max-silent-time"
497 (let* ((store (let ((s (open-connection)))
498 (set-build-options s #:max-silent-time 1)
499 s))
500 (builder '(begin (sleep 100) (mkdir %output) #t))
501 (drv (build-expression->derivation store "silent" builder))
502 (out-path (derivation->output-path drv)))
503 (guard (c ((nix-protocol-error? c)
504 (and (string-contains (nix-protocol-error-message c)
505 "failed")
506 (not (valid-path? store out-path)))))
507 (build-derivations store (list drv))
508 #f)))
509
510 (test-assert "build-expression->derivation and timeout"
511 (let* ((store (let ((s (open-connection)))
512 (set-build-options s #:timeout 1)
513 s))
514 (builder '(begin (sleep 100) (mkdir %output) #t))
515 (drv (build-expression->derivation store "slow" builder))
516 (out-path (derivation->output-path drv)))
517 (guard (c ((nix-protocol-error? c)
518 (and (string-contains (nix-protocol-error-message c)
519 "failed")
520 (not (valid-path? store out-path)))))
521 (build-derivations store (list drv))
522 #f)))
523
524 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
525 (let ((drv (build-expression->derivation %store "fail" #f)))
526 ;; The only direct dependency is (%guile-for-build) and it's already
527 ;; built.
528 (null? (derivation-prerequisites-to-build %store drv))))
529
530 (test-assert "derivation-prerequisites-to-build when outputs already present"
531 (let* ((builder '(begin (mkdir %output) #t))
532 (input-drv (build-expression->derivation %store "input" builder))
533 (input-path (derivation-output-path
534 (assoc-ref (derivation-outputs input-drv)
535 "out")))
536 (drv (build-expression->derivation %store "something" builder
537 #:inputs
538 `(("i" ,input-drv))))
539 (output (derivation->output-path drv)))
540 ;; Make sure these things are not already built.
541 (when (valid-path? %store input-path)
542 (delete-paths %store (list input-path)))
543 (when (valid-path? %store output)
544 (delete-paths %store (list output)))
545
546 (and (equal? (map derivation-input-path
547 (derivation-prerequisites-to-build %store drv))
548 (list (derivation-file-name input-drv)))
549
550 ;; Build DRV and delete its input.
551 (build-derivations %store (list drv))
552 (delete-paths %store (list input-path))
553 (not (valid-path? %store input-path))
554
555 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
556 ;; prerequisite to build because DRV itself is already built.
557 (null? (derivation-prerequisites-to-build %store drv)))))
558
559 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
560 (test-assert "derivation-prerequisites-to-build and substitutes"
561 (let* ((store (open-connection))
562 (drv (build-expression->derivation store "prereq-subst"
563 (random 1000)))
564 (output (derivation->output-path drv))
565 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
566 (compose uri-path string->uri))))
567 ;; Create fake substituter data, to be read by `substitute-binary'.
568 (call-with-output-file (string-append dir "/nix-cache-info")
569 (lambda (p)
570 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
571 (%store-prefix))))
572 (call-with-output-file (string-append dir "/" (store-path-hash-part output)
573 ".narinfo")
574 (lambda (p)
575 (format p "StorePath: ~a
576 URL: ~a
577 Compression: none
578 NarSize: 1234
579 References:
580 System: ~a
581 Deriver: ~a~%"
582 output ; StorePath
583 (string-append dir "/example.nar") ; URL
584 (%current-system) ; System
585 (basename
586 (derivation-file-name drv))))) ; Deriver
587
588 ;; Make sure substitutes are usable.
589 (set-build-options store #:use-substitutes? #t)
590
591 (let-values (((build download)
592 (derivation-prerequisites-to-build store drv))
593 ((build* download*)
594 (derivation-prerequisites-to-build store drv
595 #:use-substitutes? #f)))
596 (pk build download build* download*)
597 (and (null? build)
598 (equal? download (list output))
599 (null? download*)
600 (null? build*)))))
601
602 (test-assert "build-expression->derivation with expression returning #f"
603 (let* ((builder '(begin
604 (mkdir %output)
605 #f)) ; fail!
606 (drv (build-expression->derivation %store "fail" builder))
607 (out-path (derivation->output-path drv)))
608 (guard (c ((nix-protocol-error? c)
609 ;; Note that the output path may exist at this point, but it
610 ;; is invalid.
611 (and (string-match "build .* failed"
612 (nix-protocol-error-message c))
613 (not (valid-path? %store out-path)))))
614 (build-derivations %store (list drv))
615 #f)))
616
617 (test-assert "build-expression->derivation with two outputs"
618 (let* ((builder '(begin
619 (call-with-output-file (assoc-ref %outputs "out")
620 (lambda (p)
621 (display '(hello) p)))
622 (call-with-output-file (assoc-ref %outputs "second")
623 (lambda (p)
624 (display '(world) p)))))
625 (drv (build-expression->derivation %store "double" builder
626 #:outputs '("out"
627 "second")))
628 (succeeded? (build-derivations %store (list drv))))
629 (and succeeded?
630 (let ((one (derivation->output-path drv))
631 (two (derivation->output-path drv "second")))
632 (and (equal? '(hello) (call-with-input-file one read))
633 (equal? '(world) (call-with-input-file two read)))))))
634
635 (test-skip (if %coreutils 0 1))
636 (test-assert "build-expression->derivation with one input"
637 (let* ((builder '(call-with-output-file %output
638 (lambda (p)
639 (let ((cu (assoc-ref %build-inputs "cu")))
640 (close 1)
641 (dup2 (port->fdes p) 1)
642 (execl (string-append cu "/bin/uname")
643 "uname" "-a")))))
644 (drv (build-expression->derivation %store "uname" builder
645 #:inputs
646 `(("cu" ,%coreutils))))
647 (succeeded? (build-derivations %store (list drv))))
648 (and succeeded?
649 (let ((p (derivation->output-path drv)))
650 (string-contains (call-with-input-file p read-line) "GNU")))))
651
652 (test-assert "imported-files"
653 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
654 ("a/b/c" . ,(search-path %load-path
655 "guix/derivations.scm"))
656 ("p/q" . ,(search-path %load-path "guix.scm"))
657 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
658 (drv (imported-files %store files)))
659 (and (build-derivations %store (list drv))
660 (let ((dir (derivation->output-path drv)))
661 (every (match-lambda
662 ((path . source)
663 (equal? (call-with-input-file (string-append dir "/" path)
664 get-bytevector-all)
665 (call-with-input-file source
666 get-bytevector-all))))
667 files)))))
668
669 (test-assert "build-expression->derivation with modules"
670 (let* ((builder `(begin
671 (use-modules (guix build utils))
672 (let ((out (assoc-ref %outputs "out")))
673 (mkdir-p (string-append out "/guile/guix/nix"))
674 #t)))
675 (drv (build-expression->derivation %store "test-with-modules"
676 builder
677 #:modules
678 '((guix build utils)))))
679 (and (build-derivations %store (list drv))
680 (let* ((p (derivation->output-path drv))
681 (s (stat (string-append p "/guile/guix/nix"))))
682 (eq? (stat:type s) 'directory)))))
683
684 (test-assert "build-expression->derivation: same fixed-output path"
685 (let* ((builder1 '(call-with-output-file %output
686 (lambda (p)
687 (write "hello" p))))
688 (builder2 '(call-with-output-file (pk 'difference-here! %output)
689 (lambda (p)
690 (write "hello" p))))
691 (hash (sha256 (string->utf8 "hello")))
692 (input1 (build-expression->derivation %store "fixed" builder1
693 #:hash hash
694 #:hash-algo 'sha256))
695 (input2 (build-expression->derivation %store "fixed" builder2
696 #:hash hash
697 #:hash-algo 'sha256))
698 (succeeded? (build-derivations %store (list input1 input2))))
699 (and succeeded?
700 (not (string=? (derivation-file-name input1)
701 (derivation-file-name input2)))
702 (string=? (derivation->output-path input1)
703 (derivation->output-path input2)))))
704
705 (test-assert "build-expression->derivation with a fixed-output input"
706 (let* ((builder1 '(call-with-output-file %output
707 (lambda (p)
708 (write "hello" p))))
709 (builder2 '(call-with-output-file (pk 'difference-here! %output)
710 (lambda (p)
711 (write "hello" p))))
712 (hash (sha256 (string->utf8 "hello")))
713 (input1 (build-expression->derivation %store "fixed" builder1
714 #:hash hash
715 #:hash-algo 'sha256))
716 (input2 (build-expression->derivation %store "fixed" builder2
717 #:hash hash
718 #:hash-algo 'sha256))
719 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
720 (call-with-output-file %output
721 (lambda (out)
722 (format #f "My input is ~a.~%" input)))))
723 (final1 (build-expression->derivation %store "final" builder3
724 #:inputs
725 `(("input" ,input1))))
726 (final2 (build-expression->derivation %store "final" builder3
727 #:inputs
728 `(("input" ,input2)))))
729 (and (string=? (derivation->output-path final1)
730 (derivation->output-path final2))
731 (string=? (derivation->output-path final1)
732 (derivation-path->output-path
733 (derivation-file-name final1)))
734 (build-derivations %store (list final1 final2)))))
735
736 (test-assert "build-expression->derivation produces recursive fixed-output"
737 (let* ((builder '(begin
738 (use-modules (srfi srfi-26))
739 (mkdir %output)
740 (chdir %output)
741 (call-with-output-file "exe"
742 (cut display "executable" <>))
743 (chmod "exe" #o777)
744 (symlink "exe" "symlink")
745 (mkdir "subdir")))
746 (drv (build-expression->derivation %store "fixed-rec" builder
747 #:hash-algo 'sha256
748 #:hash (base32
749 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
750 #:recursive? #t)))
751 (and (build-derivations %store (list drv))
752 (let* ((dir (derivation->output-path drv))
753 (exe (string-append dir "/exe"))
754 (link (string-append dir "/symlink"))
755 (subdir (string-append dir "/subdir")))
756 (and (executable-file? exe)
757 (string=? "executable"
758 (call-with-input-file exe get-string-all))
759 (string=? "exe" (readlink link))
760 (file-is-directory? subdir))))))
761
762 (test-assert "build-expression->derivation uses recursive fixed-output"
763 (let* ((builder '(call-with-output-file %output
764 (lambda (port)
765 (display "hello" port))))
766 (fixed (build-expression->derivation %store "small-fixed-rec"
767 builder
768 #:hash-algo 'sha256
769 #:hash (base32
770 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
771 #:recursive? #t))
772 (in (derivation->output-path fixed))
773 (builder `(begin
774 (mkdir %output)
775 (chdir %output)
776 (symlink ,in "symlink")))
777 (drv (build-expression->derivation %store "fixed-rec-user"
778 builder
779 #:inputs `(("fixed" ,fixed)))))
780 (and (build-derivations %store (list drv))
781 (let ((out (derivation->output-path drv)))
782 (string=? (readlink (string-append out "/symlink")) in)))))
783
784 (test-assert "build-expression->derivation with #:references-graphs"
785 (let* ((input (add-text-to-store %store "foo" "hello"
786 (list %bash %mkdir)))
787 (builder '(copy-file "input" %output))
788 (drv (build-expression->derivation %store "references-graphs"
789 builder
790 #:references-graphs
791 `(("input" . ,input))))
792 (out (derivation->output-path drv)))
793 (define (deps path . deps)
794 (let ((count (length deps)))
795 (string-append path "\n\n" (number->string count) "\n"
796 (string-join (sort deps string<?) "\n")
797 (if (zero? count) "" "\n"))))
798
799 (and (build-derivations %store (list drv))
800 (equal? (call-with-input-file out get-string-all)
801 (string-concatenate
802 (map cdr
803 (sort (map (lambda (p d)
804 (cons p (apply deps p d)))
805 (list input %bash %mkdir)
806 (list (list %bash %mkdir)
807 '() '()))
808 (lambda (x y)
809 (match x
810 ((p1 . _)
811 (match y
812 ((p2 . _)
813 (string<? p1 p2)))))))))))))
814
815
816 (test-assert "graft-derivation"
817 (let* ((build `(begin
818 (mkdir %output)
819 (chdir %output)
820 (symlink %output "self")
821 (call-with-output-file "text"
822 (lambda (output)
823 (format output "foo/~a/bar" ,%mkdir)))
824 (symlink ,%bash "sh")))
825 (orig (build-expression->derivation %store "graft" build
826 #:inputs `(("a" ,%bash)
827 ("b" ,%mkdir))))
828 (one (add-text-to-store %store "bash" "fake bash"))
829 (two (build-expression->derivation %store "mkdir"
830 '(call-with-output-file %output
831 (lambda (port)
832 (display "fake mkdir" port)))))
833 (graft (graft-derivation %store "graft" orig
834 (list (graft
835 (origin %bash)
836 (replacement one))
837 (graft
838 (origin %mkdir)
839 (replacement two))))))
840 (and (build-derivations %store (list graft))
841 (let ((two (derivation->output-path two))
842 (graft (derivation->output-path graft)))
843 (and (string=? (format #f "foo/~a/bar" two)
844 (call-with-input-file (string-append graft "/text")
845 get-string-all))
846 (string=? (readlink (string-append graft "/sh")) one)
847 (string=? (readlink (string-append graft "/self")) graft))))))
848
849 (test-equal "map-derivation"
850 "hello"
851 (let* ((joke (package-derivation %store guile-1.8))
852 (good (package-derivation %store %bootstrap-guile))
853 (drv1 (build-expression->derivation %store "original-drv1"
854 #f ; systematically fail
855 #:guile-for-build joke))
856 (drv2 (build-expression->derivation %store "original-drv2"
857 '(call-with-output-file %output
858 (lambda (p)
859 (display "hello" p)))))
860 (drv3 (build-expression->derivation %store "drv-to-remap"
861 '(let ((in (assoc-ref
862 %build-inputs "in")))
863 (copy-file in %output))
864 #:inputs `(("in" ,drv1))
865 #:guile-for-build joke))
866 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
867 (,joke . ,good))))
868 (out (derivation->output-path drv4)))
869 (and (build-derivations %store (list (pk 'remapped drv4)))
870 (call-with-input-file out get-string-all))))
871
872 (test-equal "map-derivation, sources"
873 "hello"
874 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
875 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
876 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
877 (drv1 (derivation %store "drv-to-remap"
878
879 ;; XXX: This wouldn't work in practice, but if
880 ;; we append "/bin/bash" then we can't replace
881 ;; it with the bootstrap bash, which is a
882 ;; single file.
883 (derivation->output-path bash-full)
884
885 `("-e" ,script1)
886 #:inputs `((,bash-full) (,script1))))
887 (drv2 (map-derivation %store drv1
888 `((,bash-full . ,%bash)
889 (,script1 . ,script2))))
890 (out (derivation->output-path drv2)))
891 (and (build-derivations %store (list (pk 'remapped* drv2)))
892 (call-with-input-file out get-string-all))))
893
894 (test-end)
895
896 \f
897 (exit (= (test-runner-fail-count (test-runner-current)) 0))