derivations.scm 61 KB

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