derivations.scm 61 KB

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