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