gexp.scm 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  4. ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix derivations)
  26. #:use-module (guix grafts)
  27. #:use-module (guix utils)
  28. #:use-module (guix diagnostics)
  29. #:use-module (guix i18n)
  30. #:use-module (rnrs bytevectors)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-9)
  33. #:use-module (srfi srfi-9 gnu)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:use-module (ice-9 format)
  38. #:use-module (ice-9 match)
  39. #:export (gexp
  40. gexp?
  41. with-imported-modules
  42. with-extensions
  43. let-system
  44. gexp-input
  45. gexp-input?
  46. gexp-input-thing
  47. gexp-input-output
  48. gexp-input-native?
  49. assume-valid-file-name
  50. local-file
  51. local-file?
  52. local-file-file
  53. local-file-absolute-file-name
  54. local-file-name
  55. local-file-recursive?
  56. local-file-select?
  57. plain-file
  58. plain-file?
  59. plain-file-name
  60. plain-file-content
  61. computed-file
  62. computed-file?
  63. computed-file-name
  64. computed-file-gexp
  65. computed-file-options
  66. program-file
  67. program-file?
  68. program-file-name
  69. program-file-gexp
  70. program-file-guile
  71. program-file-module-path
  72. scheme-file
  73. scheme-file?
  74. scheme-file-name
  75. scheme-file-gexp
  76. file-append
  77. file-append?
  78. file-append-base
  79. file-append-suffix
  80. raw-derivation-file
  81. raw-derivation-file?
  82. with-parameters
  83. parameterized?
  84. load-path-expression
  85. gexp-modules
  86. lower-gexp
  87. lowered-gexp?
  88. lowered-gexp-sexp
  89. lowered-gexp-inputs
  90. lowered-gexp-sources
  91. lowered-gexp-guile
  92. lowered-gexp-load-path
  93. lowered-gexp-load-compiled-path
  94. gexp->derivation
  95. gexp->file
  96. gexp->script
  97. text-file*
  98. mixed-text-file
  99. file-union
  100. directory-union
  101. imported-files
  102. imported-modules
  103. compiled-modules
  104. define-gexp-compiler
  105. gexp-compiler?
  106. file-like?
  107. lower-object
  108. &gexp-error
  109. gexp-error?
  110. &gexp-input-error
  111. gexp-input-error?
  112. gexp-error-invalid-input))
  113. ;;; Commentary:
  114. ;;;
  115. ;;; This module implements "G-expressions", or "gexps". Gexps are like
  116. ;;; S-expressions (sexps), with two differences:
  117. ;;;
  118. ;;; 1. References (un-quotations) to derivations or packages in a gexp are
  119. ;;; replaced by the corresponding output file name; in addition, the
  120. ;;; 'ungexp-native' unquote-like form allows code to explicitly refer to
  121. ;;; the native code of a given package, in case of cross-compilation;
  122. ;;;
  123. ;;; 2. Gexps embed information about the derivations they refer to.
  124. ;;;
  125. ;;; Gexps make it easy to write to files Scheme code that refers to store
  126. ;;; items, or to write Scheme code to build derivations.
  127. ;;;
  128. ;;; Code:
  129. ;; "G expressions".
  130. (define-record-type <gexp>
  131. (make-gexp references modules extensions proc location)
  132. gexp?
  133. (references gexp-references) ;list of <gexp-input>
  134. (modules gexp-self-modules) ;list of module names
  135. (extensions gexp-self-extensions) ;list of lowerable things
  136. (proc gexp-proc) ;procedure
  137. (location %gexp-location)) ;location alist
  138. (define (gexp-location gexp)
  139. "Return the source code location of GEXP."
  140. (and=> (%gexp-location gexp) source-properties->location))
  141. (define (write-gexp gexp port)
  142. "Write GEXP on PORT."
  143. (display "#<gexp " port)
  144. ;; Try to write the underlying sexp. Now, this trick doesn't work when
  145. ;; doing things like (ungexp-splicing (gexp ())) because GEXP's procedure
  146. ;; tries to use 'append' on that, which fails with wrong-type-arg.
  147. (false-if-exception
  148. (write (apply (gexp-proc gexp)
  149. (gexp-references gexp))
  150. port))
  151. (let ((loc (gexp-location gexp)))
  152. (when loc
  153. (format port " ~a" (location->string loc))))
  154. (format port " ~a>"
  155. (number->string (object-address gexp) 16)))
  156. (set-record-type-printer! <gexp> write-gexp)
  157. ;;;
  158. ;;; Methods.
  159. ;;;
  160. ;; Compiler for a type of objects that may be introduced in a gexp.
  161. (define-record-type <gexp-compiler>
  162. (gexp-compiler type lower expand)
  163. gexp-compiler?
  164. (type gexp-compiler-type) ;record type descriptor
  165. (lower gexp-compiler-lower)
  166. (expand gexp-compiler-expand)) ;#f | DRV -> sexp
  167. (define-condition-type &gexp-error &error
  168. gexp-error?)
  169. (define-condition-type &gexp-input-error &gexp-error
  170. gexp-input-error?
  171. (input gexp-error-invalid-input))
  172. (define %gexp-compilers
  173. ;; 'eq?' mapping of record type descriptor to <gexp-compiler>.
  174. (make-hash-table 20))
  175. (define (default-expander thing obj output)
  176. "This is the default expander for \"things\" that appear in gexps. It
  177. returns its output file name of OBJ's OUTPUT."
  178. (match obj
  179. ((? derivation? drv)
  180. (derivation->output-path drv output))
  181. ((? string? file)
  182. file)
  183. ((? self-quoting? obj)
  184. obj)))
  185. (define (register-compiler! compiler)
  186. "Register COMPILER as a gexp compiler."
  187. (hashq-set! %gexp-compilers
  188. (gexp-compiler-type compiler) compiler))
  189. (define (lookup-compiler object)
  190. "Search for a compiler for OBJECT. Upon success, return the three argument
  191. procedure to lower it; otherwise return #f."
  192. (and=> (hashq-ref %gexp-compilers (struct-vtable object))
  193. gexp-compiler-lower))
  194. (define (file-like? object)
  195. "Return #t if OBJECT leads to a file in the store once unquoted in a
  196. G-expression; otherwise return #f."
  197. (and (struct? object) (->bool (lookup-compiler object))))
  198. (define (lookup-expander object)
  199. "Search for an expander for OBJECT. Upon success, return the three argument
  200. procedure to expand it; otherwise return #f."
  201. (and=> (hashq-ref %gexp-compilers (struct-vtable object))
  202. gexp-compiler-expand))
  203. (define* (lower-object obj
  204. #:optional (system (%current-system))
  205. #:key (target 'current))
  206. "Return as a value in %STORE-MONAD the derivation or store item
  207. corresponding to OBJ for SYSTEM, cross-compiling for TARGET if TARGET is true.
  208. OBJ must be an object that has an associated gexp compiler, such as a
  209. <package>."
  210. (mlet %store-monad ((target (if (eq? target 'current)
  211. (current-target-system)
  212. (return target)))
  213. (graft? (grafting?)))
  214. (let loop ((obj obj))
  215. (match (lookup-compiler obj)
  216. (#f
  217. (raise (condition (&gexp-input-error (input obj)))))
  218. (lower
  219. ;; Cache in STORE the result of lowering OBJ.
  220. (mcached (mlet %store-monad ((lowered (lower obj system target)))
  221. (if (and (struct? lowered)
  222. (not (derivation? lowered)))
  223. (loop lowered)
  224. (return lowered)))
  225. obj
  226. system target graft?))))))
  227. (define* (lower+expand-object obj
  228. #:optional (system (%current-system))
  229. #:key target (output "out"))
  230. "Return as a value in %STORE-MONAD the output of object OBJ expands to for
  231. SYSTEM and TARGET. Object such as <package>, <file-append>, or <plain-file>
  232. expand to file names, but it's possible to expand to a plain data type."
  233. (let loop ((obj obj)
  234. (expand (and (struct? obj) (lookup-expander obj))))
  235. (match (lookup-compiler obj)
  236. (#f
  237. (raise (condition (&gexp-input-error (input obj)))))
  238. (lower
  239. (mlet* %store-monad ((graft? (grafting?))
  240. (lowered (mcached (lower obj system target)
  241. obj
  242. system target graft?)))
  243. ;; LOWER might return something that needs to be further
  244. ;; lowered.
  245. (if (struct? lowered)
  246. ;; If we lack an expander, delegate to that of LOWERED.
  247. (if (not expand)
  248. (loop lowered (lookup-expander lowered))
  249. (return (expand obj lowered output)))
  250. (if (not expand) ;self-quoting
  251. (return lowered)
  252. (return (expand obj lowered output)))))))))
  253. (define-syntax define-gexp-compiler
  254. (syntax-rules (=> compiler expander)
  255. "Define NAME as a compiler for objects matching PREDICATE encountered in
  256. gexps.
  257. In the simplest form of the macro, BODY must return (1) a derivation for
  258. a record of the specified type, for SYSTEM and TARGET (the latter of which is
  259. #f except when cross-compiling), (2) another record that can itself be
  260. compiled down to a derivation, or (3) an object of a primitive data type.
  261. The more elaborate form allows you to specify an expander:
  262. (define-gexp-compiler something-compiler <something>
  263. compiler => (lambda (param system target) ...)
  264. expander => (lambda (param drv output) ...))
  265. The expander specifies how an object is converted to its sexp representation."
  266. ((_ (name (param record-type) system target) body ...)
  267. (define-gexp-compiler name record-type
  268. compiler => (lambda (param system target) body ...)
  269. expander => default-expander))
  270. ((_ name record-type
  271. compiler => compile
  272. expander => expand)
  273. (begin
  274. (define name
  275. (gexp-compiler record-type compile expand))
  276. (register-compiler! name)))))
  277. (define-gexp-compiler (derivation-compiler (drv <derivation>) system target)
  278. ;; Derivations are the lowest-level representation, so this is the identity
  279. ;; compiler.
  280. (with-monad %store-monad
  281. (return drv)))
  282. ;; Expand to a raw ".drv" file for the lowerable object it wraps. In other
  283. ;; words, this gives the raw ".drv" file instead of its build result.
  284. (define-record-type <raw-derivation-file>
  285. (raw-derivation-file obj)
  286. raw-derivation-file?
  287. (obj raw-derivation-file-object)) ;lowerable object
  288. (define-gexp-compiler raw-derivation-file-compiler <raw-derivation-file>
  289. compiler => (lambda (obj system target)
  290. (mlet %store-monad ((obj (lower-object
  291. (raw-derivation-file-object obj)
  292. system #:target target)))
  293. ;; Returning the .drv file name instead of the <derivation>
  294. ;; record ensures that 'lower-gexp' will classify it as a
  295. ;; "source" and not as an "input".
  296. (return (if (derivation? obj)
  297. (derivation-file-name obj)
  298. obj))))
  299. expander => (lambda (obj lowered output)
  300. (if (derivation? lowered)
  301. (derivation-file-name lowered)
  302. lowered)))
  303. ;;;
  304. ;;; System dependencies.
  305. ;;;
  306. ;; Binding form for the current system and cross-compilation target.
  307. (define-record-type <system-binding>
  308. (system-binding proc)
  309. system-binding?
  310. (proc system-binding-proc))
  311. (define-syntax let-system
  312. (syntax-rules ()
  313. "Introduce a system binding in a gexp. The simplest form is:
  314. (let-system system
  315. (cond ((string=? system \"x86_64-linux\") ...)
  316. (else ...)))
  317. which binds SYSTEM to the currently targeted system. The second form is
  318. similar, but it also shows the cross-compilation target:
  319. (let-system (system target)
  320. ...)
  321. Here TARGET is bound to the cross-compilation triplet or #f."
  322. ((_ (system target) exp0 exp ...)
  323. (system-binding (lambda (system target)
  324. exp0 exp ...)))
  325. ((_ system exp0 exp ...)
  326. (system-binding (lambda (system target)
  327. exp0 exp ...)))))
  328. (define-gexp-compiler system-binding-compiler <system-binding>
  329. compiler => (lambda (binding system target)
  330. (match binding
  331. (($ <system-binding> proc)
  332. (with-monad %store-monad
  333. ;; PROC is expected to return a lowerable object.
  334. ;; 'lower-object' takes care of residualizing it to a
  335. ;; derivation or similar.
  336. (return (proc system target))))))
  337. ;; Delegate to the expander of the object returned by PROC.
  338. expander => #f)
  339. ;;;
  340. ;;; File declarations.
  341. ;;;
  342. ;; A local file name. FILE is the file name the user entered, which can be a
  343. ;; relative file name, and ABSOLUTE is a promise that computes its canonical
  344. ;; absolute file name. We keep it in a promise to compute it lazily and avoid
  345. ;; repeated 'stat' calls.
  346. (define-record-type <local-file>
  347. (%%local-file file absolute name recursive? select?)
  348. local-file?
  349. (file local-file-file) ;string
  350. (absolute %local-file-absolute-file-name) ;promise string
  351. (name local-file-name) ;string
  352. (recursive? local-file-recursive?) ;Boolean
  353. (select? local-file-select?)) ;string stat -> Boolean
  354. (define (true file stat) #t)
  355. (define* (%local-file file promise #:optional (name (basename file))
  356. #:key
  357. (literal? #t) location
  358. recursive? (select? true))
  359. ;; This intermediate procedure is part of our ABI, but the underlying
  360. ;; %%LOCAL-FILE is not.
  361. (when (and (not literal?) (not (string-prefix? "/" file)))
  362. (warning (and=> location source-properties->location)
  363. (G_ "resolving '~a' relative to current directory~%")
  364. file))
  365. (%%local-file file promise name recursive? select?))
  366. (define (absolute-file-name file directory)
  367. "Return the canonical absolute file name for FILE, which lives in the
  368. vicinity of DIRECTORY."
  369. (canonicalize-path
  370. (cond ((string-prefix? "/" file) file)
  371. ((not directory) file)
  372. ((string-prefix? "/" directory)
  373. (string-append directory "/" file))
  374. (else file))))
  375. (define-syntax-rule (assume-valid-file-name file)
  376. "This is a syntactic keyword to tell 'local-file' that it can assume that
  377. the given file name is valid, even if it's not a string literal, and thus not
  378. warn about it."
  379. file)
  380. (define-syntax local-file
  381. (lambda (s)
  382. "Return an object representing local file FILE to add to the store; this
  383. object can be used in a gexp. If FILE is a relative file name, it is looked
  384. up relative to the source file where this form appears. FILE will be added to
  385. the store under NAME--by default the base name of FILE.
  386. When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
  387. designates a flat file and RECURSIVE? is true, its contents are added, and its
  388. permission bits are kept.
  389. When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
  390. where FILE is the entry's absolute file name and STAT is the result of
  391. 'lstat'; exclude entries for which SELECT? does not return true.
  392. This is the declarative counterpart of the 'interned-file' monadic procedure.
  393. It is implemented as a macro to capture the current source directory where it
  394. appears."
  395. (syntax-case s (assume-valid-file-name)
  396. ((_ file rest ...)
  397. (string? (syntax->datum #'file))
  398. ;; FILE is a literal, so resolve it relative to the source directory.
  399. #'(%local-file file
  400. (delay (absolute-file-name file (current-source-directory)))
  401. rest ...))
  402. ((_ (assume-valid-file-name file) rest ...)
  403. ;; FILE is not a literal, so resolve it relative to the current
  404. ;; directory. Since the user declared FILE is valid, do not pass
  405. ;; #:literal? #f so that we do not warn about it later on.
  406. #'(%local-file file
  407. (delay (absolute-file-name file (getcwd)))
  408. rest ...))
  409. ((_ file rest ...)
  410. ;; Resolve FILE relative to the current directory.
  411. (with-syntax ((location (datum->syntax s (syntax-source s))))
  412. #`(%local-file file
  413. (delay (absolute-file-name file (getcwd)))
  414. rest ...
  415. #:location 'location
  416. #:literal? #f))) ;warn if FILE is relative
  417. ((_)
  418. #'(syntax-error "missing file name"))
  419. (id
  420. (identifier? #'id)
  421. ;; XXX: We could return #'(lambda (file . rest) ...). However,
  422. ;; (syntax-source #'id) is #f so (current-source-directory) would not
  423. ;; work. Thus, simply forbid this form.
  424. #'(syntax-error
  425. "'local-file' is a macro and cannot be used like this")))))
  426. (define (local-file-absolute-file-name file)
  427. "Return the absolute file name for FILE, a <local-file> instance. A
  428. 'system-error' exception is raised if FILE could not be found."
  429. (force (%local-file-absolute-file-name file)))
  430. (define-gexp-compiler (local-file-compiler (file <local-file>) system target)
  431. ;; "Compile" FILE by adding it to the store.
  432. (match file
  433. (($ <local-file> file (= force absolute) name recursive? select?)
  434. ;; Canonicalize FILE so that if it's a symlink, it is resolved. Failing
  435. ;; to do that, when RECURSIVE? is #t, we could end up creating a dangling
  436. ;; symlink in the store, and when RECURSIVE? is #f 'add-to-store' would
  437. ;; just throw an error, both of which are inconvenient.
  438. (interned-file absolute name
  439. #:recursive? recursive? #:select? select?))))
  440. (define-record-type <plain-file>
  441. (%plain-file name content references)
  442. plain-file?
  443. (name plain-file-name) ;string
  444. (content plain-file-content) ;string or bytevector
  445. (references plain-file-references)) ;list (currently unused)
  446. (define (plain-file name content)
  447. "Return an object representing a text file called NAME with the given
  448. CONTENT (a string) to be added to the store.
  449. This is the declarative counterpart of 'text-file'."
  450. ;; XXX: For now just ignore 'references' because it's not clear how to use
  451. ;; them in a declarative context.
  452. (%plain-file name content '()))
  453. (define-gexp-compiler (plain-file-compiler (file <plain-file>) system target)
  454. ;; "Compile" FILE by adding it to the store.
  455. (match file
  456. (($ <plain-file> name (and (? string?) content) references)
  457. (text-file name content references))
  458. (($ <plain-file> name (and (? bytevector?) content) references)
  459. (binary-file name content references))))
  460. (define-record-type <computed-file>
  461. (%computed-file name gexp guile options)
  462. computed-file?
  463. (name computed-file-name) ;string
  464. (gexp computed-file-gexp) ;gexp
  465. (guile computed-file-guile) ;<package>
  466. (options computed-file-options)) ;list of arguments
  467. (define* (computed-file name gexp
  468. #:key guile (local-build? #t) (options '()))
  469. "Return an object representing the store item NAME, a file or directory
  470. computed by GEXP. When LOCAL-BUILD? is #t (the default), it ensures the
  471. corresponding derivation is built locally. OPTIONS may be used to pass
  472. additional arguments to 'gexp->derivation'.
  473. This is the declarative counterpart of 'gexp->derivation'."
  474. (let ((options* `(#:local-build? ,local-build? ,@options)))
  475. (%computed-file name gexp guile options*)))
  476. (define-gexp-compiler (computed-file-compiler (file <computed-file>)
  477. system target)
  478. ;; Compile FILE by returning a derivation whose build expression is its
  479. ;; gexp.
  480. (match file
  481. (($ <computed-file> name gexp guile options)
  482. (if guile
  483. (mlet %store-monad ((guile (lower-object guile system
  484. #:target target)))
  485. (apply gexp->derivation name gexp #:guile-for-build guile
  486. #:system system #:target target options))
  487. (apply gexp->derivation name gexp
  488. #:system system #:target target options)))))
  489. (define-record-type <program-file>
  490. (%program-file name gexp guile path)
  491. program-file?
  492. (name program-file-name) ;string
  493. (gexp program-file-gexp) ;gexp
  494. (guile program-file-guile) ;package
  495. (path program-file-module-path)) ;list of strings
  496. (define* (program-file name gexp #:key (guile #f) (module-path %load-path))
  497. "Return an object representing the executable store item NAME that runs
  498. GEXP. GUILE is the Guile package used to execute that script. Imported
  499. modules of GEXP are looked up in MODULE-PATH.
  500. This is the declarative counterpart of 'gexp->script'."
  501. (%program-file name gexp guile module-path))
  502. (define-gexp-compiler (program-file-compiler (file <program-file>)
  503. system target)
  504. ;; Compile FILE by returning a derivation that builds the script.
  505. (match file
  506. (($ <program-file> name gexp guile module-path)
  507. (gexp->script name gexp
  508. #:module-path module-path
  509. #:guile (or guile (default-guile))
  510. #:system system
  511. #:target target))))
  512. (define-record-type <scheme-file>
  513. (%scheme-file name gexp splice? load-path?)
  514. scheme-file?
  515. (name scheme-file-name) ;string
  516. (gexp scheme-file-gexp) ;gexp
  517. (splice? scheme-file-splice?) ;Boolean
  518. (load-path? scheme-file-set-load-path?)) ;Boolean
  519. (define* (scheme-file name gexp #:key splice? (set-load-path? #t))
  520. "Return an object representing the Scheme file NAME that contains GEXP.
  521. This is the declarative counterpart of 'gexp->file'."
  522. (%scheme-file name gexp splice? set-load-path?))
  523. (define-gexp-compiler (scheme-file-compiler (file <scheme-file>)
  524. system target)
  525. ;; Compile FILE by returning a derivation that builds the file.
  526. (match file
  527. (($ <scheme-file> name gexp splice? set-load-path?)
  528. (gexp->file name gexp
  529. #:set-load-path? set-load-path?
  530. #:splice? splice?
  531. #:system system
  532. #:target target))))
  533. ;; Appending SUFFIX to BASE's output file name.
  534. (define-record-type <file-append>
  535. (%file-append base suffix)
  536. file-append?
  537. (base file-append-base) ;<package> | <derivation> | ...
  538. (suffix file-append-suffix)) ;list of strings
  539. (define (write-file-append file port)
  540. (match file
  541. (($ <file-append> base suffix)
  542. (format port "#<file-append ~s ~s>" base
  543. (string-join suffix)))))
  544. (set-record-type-printer! <file-append> write-file-append)
  545. (define (file-append base . suffix)
  546. "Return a <file-append> object that expands to the concatenation of BASE and
  547. SUFFIX."
  548. (%file-append base suffix))
  549. (define-gexp-compiler file-append-compiler <file-append>
  550. compiler => (lambda (obj system target)
  551. (match obj
  552. (($ <file-append> base _)
  553. (lower-object base system #:target target))))
  554. expander => (lambda (obj lowered output)
  555. (match obj
  556. (($ <file-append> base suffix)
  557. (let* ((expand (lookup-expander base))
  558. (base (expand base lowered output)))
  559. (string-append base (string-concatenate suffix)))))))
  560. ;; Representation of SRFI-39 parameter settings in the dynamic scope of an
  561. ;; object lowering.
  562. (define-record-type <parameterized>
  563. (parameterized bindings thunk)
  564. parameterized?
  565. (bindings parameterized-bindings) ;list of parameter/value pairs
  566. (thunk parameterized-thunk)) ;thunk
  567. (define-syntax-rule (with-parameters ((param value) ...) body ...)
  568. "Bind each PARAM to the corresponding VALUE for the extent during which BODY
  569. is lowered. Consider this example:
  570. (with-parameters ((%current-system \"x86_64-linux\"))
  571. coreutils)
  572. It returns a <parameterized> object that ensures %CURRENT-SYSTEM is set to
  573. x86_64-linux when COREUTILS is lowered."
  574. (parameterized (list (list param (lambda () value)) ...)
  575. (lambda ()
  576. body ...)))
  577. (define-gexp-compiler compile-parameterized <parameterized>
  578. compiler =>
  579. (lambda (parameterized system target)
  580. (match (parameterized-bindings parameterized)
  581. (((parameters values) ...)
  582. (let ((fluids (map parameter-fluid parameters))
  583. (thunk (parameterized-thunk parameterized)))
  584. ;; Install the PARAMETERS for the dynamic extent of THUNK.
  585. (with-fluids* fluids
  586. (map (lambda (thunk) (thunk)) values)
  587. (lambda ()
  588. ;; Special-case '%current-system' and '%current-target-system' to
  589. ;; make sure we get the desired effect.
  590. (let ((system (if (memq %current-system parameters)
  591. (%current-system)
  592. system))
  593. (target (if (memq %current-target-system parameters)
  594. (%current-target-system)
  595. target)))
  596. (lower-object (thunk) system #:target target))))))))
  597. expander => (lambda (parameterized lowered output)
  598. (match (parameterized-bindings parameterized)
  599. (((parameters values) ...)
  600. (let ((fluids (map parameter-fluid parameters))
  601. (thunk (parameterized-thunk parameterized)))
  602. ;; Install the PARAMETERS for the dynamic extent of THUNK.
  603. (with-fluids* fluids
  604. (map (lambda (thunk) (thunk)) values)
  605. (lambda ()
  606. ;; Delegate to the expander of the wrapped object.
  607. (let* ((base (thunk))
  608. (expand (lookup-expander base)))
  609. (expand base lowered output)))))))))
  610. ;;;
  611. ;;; Inputs & outputs.
  612. ;;;
  613. ;; The input of a gexp.
  614. (define-record-type <gexp-input>
  615. (%gexp-input thing output native?)
  616. gexp-input?
  617. (thing gexp-input-thing) ;<package> | <origin> | <derivation> | ...
  618. (output gexp-input-output) ;string
  619. (native? gexp-input-native?)) ;Boolean
  620. (define (write-gexp-input input port)
  621. (match input
  622. (($ <gexp-input> thing output #f)
  623. (format port "#<gexp-input ~s:~a>" thing output))
  624. (($ <gexp-input> thing output #t)
  625. (format port "#<gexp-input native ~s:~a>" thing output))))
  626. (set-record-type-printer! <gexp-input> write-gexp-input)
  627. (define* (gexp-input thing ;convenience procedure
  628. #:optional (output "out")
  629. #:key native?)
  630. "Return a new <gexp-input> for the OUTPUT of THING; NATIVE? determines
  631. whether this should be considered a \"native\" input or not."
  632. (%gexp-input thing output native?))
  633. ;; Reference to one of the derivation's outputs, for gexps used in
  634. ;; derivations.
  635. (define-record-type <gexp-output>
  636. (gexp-output name)
  637. gexp-output?
  638. (name gexp-output-name))
  639. (define (write-gexp-output output port)
  640. (match output
  641. (($ <gexp-output> name)
  642. (format port "#<gexp-output ~a>" name))))
  643. (set-record-type-printer! <gexp-output> write-gexp-output)
  644. (define* (gexp-attribute gexp self-attribute #:optional (equal? equal?)
  645. #:key (validate (const #t)))
  646. "Recurse on GEXP and the expressions it refers to, summing the items
  647. returned by SELF-ATTRIBUTE, a procedure that takes a gexp. Use EQUAL? as the
  648. second argument to 'delete-duplicates'. Pass VALIDATE every gexp and
  649. attribute that is traversed."
  650. (if (gexp? gexp)
  651. (delete-duplicates
  652. (append (let ((attribute (self-attribute gexp)))
  653. (validate gexp attribute)
  654. attribute)
  655. (reverse
  656. (fold (lambda (input result)
  657. (match input
  658. (($ <gexp-input> (? gexp? exp))
  659. (append (gexp-attribute exp self-attribute
  660. #:validate validate)
  661. result))
  662. (($ <gexp-input> (lst ...))
  663. (fold/tree (lambda (obj result)
  664. (match obj
  665. ((? gexp? exp)
  666. (append (gexp-attribute exp self-attribute
  667. #:validate validate)
  668. result))
  669. (_
  670. result)))
  671. result
  672. lst))
  673. (_
  674. result)))
  675. '()
  676. (gexp-references gexp))))
  677. equal?)
  678. '())) ;plain Scheme data type
  679. (define (gexp-modules gexp)
  680. "Return the list of Guile module names GEXP relies on. If (gexp? GEXP) is
  681. false, meaning that GEXP is a plain Scheme object, return the empty list."
  682. (define (module=? m1 m2)
  683. ;; Return #t when M1 equals M2. Special-case '=>' specs because their
  684. ;; right-hand side may not be comparable with 'equal?': it's typically a
  685. ;; file-like object that embeds a gexp, which in turn embeds closure;
  686. ;; those closures may be 'eq?' when running compiled code but are unlikely
  687. ;; to be 'eq?' when running on 'eval'. Ignore the right-hand side to
  688. ;; avoid this discrepancy.
  689. (match m1
  690. (((name1 ...) '=> _)
  691. (match m2
  692. (((name2 ...) '=> _) (equal? name1 name2))
  693. (_ #f)))
  694. (_
  695. (equal? m1 m2))))
  696. (define (validate-modules gexp modules)
  697. ;; Warn if MODULES, imported by GEXP, contains modules that in general
  698. ;; should not be imported from the host because they vary from user to
  699. ;; user and may thus be a source of non-reproducibility. This includes
  700. ;; (guix config) as well as modules that come with Guile.
  701. (match (filter (match-lambda
  702. ((or ('guix 'config) ('ice-9 . _)) #t)
  703. (_ #f))
  704. modules)
  705. (() #t)
  706. (suspects
  707. (warning (gexp-location gexp)
  708. (N_ "importing module~{ ~a~} from the host~%"
  709. "importing modules~{ ~a~} from the host~%"
  710. (length suspects))
  711. suspects))))
  712. (gexp-attribute gexp gexp-self-modules module=?
  713. #:validate validate-modules))
  714. (define (gexp-extensions gexp)
  715. "Return the list of Guile extensions (packages) GEXP relies on. If (gexp?
  716. GEXP) is false, meaning that GEXP is a plain Scheme object, return the empty
  717. list."
  718. (gexp-attribute gexp gexp-self-extensions))
  719. (define (self-quoting? x)
  720. (letrec-syntax ((one-of (syntax-rules ()
  721. ((_) #f)
  722. ((_ pred rest ...)
  723. (or (pred x)
  724. (one-of rest ...))))))
  725. (one-of symbol? string? keyword? pair? null? array?
  726. number? boolean? char?)))
  727. (define (lower-inputs inputs system target)
  728. "Turn any object from INPUTS into a derivation input for SYSTEM or a store
  729. item (a \"source\"); return the corresponding input list as a monadic value.
  730. When TARGET is true, use it as the cross-compilation target triplet."
  731. (define (store-item? obj)
  732. (and (string? obj) (store-path? obj)))
  733. (define filterm
  734. (lift1 (cut filter ->bool <>) %store-monad))
  735. (with-monad %store-monad
  736. (>>= (mapm/accumulate-builds
  737. (match-lambda
  738. (($ <gexp-input> (? store-item? item))
  739. (return item))
  740. (($ <gexp-input> thing output native?)
  741. (mlet %store-monad ((obj (lower-object thing system
  742. #:target
  743. (and (not native?)
  744. target))))
  745. (return (match obj
  746. ((? derivation? drv)
  747. (derivation-input drv (list output)))
  748. ((? store-item? item)
  749. item)
  750. ((? self-quoting?)
  751. ;; Some inputs such as <system-binding> can lower to
  752. ;; a self-quoting object that FILTERM will filter
  753. ;; out.
  754. #f))))))
  755. inputs)
  756. filterm)))
  757. (define* (lower-reference-graphs graphs #:key system target)
  758. "Given GRAPHS, a list of (FILE-NAME INPUT ...) lists for use as a
  759. #:reference-graphs argument, lower it such that each INPUT is replaced by the
  760. corresponding <derivation-input> or store item."
  761. (define tuple->gexp-input
  762. (match-lambda
  763. ((thing)
  764. (%gexp-input thing "out" (not target)))
  765. ((thing output)
  766. (%gexp-input thing output (not target)))))
  767. (match graphs
  768. (((file-names . inputs) ...)
  769. (mlet %store-monad ((inputs (lower-inputs (map tuple->gexp-input inputs)
  770. system target)))
  771. (return (map cons file-names inputs))))))
  772. (define* (lower-references lst #:key system target)
  773. "Based on LST, a list of output names and packages, return a list of output
  774. names and file names suitable for the #:allowed-references argument to
  775. 'derivation'."
  776. (with-monad %store-monad
  777. (define lower
  778. (match-lambda
  779. ((? string? output)
  780. (return output))
  781. (($ <gexp-input> thing output native?)
  782. (mlet %store-monad ((drv (lower-object thing system
  783. #:target (if native?
  784. #f target))))
  785. (return (derivation->output-path drv output))))
  786. (thing
  787. (mlet %store-monad ((drv (lower-object thing system
  788. #:target target)))
  789. (return (derivation->output-path drv))))))
  790. (mapm/accumulate-builds lower lst)))
  791. (define default-guile-derivation
  792. ;; Here we break the abstraction by talking to the higher-level layer.
  793. ;; Thus, do the resolution lazily to hide the circular dependency.
  794. (let ((proc (delay
  795. (let ((iface (resolve-interface '(guix packages))))
  796. (module-ref iface 'default-guile-derivation)))))
  797. (lambda (system)
  798. ((force proc) system))))
  799. ;; Representation of a gexp instantiated for a given target and system.
  800. ;; It's an intermediate representation between <gexp> and <derivation>.
  801. (define-record-type <lowered-gexp>
  802. (lowered-gexp sexp inputs sources guile load-path load-compiled-path)
  803. lowered-gexp?
  804. (sexp lowered-gexp-sexp) ;sexp
  805. (inputs lowered-gexp-inputs) ;list of <derivation-input>
  806. (sources lowered-gexp-sources) ;list of store items
  807. (guile lowered-gexp-guile) ;<derivation-input> | #f
  808. (load-path lowered-gexp-load-path) ;list of store items
  809. (load-compiled-path lowered-gexp-load-compiled-path)) ;list of store items
  810. (define* (imported+compiled-modules modules system
  811. #:key (extensions '())
  812. deprecation-warnings guile
  813. (module-path %load-path))
  814. "Return a pair where the first element is the imported MODULES and the
  815. second element is the derivation to compile them."
  816. (mcached equal?
  817. (mlet %store-monad ((modules (if (pair? modules)
  818. (imported-modules modules
  819. #:system system
  820. #:module-path module-path)
  821. (return #f)))
  822. (compiled (if (pair? modules)
  823. (compiled-modules modules
  824. #:system system
  825. #:module-path module-path
  826. #:extensions extensions
  827. #:guile guile
  828. #:deprecation-warnings
  829. deprecation-warnings)
  830. (return #f))))
  831. (return (cons modules compiled)))
  832. modules
  833. system extensions guile deprecation-warnings module-path))
  834. (define (sexp->string sexp)
  835. "Like 'object->string', but deterministic and slightly faster."
  836. ;; Explicitly use UTF-8 for determinism, and also because UTF-8 output is
  837. ;; faster.
  838. (with-fluids ((%default-port-encoding "UTF-8"))
  839. (call-with-output-string
  840. (lambda (port)
  841. (write sexp port)))))
  842. (define* (lower-gexp exp
  843. #:key
  844. (module-path %load-path)
  845. (system (%current-system))
  846. (target 'current)
  847. (graft? (%graft?))
  848. (guile-for-build (%guile-for-build))
  849. (effective-version "3.0")
  850. deprecation-warnings)
  851. "*Note: This API is subject to change; use at your own risk!*
  852. Lower EXP, a gexp, instantiating it for SYSTEM and TARGET. Return a
  853. <lowered-gexp> ready to be used.
  854. Lowered gexps are an intermediate representation that's useful for
  855. applications that deal with gexps outside in a way that is disconnected from
  856. derivations--e.g., code evaluated for its side effects."
  857. (define %modules
  858. (delete-duplicates (gexp-modules exp)))
  859. (define (search-path modules extensions suffix)
  860. (append (match modules
  861. ((? derivation? drv)
  862. (list (derivation->output-path drv)))
  863. (#f
  864. '())
  865. ((? store-path? item)
  866. (list item)))
  867. (map (lambda (extension)
  868. (string-append (match extension
  869. ((? derivation? drv)
  870. (derivation->output-path drv))
  871. ((? store-path? item)
  872. item))
  873. suffix))
  874. extensions)))
  875. (mlet* %store-monad ( ;; The following binding forces '%current-system' and
  876. ;; '%current-target-system' to be looked up at >>=
  877. ;; time.
  878. (graft? (set-grafting graft?))
  879. (system -> (or system (%current-system)))
  880. (target -> (if (eq? target 'current)
  881. (%current-target-system)
  882. target))
  883. (guile (if guile-for-build
  884. (return guile-for-build)
  885. (default-guile-derivation system)))
  886. (inputs (lower-inputs (gexp-inputs exp)
  887. system target))
  888. (sexp (gexp->sexp exp system target))
  889. (extensions -> (gexp-extensions exp))
  890. (exts (mapm %store-monad
  891. (lambda (obj)
  892. (lower-object obj system
  893. #:target #f))
  894. extensions))
  895. (modules+compiled (imported+compiled-modules
  896. %modules system
  897. #:extensions extensions
  898. #:deprecation-warnings
  899. deprecation-warnings
  900. #:guile guile
  901. #:module-path module-path))
  902. (modules -> (car modules+compiled))
  903. (compiled -> (cdr modules+compiled)))
  904. (define load-path
  905. (search-path modules exts
  906. (string-append "/share/guile/site/" effective-version)))
  907. (define load-compiled-path
  908. (search-path compiled exts
  909. (string-append "/lib/guile/" effective-version
  910. "/site-ccache")))
  911. (mbegin %store-monad
  912. (set-grafting graft?) ;restore the initial setting
  913. (return (lowered-gexp sexp
  914. `(,@(if (derivation? modules)
  915. (list (derivation-input modules))
  916. '())
  917. ,@(if compiled
  918. (list (derivation-input compiled))
  919. '())
  920. ,@(map derivation-input exts)
  921. ,@(filter derivation-input? inputs))
  922. (filter string? (cons modules inputs))
  923. (derivation-input guile '("out"))
  924. load-path
  925. load-compiled-path)))))
  926. (define* (gexp->derivation name exp
  927. #:key
  928. system (target 'current)
  929. hash hash-algo recursive?
  930. (env-vars '())
  931. (modules '())
  932. (module-path %load-path)
  933. (guile-for-build (%guile-for-build))
  934. (effective-version "3.0")
  935. (graft? (%graft?))
  936. references-graphs
  937. allowed-references disallowed-references
  938. leaked-env-vars
  939. local-build? (substitutable? #t)
  940. (properties '())
  941. deprecation-warnings
  942. (script-name (string-append name "-builder")))
  943. "Return a derivation NAME that runs EXP (a gexp) with GUILE-FOR-BUILD (a
  944. derivation) on SYSTEM; EXP is stored in a file called SCRIPT-NAME. When
  945. TARGET is true, it is used as the cross-compilation target triplet for
  946. packages referred to by EXP.
  947. MODULES is deprecated in favor of 'with-imported-modules'. Its meaning is to
  948. make MODULES available in the evaluation context of EXP; MODULES is a list of
  949. names of Guile modules searched in MODULE-PATH to be copied in the store,
  950. compiled, and made available in the load path during the execution of
  951. EXP---e.g., '((guix build utils) (guix build gnu-build-system)).
  952. EFFECTIVE-VERSION determines the string to use when adding extensions of
  953. EXP (see 'with-extensions') to the search path---e.g., \"2.2\".
  954. GRAFT? determines whether packages referred to by EXP should be grafted when
  955. applicable.
  956. When REFERENCES-GRAPHS is true, it must be a list of tuples of one of the
  957. following forms:
  958. (FILE-NAME PACKAGE)
  959. (FILE-NAME PACKAGE OUTPUT)
  960. (FILE-NAME DERIVATION)
  961. (FILE-NAME DERIVATION OUTPUT)
  962. (FILE-NAME STORE-ITEM)
  963. The right-hand-side of each element of REFERENCES-GRAPHS is automatically made
  964. an input of the build process of EXP. In the build environment, each
  965. FILE-NAME contains the reference graph of the corresponding item, in a simple
  966. text format.
  967. ALLOWED-REFERENCES must be either #f or a list of output names and packages.
  968. In the latter case, the list denotes store items that the result is allowed to
  969. refer to. Any reference to another store item will lead to a build error.
  970. Similarly for DISALLOWED-REFERENCES, which can list items that must not be
  971. referenced by the outputs.
  972. DEPRECATION-WARNINGS determines whether to show deprecation warnings while
  973. compiling modules. It can be #f, #t, or 'detailed.
  974. The other arguments are as for 'derivation'."
  975. (define outputs (gexp-outputs exp))
  976. (define requested-graft? graft?)
  977. (define (graphs-file-names graphs)
  978. ;; Return a list of (FILE-NAME . STORE-PATH) pairs made from GRAPHS.
  979. (map (match-lambda
  980. ((file-name . (? derivation-input? input))
  981. (cons file-name (first (derivation-input-output-paths input))))
  982. ((file-name . (? string? item))
  983. (cons file-name item)))
  984. graphs))
  985. (define (add-modules exp modules)
  986. (if (null? modules)
  987. exp
  988. (make-gexp (gexp-references exp)
  989. (append modules (gexp-self-modules exp))
  990. (gexp-self-extensions exp)
  991. (gexp-proc exp)
  992. (gexp-location exp))))
  993. (mlet* %store-monad ( ;; The following binding forces '%current-system' and
  994. ;; '%current-target-system' to be looked up at >>=
  995. ;; time.
  996. (graft? (set-grafting graft?))
  997. (system -> (or system (%current-system)))
  998. (target -> (if (eq? target 'current)
  999. (%current-target-system)
  1000. target))
  1001. (exp -> (add-modules exp modules))
  1002. (lowered (lower-gexp exp
  1003. #:module-path module-path
  1004. #:system system
  1005. #:target target
  1006. #:graft? requested-graft?
  1007. #:guile-for-build
  1008. guile-for-build
  1009. #:effective-version
  1010. effective-version
  1011. #:deprecation-warnings
  1012. deprecation-warnings))
  1013. (graphs (if references-graphs
  1014. (lower-reference-graphs references-graphs
  1015. #:system system
  1016. #:target target)
  1017. (return #f)))
  1018. (allowed (if allowed-references
  1019. (lower-references allowed-references
  1020. #:system system
  1021. #:target target)
  1022. (return #f)))
  1023. (disallowed (if disallowed-references
  1024. (lower-references disallowed-references
  1025. #:system system
  1026. #:target target)
  1027. (return #f)))
  1028. (guile -> (lowered-gexp-guile lowered))
  1029. (builder (text-file script-name
  1030. (sexp->string
  1031. (lowered-gexp-sexp lowered)))))
  1032. (mbegin %store-monad
  1033. (set-grafting graft?) ;restore the initial setting
  1034. (raw-derivation name
  1035. (string-append (derivation-input-output-path guile)
  1036. "/bin/guile")
  1037. `("--no-auto-compile"
  1038. ,@(append-map (lambda (directory)
  1039. `("-L" ,directory))
  1040. (lowered-gexp-load-path lowered))
  1041. ,@(append-map (lambda (directory)
  1042. `("-C" ,directory))
  1043. (lowered-gexp-load-compiled-path lowered))
  1044. ,builder)
  1045. #:outputs outputs
  1046. #:env-vars env-vars
  1047. #:system system
  1048. #:inputs `(,guile
  1049. ,@(lowered-gexp-inputs lowered)
  1050. ,@(match graphs
  1051. (((_ . inputs) ...)
  1052. (filter derivation-input? inputs))
  1053. (#f '())))
  1054. #:sources `(,builder
  1055. ,@(if (and (string? modules)
  1056. (store-path? modules))
  1057. (list modules)
  1058. '())
  1059. ,@(lowered-gexp-sources lowered)
  1060. ,@(match graphs
  1061. (((_ . inputs) ...)
  1062. (filter string? inputs))
  1063. (#f '())))
  1064. #:hash hash #:hash-algo hash-algo #:recursive? recursive?
  1065. #:references-graphs (and=> graphs graphs-file-names)
  1066. #:allowed-references allowed
  1067. #:disallowed-references disallowed
  1068. #:leaked-env-vars leaked-env-vars
  1069. #:local-build? local-build?
  1070. #:substitutable? substitutable?
  1071. #:properties properties))))
  1072. (define (fold/tree proc seed lst)
  1073. "Like 'fold', but recurse into sub-lists of LST and accept improper lists."
  1074. (let loop ((obj lst)
  1075. (result seed))
  1076. (match obj
  1077. ((head . tail)
  1078. (loop tail (loop head result)))
  1079. (_
  1080. (proc obj result)))))
  1081. (define (gexp-inputs exp)
  1082. "Return the list of <gexp-input> for EXP."
  1083. (define set-gexp-input-native?
  1084. (match-lambda
  1085. (($ <gexp-input> thing output)
  1086. (%gexp-input thing output #t))))
  1087. (define (interesting? obj)
  1088. (or (file-like? obj)
  1089. (and (string? obj) (direct-store-path? obj))))
  1090. (define (add-reference-inputs ref result)
  1091. (match ref
  1092. (($ <gexp-input> (? gexp? exp) _ #t)
  1093. (append (map set-gexp-input-native? (gexp-inputs exp))
  1094. result))
  1095. (($ <gexp-input> (? gexp? exp) _ #f)
  1096. (append (gexp-inputs exp) result))
  1097. (($ <gexp-input> (? string? str))
  1098. (if (direct-store-path? str)
  1099. (cons ref result)
  1100. result))
  1101. (($ <gexp-input> (? struct? thing) output n?)
  1102. (if (lookup-compiler thing)
  1103. ;; THING is a derivation, or a package, or an origin, etc.
  1104. (cons ref result)
  1105. result))
  1106. (($ <gexp-input> (? pair? lst) output n?)
  1107. ;; XXX: Scan LST for inputs. Inherit N?.
  1108. (fold/tree (lambda (obj result)
  1109. (match obj
  1110. ((? gexp-input? x)
  1111. (cons (%gexp-input (gexp-input-thing x)
  1112. (gexp-input-output x)
  1113. n?)
  1114. result))
  1115. ((? interesting? x)
  1116. (cons (%gexp-input x "out" n?) result))
  1117. ((? gexp? x)
  1118. (append (gexp-inputs x) result))
  1119. (_
  1120. result)))
  1121. result
  1122. lst))
  1123. (_
  1124. ;; Ignore references to other kinds of objects.
  1125. result)))
  1126. (fold-right add-reference-inputs
  1127. '()
  1128. (gexp-references exp)))
  1129. (define (gexp-outputs exp)
  1130. "Return the outputs referred to by EXP as a list of strings."
  1131. (define (add-reference-output ref result)
  1132. (match ref
  1133. (($ <gexp-output> name)
  1134. (cons name result))
  1135. (($ <gexp-input> (? gexp? exp))
  1136. (append (gexp-outputs exp) result))
  1137. (($ <gexp-input> (? pair? lst))
  1138. ;; XXX: Scan LST for outputs.
  1139. (fold/tree (lambda (obj result)
  1140. (match obj
  1141. (($ <gexp-output> name) (cons name result))
  1142. ((? gexp? x) (append (gexp-outputs x) result))
  1143. (_ result)))
  1144. result
  1145. lst))
  1146. (_
  1147. result)))
  1148. (delete-duplicates
  1149. (fold add-reference-output '() (gexp-references exp))))
  1150. (define (gexp->sexp exp system target)
  1151. "Return (monadically) the sexp corresponding to EXP for the given OUTPUT,
  1152. and in the current monad setting (system type, etc.)"
  1153. (define* (reference->sexp ref #:optional native?)
  1154. (with-monad %store-monad
  1155. (match ref
  1156. (($ <gexp-output> output)
  1157. ;; Output file names are not known in advance but the daemon defines
  1158. ;; an environment variable for each of them at build time, so use
  1159. ;; that trick.
  1160. (return `((@ (guile) getenv) ,output)))
  1161. (($ <gexp-input> (? gexp? exp) output n?)
  1162. (gexp->sexp exp
  1163. system (if (or n? native?) #f target)))
  1164. (($ <gexp-input> (refs ...) output n?)
  1165. (mapm %store-monad
  1166. (lambda (ref)
  1167. ;; XXX: Automatically convert REF to an gexp-input.
  1168. (if (or (symbol? ref) (number? ref)
  1169. (boolean? ref) (null? ref) (array? ref))
  1170. (return ref)
  1171. (reference->sexp
  1172. (if (gexp-input? ref)
  1173. ref
  1174. (%gexp-input ref "out" n?))
  1175. (or n? native?))))
  1176. refs))
  1177. (($ <gexp-input> (? struct? thing) output n?)
  1178. (let ((target (if (or n? native?) #f target)))
  1179. (lower+expand-object thing system
  1180. #:target target
  1181. #:output output)))
  1182. (($ <gexp-input> (? self-quoting? x))
  1183. (return x))
  1184. (($ <gexp-input> x)
  1185. (raise (condition (&gexp-input-error (input x)))))
  1186. (x
  1187. (return x)))))
  1188. (mlet %store-monad
  1189. ((args (mapm %store-monad
  1190. reference->sexp (gexp-references exp))))
  1191. (return (apply (gexp-proc exp) args))))
  1192. (define-syntax-parameter current-imported-modules
  1193. ;; Current list of imported modules.
  1194. (identifier-syntax '()))
  1195. (define-syntax-rule (with-imported-modules modules body ...)
  1196. "Mark the gexps defined in BODY... as requiring MODULES in their execution
  1197. environment."
  1198. (syntax-parameterize ((current-imported-modules
  1199. (identifier-syntax modules)))
  1200. body ...))
  1201. (define-syntax-parameter current-imported-extensions
  1202. ;; Current list of extensions.
  1203. (identifier-syntax '()))
  1204. (define-syntax-rule (with-extensions extensions body ...)
  1205. "Mark the gexps defined in BODY... as requiring EXTENSIONS in their
  1206. execution environment."
  1207. (syntax-parameterize ((current-imported-extensions
  1208. (identifier-syntax extensions)))
  1209. body ...))
  1210. (define-syntax gexp
  1211. (lambda (s)
  1212. (define (collect-escapes exp)
  1213. ;; Return all the 'ungexp' present in EXP.
  1214. (let loop ((exp exp)
  1215. (result '()))
  1216. (syntax-case exp (ungexp
  1217. ungexp-splicing
  1218. ungexp-native
  1219. ungexp-native-splicing)
  1220. ((ungexp _)
  1221. (cons exp result))
  1222. ((ungexp _ _)
  1223. (cons exp result))
  1224. ((ungexp-splicing _ ...)
  1225. (cons exp result))
  1226. ((ungexp-native _ ...)
  1227. (cons exp result))
  1228. ((ungexp-native-splicing _ ...)
  1229. (cons exp result))
  1230. ((exp0 . exp)
  1231. (let ((result (loop #'exp0 result)))
  1232. (loop #'exp result)))
  1233. (_
  1234. result))))
  1235. (define (escape->ref exp)
  1236. ;; Turn 'ungexp' form EXP into a "reference".
  1237. (syntax-case exp (ungexp ungexp-splicing
  1238. ungexp-native ungexp-native-splicing
  1239. output)
  1240. ((ungexp output)
  1241. #'(gexp-output "out"))
  1242. ((ungexp output name)
  1243. #'(gexp-output name))
  1244. ((ungexp thing)
  1245. #'(%gexp-input thing "out" #f))
  1246. ((ungexp drv-or-pkg out)
  1247. #'(%gexp-input drv-or-pkg out #f))
  1248. ((ungexp-splicing lst)
  1249. #'(%gexp-input lst "out" #f))
  1250. ((ungexp-native thing)
  1251. #'(%gexp-input thing "out" #t))
  1252. ((ungexp-native drv-or-pkg out)
  1253. #'(%gexp-input drv-or-pkg out #t))
  1254. ((ungexp-native-splicing lst)
  1255. #'(%gexp-input lst "out" #t))))
  1256. (define (substitute-ungexp exp substs)
  1257. ;; Given EXP, an 'ungexp' or 'ungexp-native' form, substitute it with
  1258. ;; the corresponding form in SUBSTS.
  1259. (match (assoc exp substs)
  1260. ((_ id)
  1261. id)
  1262. (_ ;internal error
  1263. (with-syntax ((exp exp))
  1264. #'(syntax-error "error: no 'ungexp' substitution" exp)))))
  1265. (define (substitute-ungexp-splicing exp substs)
  1266. (syntax-case exp ()
  1267. ((exp rest ...)
  1268. (match (assoc #'exp substs)
  1269. ((_ id)
  1270. (with-syntax ((id id))
  1271. #`(append id
  1272. #,(substitute-references #'(rest ...) substs))))
  1273. (_
  1274. #'(syntax-error "error: no 'ungexp-splicing' substitution"
  1275. exp))))))
  1276. (define (substitute-references exp substs)
  1277. ;; Return a variant of EXP where all the cars of SUBSTS have been
  1278. ;; replaced by the corresponding cdr.
  1279. (syntax-case exp (ungexp ungexp-native
  1280. ungexp-splicing ungexp-native-splicing)
  1281. ((ungexp _ ...)
  1282. (substitute-ungexp exp substs))
  1283. ((ungexp-native _ ...)
  1284. (substitute-ungexp exp substs))
  1285. (((ungexp-splicing _ ...) rest ...)
  1286. (substitute-ungexp-splicing exp substs))
  1287. (((ungexp-native-splicing _ ...) rest ...)
  1288. (substitute-ungexp-splicing exp substs))
  1289. ((exp0 . exp)
  1290. #`(cons #,(substitute-references #'exp0 substs)
  1291. #,(substitute-references #'exp substs)))
  1292. (x #''x)))
  1293. (syntax-case s (ungexp output)
  1294. ((_ exp)
  1295. (let* ((escapes (delete-duplicates (collect-escapes #'exp)))
  1296. (formals (generate-temporaries escapes))
  1297. (sexp (substitute-references #'exp (zip escapes formals)))
  1298. (refs (map escape->ref escapes)))
  1299. #`(make-gexp (list #,@refs)
  1300. current-imported-modules
  1301. current-imported-extensions
  1302. (lambda #,formals
  1303. #,sexp)
  1304. (current-source-location)))))))
  1305. ;;;
  1306. ;;; Module handling.
  1307. ;;;
  1308. (define %utils-module
  1309. ;; This file provides 'mkdir-p', needed to implement 'imported-files' and
  1310. ;; other primitives below. Note: We give the file name relative to this
  1311. ;; file you are currently reading; 'search-path' could return a file name
  1312. ;; relative to the current working directory.
  1313. (local-file "build/utils.scm"
  1314. "build-utils.scm"))
  1315. (define* (imported-files/derivation files
  1316. #:key (name "file-import")
  1317. (symlink? #f)
  1318. (system (%current-system))
  1319. (guile (%guile-for-build)))
  1320. "Return a derivation that imports FILES into STORE. FILES must be a list
  1321. of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
  1322. resulting store path. FILE can be either a file name, or a file-like object,
  1323. as returned by 'local-file' for example. If SYMLINK? is true, create symlinks
  1324. to the source files instead of copying them."
  1325. (define file-pair
  1326. (match-lambda
  1327. ((final-path . (? string? file-name))
  1328. (mlet %store-monad ((file (interned-file file-name
  1329. (basename final-path))))
  1330. (return (list final-path file))))
  1331. ((final-path . file-like)
  1332. (mlet %store-monad ((file (lower-object file-like system)))
  1333. (return (list final-path file))))))
  1334. (mlet %store-monad ((files (mapm %store-monad file-pair files)))
  1335. (define build
  1336. (gexp
  1337. (begin
  1338. (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
  1339. (use-modules (ice-9 match))
  1340. (mkdir (ungexp output)) (chdir (ungexp output))
  1341. (for-each (match-lambda
  1342. ((final-path store-path)
  1343. (mkdir-p (dirname final-path))
  1344. ((ungexp (if symlink? 'symlink 'copy-file))
  1345. store-path final-path)))
  1346. '(ungexp files)))))
  1347. ;; TODO: Pass FILES as an environment variable so that BUILD remains
  1348. ;; exactly the same regardless of FILES: less disk space, and fewer
  1349. ;; 'add-to-store' RPCs.
  1350. (gexp->derivation name build
  1351. #:system system
  1352. #:guile-for-build guile
  1353. #:local-build? #t
  1354. #:substitutable? #f
  1355. ;; Avoid deprecation warnings about the use of the _IO*
  1356. ;; constants in (guix build utils).
  1357. #:env-vars
  1358. '(("GUILE_WARN_DEPRECATED" . "no")))))
  1359. (define* (imported-files files
  1360. #:key (name "file-import")
  1361. ;; The following parameters make sense when creating
  1362. ;; an actual derivation.
  1363. (system (%current-system))
  1364. (guile (%guile-for-build)))
  1365. "Import FILES into the store and return the resulting derivation or store
  1366. file name (a derivation is created if and only if some elements of FILES are
  1367. file-like objects and not local file names.) FILES must be a list
  1368. of (FINAL-PATH . FILE) pairs. Each FILE is mapped to FINAL-PATH in the
  1369. resulting store path. FILE can be either a file name, or a file-like object,
  1370. as returned by 'local-file' for example."
  1371. (if (any (match-lambda
  1372. ((_ . (? struct? source)) #t)
  1373. (_ #f))
  1374. files)
  1375. (imported-files/derivation files #:name name
  1376. #:symlink? derivation?
  1377. #:system system #:guile guile)
  1378. (interned-file-tree `(,name directory
  1379. ,@(file-mapping->tree files)))))
  1380. (define* (imported-modules modules
  1381. #:key (name "module-import")
  1382. (system (%current-system))
  1383. (guile (%guile-for-build))
  1384. (module-path %load-path))
  1385. "Return a derivation that contains the source files of MODULES, a list of
  1386. module names such as `(ice-9 q)'. All of MODULES must be either names of
  1387. modules to be found in the MODULE-PATH search path, or a module name followed
  1388. by an arrow followed by a file-like object. For example:
  1389. (imported-modules `((guix build utils)
  1390. (guix gcrypt)
  1391. ((guix config) => ,(scheme-file …))))
  1392. In this example, the first two modules are taken from MODULE-PATH, and the
  1393. last one is created from the given <scheme-file> object."
  1394. (let ((files (map (match-lambda
  1395. (((module ...) '=> file)
  1396. (cons (module->source-file-name module)
  1397. file))
  1398. ((module ...)
  1399. (let ((f (module->source-file-name module)))
  1400. (cons f (search-path* module-path f)))))
  1401. modules)))
  1402. (imported-files files #:name name
  1403. #:system system
  1404. #:guile guile)))
  1405. (define* (compiled-modules modules
  1406. #:key (name "module-import-compiled")
  1407. (system (%current-system))
  1408. target
  1409. (guile (%guile-for-build))
  1410. (module-path %load-path)
  1411. (extensions '())
  1412. (deprecation-warnings #f))
  1413. "Return a derivation that builds a tree containing the `.go' files
  1414. corresponding to MODULES. All the MODULES are built in a context where
  1415. they can refer to each other. When TARGET is true, cross-compile MODULES for
  1416. TARGET, a GNU triplet."
  1417. (define total (length modules))
  1418. (mlet %store-monad ((modules (imported-modules modules
  1419. #:system system
  1420. #:guile guile
  1421. #:module-path
  1422. module-path)))
  1423. (define build
  1424. (gexp
  1425. (begin
  1426. (primitive-load (ungexp %utils-module)) ;for 'mkdir-p'
  1427. (use-modules (ice-9 ftw)
  1428. (ice-9 format)
  1429. (srfi srfi-1)
  1430. (srfi srfi-26)
  1431. (system base target)
  1432. (system base compile))
  1433. (define (regular? file)
  1434. (not (member file '("." ".."))))
  1435. (define (process-entry entry output processed)
  1436. (if (file-is-directory? entry)
  1437. (let ((output (string-append output "/" (basename entry))))
  1438. (mkdir-p output)
  1439. (process-directory entry output processed))
  1440. (let* ((base (basename entry ".scm"))
  1441. (output (string-append output "/" base ".go")))
  1442. (format #t "[~2@a/~2@a] Compiling '~a'...~%"
  1443. (+ 1 processed (ungexp total))
  1444. (ungexp (* total 2))
  1445. entry)
  1446. (ungexp-splicing
  1447. (if target
  1448. (gexp ((with-target (ungexp target)
  1449. (lambda ()
  1450. (compile-file entry
  1451. #:output-file output
  1452. #:opts
  1453. %auto-compilation-options)))))
  1454. (gexp ((compile-file entry
  1455. #:output-file output
  1456. #:opts %auto-compilation-options)))))
  1457. (+ 1 processed))))
  1458. (define (process-directory directory output processed)
  1459. (let ((entries (map (cut string-append directory "/" <>)
  1460. (scandir directory regular?))))
  1461. (fold (cut process-entry <> output <>)
  1462. processed
  1463. entries)))
  1464. (define* (load-from-directory directory
  1465. #:optional (loaded 0))
  1466. "Load all the source files found in DIRECTORY."
  1467. ;; XXX: This works around <https://bugs.gnu.org/15602>.
  1468. (let ((entries (map (cut string-append directory "/" <>)
  1469. (scandir directory regular?))))
  1470. (fold (lambda (file loaded)
  1471. (if (file-is-directory? file)
  1472. (load-from-directory file loaded)
  1473. (begin
  1474. (format #t "[~2@a/~2@a] Loading '~a'...~%"
  1475. (+ 1 loaded) (ungexp (* 2 total))
  1476. file)
  1477. (save-module-excursion
  1478. (lambda ()
  1479. (primitive-load file)))
  1480. (+ 1 loaded))))
  1481. loaded
  1482. entries)))
  1483. (setvbuf (current-output-port)
  1484. (cond-expand (guile-2.2 'line) (else _IOLBF)))
  1485. (define mkdir-p
  1486. ;; Capture 'mkdir-p'.
  1487. (@ (guix build utils) mkdir-p))
  1488. ;; Add EXTENSIONS to the search path.
  1489. (set! %load-path
  1490. (append (map (lambda (extension)
  1491. (string-append extension
  1492. "/share/guile/site/"
  1493. (effective-version)))
  1494. '((ungexp-native-splicing extensions)))
  1495. %load-path))
  1496. (set! %load-compiled-path
  1497. (append (map (lambda (extension)
  1498. (string-append extension "/lib/guile/"
  1499. (effective-version)
  1500. "/site-ccache"))
  1501. '((ungexp-native-splicing extensions)))
  1502. %load-compiled-path))
  1503. (set! %load-path (cons (ungexp modules) %load-path))
  1504. ;; Above we loaded our own (guix build utils) but now we may need to
  1505. ;; load a compile a different one. Thus, force a reload.
  1506. (let ((utils (string-append (ungexp modules)
  1507. "/guix/build/utils.scm")))
  1508. (when (file-exists? utils)
  1509. (load utils)))
  1510. (mkdir (ungexp output))
  1511. (chdir (ungexp modules))
  1512. (load-from-directory ".")
  1513. (process-directory "." (ungexp output) 0))))
  1514. ;; TODO: Pass MODULES as an environment variable.
  1515. (gexp->derivation name build
  1516. #:system system
  1517. #:target target
  1518. #:guile-for-build guile
  1519. #:local-build? #t
  1520. #:env-vars
  1521. (case deprecation-warnings
  1522. ((#f)
  1523. '(("GUILE_WARN_DEPRECATED" . "no")))
  1524. ((detailed)
  1525. '(("GUILE_WARN_DEPRECATED" . "detailed")))
  1526. (else
  1527. '())))))
  1528. ;;;
  1529. ;;; Convenience procedures.
  1530. ;;;
  1531. (define (default-guile)
  1532. ;; Lazily resolve 'guile-3.0' (not 'guile-final' because this is for
  1533. ;; programs returned by 'program-file' and we don't want to keep references
  1534. ;; to several Guile packages). This module must not refer to (gnu …)
  1535. ;; modules directly, to avoid circular dependencies, hence this hack.
  1536. (module-ref (resolve-interface '(gnu packages guile))
  1537. 'guile-3.0))
  1538. (define* (load-path-expression modules #:optional (path %load-path)
  1539. #:key (extensions '()) system target)
  1540. "Return as a monadic value a gexp that sets '%load-path' and
  1541. '%load-compiled-path' to point to MODULES, a list of module names. MODULES
  1542. are searched for in PATH. Return #f when MODULES and EXTENSIONS are empty."
  1543. (if (and (null? modules) (null? extensions))
  1544. (with-monad %store-monad
  1545. (return #f))
  1546. (mlet %store-monad ((modules (imported-modules modules
  1547. #:module-path path
  1548. #:system system))
  1549. (compiled (compiled-modules modules
  1550. #:extensions extensions
  1551. #:module-path path
  1552. #:system system
  1553. #:target target)))
  1554. (return
  1555. (gexp (eval-when (expand load eval)
  1556. ;; Augment the load paths and delete duplicates. Do that
  1557. ;; without loading (srfi srfi-1) or anything.
  1558. (let ((extensions '((ungexp-splicing extensions)))
  1559. (prepend (lambda (items lst)
  1560. ;; This is O(N²) but N is typically small.
  1561. (let loop ((items items)
  1562. (lst lst))
  1563. (if (null? items)
  1564. lst
  1565. (loop (cdr items)
  1566. (cons (car items)
  1567. (delete (car items) lst))))))))
  1568. (set! %load-path
  1569. (prepend (cons (ungexp modules)
  1570. (map (lambda (extension)
  1571. (string-append extension
  1572. "/share/guile/site/"
  1573. (effective-version)))
  1574. extensions))
  1575. %load-path))
  1576. (set! %load-compiled-path
  1577. (prepend (cons (ungexp compiled)
  1578. (map (lambda (extension)
  1579. (string-append extension
  1580. "/lib/guile/"
  1581. (effective-version)
  1582. "/site-ccache"))
  1583. extensions))
  1584. %load-compiled-path)))))))))
  1585. (define* (gexp->script name exp
  1586. #:key (guile (default-guile))
  1587. (module-path %load-path)
  1588. (system (%current-system))
  1589. (target 'current))
  1590. "Return an executable script NAME that runs EXP using GUILE, with EXP's
  1591. imported modules in its search path. Look up EXP's modules in MODULE-PATH."
  1592. (mlet* %store-monad ((target (if (eq? target 'current)
  1593. (current-target-system)
  1594. (return target)))
  1595. (set-load-path
  1596. (load-path-expression (gexp-modules exp)
  1597. module-path
  1598. #:extensions
  1599. (gexp-extensions exp)
  1600. #:system system
  1601. #:target target)))
  1602. (gexp->derivation name
  1603. (gexp
  1604. (call-with-output-file (ungexp output)
  1605. (lambda (port)
  1606. ;; Note: that makes a long shebang. When the store
  1607. ;; is /gnu/store, that fits within the 128-byte
  1608. ;; limit imposed by Linux, but that may go beyond
  1609. ;; when running tests.
  1610. (format port
  1611. "#!~a/bin/guile --no-auto-compile~%!#~%"
  1612. (ungexp guile))
  1613. (ungexp-splicing
  1614. (if set-load-path
  1615. (gexp ((write '(ungexp set-load-path) port)))
  1616. (gexp ())))
  1617. (write '(ungexp exp) port)
  1618. (chmod port #o555))))
  1619. #:system system
  1620. #:target target
  1621. #:module-path module-path
  1622. ;; These derivations are not worth offloading or
  1623. ;; substituting.
  1624. #:local-build? #t
  1625. #:substitutable? #f)))
  1626. (define* (gexp->file name exp #:key
  1627. (set-load-path? #t)
  1628. (module-path %load-path)
  1629. (splice? #f)
  1630. (system (%current-system))
  1631. (target 'current))
  1632. "Return a derivation that builds a file NAME containing EXP. When SPLICE?
  1633. is true, EXP is considered to be a list of expressions that will be spliced in
  1634. the resulting file.
  1635. When SET-LOAD-PATH? is true, emit code in the resulting file to set
  1636. '%load-path' and '%load-compiled-path' to honor EXP's imported modules.
  1637. Lookup EXP's modules in MODULE-PATH."
  1638. (define modules (gexp-modules exp))
  1639. (define extensions (gexp-extensions exp))
  1640. (mlet* %store-monad
  1641. ((target (if (eq? target 'current)
  1642. (current-target-system)
  1643. (return target)))
  1644. (no-load-path? -> (or (not set-load-path?)
  1645. (and (null? modules)
  1646. (null? extensions))))
  1647. (set-load-path
  1648. (load-path-expression modules module-path
  1649. #:extensions extensions
  1650. #:system system
  1651. #:target target)))
  1652. (if no-load-path?
  1653. (gexp->derivation name
  1654. (gexp
  1655. (call-with-output-file (ungexp output)
  1656. (lambda (port)
  1657. (for-each
  1658. (lambda (exp)
  1659. (write exp port))
  1660. '(ungexp (if splice?
  1661. exp
  1662. (gexp ((ungexp exp)))))))))
  1663. #:local-build? #t
  1664. #:substitutable? #f
  1665. #:system system
  1666. #:target target)
  1667. (gexp->derivation name
  1668. (gexp
  1669. (call-with-output-file (ungexp output)
  1670. (lambda (port)
  1671. (write '(ungexp set-load-path) port)
  1672. (for-each
  1673. (lambda (exp)
  1674. (write exp port))
  1675. '(ungexp (if splice?
  1676. exp
  1677. (gexp ((ungexp exp)))))))))
  1678. #:module-path module-path
  1679. #:local-build? #t
  1680. #:substitutable? #f
  1681. #:system system
  1682. #:target target))))
  1683. (define* (text-file* name #:rest text)
  1684. "Return as a monadic value a derivation that builds a text file containing
  1685. all of TEXT. TEXT may list, in addition to strings, objects of any type that
  1686. can be used in a gexp: packages, derivations, local file objects, etc. The
  1687. resulting store file holds references to all these."
  1688. (define builder
  1689. (gexp (call-with-output-file (ungexp output "out")
  1690. (lambda (port)
  1691. (display (string-append (ungexp-splicing text)) port)))))
  1692. (gexp->derivation name builder
  1693. #:local-build? #t
  1694. #:substitutable? #f))
  1695. (define* (mixed-text-file name #:rest text)
  1696. "Return an object representing store file NAME containing TEXT. TEXT is a
  1697. sequence of strings and file-like objects, as in:
  1698. (mixed-text-file \"profile\"
  1699. \"export PATH=\" coreutils \"/bin:\" grep \"/bin\")
  1700. This is the declarative counterpart of 'text-file*'."
  1701. (define build
  1702. (gexp (call-with-output-file (ungexp output "out")
  1703. (lambda (port)
  1704. (display (string-append (ungexp-splicing text)) port)))))
  1705. (computed-file name build))
  1706. (define (file-union name files)
  1707. "Return a <computed-file> that builds a directory containing all of FILES.
  1708. Each item in FILES must be a two-element list where the first element is the
  1709. file name to use in the new directory, and the second element is a gexp
  1710. denoting the target file. Here's an example:
  1711. (file-union \"etc\"
  1712. `((\"hosts\" ,(plain-file \"hosts\"
  1713. \"127.0.0.1 localhost\"))
  1714. (\"bashrc\" ,(plain-file \"bashrc\"
  1715. \"alias ls='ls --color'\"))
  1716. (\"libvirt/qemu.conf\" ,(plain-file \"qemu.conf\" \"\"))))
  1717. This yields an 'etc' directory containing these two files."
  1718. (computed-file name
  1719. (with-imported-modules '((guix build utils))
  1720. (gexp
  1721. (begin
  1722. (use-modules (guix build utils))
  1723. (mkdir (ungexp output))
  1724. (chdir (ungexp output))
  1725. (ungexp-splicing
  1726. (map (match-lambda
  1727. ((target source)
  1728. (gexp
  1729. (begin
  1730. ;; Stat the source to abort early if it does
  1731. ;; not exist.
  1732. (stat (ungexp source))
  1733. (mkdir-p (dirname (ungexp target)))
  1734. (symlink (ungexp source)
  1735. (ungexp target))))))
  1736. files)))))))
  1737. (define* (directory-union name things
  1738. #:key (copy? #f) (quiet? #f)
  1739. (resolve-collision 'warn-about-collision))
  1740. "Return a directory that is the union of THINGS, where THINGS is a list of
  1741. file-like objects denoting directories. For example:
  1742. (directory-union \"guile+emacs\" (list guile emacs))
  1743. yields a directory that is the union of the 'guile' and 'emacs' packages.
  1744. Call RESOLVE-COLLISION when several files collide, passing it the list of
  1745. colliding files. RESOLVE-COLLISION must return the chosen file or #f, in
  1746. which case the colliding entry is skipped altogether.
  1747. When HARD-LINKS? is true, create hard links instead of symlinks. When QUIET?
  1748. is true, the derivation will not print anything."
  1749. (define symlink
  1750. (if copy?
  1751. (gexp (lambda (old new)
  1752. (if (file-is-directory? old)
  1753. (symlink old new)
  1754. (copy-file old new))))
  1755. (gexp symlink)))
  1756. (define log-port
  1757. (if quiet?
  1758. (gexp (%make-void-port "w"))
  1759. (gexp (current-error-port))))
  1760. (match things
  1761. ((one)
  1762. ;; Only one thing; return it.
  1763. one)
  1764. (_
  1765. (computed-file name
  1766. (with-imported-modules '((guix build union))
  1767. (gexp (begin
  1768. (use-modules (guix build union)
  1769. (srfi srfi-1)) ;for 'first' and 'last'
  1770. (union-build (ungexp output)
  1771. '(ungexp things)
  1772. #:log-port (ungexp log-port)
  1773. #:symlink (ungexp symlink)
  1774. #:resolve-collision
  1775. (ungexp resolve-collision)))))))))
  1776. ;;;
  1777. ;;; Syntactic sugar.
  1778. ;;;
  1779. (eval-when (expand load eval)
  1780. (define* (read-ungexp chr port #:optional native?)
  1781. "Read an 'ungexp' or 'ungexp-splicing' form from PORT. When NATIVE? is
  1782. true, use 'ungexp-native' and 'ungexp-native-splicing' instead."
  1783. (define unquote-symbol
  1784. (match (peek-char port)
  1785. (#\@
  1786. (read-char port)
  1787. (if native?
  1788. 'ungexp-native-splicing
  1789. 'ungexp-splicing))
  1790. (_
  1791. (if native?
  1792. 'ungexp-native
  1793. 'ungexp))))
  1794. (match (read port)
  1795. ((? symbol? symbol)
  1796. (let ((str (symbol->string symbol)))
  1797. (match (string-index-right str #\:)
  1798. (#f
  1799. `(,unquote-symbol ,symbol))
  1800. (colon
  1801. (let ((name (string->symbol (substring str 0 colon)))
  1802. (output (substring str (+ colon 1))))
  1803. `(,unquote-symbol ,name ,output))))))
  1804. (x
  1805. `(,unquote-symbol ,x))))
  1806. (define (read-gexp chr port)
  1807. "Read a 'gexp' form from PORT."
  1808. `(gexp ,(read port)))
  1809. ;; Extend the reader
  1810. (read-hash-extend #\~ read-gexp)
  1811. (read-hash-extend #\$ read-ungexp)
  1812. (read-hash-extend #\+ (cut read-ungexp <> <> #t)))
  1813. ;;; gexp.scm ends here