derivations.scm 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016, 2017 Mathieu Lirzin <mthl@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix derivations)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-9)
  22. #:use-module (srfi srfi-9 gnu)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-35)
  27. #:use-module (ice-9 binary-ports)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 rdelim)
  31. #:use-module (ice-9 vlist)
  32. #:use-module (guix store)
  33. #:use-module (guix utils)
  34. #:use-module (guix base16)
  35. #:use-module (guix memoization)
  36. #:use-module (guix combinators)
  37. #:use-module (guix deprecation)
  38. #:use-module (guix diagnostics)
  39. #:use-module (guix i18n)
  40. #:use-module (guix monads)
  41. #:use-module (gcrypt hash)
  42. #:use-module (guix base32)
  43. #:use-module (guix records)
  44. #:use-module (guix sets)
  45. #:export (<derivation>
  46. derivation?
  47. derivation-outputs
  48. derivation-inputs
  49. derivation-sources
  50. derivation-system
  51. derivation-builder
  52. derivation-builder-arguments
  53. derivation-builder-environment-vars
  54. derivation-file-name
  55. derivation-prerequisites
  56. derivation-build-plan
  57. derivation-prerequisites-to-build ;deprecated
  58. <derivation-output>
  59. derivation-output?
  60. derivation-output-path
  61. derivation-output-hash-algo
  62. derivation-output-hash
  63. derivation-output-recursive?
  64. <derivation-input>
  65. derivation-input?
  66. derivation-input
  67. derivation-input-path
  68. derivation-input-derivation
  69. derivation-input-sub-derivations
  70. derivation-input-output-paths
  71. derivation-input-output-path
  72. valid-derivation-input?
  73. &derivation-error
  74. derivation-error?
  75. derivation-error-derivation
  76. &derivation-missing-output-error
  77. derivation-missing-output-error?
  78. derivation-missing-output
  79. derivation-name
  80. derivation-output-names
  81. fixed-output-derivation?
  82. offloadable-derivation?
  83. substitutable-derivation?
  84. derivation-input-fold
  85. substitution-oracle
  86. derivation-hash
  87. derivation-properties
  88. read-derivation
  89. read-derivation-from-file
  90. write-derivation
  91. derivation->output-path
  92. derivation->output-paths
  93. derivation-path->output-path
  94. derivation-path->output-paths
  95. derivation
  96. raw-derivation
  97. invalidate-derivation-caches!
  98. map-derivation
  99. build-derivations
  100. built-derivations
  101. file-search-error?
  102. file-search-error-file-name
  103. file-search-error-search-path
  104. search-path*
  105. module->source-file-name
  106. build-expression->derivation)
  107. ;; Re-export it from here for backward compatibility.
  108. #:re-export (%guile-for-build))
  109. ;;;
  110. ;;; Error conditions.
  111. ;;;
  112. (define-condition-type &derivation-error &store-error
  113. derivation-error?
  114. (derivation derivation-error-derivation))
  115. (define-condition-type &derivation-missing-output-error &derivation-error
  116. derivation-missing-output-error?
  117. (output derivation-missing-output))
  118. ;;;
  119. ;;; Nix derivations, as implemented in Nix's `derivations.cc'.
  120. ;;;
  121. (define-immutable-record-type <derivation>
  122. (make-derivation outputs inputs sources system builder args env-vars
  123. file-name)
  124. derivation?
  125. (outputs derivation-outputs) ; list of name/<derivation-output> pairs
  126. (inputs derivation-inputs) ; list of <derivation-input>
  127. (sources derivation-sources) ; list of store paths
  128. (system derivation-system) ; string
  129. (builder derivation-builder) ; store path
  130. (args derivation-builder-arguments) ; list of strings
  131. (env-vars derivation-builder-environment-vars) ; list of name/value pairs
  132. (file-name derivation-file-name)) ; the .drv file name
  133. (define-immutable-record-type <derivation-output>
  134. (make-derivation-output path hash-algo hash recursive?)
  135. derivation-output?
  136. (path derivation-output-path) ; store path
  137. (hash-algo derivation-output-hash-algo) ; symbol | #f
  138. (hash derivation-output-hash) ; bytevector | #f
  139. (recursive? derivation-output-recursive?)) ; Boolean
  140. (define-immutable-record-type <derivation-input>
  141. (make-derivation-input drv sub-derivations)
  142. derivation-input?
  143. (drv derivation-input-derivation) ; <derivation>
  144. (sub-derivations derivation-input-sub-derivations)) ; list of strings
  145. (define (derivation-input-path input)
  146. "Return the file name of the derivation INPUT refers to."
  147. (derivation-file-name (derivation-input-derivation input)))
  148. (define* (derivation-input drv #:optional
  149. (outputs (derivation-output-names drv)))
  150. "Return a <derivation-input> for the OUTPUTS of DRV."
  151. ;; This is a public interface meant to be more convenient than
  152. ;; 'make-derivation-input' and giving us more control.
  153. (make-derivation-input drv outputs))
  154. (define (derivation-input-key input)
  155. "Return an object for which 'equal?' and 'hash' are constant-time, and which
  156. can thus be used as a key for INPUT in lookup tables."
  157. (cons (derivation-input-path input)
  158. (derivation-input-sub-derivations input)))
  159. (set-record-type-printer! <derivation>
  160. (lambda (drv port)
  161. (format port "#<derivation ~a => ~a ~a>"
  162. (derivation-file-name drv)
  163. (string-join
  164. (map (match-lambda
  165. ((_ . output)
  166. (derivation-output-path output)))
  167. (derivation-outputs drv)))
  168. (number->string (object-address drv) 16))))
  169. (define (derivation-name drv)
  170. "Return the base name of DRV."
  171. (let ((base (store-path-package-name (derivation-file-name drv))))
  172. (string-drop-right base 4)))
  173. (define (derivation-output-names drv)
  174. "Return the names of the outputs of DRV."
  175. (match (derivation-outputs drv)
  176. (((names . _) ...)
  177. names)))
  178. (define (fixed-output-derivation? drv)
  179. "Return #t if DRV is a fixed-output derivation, such as the result of a
  180. download with a fixed hash (aka. `fetchurl')."
  181. (match drv
  182. (($ <derivation>
  183. (("out" . ($ <derivation-output> _ (? symbol?) (? bytevector?)))))
  184. #t)
  185. (_ #f)))
  186. (define (derivation-input<? input1 input2)
  187. "Compare INPUT1 and INPUT2, two <derivation-input>."
  188. (string<? (derivation-input-path input1)
  189. (derivation-input-path input2)))
  190. (define (derivation-input-output-paths input)
  191. "Return the list of output paths corresponding to INPUT, a
  192. <derivation-input>."
  193. (match input
  194. (($ <derivation-input> drv sub-drvs)
  195. (map (cut derivation->output-path drv <>)
  196. sub-drvs))))
  197. (define (derivation-input-output-path input)
  198. "Return the output file name of INPUT. If INPUT has more than one outputs,
  199. an error is raised."
  200. (match input
  201. (($ <derivation-input> drv (output))
  202. (derivation->output-path drv output))))
  203. (define (valid-derivation-input? store input)
  204. "Return true if INPUT is valid--i.e., if all the outputs it requests are in
  205. the store."
  206. (every (cut valid-path? store <>)
  207. (derivation-input-output-paths input)))
  208. (define (coalesce-duplicate-inputs inputs)
  209. "Return a list of inputs, such that when INPUTS contains the same DRV twice,
  210. they are coalesced, with their sub-derivations merged. This is needed because
  211. Nix itself keeps only one of them."
  212. (fold (lambda (input result)
  213. (match input
  214. (($ <derivation-input> (= derivation-file-name path) sub-drvs)
  215. ;; XXX: quadratic
  216. (match (find (match-lambda
  217. (($ <derivation-input> (= derivation-file-name p)
  218. s)
  219. (string=? p path)))
  220. result)
  221. (#f
  222. (cons input result))
  223. ((and dup ($ <derivation-input> drv sub-drvs2))
  224. ;; Merge DUP with INPUT.
  225. (let ((sub-drvs (delete-duplicates
  226. (append sub-drvs sub-drvs2))))
  227. (cons (make-derivation-input drv (sort sub-drvs string<?))
  228. (delq dup result))))))))
  229. '()
  230. inputs))
  231. (define* (derivation-prerequisites drv #:optional (cut? (const #f)))
  232. "Return the list of derivation-inputs required to build DRV, recursively.
  233. CUT? is a predicate that is passed a derivation-input and returns true to
  234. eliminate the given input and its dependencies from the search. An example of
  235. such a predicate is 'valid-derivation-input?'; when it is used as CUT?, the
  236. result is the set of prerequisites of DRV not already in valid."
  237. (let loop ((drv drv)
  238. (result '())
  239. (input-set (set)))
  240. (let ((inputs (remove (lambda (input)
  241. (or (set-contains? input-set
  242. (derivation-input-key input))
  243. (cut? input)))
  244. (derivation-inputs drv))))
  245. (fold2 loop
  246. (append inputs result)
  247. (fold set-insert input-set
  248. (map derivation-input-key inputs))
  249. (map derivation-input-derivation inputs)))))
  250. (define (offloadable-derivation? drv)
  251. "Return true if DRV can be offloaded, false otherwise."
  252. (match (assoc "preferLocalBuild"
  253. (derivation-builder-environment-vars drv))
  254. (("preferLocalBuild" . "1") #f)
  255. (_ #t)))
  256. (define (substitutable-derivation? drv)
  257. "Return #t if DRV can be substituted."
  258. (match (assoc "allowSubstitutes"
  259. (derivation-builder-environment-vars drv))
  260. (("allowSubstitutes" . value)
  261. (string=? value "1"))
  262. (_ #t)))
  263. (define (derivation-output-paths drv sub-drvs)
  264. "Return the output paths of outputs SUB-DRVS of DRV."
  265. (match drv
  266. (($ <derivation> outputs)
  267. (map (lambda (sub-drv)
  268. (derivation-output-path (assoc-ref outputs sub-drv)))
  269. sub-drvs))))
  270. (define* (derivation-input-fold proc seed inputs
  271. #:key (cut? (const #f)))
  272. "Perform a breadth-first traversal of INPUTS, calling PROC on each input
  273. with the current result, starting from SEED. Skip recursion on inputs that
  274. match CUT?."
  275. (let loop ((inputs inputs)
  276. (result seed)
  277. (visited (set)))
  278. (match inputs
  279. (()
  280. result)
  281. ((input rest ...)
  282. (let ((key (derivation-input-key input)))
  283. (cond ((set-contains? visited key)
  284. (loop rest result visited))
  285. ((cut? input)
  286. (loop rest result (set-insert key visited)))
  287. (else
  288. (let ((drv (derivation-input-derivation input)))
  289. (loop (append (derivation-inputs drv) rest)
  290. (proc input result)
  291. (set-insert key visited))))))))))
  292. (define* (substitution-oracle store inputs-or-drv
  293. #:key (mode (build-mode normal)))
  294. "Return a one-argument procedure that, when passed a store file name,
  295. returns a 'substitutable?' if it's substitutable and #f otherwise.
  296. The returned procedure knows about all substitutes for all the derivation
  297. inputs or derivations listed in INPUTS-OR-DRV, *except* those that are already
  298. valid (that is, it won't bother checking whether an item is substitutable if
  299. it's already on disk); it also knows about their prerequisites, unless they
  300. are themselves substitutable.
  301. Creating a single oracle (thus making a single 'substitutable-path-info' call) and
  302. reusing it is much more efficient than calling 'has-substitutes?' or similar
  303. repeatedly, because it avoids the costs associated with launching the
  304. substituter many times."
  305. (define valid-input?
  306. (cut valid-derivation-input? store <>))
  307. (define (closure inputs)
  308. (reverse
  309. (derivation-input-fold (lambda (input closure)
  310. (let ((drv (derivation-input-derivation input)))
  311. (if (substitutable-derivation? drv)
  312. (cons input closure)
  313. closure)))
  314. '()
  315. inputs
  316. #:cut? valid-input?)))
  317. (let* ((inputs (closure (map (match-lambda
  318. ((? derivation-input? input)
  319. input)
  320. ((? derivation? drv)
  321. (derivation-input drv)))
  322. inputs-or-drv)))
  323. (items (append-map derivation-input-output-paths inputs))
  324. (subst (fold (lambda (subst vhash)
  325. (vhash-cons (substitutable-path subst) subst
  326. vhash))
  327. vlist-null
  328. (substitutable-path-info store items))))
  329. (lambda (item)
  330. (match (vhash-assoc item subst)
  331. (#f #f)
  332. ((key . value) value)))))
  333. (define (dependencies-of-substitutables substitutables inputs)
  334. "Return the subset of INPUTS whose output file names is among the references
  335. of SUBSTITUTABLES."
  336. (let ((items (fold set-insert (set)
  337. (append-map substitutable-references substitutables))))
  338. (filter (lambda (input)
  339. (any (cut set-contains? items <>)
  340. (derivation-input-output-paths input)))
  341. inputs)))
  342. (define* (derivation-build-plan store inputs
  343. #:key
  344. (mode (build-mode normal))
  345. (substitutable-info
  346. (substitution-oracle
  347. store inputs #:mode mode)))
  348. "Given INPUTS, a list of derivation-inputs, return two values: the list of
  349. derivations to build, and the list of substitutable items that, together,
  350. allow INPUTS to be realized.
  351. SUBSTITUTABLE-INFO must be a one-argument procedure similar to that returned
  352. by 'substitution-oracle'."
  353. (define (built? item)
  354. (valid-path? store item))
  355. (define (input-built? input)
  356. ;; In 'check' mode, assume that DRV is not built.
  357. (and (not (and (eqv? mode (build-mode check))
  358. (member input inputs)))
  359. (every built? (derivation-input-output-paths input))))
  360. (define (input-substitutable-info input)
  361. (and (substitutable-derivation? (derivation-input-derivation input))
  362. (let* ((items (derivation-input-output-paths input))
  363. (info (filter-map substitutable-info items)))
  364. (and (= (length info) (length items))
  365. info))))
  366. (let loop ((inputs inputs) ;list of <derivation-input>
  367. (build '()) ;list of <derivation>
  368. (substitute '()) ;list of <substitutable>
  369. (visited (set))) ;set of <derivation-input>
  370. (match inputs
  371. (()
  372. (values build substitute))
  373. ((input rest ...)
  374. (let ((key (derivation-input-key input))
  375. (deps (derivation-inputs
  376. (derivation-input-derivation input))))
  377. (cond ((set-contains? visited key)
  378. (loop rest build substitute visited))
  379. ((input-built? input)
  380. (loop rest build substitute
  381. (set-insert key visited)))
  382. ((input-substitutable-info input)
  383. =>
  384. (lambda (substitutables)
  385. (loop (append (dependencies-of-substitutables substitutables
  386. deps)
  387. rest)
  388. build
  389. (append substitutables substitute)
  390. (set-insert key visited))))
  391. (else
  392. (loop (append deps rest)
  393. (cons (derivation-input-derivation input) build)
  394. substitute
  395. (set-insert key visited)))))))))
  396. (define-deprecated (derivation-prerequisites-to-build store drv #:rest rest)
  397. derivation-build-plan
  398. (let-values (((build download)
  399. (apply derivation-build-plan store
  400. (list (derivation-input drv)) rest)))
  401. (values (map derivation-input build) download)))
  402. (define* (read-derivation drv-port
  403. #:optional (read-derivation-from-file
  404. read-derivation-from-file))
  405. "Read the derivation from DRV-PORT and return the corresponding <derivation>
  406. object. Call READ-DERIVATION-FROM-FILE to read derivations declared as inputs
  407. of the derivation being parsed.
  408. Most of the time you'll want to use 'read-derivation-from-file', which caches
  409. things as appropriate and is thus more efficient."
  410. (define comma (string->symbol ","))
  411. (define (ununquote x)
  412. (match x
  413. (('unquote x) (ununquote x))
  414. ((x ...) (map ununquote x))
  415. (_ x)))
  416. (define (outputs->alist x)
  417. (fold-right (lambda (output result)
  418. (match output
  419. ((name path "" "")
  420. (alist-cons name
  421. (make-derivation-output path #f #f #f)
  422. result))
  423. ((name path hash-algo hash)
  424. ;; fixed-output
  425. (let* ((rec? (string-prefix? "r:" hash-algo))
  426. (algo (string->symbol
  427. (if rec?
  428. (string-drop hash-algo 2)
  429. hash-algo)))
  430. (hash (base16-string->bytevector hash)))
  431. (alist-cons name
  432. (make-derivation-output path algo
  433. hash rec?)
  434. result)))))
  435. '()
  436. x))
  437. (define (make-input-drvs x)
  438. (fold-right (lambda (input result)
  439. (match input
  440. ((path (sub-drvs ...))
  441. (let ((drv (read-derivation-from-file path)))
  442. (cons (make-derivation-input drv sub-drvs)
  443. result)))))
  444. '()
  445. x))
  446. ;; The contents of a derivation are typically ASCII, but choosing
  447. ;; UTF-8 allows us to take the fast path for Guile's `scm_getc'.
  448. (set-port-encoding! drv-port "UTF-8")
  449. (let loop ((exp (read drv-port))
  450. (result '()))
  451. (match exp
  452. ((? eof-object?)
  453. (let ((result (reverse result)))
  454. (match result
  455. (('Derive ((outputs ...) (input-drvs ...)
  456. (input-srcs ...)
  457. (? string? system)
  458. (? string? builder)
  459. ((? string? args) ...)
  460. ((var value) ...)))
  461. (make-derivation (outputs->alist outputs)
  462. (make-input-drvs input-drvs)
  463. input-srcs
  464. system builder args
  465. (fold-right alist-cons '() var value)
  466. (port-filename drv-port)))
  467. (_
  468. (error "failed to parse derivation" drv-port result)))))
  469. ((? (cut eq? <> comma))
  470. (loop (read drv-port) result))
  471. (_
  472. (loop (read drv-port)
  473. (cons (ununquote exp) result))))))
  474. (define %derivation-cache
  475. ;; Maps derivation file names to <derivation> objects.
  476. ;; XXX: This is redundant with 'atts-cache' in the store.
  477. (make-weak-value-hash-table 200))
  478. (define (read-derivation-from-file file)
  479. "Read the derivation in FILE, a '.drv' file, and return the corresponding
  480. <derivation> object."
  481. ;; Memoize that operation because 'read-derivation' is quite expensive,
  482. ;; and because the same argument is read more than 15 times on average
  483. ;; during something like (package-derivation s gdb).
  484. (or (and file (hash-ref %derivation-cache file))
  485. (let ((drv (call-with-input-file file read-derivation)))
  486. (hash-set! %derivation-cache file drv)
  487. drv)))
  488. (define-inlinable (write-sequence lst write-item port)
  489. ;; Write each element of LST with WRITE-ITEM to PORT, separating them with a
  490. ;; comma.
  491. (match lst
  492. (()
  493. #t)
  494. ((prefix (... ...) last)
  495. (for-each (lambda (item)
  496. (write-item item port)
  497. (display "," port))
  498. prefix)
  499. (write-item last port))))
  500. (define-inlinable (write-list lst write-item port)
  501. ;; Write LST as a derivation list to PORT, using WRITE-ITEM to write each
  502. ;; element.
  503. (display "[" port)
  504. (write-sequence lst write-item port)
  505. (display "]" port))
  506. (define-inlinable (write-tuple lst write-item port)
  507. ;; Same, but write LST as a tuple.
  508. (display "(" port)
  509. (write-sequence lst write-item port)
  510. (display ")" port))
  511. (define (write-derivation drv port)
  512. "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
  513. Eelco Dolstra's PhD dissertation for an overview of a previous version of
  514. that form."
  515. ;; Make sure we're using the faster implementation.
  516. (define format simple-format)
  517. (define (write-string-list lst)
  518. (write-list lst write port))
  519. (define (write-output output port)
  520. (match output
  521. ((name . ($ <derivation-output> path hash-algo hash recursive?))
  522. (write-tuple (list name path
  523. (if hash-algo
  524. (string-append (if recursive? "r:" "")
  525. (symbol->string hash-algo))
  526. "")
  527. (or (and=> hash bytevector->base16-string)
  528. ""))
  529. write
  530. port))))
  531. (define (write-input input port)
  532. (match input
  533. (($ <derivation-input> obj sub-drvs)
  534. (display "(\"" port)
  535. ;; 'derivation/masked-inputs' produces objects that contain a string
  536. ;; instead of a <derivation>, so we need to account for that.
  537. (display (if (derivation? obj)
  538. (derivation-file-name obj)
  539. obj)
  540. port)
  541. (display "\"," port)
  542. (write-string-list sub-drvs)
  543. (display ")" port))))
  544. (define (write-env-var env-var port)
  545. (match env-var
  546. ((name . value)
  547. (display "(" port)
  548. (write name port)
  549. (display "," port)
  550. (write value port)
  551. (display ")" port))))
  552. ;; Assume all the lists we are writing are already sorted.
  553. (match drv
  554. (($ <derivation> outputs inputs sources
  555. system builder args env-vars)
  556. (display "Derive(" port)
  557. (write-list outputs write-output port)
  558. (display "," port)
  559. (write-list inputs write-input port)
  560. (display "," port)
  561. (write-string-list sources)
  562. (simple-format port ",\"~a\",\"~a\"," system builder)
  563. (write-string-list args)
  564. (display "," port)
  565. (write-list env-vars write-env-var port)
  566. (display ")" port))))
  567. (define derivation->bytevector
  568. (lambda (drv)
  569. "Return the external representation of DRV as a UTF-8-encoded string."
  570. (with-fluids ((%default-port-encoding "UTF-8"))
  571. (call-with-values open-bytevector-output-port
  572. (lambda (port get-bytevector)
  573. (write-derivation drv port)
  574. (get-bytevector))))))
  575. (define* (derivation->output-path drv #:optional (output "out"))
  576. "Return the store path of its output OUTPUT. Raise a
  577. '&derivation-missing-output-error' condition if OUTPUT is not an output of
  578. DRV."
  579. (let ((output* (assoc-ref (derivation-outputs drv) output)))
  580. (if output*
  581. (derivation-output-path output*)
  582. (raise (condition (&derivation-missing-output-error
  583. (derivation drv)
  584. (output output)))))))
  585. (define (derivation->output-paths drv)
  586. "Return the list of name/path pairs of the outputs of DRV."
  587. (map (match-lambda
  588. ((name . output)
  589. (cons name (derivation-output-path output))))
  590. (derivation-outputs drv)))
  591. (define derivation-path->output-path
  592. ;; This procedure is called frequently, so memoize it.
  593. (let ((memoized (mlambda (path output)
  594. (derivation->output-path (read-derivation-from-file path)
  595. output))))
  596. (lambda* (path #:optional (output "out"))
  597. "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the store
  598. path of its output OUTPUT."
  599. (memoized path output))))
  600. (define (derivation-path->output-paths path)
  601. "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the
  602. list of name/path pairs of its outputs."
  603. (derivation->output-paths (read-derivation-from-file path)))
  604. ;;;
  605. ;;; Derivation primitive.
  606. ;;;
  607. (define derivation-base16-hash
  608. (mlambdaq (drv)
  609. "Return a string containing the base16 representation of the hash of DRV."
  610. (bytevector->base16-string (derivation-hash drv))))
  611. (define (derivation/masked-inputs drv)
  612. "Assuming DRV is a regular derivation (not fixed-output), replace the file
  613. name of each input with that input's hash."
  614. (match drv
  615. (($ <derivation> outputs inputs sources
  616. system builder args env-vars)
  617. (let ((inputs (map (match-lambda
  618. (($ <derivation-input> drv sub-drvs)
  619. (let ((hash (derivation-base16-hash drv)))
  620. (make-derivation-input hash sub-drvs))))
  621. inputs)))
  622. (make-derivation outputs
  623. (sort (delete-duplicates inputs)
  624. (lambda (drv1 drv2)
  625. (string<? (derivation-input-derivation drv1)
  626. (derivation-input-derivation drv2))))
  627. sources
  628. system builder args env-vars
  629. #f)))))
  630. (define derivation-hash ; `hashDerivationModulo' in derivations.cc
  631. (lambda (drv)
  632. "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
  633. (match drv
  634. (($ <derivation> ((_ . ($ <derivation-output> path
  635. (? symbol? hash-algo) (? bytevector? hash)
  636. (? boolean? recursive?)))))
  637. ;; A fixed-output derivation.
  638. (sha256
  639. (string->utf8
  640. (string-append "fixed:out:"
  641. (if recursive? "r:" "")
  642. (symbol->string hash-algo)
  643. ":" (bytevector->base16-string hash)
  644. ":" path))))
  645. (_
  646. ;; XXX: At this point this remains faster than `port-sha256', because
  647. ;; the SHA256 port's `write' method gets called for every single
  648. ;; character.
  649. (sha256 (derivation->bytevector (derivation/masked-inputs drv)))))))
  650. (define (warn-about-derivation-deprecation name)
  651. ;; TRANSLATORS: 'derivation' must not be translated; it refers to the
  652. ;; 'derivation' procedure.
  653. (warning (G_ "in '~a': deprecated 'derivation' calling convention used~%")
  654. name))
  655. (define* (derivation store name builder args
  656. #:key
  657. (system (%current-system)) (env-vars '())
  658. (inputs '()) (sources '())
  659. (outputs '("out"))
  660. hash hash-algo recursive?
  661. references-graphs
  662. allowed-references disallowed-references
  663. leaked-env-vars local-build?
  664. (substitutable? #t)
  665. (properties '())
  666. (%deprecation-warning? #t))
  667. "Build a derivation with the given arguments, and return the resulting
  668. <derivation> object. When HASH and HASH-ALGO are given, a
  669. fixed-output derivation is created---i.e., one whose result is known in
  670. advance, such as a file download. If, in addition, RECURSIVE? is true, then
  671. that fixed output may be an executable file or a directory and HASH must be
  672. the hash of an archive containing this output.
  673. When REFERENCES-GRAPHS is true, it must be a list of file name/store path
  674. pairs. In that case, the reference graph of each store path is exported in
  675. the build environment in the corresponding file, in a simple text format.
  676. When ALLOWED-REFERENCES is true, it must be a list of store items or outputs
  677. that the derivation's outputs may refer to. Likewise, DISALLOWED-REFERENCES,
  678. if true, must be a list of things the outputs may not refer to.
  679. When LEAKED-ENV-VARS is true, it must be a list of strings denoting
  680. environment variables that are allowed to \"leak\" from the daemon's
  681. environment to the build environment. This is only applicable to fixed-output
  682. derivations--i.e., when HASH is true. The main use is to allow variables such
  683. as \"http_proxy\" to be passed to derivations that download files.
  684. When LOCAL-BUILD? is true, declare that the derivation is not a good candidate
  685. for offloading and should rather be built locally. This is the case for small
  686. derivations where the costs of data transfers would outweigh the benefits.
  687. When SUBSTITUTABLE? is false, declare that substitutes of the derivation's
  688. output should not be used.
  689. PROPERTIES must be an association list describing \"properties\" of the
  690. derivation. It is kept as-is, uninterpreted, in the derivation."
  691. (define (add-output-paths drv)
  692. ;; Return DRV with an actual store path for each of its output and the
  693. ;; corresponding environment variable.
  694. (match drv
  695. (($ <derivation> outputs inputs sources
  696. system builder args env-vars)
  697. (let* ((drv-hash (derivation-hash drv))
  698. (outputs (map (match-lambda
  699. ((output-name . ($ <derivation-output>
  700. _ algo hash rec?))
  701. (let ((path
  702. (if hash
  703. (fixed-output-path name hash
  704. #:hash-algo algo
  705. #:output output-name
  706. #:recursive? rec?)
  707. (output-path output-name
  708. drv-hash name))))
  709. (cons output-name
  710. (make-derivation-output path algo
  711. hash rec?)))))
  712. outputs)))
  713. (make-derivation outputs inputs sources system builder args
  714. (map (match-lambda
  715. ((name . value)
  716. (cons name
  717. (or (and=> (assoc-ref outputs name)
  718. derivation-output-path)
  719. value))))
  720. env-vars)
  721. #f)))))
  722. (define (user+system-env-vars)
  723. ;; Some options are passed to the build daemon via the env. vars of
  724. ;; derivations (urgh!). We hide that from our API, but here is the place
  725. ;; where we kludgify those options.
  726. (let ((env-vars `(,@(if local-build?
  727. `(("preferLocalBuild" . "1"))
  728. '())
  729. ,@(if (not substitutable?)
  730. `(("allowSubstitutes" . "0"))
  731. '())
  732. ,@(if allowed-references
  733. `(("allowedReferences"
  734. . ,(string-join allowed-references)))
  735. '())
  736. ,@(if disallowed-references
  737. `(("disallowedReferences"
  738. . ,(string-join disallowed-references)))
  739. '())
  740. ,@(if leaked-env-vars
  741. `(("impureEnvVars"
  742. . ,(string-join leaked-env-vars)))
  743. '())
  744. ,@(match properties
  745. (() '())
  746. (lst `(("guix properties"
  747. . ,(object->string properties)))))
  748. ,@env-vars)))
  749. (match references-graphs
  750. (((file . path) ...)
  751. (let ((value (map (cut string-append <> " " <>)
  752. file path)))
  753. ;; XXX: This all breaks down if an element of FILE or PATH contains
  754. ;; white space.
  755. `(("exportReferencesGraph" . ,(string-join value " "))
  756. ,@env-vars)))
  757. (#f
  758. env-vars))))
  759. (define (env-vars-with-empty-outputs env-vars)
  760. ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
  761. ;; empty string, even outputs that do not appear in ENV-VARS.
  762. (let ((e (map (match-lambda
  763. ((name . val)
  764. (if (member name outputs)
  765. (cons name "")
  766. (cons name val))))
  767. env-vars)))
  768. (fold (lambda (output-name env-vars)
  769. (if (assoc output-name env-vars)
  770. env-vars
  771. (append env-vars `((,output-name . "")))))
  772. e
  773. outputs)))
  774. (define-syntax-rule (warn-deprecation name)
  775. (when %deprecation-warning?
  776. (warn-about-derivation-deprecation name)))
  777. (define input->derivation-input
  778. (match-lambda
  779. ((? derivation-input? input)
  780. input)
  781. (((? derivation? drv))
  782. (warn-deprecation name)
  783. (make-derivation-input drv '("out")))
  784. (((? derivation? drv) sub-drvs ...)
  785. (warn-deprecation name)
  786. (make-derivation-input drv sub-drvs))
  787. (_
  788. (warn-deprecation name)
  789. #f)))
  790. (define input->source
  791. (match-lambda
  792. (((? string? input) . _)
  793. (warn-deprecation name)
  794. (if (direct-store-path? input)
  795. input
  796. (add-to-store store (basename input)
  797. #t "sha256" input)))
  798. (_ #f)))
  799. ;; Note: lists are sorted alphabetically, to conform with the behavior of
  800. ;; C++ `std::map' in Nix itself.
  801. (let* ((outputs (map (lambda (name)
  802. ;; Return outputs with an empty path.
  803. (cons name
  804. (make-derivation-output "" hash-algo
  805. hash recursive?)))
  806. (sort outputs string<?)))
  807. (sources (sort (delete-duplicates
  808. (append (filter-map input->source inputs)
  809. sources))
  810. string<?))
  811. (inputs (sort (coalesce-duplicate-inputs
  812. (filter-map input->derivation-input inputs))
  813. derivation-input<?))
  814. (env-vars (sort (env-vars-with-empty-outputs
  815. (user+system-env-vars))
  816. (lambda (e1 e2)
  817. (string<? (car e1) (car e2)))))
  818. (drv-masked (make-derivation outputs inputs sources
  819. system builder args env-vars #f))
  820. (drv (add-output-paths drv-masked)))
  821. (let* ((file (add-data-to-store store (string-append name ".drv")
  822. (derivation->bytevector drv)
  823. (append (map derivation-input-path inputs)
  824. sources)))
  825. (drv* (set-field drv (derivation-file-name) file)))
  826. ;; Preserve pointer equality. This improves the performance of
  827. ;; 'eq?'-memoization on derivations.
  828. (or (hash-ref %derivation-cache file)
  829. (begin
  830. (hash-set! %derivation-cache file drv*)
  831. drv*)))))
  832. (define (invalidate-derivation-caches!)
  833. "Invalidate internal derivation caches. This is mostly useful for
  834. long-running processes that know what they're doing. Use with care!"
  835. ;; Typically this is meant to be used by Cuirass and Hydra, which can clear
  836. ;; caches when they start evaluating packages for another architecture.
  837. (invalidate-memoization! derivation-base16-hash)
  838. ;; FIXME: Comment out to work around <https://bugs.gnu.org/36487>.
  839. ;; (hash-clear! %derivation-cache)
  840. )
  841. (define derivation-properties
  842. (mlambdaq (drv)
  843. "Return the property alist associated with DRV."
  844. (match (assoc "guix properties"
  845. (derivation-builder-environment-vars drv))
  846. ((_ . str) (call-with-input-string str read))
  847. (#f '()))))
  848. (define* (map-derivation store drv mapping
  849. #:key (system (%current-system)))
  850. "Given MAPPING, a list of pairs of derivations, return a derivation based on
  851. DRV where all the 'car's of MAPPING have been replaced by its 'cdr's,
  852. recursively."
  853. (define (substitute str initial replacements)
  854. (fold (lambda (path replacement result)
  855. (string-replace-substring result path
  856. replacement))
  857. str
  858. initial replacements))
  859. (define (substitute-file file initial replacements)
  860. (define contents
  861. (with-fluids ((%default-port-encoding #f))
  862. (call-with-input-file file read-string)))
  863. (let ((updated (substitute contents initial replacements)))
  864. (if (string=? updated contents)
  865. file
  866. ;; XXX: permissions aren't preserved.
  867. (add-text-to-store store (store-path-package-name file)
  868. updated))))
  869. (define input->output-paths
  870. (match-lambda
  871. ((? derivation-input? input)
  872. (derivation-input-output-paths input))
  873. ((? string? file)
  874. (list file))))
  875. (let ((mapping (fold (lambda (pair result)
  876. (match pair
  877. (((? derivation? orig) . replacement)
  878. (vhash-cons (derivation-file-name orig)
  879. replacement result))
  880. ((file . replacement)
  881. (vhash-cons file replacement result))))
  882. vlist-null
  883. mapping)))
  884. (define rewritten-input
  885. ;; Rewrite the given input according to MAPPING, and return an input
  886. ;; in the format used in 'derivation' calls.
  887. (mlambda (input loop)
  888. (match input
  889. (($ <derivation-input> drv (sub-drvs ...))
  890. (match (vhash-assoc (derivation-file-name drv) mapping)
  891. ((_ . (? derivation? replacement))
  892. (derivation-input replacement sub-drvs))
  893. ((_ . (? string? source))
  894. source)
  895. (#f
  896. (derivation-input (loop drv) sub-drvs)))))))
  897. (let loop ((drv drv))
  898. (let* ((inputs (map (cut rewritten-input <> loop)
  899. (derivation-inputs drv)))
  900. (initial (append-map derivation-input-output-paths
  901. (derivation-inputs drv)))
  902. (replacements (append-map input->output-paths inputs))
  903. ;; Sources typically refer to the output directories of the
  904. ;; original inputs, INITIAL. Rewrite them by substituting
  905. ;; REPLACEMENTS.
  906. (sources (map (lambda (source)
  907. (match (vhash-assoc source mapping)
  908. ((_ . replacement)
  909. replacement)
  910. (#f
  911. (substitute-file source
  912. initial replacements))))
  913. (derivation-sources drv)))
  914. ;; Now augment the lists of initials and replacements.
  915. (initial (append (derivation-sources drv) initial))
  916. (replacements (append sources replacements))
  917. (name (store-path-package-name
  918. (string-drop-right (derivation-file-name drv)
  919. 4))))
  920. (derivation store name
  921. (substitute (derivation-builder drv)
  922. initial replacements)
  923. (map (cut substitute <> initial replacements)
  924. (derivation-builder-arguments drv))
  925. #:system system
  926. #:env-vars (map (match-lambda
  927. ((var . value)
  928. `(,var
  929. . ,(substitute value initial
  930. replacements))))
  931. (derivation-builder-environment-vars drv))
  932. #:inputs (filter derivation-input? inputs)
  933. #:sources (append sources (filter string? inputs))
  934. #:outputs (derivation-output-names drv)
  935. #:hash (match (derivation-outputs drv)
  936. ((($ <derivation-output> _ algo hash))
  937. hash)
  938. (_ #f))
  939. #:hash-algo (match (derivation-outputs drv)
  940. ((($ <derivation-output> _ algo hash))
  941. algo)
  942. (_ #f)))))))
  943. ;;;
  944. ;;; Store compatibility layer.
  945. ;;;
  946. (define* (build-derivations store derivations
  947. #:optional (mode (build-mode normal)))
  948. "Build DERIVATIONS, a list of <derivation> or <derivation-input> objects,
  949. .drv file names, or derivation/output pairs, using the specified MODE."
  950. (build-things store (map (match-lambda
  951. ((? derivation? drv)
  952. (derivation-file-name drv))
  953. ((? derivation-input? input)
  954. (cons (derivation-input-path input)
  955. (string-join
  956. (derivation-input-sub-derivations input)
  957. ",")))
  958. ((? string? file) file)
  959. (((? derivation? drv) . output)
  960. (cons (derivation-file-name drv)
  961. output))
  962. (((? string? file) . output)
  963. (cons file output)))
  964. derivations)
  965. mode))
  966. ;;;
  967. ;;; Guile-based builders.
  968. ;;;
  969. (define (parent-directories file-name)
  970. "Return the list of parent dirs of FILE-NAME, in the order in which an
  971. `mkdir -p' implementation would make them."
  972. (let ((not-slash (char-set-complement (char-set #\/))))
  973. (reverse
  974. (fold (lambda (dir result)
  975. (match result
  976. (()
  977. (list dir))
  978. ((prev _ ...)
  979. (cons (string-append prev "/" dir)
  980. result))))
  981. '()
  982. (remove (cut string=? <> ".")
  983. (string-tokenize (dirname file-name) not-slash))))))
  984. (define* (imported-files store files ;deprecated
  985. #:key (name "file-import")
  986. (system (%current-system))
  987. (guile (%guile-for-build)))
  988. "Return a derivation that imports FILES into STORE. FILES must be a list
  989. of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
  990. system, imported, and appears under FINAL-PATH in the resulting store path."
  991. (let* ((files (map (match-lambda
  992. ((final-path . file-name)
  993. (list final-path
  994. (add-to-store store (basename final-path) #f
  995. "sha256" file-name))))
  996. files))
  997. (builder
  998. `(begin
  999. (mkdir %output) (chdir %output)
  1000. ,@(append-map (match-lambda
  1001. ((final-path store-path)
  1002. (append (match (parent-directories final-path)
  1003. (() '())
  1004. ((head ... tail)
  1005. (append (map (lambda (d)
  1006. `(false-if-exception
  1007. (mkdir ,d)))
  1008. head)
  1009. `((or (file-exists? ,tail)
  1010. (mkdir ,tail))))))
  1011. `((symlink ,store-path ,final-path)))))
  1012. files))))
  1013. (build-expression->derivation store name builder
  1014. #:system system
  1015. #:inputs files
  1016. #:guile-for-build guile
  1017. #:local-build? #t)))
  1018. ;; The "file not found" error condition.
  1019. (define-condition-type &file-search-error &error
  1020. file-search-error?
  1021. (file file-search-error-file-name)
  1022. (path file-search-error-search-path))
  1023. (define search-path*
  1024. ;; A memoizing version of 'search-path' so 'imported-modules' does not end
  1025. ;; up looking for the same files over and over again.
  1026. (mlambda (path file)
  1027. "Search for FILE in PATH and memoize the result. Raise a
  1028. '&file-search-error' condition if it could not be found."
  1029. (or (search-path path file)
  1030. (raise (condition
  1031. (&file-search-error (file file)
  1032. (path path)))))))
  1033. (define (module->source-file-name module)
  1034. "Return the file name corresponding to MODULE, a Guile module name (a list
  1035. of symbols.)"
  1036. (string-append (string-join (map symbol->string module) "/")
  1037. ".scm"))
  1038. (define* (%imported-modules store modules ;deprecated
  1039. #:key (name "module-import")
  1040. (system (%current-system))
  1041. (guile (%guile-for-build))
  1042. (module-path %load-path))
  1043. "Return a derivation that contains the source files of MODULES, a list of
  1044. module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
  1045. search path."
  1046. ;; TODO: Determine the closure of MODULES, build the `.go' files,
  1047. ;; canonicalize the source files through read/write, etc.
  1048. (let ((files (map (lambda (m)
  1049. (let ((f (module->source-file-name m)))
  1050. (cons f (search-path* module-path f))))
  1051. modules)))
  1052. (imported-files store files #:name name #:system system
  1053. #:guile guile)))
  1054. (define* (%compiled-modules store modules ;deprecated
  1055. #:key (name "module-import-compiled")
  1056. (system (%current-system))
  1057. (guile (%guile-for-build))
  1058. (module-path %load-path))
  1059. "Return a derivation that builds a tree containing the `.go' files
  1060. corresponding to MODULES. All the MODULES are built in a context where
  1061. they can refer to each other."
  1062. (let* ((module-drv (%imported-modules store modules
  1063. #:system system
  1064. #:guile guile
  1065. #:module-path module-path))
  1066. (module-dir (derivation->output-path module-drv))
  1067. (files (map (lambda (m)
  1068. (let ((f (string-join (map symbol->string m)
  1069. "/")))
  1070. (cons (string-append f ".go")
  1071. (string-append module-dir "/" f ".scm"))))
  1072. modules)))
  1073. (define builder
  1074. `(begin
  1075. (use-modules (system base compile))
  1076. (let ((out (assoc-ref %outputs "out")))
  1077. (mkdir out)
  1078. (chdir out))
  1079. (set! %load-path
  1080. (cons ,module-dir %load-path))
  1081. ,@(map (match-lambda
  1082. ((output . input)
  1083. (let ((make-parent-dirs (map (lambda (dir)
  1084. `(unless (file-exists? ,dir)
  1085. (mkdir ,dir)))
  1086. (parent-directories output))))
  1087. `(begin
  1088. ,@make-parent-dirs
  1089. (compile-file ,input
  1090. #:output-file ,output
  1091. #:opts %auto-compilation-options)))))
  1092. files)))
  1093. (build-expression->derivation store name builder
  1094. #:inputs `(("modules" ,module-drv))
  1095. #:system system
  1096. #:guile-for-build guile
  1097. #:local-build? #t)))
  1098. (define %module-cache
  1099. ;; Map a list of modules to its 'imported+compiled-modules' result.
  1100. (make-hash-table))
  1101. (define* (imported+compiled-modules store modules #:key
  1102. (system (%current-system))
  1103. (guile (%guile-for-build)))
  1104. "Return a pair containing the derivation to import MODULES and that where
  1105. MODULES are compiled."
  1106. (define key
  1107. (list modules (derivation-file-name guile) system))
  1108. (or (hash-ref %module-cache key)
  1109. (let ((result (cons (%imported-modules store modules
  1110. #:system system #:guile guile)
  1111. (%compiled-modules store modules
  1112. #:system system #:guile guile))))
  1113. (hash-set! %module-cache key result)
  1114. result)))
  1115. (define* (build-expression->derivation store name exp ;deprecated
  1116. #:key
  1117. (system (%current-system))
  1118. (inputs '())
  1119. (outputs '("out"))
  1120. hash hash-algo recursive?
  1121. (env-vars '())
  1122. (modules '())
  1123. guile-for-build
  1124. references-graphs
  1125. allowed-references
  1126. disallowed-references
  1127. local-build? (substitutable? #t)
  1128. (properties '()))
  1129. "Return a derivation that executes Scheme expression EXP as a builder
  1130. for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
  1131. tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
  1132. of names of Guile modules from the current search path to be copied in
  1133. the store, compiled, and made available in the load path during the
  1134. execution of EXP.
  1135. EXP is evaluated in an environment where %OUTPUT is bound to the main
  1136. output path, %OUTPUTS is bound to a list of output/path pairs, and where
  1137. %BUILD-INPUTS is bound to an alist of string/output-path pairs made from
  1138. INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
  1139. name and value of environment variables visible to the builder. The
  1140. builder terminates by passing the result of EXP to `exit'; thus, when
  1141. EXP returns #f, the build is considered to have failed.
  1142. EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
  1143. omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
  1144. See the `derivation' procedure for the meaning of REFERENCES-GRAPHS,
  1145. ALLOWED-REFERENCES, DISALLOWED-REFERENCES, LOCAL-BUILD?, SUBSTITUTABLE?,
  1146. and PROPERTIES."
  1147. (define guile-drv
  1148. (or guile-for-build (%guile-for-build)))
  1149. (define guile
  1150. (string-append (derivation->output-path guile-drv)
  1151. "/bin/guile"))
  1152. (define module-form?
  1153. (match-lambda
  1154. (((or 'define-module 'use-modules) _ ...) #t)
  1155. (_ #f)))
  1156. (define source-path
  1157. ;; When passed an input that is a source, return its path; otherwise
  1158. ;; return #f.
  1159. (match-lambda
  1160. ((_ (? derivation?) _ ...)
  1161. #f)
  1162. ((_ path _ ...)
  1163. (and (not (derivation-path? path))
  1164. path))))
  1165. (let* ((prologue `(begin
  1166. ,@(match exp
  1167. ((_ ...)
  1168. ;; Module forms must appear at the top-level so
  1169. ;; that any macros they export can be expanded.
  1170. (filter module-form? exp))
  1171. (_ `(,exp)))
  1172. (define %output (getenv "out"))
  1173. (define %outputs
  1174. (map (lambda (o)
  1175. (cons o (getenv o)))
  1176. ',outputs))
  1177. (define %build-inputs
  1178. ',(map (match-lambda
  1179. ((name drv . rest)
  1180. (let ((sub (match rest
  1181. (() "out")
  1182. ((x) x))))
  1183. (cons name
  1184. (cond
  1185. ((derivation? drv)
  1186. (derivation->output-path drv sub))
  1187. ((derivation-path? drv)
  1188. (derivation-path->output-path drv
  1189. sub))
  1190. (else drv))))))
  1191. inputs))
  1192. ,@(if (null? modules)
  1193. '()
  1194. ;; Remove our own settings.
  1195. '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
  1196. ;; Guile sets it, but remove it to avoid conflicts when
  1197. ;; building Guile-using packages.
  1198. (unsetenv "LD_LIBRARY_PATH")))
  1199. (builder (add-text-to-store store
  1200. (string-append name "-guile-builder")
  1201. ;; Explicitly use UTF-8 for determinism,
  1202. ;; and also because UTF-8 output is faster.
  1203. (with-fluids ((%default-port-encoding
  1204. "UTF-8"))
  1205. (call-with-output-string
  1206. (lambda (port)
  1207. (write prologue port)
  1208. (write
  1209. `(exit
  1210. ,(match exp
  1211. ((_ ...)
  1212. (remove module-form? exp))
  1213. (_ `(,exp))))
  1214. port))))
  1215. ;; The references don't really matter
  1216. ;; since the builder is always used in
  1217. ;; conjunction with the drv that needs
  1218. ;; it. For clarity, we add references
  1219. ;; to the subset of INPUTS that are
  1220. ;; sources, avoiding references to other
  1221. ;; .drv; otherwise, BUILDER's hash would
  1222. ;; depend on those, even if they are
  1223. ;; fixed-output.
  1224. (filter-map source-path inputs)))
  1225. (mod+go-drv (if (pair? modules)
  1226. (imported+compiled-modules store modules
  1227. #:guile guile-drv
  1228. #:system system)
  1229. '(#f . #f)))
  1230. (mod-drv (car mod+go-drv))
  1231. (go-drv (cdr mod+go-drv))
  1232. (mod-dir (and mod-drv
  1233. (derivation->output-path mod-drv)))
  1234. (go-dir (and go-drv
  1235. (derivation->output-path go-drv))))
  1236. (derivation store name guile
  1237. `("--no-auto-compile"
  1238. ,@(if mod-dir `("-L" ,mod-dir) '())
  1239. ,builder)
  1240. ;; 'build-expression->derivation' is somewhat deprecated so
  1241. ;; don't bother warning here.
  1242. #:%deprecation-warning? #f
  1243. #:system system
  1244. #:inputs `((,(or guile-for-build (%guile-for-build)))
  1245. (,builder)
  1246. ,@(map cdr inputs)
  1247. ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
  1248. ;; When MODULES is non-empty, shamelessly clobber
  1249. ;; $GUILE_LOAD_COMPILED_PATH.
  1250. #:env-vars (if go-dir
  1251. `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
  1252. ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
  1253. env-vars))
  1254. env-vars)
  1255. #:hash hash #:hash-algo hash-algo
  1256. #:recursive? recursive?
  1257. #:outputs outputs
  1258. #:references-graphs references-graphs
  1259. #:allowed-references allowed-references
  1260. #:disallowed-references disallowed-references
  1261. #:local-build? local-build?
  1262. #:substitutable? substitutable?
  1263. #:properties properties)))
  1264. ;;;
  1265. ;;; Monadic interface.
  1266. ;;;
  1267. (define built-derivations
  1268. (store-lift build-derivations))
  1269. (define raw-derivation
  1270. (store-lift derivation))