derivations.scm 61 KB

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