1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (test-store)
20 #:use-module (guix tests)
21 #:use-module (guix config)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix monads)
25 #:use-module ((gcrypt hash) #:prefix gcrypt:)
26 #:use-module ((gcrypt pk-crypto) #:prefix gcrypt:)
27 #:use-module (guix pki)
28 #:use-module (guix base32)
29 #:use-module (guix packages)
30 #:use-module (guix derivations)
31 #:use-module (guix serialization)
32 #:use-module (guix build utils)
33 #:use-module (guix gexp)
34 #:use-module (gnu packages)
35 #:use-module (gnu packages bootstrap)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 regex)
38 #:use-module (rnrs bytevectors)
39 #:use-module (rnrs io ports)
40 #:use-module (web uri)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-11)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-64))
47 ;; Test the (guix store) module.
50 (open-connection-for-tests))
53 (or (getenv "SHELL") (getenv "CONFIG_SHELL") "/bin/sh"))
58 (test-assert "open-connection with file:// URI"
59 (let ((store (open-connection (string-append "file://"
60 (%daemon-socket-uri)))))
61 (and (add-text-to-store store "foo" "bar")
63 (close-connection store)
66 (test-equal "connection handshake error"
68 (let ((port (%make-void-port "rw")))
69 (guard (c ((store-connection-error? c)
70 (and (eq? port (store-connection-error-file c))
71 (store-connection-error-code c))))
72 (open-connection #f #:port port)
75 (test-equal "store-path-hash-part"
76 "283gqy39v3g9dxjy26rynl0zls82fmcg"
78 (string-append (%store-prefix)
79 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
81 (test-equal "store-path-hash-part #f"
84 (string-append (%store-prefix)
85 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
87 (test-equal "store-path-package-name"
89 (store-path-package-name
90 (string-append (%store-prefix)
91 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
93 (test-equal "store-path-package-name #f"
95 (store-path-package-name
96 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
98 (test-assert "direct-store-path?"
99 (and (direct-store-path?
100 (string-append (%store-prefix)
101 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
102 (not (direct-store-path?
105 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
106 (not (direct-store-path? (%store-prefix)))))
108 (test-skip (if %store 0 15))
110 (test-equal "profiles/per-user exists and is not writable"
112 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
114 (test-equal "profiles/per-user/$USER exists"
115 (list (getuid) #o755)
116 (let ((s (stat (string-append %state-directory "/profiles/per-user/"
117 (passwd:name (getpwuid (getuid)))))))
118 (list (stat:uid s) (stat:perms s))))
120 (test-equal "add-to-store"
121 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256")
122 (let* ((file (search-path %load-path "guix.scm"))
123 (content (call-with-input-file file get-bytevector-all)))
124 (map (lambda (hash-algo)
125 (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
126 (and (direct-store-path? file)
127 (bytevector=? (call-with-input-file file get-bytevector-all)
130 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
132 (test-equal "add-data-to-store"
134 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
137 (test-assert "valid-path? live"
138 (let ((p (add-text-to-store %store "hello" "hello, world")))
139 (valid-path? %store p)))
141 (test-assert "valid-path? false"
142 (not (valid-path? %store
143 (string-append (%store-prefix) "/"
144 (make-string 32 #\e) "-foobar"))))
146 (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
151 (add-text-to-store s "foo" "bar")
155 (test-assert "valid-path? error"
157 (guard (c ((store-protocol-error? c) #t))
158 (valid-path? s "foo")
161 (test-assert "valid-path? recovery"
162 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
163 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
165 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
168 (let-syntax ((true-if-error (syntax-rules ()
170 (guard (c ((store-protocol-error? c) #t))
172 (and (true-if-error (valid-path? s "foo"))
173 (true-if-error (valid-path? s "bar"))
174 (true-if-error (valid-path? s "baz"))
175 (true-if-error (valid-path? s "chbouib"))
176 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
178 (test-assert "hash-part->path"
179 (let ((p (add-text-to-store %store "hello" "hello, world")))
180 (equal? (hash-part->path %store (store-path-hash-part p))
183 (test-assert "dead-paths"
184 (let ((p (add-text-to-store %store "random-text" (random-text))))
185 (->bool (member p (dead-paths %store)))))
187 ;; FIXME: Find a test for `live-paths'.
189 ;; (test-assert "temporary root is in live-paths"
190 ;; (let* ((p1 (add-text-to-store %store "random-text"
191 ;; (random-text) '()))
192 ;; (b (add-text-to-store %store "link-builder"
193 ;; (format #f "echo ~a > $out" p1)
195 ;; (d1 (derivation %store "link"
196 ;; "/bin/sh" `("-e" ,b)
197 ;; #:inputs `((,b) (,p1))))
198 ;; (p2 (derivation->output-path d1)))
199 ;; (and (add-temp-root %store p2)
200 ;; (build-derivations %store (list d1))
201 ;; (valid-path? %store p1)
202 ;; (member (pk p2) (live-paths %store)))))
204 (test-assert "add-indirect-root and find-roots"
205 (call-with-temporary-directory
207 (let* ((item (add-text-to-store %store "something" (random-text)))
208 (root (string-append directory "/gc-root")))
210 (add-indirect-root %store root)
211 (let ((result (member (cons root item) (find-roots %store))))
215 (test-assert "permanent root"
216 (let* ((p (with-store store
217 (let ((p (add-text-to-store store "random-text"
219 (add-permanent-root p)
220 (add-permanent-root p) ; should not throw
222 (and (member p (live-paths %store))
224 (remove-permanent-root p)
225 (->bool (member p (dead-paths %store)))))))
227 (test-assert "dead path can be explicitly collected"
228 (let ((p (add-text-to-store %store "random-text"
230 (let-values (((paths freed) (delete-paths %store (list p))))
231 (and (equal? paths (list p))
232 ;; XXX: On some file systems (notably Btrfs), freed
233 ;; may return 0. See <https://bugs.gnu.org/29363>.
235 (not (file-exists? p))))))
237 (test-assert "add-text-to-store/add-to-store vs. delete-paths"
238 ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
239 ;; store item without noticing that it is no longer valid.
241 (let* ((text (random-text))
242 (file (search-path %load-path "guix.scm"))
243 (path1 (add-text-to-store store "delete-me" text))
244 (path2 (add-to-store store "delete-me" #t "sha256" file))
245 (deleted (delete-paths store (list path1 path2))))
246 (and (string=? path1 (add-text-to-store store "delete-me" text))
247 (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
248 (lset= string=? deleted (list path1 path2))
249 (valid-path? store path1)
250 (valid-path? store path2)
252 (file-exists? path2)))))
254 (test-equal "add-file-tree-to-store"
257 ("./bar" directory #t)
258 ("./foo" directory #t)
259 ("./foo/a" regular "file a")
260 ("./foo/b" symlink "a")
261 ("./foo/c" directory #t)
262 ("./foo/c/p" regular "file p")
263 ("./foo/c/q" directory #t)
264 ("./foo/c/q/x" regular
265 ,(string-append "#!" %shell "\nexit 42"))
266 ("./foo/c/q/y" symlink "..")
267 ("./foo/c/q/z" directory #t))
268 (let* ((tree `("file-tree" directory
270 ("a" regular (data "file a"))
273 ("p" regular (data ,(string->utf8 "file p")))
276 (data ,(string-append "#!" %shell "\nexit 42")))
280 (result (add-file-tree-to-store %store tree)))
281 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
282 (with-directory-excursion result
284 (let ((type (stat:type (lstat file))))
287 ((or 'regular 'executable)
288 (call-with-input-file file
290 ('symlink (readlink file))
292 (find-files "." #:directories? #t))))))
294 (test-equal "add-file-tree-to-store, flat"
296 (let* ((tree `("flat-file" regular (data "Hello, world!")))
297 (result (add-file-tree-to-store %store tree)))
298 (and (file-exists? result)
299 (call-with-input-file result get-string-all))))
301 (test-assert "references"
302 (let* ((t1 (add-text-to-store %store "random1"
304 (t2 (add-text-to-store %store "random2"
305 (random-text) (list t1))))
306 (and (equal? (list t1) (references %store t2))
307 (equal? (list t2) (referrers %store t1))
308 (null? (references %store t1))
309 (null? (referrers %store t2)))))
311 (test-equal "substitutable-path-info when substitutes are turned off"
314 (set-build-options s #:use-substitutes? #f)
315 (let* ((b (add-to-store s "bash" #t "sha256"
316 (search-bootstrap-binary "bash"
318 (d (derivation s "the-thing" b '("--version")
320 (o (derivation->output-path d)))
321 (with-derivation-narinfo d
322 (substitutable-path-info s (list o))))))
324 (test-equal "substitutable-paths when substitutes are turned off"
327 (set-build-options s #:use-substitutes? #f)
328 (let* ((b (add-to-store s "bash" #t "sha256"
329 (search-bootstrap-binary "bash"
331 (d (derivation s "the-thing" b '("--version")
333 (o (derivation->output-path d)))
334 (with-derivation-narinfo d
335 (substitutable-paths s (list o))))))
337 (test-assert "requisites"
338 (let* ((t1 (add-text-to-store %store "random1"
340 (t2 (add-text-to-store %store "random2"
341 (random-text) (list t1)))
342 (t3 (add-text-to-store %store "random3"
343 (random-text) (list t2)))
344 (t4 (add-text-to-store %store "random4"
345 (random-text) (list t1 t3))))
347 (and (= (length x) (length y))
350 (and (same? (requisites %store (list t1)) (list t1))
351 (same? (requisites %store (list t2)) (list t1 t2))
352 (same? (requisites %store (list t3)) (list t1 t2 t3))
353 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
354 (same? (requisites %store (list t1 t2 t3 t4))
355 (list t1 t2 t3 t4)))))
357 (test-assert "derivers"
358 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
359 (s (add-to-store %store "bash" #t "sha256"
360 (search-bootstrap-binary "bash"
362 (d (derivation %store "the-thing"
364 #:env-vars `(("foo" . ,(random-text)))
365 #:inputs `((,b) (,s))))
366 (o (derivation->output-path d)))
367 (and (build-derivations %store (list d))
368 (equal? (query-derivation-outputs %store (derivation-file-name d))
370 (equal? (valid-derivers %store o)
371 (list (derivation-file-name d))))))
373 (test-equal "with-build-handler"
375 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
376 (s (add-to-store %store "bash" #t "sha256"
377 (search-bootstrap-binary "bash"
379 (d1 (derivation %store "the-thing"
381 #:env-vars `(("foo" . ,(random-text)))
382 #:sources (list b s)))
383 (d2 (derivation %store "the-thing"
385 #:env-vars `(("foo" . ,(random-text))
387 #:sources (list b s)))
388 (o1 (derivation->output-path d1))
389 (o2 (derivation->output-path d2)))
392 (lambda (continue store things mode)
395 (set! counter (+ 1 counter))
396 (if (string=? drv (derivation-file-name d1))
398 (and (string=? drv (derivation-file-name d2))
401 (build-derivations %store (list d1))
402 (build-derivations %store (list d2))
405 (test-equal "with-build-handler + with-store"
407 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
408 ;; even though 'with-build-handler' is outside the dynamic extent of
410 (with-build-handler (lambda (continue store things mode)
413 (and (string-suffix? "thingie.drv" drv)
415 (store-connection-socket store)))
418 (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
419 (s (add-to-store store "bash" #t "sha256"
420 (search-bootstrap-binary "bash"
422 (d (derivation store "thingie"
424 #:env-vars `(("foo" . ,(random-text)))
425 #:sources (list b s))))
426 (build-derivations store (list d))
428 ;; Here STORE's socket should still be open.
429 (and (valid-path? store (derivation->output-path d))
432 (test-assert "map/accumulate-builds"
433 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
434 (s (add-to-store %store "bash" #t "sha256"
435 (search-bootstrap-binary "bash"
437 (d1 (derivation %store "the-thing"
439 #:env-vars `(("foo" . ,(random-text)))
440 #:sources (list b s)))
441 (d2 (derivation %store "the-thing"
443 #:env-vars `(("foo" . ,(random-text))
445 #:sources (list b s))))
446 (with-build-handler (lambda (continue store things mode)
447 (equal? (map derivation-file-name (list d1 d2))
449 (map/accumulate-builds %store
451 (build-derivations %store (list drv))
452 (add-to-store %store "content-addressed"
454 (derivation->output-path drv)))
457 (test-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
460 ;; Make sure that, when the cutoff is reached, 'map/accumulate-builds' still
461 ;; returns the right result and calls the build handler by batches.
462 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
463 (s (add-to-store %store "bash" #t "sha256"
464 (search-bootstrap-binary "bash"
467 (derivation %store (string-append "the-thing-"
470 #:env-vars `(("foo" . ,(random-text)))
472 #:properties `((n . ,i))))
476 (with-build-handler (lambda (continue store things mode)
477 (set! calls (cons things calls))
479 (map/accumulate-builds %store
481 (build-derivations %store (list d))
482 (assq-ref (derivation-properties d) 'n))
486 (match (reverse calls)
487 (((batch1 ...) (batch2 ...) (batch3 ...))
488 (and (equal? (map derivation-file-name (take d 8)) batch1)
489 (equal? (map derivation-file-name (take (drop d 8) 8)) batch2)
490 (equal? (map derivation-file-name (drop d 16)) batch3)
493 (test-equal "map/accumulate-builds and different store"
494 '(d2) ;see <https://issues.guix.gnu.org/46756>
495 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
496 (s (add-to-store %store "bash" #t "sha256"
497 (search-bootstrap-binary "bash"
499 (d1 (derivation %store "first"
501 #:env-vars `(("foo" . ,(random-text)))
502 #:sources (list b s)))
503 (d2 (derivation %store "second"
505 #:env-vars `(("foo" . ,(random-text))
507 #:sources (list b s))))
508 (with-store alternate-store
509 (with-build-handler (lambda (continue store things mode)
510 ;; If this handler is called, it means that
511 ;; 'map/accumulate-builds' triggered a build,
512 ;; which it shouldn't since the inner
513 ;; 'build-derivations' call is for another store.
515 (map/accumulate-builds %store
517 (build-derivations alternate-store (list d2))
521 (test-assert "mapm/accumulate-builds"
522 (let* ((d1 (run-with-store %store
523 (gexp->derivation "foo" #~(mkdir #$output))))
524 (d2 (run-with-store %store
525 (gexp->derivation "bar" #~(mkdir #$output)))))
526 (with-build-handler (lambda (continue store things mode)
527 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
529 (run-with-store %store
530 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
532 (test-equal "mapm/accumulate-builds, %current-target-system"
533 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
534 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
536 (run-with-store %store
537 (mlet %store-monad ((lst1 (mapm %store-monad
539 (current-target-system))
541 (lst2 (mapm/accumulate-builds
543 (current-target-system))
545 (return (list lst1 lst2)))
547 #:target "i586-pc-gnu"))
549 (test-assert "topologically-sorted, one item"
550 (let* ((a (add-text-to-store %store "a" "a"))
551 (b (add-text-to-store %store "b" "b" (list a)))
552 (c (add-text-to-store %store "c" "c" (list b)))
553 (d (add-text-to-store %store "d" "d" (list c)))
554 (s (topologically-sorted %store (list d))))
555 (equal? s (list a b c d))))
557 (test-assert "topologically-sorted, several items"
558 (let* ((a (add-text-to-store %store "a" "a"))
559 (b (add-text-to-store %store "b" "b" (list a)))
560 (c (add-text-to-store %store "c" "c" (list b)))
561 (d (add-text-to-store %store "d" "d" (list c)))
562 (s1 (topologically-sorted %store (list d a c b)))
563 (s2 (topologically-sorted %store (list b d c a b d))))
564 (equal? s1 s2 (list a b c d))))
566 (test-assert "topologically-sorted, more difficult"
567 (let* ((a (add-text-to-store %store "a" "a"))
568 (b (add-text-to-store %store "b" "b" (list a)))
569 (c (add-text-to-store %store "c" "c" (list b)))
570 (d (add-text-to-store %store "d" "d" (list c)))
571 (w (add-text-to-store %store "w" "w"))
572 (x (add-text-to-store %store "x" "x" (list w)))
573 (y (add-text-to-store %store "y" "y" (list x d)))
574 (s1 (topologically-sorted %store (list y)))
575 (s2 (topologically-sorted %store (list c y)))
576 (s3 (topologically-sorted %store (cons y (references %store y)))))
577 ;; The order in which 'references' returns the references of Y is
578 ;; unspecified, so accommodate.
579 (let* ((x-then-d? (equal? (references %store y) (list x d))))
583 (list a b c d w x y)))
587 (list a b c d w x y)))
588 (lset= string=? s1 s3)))))
590 (test-assert "current-build-output-port, UTF-8"
591 ;; Are UTF-8 strings in the build log properly interpreted?
593 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
594 (call-with-output-string
596 (parameterize ((current-build-output-port port))
597 (let* ((s "Here’s a Greek letter: λ.")
598 (d (build-expression->derivation
599 %store "foo" `(display ,s)
601 (package-derivation %store %bootstrap-guile
602 (%current-system)))))
603 (guard (c ((store-protocol-error? c) #t))
604 (build-derivations %store (list d))))))))
605 "Here’s a Greek letter: λ."))
607 (test-assert "current-build-output-port, UTF-8 + garbage"
608 ;; What about a mixture of UTF-8 + garbage?
610 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
611 (call-with-output-string
613 (parameterize ((current-build-output-port port))
614 (let ((d (build-expression->derivation
617 (use-modules (rnrs io ports))
618 (display "garbage: ")
619 (put-bytevector (current-output-port) #vu8(128))
620 (display "lambda: λ\n"))
622 (package-derivation %store %bootstrap-guile))))
623 (guard (c ((store-protocol-error? c) #t))
624 (build-derivations %store (list d))))))))
625 "garbage: �lambda: λ"))
627 (test-assert "log-file, derivation"
628 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
629 (s (add-to-store %store "bash" #t "sha256"
630 (search-bootstrap-binary "bash"
632 (d (derivation %store "the-thing"
634 #:env-vars `(("foo" . ,(random-text)))
635 #:inputs `((,b) (,s)))))
636 (and (build-derivations %store (list d))
637 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
639 (test-assert "log-file, output file name"
640 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
641 (s (add-to-store %store "bash" #t "sha256"
642 (search-bootstrap-binary "bash"
644 (d (derivation %store "the-thing"
646 #:env-vars `(("foo" . ,(random-text)))
647 #:inputs `((,b) (,s))))
648 (o (derivation->output-path d)))
649 (and (build-derivations %store (list d))
650 (file-exists? (pk (log-file %store o)))
651 (string=? (log-file %store (derivation-file-name d))
652 (log-file %store o)))))
654 (test-assert "no substitutes"
656 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
657 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
658 (o (map derivation->output-path (list d1 d2))))
659 (set-build-options s #:use-substitutes? #f)
660 (and (not (has-substitutes? s (derivation-file-name d1)))
661 (not (has-substitutes? s (derivation-file-name d2)))
662 (null? (substitutable-paths s o))
663 (null? (substitutable-path-info s o))))))
665 (test-assert "build-things with output path"
667 (let* ((c (random-text)) ;contents of the output
668 (d (build-expression->derivation
670 `(call-with-output-file %output
674 (package-derivation s %bootstrap-guile (%current-system))))
675 (o (derivation->output-path d)))
676 (set-build-options s #:use-substitutes? #f)
678 ;; Pass 'build-things' the output file name, O. However, since there
679 ;; are no substitutes for O, it will just do nothing.
680 (build-things s (list o))
681 (not (valid-path? s o)))))
683 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
685 (test-assert "substitute query"
687 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
688 (o (derivation->output-path d)))
689 ;; Create fake substituter data, to be read by 'guix substitute'.
690 (with-derivation-narinfo d
691 ;; Remove entry from the local cache.
693 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
694 "/guix/substitute")))
696 ;; Make sure 'guix substitute' correctly communicates the above
698 (set-build-options s #:use-substitutes? #t
699 #:substitute-urls (%test-substitute-urls))
700 (and (has-substitutes? s o)
701 (equal? (list o) (substitutable-paths s (list o)))
702 (match (pk 'spi (substitutable-path-info s (list o)))
703 (((? substitutable? s))
704 (and (string=? (substitutable-deriver s)
705 (derivation-file-name d))
706 (null? (substitutable-references s))
707 (equal? (substitutable-nar-size s) 1234)))))))))
709 (test-assert "substitute query, alternating URLs"
710 (let* ((d (with-store s
711 (package-derivation s %bootstrap-guile (%current-system))))
712 (o (derivation->output-path d)))
713 (with-derivation-narinfo d
714 ;; Remove entry from the local cache.
716 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
717 "/guix/substitute")))
719 ;; Note: We reconnect to the daemon to force a new instance of 'guix
720 ;; substitute' to be used; otherwise the #:substitute-urls of
721 ;; 'set-build-options' would have no effect.
723 (and (with-store s ;the right substitute URL
724 (set-build-options s #:use-substitutes? #t
725 #:substitute-urls (%test-substitute-urls))
726 (has-substitutes? s o))
727 (with-store s ;the wrong one
728 (set-build-options s #:use-substitutes? #t
729 #:substitute-urls (list
730 "http://does-not-exist"))
731 (not (has-substitutes? s o)))
732 (with-store s ;the right one again
733 (set-build-options s #:use-substitutes? #t
734 #:substitute-urls (%test-substitute-urls))
735 (has-substitutes? s o))
736 (with-store s ;empty list of URLs
737 (set-build-options s #:use-substitutes? #t
738 #:substitute-urls '())
739 (not (has-substitutes? s o)))))))
741 (test-assert "substitute"
743 (let* ((c (random-text)) ; contents of the output
744 (d (build-expression->derivation
746 `(call-with-output-file %output
748 (exit 1) ; would actually fail
751 (package-derivation s %bootstrap-guile (%current-system))))
752 (o (derivation->output-path d)))
753 (with-derivation-substitute d c
754 (set-build-options s #:use-substitutes? #t
755 #:substitute-urls (%test-substitute-urls))
756 (and (has-substitutes? s o)
757 (build-derivations s (list d))
759 (equal? c (call-with-input-file o get-string-all)))))))
761 (test-assert "substitute, deduplication"
763 ;; Note: C must be longer than %DEDUPLICATION-MINIMUM-SIZE.
764 (let* ((c (string-concatenate
765 (make-list 200 (random-text)))) ; contents of the output
766 (g (package-derivation s %bootstrap-guile))
767 (d1 (build-expression->derivation s "substitute-me"
769 #:guile-for-build g))
770 (d2 (build-expression->derivation s "build-me"
771 `(call-with-output-file %output
774 #:guile-for-build g))
775 (o1 (derivation->output-path d1))
776 (o2 (derivation->output-path d2)))
777 (with-derivation-substitute d1 c
778 (set-build-options s #:use-substitutes? #t
779 #:substitute-urls (%test-substitute-urls))
780 (and (has-substitutes? s o1)
781 (build-derivations s (list d2)) ;build
782 (build-derivations s (list d1)) ;substitute
784 (equal? c (call-with-input-file o1 get-string-all))
785 (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
787 (test-assert "substitute + build-things with output path"
789 (let* ((c (random-text)) ;contents of the output
790 (d (build-expression->derivation
792 `(call-with-output-file %output
794 (exit 1) ;would actually fail
797 (package-derivation s %bootstrap-guile (%current-system))))
798 (o (derivation->output-path d)))
799 (with-derivation-substitute d c
800 (set-build-options s #:use-substitutes? #t
801 #:substitute-urls (%test-substitute-urls))
802 (and (has-substitutes? s o)
803 (build-things s (list o)) ;give the output path
806 (equal? c (call-with-input-file o get-string-all)))))))
808 (test-assert "substitute + build-things with specific output"
810 (let* ((c (random-text)) ;contents of the output
811 (d (build-expression->derivation
812 s "substitute-me" `(begin ,c (exit 1)) ;would fail
813 #:outputs '("out" "one" "two")
815 (package-derivation s %bootstrap-guile (%current-system))))
816 (o (derivation->output-path d)))
817 (with-derivation-substitute d c
818 (set-build-options s #:use-substitutes? #t
819 #:substitute-urls (%test-substitute-urls))
820 (and (has-substitutes? s o)
822 ;; Ask for nothing but the "out" output of D.
823 (build-things s `((,(derivation-file-name d) . "out")))
827 (equal? c (call-with-input-file o get-string-all)))))))
829 (test-assert "substitute, corrupt output hash"
830 ;; Tweak the substituter into installing a substitute whose hash doesn't
831 ;; match the one announced in the narinfo. The daemon must notice this and
834 (let* ((c "hello, world") ; contents of the output
835 (d (build-expression->derivation
836 s "corrupt-substitute"
839 (package-derivation s %bootstrap-guile (%current-system))))
840 (o (derivation->output-path d)))
841 (with-derivation-substitute d c
842 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
844 ;; Make sure we use 'guix substitute'.
846 #:use-substitutes? #t
848 #:substitute-urls (%test-substitute-urls))
849 (and (has-substitutes? s o)
850 (guard (c ((store-protocol-error? c)
851 ;; XXX: the daemon writes "hash mismatch in downloaded
852 ;; path", but the actual error returned to the client
853 ;; doesn't mention that.
855 (not (zero? (store-protocol-error-status c)))))
856 (build-derivations s (list d))
859 (test-assert "substitute, corrupt output hash, build trace"
860 ;; Likewise, and check the build trace.
862 (let* ((c "hello, world") ; contents of the output
863 (d (build-expression->derivation
864 s "corrupt-substitute"
867 (package-derivation s %bootstrap-guile (%current-system))))
868 (o (derivation->output-path d)))
869 ;; Make sure we use 'guix substitute'.
871 #:print-build-trace #t
872 #:use-substitutes? #t
874 #:substitute-urls (%test-substitute-urls))
876 (with-derivation-substitute d c
877 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
880 (call-with-output-string
882 (parameterize ((current-build-output-port port))
883 (guard (c ((store-protocol-error? c) #t))
884 (build-derivations s (list d))
888 (let-values (((port get-hash)
889 (gcrypt:open-hash-port
890 (gcrypt:hash-algorithm gcrypt:sha256))))
891 (write-file-tree "foo" port
894 (values 'regular (string-length c)))
897 (open-input-string c)))
899 (bytevector->nix-base32-string (get-hash))))
901 (define expected-hash
902 (bytevector->nix-base32-string (make-bytevector 32 0)))
905 (string-append "@ hash-mismatch " o " sha256 "
906 expected-hash " " actual-hash "\n"))
909 (string-append "@ substituter-failed " o))
911 (and (string-contains output mismatch)
912 (string-contains output failure))))))
914 (test-assert "substitute --fallback"
916 (let* ((t (random-text)) ; contents of the output
917 (d (build-expression->derivation
918 s "substitute-me-not"
919 `(call-with-output-file %output
923 (package-derivation s %bootstrap-guile (%current-system))))
924 (o (derivation->output-path d)))
925 ;; Create fake substituter data, to be read by 'guix substitute'.
926 (with-derivation-narinfo d
927 ;; Make sure we use 'guix substitute'.
928 (set-build-options s #:use-substitutes? #t
929 #:substitute-urls (%test-substitute-urls))
930 (and (has-substitutes? s o)
931 (guard (c ((store-protocol-error? c)
932 ;; The substituter failed as expected. Now make
933 ;; sure that #:fallback? #t works correctly.
935 #:use-substitutes? #t
937 (%test-substitute-urls)
939 (and (build-derivations s (list d))
940 (equal? t (call-with-input-file o
943 (build-derivations s (list d))
946 (test-equal "substitute query and large size"
947 (+ 100 (expt 2 63)) ;<https://issues.guix.gnu.org/51983>
949 (let* ((size (+ 100 (expt 2 63))) ;does not fit in signed 'long long'
950 (item (string-append (%store-prefix)
951 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size")))
952 ;; Create fake substituter data, to be read by 'guix substitute'.
953 (call-with-output-file (string-append (%substitute-directory)
954 "/" (store-path-hash-part item)
957 (format port "StorePath: ~a
958 URL: http://example.org
961 NarHash: sha256:0fj9vhblff2997pi7qjj7lhmy7wzhnjwmkm2hmq6gr4fzmg10s0w
963 System: x86_64-linux~%"
966 ;; Remove entry from the local cache.
968 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
969 "/guix/substitute")))
971 ;; Make sure 'guix substitute' correctly communicates the above
973 (set-build-options s #:use-substitutes? #t
974 #:substitute-urls (%test-substitute-urls))
975 (match (pk 'spi (substitutable-path-info s (list item)))
976 (((? substitutable? s))
977 (and (equal? (substitutable-path s) item)
978 (substitutable-nar-size s)))))))
980 (test-equal "substitute and large size"
981 (+ 100 (expt 2 31)) ;<https://issues.guix.gnu.org/46212>
983 (let* ((size (+ 100 (expt 2 31))) ;does not fit in signed 'int'
984 (item (string-append (%store-prefix)
985 "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size-"
987 (nar (string-append (%substitute-directory) "/nar")))
988 ;; Create a dummy nar to allow for substitution.
989 (call-with-output-file nar
991 (write-file-tree (store-path-package-name item) port
992 #:file-type+size (lambda _
993 (values 'regular 12))
994 #:file-port (lambda _
995 (open-input-string "Hello world.")))))
997 ;; Create fake substituter data, to be read by 'guix substitute'.
998 (call-with-output-file (string-append (%substitute-directory)
999 "/" (store-path-hash-part item)
1002 (format port "StorePath: ~a
1008 System: x86_64-linux~%"
1010 (bytevector->nix-base32-string (gcrypt:file-sha256 nar)))))
1012 ;; Remove entry from the local cache.
1014 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
1015 "/guix/substitute")))
1017 ;; Make sure 'guix substitute' correctly communicates the above
1019 (set-build-options s #:use-substitutes? #t
1020 #:substitute-urls (%test-substitute-urls))
1021 (ensure-path s item)
1022 (path-info-nar-size (query-path-info s item)))))
1024 (test-assert "export/import several paths"
1025 (let* ((texts (unfold (cut >= <> 10)
1026 (lambda _ (random-text))
1029 (files (map (cut add-text-to-store %store "text" <>) texts))
1030 (dump (call-with-bytevector-output-port
1031 (cut export-paths %store files <>))))
1032 (delete-paths %store files)
1033 (and (every (negate file-exists?) files)
1034 (let* ((source (open-bytevector-input-port dump))
1035 (imported (import-paths %store source)))
1036 (and (equal? imported files)
1037 (every file-exists? files)
1040 (call-with-input-file file
1044 (test-assert "export/import paths, ensure topological order"
1045 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1046 (file1 (add-text-to-store %store "foo" (random-text)
1048 (file2 (add-text-to-store %store "bar" (random-text)
1050 (files (list file1 file2))
1051 (dump1 (call-with-bytevector-output-port
1052 (cute export-paths %store (list file1 file2) <>)))
1053 (dump2 (call-with-bytevector-output-port
1054 (cute export-paths %store (list file2 file1) <>))))
1055 (delete-paths %store files)
1056 (and (every (negate file-exists?) files)
1057 (bytevector=? dump1 dump2)
1058 (let* ((source (open-bytevector-input-port dump1))
1059 (imported (import-paths %store source)))
1060 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
1061 (and (equal? imported (list file1 file2))
1062 (every file-exists? files)
1063 (equal? (list file0) (references %store file1))
1064 (equal? (list file1) (references %store file2)))))))
1066 (test-assert "export/import incomplete"
1067 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1068 (file1 (add-text-to-store %store "foo" (random-text)
1070 (file2 (add-text-to-store %store "bar" (random-text)
1072 (dump (call-with-bytevector-output-port
1073 (cute export-paths %store (list file2) <>))))
1074 (delete-paths %store (list file0 file1 file2))
1075 (guard (c ((store-protocol-error? c)
1076 (and (not (zero? (store-protocol-error-status c)))
1077 (string-contains (store-protocol-error-message c)
1079 ;; Here we get an exception because DUMP does not include FILE0 and
1080 ;; FILE1, which are dependencies of FILE2.
1081 (import-paths %store (open-bytevector-input-port dump)))))
1083 (test-assert "export/import recursive"
1084 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
1085 (file1 (add-text-to-store %store "foo" (random-text)
1087 (file2 (add-text-to-store %store "bar" (random-text)
1089 (dump (call-with-bytevector-output-port
1090 (cute export-paths %store (list file2) <>
1092 (delete-paths %store (list file0 file1 file2))
1093 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
1094 (and (equal? imported (list file0 file1 file2))
1095 (every file-exists? (list file0 file1 file2))
1096 (equal? (list file0) (references %store file1))
1097 (equal? (list file1) (references %store file2))))))
1099 (test-assert "write-file & export-path yield the same result"
1100 ;; Here we compare 'write-file' and the daemon's own implementation.
1101 ;; 'write-file' is the reference because we know it sorts file
1102 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
1103 ;; currently happen to be sorted as a side-effect of some unrelated
1104 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
1106 (run-with-store %store
1107 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
1108 (out1 -> (derivation->output-path drv1))
1109 (data -> (unfold (cut >= <> 26)
1111 (random-bytevector 128))
1115 (use-modules (rnrs io ports) (srfi srfi-1))
1121 (+ i (char->integer #\a)))))
1123 (define (touch file data)
1124 (call-with-output-file file
1126 (put-bytevector port data))))
1131 ;; The files must be different so they have
1132 ;; different inode numbers, and the inode
1133 ;; order must differ from the lexicographic
1136 (append (drop letters 10)
1140 (drv2 (gexp->derivation "bunch" build))
1141 (out2 -> (derivation->output-path drv2))
1142 (item-info -> (store-lift query-path-info)))
1143 (mbegin %store-monad
1144 (built-derivations (list drv1 drv2))
1146 (lambda (item result)
1148 (let-values (((port get) (gcrypt:open-sha256-port)))
1149 (write-file item port)
1153 ;; 'query-path-info' returns a hash produced by using the
1154 ;; daemon's C++ 'dump' function, which is the implementation
1156 (>>= (item-info item)
1160 (bytevector=? (path-info-hash info) ref-hash))))))
1163 #:guile-for-build (%guile-for-build)))
1166 (test-assert "import not signed"
1167 (let* ((text (random-text))
1168 (file (add-file-tree-to-store %store
1170 ("text" regular (data ,text))
1171 ("link" symlink "text"))))
1172 (dump (call-with-bytevector-output-port
1174 (write-int 1 port) ;start
1176 (write-file file port) ;contents
1177 (write-int #x4558494e port) ;%export-magic
1178 (write-string file port) ;store item
1179 (write-string-list '() port) ;references
1180 (write-string "" port) ;deriver
1181 (write-int 0 port) ;not signed
1183 (write-int 0 port))))) ;done
1185 ;; Ensure 'import-paths' raises an exception.
1186 (guard (c ((store-protocol-error? c)
1187 (and (not (zero? (store-protocol-error-status c)))
1188 (string-contains (store-protocol-error-message c)
1189 "lacks a signature"))))
1190 (let* ((source (open-bytevector-input-port dump))
1191 (imported (import-paths %store source)))
1192 (pk 'unsigned-imported imported)
1195 (test-assert "import signed by unauthorized key"
1196 (let* ((text (random-text))
1197 (file (add-file-tree-to-store %store
1199 ("text" regular (data ,text))
1200 ("link" symlink "text"))))
1201 (key (gcrypt:generate-key
1202 (gcrypt:string->canonical-sexp
1203 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1204 (dump (call-with-bytevector-output-port
1206 (write-int 1 port) ;start
1208 (write-file file port) ;contents
1209 (write-int #x4558494e port) ;%export-magic
1210 (write-string file port) ;store item
1211 (write-string-list '() port) ;references
1212 (write-string "" port) ;deriver
1213 (write-int 1 port) ;signed
1214 (write-string (gcrypt:canonical-sexp->string
1216 (gcrypt:bytevector->hash-data
1217 (gcrypt:sha256 #vu8(0 1 2))
1219 (gcrypt:find-sexp-token key 'private-key)
1220 (gcrypt:find-sexp-token key 'public-key)))
1223 (write-int 0 port))))) ;done
1225 ;; Ensure 'import-paths' raises an exception.
1226 (guard (c ((store-protocol-error? c)
1227 (and (not (zero? (store-protocol-error-status c)))
1228 (string-contains (store-protocol-error-message c)
1229 "unauthorized public key"))))
1230 (let* ((source (open-bytevector-input-port dump))
1231 (imported (import-paths %store source)))
1232 (pk 'unauthorized-imported imported)
1235 (test-assert "import corrupt path"
1236 (let* ((text (random-text))
1237 (file (add-text-to-store %store "text" text))
1238 (dump (call-with-bytevector-output-port
1239 (cut export-paths %store (list file) <>))))
1240 (delete-paths %store (list file))
1242 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1243 ;; the file contents in DUMP, regardless of the store prefix.
1245 (byte (bytevector-u8-ref dump index)))
1246 (bytevector-u8-set! dump index (logxor #xff byte)))
1248 (and (not (file-exists? file))
1249 (guard (c ((store-protocol-error? c)
1251 (and (not (zero? (store-protocol-error-status c)))
1252 (string-contains (store-protocol-error-message c)
1254 (let* ((source (open-bytevector-input-port dump))
1255 (imported (import-paths %store source)))
1256 (pk 'corrupt-imported imported)
1259 (test-assert "verify-store"
1260 (let* ((text (random-text))
1261 (file1 (add-text-to-store %store "foo" text))
1262 (file2 (add-text-to-store %store "bar" (random-text)
1264 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1267 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1269 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1270 ;; without actually creating the file. ;
1271 (call-with-output-file file1
1273 (display text port)))
1274 (pk 'verify3 (verify-store %store)))))) ;OK again
1276 (test-assert "verify-store + check-contents"
1277 ;; XXX: This test is I/O intensive.
1279 (let* ((text (random-text))
1280 (drv (build-expression->derivation
1282 `(let ((out (assoc-ref %outputs "out")))
1283 (call-with-output-file out
1285 (display ,text port)))
1288 (package-derivation s %bootstrap-guile (%current-system))))
1289 (file (derivation->output-path drv)))
1290 (with-derivation-substitute drv text
1291 (and (build-derivations s (list drv))
1292 (verify-store s #:check-contents? #t) ;should be OK
1295 (call-with-output-file file
1297 (display "corrupt!" port)))
1300 ;; Make sure the corruption is detected. We don't test repairing
1301 ;; because only "trusted" users are allowed to do it, but we
1302 ;; don't expose that notion of trusted users that nix-daemon
1303 ;; supports because it seems dubious and redundant with what the
1304 ;; OS provides (in Nix "trusted" users have additional
1305 ;; privileges, such as overriding the set of substitute URLs, but
1306 ;; we instead want to allow anyone to modify them, provided
1307 ;; substitutes are signed by a root-approved key.)
1308 (not (verify-store s #:check-contents? #t))
1310 ;; Delete the corrupt item to leave the store in a clean state.
1311 (delete-paths s (list file)))))))
1313 (test-assert "build-things, check mode"
1315 (call-with-temporary-output-file
1316 (lambda (entropy entropy-port)
1317 (write (random-text) entropy-port)
1318 (force-output entropy-port)
1319 (let* ((drv (build-expression->derivation
1320 store "non-deterministic"
1322 (use-modules (rnrs io ports))
1323 (let ((out (assoc-ref %outputs "out")))
1324 (call-with-output-file out
1326 ;; Rely on the fact that tests do not use the
1327 ;; chroot, and thus ENTROPY is readable.
1328 (display (call-with-input-file ,entropy
1333 (package-derivation store %bootstrap-guile (%current-system))))
1334 (file (derivation->output-path drv)))
1335 (and (build-things store (list (derivation-file-name drv)))
1337 (write (random-text) entropy-port)
1338 (force-output entropy-port)
1339 (guard (c ((store-protocol-error? c)
1340 (pk 'determinism-exception c)
1341 (and (not (zero? (store-protocol-error-status c)))
1342 (string-contains (store-protocol-error-message c)
1344 ;; This one will produce a different result. Since we're in
1345 ;; 'check' mode, this must fail.
1346 (build-things store (list (derivation-file-name drv))
1350 (test-assert "build-succeeded trace in check mode"
1352 (call-with-output-string
1354 (let ((d (build-expression->derivation
1355 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1357 (package-derivation %store %bootstrap-guile))))
1358 (build-derivations %store (list d))
1359 (parameterize ((current-build-output-port port))
1360 (build-derivations %store (list d) (build-mode check))))))
1361 "@ build-succeeded"))
1363 (test-assert "build multiple times"
1365 ;; Ask to build twice.
1366 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1368 (call-with-temporary-output-file
1369 (lambda (entropy entropy-port)
1370 (write (random-text) entropy-port)
1371 (force-output entropy-port)
1372 (let* ((drv (build-expression->derivation
1373 store "non-deterministic"
1375 (use-modules (rnrs io ports))
1376 (let ((out (assoc-ref %outputs "out")))
1377 (call-with-output-file out
1379 ;; Rely on the fact that tests do not use the
1380 ;; chroot, and thus ENTROPY is accessible.
1381 (display (call-with-input-file ,entropy
1384 (call-with-output-file ,entropy
1386 (write 'foobar port)))))
1389 (package-derivation store %bootstrap-guile (%current-system))))
1390 (file (derivation->output-path drv)))
1391 (guard (c ((store-protocol-error? c)
1392 (pk 'multiple-build c)
1393 (and (not (zero? (store-protocol-error-status c)))
1394 (string-contains (store-protocol-error-message c)
1396 ;; This one will produce a different result on the second run.
1397 (current-build-output-port (current-error-port))
1398 (build-things store (list (derivation-file-name drv)))
1401 (test-equal "store-lower"
1403 (let* ((add (store-lower text-file))
1404 (file (add %store "foo" "Lowered.")))
1405 (call-with-input-file file get-string-all)))
1407 (test-equal "current-system"
1409 (parameterize ((%current-system "frob"))
1410 (run-with-store %store
1411 (mbegin %store-monad
1412 (set-current-system "bar")
1416 (test-assert "query-path-info"
1417 (let* ((ref (add-text-to-store %store "ref" "foo"))
1418 (item (add-text-to-store %store "item" "bar" (list ref)))
1419 (info (query-path-info %store item)))
1420 (and (equal? (path-info-references info) (list ref))
1421 (equal? (path-info-hash info)
1424 (call-with-output-string (cut write-file item <>))))))))
1426 (test-assert "path-info-deriver"
1427 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1428 (s (add-to-store %store "bash" #t "sha256"
1429 (search-bootstrap-binary "bash"
1430 (%current-system))))
1431 (d (derivation %store "the-thing"
1433 #:env-vars `(("foo" . ,(random-text)))
1434 #:inputs `((,b) (,s))))
1435 (o (derivation->output-path d)))
1436 (and (build-derivations %store (list d))
1437 (not (path-info-deriver (query-path-info %store b)))
1438 (string=? (derivation-file-name d)
1439 (path-info-deriver (query-path-info %store o))))))
1441 (test-equal "build-cores"
1444 (let* ((build (add-text-to-store store "build.sh"
1445 "echo $NIX_BUILD_CORES > $out"))
1446 (bash (add-to-store store "bash" #t "sha256"
1447 (search-bootstrap-binary "bash"
1448 (%current-system))))
1449 (drv1 (derivation store "the-thing" bash
1451 #:inputs `((,bash) (,build))
1452 #:env-vars `(("x" . ,(random-text)))))
1453 (drv2 (derivation store "the-thing" bash
1455 #:inputs `((,bash) (,build))
1456 #:env-vars `(("x" . ,(random-text))))))
1457 (and (build-derivations store (list drv1))
1459 (set-build-options store #:build-cores 42)
1460 (build-derivations store (list drv2)))
1461 (list (call-with-input-file (derivation->output-path drv1)
1463 (call-with-input-file (derivation->output-path drv2)
1466 (test-equal "multiplexed-build-output"
1467 '("Hello from first." "Hello from second.")
1469 (let* ((build (add-text-to-store store "build.sh"
1470 "echo Hello from $NAME.; echo > $out"))
1471 (bash (add-to-store store "bash" #t "sha256"
1472 (search-bootstrap-binary "bash"
1473 (%current-system))))
1474 (drv1 (derivation store "one" bash
1476 #:inputs `((,bash) (,build))
1477 #:env-vars `(("NAME" . "first")
1478 ("x" . ,(random-text)))))
1479 (drv2 (derivation store "two" bash
1481 #:inputs `((,bash) (,build))
1482 #:env-vars `(("NAME" . "second")
1483 ("x" . ,(random-text))))))
1484 (set-build-options store
1485 #:print-build-trace #t
1486 #:multiplexed-build-output? #t
1487 #:max-build-jobs 10)
1488 (let ((port (open-output-string)))
1489 ;; Send the build log to PORT.
1490 (parameterize ((current-build-output-port port))
1491 (build-derivations store (list drv1 drv2)))
1493 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1494 ;; traces that allow us to retrieve each builder's output (we assume
1495 ;; there's exactly one "build-output" trace for each builder, which is
1497 (let* ((log (get-output-string port))
1498 (started (fold-matches
1499 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1502 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1504 (output (fold-matches
1505 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1507 (drv-pid (lambda (name)
1509 (let ((drv (match:substring m 1))
1510 (pid (string->number
1511 (match:substring m 4))))
1512 (and (string-suffix? name drv) pid)))))
1513 (pid-log (lambda (pid)
1515 (let ((n (string->number
1516 (match:substring m 1)))
1517 (len (string->number
1518 (match:substring m 2)))
1519 (str (match:substring m 3)))
1521 (= (string-length str) (- len 1))
1523 (pid1 (any (drv-pid "one.drv") started))
1524 (pid2 (any (drv-pid "two.drv") started)))
1525 (list (any (pid-log pid1) output)
1526 (any (pid-log pid2) output)))))))