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-assert "mapm/accumulate-builds"
458 (let* ((d1 (run-with-store %store
459 (gexp->derivation "foo" #~(mkdir #$output))))
460 (d2 (run-with-store %store
461 (gexp->derivation "bar" #~(mkdir #$output)))))
462 (with-build-handler (lambda (continue store things mode)
463 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
465 (run-with-store %store
466 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
468 (test-equal "mapm/accumulate-builds, %current-target-system"
469 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
470 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
472 (run-with-store %store
473 (mlet %store-monad ((lst1 (mapm %store-monad
475 (current-target-system))
477 (lst2 (mapm/accumulate-builds
479 (current-target-system))
481 (return (list lst1 lst2)))
483 #:target "i586-pc-gnu"))
485 (test-assert "topologically-sorted, one item"
486 (let* ((a (add-text-to-store %store "a" "a"))
487 (b (add-text-to-store %store "b" "b" (list a)))
488 (c (add-text-to-store %store "c" "c" (list b)))
489 (d (add-text-to-store %store "d" "d" (list c)))
490 (s (topologically-sorted %store (list d))))
491 (equal? s (list a b c d))))
493 (test-assert "topologically-sorted, several items"
494 (let* ((a (add-text-to-store %store "a" "a"))
495 (b (add-text-to-store %store "b" "b" (list a)))
496 (c (add-text-to-store %store "c" "c" (list b)))
497 (d (add-text-to-store %store "d" "d" (list c)))
498 (s1 (topologically-sorted %store (list d a c b)))
499 (s2 (topologically-sorted %store (list b d c a b d))))
500 (equal? s1 s2 (list a b c d))))
502 (test-assert "topologically-sorted, more difficult"
503 (let* ((a (add-text-to-store %store "a" "a"))
504 (b (add-text-to-store %store "b" "b" (list a)))
505 (c (add-text-to-store %store "c" "c" (list b)))
506 (d (add-text-to-store %store "d" "d" (list c)))
507 (w (add-text-to-store %store "w" "w"))
508 (x (add-text-to-store %store "x" "x" (list w)))
509 (y (add-text-to-store %store "y" "y" (list x d)))
510 (s1 (topologically-sorted %store (list y)))
511 (s2 (topologically-sorted %store (list c y)))
512 (s3 (topologically-sorted %store (cons y (references %store y)))))
513 ;; The order in which 'references' returns the references of Y is
514 ;; unspecified, so accommodate.
515 (let* ((x-then-d? (equal? (references %store y) (list x d))))
519 (list a b c d w x y)))
523 (list a b c d w x y)))
524 (lset= string=? s1 s3)))))
526 (test-assert "current-build-output-port, UTF-8"
527 ;; Are UTF-8 strings in the build log properly interpreted?
529 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
530 (call-with-output-string
532 (parameterize ((current-build-output-port port))
533 (let* ((s "Here’s a Greek letter: λ.")
534 (d (build-expression->derivation
535 %store "foo" `(display ,s)
537 (package-derivation %store %bootstrap-guile
538 (%current-system)))))
539 (guard (c ((store-protocol-error? c) #t))
540 (build-derivations %store (list d))))))))
541 "Here’s a Greek letter: λ."))
543 (test-assert "current-build-output-port, UTF-8 + garbage"
544 ;; What about a mixture of UTF-8 + garbage?
546 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
547 (call-with-output-string
549 (parameterize ((current-build-output-port port))
550 (let ((d (build-expression->derivation
553 (use-modules (rnrs io ports))
554 (display "garbage: ")
555 (put-bytevector (current-output-port) #vu8(128))
556 (display "lambda: λ\n"))
558 (package-derivation %store %bootstrap-guile))))
559 (guard (c ((store-protocol-error? c) #t))
560 (build-derivations %store (list d))))))))
561 "garbage: �lambda: λ"))
563 (test-assert "log-file, derivation"
564 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
565 (s (add-to-store %store "bash" #t "sha256"
566 (search-bootstrap-binary "bash"
568 (d (derivation %store "the-thing"
570 #:env-vars `(("foo" . ,(random-text)))
571 #:inputs `((,b) (,s)))))
572 (and (build-derivations %store (list d))
573 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
575 (test-assert "log-file, output file name"
576 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
577 (s (add-to-store %store "bash" #t "sha256"
578 (search-bootstrap-binary "bash"
580 (d (derivation %store "the-thing"
582 #:env-vars `(("foo" . ,(random-text)))
583 #:inputs `((,b) (,s))))
584 (o (derivation->output-path d)))
585 (and (build-derivations %store (list d))
586 (file-exists? (pk (log-file %store o)))
587 (string=? (log-file %store (derivation-file-name d))
588 (log-file %store o)))))
590 (test-assert "no substitutes"
592 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
593 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
594 (o (map derivation->output-path (list d1 d2))))
595 (set-build-options s #:use-substitutes? #f)
596 (and (not (has-substitutes? s (derivation-file-name d1)))
597 (not (has-substitutes? s (derivation-file-name d2)))
598 (null? (substitutable-paths s o))
599 (null? (substitutable-path-info s o))))))
601 (test-assert "build-things with output path"
603 (let* ((c (random-text)) ;contents of the output
604 (d (build-expression->derivation
606 `(call-with-output-file %output
610 (package-derivation s %bootstrap-guile (%current-system))))
611 (o (derivation->output-path d)))
612 (set-build-options s #:use-substitutes? #f)
614 ;; Pass 'build-things' the output file name, O. However, since there
615 ;; are no substitutes for O, it will just do nothing.
616 (build-things s (list o))
617 (not (valid-path? s o)))))
619 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
621 (test-assert "substitute query"
623 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
624 (o (derivation->output-path d)))
625 ;; Create fake substituter data, to be read by 'guix substitute'.
626 (with-derivation-narinfo d
627 ;; Remove entry from the local cache.
629 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
630 "/guix/substitute")))
632 ;; Make sure 'guix substitute' correctly communicates the above
634 (set-build-options s #:use-substitutes? #t
635 #:substitute-urls (%test-substitute-urls))
636 (and (has-substitutes? s o)
637 (equal? (list o) (substitutable-paths s (list o)))
638 (match (pk 'spi (substitutable-path-info s (list o)))
639 (((? substitutable? s))
640 (and (string=? (substitutable-deriver s)
641 (derivation-file-name d))
642 (null? (substitutable-references s))
643 (equal? (substitutable-nar-size s) 1234)))))))))
645 (test-assert "substitute query, alternating URLs"
646 (let* ((d (with-store s
647 (package-derivation s %bootstrap-guile (%current-system))))
648 (o (derivation->output-path d)))
649 (with-derivation-narinfo d
650 ;; Remove entry from the local cache.
652 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
653 "/guix/substitute")))
655 ;; Note: We reconnect to the daemon to force a new instance of 'guix
656 ;; substitute' to be used; otherwise the #:substitute-urls of
657 ;; 'set-build-options' would have no effect.
659 (and (with-store s ;the right substitute URL
660 (set-build-options s #:use-substitutes? #t
661 #:substitute-urls (%test-substitute-urls))
662 (has-substitutes? s o))
663 (with-store s ;the wrong one
664 (set-build-options s #:use-substitutes? #t
665 #:substitute-urls (list
666 "http://does-not-exist"))
667 (not (has-substitutes? s o)))
668 (with-store s ;the right one again
669 (set-build-options s #:use-substitutes? #t
670 #:substitute-urls (%test-substitute-urls))
671 (has-substitutes? s o))
672 (with-store s ;empty list of URLs
673 (set-build-options s #:use-substitutes? #t
674 #:substitute-urls '())
675 (not (has-substitutes? s o)))))))
677 (test-assert "substitute"
679 (let* ((c (random-text)) ; contents of the output
680 (d (build-expression->derivation
682 `(call-with-output-file %output
684 (exit 1) ; would actually fail
687 (package-derivation s %bootstrap-guile (%current-system))))
688 (o (derivation->output-path d)))
689 (with-derivation-substitute d c
690 (set-build-options s #:use-substitutes? #t
691 #:substitute-urls (%test-substitute-urls))
692 (and (has-substitutes? s o)
693 (build-derivations s (list d))
695 (equal? c (call-with-input-file o get-string-all)))))))
697 (test-assert "substitute, deduplication"
699 (let* ((c (random-text)) ; contents of the output
700 (g (package-derivation s %bootstrap-guile))
701 (d1 (build-expression->derivation s "substitute-me"
703 #:guile-for-build g))
704 (d2 (build-expression->derivation s "build-me"
705 `(call-with-output-file %output
708 #:guile-for-build g))
709 (o1 (derivation->output-path d1))
710 (o2 (derivation->output-path d2)))
711 (with-derivation-substitute d1 c
712 (set-build-options s #:use-substitutes? #t
713 #:substitute-urls (%test-substitute-urls))
714 (and (has-substitutes? s o1)
715 (build-derivations s (list d2)) ;build
716 (build-derivations s (list d1)) ;substitute
718 (equal? c (call-with-input-file o1 get-string-all))
719 (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
721 (test-assert "substitute + build-things with output path"
723 (let* ((c (random-text)) ;contents of the output
724 (d (build-expression->derivation
726 `(call-with-output-file %output
728 (exit 1) ;would actually fail
731 (package-derivation s %bootstrap-guile (%current-system))))
732 (o (derivation->output-path d)))
733 (with-derivation-substitute d c
734 (set-build-options s #:use-substitutes? #t
735 #:substitute-urls (%test-substitute-urls))
736 (and (has-substitutes? s o)
737 (build-things s (list o)) ;give the output path
740 (equal? c (call-with-input-file o get-string-all)))))))
742 (test-assert "substitute + build-things with specific output"
744 (let* ((c (random-text)) ;contents of the output
745 (d (build-expression->derivation
746 s "substitute-me" `(begin ,c (exit 1)) ;would fail
747 #:outputs '("out" "one" "two")
749 (package-derivation s %bootstrap-guile (%current-system))))
750 (o (derivation->output-path d)))
751 (with-derivation-substitute d c
752 (set-build-options s #:use-substitutes? #t
753 #:substitute-urls (%test-substitute-urls))
754 (and (has-substitutes? s o)
756 ;; Ask for nothing but the "out" output of D.
757 (build-things s `((,(derivation-file-name d) . "out")))
761 (equal? c (call-with-input-file o get-string-all)))))))
763 (test-assert "substitute, corrupt output hash"
764 ;; Tweak the substituter into installing a substitute whose hash doesn't
765 ;; match the one announced in the narinfo. The daemon must notice this and
768 (let* ((c "hello, world") ; contents of the output
769 (d (build-expression->derivation
770 s "corrupt-substitute"
773 (package-derivation s %bootstrap-guile (%current-system))))
774 (o (derivation->output-path d)))
775 (with-derivation-substitute d c
776 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
778 ;; Make sure we use 'guix substitute'.
780 #:use-substitutes? #t
782 #:substitute-urls (%test-substitute-urls))
783 (and (has-substitutes? s o)
784 (guard (c ((store-protocol-error? c)
785 ;; XXX: the daemon writes "hash mismatch in downloaded
786 ;; path", but the actual error returned to the client
787 ;; doesn't mention that.
789 (not (zero? (store-protocol-error-status c)))))
790 (build-derivations s (list d))
793 (test-assert "substitute, corrupt output hash, build trace"
794 ;; Likewise, and check the build trace.
796 (let* ((c "hello, world") ; contents of the output
797 (d (build-expression->derivation
798 s "corrupt-substitute"
801 (package-derivation s %bootstrap-guile (%current-system))))
802 (o (derivation->output-path d)))
803 ;; Make sure we use 'guix substitute'.
805 #:print-build-trace #t
806 #:use-substitutes? #t
808 #:substitute-urls (%test-substitute-urls))
810 (with-derivation-substitute d c
811 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
814 (call-with-output-string
816 (parameterize ((current-build-output-port port))
817 (guard (c ((store-protocol-error? c) #t))
818 (build-derivations s (list d))
822 (let-values (((port get-hash)
823 (gcrypt:open-hash-port
824 (gcrypt:hash-algorithm gcrypt:sha256))))
825 (write-file-tree "foo" port
828 (values 'regular (string-length c)))
831 (open-input-string c)))
833 (bytevector->nix-base32-string (get-hash))))
835 (define expected-hash
836 (bytevector->nix-base32-string (make-bytevector 32 0)))
839 (string-append "@ hash-mismatch " o " sha256 "
840 expected-hash " " actual-hash "\n"))
843 (string-append "@ substituter-failed " o))
845 (and (string-contains output mismatch)
846 (string-contains output failure))))))
848 (test-assert "substitute --fallback"
850 (let* ((t (random-text)) ; contents of the output
851 (d (build-expression->derivation
852 s "substitute-me-not"
853 `(call-with-output-file %output
857 (package-derivation s %bootstrap-guile (%current-system))))
858 (o (derivation->output-path d)))
859 ;; Create fake substituter data, to be read by 'guix substitute'.
860 (with-derivation-narinfo d
861 ;; Make sure we use 'guix substitute'.
862 (set-build-options s #:use-substitutes? #t
863 #:substitute-urls (%test-substitute-urls))
864 (and (has-substitutes? s o)
865 (guard (c ((store-protocol-error? c)
866 ;; The substituter failed as expected. Now make
867 ;; sure that #:fallback? #t works correctly.
869 #:use-substitutes? #t
871 (%test-substitute-urls)
873 (and (build-derivations s (list d))
874 (equal? t (call-with-input-file o
877 (build-derivations s (list d))
880 (test-assert "export/import several paths"
881 (let* ((texts (unfold (cut >= <> 10)
882 (lambda _ (random-text))
885 (files (map (cut add-text-to-store %store "text" <>) texts))
886 (dump (call-with-bytevector-output-port
887 (cut export-paths %store files <>))))
888 (delete-paths %store files)
889 (and (every (negate file-exists?) files)
890 (let* ((source (open-bytevector-input-port dump))
891 (imported (import-paths %store source)))
892 (and (equal? imported files)
893 (every file-exists? files)
896 (call-with-input-file file
900 (test-assert "export/import paths, ensure topological order"
901 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
902 (file1 (add-text-to-store %store "foo" (random-text)
904 (file2 (add-text-to-store %store "bar" (random-text)
906 (files (list file1 file2))
907 (dump1 (call-with-bytevector-output-port
908 (cute export-paths %store (list file1 file2) <>)))
909 (dump2 (call-with-bytevector-output-port
910 (cute export-paths %store (list file2 file1) <>))))
911 (delete-paths %store files)
912 (and (every (negate file-exists?) files)
913 (bytevector=? dump1 dump2)
914 (let* ((source (open-bytevector-input-port dump1))
915 (imported (import-paths %store source)))
916 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
917 (and (equal? imported (list file1 file2))
918 (every file-exists? files)
919 (equal? (list file0) (references %store file1))
920 (equal? (list file1) (references %store file2)))))))
922 (test-assert "export/import incomplete"
923 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
924 (file1 (add-text-to-store %store "foo" (random-text)
926 (file2 (add-text-to-store %store "bar" (random-text)
928 (dump (call-with-bytevector-output-port
929 (cute export-paths %store (list file2) <>))))
930 (delete-paths %store (list file0 file1 file2))
931 (guard (c ((store-protocol-error? c)
932 (and (not (zero? (store-protocol-error-status c)))
933 (string-contains (store-protocol-error-message c)
935 ;; Here we get an exception because DUMP does not include FILE0 and
936 ;; FILE1, which are dependencies of FILE2.
937 (import-paths %store (open-bytevector-input-port dump)))))
939 (test-assert "export/import recursive"
940 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
941 (file1 (add-text-to-store %store "foo" (random-text)
943 (file2 (add-text-to-store %store "bar" (random-text)
945 (dump (call-with-bytevector-output-port
946 (cute export-paths %store (list file2) <>
948 (delete-paths %store (list file0 file1 file2))
949 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
950 (and (equal? imported (list file0 file1 file2))
951 (every file-exists? (list file0 file1 file2))
952 (equal? (list file0) (references %store file1))
953 (equal? (list file1) (references %store file2))))))
955 (test-assert "write-file & export-path yield the same result"
956 ;; Here we compare 'write-file' and the daemon's own implementation.
957 ;; 'write-file' is the reference because we know it sorts file
958 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
959 ;; currently happen to be sorted as a side-effect of some unrelated
960 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
962 (run-with-store %store
963 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
964 (out1 -> (derivation->output-path drv1))
965 (data -> (unfold (cut >= <> 26)
967 (random-bytevector 128))
971 (use-modules (rnrs io ports) (srfi srfi-1))
977 (+ i (char->integer #\a)))))
979 (define (touch file data)
980 (call-with-output-file file
982 (put-bytevector port data))))
987 ;; The files must be different so they have
988 ;; different inode numbers, and the inode
989 ;; order must differ from the lexicographic
992 (append (drop letters 10)
996 (drv2 (gexp->derivation "bunch" build))
997 (out2 -> (derivation->output-path drv2))
998 (item-info -> (store-lift query-path-info)))
1000 (built-derivations (list drv1 drv2))
1002 (lambda (item result)
1004 (let-values (((port get) (gcrypt:open-sha256-port)))
1005 (write-file item port)
1009 ;; 'query-path-info' returns a hash produced by using the
1010 ;; daemon's C++ 'dump' function, which is the implementation
1012 (>>= (item-info item)
1016 (bytevector=? (path-info-hash info) ref-hash))))))
1019 #:guile-for-build (%guile-for-build)))
1022 (test-assert "import not signed"
1023 (let* ((text (random-text))
1024 (file (add-file-tree-to-store %store
1026 ("text" regular (data ,text))
1027 ("link" symlink "text"))))
1028 (dump (call-with-bytevector-output-port
1030 (write-int 1 port) ;start
1032 (write-file file port) ;contents
1033 (write-int #x4558494e port) ;%export-magic
1034 (write-string file port) ;store item
1035 (write-string-list '() port) ;references
1036 (write-string "" port) ;deriver
1037 (write-int 0 port) ;not signed
1039 (write-int 0 port))))) ;done
1041 ;; Ensure 'import-paths' raises an exception.
1042 (guard (c ((store-protocol-error? c)
1043 (and (not (zero? (store-protocol-error-status c)))
1044 (string-contains (store-protocol-error-message c)
1045 "lacks a signature"))))
1046 (let* ((source (open-bytevector-input-port dump))
1047 (imported (import-paths %store source)))
1048 (pk 'unsigned-imported imported)
1051 (test-assert "import signed by unauthorized key"
1052 (let* ((text (random-text))
1053 (file (add-file-tree-to-store %store
1055 ("text" regular (data ,text))
1056 ("link" symlink "text"))))
1057 (key (gcrypt:generate-key
1058 (gcrypt:string->canonical-sexp
1059 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1060 (dump (call-with-bytevector-output-port
1062 (write-int 1 port) ;start
1064 (write-file file port) ;contents
1065 (write-int #x4558494e port) ;%export-magic
1066 (write-string file port) ;store item
1067 (write-string-list '() port) ;references
1068 (write-string "" port) ;deriver
1069 (write-int 1 port) ;signed
1070 (write-string (gcrypt:canonical-sexp->string
1072 (gcrypt:bytevector->hash-data
1073 (gcrypt:sha256 #vu8(0 1 2))
1075 (gcrypt:find-sexp-token key 'private-key)
1076 (gcrypt:find-sexp-token key 'public-key)))
1079 (write-int 0 port))))) ;done
1081 ;; Ensure 'import-paths' raises an exception.
1082 (guard (c ((store-protocol-error? c)
1083 (and (not (zero? (store-protocol-error-status c)))
1084 (string-contains (store-protocol-error-message c)
1085 "unauthorized public key"))))
1086 (let* ((source (open-bytevector-input-port dump))
1087 (imported (import-paths %store source)))
1088 (pk 'unauthorized-imported imported)
1091 (test-assert "import corrupt path"
1092 (let* ((text (random-text))
1093 (file (add-text-to-store %store "text" text))
1094 (dump (call-with-bytevector-output-port
1095 (cut export-paths %store (list file) <>))))
1096 (delete-paths %store (list file))
1098 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1099 ;; the file contents in DUMP, regardless of the store prefix.
1101 (byte (bytevector-u8-ref dump index)))
1102 (bytevector-u8-set! dump index (logxor #xff byte)))
1104 (and (not (file-exists? file))
1105 (guard (c ((store-protocol-error? c)
1107 (and (not (zero? (store-protocol-error-status c)))
1108 (string-contains (store-protocol-error-message c)
1110 (let* ((source (open-bytevector-input-port dump))
1111 (imported (import-paths %store source)))
1112 (pk 'corrupt-imported imported)
1115 (test-assert "verify-store"
1116 (let* ((text (random-text))
1117 (file1 (add-text-to-store %store "foo" text))
1118 (file2 (add-text-to-store %store "bar" (random-text)
1120 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1123 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1125 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1126 ;; without actually creating the file. ;
1127 (call-with-output-file file1
1129 (display text port)))
1130 (pk 'verify3 (verify-store %store)))))) ;OK again
1132 (test-assert "verify-store + check-contents"
1133 ;; XXX: This test is I/O intensive.
1135 (let* ((text (random-text))
1136 (drv (build-expression->derivation
1138 `(let ((out (assoc-ref %outputs "out")))
1139 (call-with-output-file out
1141 (display ,text port)))
1144 (package-derivation s %bootstrap-guile (%current-system))))
1145 (file (derivation->output-path drv)))
1146 (with-derivation-substitute drv text
1147 (and (build-derivations s (list drv))
1148 (verify-store s #:check-contents? #t) ;should be OK
1151 (call-with-output-file file
1153 (display "corrupt!" port)))
1156 ;; Make sure the corruption is detected. We don't test repairing
1157 ;; because only "trusted" users are allowed to do it, but we
1158 ;; don't expose that notion of trusted users that nix-daemon
1159 ;; supports because it seems dubious and redundant with what the
1160 ;; OS provides (in Nix "trusted" users have additional
1161 ;; privileges, such as overriding the set of substitute URLs, but
1162 ;; we instead want to allow anyone to modify them, provided
1163 ;; substitutes are signed by a root-approved key.)
1164 (not (verify-store s #:check-contents? #t))
1166 ;; Delete the corrupt item to leave the store in a clean state.
1167 (delete-paths s (list file)))))))
1169 (test-assert "build-things, check mode"
1171 (call-with-temporary-output-file
1172 (lambda (entropy entropy-port)
1173 (write (random-text) entropy-port)
1174 (force-output entropy-port)
1175 (let* ((drv (build-expression->derivation
1176 store "non-deterministic"
1178 (use-modules (rnrs io ports))
1179 (let ((out (assoc-ref %outputs "out")))
1180 (call-with-output-file out
1182 ;; Rely on the fact that tests do not use the
1183 ;; chroot, and thus ENTROPY is readable.
1184 (display (call-with-input-file ,entropy
1189 (package-derivation store %bootstrap-guile (%current-system))))
1190 (file (derivation->output-path drv)))
1191 (and (build-things store (list (derivation-file-name drv)))
1193 (write (random-text) entropy-port)
1194 (force-output entropy-port)
1195 (guard (c ((store-protocol-error? c)
1196 (pk 'determinism-exception c)
1197 (and (not (zero? (store-protocol-error-status c)))
1198 (string-contains (store-protocol-error-message c)
1200 ;; This one will produce a different result. Since we're in
1201 ;; 'check' mode, this must fail.
1202 (build-things store (list (derivation-file-name drv))
1206 (test-assert "build-succeeded trace in check mode"
1208 (call-with-output-string
1210 (let ((d (build-expression->derivation
1211 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1213 (package-derivation %store %bootstrap-guile))))
1214 (build-derivations %store (list d))
1215 (parameterize ((current-build-output-port port))
1216 (build-derivations %store (list d) (build-mode check))))))
1217 "@ build-succeeded"))
1219 (test-assert "build multiple times"
1221 ;; Ask to build twice.
1222 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1224 (call-with-temporary-output-file
1225 (lambda (entropy entropy-port)
1226 (write (random-text) entropy-port)
1227 (force-output entropy-port)
1228 (let* ((drv (build-expression->derivation
1229 store "non-deterministic"
1231 (use-modules (rnrs io ports))
1232 (let ((out (assoc-ref %outputs "out")))
1233 (call-with-output-file out
1235 ;; Rely on the fact that tests do not use the
1236 ;; chroot, and thus ENTROPY is accessible.
1237 (display (call-with-input-file ,entropy
1240 (call-with-output-file ,entropy
1242 (write 'foobar port)))))
1245 (package-derivation store %bootstrap-guile (%current-system))))
1246 (file (derivation->output-path drv)))
1247 (guard (c ((store-protocol-error? c)
1248 (pk 'multiple-build c)
1249 (and (not (zero? (store-protocol-error-status c)))
1250 (string-contains (store-protocol-error-message c)
1252 ;; This one will produce a different result on the second run.
1253 (current-build-output-port (current-error-port))
1254 (build-things store (list (derivation-file-name drv)))
1257 (test-equal "store-lower"
1259 (let* ((add (store-lower text-file))
1260 (file (add %store "foo" "Lowered.")))
1261 (call-with-input-file file get-string-all)))
1263 (test-equal "current-system"
1265 (parameterize ((%current-system "frob"))
1266 (run-with-store %store
1267 (mbegin %store-monad
1268 (set-current-system "bar")
1272 (test-assert "query-path-info"
1273 (let* ((ref (add-text-to-store %store "ref" "foo"))
1274 (item (add-text-to-store %store "item" "bar" (list ref)))
1275 (info (query-path-info %store item)))
1276 (and (equal? (path-info-references info) (list ref))
1277 (equal? (path-info-hash info)
1280 (call-with-output-string (cut write-file item <>))))))))
1282 (test-assert "path-info-deriver"
1283 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1284 (s (add-to-store %store "bash" #t "sha256"
1285 (search-bootstrap-binary "bash"
1286 (%current-system))))
1287 (d (derivation %store "the-thing"
1289 #:env-vars `(("foo" . ,(random-text)))
1290 #:inputs `((,b) (,s))))
1291 (o (derivation->output-path d)))
1292 (and (build-derivations %store (list d))
1293 (not (path-info-deriver (query-path-info %store b)))
1294 (string=? (derivation-file-name d)
1295 (path-info-deriver (query-path-info %store o))))))
1297 (test-equal "build-cores"
1300 (let* ((build (add-text-to-store store "build.sh"
1301 "echo $NIX_BUILD_CORES > $out"))
1302 (bash (add-to-store store "bash" #t "sha256"
1303 (search-bootstrap-binary "bash"
1304 (%current-system))))
1305 (drv1 (derivation store "the-thing" bash
1307 #:inputs `((,bash) (,build))
1308 #:env-vars `(("x" . ,(random-text)))))
1309 (drv2 (derivation store "the-thing" bash
1311 #:inputs `((,bash) (,build))
1312 #:env-vars `(("x" . ,(random-text))))))
1313 (and (build-derivations store (list drv1))
1315 (set-build-options store #:build-cores 42)
1316 (build-derivations store (list drv2)))
1317 (list (call-with-input-file (derivation->output-path drv1)
1319 (call-with-input-file (derivation->output-path drv2)
1322 (test-equal "multiplexed-build-output"
1323 '("Hello from first." "Hello from second.")
1325 (let* ((build (add-text-to-store store "build.sh"
1326 "echo Hello from $NAME.; echo > $out"))
1327 (bash (add-to-store store "bash" #t "sha256"
1328 (search-bootstrap-binary "bash"
1329 (%current-system))))
1330 (drv1 (derivation store "one" bash
1332 #:inputs `((,bash) (,build))
1333 #:env-vars `(("NAME" . "first")
1334 ("x" . ,(random-text)))))
1335 (drv2 (derivation store "two" bash
1337 #:inputs `((,bash) (,build))
1338 #:env-vars `(("NAME" . "second")
1339 ("x" . ,(random-text))))))
1340 (set-build-options store
1341 #:print-build-trace #t
1342 #:multiplexed-build-output? #t
1343 #:max-build-jobs 10)
1344 (let ((port (open-output-string)))
1345 ;; Send the build log to PORT.
1346 (parameterize ((current-build-output-port port))
1347 (build-derivations store (list drv1 drv2)))
1349 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1350 ;; traces that allow us to retrieve each builder's output (we assume
1351 ;; there's exactly one "build-output" trace for each builder, which is
1353 (let* ((log (get-output-string port))
1354 (started (fold-matches
1355 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1358 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1360 (output (fold-matches
1361 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1363 (drv-pid (lambda (name)
1365 (let ((drv (match:substring m 1))
1366 (pid (string->number
1367 (match:substring m 4))))
1368 (and (string-suffix? name drv) pid)))))
1369 (pid-log (lambda (pid)
1371 (let ((n (string->number
1372 (match:substring m 1)))
1373 (len (string->number
1374 (match:substring m 2)))
1375 (str (match:substring m 3)))
1377 (= (string-length str) (- len 1))
1379 (pid1 (any (drv-pid "one.drv") started))
1380 (pid2 (any (drv-pid "two.drv") started)))
1381 (list (any (pid-log pid1) output)
1382 (any (pid-log pid2) output)))))))