daemon: Add "builtin:download" derivation builder.
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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 (unsetenv "http_proxy")
20
21 (define-module (test-derivations)
22 #:use-module (guix derivations)
23 #:use-module (guix grafts)
24 #:use-module (guix store)
25 #:use-module (guix utils)
26 #:use-module (guix hash)
27 #:use-module (guix base32)
28 #:use-module (guix tests)
29 #:use-module (guix tests http)
30 #:use-module ((guix packages) #:select (package-derivation base32))
31 #:use-module ((guix build utils) #:select (executable-file?))
32 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
33 #:use-module (gnu packages bootstrap)
34 #:use-module ((gnu packages guile) #:select (guile-1.8))
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-11)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-64)
40 #:use-module (rnrs io ports)
41 #:use-module (rnrs bytevectors)
42 #:use-module (web uri)
43 #:use-module (ice-9 rdelim)
44 #:use-module (ice-9 regex)
45 #:use-module (ice-9 ftw)
46 #:use-module (ice-9 match))
47
48 (define %store
49 (open-connection-for-tests))
50
51 ;; Globally disable grafts because they can trigger early builds.
52 (%graft? #f)
53
54 (define (bootstrap-binary name)
55 (let ((bin (search-bootstrap-binary name (%current-system))))
56 (and %store
57 (add-to-store %store name #t "sha256" bin))))
58
59 (define %bash
60 (bootstrap-binary "bash"))
61 (define %mkdir
62 (bootstrap-binary "mkdir"))
63
64 (define* (directory-contents dir #:optional (slurp get-bytevector-all))
65 "Return an alist representing the contents of DIR."
66 (define prefix-len (string-length dir))
67 (sort (file-system-fold (const #t) ; enter?
68 (lambda (path stat result) ; leaf
69 (alist-cons (string-drop path prefix-len)
70 (call-with-input-file path slurp)
71 result))
72 (lambda (path stat result) result) ; down
73 (lambda (path stat result) result) ; up
74 (lambda (path stat result) result) ; skip
75 (lambda (path stat errno result) result) ; error
76 '()
77 dir)
78 (lambda (e1 e2)
79 (string<? (car e1) (car e2)))))
80
81 ;; Avoid collisions with other tests.
82 (%http-server-port 10500)
83
84 \f
85 (test-begin "derivations")
86
87 (test-assert "parse & export"
88 (let* ((f (search-path %load-path "tests/test.drv"))
89 (b1 (call-with-input-file f get-bytevector-all))
90 (d1 (read-derivation (open-bytevector-input-port b1)))
91 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
92 (d2 (read-derivation (open-bytevector-input-port b2))))
93 (and (equal? b1 b2)
94 (equal? d1 d2))))
95
96 (test-skip (if %store 0 12))
97
98 (test-assert "add-to-store, flat"
99 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
100 (drv (add-to-store %store "flat-test" #f "sha256" file)))
101 (and (eq? 'regular (stat:type (stat drv)))
102 (valid-path? %store drv)
103 (equal? (call-with-input-file file get-bytevector-all)
104 (call-with-input-file drv get-bytevector-all)))))
105
106 (test-assert "add-to-store, recursive"
107 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
108 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
109 (and (eq? 'directory (stat:type (stat drv)))
110 (valid-path? %store drv)
111 (equal? (directory-contents dir)
112 (directory-contents drv)))))
113
114 (test-assert "derivation with no inputs"
115 (let* ((builder (add-text-to-store %store "my-builder.sh"
116 "echo hello, world\n"
117 '()))
118 (drv (derivation %store "foo"
119 %bash `("-e" ,builder)
120 #:env-vars '(("HOME" . "/homeless")))))
121 (and (store-path? (derivation-file-name drv))
122 (valid-path? %store (derivation-file-name drv)))))
123
124 (test-assert "build derivation with 1 source"
125 (let* ((builder (add-text-to-store %store "my-builder.sh"
126 "echo hello, world > \"$out\"\n"
127 '()))
128 (drv (derivation %store "foo"
129 %bash `(,builder)
130 #:env-vars '(("HOME" . "/homeless")
131 ("zzz" . "Z!")
132 ("AAA" . "A!"))
133 #:inputs `((,%bash) (,builder))))
134 (succeeded?
135 (build-derivations %store (list drv))))
136 (and succeeded?
137 (let ((path (derivation->output-path drv)))
138 (and (valid-path? %store path)
139 (string=? (call-with-input-file path read-line)
140 "hello, world"))))))
141
142 (test-assert "derivation with local file as input"
143 (let* ((builder (add-text-to-store
144 %store "my-builder.sh"
145 "(while read line ; do echo \"$line\" ; done) < $in > $out"
146 '()))
147 (input (search-path %load-path "ice-9/boot-9.scm"))
148 (input* (add-to-store %store (basename input)
149 #t "sha256" input))
150 (drv (derivation %store "derivation-with-input-file"
151 %bash `(,builder)
152
153 ;; Cheat to pass the actual file name to the
154 ;; builder.
155 #:env-vars `(("in" . ,input*))
156
157 #:inputs `((,%bash)
158 (,builder)
159 (,input))))) ; ← local file name
160 (and (build-derivations %store (list drv))
161 ;; Note: we can't compare the files because the above trick alters
162 ;; the contents.
163 (valid-path? %store (derivation->output-path drv)))))
164
165 (test-assert "derivation fails but keep going"
166 ;; In keep-going mode, 'build-derivations' should fail because of D1, but it
167 ;; must return only after D2 has succeeded.
168 (with-store store
169 (let* ((d1 (derivation %store "fails"
170 %bash `("-c" "false")
171 #:inputs `((,%bash))))
172 (d2 (build-expression->derivation %store "sleep-then-succeed"
173 `(begin
174 ,(random-text)
175 ;; XXX: Hopefully that's long
176 ;; enough that D1 has already
177 ;; failed.
178 (sleep 2)
179 (mkdir %output)))))
180 (set-build-options %store
181 #:use-substitutes? #f
182 #:keep-going? #t)
183 (guard (c ((nix-protocol-error? c)
184 (and (= 100 (nix-protocol-error-status c))
185 (string-contains (nix-protocol-error-message c)
186 (derivation-file-name d1))
187 (not (valid-path? %store (derivation->output-path d1)))
188 (valid-path? %store (derivation->output-path d2)))))
189 (build-derivations %store (list d1 d2))
190 #f))))
191
192 (test-assert "identical files are deduplicated"
193 (let* ((build1 (add-text-to-store %store "one.sh"
194 "echo hello, world > \"$out\"\n"
195 '()))
196 (build2 (add-text-to-store %store "two.sh"
197 "# Hey!\necho hello, world > \"$out\"\n"
198 '()))
199 (drv1 (derivation %store "foo"
200 %bash `(,build1)
201 #:inputs `((,%bash) (,build1))))
202 (drv2 (derivation %store "bar"
203 %bash `(,build2)
204 #:inputs `((,%bash) (,build2)))))
205 (and (build-derivations %store (list drv1 drv2))
206 (let ((file1 (derivation->output-path drv1))
207 (file2 (derivation->output-path drv2)))
208 (and (valid-path? %store file1) (valid-path? %store file2)
209 (string=? (call-with-input-file file1 get-string-all)
210 "hello, world\n")
211 (= (stat:ino (lstat file1))
212 (stat:ino (lstat file2))))))))
213
214 (test-assert "unknown built-in builder"
215 (let ((drv (derivation %store "ohoh" "builtin:does-not-exist" '())))
216 (guard (c ((nix-protocol-error? c)
217 (string-contains (nix-protocol-error-message c) "failed")))
218 (build-derivations %store (list drv))
219 #f)))
220
221 (unless (force %http-server-socket)
222 (test-skip 1))
223 (test-assert "'download' built-in builder"
224 (let ((text (random-text)))
225 (with-http-server 200 text
226 (let* ((drv (derivation %store "world"
227 "builtin:download" '()
228 #:env-vars `(("url"
229 . ,(object->string (%local-url))))
230 #:hash-algo 'sha256
231 #:hash (sha256 (string->utf8 text)))))
232 (and (build-derivations %store (list drv))
233 (string=? (call-with-input-file (derivation->output-path drv)
234 get-string-all)
235 text))))))
236
237 (unless (force %http-server-socket)
238 (test-skip 1))
239 (test-assert "'download' built-in builder, invalid hash"
240 (with-http-server 200 "hello, world!"
241 (let* ((drv (derivation %store "world"
242 "builtin:download" '()
243 #:env-vars `(("url"
244 . ,(object->string (%local-url))))
245 #:hash-algo 'sha256
246 #:hash (sha256 (random-bytevector 100))))) ;wrong
247 (guard (c ((nix-protocol-error? c)
248 (string-contains (nix-protocol-error-message c) "failed")))
249 (build-derivations %store (list drv))
250 #f))))
251
252 (unless (force %http-server-socket)
253 (test-skip 1))
254 (test-assert "'download' built-in builder, not found"
255 (with-http-server 404 "not found"
256 (let* ((drv (derivation %store "will-never-be-found"
257 "builtin:download" '()
258 #:env-vars `(("url"
259 . ,(object->string (%local-url))))
260 #:hash-algo 'sha256
261 #:hash (sha256 (random-bytevector 100)))))
262 (guard (c ((nix-protocol-error? c)
263 (string-contains (nix-protocol-error-message (pk c)) "failed")))
264 (build-derivations %store (list drv))
265 #f))))
266
267 (test-assert "'download' built-in builder, not fixed-output"
268 (let* ((source (add-text-to-store %store "hello" "hi!"))
269 (url (string-append "file://" source))
270 (drv (derivation %store "world"
271 "builtin:download" '()
272 #:env-vars `(("url" . ,(object->string url))))))
273 (guard (c ((nix-protocol-error? c)
274 (string-contains (nix-protocol-error-message c) "failed")))
275 (build-derivations %store (list drv))
276 #f)))
277
278 (test-equal "derivation-name"
279 "foo-0.0"
280 (let ((drv (derivation %store "foo-0.0" %bash '())))
281 (derivation-name drv)))
282
283 (test-equal "derivation-output-names"
284 '(("out") ("bar" "chbouib"))
285 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
286 (drv2 (derivation %store "foo-0.0" %bash '()
287 #:outputs '("bar" "chbouib"))))
288 (list (derivation-output-names drv1)
289 (derivation-output-names drv2))))
290
291 (test-assert "offloadable-derivation?"
292 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
293 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
294 (derivation %store "foo" %bash '()
295 #:substitutable? #f))
296 (not (offloadable-derivation?
297 (derivation %store "foo" %bash '()
298 #:local-build? #t)))))
299
300 (test-assert "substitutable-derivation?"
301 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
302 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
303 (derivation %store "foo" %bash '()
304 #:local-build? #t))
305 (not (substitutable-derivation?
306 (derivation %store "foo" %bash '()
307 #:substitutable? #f)))))
308
309 (test-assert "fixed-output-derivation?"
310 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
311 "echo -n hello > $out" '()))
312 (hash (sha256 (string->utf8 "hello")))
313 (drv (derivation %store "fixed"
314 %bash `(,builder)
315 #:inputs `((,builder))
316 #:hash hash #:hash-algo 'sha256)))
317 (fixed-output-derivation? drv)))
318
319 (test-assert "fixed-output derivation"
320 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
321 "echo -n hello > $out" '()))
322 (hash (sha256 (string->utf8 "hello")))
323 (drv (derivation %store "fixed"
324 %bash `(,builder)
325 #:inputs `((,builder)) ; optional
326 #:hash hash #:hash-algo 'sha256))
327 (succeeded? (build-derivations %store (list drv))))
328 (and succeeded?
329 (let ((p (derivation->output-path drv)))
330 (and (equal? (string->utf8 "hello")
331 (call-with-input-file p get-bytevector-all))
332 (bytevector? (query-path-hash %store p)))))))
333
334 (test-assert "fixed-output derivation: output paths are equal"
335 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
336 "echo -n hello > $out" '()))
337 (builder2 (add-text-to-store %store "fixed-builder2.sh"
338 "echo hey; echo -n hello > $out" '()))
339 (hash (sha256 (string->utf8 "hello")))
340 (drv1 (derivation %store "fixed"
341 %bash `(,builder1)
342 #:hash hash #:hash-algo 'sha256))
343 (drv2 (derivation %store "fixed"
344 %bash `(,builder2)
345 #:hash hash #:hash-algo 'sha256))
346 (succeeded? (build-derivations %store (list drv1 drv2))))
347 (and succeeded?
348 (equal? (derivation->output-path drv1)
349 (derivation->output-path drv2)))))
350
351 (test-assert "fixed-output derivation, recursive"
352 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
353 "echo -n hello > $out" '()))
354 (hash (sha256 (string->utf8 "hello")))
355 (drv (derivation %store "fixed-rec"
356 %bash `(,builder)
357 #:inputs `((,builder))
358 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
359 #:hash-algo 'sha256
360 #:recursive? #t))
361 (succeeded? (build-derivations %store (list drv))))
362 (and succeeded?
363 (let ((p (derivation->output-path drv)))
364 (and (equal? (string->utf8 "hello")
365 (call-with-input-file p get-bytevector-all))
366 (bytevector? (query-path-hash %store p)))))))
367
368 (test-assert "derivation with a fixed-output input"
369 ;; A derivation D using a fixed-output derivation F doesn't has the same
370 ;; output path when passed F or F', as long as F and F' have the same output
371 ;; path.
372 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
373 "echo -n hello > $out" '()))
374 (builder2 (add-text-to-store %store "fixed-builder2.sh"
375 "echo hey; echo -n hello > $out" '()))
376 (hash (sha256 (string->utf8 "hello")))
377 (fixed1 (derivation %store "fixed"
378 %bash `(,builder1)
379 #:hash hash #:hash-algo 'sha256))
380 (fixed2 (derivation %store "fixed"
381 %bash `(,builder2)
382 #:hash hash #:hash-algo 'sha256))
383 (fixed-out (derivation->output-path fixed1))
384 (builder3 (add-text-to-store
385 %store "final-builder.sh"
386 ;; Use Bash hackery to avoid Coreutils.
387 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
388 (final1 (derivation %store "final"
389 %bash `(,builder3)
390 #:env-vars `(("in" . ,fixed-out))
391 #:inputs `((,%bash) (,builder3) (,fixed1))))
392 (final2 (derivation %store "final"
393 %bash `(,builder3)
394 #:env-vars `(("in" . ,fixed-out))
395 #:inputs `((,%bash) (,builder3) (,fixed2))))
396 (succeeded? (build-derivations %store
397 (list final1 final2))))
398 (and succeeded?
399 (equal? (derivation->output-path final1)
400 (derivation->output-path final2)))))
401
402 (test-assert "multiple-output derivation"
403 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
404 "echo one > $out ; echo two > $second"
405 '()))
406 (drv (derivation %store "fixed"
407 %bash `(,builder)
408 #:env-vars '(("HOME" . "/homeless")
409 ("zzz" . "Z!")
410 ("AAA" . "A!"))
411 #:inputs `((,%bash) (,builder))
412 #:outputs '("out" "second")))
413 (succeeded? (build-derivations %store (list drv))))
414 (and succeeded?
415 (let ((one (derivation->output-path drv "out"))
416 (two (derivation->output-path drv "second")))
417 (and (lset= equal?
418 (derivation->output-paths drv)
419 `(("out" . ,one) ("second" . ,two)))
420 (eq? 'one (call-with-input-file one read))
421 (eq? 'two (call-with-input-file two read)))))))
422
423 (test-assert "multiple-output derivation, non-alphabetic order"
424 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
425 ;; path computation must reorder them first.
426 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
427 "echo one > $out ; echo two > $AAA"
428 '()))
429 (drv (derivation %store "fixed"
430 %bash `(,builder)
431 #:inputs `((,%bash) (,builder))
432 #:outputs '("out" "AAA")))
433 (succeeded? (build-derivations %store (list drv))))
434 (and succeeded?
435 (let ((one (derivation->output-path drv "out"))
436 (two (derivation->output-path drv "AAA")))
437 (and (eq? 'one (call-with-input-file one read))
438 (eq? 'two (call-with-input-file two read)))))))
439
440 (test-assert "read-derivation vs. derivation"
441 ;; Make sure 'derivation' and 'read-derivation' return objects that are
442 ;; identical.
443 (let* ((sources (unfold (cut >= <> 10)
444 (lambda (n)
445 (add-text-to-store %store
446 (format #f "input~a" n)
447 (random-text)))
448 1+
449 0))
450 (inputs (map (lambda (file)
451 (derivation %store "derivation-input"
452 %bash '()
453 #:inputs `((,%bash) (,file))))
454 sources))
455 (builder (add-text-to-store %store "builder.sh"
456 "echo one > $one ; echo two > $two"
457 '()))
458 (drv (derivation %store "derivation"
459 %bash `(,builder)
460 #:inputs `((,%bash) (,builder)
461 ,@(map list (append sources inputs)))
462 #:outputs '("two" "one")))
463 (drv* (call-with-input-file (derivation-file-name drv)
464 read-derivation)))
465 (equal? drv* drv)))
466
467 (test-assert "multiple-output derivation, derivation-path->output-path"
468 (let* ((builder (add-text-to-store %store "builder.sh"
469 "echo one > $out ; echo two > $second"
470 '()))
471 (drv (derivation %store "multiple"
472 %bash `(,builder)
473 #:outputs '("out" "second")))
474 (drv-file (derivation-file-name drv))
475 (one (derivation->output-path drv "out"))
476 (two (derivation->output-path drv "second"))
477 (first (derivation-path->output-path drv-file "out"))
478 (second (derivation-path->output-path drv-file "second")))
479 (and (not (string=? one two))
480 (string-suffix? "-second" two)
481 (string=? first one)
482 (string=? second two))))
483
484 (test-assert "user of multiple-output derivation"
485 ;; Check whether specifying several inputs coming from the same
486 ;; multiple-output derivation works.
487 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
488 "echo one > $out ; echo two > $two"
489 '()))
490 (mdrv (derivation %store "multiple-output"
491 %bash `(,builder1)
492 #:inputs `((,%bash) (,builder1))
493 #:outputs '("out" "two")))
494 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
495 "read x < $one;
496 read y < $two;
497 echo \"($x $y)\" > $out"
498 '()))
499 (udrv (derivation %store "multiple-output-user"
500 %bash `(,builder2)
501 #:env-vars `(("one"
502 . ,(derivation->output-path
503 mdrv "out"))
504 ("two"
505 . ,(derivation->output-path
506 mdrv "two")))
507 #:inputs `((,%bash)
508 (,builder2)
509 ;; two occurrences of MDRV:
510 (,mdrv)
511 (,mdrv "two")))))
512 (and (build-derivations %store (list (pk 'udrv udrv)))
513 (let ((p (derivation->output-path udrv)))
514 (and (valid-path? %store p)
515 (equal? '(one two) (call-with-input-file p read)))))))
516
517 (test-assert "derivation with #:references-graphs"
518 (let* ((input1 (add-text-to-store %store "foo" "hello"
519 (list %bash)))
520 (input2 (add-text-to-store %store "bar"
521 (number->string (random 7777))
522 (list input1)))
523 (builder (add-text-to-store %store "build-graph"
524 (format #f "
525 ~a $out
526 (while read l ; do echo $l ; done) < bash > $out/bash
527 (while read l ; do echo $l ; done) < input1 > $out/input1
528 (while read l ; do echo $l ; done) < input2 > $out/input2"
529 %mkdir)
530 (list %mkdir)))
531 (drv (derivation %store "closure-graphs"
532 %bash `(,builder)
533 #:references-graphs
534 `(("bash" . ,%bash)
535 ("input1" . ,input1)
536 ("input2" . ,input2))
537 #:inputs `((,%bash) (,builder))))
538 (out (derivation->output-path drv)))
539 (define (deps path . deps)
540 (let ((count (length deps)))
541 (string-append path "\n\n" (number->string count) "\n"
542 (string-join (sort deps string<?) "\n")
543 (if (zero? count) "" "\n"))))
544
545 (and (build-derivations %store (list drv))
546 (equal? (directory-contents out get-string-all)
547 `(("/bash" . ,(string-append %bash "\n\n0\n"))
548 ("/input1" . ,(if (string>? input1 %bash)
549 (string-append (deps %bash)
550 (deps input1 %bash))
551 (string-append (deps input1 %bash)
552 (deps %bash))))
553 ("/input2" . ,(string-concatenate
554 (map cdr
555 (sort
556 (map (lambda (p d)
557 (cons p (apply deps p d)))
558 (list %bash input1 input2)
559 (list '() (list %bash) (list input1)))
560 (lambda (x y)
561 (match x
562 ((p1 . _)
563 (match y
564 ((p2 . _)
565 (string<? p1 p2)))))))))))))))
566
567 (test-assert "derivation #:allowed-references, ok"
568 (let ((drv (derivation %store "allowed" %bash
569 '("-c" "echo hello > $out")
570 #:inputs `((,%bash))
571 #:allowed-references '())))
572 (build-derivations %store (list drv))))
573
574 (test-assert "derivation #:allowed-references, not allowed"
575 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
576 (drv (derivation %store "disallowed" %bash
577 `("-c" ,(string-append "echo " txt "> $out"))
578 #:inputs `((,%bash) (,txt))
579 #:allowed-references '())))
580 (guard (c ((nix-protocol-error? c)
581 ;; There's no specific error message to check for.
582 #t))
583 (build-derivations %store (list drv))
584 #f)))
585
586 (test-assert "derivation #:allowed-references, self allowed"
587 (let ((drv (derivation %store "allowed" %bash
588 '("-c" "echo $out > $out")
589 #:inputs `((,%bash))
590 #:allowed-references '("out"))))
591 (build-derivations %store (list drv))))
592
593 (test-assert "derivation #:allowed-references, self not allowed"
594 (let ((drv (derivation %store "disallowed" %bash
595 `("-c" ,"echo $out > $out")
596 #:inputs `((,%bash))
597 #:allowed-references '())))
598 (guard (c ((nix-protocol-error? c)
599 ;; There's no specific error message to check for.
600 #t))
601 (build-derivations %store (list drv))
602 #f)))
603
604 (test-assert "derivation #:disallowed-references, ok"
605 (let ((drv (derivation %store "disallowed" %bash
606 '("-c" "echo hello > $out")
607 #:inputs `((,%bash))
608 #:disallowed-references '("out"))))
609 (build-derivations %store (list drv))))
610
611 (test-assert "derivation #:disallowed-references, not ok"
612 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
613 (drv (derivation %store "disdisallowed" %bash
614 `("-c" ,(string-append "echo " txt "> $out"))
615 #:inputs `((,%bash) (,txt))
616 #:disallowed-references (list txt))))
617 (guard (c ((nix-protocol-error? c)
618 ;; There's no specific error message to check for.
619 #t))
620 (build-derivations %store (list drv))
621 #f)))
622
623 ;; Here we should get the value of $NIX_STATE_DIR that the daemon sees, which
624 ;; is a unique value for each test process; this value is the same as the one
625 ;; we see in the process executing this file since it is set by 'test-env'.
626 (test-equal "derivation #:leaked-env-vars"
627 (getenv "NIX_STATE_DIR")
628 (let* ((value (getenv "NIX_STATE_DIR"))
629 (drv (derivation %store "leaked-env-vars" %bash
630 '("-c" "echo -n $NIX_STATE_DIR > $out")
631 #:hash (sha256 (string->utf8 value))
632 #:hash-algo 'sha256
633 #:inputs `((,%bash))
634 #:leaked-env-vars '("NIX_STATE_DIR"))))
635 (and (build-derivations %store (list drv))
636 (call-with-input-file (derivation->output-path drv)
637 get-string-all))))
638
639 \f
640 (define %coreutils
641 (false-if-exception
642 (and (network-reachable?)
643 (package-derivation %store %bootstrap-coreutils&co))))
644
645 (test-skip (if %coreutils 0 1))
646
647 (test-assert "build derivation with coreutils"
648 (let* ((builder
649 (add-text-to-store %store "build-with-coreutils.sh"
650 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
651 '()))
652 (drv
653 (derivation %store "foo"
654 %bash `(,builder)
655 #:env-vars `(("PATH" .
656 ,(string-append
657 (derivation->output-path %coreutils)
658 "/bin")))
659 #:inputs `((,builder)
660 (,%coreutils))))
661 (succeeded?
662 (build-derivations %store (list drv))))
663 (and succeeded?
664 (let ((p (derivation->output-path drv)))
665 (and (valid-path? %store p)
666 (file-exists? (string-append p "/good")))))))
667
668 (test-skip (if (%guile-for-build) 0 8))
669
670 (test-equal "build-expression->derivation and invalid module name"
671 '(file-search-error "guix/module/that/does/not/exist.scm")
672 (guard (c ((file-search-error? c)
673 (list 'file-search-error
674 (file-search-error-file-name c))))
675 (build-expression->derivation %store "foo" #t
676 #:modules '((guix module that
677 does not exist)))))
678
679 (test-assert "build-expression->derivation and derivation-prerequisites"
680 (let ((drv (build-expression->derivation %store "fail" #f)))
681 (any (match-lambda
682 (($ <derivation-input> path)
683 (string=? path (derivation-file-name (%guile-for-build)))))
684 (derivation-prerequisites drv))))
685
686 (test-assert "derivation-prerequisites and valid-derivation-input?"
687 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
688 (b (build-expression->derivation %store "b" `(list ,(random-text))))
689 (c (build-expression->derivation %store "c" `(mkdir %output)
690 #:inputs `(("a" ,a) ("b" ,b)))))
691 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
692 ;; be removed by tests/guix-gc.sh.)
693 (build-derivations %store
694 (list a (package-derivation %store %bootstrap-guile)))
695
696 (match (derivation-prerequisites c
697 (cut valid-derivation-input? %store
698 <>))
699 ((($ <derivation-input> file ("out")))
700 (string=? file (derivation-file-name b)))
701 (x
702 (pk 'fail x #f)))))
703
704 (test-assert "build-expression->derivation without inputs"
705 (let* ((builder '(begin
706 (mkdir %output)
707 (call-with-output-file (string-append %output "/test")
708 (lambda (p)
709 (display '(hello guix) p)))))
710 (drv (build-expression->derivation %store "goo" builder))
711 (succeeded? (build-derivations %store (list drv))))
712 (and succeeded?
713 (let ((p (derivation->output-path drv)))
714 (equal? '(hello guix)
715 (call-with-input-file (string-append p "/test") read))))))
716
717 (test-assert "build-expression->derivation and max-silent-time"
718 (let* ((store (let ((s (open-connection)))
719 (set-build-options s #:max-silent-time 1)
720 s))
721 (builder '(begin (sleep 100) (mkdir %output) #t))
722 (drv (build-expression->derivation store "silent" builder))
723 (out-path (derivation->output-path drv)))
724 (guard (c ((nix-protocol-error? c)
725 (and (string-contains (nix-protocol-error-message c)
726 "failed")
727 (not (valid-path? store out-path)))))
728 (build-derivations store (list drv))
729 #f)))
730
731 (test-assert "build-expression->derivation and timeout"
732 (let* ((store (let ((s (open-connection)))
733 (set-build-options s #:timeout 1)
734 s))
735 (builder '(begin (sleep 100) (mkdir %output) #t))
736 (drv (build-expression->derivation store "slow" builder))
737 (out-path (derivation->output-path drv)))
738 (guard (c ((nix-protocol-error? c)
739 (and (string-contains (nix-protocol-error-message c)
740 "failed")
741 (not (valid-path? store out-path)))))
742 (build-derivations store (list drv))
743 #f)))
744
745 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
746 (let ((drv (build-expression->derivation %store "fail" #f)))
747 ;; The only direct dependency is (%guile-for-build) and it's already
748 ;; built.
749 (null? (derivation-prerequisites-to-build %store drv))))
750
751 (test-assert "derivation-prerequisites-to-build when outputs already present"
752 (let* ((builder '(begin (mkdir %output) #t))
753 (input-drv (build-expression->derivation %store "input" builder))
754 (input-path (derivation-output-path
755 (assoc-ref (derivation-outputs input-drv)
756 "out")))
757 (drv (build-expression->derivation %store "something" builder
758 #:inputs
759 `(("i" ,input-drv))))
760 (output (derivation->output-path drv)))
761 ;; Make sure these things are not already built.
762 (when (valid-path? %store input-path)
763 (delete-paths %store (list input-path)))
764 (when (valid-path? %store output)
765 (delete-paths %store (list output)))
766
767 (and (equal? (map derivation-input-path
768 (derivation-prerequisites-to-build %store drv))
769 (list (derivation-file-name input-drv)))
770
771 ;; Build DRV and delete its input.
772 (build-derivations %store (list drv))
773 (delete-paths %store (list input-path))
774 (not (valid-path? %store input-path))
775
776 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
777 ;; prerequisite to build because DRV itself is already built.
778 (null? (derivation-prerequisites-to-build %store drv)))))
779
780 (test-assert "derivation-prerequisites-to-build and substitutes"
781 (let* ((store (open-connection))
782 (drv (build-expression->derivation store "prereq-subst"
783 (random 1000)))
784 (output (derivation->output-path drv)))
785
786 ;; Make sure substitutes are usable.
787 (set-build-options store #:use-substitutes? #t
788 #:substitute-urls (%test-substitute-urls))
789
790 (with-derivation-narinfo drv
791 (let-values (((build download)
792 (derivation-prerequisites-to-build store drv))
793 ((build* download*)
794 (derivation-prerequisites-to-build store drv
795 #:substitutable?
796 (const #f))))
797 (and (null? build)
798 (equal? download (list output))
799 (null? download*)
800 (null? build*))))))
801
802 (test-assert "derivation-prerequisites-to-build and substitutes, non-substitutable build"
803 (let* ((store (open-connection))
804 (drv (build-expression->derivation store "prereq-no-subst"
805 (random 1000)
806 #:substitutable? #f))
807 (output (derivation->output-path drv)))
808
809 ;; Make sure substitutes are usable.
810 (set-build-options store #:use-substitutes? #t
811 #:substitute-urls (%test-substitute-urls))
812
813 (with-derivation-narinfo drv
814 (let-values (((build download)
815 (derivation-prerequisites-to-build store drv)))
816 ;; Despite being available as a substitute, DRV will be built locally
817 ;; due to #:substitutable? #f.
818 (and (null? download)
819 (match build
820 (((? derivation-input? input))
821 (string=? (derivation-input-path input)
822 (derivation-file-name drv)))))))))
823
824 (test-assert "derivation-prerequisites-to-build and substitutes, local build"
825 (with-store store
826 (let* ((drv (build-expression->derivation store "prereq-subst-local"
827 (random 1000)
828 #:local-build? #t))
829 (output (derivation->output-path drv)))
830
831 ;; Make sure substitutes are usable.
832 (set-build-options store #:use-substitutes? #t
833 #:substitute-urls (%test-substitute-urls))
834
835 (with-derivation-narinfo drv
836 (let-values (((build download)
837 (derivation-prerequisites-to-build store drv)))
838 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
839 ;; must be able to substitute DRV's output.
840 ;; See <http://bugs.gnu.org/18747>.
841 (and (null? build)
842 (match download
843 (((? string? item))
844 (string=? item (derivation->output-path drv))))))))))
845
846 (test-assert "derivation-prerequisites-to-build in 'check' mode"
847 (with-store store
848 (let* ((dep (build-expression->derivation store "dep"
849 `(begin ,(random-text)
850 (mkdir %output))))
851 (drv (build-expression->derivation store "to-check"
852 '(mkdir %output)
853 #:inputs `(("dep" ,dep)))))
854 (build-derivations store (list drv))
855 (delete-paths store (list (derivation->output-path dep)))
856
857 ;; In 'check' mode, DEP must be rebuilt.
858 (and (null? (derivation-prerequisites-to-build store drv))
859 (match (derivation-prerequisites-to-build store drv
860 #:mode (build-mode
861 check))
862 ((input)
863 (string=? (derivation-input-path input)
864 (derivation-file-name dep))))))))
865
866 (test-assert "build-expression->derivation with expression returning #f"
867 (let* ((builder '(begin
868 (mkdir %output)
869 #f)) ; fail!
870 (drv (build-expression->derivation %store "fail" builder))
871 (out-path (derivation->output-path drv)))
872 (guard (c ((nix-protocol-error? c)
873 ;; Note that the output path may exist at this point, but it
874 ;; is invalid.
875 (and (string-match "build .* failed"
876 (nix-protocol-error-message c))
877 (not (valid-path? %store out-path)))))
878 (build-derivations %store (list drv))
879 #f)))
880
881 (test-assert "build-expression->derivation with two outputs"
882 (let* ((builder '(begin
883 (call-with-output-file (assoc-ref %outputs "out")
884 (lambda (p)
885 (display '(hello) p)))
886 (call-with-output-file (assoc-ref %outputs "second")
887 (lambda (p)
888 (display '(world) p)))))
889 (drv (build-expression->derivation %store "double" builder
890 #:outputs '("out"
891 "second")))
892 (succeeded? (build-derivations %store (list drv))))
893 (and succeeded?
894 (let ((one (derivation->output-path drv))
895 (two (derivation->output-path drv "second")))
896 (and (equal? '(hello) (call-with-input-file one read))
897 (equal? '(world) (call-with-input-file two read)))))))
898
899 (test-skip (if %coreutils 0 1))
900 (test-assert "build-expression->derivation with one input"
901 (let* ((builder '(call-with-output-file %output
902 (lambda (p)
903 (let ((cu (assoc-ref %build-inputs "cu")))
904 (close 1)
905 (dup2 (port->fdes p) 1)
906 (execl (string-append cu "/bin/uname")
907 "uname" "-a")))))
908 (drv (build-expression->derivation %store "uname" builder
909 #:inputs
910 `(("cu" ,%coreutils))))
911 (succeeded? (build-derivations %store (list drv))))
912 (and succeeded?
913 (let ((p (derivation->output-path drv)))
914 (string-contains (call-with-input-file p read-line) "GNU")))))
915
916 (test-assert "build-expression->derivation with modules"
917 (let* ((builder `(begin
918 (use-modules (guix build utils))
919 (let ((out (assoc-ref %outputs "out")))
920 (mkdir-p (string-append out "/guile/guix/nix"))
921 #t)))
922 (drv (build-expression->derivation %store "test-with-modules"
923 builder
924 #:modules
925 '((guix build utils)))))
926 (and (build-derivations %store (list drv))
927 (let* ((p (derivation->output-path drv))
928 (s (stat (string-append p "/guile/guix/nix"))))
929 (eq? (stat:type s) 'directory)))))
930
931 (test-assert "build-expression->derivation: same fixed-output path"
932 (let* ((builder1 '(call-with-output-file %output
933 (lambda (p)
934 (write "hello" p))))
935 (builder2 '(call-with-output-file (pk 'difference-here! %output)
936 (lambda (p)
937 (write "hello" p))))
938 (hash (sha256 (string->utf8 "hello")))
939 (input1 (build-expression->derivation %store "fixed" builder1
940 #:hash hash
941 #:hash-algo 'sha256))
942 (input2 (build-expression->derivation %store "fixed" builder2
943 #:hash hash
944 #:hash-algo 'sha256))
945 (succeeded? (build-derivations %store (list input1 input2))))
946 (and succeeded?
947 (not (string=? (derivation-file-name input1)
948 (derivation-file-name input2)))
949 (string=? (derivation->output-path input1)
950 (derivation->output-path input2)))))
951
952 (test-assert "build-expression->derivation with a fixed-output input"
953 (let* ((builder1 '(call-with-output-file %output
954 (lambda (p)
955 (write "hello" p))))
956 (builder2 '(call-with-output-file (pk 'difference-here! %output)
957 (lambda (p)
958 (write "hello" p))))
959 (hash (sha256 (string->utf8 "hello")))
960 (input1 (build-expression->derivation %store "fixed" builder1
961 #:hash hash
962 #:hash-algo 'sha256))
963 (input2 (build-expression->derivation %store "fixed" builder2
964 #:hash hash
965 #:hash-algo 'sha256))
966 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
967 (call-with-output-file %output
968 (lambda (out)
969 (format #f "My input is ~a.~%" input)))))
970 (final1 (build-expression->derivation %store "final" builder3
971 #:inputs
972 `(("input" ,input1))))
973 (final2 (build-expression->derivation %store "final" builder3
974 #:inputs
975 `(("input" ,input2)))))
976 (and (string=? (derivation->output-path final1)
977 (derivation->output-path final2))
978 (string=? (derivation->output-path final1)
979 (derivation-path->output-path
980 (derivation-file-name final1)))
981 (build-derivations %store (list final1 final2)))))
982
983 (test-assert "build-expression->derivation produces recursive fixed-output"
984 (let* ((builder '(begin
985 (use-modules (srfi srfi-26))
986 (mkdir %output)
987 (chdir %output)
988 (call-with-output-file "exe"
989 (cut display "executable" <>))
990 (chmod "exe" #o777)
991 (symlink "exe" "symlink")
992 (mkdir "subdir")))
993 (drv (build-expression->derivation %store "fixed-rec" builder
994 #:hash-algo 'sha256
995 #:hash (base32
996 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
997 #:recursive? #t)))
998 (and (build-derivations %store (list drv))
999 (let* ((dir (derivation->output-path drv))
1000 (exe (string-append dir "/exe"))
1001 (link (string-append dir "/symlink"))
1002 (subdir (string-append dir "/subdir")))
1003 (and (executable-file? exe)
1004 (string=? "executable"
1005 (call-with-input-file exe get-string-all))
1006 (string=? "exe" (readlink link))
1007 (file-is-directory? subdir))))))
1008
1009 (test-assert "build-expression->derivation uses recursive fixed-output"
1010 (let* ((builder '(call-with-output-file %output
1011 (lambda (port)
1012 (display "hello" port))))
1013 (fixed (build-expression->derivation %store "small-fixed-rec"
1014 builder
1015 #:hash-algo 'sha256
1016 #:hash (base32
1017 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
1018 #:recursive? #t))
1019 (in (derivation->output-path fixed))
1020 (builder `(begin
1021 (mkdir %output)
1022 (chdir %output)
1023 (symlink ,in "symlink")))
1024 (drv (build-expression->derivation %store "fixed-rec-user"
1025 builder
1026 #:inputs `(("fixed" ,fixed)))))
1027 (and (build-derivations %store (list drv))
1028 (let ((out (derivation->output-path drv)))
1029 (string=? (readlink (string-append out "/symlink")) in)))))
1030
1031 (test-assert "build-expression->derivation with #:references-graphs"
1032 (let* ((input (add-text-to-store %store "foo" "hello"
1033 (list %bash %mkdir)))
1034 (builder '(copy-file "input" %output))
1035 (drv (build-expression->derivation %store "references-graphs"
1036 builder
1037 #:references-graphs
1038 `(("input" . ,input))))
1039 (out (derivation->output-path drv)))
1040 (define (deps path . deps)
1041 (let ((count (length deps)))
1042 (string-append path "\n\n" (number->string count) "\n"
1043 (string-join (sort deps string<?) "\n")
1044 (if (zero? count) "" "\n"))))
1045
1046 (and (build-derivations %store (list drv))
1047 (equal? (call-with-input-file out get-string-all)
1048 (string-concatenate
1049 (map cdr
1050 (sort (map (lambda (p d)
1051 (cons p (apply deps p d)))
1052 (list input %bash %mkdir)
1053 (list (list %bash %mkdir)
1054 '() '()))
1055 (lambda (x y)
1056 (match x
1057 ((p1 . _)
1058 (match y
1059 ((p2 . _)
1060 (string<? p1 p2)))))))))))))
1061
1062 (test-equal "map-derivation"
1063 "hello"
1064 (let* ((joke (package-derivation %store guile-1.8))
1065 (good (package-derivation %store %bootstrap-guile))
1066 (drv1 (build-expression->derivation %store "original-drv1"
1067 #f ; systematically fail
1068 #:guile-for-build joke))
1069 (drv2 (build-expression->derivation %store "original-drv2"
1070 '(call-with-output-file %output
1071 (lambda (p)
1072 (display "hello" p)))))
1073 (drv3 (build-expression->derivation %store "drv-to-remap"
1074 '(let ((in (assoc-ref
1075 %build-inputs "in")))
1076 (copy-file in %output))
1077 #:inputs `(("in" ,drv1))
1078 #:guile-for-build joke))
1079 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
1080 (,joke . ,good))))
1081 (out (derivation->output-path drv4)))
1082 (and (build-derivations %store (list (pk 'remapped drv4)))
1083 (call-with-input-file out get-string-all))))
1084
1085 (test-equal "map-derivation, sources"
1086 "hello"
1087 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
1088 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
1089 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
1090 (drv1 (derivation %store "drv-to-remap"
1091
1092 ;; XXX: This wouldn't work in practice, but if
1093 ;; we append "/bin/bash" then we can't replace
1094 ;; it with the bootstrap bash, which is a
1095 ;; single file.
1096 (derivation->output-path bash-full)
1097
1098 `("-e" ,script1)
1099 #:inputs `((,bash-full) (,script1))))
1100 (drv2 (map-derivation %store drv1
1101 `((,bash-full . ,%bash)
1102 (,script1 . ,script2))))
1103 (out (derivation->output-path drv2)))
1104 (and (build-derivations %store (list (pk 'remapped* drv2)))
1105 (call-with-input-file out get-string-all))))
1106
1107 (test-end)