derivations.scm 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. (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-deprecated (build-expression->derivation store name exp
  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. gexp->derivation ;unbound, but that's okay
  1127. "Return a derivation that executes Scheme expression EXP as a builder
  1128. for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
  1129. tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
  1130. of names of Guile modules from the current search path to be copied in
  1131. the store, compiled, and made available in the load path during the
  1132. execution of EXP.
  1133. EXP is evaluated in an environment where %OUTPUT is bound to the main
  1134. output path, %OUTPUTS is bound to a list of output/path pairs, and where
  1135. %BUILD-INPUTS is bound to an alist of string/output-path pairs made from
  1136. INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
  1137. name and value of environment variables visible to the builder. The
  1138. builder terminates by passing the result of EXP to `exit'; thus, when
  1139. EXP returns #f, the build is considered to have failed.
  1140. EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
  1141. omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
  1142. See the `derivation' procedure for the meaning of REFERENCES-GRAPHS,
  1143. ALLOWED-REFERENCES, DISALLOWED-REFERENCES, LOCAL-BUILD?, SUBSTITUTABLE?,
  1144. and PROPERTIES."
  1145. (define guile-drv
  1146. (or guile-for-build (%guile-for-build)))
  1147. (define guile
  1148. (string-append (derivation->output-path guile-drv)
  1149. "/bin/guile"))
  1150. (define module-form?
  1151. (match-lambda
  1152. (((or 'define-module 'use-modules) _ ...) #t)
  1153. (_ #f)))
  1154. (define source-path
  1155. ;; When passed an input that is a source, return its path; otherwise
  1156. ;; return #f.
  1157. (match-lambda
  1158. ((_ (? derivation?) _ ...)
  1159. #f)
  1160. ((_ path _ ...)
  1161. (and (not (derivation-path? path))
  1162. path))))
  1163. (let* ((prologue `(begin
  1164. ,@(match exp
  1165. ((_ ...)
  1166. ;; Module forms must appear at the top-level so
  1167. ;; that any macros they export can be expanded.
  1168. (filter module-form? exp))
  1169. (_ `(,exp)))
  1170. (define %output (getenv "out"))
  1171. (define %outputs
  1172. (map (lambda (o)
  1173. (cons o (getenv o)))
  1174. ',outputs))
  1175. (define %build-inputs
  1176. ',(map (match-lambda
  1177. ((name drv . rest)
  1178. (let ((sub (match rest
  1179. (() "out")
  1180. ((x) x))))
  1181. (cons name
  1182. (cond
  1183. ((derivation? drv)
  1184. (derivation->output-path drv sub))
  1185. ((derivation-path? drv)
  1186. (derivation-path->output-path drv
  1187. sub))
  1188. (else drv))))))
  1189. inputs))
  1190. ,@(if (null? modules)
  1191. '()
  1192. ;; Remove our own settings.
  1193. '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
  1194. ;; Guile sets it, but remove it to avoid conflicts when
  1195. ;; building Guile-using packages.
  1196. (unsetenv "LD_LIBRARY_PATH")))
  1197. (builder (add-text-to-store store
  1198. (string-append name "-guile-builder")
  1199. ;; Explicitly use UTF-8 for determinism,
  1200. ;; and also because UTF-8 output is faster.
  1201. (with-fluids ((%default-port-encoding
  1202. "UTF-8"))
  1203. (call-with-output-string
  1204. (lambda (port)
  1205. (write prologue port)
  1206. (write
  1207. `(exit
  1208. ,(match exp
  1209. ((_ ...)
  1210. (remove module-form? exp))
  1211. (_ `(,exp))))
  1212. port))))
  1213. ;; The references don't really matter
  1214. ;; since the builder is always used in
  1215. ;; conjunction with the drv that needs
  1216. ;; it. For clarity, we add references
  1217. ;; to the subset of INPUTS that are
  1218. ;; sources, avoiding references to other
  1219. ;; .drv; otherwise, BUILDER's hash would
  1220. ;; depend on those, even if they are
  1221. ;; fixed-output.
  1222. (filter-map source-path inputs)))
  1223. (mod+go-drv (if (pair? modules)
  1224. (imported+compiled-modules store modules
  1225. #:guile guile-drv
  1226. #:system system)
  1227. '(#f . #f)))
  1228. (mod-dir (car mod+go-drv))
  1229. (go-drv (cdr mod+go-drv))
  1230. (go-dir (and go-drv
  1231. (derivation->output-path go-drv))))
  1232. (derivation store name guile
  1233. `("--no-auto-compile"
  1234. ,@(if mod-dir `("-L" ,mod-dir) '())
  1235. ,builder)
  1236. ;; 'build-expression->derivation' is somewhat deprecated so
  1237. ;; don't bother warning here.
  1238. #:%deprecation-warning? #f
  1239. #:system system
  1240. #:inputs `((,(or guile-for-build (%guile-for-build)))
  1241. (,builder)
  1242. ,@(map cdr inputs)
  1243. ,@(if mod-dir `((,mod-dir) (,go-drv)) '()))
  1244. ;; When MODULES is non-empty, shamelessly clobber
  1245. ;; $GUILE_LOAD_COMPILED_PATH.
  1246. #:env-vars (if go-dir
  1247. `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
  1248. ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
  1249. env-vars))
  1250. env-vars)
  1251. #:hash hash #:hash-algo hash-algo
  1252. #:recursive? recursive?
  1253. #:outputs outputs
  1254. #:references-graphs references-graphs
  1255. #:allowed-references allowed-references
  1256. #:disallowed-references disallowed-references
  1257. #:local-build? local-build?
  1258. #:substitutable? substitutable?
  1259. #:properties properties)))
  1260. ;;;
  1261. ;;; Monadic interface.
  1262. ;;;
  1263. (define built-derivations
  1264. (store-lift build-derivations))
  1265. (define raw-derivation
  1266. (store-lift derivation))