derivations: Do not fetch narinfos for non-substitutable items.
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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-equal "built-in-builders"
215 '("download")
216 (built-in-builders %store))
217
218 (test-assert "unknown built-in builder"
219 (let ((drv (derivation %store "ohoh" "builtin:does-not-exist" '())))
220 (guard (c ((nix-protocol-error? c)
221 (string-contains (nix-protocol-error-message c) "failed")))
222 (build-derivations %store (list drv))
223 #f)))
224
225 (unless (force %http-server-socket)
226 (test-skip 1))
227 (test-assert "'download' built-in builder"
228 (let ((text (random-text)))
229 (with-http-server 200 text
230 (let* ((drv (derivation %store "world"
231 "builtin:download" '()
232 #:env-vars `(("url"
233 . ,(object->string (%local-url))))
234 #:hash-algo 'sha256
235 #:hash (sha256 (string->utf8 text)))))
236 (and (build-derivations %store (list drv))
237 (string=? (call-with-input-file (derivation->output-path drv)
238 get-string-all)
239 text))))))
240
241 (unless (force %http-server-socket)
242 (test-skip 1))
243 (test-assert "'download' built-in builder, invalid hash"
244 (with-http-server 200 "hello, world!"
245 (let* ((drv (derivation %store "world"
246 "builtin:download" '()
247 #:env-vars `(("url"
248 . ,(object->string (%local-url))))
249 #:hash-algo 'sha256
250 #:hash (sha256 (random-bytevector 100))))) ;wrong
251 (guard (c ((nix-protocol-error? c)
252 (string-contains (nix-protocol-error-message c) "failed")))
253 (build-derivations %store (list drv))
254 #f))))
255
256 (unless (force %http-server-socket)
257 (test-skip 1))
258 (test-assert "'download' built-in builder, not found"
259 (with-http-server 404 "not found"
260 (let* ((drv (derivation %store "will-never-be-found"
261 "builtin:download" '()
262 #:env-vars `(("url"
263 . ,(object->string (%local-url))))
264 #:hash-algo 'sha256
265 #:hash (sha256 (random-bytevector 100)))))
266 (guard (c ((nix-protocol-error? c)
267 (string-contains (nix-protocol-error-message (pk c)) "failed")))
268 (build-derivations %store (list drv))
269 #f))))
270
271 (test-assert "'download' built-in builder, not fixed-output"
272 (let* ((source (add-text-to-store %store "hello" "hi!"))
273 (url (string-append "file://" source))
274 (drv (derivation %store "world"
275 "builtin:download" '()
276 #:env-vars `(("url" . ,(object->string url))))))
277 (guard (c ((nix-protocol-error? c)
278 (string-contains (nix-protocol-error-message c) "failed")))
279 (build-derivations %store (list drv))
280 #f)))
281
282 (unless (force %http-server-socket)
283 (test-skip 1))
284 (test-assert "'download' built-in builder, check mode"
285 ;; Make sure rebuilding the 'builtin:download' derivation in check mode
286 ;; works. See <http://bugs.gnu.org/25089>.
287 (let* ((text (random-text))
288 (drv (derivation %store "world"
289 "builtin:download" '()
290 #:env-vars `(("url"
291 . ,(object->string (%local-url))))
292 #:hash-algo 'sha256
293 #:hash (sha256 (string->utf8 text)))))
294 (and (with-http-server 200 text
295 (build-derivations %store (list drv)))
296 (with-http-server 200 text
297 (build-derivations %store (list drv)
298 (build-mode check)))
299 (string=? (call-with-input-file (derivation->output-path drv)
300 get-string-all)
301 text))))
302
303 (test-equal "derivation-name"
304 "foo-0.0"
305 (let ((drv (derivation %store "foo-0.0" %bash '())))
306 (derivation-name drv)))
307
308 (test-equal "derivation-output-names"
309 '(("out") ("bar" "chbouib"))
310 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
311 (drv2 (derivation %store "foo-0.0" %bash '()
312 #:outputs '("bar" "chbouib"))))
313 (list (derivation-output-names drv1)
314 (derivation-output-names drv2))))
315
316 (test-assert "offloadable-derivation?"
317 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
318 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
319 (derivation %store "foo" %bash '()
320 #:substitutable? #f))
321 (not (offloadable-derivation?
322 (derivation %store "foo" %bash '()
323 #:local-build? #t)))))
324
325 (test-assert "substitutable-derivation?"
326 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
327 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
328 (derivation %store "foo" %bash '()
329 #:local-build? #t))
330 (not (substitutable-derivation?
331 (derivation %store "foo" %bash '()
332 #:substitutable? #f)))))
333
334 (test-assert "fixed-output-derivation?"
335 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
336 "echo -n hello > $out" '()))
337 (hash (sha256 (string->utf8 "hello")))
338 (drv (derivation %store "fixed"
339 %bash `(,builder)
340 #:inputs `((,builder))
341 #:hash hash #:hash-algo 'sha256)))
342 (fixed-output-derivation? drv)))
343
344 (test-assert "fixed-output derivation"
345 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
346 "echo -n hello > $out" '()))
347 (hash (sha256 (string->utf8 "hello")))
348 (drv (derivation %store "fixed"
349 %bash `(,builder)
350 #:inputs `((,builder)) ; optional
351 #:hash hash #:hash-algo 'sha256))
352 (succeeded? (build-derivations %store (list drv))))
353 (and succeeded?
354 (let ((p (derivation->output-path drv)))
355 (and (equal? (string->utf8 "hello")
356 (call-with-input-file p get-bytevector-all))
357 (bytevector? (query-path-hash %store p)))))))
358
359 (test-assert "fixed-output derivation: output paths are equal"
360 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
361 "echo -n hello > $out" '()))
362 (builder2 (add-text-to-store %store "fixed-builder2.sh"
363 "echo hey; echo -n hello > $out" '()))
364 (hash (sha256 (string->utf8 "hello")))
365 (drv1 (derivation %store "fixed"
366 %bash `(,builder1)
367 #:hash hash #:hash-algo 'sha256))
368 (drv2 (derivation %store "fixed"
369 %bash `(,builder2)
370 #:hash hash #:hash-algo 'sha256))
371 (succeeded? (build-derivations %store (list drv1 drv2))))
372 (and succeeded?
373 (equal? (derivation->output-path drv1)
374 (derivation->output-path drv2)))))
375
376 (test-assert "fixed-output derivation, recursive"
377 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
378 "echo -n hello > $out" '()))
379 (hash (sha256 (string->utf8 "hello")))
380 (drv (derivation %store "fixed-rec"
381 %bash `(,builder)
382 #:inputs `((,builder))
383 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
384 #:hash-algo 'sha256
385 #:recursive? #t))
386 (succeeded? (build-derivations %store (list drv))))
387 (and succeeded?
388 (let ((p (derivation->output-path drv)))
389 (and (equal? (string->utf8 "hello")
390 (call-with-input-file p get-bytevector-all))
391 (bytevector? (query-path-hash %store p)))))))
392
393 (test-assert "derivation with a fixed-output input"
394 ;; A derivation D using a fixed-output derivation F doesn't has the same
395 ;; output path when passed F or F', as long as F and F' have the same output
396 ;; path.
397 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
398 "echo -n hello > $out" '()))
399 (builder2 (add-text-to-store %store "fixed-builder2.sh"
400 "echo hey; echo -n hello > $out" '()))
401 (hash (sha256 (string->utf8 "hello")))
402 (fixed1 (derivation %store "fixed"
403 %bash `(,builder1)
404 #:hash hash #:hash-algo 'sha256))
405 (fixed2 (derivation %store "fixed"
406 %bash `(,builder2)
407 #:hash hash #:hash-algo 'sha256))
408 (fixed-out (derivation->output-path fixed1))
409 (builder3 (add-text-to-store
410 %store "final-builder.sh"
411 ;; Use Bash hackery to avoid Coreutils.
412 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
413 (final1 (derivation %store "final"
414 %bash `(,builder3)
415 #:env-vars `(("in" . ,fixed-out))
416 #:inputs `((,%bash) (,builder3) (,fixed1))))
417 (final2 (derivation %store "final"
418 %bash `(,builder3)
419 #:env-vars `(("in" . ,fixed-out))
420 #:inputs `((,%bash) (,builder3) (,fixed2))))
421 (succeeded? (build-derivations %store
422 (list final1 final2))))
423 (and succeeded?
424 (equal? (derivation->output-path final1)
425 (derivation->output-path final2)))))
426
427 (test-assert "multiple-output derivation"
428 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
429 "echo one > $out ; echo two > $second"
430 '()))
431 (drv (derivation %store "fixed"
432 %bash `(,builder)
433 #:env-vars '(("HOME" . "/homeless")
434 ("zzz" . "Z!")
435 ("AAA" . "A!"))
436 #:inputs `((,%bash) (,builder))
437 #:outputs '("out" "second")))
438 (succeeded? (build-derivations %store (list drv))))
439 (and succeeded?
440 (let ((one (derivation->output-path drv "out"))
441 (two (derivation->output-path drv "second")))
442 (and (lset= equal?
443 (derivation->output-paths drv)
444 `(("out" . ,one) ("second" . ,two)))
445 (eq? 'one (call-with-input-file one read))
446 (eq? 'two (call-with-input-file two read)))))))
447
448 (test-assert "multiple-output derivation, non-alphabetic order"
449 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
450 ;; path computation must reorder them first.
451 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
452 "echo one > $out ; echo two > $AAA"
453 '()))
454 (drv (derivation %store "fixed"
455 %bash `(,builder)
456 #:inputs `((,%bash) (,builder))
457 #:outputs '("out" "AAA")))
458 (succeeded? (build-derivations %store (list drv))))
459 (and succeeded?
460 (let ((one (derivation->output-path drv "out"))
461 (two (derivation->output-path drv "AAA")))
462 (and (eq? 'one (call-with-input-file one read))
463 (eq? 'two (call-with-input-file two read)))))))
464
465 (test-assert "read-derivation vs. derivation"
466 ;; Make sure 'derivation' and 'read-derivation' return objects that are
467 ;; identical.
468 (let* ((sources (unfold (cut >= <> 10)
469 (lambda (n)
470 (add-text-to-store %store
471 (format #f "input~a" n)
472 (random-text)))
473 1+
474 0))
475 (inputs (map (lambda (file)
476 (derivation %store "derivation-input"
477 %bash '()
478 #:inputs `((,%bash) (,file))))
479 sources))
480 (builder (add-text-to-store %store "builder.sh"
481 "echo one > $one ; echo two > $two"
482 '()))
483 (drv (derivation %store "derivation"
484 %bash `(,builder)
485 #:inputs `((,%bash) (,builder)
486 ,@(map list (append sources inputs)))
487 #:outputs '("two" "one")))
488 (drv* (call-with-input-file (derivation-file-name drv)
489 read-derivation)))
490 (equal? drv* drv)))
491
492 (test-assert "multiple-output derivation, derivation-path->output-path"
493 (let* ((builder (add-text-to-store %store "builder.sh"
494 "echo one > $out ; echo two > $second"
495 '()))
496 (drv (derivation %store "multiple"
497 %bash `(,builder)
498 #:outputs '("out" "second")))
499 (drv-file (derivation-file-name drv))
500 (one (derivation->output-path drv "out"))
501 (two (derivation->output-path drv "second"))
502 (first (derivation-path->output-path drv-file "out"))
503 (second (derivation-path->output-path drv-file "second")))
504 (and (not (string=? one two))
505 (string-suffix? "-second" two)
506 (string=? first one)
507 (string=? second two))))
508
509 (test-assert "user of multiple-output derivation"
510 ;; Check whether specifying several inputs coming from the same
511 ;; multiple-output derivation works.
512 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
513 "echo one > $out ; echo two > $two"
514 '()))
515 (mdrv (derivation %store "multiple-output"
516 %bash `(,builder1)
517 #:inputs `((,%bash) (,builder1))
518 #:outputs '("out" "two")))
519 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
520 "read x < $one;
521 read y < $two;
522 echo \"($x $y)\" > $out"
523 '()))
524 (udrv (derivation %store "multiple-output-user"
525 %bash `(,builder2)
526 #:env-vars `(("one"
527 . ,(derivation->output-path
528 mdrv "out"))
529 ("two"
530 . ,(derivation->output-path
531 mdrv "two")))
532 #:inputs `((,%bash)
533 (,builder2)
534 ;; two occurrences of MDRV:
535 (,mdrv)
536 (,mdrv "two")))))
537 (and (build-derivations %store (list (pk 'udrv udrv)))
538 (let ((p (derivation->output-path udrv)))
539 (and (valid-path? %store p)
540 (equal? '(one two) (call-with-input-file p read)))))))
541
542 (test-assert "derivation with #:references-graphs"
543 (let* ((input1 (add-text-to-store %store "foo" "hello"
544 (list %bash)))
545 (input2 (add-text-to-store %store "bar"
546 (number->string (random 7777))
547 (list input1)))
548 (builder (add-text-to-store %store "build-graph"
549 (format #f "
550 ~a $out
551 (while read l ; do echo $l ; done) < bash > $out/bash
552 (while read l ; do echo $l ; done) < input1 > $out/input1
553 (while read l ; do echo $l ; done) < input2 > $out/input2"
554 %mkdir)
555 (list %mkdir)))
556 (drv (derivation %store "closure-graphs"
557 %bash `(,builder)
558 #:references-graphs
559 `(("bash" . ,%bash)
560 ("input1" . ,input1)
561 ("input2" . ,input2))
562 #:inputs `((,%bash) (,builder))))
563 (out (derivation->output-path drv)))
564 (define (deps path . deps)
565 (let ((count (length deps)))
566 (string-append path "\n\n" (number->string count) "\n"
567 (string-join (sort deps string<?) "\n")
568 (if (zero? count) "" "\n"))))
569
570 (and (build-derivations %store (list drv))
571 (equal? (directory-contents out get-string-all)
572 `(("/bash" . ,(string-append %bash "\n\n0\n"))
573 ("/input1" . ,(if (string>? input1 %bash)
574 (string-append (deps %bash)
575 (deps input1 %bash))
576 (string-append (deps input1 %bash)
577 (deps %bash))))
578 ("/input2" . ,(string-concatenate
579 (map cdr
580 (sort
581 (map (lambda (p d)
582 (cons p (apply deps p d)))
583 (list %bash input1 input2)
584 (list '() (list %bash) (list input1)))
585 (lambda (x y)
586 (match x
587 ((p1 . _)
588 (match y
589 ((p2 . _)
590 (string<? p1 p2)))))))))))))))
591
592 (test-assert "derivation #:allowed-references, ok"
593 (let ((drv (derivation %store "allowed" %bash
594 '("-c" "echo hello > $out")
595 #:inputs `((,%bash))
596 #:allowed-references '())))
597 (build-derivations %store (list drv))))
598
599 (test-assert "derivation #:allowed-references, not allowed"
600 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
601 (drv (derivation %store "disallowed" %bash
602 `("-c" ,(string-append "echo " txt "> $out"))
603 #:inputs `((,%bash) (,txt))
604 #:allowed-references '())))
605 (guard (c ((nix-protocol-error? c)
606 ;; There's no specific error message to check for.
607 #t))
608 (build-derivations %store (list drv))
609 #f)))
610
611 (test-assert "derivation #:allowed-references, self allowed"
612 (let ((drv (derivation %store "allowed" %bash
613 '("-c" "echo $out > $out")
614 #:inputs `((,%bash))
615 #:allowed-references '("out"))))
616 (build-derivations %store (list drv))))
617
618 (test-assert "derivation #:allowed-references, self not allowed"
619 (let ((drv (derivation %store "disallowed" %bash
620 `("-c" ,"echo $out > $out")
621 #:inputs `((,%bash))
622 #:allowed-references '())))
623 (guard (c ((nix-protocol-error? c)
624 ;; There's no specific error message to check for.
625 #t))
626 (build-derivations %store (list drv))
627 #f)))
628
629 (test-assert "derivation #:disallowed-references, ok"
630 (let ((drv (derivation %store "disallowed" %bash
631 '("-c" "echo hello > $out")
632 #:inputs `((,%bash))
633 #:disallowed-references '("out"))))
634 (build-derivations %store (list drv))))
635
636 (test-assert "derivation #:disallowed-references, not ok"
637 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
638 (drv (derivation %store "disdisallowed" %bash
639 `("-c" ,(string-append "echo " txt "> $out"))
640 #:inputs `((,%bash) (,txt))
641 #:disallowed-references (list txt))))
642 (guard (c ((nix-protocol-error? c)
643 ;; There's no specific error message to check for.
644 #t))
645 (build-derivations %store (list drv))
646 #f)))
647
648 ;; Here we should get the value of $NIX_STATE_DIR that the daemon sees, which
649 ;; is a unique value for each test process; this value is the same as the one
650 ;; we see in the process executing this file since it is set by 'test-env'.
651 (test-equal "derivation #:leaked-env-vars"
652 (getenv "NIX_STATE_DIR")
653 (let* ((value (getenv "NIX_STATE_DIR"))
654 (drv (derivation %store "leaked-env-vars" %bash
655 '("-c" "echo -n $NIX_STATE_DIR > $out")
656 #:hash (sha256 (string->utf8 value))
657 #:hash-algo 'sha256
658 #:inputs `((,%bash))
659 #:leaked-env-vars '("NIX_STATE_DIR"))))
660 (and (build-derivations %store (list drv))
661 (call-with-input-file (derivation->output-path drv)
662 get-string-all))))
663
664 \f
665 (define %coreutils
666 (false-if-exception
667 (and (network-reachable?)
668 (package-derivation %store %bootstrap-coreutils&co))))
669
670 (test-skip (if %coreutils 0 1))
671
672 (test-assert "build derivation with coreutils"
673 (let* ((builder
674 (add-text-to-store %store "build-with-coreutils.sh"
675 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
676 '()))
677 (drv
678 (derivation %store "foo"
679 %bash `(,builder)
680 #:env-vars `(("PATH" .
681 ,(string-append
682 (derivation->output-path %coreutils)
683 "/bin")))
684 #:inputs `((,builder)
685 (,%coreutils))))
686 (succeeded?
687 (build-derivations %store (list drv))))
688 (and succeeded?
689 (let ((p (derivation->output-path drv)))
690 (and (valid-path? %store p)
691 (file-exists? (string-append p "/good")))))))
692
693 (test-skip (if (%guile-for-build) 0 8))
694
695 (test-equal "build-expression->derivation and invalid module name"
696 '(file-search-error "guix/module/that/does/not/exist.scm")
697 (guard (c ((file-search-error? c)
698 (list 'file-search-error
699 (file-search-error-file-name c))))
700 (build-expression->derivation %store "foo" #t
701 #:modules '((guix module that
702 does not exist)))))
703
704 (test-assert "build-expression->derivation and derivation-prerequisites"
705 (let ((drv (build-expression->derivation %store "fail" #f)))
706 (any (match-lambda
707 (($ <derivation-input> path)
708 (string=? path (derivation-file-name (%guile-for-build)))))
709 (derivation-prerequisites drv))))
710
711 (test-assert "derivation-prerequisites and valid-derivation-input?"
712 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
713 (b (build-expression->derivation %store "b" `(list ,(random-text))))
714 (c (build-expression->derivation %store "c" `(mkdir %output)
715 #:inputs `(("a" ,a) ("b" ,b)))))
716 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
717 ;; be removed by tests/guix-gc.sh.)
718 (build-derivations %store
719 (list a (package-derivation %store %bootstrap-guile)))
720
721 (match (derivation-prerequisites c
722 (cut valid-derivation-input? %store
723 <>))
724 ((($ <derivation-input> file ("out")))
725 (string=? file (derivation-file-name b)))
726 (x
727 (pk 'fail x #f)))))
728
729 (test-assert "build-expression->derivation without inputs"
730 (let* ((builder '(begin
731 (mkdir %output)
732 (call-with-output-file (string-append %output "/test")
733 (lambda (p)
734 (display '(hello guix) p)))))
735 (drv (build-expression->derivation %store "goo" builder))
736 (succeeded? (build-derivations %store (list drv))))
737 (and succeeded?
738 (let ((p (derivation->output-path drv)))
739 (equal? '(hello guix)
740 (call-with-input-file (string-append p "/test") read))))))
741
742 (test-assert "build-expression->derivation and max-silent-time"
743 (let* ((store (let ((s (open-connection)))
744 (set-build-options s #:max-silent-time 1)
745 s))
746 (builder '(begin (sleep 100) (mkdir %output) #t))
747 (drv (build-expression->derivation store "silent" builder))
748 (out-path (derivation->output-path drv)))
749 (guard (c ((nix-protocol-error? c)
750 (and (string-contains (nix-protocol-error-message c)
751 "failed")
752 (not (valid-path? store out-path)))))
753 (build-derivations store (list drv))
754 #f)))
755
756 (test-assert "build-expression->derivation and timeout"
757 (let* ((store (let ((s (open-connection)))
758 (set-build-options s #:timeout 1)
759 s))
760 (builder '(begin (sleep 100) (mkdir %output) #t))
761 (drv (build-expression->derivation store "slow" builder))
762 (out-path (derivation->output-path drv)))
763 (guard (c ((nix-protocol-error? c)
764 (and (string-contains (nix-protocol-error-message c)
765 "failed")
766 (not (valid-path? store out-path)))))
767 (build-derivations store (list drv))
768 #f)))
769
770 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
771 (let ((drv (build-expression->derivation %store "fail" #f)))
772 ;; The only direct dependency is (%guile-for-build) and it's already
773 ;; built.
774 (null? (derivation-prerequisites-to-build %store drv))))
775
776 (test-assert "derivation-prerequisites-to-build when outputs already present"
777 (let* ((builder '(begin (mkdir %output) #t))
778 (input-drv (build-expression->derivation %store "input" builder))
779 (input-path (derivation-output-path
780 (assoc-ref (derivation-outputs input-drv)
781 "out")))
782 (drv (build-expression->derivation %store "something" builder
783 #:inputs
784 `(("i" ,input-drv))))
785 (output (derivation->output-path drv)))
786 ;; Make sure these things are not already built.
787 (when (valid-path? %store input-path)
788 (delete-paths %store (list input-path)))
789 (when (valid-path? %store output)
790 (delete-paths %store (list output)))
791
792 (and (equal? (map derivation-input-path
793 (derivation-prerequisites-to-build %store drv))
794 (list (derivation-file-name input-drv)))
795
796 ;; Build DRV and delete its input.
797 (build-derivations %store (list drv))
798 (delete-paths %store (list input-path))
799 (not (valid-path? %store input-path))
800
801 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
802 ;; prerequisite to build because DRV itself is already built.
803 (null? (derivation-prerequisites-to-build %store drv)))))
804
805 (test-assert "derivation-prerequisites-to-build and substitutes"
806 (let* ((store (open-connection))
807 (drv (build-expression->derivation store "prereq-subst"
808 (random 1000)))
809 (output (derivation->output-path drv)))
810
811 ;; Make sure substitutes are usable.
812 (set-build-options store #:use-substitutes? #t
813 #:substitute-urls (%test-substitute-urls))
814
815 (with-derivation-narinfo drv
816 (let-values (((build download)
817 (derivation-prerequisites-to-build store drv))
818 ((build* download*)
819 (derivation-prerequisites-to-build store drv
820 #:substitutable?
821 (const #f))))
822 (and (null? build)
823 (equal? download (list output))
824 (null? download*)
825 (null? build*))))))
826
827 (test-assert "derivation-prerequisites-to-build and substitutes, non-substitutable build"
828 (let* ((store (open-connection))
829 (drv (build-expression->derivation store "prereq-no-subst"
830 (random 1000)
831 #:substitutable? #f))
832 (output (derivation->output-path drv)))
833
834 ;; Make sure substitutes are usable.
835 (set-build-options store #:use-substitutes? #t
836 #:substitute-urls (%test-substitute-urls))
837
838 (with-derivation-narinfo drv
839 (let-values (((build download)
840 (derivation-prerequisites-to-build store drv)))
841 ;; Despite being available as a substitute, DRV will be built locally
842 ;; due to #:substitutable? #f.
843 (and (null? download)
844 (match build
845 (((? derivation-input? input))
846 (string=? (derivation-input-path input)
847 (derivation-file-name drv)))))))))
848
849 (test-assert "derivation-prerequisites-to-build and substitutes, local build"
850 (with-store store
851 (let* ((drv (build-expression->derivation store "prereq-subst-local"
852 (random 1000)
853 #:local-build? #t))
854 (output (derivation->output-path drv)))
855
856 ;; Make sure substitutes are usable.
857 (set-build-options store #:use-substitutes? #t
858 #:substitute-urls (%test-substitute-urls))
859
860 (with-derivation-narinfo drv
861 (let-values (((build download)
862 (derivation-prerequisites-to-build store drv)))
863 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
864 ;; must be able to substitute DRV's output.
865 ;; See <http://bugs.gnu.org/18747>.
866 (and (null? build)
867 (match download
868 (((? string? item))
869 (string=? item (derivation->output-path drv))))))))))
870
871 (test-assert "derivation-prerequisites-to-build in 'check' mode"
872 (with-store store
873 (let* ((dep (build-expression->derivation store "dep"
874 `(begin ,(random-text)
875 (mkdir %output))))
876 (drv (build-expression->derivation store "to-check"
877 '(mkdir %output)
878 #:inputs `(("dep" ,dep)))))
879 (build-derivations store (list drv))
880 (delete-paths store (list (derivation->output-path dep)))
881
882 ;; In 'check' mode, DEP must be rebuilt.
883 (and (null? (derivation-prerequisites-to-build store drv))
884 (match (derivation-prerequisites-to-build store drv
885 #:mode (build-mode
886 check))
887 ((input)
888 (string=? (derivation-input-path input)
889 (derivation-file-name dep))))))))
890
891 (test-assert "substitution-oracle and #:substitute? #f"
892 (with-store store
893 (let* ((dep (build-expression->derivation store "dep"
894 `(begin ,(random-text)
895 (mkdir %output))))
896 (drv (build-expression->derivation store "not-subst"
897 `(begin ,(random-text)
898 (mkdir %output))
899 #:substitutable? #f
900 #:inputs `(("dep" ,dep))))
901 (query #f))
902 (define (record-substitutable-path-query store paths)
903 (when query
904 (error "already called!" query))
905 (set! query paths)
906 '())
907
908 (mock ((guix store) substitutable-paths
909 record-substitutable-path-query)
910
911 (let ((pred (substitution-oracle store (list drv))))
912 (pred (derivation->output-path drv))))
913
914 ;; Make sure the oracle didn't try to get substitute info for DRV since
915 ;; DRV is mark as non-substitutable. Assume that GUILE-FOR-BUILD is
916 ;; already in store and thus not part of QUERY.
917 (equal? (pk 'query query)
918 (list (derivation->output-path dep))))))
919
920 (test-assert "build-expression->derivation with expression returning #f"
921 (let* ((builder '(begin
922 (mkdir %output)
923 #f)) ; fail!
924 (drv (build-expression->derivation %store "fail" builder))
925 (out-path (derivation->output-path drv)))
926 (guard (c ((nix-protocol-error? c)
927 ;; Note that the output path may exist at this point, but it
928 ;; is invalid.
929 (and (string-match "build .* failed"
930 (nix-protocol-error-message c))
931 (not (valid-path? %store out-path)))))
932 (build-derivations %store (list drv))
933 #f)))
934
935 (test-assert "build-expression->derivation with two outputs"
936 (let* ((builder '(begin
937 (call-with-output-file (assoc-ref %outputs "out")
938 (lambda (p)
939 (display '(hello) p)))
940 (call-with-output-file (assoc-ref %outputs "second")
941 (lambda (p)
942 (display '(world) p)))))
943 (drv (build-expression->derivation %store "double" builder
944 #:outputs '("out"
945 "second")))
946 (succeeded? (build-derivations %store (list drv))))
947 (and succeeded?
948 (let ((one (derivation->output-path drv))
949 (two (derivation->output-path drv "second")))
950 (and (equal? '(hello) (call-with-input-file one read))
951 (equal? '(world) (call-with-input-file two read)))))))
952
953 (test-skip (if %coreutils 0 1))
954 (test-assert "build-expression->derivation with one input"
955 (let* ((builder '(call-with-output-file %output
956 (lambda (p)
957 (let ((cu (assoc-ref %build-inputs "cu")))
958 (close 1)
959 (dup2 (port->fdes p) 1)
960 (execl (string-append cu "/bin/uname")
961 "uname" "-a")))))
962 (drv (build-expression->derivation %store "uname" builder
963 #:inputs
964 `(("cu" ,%coreutils))))
965 (succeeded? (build-derivations %store (list drv))))
966 (and succeeded?
967 (let ((p (derivation->output-path drv)))
968 (string-contains (call-with-input-file p read-line) "GNU")))))
969
970 (test-assert "build-expression->derivation with modules"
971 (let* ((builder `(begin
972 (use-modules (guix build utils))
973 (let ((out (assoc-ref %outputs "out")))
974 (mkdir-p (string-append out "/guile/guix/nix"))
975 #t)))
976 (drv (build-expression->derivation %store "test-with-modules"
977 builder
978 #:modules
979 '((guix build utils)))))
980 (and (build-derivations %store (list drv))
981 (let* ((p (derivation->output-path drv))
982 (s (stat (string-append p "/guile/guix/nix"))))
983 (eq? (stat:type s) 'directory)))))
984
985 (test-assert "build-expression->derivation: same fixed-output path"
986 (let* ((builder1 '(call-with-output-file %output
987 (lambda (p)
988 (write "hello" p))))
989 (builder2 '(call-with-output-file (pk 'difference-here! %output)
990 (lambda (p)
991 (write "hello" p))))
992 (hash (sha256 (string->utf8 "hello")))
993 (input1 (build-expression->derivation %store "fixed" builder1
994 #:hash hash
995 #:hash-algo 'sha256))
996 (input2 (build-expression->derivation %store "fixed" builder2
997 #:hash hash
998 #:hash-algo 'sha256))
999 (succeeded? (build-derivations %store (list input1 input2))))
1000 (and succeeded?
1001 (not (string=? (derivation-file-name input1)
1002 (derivation-file-name input2)))
1003 (string=? (derivation->output-path input1)
1004 (derivation->output-path input2)))))
1005
1006 (test-assert "build-expression->derivation with a fixed-output input"
1007 (let* ((builder1 '(call-with-output-file %output
1008 (lambda (p)
1009 (write "hello" p))))
1010 (builder2 '(call-with-output-file (pk 'difference-here! %output)
1011 (lambda (p)
1012 (write "hello" p))))
1013 (hash (sha256 (string->utf8 "hello")))
1014 (input1 (build-expression->derivation %store "fixed" builder1
1015 #:hash hash
1016 #:hash-algo 'sha256))
1017 (input2 (build-expression->derivation %store "fixed" builder2
1018 #:hash hash
1019 #:hash-algo 'sha256))
1020 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
1021 (call-with-output-file %output
1022 (lambda (out)
1023 (format #f "My input is ~a.~%" input)))))
1024 (final1 (build-expression->derivation %store "final" builder3
1025 #:inputs
1026 `(("input" ,input1))))
1027 (final2 (build-expression->derivation %store "final" builder3
1028 #:inputs
1029 `(("input" ,input2)))))
1030 (and (string=? (derivation->output-path final1)
1031 (derivation->output-path final2))
1032 (string=? (derivation->output-path final1)
1033 (derivation-path->output-path
1034 (derivation-file-name final1)))
1035 (build-derivations %store (list final1 final2)))))
1036
1037 (test-assert "build-expression->derivation produces recursive fixed-output"
1038 (let* ((builder '(begin
1039 (use-modules (srfi srfi-26))
1040 (mkdir %output)
1041 (chdir %output)
1042 (call-with-output-file "exe"
1043 (cut display "executable" <>))
1044 (chmod "exe" #o777)
1045 (symlink "exe" "symlink")
1046 (mkdir "subdir")))
1047 (drv (build-expression->derivation %store "fixed-rec" builder
1048 #:hash-algo 'sha256
1049 #:hash (base32
1050 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
1051 #:recursive? #t)))
1052 (and (build-derivations %store (list drv))
1053 (let* ((dir (derivation->output-path drv))
1054 (exe (string-append dir "/exe"))
1055 (link (string-append dir "/symlink"))
1056 (subdir (string-append dir "/subdir")))
1057 (and (executable-file? exe)
1058 (string=? "executable"
1059 (call-with-input-file exe get-string-all))
1060 (string=? "exe" (readlink link))
1061 (file-is-directory? subdir))))))
1062
1063 (test-assert "build-expression->derivation uses recursive fixed-output"
1064 (let* ((builder '(call-with-output-file %output
1065 (lambda (port)
1066 (display "hello" port))))
1067 (fixed (build-expression->derivation %store "small-fixed-rec"
1068 builder
1069 #:hash-algo 'sha256
1070 #:hash (base32
1071 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
1072 #:recursive? #t))
1073 (in (derivation->output-path fixed))
1074 (builder `(begin
1075 (mkdir %output)
1076 (chdir %output)
1077 (symlink ,in "symlink")))
1078 (drv (build-expression->derivation %store "fixed-rec-user"
1079 builder
1080 #:inputs `(("fixed" ,fixed)))))
1081 (and (build-derivations %store (list drv))
1082 (let ((out (derivation->output-path drv)))
1083 (string=? (readlink (string-append out "/symlink")) in)))))
1084
1085 (test-assert "build-expression->derivation with #:references-graphs"
1086 (let* ((input (add-text-to-store %store "foo" "hello"
1087 (list %bash %mkdir)))
1088 (builder '(copy-file "input" %output))
1089 (drv (build-expression->derivation %store "references-graphs"
1090 builder
1091 #:references-graphs
1092 `(("input" . ,input))))
1093 (out (derivation->output-path drv)))
1094 (define (deps path . deps)
1095 (let ((count (length deps)))
1096 (string-append path "\n\n" (number->string count) "\n"
1097 (string-join (sort deps string<?) "\n")
1098 (if (zero? count) "" "\n"))))
1099
1100 (and (build-derivations %store (list drv))
1101 (equal? (call-with-input-file out get-string-all)
1102 (string-concatenate
1103 (map cdr
1104 (sort (map (lambda (p d)
1105 (cons p (apply deps p d)))
1106 (list input %bash %mkdir)
1107 (list (list %bash %mkdir)
1108 '() '()))
1109 (lambda (x y)
1110 (match x
1111 ((p1 . _)
1112 (match y
1113 ((p2 . _)
1114 (string<? p1 p2)))))))))))))
1115
1116 (test-equal "map-derivation"
1117 "hello"
1118 (let* ((joke (package-derivation %store guile-1.8))
1119 (good (package-derivation %store %bootstrap-guile))
1120 (drv1 (build-expression->derivation %store "original-drv1"
1121 #f ; systematically fail
1122 #:guile-for-build joke))
1123 (drv2 (build-expression->derivation %store "original-drv2"
1124 '(call-with-output-file %output
1125 (lambda (p)
1126 (display "hello" p)))))
1127 (drv3 (build-expression->derivation %store "drv-to-remap"
1128 '(let ((in (assoc-ref
1129 %build-inputs "in")))
1130 (copy-file in %output))
1131 #:inputs `(("in" ,drv1))
1132 #:guile-for-build joke))
1133 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
1134 (,joke . ,good))))
1135 (out (derivation->output-path drv4)))
1136 (and (build-derivations %store (list (pk 'remapped drv4)))
1137 (call-with-input-file out get-string-all))))
1138
1139 (test-equal "map-derivation, sources"
1140 "hello"
1141 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
1142 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
1143 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
1144 (drv1 (derivation %store "drv-to-remap"
1145
1146 ;; XXX: This wouldn't work in practice, but if
1147 ;; we append "/bin/bash" then we can't replace
1148 ;; it with the bootstrap bash, which is a
1149 ;; single file.
1150 (derivation->output-path bash-full)
1151
1152 `("-e" ,script1)
1153 #:inputs `((,bash-full) (,script1))))
1154 (drv2 (map-derivation %store drv1
1155 `((,bash-full . ,%bash)
1156 (,script1 . ,script2))))
1157 (out (derivation->output-path drv2)))
1158 (and (build-derivations %store (list (pk 'remapped* drv2)))
1159 (call-with-input-file out get-string-all))))
1160
1161 (test-end)
1162
1163 ;; Local Variables:
1164 ;; eval: (put 'with-http-server 'scheme-indent-function 2)
1165 ;; End: