derivations.scm 58 KB

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