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