derivations.scm 58 KB

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