derivations.scm 61 KB

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