derivations.scm 66 KB

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