derivations.scm 58 KB

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