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