a8cccac34a4310338a469c95866e6368e02d5f1a
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 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-equal "derivation-name"
177 "foo-0.0"
178 (let ((drv (derivation %store "foo-0.0" %bash '())))
179 (derivation-name drv)))
180
181 (test-equal "derivation-output-names"
182 '(("out") ("bar" "chbouib"))
183 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
184 (drv2 (derivation %store "foo-0.0" %bash '()
185 #:outputs '("bar" "chbouib"))))
186 (list (derivation-output-names drv1)
187 (derivation-output-names drv2))))
188
189 (test-assert "offloadable-derivation?"
190 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
191 (not (offloadable-derivation?
192 (derivation %store "foo" %bash '()
193 #:local-build? #t)))))
194
195 (test-assert "fixed-output-derivation?"
196 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
197 "echo -n hello > $out" '()))
198 (hash (sha256 (string->utf8 "hello")))
199 (drv (derivation %store "fixed"
200 %bash `(,builder)
201 #:inputs `((,builder))
202 #:hash hash #:hash-algo 'sha256)))
203 (fixed-output-derivation? drv)))
204
205 (test-assert "fixed-output derivation"
206 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
207 "echo -n hello > $out" '()))
208 (hash (sha256 (string->utf8 "hello")))
209 (drv (derivation %store "fixed"
210 %bash `(,builder)
211 #:inputs `((,builder)) ; optional
212 #:hash hash #:hash-algo 'sha256))
213 (succeeded? (build-derivations %store (list drv))))
214 (and succeeded?
215 (let ((p (derivation->output-path drv)))
216 (and (equal? (string->utf8 "hello")
217 (call-with-input-file p get-bytevector-all))
218 (bytevector? (query-path-hash %store p)))))))
219
220 (test-assert "fixed-output derivation: output paths are equal"
221 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
222 "echo -n hello > $out" '()))
223 (builder2 (add-text-to-store %store "fixed-builder2.sh"
224 "echo hey; echo -n hello > $out" '()))
225 (hash (sha256 (string->utf8 "hello")))
226 (drv1 (derivation %store "fixed"
227 %bash `(,builder1)
228 #:hash hash #:hash-algo 'sha256))
229 (drv2 (derivation %store "fixed"
230 %bash `(,builder2)
231 #:hash hash #:hash-algo 'sha256))
232 (succeeded? (build-derivations %store (list drv1 drv2))))
233 (and succeeded?
234 (equal? (derivation->output-path drv1)
235 (derivation->output-path drv2)))))
236
237 (test-assert "fixed-output derivation, recursive"
238 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
239 "echo -n hello > $out" '()))
240 (hash (sha256 (string->utf8 "hello")))
241 (drv (derivation %store "fixed-rec"
242 %bash `(,builder)
243 #:inputs `((,builder))
244 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
245 #:hash-algo 'sha256
246 #:recursive? #t))
247 (succeeded? (build-derivations %store (list drv))))
248 (and succeeded?
249 (let ((p (derivation->output-path drv)))
250 (and (equal? (string->utf8 "hello")
251 (call-with-input-file p get-bytevector-all))
252 (bytevector? (query-path-hash %store p)))))))
253
254 (test-assert "derivation with a fixed-output input"
255 ;; A derivation D using a fixed-output derivation F doesn't has the same
256 ;; output path when passed F or F', as long as F and F' have the same output
257 ;; path.
258 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
259 "echo -n hello > $out" '()))
260 (builder2 (add-text-to-store %store "fixed-builder2.sh"
261 "echo hey; echo -n hello > $out" '()))
262 (hash (sha256 (string->utf8 "hello")))
263 (fixed1 (derivation %store "fixed"
264 %bash `(,builder1)
265 #:hash hash #:hash-algo 'sha256))
266 (fixed2 (derivation %store "fixed"
267 %bash `(,builder2)
268 #:hash hash #:hash-algo 'sha256))
269 (fixed-out (derivation->output-path fixed1))
270 (builder3 (add-text-to-store
271 %store "final-builder.sh"
272 ;; Use Bash hackery to avoid Coreutils.
273 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
274 (final1 (derivation %store "final"
275 %bash `(,builder3)
276 #:env-vars `(("in" . ,fixed-out))
277 #:inputs `((,%bash) (,builder3) (,fixed1))))
278 (final2 (derivation %store "final"
279 %bash `(,builder3)
280 #:env-vars `(("in" . ,fixed-out))
281 #:inputs `((,%bash) (,builder3) (,fixed2))))
282 (succeeded? (build-derivations %store
283 (list final1 final2))))
284 (and succeeded?
285 (equal? (derivation->output-path final1)
286 (derivation->output-path final2)))))
287
288 (test-assert "multiple-output derivation"
289 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
290 "echo one > $out ; echo two > $second"
291 '()))
292 (drv (derivation %store "fixed"
293 %bash `(,builder)
294 #:env-vars '(("HOME" . "/homeless")
295 ("zzz" . "Z!")
296 ("AAA" . "A!"))
297 #:inputs `((,%bash) (,builder))
298 #:outputs '("out" "second")))
299 (succeeded? (build-derivations %store (list drv))))
300 (and succeeded?
301 (let ((one (derivation->output-path drv "out"))
302 (two (derivation->output-path drv "second")))
303 (and (lset= equal?
304 (derivation->output-paths drv)
305 `(("out" . ,one) ("second" . ,two)))
306 (eq? 'one (call-with-input-file one read))
307 (eq? 'two (call-with-input-file two read)))))))
308
309 (test-assert "multiple-output derivation, non-alphabetic order"
310 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
311 ;; path computation must reorder them first.
312 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
313 "echo one > $out ; echo two > $AAA"
314 '()))
315 (drv (derivation %store "fixed"
316 %bash `(,builder)
317 #:inputs `((,%bash) (,builder))
318 #:outputs '("out" "AAA")))
319 (succeeded? (build-derivations %store (list drv))))
320 (and succeeded?
321 (let ((one (derivation->output-path drv "out"))
322 (two (derivation->output-path drv "AAA")))
323 (and (eq? 'one (call-with-input-file one read))
324 (eq? 'two (call-with-input-file two read)))))))
325
326 (test-assert "multiple-output derivation, derivation-path->output-path"
327 (let* ((builder (add-text-to-store %store "builder.sh"
328 "echo one > $out ; echo two > $second"
329 '()))
330 (drv (derivation %store "multiple"
331 %bash `(,builder)
332 #:outputs '("out" "second")))
333 (drv-file (derivation-file-name drv))
334 (one (derivation->output-path drv "out"))
335 (two (derivation->output-path drv "second"))
336 (first (derivation-path->output-path drv-file "out"))
337 (second (derivation-path->output-path drv-file "second")))
338 (and (not (string=? one two))
339 (string-suffix? "-second" two)
340 (string=? first one)
341 (string=? second two))))
342
343 (test-assert "user of multiple-output derivation"
344 ;; Check whether specifying several inputs coming from the same
345 ;; multiple-output derivation works.
346 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
347 "echo one > $out ; echo two > $two"
348 '()))
349 (mdrv (derivation %store "multiple-output"
350 %bash `(,builder1)
351 #:inputs `((,%bash) (,builder1))
352 #:outputs '("out" "two")))
353 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
354 "read x < $one;
355 read y < $two;
356 echo \"($x $y)\" > $out"
357 '()))
358 (udrv (derivation %store "multiple-output-user"
359 %bash `(,builder2)
360 #:env-vars `(("one"
361 . ,(derivation->output-path
362 mdrv "out"))
363 ("two"
364 . ,(derivation->output-path
365 mdrv "two")))
366 #:inputs `((,%bash)
367 (,builder2)
368 ;; two occurrences of MDRV:
369 (,mdrv)
370 (,mdrv "two")))))
371 (and (build-derivations %store (list (pk 'udrv udrv)))
372 (let ((p (derivation->output-path udrv)))
373 (and (valid-path? %store p)
374 (equal? '(one two) (call-with-input-file p read)))))))
375
376 (test-assert "derivation with #:references-graphs"
377 (let* ((input1 (add-text-to-store %store "foo" "hello"
378 (list %bash)))
379 (input2 (add-text-to-store %store "bar"
380 (number->string (random 7777))
381 (list input1)))
382 (builder (add-text-to-store %store "build-graph"
383 (format #f "
384 ~a $out
385 (while read l ; do echo $l ; done) < bash > $out/bash
386 (while read l ; do echo $l ; done) < input1 > $out/input1
387 (while read l ; do echo $l ; done) < input2 > $out/input2"
388 %mkdir)
389 (list %mkdir)))
390 (drv (derivation %store "closure-graphs"
391 %bash `(,builder)
392 #:references-graphs
393 `(("bash" . ,%bash)
394 ("input1" . ,input1)
395 ("input2" . ,input2))
396 #:inputs `((,%bash) (,builder))))
397 (out (derivation->output-path drv)))
398 (define (deps path . deps)
399 (let ((count (length deps)))
400 (string-append path "\n\n" (number->string count) "\n"
401 (string-join (sort deps string<?) "\n")
402 (if (zero? count) "" "\n"))))
403
404 (and (build-derivations %store (list drv))
405 (equal? (directory-contents out get-string-all)
406 `(("/bash" . ,(string-append %bash "\n\n0\n"))
407 ("/input1" . ,(if (string>? input1 %bash)
408 (string-append (deps %bash)
409 (deps input1 %bash))
410 (string-append (deps input1 %bash)
411 (deps %bash))))
412 ("/input2" . ,(string-concatenate
413 (map cdr
414 (sort
415 (map (lambda (p d)
416 (cons p (apply deps p d)))
417 (list %bash input1 input2)
418 (list '() (list %bash) (list input1)))
419 (lambda (x y)
420 (match x
421 ((p1 . _)
422 (match y
423 ((p2 . _)
424 (string<? p1 p2)))))))))))))))
425
426 (test-assert "derivation #:allowed-references, ok"
427 (let ((drv (derivation %store "allowed" %bash
428 '("-c" "echo hello > $out")
429 #:inputs `((,%bash))
430 #:allowed-references '())))
431 (build-derivations %store (list drv))))
432
433 (test-assert "derivation #:allowed-references, not allowed"
434 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
435 (drv (derivation %store "disallowed" %bash
436 `("-c" ,(string-append "echo " txt "> $out"))
437 #:inputs `((,%bash) (,txt))
438 #:allowed-references '())))
439 (guard (c ((nix-protocol-error? c)
440 ;; There's no specific error message to check for.
441 #t))
442 (build-derivations %store (list drv))
443 #f)))
444
445 (test-assert "derivation #:allowed-references, self allowed"
446 (let ((drv (derivation %store "allowed" %bash
447 '("-c" "echo $out > $out")
448 #:inputs `((,%bash))
449 #:allowed-references '("out"))))
450 (build-derivations %store (list drv))))
451
452 (test-assert "derivation #:allowed-references, self not allowed"
453 (let ((drv (derivation %store "disallowed" %bash
454 `("-c" ,"echo $out > $out")
455 #:inputs `((,%bash))
456 #:allowed-references '())))
457 (guard (c ((nix-protocol-error? c)
458 ;; There's no specific error message to check for.
459 #t))
460 (build-derivations %store (list drv))
461 #f)))
462
463 \f
464 (define %coreutils
465 (false-if-exception
466 (and (network-reachable?)
467 (or (package-derivation %store %bootstrap-coreutils&co)
468 (nixpkgs-derivation "coreutils")))))
469
470 (test-skip (if %coreutils 0 1))
471
472 (test-assert "build derivation with coreutils"
473 (let* ((builder
474 (add-text-to-store %store "build-with-coreutils.sh"
475 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
476 '()))
477 (drv
478 (derivation %store "foo"
479 %bash `(,builder)
480 #:env-vars `(("PATH" .
481 ,(string-append
482 (derivation->output-path %coreutils)
483 "/bin")))
484 #:inputs `((,builder)
485 (,%coreutils))))
486 (succeeded?
487 (build-derivations %store (list drv))))
488 (and succeeded?
489 (let ((p (derivation->output-path drv)))
490 (and (valid-path? %store p)
491 (file-exists? (string-append p "/good")))))))
492
493 (test-skip (if (%guile-for-build) 0 8))
494
495 (test-assert "build-expression->derivation and derivation-prerequisites"
496 (let ((drv (build-expression->derivation %store "fail" #f)))
497 (any (match-lambda
498 (($ <derivation-input> path)
499 (string=? path (derivation-file-name (%guile-for-build)))))
500 (derivation-prerequisites drv))))
501
502 (test-assert "derivation-prerequisites and derivation-input-is-valid?"
503 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
504 (b (build-expression->derivation %store "b" `(list ,(random-text))))
505 (c (build-expression->derivation %store "c" `(mkdir %output)
506 #:inputs `(("a" ,a) ("b" ,b)))))
507 (build-derivations %store (list a))
508 (match (derivation-prerequisites c
509 (cut valid-derivation-input? %store
510 <>))
511 ((($ <derivation-input> file ("out")))
512 (string=? file (derivation-file-name b)))
513 (x
514 (pk 'fail x #f)))))
515
516 (test-assert "build-expression->derivation without inputs"
517 (let* ((builder '(begin
518 (mkdir %output)
519 (call-with-output-file (string-append %output "/test")
520 (lambda (p)
521 (display '(hello guix) p)))))
522 (drv (build-expression->derivation %store "goo" builder))
523 (succeeded? (build-derivations %store (list drv))))
524 (and succeeded?
525 (let ((p (derivation->output-path drv)))
526 (equal? '(hello guix)
527 (call-with-input-file (string-append p "/test") read))))))
528
529 (test-assert "build-expression->derivation and max-silent-time"
530 (let* ((store (let ((s (open-connection)))
531 (set-build-options s #:max-silent-time 1)
532 s))
533 (builder '(begin (sleep 100) (mkdir %output) #t))
534 (drv (build-expression->derivation store "silent" builder))
535 (out-path (derivation->output-path drv)))
536 (guard (c ((nix-protocol-error? c)
537 (and (string-contains (nix-protocol-error-message c)
538 "failed")
539 (not (valid-path? store out-path)))))
540 (build-derivations store (list drv))
541 #f)))
542
543 (test-assert "build-expression->derivation and timeout"
544 (let* ((store (let ((s (open-connection)))
545 (set-build-options s #:timeout 1)
546 s))
547 (builder '(begin (sleep 100) (mkdir %output) #t))
548 (drv (build-expression->derivation store "slow" builder))
549 (out-path (derivation->output-path drv)))
550 (guard (c ((nix-protocol-error? c)
551 (and (string-contains (nix-protocol-error-message c)
552 "failed")
553 (not (valid-path? store out-path)))))
554 (build-derivations store (list drv))
555 #f)))
556
557 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
558 (let ((drv (build-expression->derivation %store "fail" #f)))
559 ;; The only direct dependency is (%guile-for-build) and it's already
560 ;; built.
561 (null? (derivation-prerequisites-to-build %store drv))))
562
563 (test-assert "derivation-prerequisites-to-build when outputs already present"
564 (let* ((builder '(begin (mkdir %output) #t))
565 (input-drv (build-expression->derivation %store "input" builder))
566 (input-path (derivation-output-path
567 (assoc-ref (derivation-outputs input-drv)
568 "out")))
569 (drv (build-expression->derivation %store "something" builder
570 #:inputs
571 `(("i" ,input-drv))))
572 (output (derivation->output-path drv)))
573 ;; Make sure these things are not already built.
574 (when (valid-path? %store input-path)
575 (delete-paths %store (list input-path)))
576 (when (valid-path? %store output)
577 (delete-paths %store (list output)))
578
579 (and (equal? (map derivation-input-path
580 (derivation-prerequisites-to-build %store drv))
581 (list (derivation-file-name input-drv)))
582
583 ;; Build DRV and delete its input.
584 (build-derivations %store (list drv))
585 (delete-paths %store (list input-path))
586 (not (valid-path? %store input-path))
587
588 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
589 ;; prerequisite to build because DRV itself is already built.
590 (null? (derivation-prerequisites-to-build %store drv)))))
591
592 (test-assert "derivation-prerequisites-to-build and substitutes"
593 (let* ((store (open-connection))
594 (drv (build-expression->derivation store "prereq-subst"
595 (random 1000)))
596 (output (derivation->output-path drv)))
597
598 ;; Make sure substitutes are usable.
599 (set-build-options store #:use-substitutes? #t)
600
601 (with-derivation-narinfo drv
602 (let-values (((build download)
603 (derivation-prerequisites-to-build store drv))
604 ((build* download*)
605 (derivation-prerequisites-to-build store drv
606 #:substitutable?
607 (const #f))))
608 (and (null? build)
609 (equal? download (list output))
610 (null? download*)
611 (null? build*))))))
612
613 (test-assert "derivation-prerequisites-to-build and substitutes, local build"
614 (let* ((store (open-connection))
615 (drv (build-expression->derivation store "prereq-subst-local"
616 (random 1000)
617 ;; XXX: Adjust once
618 ;; <http://bugs.gnu.org/18747>
619 ;; is fixed.
620 #:local-build? #t))
621 (output (derivation->output-path drv)))
622
623 ;; Make sure substitutes are usable.
624 (set-build-options store #:use-substitutes? #t)
625
626 (with-derivation-narinfo drv
627 (let-values (((build download)
628 (derivation-prerequisites-to-build store drv)))
629 ;; Despite being available as a substitute, DRV will be built locally
630 ;; due to #:local-build?.
631 (and (null? download)
632 (match build
633 (((? derivation-input? input))
634 (string=? (derivation-input-path input)
635 (derivation-file-name drv)))))))))
636
637 (test-assert "build-expression->derivation with expression returning #f"
638 (let* ((builder '(begin
639 (mkdir %output)
640 #f)) ; fail!
641 (drv (build-expression->derivation %store "fail" builder))
642 (out-path (derivation->output-path drv)))
643 (guard (c ((nix-protocol-error? c)
644 ;; Note that the output path may exist at this point, but it
645 ;; is invalid.
646 (and (string-match "build .* failed"
647 (nix-protocol-error-message c))
648 (not (valid-path? %store out-path)))))
649 (build-derivations %store (list drv))
650 #f)))
651
652 (test-assert "build-expression->derivation with two outputs"
653 (let* ((builder '(begin
654 (call-with-output-file (assoc-ref %outputs "out")
655 (lambda (p)
656 (display '(hello) p)))
657 (call-with-output-file (assoc-ref %outputs "second")
658 (lambda (p)
659 (display '(world) p)))))
660 (drv (build-expression->derivation %store "double" builder
661 #:outputs '("out"
662 "second")))
663 (succeeded? (build-derivations %store (list drv))))
664 (and succeeded?
665 (let ((one (derivation->output-path drv))
666 (two (derivation->output-path drv "second")))
667 (and (equal? '(hello) (call-with-input-file one read))
668 (equal? '(world) (call-with-input-file two read)))))))
669
670 (test-skip (if %coreutils 0 1))
671 (test-assert "build-expression->derivation with one input"
672 (let* ((builder '(call-with-output-file %output
673 (lambda (p)
674 (let ((cu (assoc-ref %build-inputs "cu")))
675 (close 1)
676 (dup2 (port->fdes p) 1)
677 (execl (string-append cu "/bin/uname")
678 "uname" "-a")))))
679 (drv (build-expression->derivation %store "uname" builder
680 #:inputs
681 `(("cu" ,%coreutils))))
682 (succeeded? (build-derivations %store (list drv))))
683 (and succeeded?
684 (let ((p (derivation->output-path drv)))
685 (string-contains (call-with-input-file p read-line) "GNU")))))
686
687 (test-assert "build-expression->derivation with modules"
688 (let* ((builder `(begin
689 (use-modules (guix build utils))
690 (let ((out (assoc-ref %outputs "out")))
691 (mkdir-p (string-append out "/guile/guix/nix"))
692 #t)))
693 (drv (build-expression->derivation %store "test-with-modules"
694 builder
695 #:modules
696 '((guix build utils)))))
697 (and (build-derivations %store (list drv))
698 (let* ((p (derivation->output-path drv))
699 (s (stat (string-append p "/guile/guix/nix"))))
700 (eq? (stat:type s) 'directory)))))
701
702 (test-assert "build-expression->derivation: same fixed-output path"
703 (let* ((builder1 '(call-with-output-file %output
704 (lambda (p)
705 (write "hello" p))))
706 (builder2 '(call-with-output-file (pk 'difference-here! %output)
707 (lambda (p)
708 (write "hello" p))))
709 (hash (sha256 (string->utf8 "hello")))
710 (input1 (build-expression->derivation %store "fixed" builder1
711 #:hash hash
712 #:hash-algo 'sha256))
713 (input2 (build-expression->derivation %store "fixed" builder2
714 #:hash hash
715 #:hash-algo 'sha256))
716 (succeeded? (build-derivations %store (list input1 input2))))
717 (and succeeded?
718 (not (string=? (derivation-file-name input1)
719 (derivation-file-name input2)))
720 (string=? (derivation->output-path input1)
721 (derivation->output-path input2)))))
722
723 (test-assert "build-expression->derivation with a fixed-output input"
724 (let* ((builder1 '(call-with-output-file %output
725 (lambda (p)
726 (write "hello" p))))
727 (builder2 '(call-with-output-file (pk 'difference-here! %output)
728 (lambda (p)
729 (write "hello" p))))
730 (hash (sha256 (string->utf8 "hello")))
731 (input1 (build-expression->derivation %store "fixed" builder1
732 #:hash hash
733 #:hash-algo 'sha256))
734 (input2 (build-expression->derivation %store "fixed" builder2
735 #:hash hash
736 #:hash-algo 'sha256))
737 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
738 (call-with-output-file %output
739 (lambda (out)
740 (format #f "My input is ~a.~%" input)))))
741 (final1 (build-expression->derivation %store "final" builder3
742 #:inputs
743 `(("input" ,input1))))
744 (final2 (build-expression->derivation %store "final" builder3
745 #:inputs
746 `(("input" ,input2)))))
747 (and (string=? (derivation->output-path final1)
748 (derivation->output-path final2))
749 (string=? (derivation->output-path final1)
750 (derivation-path->output-path
751 (derivation-file-name final1)))
752 (build-derivations %store (list final1 final2)))))
753
754 (test-assert "build-expression->derivation produces recursive fixed-output"
755 (let* ((builder '(begin
756 (use-modules (srfi srfi-26))
757 (mkdir %output)
758 (chdir %output)
759 (call-with-output-file "exe"
760 (cut display "executable" <>))
761 (chmod "exe" #o777)
762 (symlink "exe" "symlink")
763 (mkdir "subdir")))
764 (drv (build-expression->derivation %store "fixed-rec" builder
765 #:hash-algo 'sha256
766 #:hash (base32
767 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
768 #:recursive? #t)))
769 (and (build-derivations %store (list drv))
770 (let* ((dir (derivation->output-path drv))
771 (exe (string-append dir "/exe"))
772 (link (string-append dir "/symlink"))
773 (subdir (string-append dir "/subdir")))
774 (and (executable-file? exe)
775 (string=? "executable"
776 (call-with-input-file exe get-string-all))
777 (string=? "exe" (readlink link))
778 (file-is-directory? subdir))))))
779
780 (test-assert "build-expression->derivation uses recursive fixed-output"
781 (let* ((builder '(call-with-output-file %output
782 (lambda (port)
783 (display "hello" port))))
784 (fixed (build-expression->derivation %store "small-fixed-rec"
785 builder
786 #:hash-algo 'sha256
787 #:hash (base32
788 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
789 #:recursive? #t))
790 (in (derivation->output-path fixed))
791 (builder `(begin
792 (mkdir %output)
793 (chdir %output)
794 (symlink ,in "symlink")))
795 (drv (build-expression->derivation %store "fixed-rec-user"
796 builder
797 #:inputs `(("fixed" ,fixed)))))
798 (and (build-derivations %store (list drv))
799 (let ((out (derivation->output-path drv)))
800 (string=? (readlink (string-append out "/symlink")) in)))))
801
802 (test-assert "build-expression->derivation with #:references-graphs"
803 (let* ((input (add-text-to-store %store "foo" "hello"
804 (list %bash %mkdir)))
805 (builder '(copy-file "input" %output))
806 (drv (build-expression->derivation %store "references-graphs"
807 builder
808 #:references-graphs
809 `(("input" . ,input))))
810 (out (derivation->output-path drv)))
811 (define (deps path . deps)
812 (let ((count (length deps)))
813 (string-append path "\n\n" (number->string count) "\n"
814 (string-join (sort deps string<?) "\n")
815 (if (zero? count) "" "\n"))))
816
817 (and (build-derivations %store (list drv))
818 (equal? (call-with-input-file out get-string-all)
819 (string-concatenate
820 (map cdr
821 (sort (map (lambda (p d)
822 (cons p (apply deps p d)))
823 (list input %bash %mkdir)
824 (list (list %bash %mkdir)
825 '() '()))
826 (lambda (x y)
827 (match x
828 ((p1 . _)
829 (match y
830 ((p2 . _)
831 (string<? p1 p2)))))))))))))
832
833
834 (test-assert "graft-derivation"
835 (let* ((build `(begin
836 (mkdir %output)
837 (chdir %output)
838 (symlink %output "self")
839 (call-with-output-file "text"
840 (lambda (output)
841 (format output "foo/~a/bar" ,%mkdir)))
842 (symlink ,%bash "sh")))
843 (orig (build-expression->derivation %store "graft" build
844 #:inputs `(("a" ,%bash)
845 ("b" ,%mkdir))))
846 (one (add-text-to-store %store "bash" "fake bash"))
847 (two (build-expression->derivation %store "mkdir"
848 '(call-with-output-file %output
849 (lambda (port)
850 (display "fake mkdir" port)))))
851 (graft (graft-derivation %store "graft" orig
852 (list (graft
853 (origin %bash)
854 (replacement one))
855 (graft
856 (origin %mkdir)
857 (replacement two))))))
858 (and (build-derivations %store (list graft))
859 (let ((two (derivation->output-path two))
860 (graft (derivation->output-path graft)))
861 (and (string=? (format #f "foo/~a/bar" two)
862 (call-with-input-file (string-append graft "/text")
863 get-string-all))
864 (string=? (readlink (string-append graft "/sh")) one)
865 (string=? (readlink (string-append graft "/self")) graft))))))
866
867 (test-equal "map-derivation"
868 "hello"
869 (let* ((joke (package-derivation %store guile-1.8))
870 (good (package-derivation %store %bootstrap-guile))
871 (drv1 (build-expression->derivation %store "original-drv1"
872 #f ; systematically fail
873 #:guile-for-build joke))
874 (drv2 (build-expression->derivation %store "original-drv2"
875 '(call-with-output-file %output
876 (lambda (p)
877 (display "hello" p)))))
878 (drv3 (build-expression->derivation %store "drv-to-remap"
879 '(let ((in (assoc-ref
880 %build-inputs "in")))
881 (copy-file in %output))
882 #:inputs `(("in" ,drv1))
883 #:guile-for-build joke))
884 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
885 (,joke . ,good))))
886 (out (derivation->output-path drv4)))
887 (and (build-derivations %store (list (pk 'remapped drv4)))
888 (call-with-input-file out get-string-all))))
889
890 (test-equal "map-derivation, sources"
891 "hello"
892 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
893 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
894 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
895 (drv1 (derivation %store "drv-to-remap"
896
897 ;; XXX: This wouldn't work in practice, but if
898 ;; we append "/bin/bash" then we can't replace
899 ;; it with the bootstrap bash, which is a
900 ;; single file.
901 (derivation->output-path bash-full)
902
903 `("-e" ,script1)
904 #:inputs `((,bash-full) (,script1))))
905 (drv2 (map-derivation %store drv1
906 `((,bash-full . ,%bash)
907 (,script1 . ,script2))))
908 (out (derivation->output-path drv2)))
909 (and (build-derivations %store (list (pk 'remapped* drv2)))
910 (call-with-input-file out get-string-all))))
911
912 (test-end)
913
914 \f
915 (exit (= (test-runner-fail-count (test-runner-current)) 0))