derivations.scm 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2022 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 drv))
  406. (memq drv (list fixed1 fixed2)))))))
  407. (test-assert "derivation with equivalent fixed-output inputs"
  408. ;; Similar as the test above, but indirectly: DRV3A and DRV3B below are
  409. ;; equivalent derivations (same output paths) but they depend on
  410. ;; different-but-equivalent fixed-output derivations. Thus, DRV3A and DRV3B
  411. ;; must be coalesced as inputs of DRV4. See <https://bugs.gnu.org/54209>.
  412. (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
  413. "echo -n hello > $out"
  414. '()))
  415. (builder2 (add-text-to-store %store "fixed-builder2.sh"
  416. "echo -n hello > $out"
  417. '()))
  418. (builder3 (add-text-to-store %store "user-builder.sh"
  419. "echo 1 > $one; echo 2 > $two"
  420. '()))
  421. (hash (gcrypt:sha256 (string->utf8 "hello")))
  422. (drv1 (derivation %store "fixed" %bash (list builder1)
  423. #:sources (list builder1)
  424. #:hash hash #:hash-algo 'sha256))
  425. (drv2 (derivation %store "fixed" %bash (list builder2)
  426. #:sources (list builder2)
  427. #:hash hash #:hash-algo 'sha256))
  428. (drv3a (derivation %store "fixed-user" %bash (list builder3)
  429. #:outputs '("one" "two")
  430. #:sources (list builder3)
  431. #:inputs (list (derivation-input drv1))))
  432. (drv3b (derivation %store "fixed-user" %bash (list builder3)
  433. #:outputs '("one" "two")
  434. #:sources (list builder3)
  435. #:inputs (list (derivation-input drv2))))
  436. (drv4 (derivation %store "fixed-user-user" %bash (list builder1)
  437. #:sources (list builder1)
  438. #:inputs (list (derivation-input drv3a '("one"))
  439. (derivation-input drv3b '("two"))))))
  440. (match (derivation-inputs drv4)
  441. ((input)
  442. (and (memq (derivation-input-derivation input)
  443. (list drv3a drv3b))
  444. (lset= string=? (derivation-input-sub-derivations input)
  445. '("one" "two")))))))
  446. (test-assert "multiple-output derivation"
  447. (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
  448. "echo one > $out ; echo two > $second"
  449. '()))
  450. (drv (derivation %store "fixed"
  451. %bash `(,builder)
  452. #:env-vars '(("HOME" . "/homeless")
  453. ("zzz" . "Z!")
  454. ("AAA" . "A!"))
  455. #:sources `(,%bash ,builder)
  456. #:outputs '("out" "second")))
  457. (succeeded? (build-derivations %store (list drv))))
  458. (and succeeded?
  459. (let ((one (derivation->output-path drv "out"))
  460. (two (derivation->output-path drv "second")))
  461. (and (lset= equal?
  462. (derivation->output-paths drv)
  463. `(("out" . ,one) ("second" . ,two)))
  464. (eq? 'one (call-with-input-file one read))
  465. (eq? 'two (call-with-input-file two read)))))))
  466. (test-assert "multiple-output derivation, non-alphabetic order"
  467. ;; Here, the outputs are not listed in alphabetic order. Yet, the store
  468. ;; path computation must reorder them first.
  469. (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
  470. "echo one > $out ; echo two > $AAA"
  471. '()))
  472. (drv (derivation %store "fixed"
  473. %bash `(,builder)
  474. #:sources `(,%bash ,builder)
  475. #:outputs '("out" "AAA")))
  476. (succeeded? (build-derivations %store (list drv))))
  477. (and succeeded?
  478. (let ((one (derivation->output-path drv "out"))
  479. (two (derivation->output-path drv "AAA")))
  480. (and (eq? 'one (call-with-input-file one read))
  481. (eq? 'two (call-with-input-file two read)))))))
  482. (test-assert "read-derivation vs. derivation"
  483. ;; Make sure 'derivation' and 'read-derivation' return objects that are
  484. ;; identical.
  485. (let* ((sources (unfold (cut >= <> 10)
  486. (lambda (n)
  487. (add-text-to-store %store
  488. (format #f "input~a" n)
  489. (random-text)))
  490. 1+
  491. 0))
  492. (inputs (map (lambda (file)
  493. (derivation %store "derivation-input"
  494. %bash '()
  495. #:sources `(,%bash ,file)))
  496. sources))
  497. (builder (add-text-to-store %store "builder.sh"
  498. "echo one > $one ; echo two > $two"
  499. '()))
  500. (drv (derivation %store "derivation"
  501. %bash `(,builder)
  502. #:sources `(,%bash ,builder ,@sources)
  503. #:inputs (map derivation-input inputs)
  504. #:outputs '("two" "one")))
  505. (drv* (call-with-input-file (derivation-file-name drv)
  506. read-derivation)))
  507. (equal? drv* drv)))
  508. (test-assert "multiple-output derivation, derivation-path->output-path"
  509. (let* ((builder (add-text-to-store %store "builder.sh"
  510. "echo one > $out ; echo two > $second"
  511. '()))
  512. (drv (derivation %store "multiple"
  513. %bash `(,builder)
  514. #:outputs '("out" "second")))
  515. (drv-file (derivation-file-name drv))
  516. (one (derivation->output-path drv "out"))
  517. (two (derivation->output-path drv "second"))
  518. (first (derivation-path->output-path drv-file "out"))
  519. (second (derivation-path->output-path drv-file "second")))
  520. (and (not (string=? one two))
  521. (string-suffix? "-second" two)
  522. (string=? first one)
  523. (string=? second two))))
  524. (test-assert "user of multiple-output derivation"
  525. ;; Check whether specifying several inputs coming from the same
  526. ;; multiple-output derivation works.
  527. (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
  528. "echo one > $out ; echo two > $two"
  529. '()))
  530. (mdrv (derivation %store "multiple-output"
  531. %bash `(,builder1)
  532. #:sources (list %bash builder1)
  533. #:outputs '("out" "two")))
  534. (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
  535. "read x < $one;
  536. read y < $two;
  537. echo \"($x $y)\" > $out"
  538. '()))
  539. (udrv (derivation %store "multiple-output-user"
  540. %bash `(,builder2)
  541. #:env-vars `(("one"
  542. . ,(derivation->output-path
  543. mdrv "out"))
  544. ("two"
  545. . ,(derivation->output-path
  546. mdrv "two")))
  547. #:sources (list %bash builder2)
  548. ;; two occurrences of MDRV:
  549. #:inputs
  550. (list (derivation-input mdrv)
  551. (derivation-input mdrv '("two"))))))
  552. (and (build-derivations %store (list (pk 'udrv udrv)))
  553. (let ((p (derivation->output-path udrv)))
  554. (and (valid-path? %store p)
  555. (equal? '(one two) (call-with-input-file p read)))))))
  556. (test-assert "derivation with #:references-graphs"
  557. (let* ((input1 (add-text-to-store %store "foo" "hello"
  558. (list %bash)))
  559. (input2 (add-text-to-store %store "bar"
  560. (number->string (random 7777))
  561. (list input1)))
  562. (builder (add-text-to-store %store "build-graph"
  563. (format #f "
  564. ~a $out
  565. (while read l ; do echo $l ; done) < bash > $out/bash
  566. (while read l ; do echo $l ; done) < input1 > $out/input1
  567. (while read l ; do echo $l ; done) < input2 > $out/input2"
  568. %mkdir)
  569. (list %mkdir)))
  570. (drv (derivation %store "closure-graphs"
  571. %bash `(,builder)
  572. #:references-graphs
  573. `(("bash" . ,%bash)
  574. ("input1" . ,input1)
  575. ("input2" . ,input2))
  576. #:sources (list %bash builder)))
  577. (out (derivation->output-path drv)))
  578. (define (deps path . deps)
  579. (let ((count (length deps)))
  580. (string-append path "\n\n" (number->string count) "\n"
  581. (string-join (sort deps string<?) "\n")
  582. (if (zero? count) "" "\n"))))
  583. (and (build-derivations %store (list drv))
  584. (equal? (directory-contents out get-string-all)
  585. `(("/bash" . ,(string-append %bash "\n\n0\n"))
  586. ("/input1" . ,(if (string>? input1 %bash)
  587. (string-append (deps %bash)
  588. (deps input1 %bash))
  589. (string-append (deps input1 %bash)
  590. (deps %bash))))
  591. ("/input2" . ,(string-concatenate
  592. (map cdr
  593. (sort
  594. (map (lambda (p d)
  595. (cons p (apply deps p d)))
  596. (list %bash input1 input2)
  597. (list '() (list %bash) (list input1)))
  598. (lambda (x y)
  599. (match x
  600. ((p1 . _)
  601. (match y
  602. ((p2 . _)
  603. (string<? p1 p2)))))))))))))))
  604. (test-assert "derivation #:allowed-references, ok"
  605. (let ((drv (derivation %store "allowed" %bash
  606. '("-c" "echo hello > $out")
  607. #:sources (list %bash)
  608. #:allowed-references '())))
  609. (build-derivations %store (list drv))))
  610. (test-assert "derivation #:allowed-references, not allowed"
  611. (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
  612. (drv (derivation %store "disallowed" %bash
  613. `("-c" ,(string-append "echo " txt "> $out"))
  614. #:sources (list %bash txt)
  615. #:allowed-references '())))
  616. (guard (c ((store-protocol-error? c)
  617. ;; There's no specific error message to check for.
  618. #t))
  619. (build-derivations %store (list drv))
  620. #f)))
  621. (test-assert "derivation #:allowed-references, self allowed"
  622. (let ((drv (derivation %store "allowed" %bash
  623. '("-c" "echo $out > $out")
  624. #:sources (list %bash)
  625. #:allowed-references '("out"))))
  626. (build-derivations %store (list drv))))
  627. (test-assert "derivation #:allowed-references, self not allowed"
  628. (let ((drv (derivation %store "disallowed" %bash
  629. `("-c" ,"echo $out > $out")
  630. #:sources (list %bash)
  631. #:allowed-references '())))
  632. (guard (c ((store-protocol-error? c)
  633. ;; There's no specific error message to check for.
  634. #t))
  635. (build-derivations %store (list drv))
  636. #f)))
  637. (test-assert "derivation #:disallowed-references, ok"
  638. (let ((drv (derivation %store "disallowed" %bash
  639. '("-c" "echo hello > $out")
  640. #:sources (list %bash)
  641. #:disallowed-references '("out"))))
  642. (build-derivations %store (list drv))))
  643. (test-assert "derivation #:disallowed-references, not ok"
  644. (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
  645. (drv (derivation %store "disdisallowed" %bash
  646. `("-c" ,(string-append "echo " txt "> $out"))
  647. #:sources (list %bash txt)
  648. #:disallowed-references (list txt))))
  649. (guard (c ((store-protocol-error? c)
  650. ;; There's no specific error message to check for.
  651. #t))
  652. (build-derivations %store (list drv))
  653. #f)))
  654. ;; Here we should get the value of $GUIX_STATE_DIRECTORY that the daemon sees,
  655. ;; which is a unique value for each test process; this value is the same as
  656. ;; the one we see in the process executing this file since it is set by
  657. ;; 'test-env'.
  658. (test-equal "derivation #:leaked-env-vars"
  659. (getenv "GUIX_STATE_DIRECTORY")
  660. (let* ((value (getenv "GUIX_STATE_DIRECTORY"))
  661. (drv (derivation %store "leaked-env-vars" %bash
  662. '("-c" "echo -n $GUIX_STATE_DIRECTORY > $out")
  663. #:hash (gcrypt:sha256 (string->utf8 value))
  664. #:hash-algo 'sha256
  665. #:sources (list %bash)
  666. #:leaked-env-vars '("GUIX_STATE_DIRECTORY"))))
  667. (and (build-derivations %store (list drv))
  668. (call-with-input-file (derivation->output-path drv)
  669. get-string-all))))
  670. (define %coreutils
  671. (false-if-exception
  672. (and (network-reachable?)
  673. (package-derivation %store %bootstrap-coreutils&co))))
  674. (test-skip (if %coreutils 0 1))
  675. (test-assert "build derivation with coreutils"
  676. (let* ((builder
  677. (add-text-to-store %store "build-with-coreutils.sh"
  678. "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
  679. '()))
  680. (drv
  681. (derivation %store "foo"
  682. %bash `(,builder)
  683. #:env-vars `(("PATH" .
  684. ,(string-append
  685. (derivation->output-path %coreutils)
  686. "/bin")))
  687. #:sources (list builder)
  688. #:inputs (list (derivation-input %coreutils))))
  689. (succeeded?
  690. (build-derivations %store (list drv))))
  691. (and succeeded?
  692. (let ((p (derivation->output-path drv)))
  693. (and (valid-path? %store p)
  694. (file-exists? (string-append p "/good")))))))
  695. (test-skip (if (%guile-for-build) 0 8))
  696. (test-equal "build-expression->derivation and invalid module name"
  697. '(file-search-error "guix/module/that/does/not/exist.scm")
  698. (guard (c ((file-search-error? c)
  699. (list 'file-search-error
  700. (file-search-error-file-name c))))
  701. (build-expression->derivation %store "foo" #t
  702. #:modules '((guix module that
  703. does not exist)))))
  704. (test-equal "build-expression->derivation and builder encoding"
  705. '("UTF-8" #t)
  706. (let* ((exp '(λ (α) (+ α 1)))
  707. (drv (build-expression->derivation %store "foo" exp)))
  708. (match (derivation-builder-arguments drv)
  709. ((... builder)
  710. (with-fluids ((%default-port-encoding "UTF-8"))
  711. (call-with-input-file builder
  712. (lambda (port)
  713. (list (port-encoding port)
  714. (->bool
  715. (string-contains (get-string-all port)
  716. "(λ (α) (+ α 1))"))))))))))
  717. (test-assert "build-expression->derivation and derivation-prerequisites"
  718. (let ((drv (build-expression->derivation %store "fail" #f)))
  719. (any (match-lambda
  720. (($ <derivation-input> (= derivation-file-name path))
  721. (string=? path (derivation-file-name (%guile-for-build)))))
  722. (derivation-prerequisites drv))))
  723. (test-assert "derivation-prerequisites and valid-derivation-input?"
  724. (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
  725. (b (build-expression->derivation %store "b" `(list ,(random-text))))
  726. (c (build-expression->derivation %store "c" `(mkdir %output)
  727. #:inputs `(("a" ,a) ("b" ,b)))))
  728. ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
  729. ;; be removed by tests/guix-gc.sh.)
  730. (build-derivations %store
  731. (list a (package-derivation %store %bootstrap-guile)))
  732. (match (derivation-prerequisites c
  733. (cut valid-derivation-input? %store
  734. <>))
  735. ((($ <derivation-input> (= derivation-file-name file) ("out")))
  736. (string=? file (derivation-file-name b)))
  737. (x
  738. (pk 'fail x #f)))))
  739. (test-assert "build-expression->derivation without inputs"
  740. (let* ((builder '(begin
  741. (mkdir %output)
  742. (call-with-output-file (string-append %output "/test")
  743. (lambda (p)
  744. (display '(hello guix) p)))))
  745. (drv (build-expression->derivation %store "goo" builder))
  746. (succeeded? (build-derivations %store (list drv))))
  747. (and succeeded?
  748. (let ((p (derivation->output-path drv)))
  749. (equal? '(hello guix)
  750. (call-with-input-file (string-append p "/test") read))))))
  751. (test-assert "build-expression->derivation and max-silent-time"
  752. (let* ((store (let ((s (open-connection)))
  753. (set-build-options s #:max-silent-time 1)
  754. s))
  755. (builder '(begin (sleep 100) (mkdir %output) #t))
  756. (drv (build-expression->derivation store "silent" builder))
  757. (out-path (derivation->output-path drv)))
  758. (guard (c ((store-protocol-error? c)
  759. (and (string-contains (store-protocol-error-message c)
  760. "failed")
  761. (not (valid-path? store out-path)))))
  762. (build-derivations store (list drv))
  763. #f)))
  764. (test-assert "build-expression->derivation and timeout"
  765. (let* ((store (let ((s (open-connection)))
  766. (set-build-options s #:timeout 1)
  767. s))
  768. (builder '(begin (sleep 100) (mkdir %output) #t))
  769. (drv (build-expression->derivation store "slow" builder))
  770. (out-path (derivation->output-path drv)))
  771. (guard (c ((store-protocol-error? c)
  772. (and (string-contains (store-protocol-error-message c)
  773. "failed")
  774. (not (valid-path? store out-path)))))
  775. (build-derivations store (list drv))
  776. #f)))
  777. (test-assert "build-derivations with specific output"
  778. (with-store store
  779. (let* ((content (random-text)) ;contents of the output
  780. (drv (build-expression->derivation
  781. store "substitute-me"
  782. `(begin ,content (exit 1)) ;would fail
  783. #:outputs '("out" "one" "two")
  784. #:guile-for-build
  785. (package-derivation store %bootstrap-guile)))
  786. (out (derivation->output-path drv)))
  787. (with-derivation-substitute drv content
  788. (set-build-options store #:use-substitutes? #t
  789. #:substitute-urls (%test-substitute-urls))
  790. (and (has-substitutes? store out)
  791. ;; Ask for nothing but the "out" output of DRV.
  792. (build-derivations store `((,drv . "out")))
  793. ;; Synonymous:
  794. (build-derivations store (list (derivation-input drv '("out"))))
  795. (valid-path? store out)
  796. (equal? (pk 'x content)
  797. (pk 'y (call-with-input-file out get-string-all))))))))
  798. (test-assert "build-expression->derivation and derivation-build-plan"
  799. (let ((drv (build-expression->derivation %store "fail" #f)))
  800. ;; The only direct dependency is (%guile-for-build) and it's already
  801. ;; built.
  802. (null? (derivation-build-plan %store (derivation-inputs drv)))))
  803. (test-assert "derivation-build-plan when outputs already present"
  804. (let* ((builder `(begin ,(random-text) (mkdir %output) #t))
  805. (input-drv (build-expression->derivation %store "input" builder))
  806. (input-path (derivation->output-path input-drv))
  807. (drv (build-expression->derivation %store "something" builder
  808. #:inputs
  809. `(("i" ,input-drv))))
  810. (output (derivation->output-path drv)))
  811. ;; Assume these things are not already built.
  812. (when (or (valid-path? %store input-path)
  813. (valid-path? %store output))
  814. (error "things already built" input-drv))
  815. (and (lset= equal?
  816. (map derivation-file-name
  817. (derivation-build-plan %store
  818. (list (derivation-input drv))))
  819. (list (derivation-file-name input-drv)
  820. (derivation-file-name drv)))
  821. ;; Build DRV and delete its input.
  822. (build-derivations %store (list drv))
  823. (delete-paths %store (list input-path))
  824. (not (valid-path? %store input-path))
  825. ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
  826. ;; prerequisite to build because DRV itself is already built.
  827. (null? (derivation-build-plan %store
  828. (list (derivation-input drv)))))))
  829. (test-assert "derivation-build-plan and substitutes"
  830. (let* ((store (open-connection))
  831. (drv (build-expression->derivation store "prereq-subst"
  832. (random 1000)))
  833. (output (derivation->output-path drv)))
  834. ;; Make sure substitutes are usable.
  835. (set-build-options store #:use-substitutes? #t
  836. #:substitute-urls (%test-substitute-urls))
  837. (with-derivation-narinfo drv
  838. (let-values (((build download)
  839. (derivation-build-plan store
  840. (list (derivation-input drv))))
  841. ((build* download*)
  842. (derivation-build-plan store
  843. (list (derivation-input drv))
  844. #:substitutable-info
  845. (const #f))))
  846. (and (null? build)
  847. (equal? (map substitutable-path download) (list output))
  848. (null? download*)
  849. (equal? (list drv) build*))))))
  850. (test-assert "derivation-build-plan and substitutes, non-substitutable build"
  851. (let* ((store (open-connection))
  852. (drv (build-expression->derivation store "prereq-no-subst"
  853. (random 1000)
  854. #:substitutable? #f))
  855. (output (derivation->output-path drv)))
  856. ;; Make sure substitutes are usable.
  857. (set-build-options store #:use-substitutes? #t
  858. #:substitute-urls (%test-substitute-urls))
  859. (with-derivation-narinfo drv
  860. (let-values (((build download)
  861. (derivation-build-plan store
  862. (list (derivation-input drv)))))
  863. ;; Despite being available as a substitute, DRV will be built locally
  864. ;; due to #:substitutable? #f.
  865. (and (null? download)
  866. (match build
  867. (((= derivation-file-name build))
  868. (string=? build (derivation-file-name drv)))))))))
  869. (test-assert "derivation-build-plan and substitutes, non-substitutable dep"
  870. (with-store store
  871. (let* ((drv1 (build-expression->derivation store "prereq-no-subst"
  872. (random 1000)
  873. #:substitutable? #f))
  874. (drv2 (build-expression->derivation store "substitutable"
  875. (random 1000)
  876. #:inputs `(("dep" ,drv1)))))
  877. ;; Make sure substitutes are usable.
  878. (set-build-options store #:use-substitutes? #t
  879. #:substitute-urls (%test-substitute-urls))
  880. (with-derivation-narinfo drv2
  881. (sha256 => (make-bytevector 32 0))
  882. (references => (list (derivation->output-path drv1)))
  883. (let-values (((build download)
  884. (derivation-build-plan store
  885. (list (derivation-input drv2)))))
  886. ;; Although DRV2 is available as a substitute, we must build its
  887. ;; dependency, DRV1, due to #:substitutable? #f.
  888. (and (match download
  889. (((= substitutable-path item))
  890. (string=? item (derivation->output-path drv2))))
  891. (match build
  892. (((= derivation-file-name build))
  893. (string=? build (derivation-file-name drv1))))))))))
  894. (test-assert "derivation-build-plan and substitutes, local build"
  895. (with-store store
  896. (let* ((drv (build-expression->derivation store "prereq-subst-local"
  897. (random 1000)
  898. #:local-build? #t))
  899. (output (derivation->output-path drv)))
  900. ;; Make sure substitutes are usable.
  901. (set-build-options store #:use-substitutes? #t
  902. #:substitute-urls (%test-substitute-urls))
  903. (with-derivation-narinfo drv
  904. (let-values (((build download)
  905. (derivation-build-plan store
  906. (list (derivation-input drv)))))
  907. ;; #:local-build? is *not* synonymous with #:substitutable?, so we
  908. ;; must be able to substitute DRV's output.
  909. ;; See <http://bugs.gnu.org/18747>.
  910. (and (null? build)
  911. (match download
  912. (((= substitutable-path item))
  913. (string=? item (derivation->output-path drv))))))))))
  914. (test-assert "derivation-build-plan in 'check' mode"
  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 "to-check"
  920. '(mkdir %output)
  921. #:inputs `(("dep" ,dep)))))
  922. (build-derivations store (list drv))
  923. (delete-paths store (list (derivation->output-path dep)))
  924. ;; In 'check' mode, DEP must be rebuilt.
  925. (and (null? (derivation-build-plan store
  926. (list (derivation-input drv))))
  927. (lset= equal?
  928. (derivation-build-plan store
  929. (list (derivation-input drv))
  930. #:mode (build-mode check))
  931. (list drv dep))))))
  932. (test-assert "derivation-input-fold"
  933. (let* ((builder (add-text-to-store %store "my-builder.sh"
  934. "echo hello, world > \"$out\"\n"
  935. '()))
  936. (drv1 (derivation %store "foo"
  937. %bash `(,builder)
  938. #:sources `(,%bash ,builder)))
  939. (drv2 (derivation %store "bar"
  940. %bash `(,builder)
  941. #:inputs `((,drv1))
  942. #:sources `(,%bash ,builder))))
  943. (equal? (derivation-input-fold (lambda (input result)
  944. (cons (derivation-input-derivation input)
  945. result))
  946. '()
  947. (list (derivation-input drv2)))
  948. (list drv1 drv2))))
  949. (test-assert "substitution-oracle and #:substitute? #f"
  950. (with-store store
  951. (let* ((dep (build-expression->derivation store "dep"
  952. `(begin ,(random-text)
  953. (mkdir %output))))
  954. (drv (build-expression->derivation store "not-subst"
  955. `(begin ,(random-text)
  956. (mkdir %output))
  957. #:substitutable? #f
  958. #:inputs `(("dep" ,dep))))
  959. (query #f))
  960. (define (record-substitutable-path-query store paths)
  961. (when query
  962. (error "already called!" query))
  963. (set! query paths)
  964. '())
  965. (mock ((guix store) substitutable-path-info
  966. record-substitutable-path-query)
  967. (let ((pred (substitution-oracle store (list drv))))
  968. (pred (derivation->output-path drv))))
  969. ;; Make sure the oracle didn't try to get substitute info for DRV since
  970. ;; DRV is mark as non-substitutable. Assume that GUILE-FOR-BUILD is
  971. ;; already in store and thus not part of QUERY.
  972. (equal? (pk 'query query)
  973. (list (derivation->output-path dep))))))
  974. (test-assert "build-expression->derivation with expression returning #f"
  975. (let* ((builder '(begin
  976. (mkdir %output)
  977. #f)) ; fail!
  978. (drv (build-expression->derivation %store "fail" builder))
  979. (out-path (derivation->output-path drv)))
  980. (guard (c ((store-protocol-error? c)
  981. ;; Note that the output path may exist at this point, but it
  982. ;; is invalid.
  983. (and (string-match "build .* failed"
  984. (store-protocol-error-message c))
  985. (not (valid-path? %store out-path)))))
  986. (build-derivations %store (list drv))
  987. #f)))
  988. (test-assert "build-expression->derivation with two outputs"
  989. (let* ((builder '(begin
  990. (call-with-output-file (assoc-ref %outputs "out")
  991. (lambda (p)
  992. (display '(hello) p)))
  993. (call-with-output-file (assoc-ref %outputs "second")
  994. (lambda (p)
  995. (display '(world) p)))))
  996. (drv (build-expression->derivation %store "double" builder
  997. #:outputs '("out"
  998. "second")))
  999. (succeeded? (build-derivations %store (list drv))))
  1000. (and succeeded?
  1001. (let ((one (derivation->output-path drv))
  1002. (two (derivation->output-path drv "second")))
  1003. (and (equal? '(hello) (call-with-input-file one read))
  1004. (equal? '(world) (call-with-input-file two read)))))))
  1005. (test-skip (if %coreutils 0 1))
  1006. (test-assert "build-expression->derivation with one input"
  1007. (let* ((builder '(call-with-output-file %output
  1008. (lambda (p)
  1009. (let ((cu (assoc-ref %build-inputs "cu")))
  1010. (close 1)
  1011. (dup2 (port->fdes p) 1)
  1012. (execl (string-append cu "/bin/uname")
  1013. "uname" "-a")))))
  1014. (drv (build-expression->derivation %store "uname" builder
  1015. #:inputs
  1016. `(("cu" ,%coreutils))))
  1017. (succeeded? (build-derivations %store (list drv))))
  1018. (and succeeded?
  1019. (let ((p (derivation->output-path drv)))
  1020. (string-contains (call-with-input-file p read-line) "GNU")))))
  1021. (test-assert "build-expression->derivation with modules"
  1022. (let* ((builder `(begin
  1023. (use-modules (guix build utils))
  1024. (let ((out (assoc-ref %outputs "out")))
  1025. (mkdir-p (string-append out "/guile/guix/nix"))
  1026. #t)))
  1027. (drv (build-expression->derivation %store "test-with-modules"
  1028. builder
  1029. #:modules
  1030. '((guix build utils)))))
  1031. (and (build-derivations %store (list drv))
  1032. (let* ((p (derivation->output-path drv))
  1033. (s (stat (string-append p "/guile/guix/nix"))))
  1034. (eq? (stat:type s) 'directory)))))
  1035. (test-assert "build-expression->derivation: same fixed-output path"
  1036. (let* ((builder1 '(call-with-output-file %output
  1037. (lambda (p)
  1038. (write "hello" p))))
  1039. (builder2 '(call-with-output-file (pk 'difference-here! %output)
  1040. (lambda (p)
  1041. (write "hello" p))))
  1042. (hash (gcrypt:sha256 (string->utf8 "hello")))
  1043. (input1 (build-expression->derivation %store "fixed" builder1
  1044. #:hash hash
  1045. #:hash-algo 'sha256))
  1046. (input2 (build-expression->derivation %store "fixed" builder2
  1047. #:hash hash
  1048. #:hash-algo 'sha256))
  1049. (succeeded? (build-derivations %store (list input1 input2))))
  1050. (and succeeded?
  1051. (not (string=? (derivation-file-name input1)
  1052. (derivation-file-name input2)))
  1053. (string=? (derivation->output-path input1)
  1054. (derivation->output-path input2)))))
  1055. (test-assert "build-expression->derivation with a fixed-output input"
  1056. (let* ((builder1 '(call-with-output-file %output
  1057. (lambda (p)
  1058. (write "hello" p))))
  1059. (builder2 '(call-with-output-file (pk 'difference-here! %output)
  1060. (lambda (p)
  1061. (write "hello" p))))
  1062. (hash (gcrypt:sha256 (string->utf8 "hello")))
  1063. (input1 (build-expression->derivation %store "fixed" builder1
  1064. #:hash hash
  1065. #:hash-algo 'sha256))
  1066. (input2 (build-expression->derivation %store "fixed" builder2
  1067. #:hash hash
  1068. #:hash-algo 'sha256))
  1069. (builder3 '(let ((input (assoc-ref %build-inputs "input")))
  1070. (call-with-output-file %output
  1071. (lambda (out)
  1072. (format #f "My input is ~a.~%" input)))))
  1073. (final1 (build-expression->derivation %store "final" builder3
  1074. #:inputs
  1075. `(("input" ,input1))))
  1076. (final2 (build-expression->derivation %store "final" builder3
  1077. #:inputs
  1078. `(("input" ,input2)))))
  1079. (and (string=? (derivation->output-path final1)
  1080. (derivation->output-path final2))
  1081. (string=? (derivation->output-path final1)
  1082. (derivation-path->output-path
  1083. (derivation-file-name final1)))
  1084. (build-derivations %store (list final1 final2)))))
  1085. (test-assert "build-expression->derivation produces recursive fixed-output"
  1086. (let* ((builder '(begin
  1087. (use-modules (srfi srfi-26))
  1088. (mkdir %output)
  1089. (chdir %output)
  1090. (call-with-output-file "exe"
  1091. (cut display "executable" <>))
  1092. (chmod "exe" #o777)
  1093. (symlink "exe" "symlink")
  1094. (mkdir "subdir")))
  1095. (drv (build-expression->derivation %store "fixed-rec" builder
  1096. #:hash-algo 'sha256
  1097. #:hash (base32
  1098. "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
  1099. #:recursive? #t)))
  1100. (and (build-derivations %store (list drv))
  1101. (let* ((dir (derivation->output-path drv))
  1102. (exe (string-append dir "/exe"))
  1103. (link (string-append dir "/symlink"))
  1104. (subdir (string-append dir "/subdir")))
  1105. (and (executable-file? exe)
  1106. (string=? "executable"
  1107. (call-with-input-file exe get-string-all))
  1108. (string=? "exe" (readlink link))
  1109. (file-is-directory? subdir))))))
  1110. (test-assert "build-expression->derivation uses recursive fixed-output"
  1111. (let* ((builder '(call-with-output-file %output
  1112. (lambda (port)
  1113. (display "hello" port))))
  1114. (fixed (build-expression->derivation %store "small-fixed-rec"
  1115. builder
  1116. #:hash-algo 'sha256
  1117. #:hash (base32
  1118. "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
  1119. #:recursive? #t))
  1120. (in (derivation->output-path fixed))
  1121. (builder `(begin
  1122. (mkdir %output)
  1123. (chdir %output)
  1124. (symlink ,in "symlink")))
  1125. (drv (build-expression->derivation %store "fixed-rec-user"
  1126. builder
  1127. #:inputs `(("fixed" ,fixed)))))
  1128. (and (build-derivations %store (list drv))
  1129. (let ((out (derivation->output-path drv)))
  1130. (string=? (readlink (string-append out "/symlink")) in)))))
  1131. (test-assert "build-expression->derivation with #:references-graphs"
  1132. (let* ((input (add-text-to-store %store "foo" "hello"
  1133. (list %bash %mkdir)))
  1134. (builder '(copy-file "input" %output))
  1135. (drv (build-expression->derivation %store "references-graphs"
  1136. builder
  1137. #:references-graphs
  1138. `(("input" . ,input))))
  1139. (out (derivation->output-path drv)))
  1140. (define (deps path . deps)
  1141. (let ((count (length deps)))
  1142. (string-append path "\n\n" (number->string count) "\n"
  1143. (string-join (sort deps string<?) "\n")
  1144. (if (zero? count) "" "\n"))))
  1145. (and (build-derivations %store (list drv))
  1146. (equal? (call-with-input-file out get-string-all)
  1147. (string-concatenate
  1148. (map cdr
  1149. (sort (map (lambda (p d)
  1150. (cons p (apply deps p d)))
  1151. (list input %bash %mkdir)
  1152. (list (list %bash %mkdir)
  1153. '() '()))
  1154. (lambda (x y)
  1155. (match x
  1156. ((p1 . _)
  1157. (match y
  1158. ((p2 . _)
  1159. (string<? p1 p2)))))))))))))
  1160. (test-equal "derivation-properties"
  1161. (list '() '((type . test)))
  1162. (let ((drv1 (build-expression->derivation %store "bar"
  1163. '(mkdir %output)))
  1164. (drv2 (build-expression->derivation %store "foo"
  1165. '(mkdir %output)
  1166. #:properties '((type . test)))))
  1167. (list (derivation-properties drv1)
  1168. (derivation-properties drv2))))
  1169. (test-equal "map-derivation"
  1170. "hello"
  1171. (let* ((joke (package-derivation %store guile-1.8))
  1172. (good (package-derivation %store %bootstrap-guile))
  1173. (drv1 (build-expression->derivation %store "original-drv1"
  1174. #f ; systematically fail
  1175. #:guile-for-build joke))
  1176. (drv2 (build-expression->derivation %store "original-drv2"
  1177. '(call-with-output-file %output
  1178. (lambda (p)
  1179. (display "hello" p)))))
  1180. (drv3 (build-expression->derivation %store "drv-to-remap"
  1181. '(let ((in (assoc-ref
  1182. %build-inputs "in")))
  1183. (copy-file in %output))
  1184. #:inputs `(("in" ,drv1))
  1185. #:guile-for-build joke))
  1186. (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
  1187. (,joke . ,good))))
  1188. (out (derivation->output-path drv4)))
  1189. (and (build-derivations %store (list (pk 'remapped drv4)))
  1190. (call-with-input-file out get-string-all))))
  1191. (test-equal "map-derivation, sources"
  1192. "hello"
  1193. (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
  1194. (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
  1195. (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
  1196. (drv1 (derivation %store "drv-to-remap"
  1197. ;; XXX: This wouldn't work in practice, but if
  1198. ;; we append "/bin/bash" then we can't replace
  1199. ;; it with the bootstrap bash, which is a
  1200. ;; single file.
  1201. (derivation->output-path bash-full)
  1202. `("-e" ,script1)
  1203. #:sources (list script1)
  1204. #:inputs
  1205. (list (derivation-input bash-full '("out")))))
  1206. (drv2 (map-derivation %store drv1
  1207. `((,bash-full . ,%bash)
  1208. (,script1 . ,script2))))
  1209. (out (derivation->output-path drv2)))
  1210. (and (build-derivations %store (list (pk 'remapped* drv2)))
  1211. (call-with-input-file out get-string-all))))
  1212. (test-end)
  1213. ;; Local Variables:
  1214. ;; eval: (put 'with-http-server 'scheme-indent-function 1)
  1215. ;; End: