packages.scm 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014, 2015, 2017, 2018 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
  5. ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  6. ;;; Copyright © 2017, 2019 Efraim Flashner <efraim@flashner.co.il>
  7. ;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix packages)
  24. #:use-module (guix utils)
  25. #:use-module (guix records)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (guix gexp)
  29. #:use-module (guix base32)
  30. #:use-module (guix grafts)
  31. #:use-module (guix derivations)
  32. #:use-module (guix memoization)
  33. #:use-module (guix build-system)
  34. #:use-module (guix search-paths)
  35. #:use-module (guix sets)
  36. #:use-module (ice-9 match)
  37. #:use-module (ice-9 vlist)
  38. #:use-module (ice-9 regex)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-9 gnu)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-34)
  44. #:use-module (srfi srfi-35)
  45. #:use-module (web uri)
  46. #:re-export (%current-system
  47. %current-target-system
  48. search-path-specification) ;for convenience
  49. #:export (origin
  50. origin?
  51. this-origin
  52. origin-uri
  53. origin-method
  54. origin-sha256
  55. origin-file-name
  56. origin-actual-file-name
  57. origin-patches
  58. origin-patch-flags
  59. origin-patch-inputs
  60. origin-patch-guile
  61. origin-snippet
  62. origin-modules
  63. base32
  64. package
  65. package?
  66. this-package
  67. package-name
  68. package-upstream-name
  69. package-version
  70. package-full-name
  71. package-source
  72. package-build-system
  73. package-arguments
  74. package-inputs
  75. package-native-inputs
  76. package-propagated-inputs
  77. package-outputs
  78. package-native-search-paths
  79. package-search-paths
  80. package-replacement
  81. package-synopsis
  82. package-description
  83. package-license
  84. package-home-page
  85. package-supported-systems
  86. package-properties
  87. package-location
  88. hidden-package
  89. hidden-package?
  90. package-superseded
  91. deprecated-package
  92. package-field-location
  93. package-direct-sources
  94. package-transitive-sources
  95. package-direct-inputs
  96. package-transitive-inputs
  97. package-transitive-target-inputs
  98. package-transitive-native-inputs
  99. package-transitive-propagated-inputs
  100. package-transitive-native-search-paths
  101. package-transitive-supported-systems
  102. package-mapping
  103. package-input-rewriting
  104. package-input-rewriting/spec
  105. package-source-derivation
  106. package-derivation
  107. package-cross-derivation
  108. package-output
  109. package-grafts
  110. package-patched-vulnerabilities
  111. package/inherit
  112. transitive-input-references
  113. %supported-systems
  114. %hurd-systems
  115. %hydra-supported-systems
  116. supported-package?
  117. &package-error
  118. package-error?
  119. package-error-package
  120. &package-input-error
  121. package-input-error?
  122. package-error-invalid-input
  123. &package-cross-build-system-error
  124. package-cross-build-system-error?
  125. package->bag
  126. bag->derivation
  127. bag-direct-inputs
  128. bag-transitive-inputs
  129. bag-transitive-host-inputs
  130. bag-transitive-build-inputs
  131. bag-transitive-target-inputs
  132. package-closure
  133. default-guile
  134. default-guile-derivation
  135. set-guile-for-build
  136. package-file
  137. package->derivation
  138. package->cross-derivation
  139. origin->derivation))
  140. ;;; Commentary:
  141. ;;;
  142. ;;; This module provides a high-level mechanism to define packages in a
  143. ;;; Guix-based distribution.
  144. ;;;
  145. ;;; Code:
  146. ;; The source of a package, such as a tarball URL and fetcher---called
  147. ;; "origin" to avoid name clash with `package-source', `source', etc.
  148. (define-record-type* <origin>
  149. origin make-origin
  150. origin?
  151. this-origin
  152. (uri origin-uri) ; string
  153. (method origin-method) ; procedure
  154. (sha256 origin-sha256) ; bytevector
  155. (file-name origin-file-name (default #f)) ; optional file name
  156. ;; Patches are delayed so that the 'search-patch' calls are made lazily,
  157. ;; which reduces I/O on startup and allows patch-not-found errors to be
  158. ;; gracefully handled at run time.
  159. (patches origin-patches ; list of file names
  160. (default '()) (delayed))
  161. (snippet origin-snippet (default #f)) ; sexp or #f
  162. (patch-flags origin-patch-flags ; list of strings
  163. (default '("-p1")))
  164. ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
  165. ;; used to specify these dependencies when needed.
  166. (patch-inputs origin-patch-inputs ; input list or #f
  167. (default #f))
  168. (modules origin-modules ; list of module names
  169. (default '()))
  170. (patch-guile origin-patch-guile ; package or #f
  171. (default #f)))
  172. (define (print-origin origin port)
  173. "Write a concise representation of ORIGIN to PORT."
  174. (match origin
  175. (($ <origin> uri method sha256 file-name patches)
  176. (simple-format port "#<origin ~s ~a ~s ~a>"
  177. uri (bytevector->base32-string sha256)
  178. (force patches)
  179. (number->string (object-address origin) 16)))))
  180. (set-record-type-printer! <origin> print-origin)
  181. (define-syntax base32
  182. (lambda (s)
  183. "Return the bytevector corresponding to the given Nix-base32
  184. representation."
  185. (syntax-case s ()
  186. ((_ str)
  187. (string? (syntax->datum #'str))
  188. ;; A literal string: do the conversion at expansion time.
  189. (with-syntax ((bv (nix-base32-string->bytevector
  190. (syntax->datum #'str))))
  191. #''bv))
  192. ((_ str)
  193. #'(nix-base32-string->bytevector str)))))
  194. (define (origin-actual-file-name origin)
  195. "Return the file name of ORIGIN, either its 'file-name' field or the file
  196. name of its URI."
  197. (define (uri->file-name uri)
  198. ;; Return the 'base name' of URI or URI itself, where URI is a string.
  199. (let ((path (and=> (string->uri uri) uri-path)))
  200. (if path
  201. (basename path)
  202. uri)))
  203. (or (origin-file-name origin)
  204. (match (origin-uri origin)
  205. ((head . tail)
  206. (uri->file-name head))
  207. ((? string? uri)
  208. (uri->file-name uri))
  209. (else
  210. ;; git, svn, cvs, etc. reference
  211. #f))))
  212. (define %supported-systems
  213. ;; This is the list of system types that are supported. By default, we
  214. ;; expect all packages to build successfully here.
  215. '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux" "mips64el-linux"))
  216. (define %hurd-systems
  217. ;; The GNU/Hurd systems for which support is being developed.
  218. '("i586-gnu" "i686-gnu"))
  219. (define %hydra-supported-systems
  220. ;; This is the list of system types for which build machines are available.
  221. ;;
  222. ;; XXX: MIPS is unavailable in CI:
  223. ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
  224. (fold delete %supported-systems '("mips64el-linux")))
  225. ;; A package.
  226. (define-record-type* <package>
  227. package make-package
  228. package?
  229. this-package
  230. (name package-name) ; string
  231. (version package-version) ; string
  232. (source package-source) ; <origin> instance
  233. (build-system package-build-system) ; build system
  234. (arguments package-arguments ; arguments for the build method
  235. (default '()) (thunked))
  236. (inputs package-inputs ; input packages or derivations
  237. (default '()) (thunked))
  238. (propagated-inputs package-propagated-inputs ; same, but propagated
  239. (default '()) (thunked))
  240. (native-inputs package-native-inputs ; native input packages/derivations
  241. (default '()) (thunked))
  242. (outputs package-outputs ; list of strings
  243. (default '("out")))
  244. ; lists of
  245. ; <search-path-specification>,
  246. ; for native and cross
  247. ; inputs
  248. (native-search-paths package-native-search-paths (default '()))
  249. (search-paths package-search-paths (default '()))
  250. ;; The 'replacement' field is marked as "innate" because it never makes
  251. ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
  252. (replacement package-replacement ; package | #f
  253. (default #f) (thunked) (innate))
  254. (synopsis package-synopsis) ; one-line description
  255. (description package-description) ; one or two paragraphs
  256. (license package-license)
  257. (home-page package-home-page)
  258. (supported-systems package-supported-systems ; list of strings
  259. (default %supported-systems))
  260. (properties package-properties (default '())) ; alist for anything else
  261. (location package-location
  262. (default (and=> (current-source-location)
  263. source-properties->location))
  264. (innate)))
  265. (set-record-type-printer! <package>
  266. (lambda (package port)
  267. (let ((loc (package-location package))
  268. (format simple-format))
  269. (format port "#<package ~a@~a ~a~a>"
  270. (package-name package)
  271. (package-version package)
  272. (if loc
  273. (format #f "~a:~a "
  274. (location-file loc)
  275. (location-line loc))
  276. "")
  277. (number->string (object-address
  278. package)
  279. 16)))))
  280. (define (package-upstream-name package)
  281. "Return the upstream name of PACKAGE, which could be different from the name
  282. it has in Guix."
  283. (or (assq-ref (package-properties package) 'upstream-name)
  284. (package-name package)))
  285. (define (hidden-package p)
  286. "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
  287. user interfaces, ignores."
  288. (package
  289. (inherit p)
  290. (properties `((hidden? . #t)
  291. ,@(package-properties p)))))
  292. (define (hidden-package? p)
  293. "Return true if P is \"hidden\"--i.e., must not be visible to user
  294. interfaces."
  295. (assoc-ref (package-properties p) 'hidden?))
  296. (define (package-superseded p)
  297. "Return the package the supersedes P, or #f if P is still current."
  298. (assoc-ref (package-properties p) 'superseded))
  299. (define (deprecated-package old-name p)
  300. "Return a package called OLD-NAME and marked as superseded by P, a package
  301. object."
  302. (package
  303. (inherit p)
  304. (name old-name)
  305. (properties `((superseded . ,p)))))
  306. (define (package-field-location package field)
  307. "Return the source code location of the definition of FIELD for PACKAGE, or
  308. #f if it could not be determined."
  309. (define (goto port line column)
  310. (unless (and (= (port-column port) (- column 1))
  311. (= (port-line port) (- line 1)))
  312. (unless (eof-object? (read-char port))
  313. (goto port line column))))
  314. (match (package-location package)
  315. (($ <location> file line column)
  316. (catch 'system-error
  317. (lambda ()
  318. ;; In general we want to keep relative file names for modules.
  319. (with-fluids ((%file-port-name-canonicalization 'relative))
  320. (call-with-input-file (search-path %load-path file)
  321. (lambda (port)
  322. (goto port line column)
  323. (match (read port)
  324. (('package inits ...)
  325. (let ((field (assoc field inits)))
  326. (match field
  327. ((_ value)
  328. ;; Put the `or' here, and not in the first argument of
  329. ;; `and=>', to work around a compiler bug in 2.0.5.
  330. (or (and=> (source-properties value)
  331. source-properties->location)
  332. (and=> (source-properties field)
  333. source-properties->location)))
  334. (_
  335. #f))))
  336. (_
  337. #f))))))
  338. (lambda _
  339. #f)))
  340. (_ #f)))
  341. ;; Error conditions.
  342. (define-condition-type &package-error &error
  343. package-error?
  344. (package package-error-package))
  345. (define-condition-type &package-input-error &package-error
  346. package-input-error?
  347. (input package-error-invalid-input))
  348. (define-condition-type &package-cross-build-system-error &package-error
  349. package-cross-build-system-error?)
  350. (define* (package-full-name package #:optional (delimiter "@"))
  351. "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
  352. DELIMITER (a string), you can customize what will appear between the name and
  353. the version. By default, DELIMITER is \"@\"."
  354. (string-append (package-name package) delimiter (package-version package)))
  355. (define (patch-file-name patch)
  356. "Return the basename of PATCH's file name, or #f if the file name could not
  357. be determined."
  358. (match patch
  359. ((? string?)
  360. (basename patch))
  361. ((? origin?)
  362. (and=> (origin-actual-file-name patch) basename))))
  363. (define %vulnerability-regexp
  364. ;; Regexp matching a CVE identifier in patch file names.
  365. (make-regexp "CVE-[0-9]{4}-[0-9]+"))
  366. (define (package-patched-vulnerabilities package)
  367. "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
  368. identifiers. The result is inferred from the file names of patches."
  369. (define (patch-vulnerabilities patch)
  370. (map (cut match:substring <> 0)
  371. (list-matches %vulnerability-regexp patch)))
  372. (let ((patches (filter-map patch-file-name
  373. (or (and=> (package-source package)
  374. origin-patches)
  375. '()))))
  376. (append-map patch-vulnerabilities patches)))
  377. (define (%standard-patch-inputs)
  378. (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
  379. 'canonical-package))
  380. (ref (lambda (module var)
  381. (canonical
  382. (module-ref (resolve-interface module) var)))))
  383. `(("tar" ,(ref '(gnu packages base) 'tar))
  384. ("xz" ,(ref '(gnu packages compression) 'xz))
  385. ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
  386. ("gzip" ,(ref '(gnu packages compression) 'gzip))
  387. ("lzip" ,(ref '(gnu packages compression) 'lzip))
  388. ("unzip" ,(ref '(gnu packages compression) 'unzip))
  389. ("patch" ,(ref '(gnu packages base) 'patch))
  390. ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
  391. (define (default-guile)
  392. "Return the default Guile package used to run the build code of
  393. derivations."
  394. (let ((distro (resolve-interface '(gnu packages commencement))))
  395. (module-ref distro 'guile-final)))
  396. (define (guile-2.0)
  397. "Return Guile 2.0."
  398. ;; FIXME: This is used as a workaround for <https://bugs.gnu.org/28211> when
  399. ;; grafting packages.
  400. (let ((distro (resolve-interface '(gnu packages guile))))
  401. (module-ref distro 'guile-2.0)))
  402. (define* (default-guile-derivation #:optional (system (%current-system)))
  403. "Return the derivation for SYSTEM of the default Guile package used to run
  404. the build code of derivation."
  405. (package->derivation (default-guile) system
  406. #:graft? #f))
  407. (define* (patch-and-repack source patches
  408. #:key
  409. inputs
  410. (snippet #f)
  411. (flags '("-p1"))
  412. (modules '())
  413. (guile-for-build (%guile-for-build))
  414. (system (%current-system)))
  415. "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
  416. repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
  417. it must be an s-expression that will run from within the directory where
  418. SOURCE was unpacked, after all of PATCHES have been applied. MODULES
  419. specifies modules in scope when evaluating SNIPPET."
  420. (define source-file-name
  421. ;; SOURCE is usually a derivation, but it could be a store file.
  422. (if (derivation? source)
  423. (derivation->output-path source)
  424. source))
  425. (define lookup-input
  426. ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
  427. ;; so deal with that.
  428. (let ((inputs (or inputs (%standard-patch-inputs))))
  429. (lambda (name)
  430. (match (assoc-ref inputs name)
  431. ((package) package)
  432. (#f #f)))))
  433. (define decompression-type
  434. (cond ((string-suffix? "gz" source-file-name) "gzip")
  435. ((string-suffix? "Z" source-file-name) "gzip")
  436. ((string-suffix? "bz2" source-file-name) "bzip2")
  437. ((string-suffix? "lz" source-file-name) "lzip")
  438. ((string-suffix? "zip" source-file-name) "unzip")
  439. (else "xz")))
  440. (define original-file-name
  441. ;; Remove the store prefix plus the slash, hash, and hyphen.
  442. (let* ((sans (string-drop source-file-name
  443. (+ (string-length (%store-prefix)) 1)))
  444. (dash (string-index sans #\-)))
  445. (string-drop sans (+ 1 dash))))
  446. (define (numeric-extension? file-name)
  447. ;; Return true if FILE-NAME ends with digits.
  448. (and=> (file-extension file-name)
  449. (cut string-every char-set:hex-digit <>)))
  450. (define (checkout? directory)
  451. ;; Return true if DIRECTORY is a checkout (git, svn, etc).
  452. (string-suffix? "-checkout" directory))
  453. (define (tarxz-name file-name)
  454. ;; Return a '.tar.xz' file name based on FILE-NAME.
  455. (let ((base (cond ((numeric-extension? file-name)
  456. original-file-name)
  457. ((checkout? file-name)
  458. (string-drop-right file-name 9))
  459. (else (file-sans-extension file-name)))))
  460. (string-append base
  461. (if (equal? (file-extension base) "tar")
  462. ".xz"
  463. ".tar.xz"))))
  464. (define instantiate-patch
  465. (match-lambda
  466. ((? string? patch) ;deprecated
  467. (interned-file patch #:recursive? #t))
  468. ((? struct? patch) ;origin, local-file, etc.
  469. (lower-object patch system))))
  470. (mlet %store-monad ((tar -> (lookup-input "tar"))
  471. (xz -> (lookup-input "xz"))
  472. (patch -> (lookup-input "patch"))
  473. (locales -> (lookup-input "locales"))
  474. (decomp -> (lookup-input decompression-type))
  475. (patches (sequence %store-monad
  476. (map instantiate-patch patches))))
  477. (define build
  478. (with-imported-modules '((guix build utils))
  479. #~(begin
  480. (use-modules (ice-9 ftw)
  481. (srfi srfi-1)
  482. (guix build utils))
  483. ;; The --sort option was added to GNU tar in version 1.28, released
  484. ;; 2014-07-28. During bootstrap we must cope with older versions.
  485. (define tar-supports-sort?
  486. (zero? (system* (string-append #+tar "/bin/tar")
  487. "cf" "/dev/null" "--files-from=/dev/null"
  488. "--sort=name")))
  489. (define (apply-patch patch)
  490. (format (current-error-port) "applying '~a'...~%" patch)
  491. ;; Use '--force' so that patches that do not apply perfectly are
  492. ;; rejected. Use '--no-backup-if-mismatch' to prevent making
  493. ;; "*.orig" file if a patch is applied with offset.
  494. (invoke (string-append #+patch "/bin/patch")
  495. "--force" "--no-backup-if-mismatch"
  496. #+@flags "--input" patch))
  497. (define (first-file directory)
  498. ;; Return the name of the first file in DIRECTORY.
  499. (car (scandir directory
  500. (lambda (name)
  501. (not (member name '("." "..")))))))
  502. ;; Encoding/decoding errors shouldn't be silent.
  503. (fluid-set! %default-port-conversion-strategy 'error)
  504. (when #+locales
  505. ;; First of all, install a UTF-8 locale so that UTF-8 file names
  506. ;; are correctly interpreted. During bootstrap, LOCALES is #f.
  507. (setenv "LOCPATH"
  508. (string-append #+locales "/lib/locale/"
  509. #+(and locales
  510. (version-major+minor
  511. (package-version locales)))))
  512. (setlocale LC_ALL "en_US.utf8"))
  513. (setenv "PATH" (string-append #+xz "/bin" ":"
  514. #+decomp "/bin"))
  515. ;; SOURCE may be either a directory or a tarball.
  516. (if (file-is-directory? #+source)
  517. (let* ((store (%store-directory))
  518. (len (+ 1 (string-length store)))
  519. (base (string-drop #+source len))
  520. (dash (string-index base #\-))
  521. (directory (string-drop base (+ 1 dash))))
  522. (mkdir directory)
  523. (copy-recursively #+source directory))
  524. #+(if (string=? decompression-type "unzip")
  525. #~(invoke "unzip" #+source)
  526. #~(invoke (string-append #+tar "/bin/tar")
  527. "xvf" #+source)))
  528. (let ((directory (first-file ".")))
  529. (format (current-error-port)
  530. "source is under '~a'~%" directory)
  531. (chdir directory)
  532. (for-each apply-patch '#+patches)
  533. (let ((result #+(if snippet
  534. #~(let ((module (make-fresh-user-module)))
  535. (module-use-interfaces!
  536. module
  537. (map resolve-interface '#+modules))
  538. ((@ (system base compile) compile)
  539. '#+snippet
  540. #:to 'value
  541. #:opts %auto-compilation-options
  542. #:env module))
  543. #~#t)))
  544. ;; Issue a warning unless the result is #t.
  545. (unless (eqv? result #t)
  546. (format (current-error-port) "\
  547. ## WARNING: the snippet returned `~s'. Return values other than #t
  548. ## are deprecated. Please migrate this package so that its snippet
  549. ## reports errors by raising an exception, and otherwise returns #t.~%"
  550. result))
  551. (unless result
  552. (error "snippet returned false")))
  553. (chdir "..")
  554. (unless tar-supports-sort?
  555. (call-with-output-file ".file_list"
  556. (lambda (port)
  557. (for-each (lambda (name)
  558. (format port "~a~%" name))
  559. (find-files directory
  560. #:directories? #t
  561. #:fail-on-error? #t)))))
  562. (apply invoke
  563. (string-append #+tar "/bin/tar")
  564. "cvfa" #$output
  565. ;; avoid non-determinism in the archive
  566. "--mtime=@0"
  567. "--owner=root:0"
  568. "--group=root:0"
  569. (if tar-supports-sort?
  570. `("--sort=name"
  571. ,directory)
  572. '("--no-recursion"
  573. "--files-from=.file_list")))))))
  574. (let ((name (tarxz-name original-file-name)))
  575. (gexp->derivation name build
  576. #:graft? #f
  577. #:system system
  578. #:guile-for-build guile-for-build
  579. #:properties `((type . origin)
  580. (patches . ,(length patches)))))))
  581. (define (transitive-inputs inputs)
  582. "Return the closure of INPUTS when considering the 'propagated-inputs'
  583. edges. Omit duplicate inputs, except for those already present in INPUTS
  584. itself.
  585. This is implemented as a breadth-first traversal such that INPUTS is
  586. preserved, and only duplicate propagated inputs are removed."
  587. (define (seen? seen item outputs)
  588. ;; FIXME: We're using pointer identity here, which is extremely sensitive
  589. ;; to memoization in package-producing procedures; see
  590. ;; <https://bugs.gnu.org/30155>.
  591. (match (vhash-assq item seen)
  592. ((_ . o) (equal? o outputs))
  593. (_ #f)))
  594. (let loop ((inputs inputs)
  595. (result '())
  596. (propagated '())
  597. (first? #t)
  598. (seen vlist-null))
  599. (match inputs
  600. (()
  601. (if (null? propagated)
  602. (reverse result)
  603. (loop (reverse (concatenate propagated)) result '() #f seen)))
  604. (((and input (label (? package? package) outputs ...)) rest ...)
  605. (if (and (not first?) (seen? seen package outputs))
  606. (loop rest result propagated first? seen)
  607. (loop rest
  608. (cons input result)
  609. (cons (package-propagated-inputs package) propagated)
  610. first?
  611. (vhash-consq package outputs seen))))
  612. ((input rest ...)
  613. (loop rest (cons input result) propagated first? seen)))))
  614. (define (package-direct-sources package)
  615. "Return all source origins associated with PACKAGE; including origins in
  616. PACKAGE's inputs."
  617. `(,@(or (and=> (package-source package) list) '())
  618. ,@(filter-map (match-lambda
  619. ((_ (? origin? orig) _ ...)
  620. orig)
  621. (_ #f))
  622. (package-direct-inputs package))))
  623. (define (package-transitive-sources package)
  624. "Return PACKAGE's direct sources, and their direct sources, recursively."
  625. (delete-duplicates
  626. (concatenate (filter-map (match-lambda
  627. ((_ (? origin? orig) _ ...)
  628. (list orig))
  629. ((_ (? package? p) _ ...)
  630. (package-direct-sources p))
  631. (_ #f))
  632. (bag-transitive-inputs
  633. (package->bag package))))))
  634. (define (package-direct-inputs package)
  635. "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
  636. with their propagated inputs."
  637. (append (package-native-inputs package)
  638. (package-inputs package)
  639. (package-propagated-inputs package)))
  640. (define (package-transitive-inputs package)
  641. "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
  642. with their propagated inputs, recursively."
  643. (transitive-inputs (package-direct-inputs package)))
  644. (define (package-transitive-target-inputs package)
  645. "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
  646. along with their propagated inputs, recursively. This only includes inputs
  647. for the target system, and not native inputs."
  648. (transitive-inputs (append (package-inputs package)
  649. (package-propagated-inputs package))))
  650. (define (package-transitive-native-inputs package)
  651. "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
  652. along with their propagated inputs, recursively. This only includes inputs
  653. for the host system (\"native inputs\"), and not target inputs."
  654. (transitive-inputs (package-native-inputs package)))
  655. (define (package-transitive-propagated-inputs package)
  656. "Return the propagated inputs of PACKAGE, and their propagated inputs,
  657. recursively."
  658. (transitive-inputs (package-propagated-inputs package)))
  659. (define (package-transitive-native-search-paths package)
  660. "Return the list of search paths for PACKAGE and its propagated inputs,
  661. recursively."
  662. (append (package-native-search-paths package)
  663. (append-map (match-lambda
  664. ((label (? package? p) _ ...)
  665. (package-native-search-paths p))
  666. (_
  667. '()))
  668. (package-transitive-propagated-inputs package))))
  669. (define (transitive-input-references alist inputs)
  670. "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
  671. in INPUTS and their transitive propagated inputs."
  672. (define label
  673. (match-lambda
  674. ((label . _)
  675. label)))
  676. (map (lambda (input)
  677. `(assoc-ref ,alist ,(label input)))
  678. (transitive-inputs inputs)))
  679. (define package-transitive-supported-systems
  680. (let ()
  681. (define supported-systems
  682. (mlambda (package system)
  683. (parameterize ((%current-system system))
  684. (fold (lambda (input systems)
  685. (match input
  686. ((label (? package? package) . _)
  687. (lset-intersection string=? systems
  688. (supported-systems package system)))
  689. (_
  690. systems)))
  691. (package-supported-systems package)
  692. (bag-direct-inputs (package->bag package))))))
  693. (lambda* (package #:optional (system (%current-system)))
  694. "Return the intersection of the systems supported by PACKAGE and those
  695. supported by its dependencies."
  696. (supported-systems package system))))
  697. (define* (supported-package? package #:optional (system (%current-system)))
  698. "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
  699. dependencies are known to build on SYSTEM."
  700. (member system (package-transitive-supported-systems package system)))
  701. (define (bag-direct-inputs bag)
  702. "Same as 'package-direct-inputs', but applied to a bag."
  703. (append (bag-build-inputs bag)
  704. (bag-host-inputs bag)
  705. (bag-target-inputs bag)))
  706. (define (bag-transitive-inputs bag)
  707. "Same as 'package-transitive-inputs', but applied to a bag."
  708. (transitive-inputs (bag-direct-inputs bag)))
  709. (define (bag-transitive-build-inputs bag)
  710. "Same as 'package-transitive-native-inputs', but applied to a bag."
  711. (transitive-inputs (bag-build-inputs bag)))
  712. (define (bag-transitive-host-inputs bag)
  713. "Same as 'package-transitive-target-inputs', but applied to a bag."
  714. (parameterize ((%current-target-system (bag-target bag)))
  715. (transitive-inputs (bag-host-inputs bag))))
  716. (define (bag-transitive-target-inputs bag)
  717. "Return the \"target inputs\" of BAG, recursively."
  718. (transitive-inputs (bag-target-inputs bag)))
  719. (define* (package-closure packages #:key (system (%current-system)))
  720. "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
  721. packages they depend on, recursively."
  722. (let loop ((packages packages)
  723. (visited vlist-null)
  724. (closure (list->setq packages)))
  725. (match packages
  726. (()
  727. (set->list closure))
  728. ((package . rest)
  729. (if (vhash-assq package visited)
  730. (loop rest visited closure)
  731. (let* ((bag (package->bag package system))
  732. (dependencies (filter-map (match-lambda
  733. ((label (? package? package) . _)
  734. package)
  735. (_ #f))
  736. (bag-direct-inputs bag))))
  737. (loop (append dependencies rest)
  738. (vhash-consq package #t visited)
  739. (fold set-insert closure dependencies))))))))
  740. (define* (package-mapping proc #:optional (cut? (const #f)))
  741. "Return a procedure that, given a package, applies PROC to all the packages
  742. depended on and returns the resulting package. The procedure stops recursion
  743. when CUT? returns true for a given package."
  744. (define (rewrite input)
  745. (match input
  746. ((label (? package? package) outputs ...)
  747. (let ((proc (if (cut? package) proc replace)))
  748. (cons* label (proc package) outputs)))
  749. (_
  750. input)))
  751. (define replace
  752. (mlambdaq (p)
  753. ;; Return a variant of P with PROC applied to P and its explicit
  754. ;; dependencies, recursively. Memoize the transformations. Failing to
  755. ;; do that, we would build a huge object graph with lots of duplicates,
  756. ;; which in turns prevents us from benefiting from memoization in
  757. ;; 'package-derivation'.
  758. (let ((p (proc p)))
  759. (package
  760. (inherit p)
  761. (location (package-location p))
  762. (inputs (map rewrite (package-inputs p)))
  763. (native-inputs (map rewrite (package-native-inputs p)))
  764. (propagated-inputs (map rewrite (package-propagated-inputs p)))
  765. (replacement (and=> (package-replacement p) proc))))))
  766. replace)
  767. (define* (package-input-rewriting replacements
  768. #:optional (rewrite-name identity))
  769. "Return a procedure that, when passed a package, replaces its direct and
  770. indirect dependencies (but not its implicit inputs) according to REPLACEMENTS.
  771. REPLACEMENTS is a list of package pairs; the first element of each pair is the
  772. package to replace, and the second one is the replacement.
  773. Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
  774. package and returns its new name after rewrite."
  775. (define (rewrite p)
  776. (match (assq-ref replacements p)
  777. (#f (package
  778. (inherit p)
  779. (name (rewrite-name (package-name p)))))
  780. (new new)))
  781. (package-mapping rewrite (cut assq <> replacements)))
  782. (define (package-input-rewriting/spec replacements)
  783. "Return a procedure that, given a package, applies the given REPLACEMENTS to
  784. all the package graph (excluding implicit inputs). REPLACEMENTS is a list of
  785. spec/procedures pair; each spec is a package specification such as \"gcc\" or
  786. \"guile@2\", and each procedure takes a matching package and returns a
  787. replacement for that package."
  788. (define table
  789. (fold (lambda (replacement table)
  790. (match replacement
  791. ((spec . proc)
  792. (let-values (((name version)
  793. (package-name->name+version spec)))
  794. (vhash-cons name (list version proc) table)))))
  795. vlist-null
  796. replacements))
  797. (define (find-replacement package)
  798. (vhash-fold* (lambda (item proc)
  799. (or proc
  800. (match item
  801. ((#f proc)
  802. proc)
  803. ((version proc)
  804. (and (version-prefix? version
  805. (package-version package))
  806. proc)))))
  807. #f
  808. (package-name package)
  809. table))
  810. (define (rewrite package)
  811. (match (find-replacement package)
  812. (#f package)
  813. (proc (proc package))))
  814. (package-mapping rewrite find-replacement))
  815. (define-syntax-rule (package/inherit p overrides ...)
  816. "Like (package (inherit P) OVERRIDES ...), except that the same
  817. transformation is done to the package replacement, if any. P must be a bare
  818. identifier, and will be bound to either P or its replacement when evaluating
  819. OVERRIDES."
  820. (let loop ((p p))
  821. (package (inherit p)
  822. overrides ...
  823. (replacement (and=> (package-replacement p) loop)))))
  824. ;;;
  825. ;;; Package derivations.
  826. ;;;
  827. (define %derivation-cache
  828. ;; Package to derivation-path mapping.
  829. (make-weak-key-hash-table 100))
  830. (define (cache! cache package system thunk)
  831. "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
  832. SYSTEM."
  833. ;; FIXME: This memoization should be associated with the open store, because
  834. ;; otherwise it breaks when switching to a different store.
  835. (let ((result (thunk)))
  836. ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
  837. ;; same value for all structs (as of Guile 2.0.6), and because pointer
  838. ;; equality is sufficient in practice.
  839. (hashq-set! cache package
  840. `((,system . ,result)
  841. ,@(or (hashq-ref cache package) '())))
  842. result))
  843. (define-syntax cached
  844. (syntax-rules (=>)
  845. "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
  846. Return the cached result when available."
  847. ((_ (=> cache) package system body ...)
  848. (let ((thunk (lambda () body ...))
  849. (key system))
  850. (match (hashq-ref cache package)
  851. ((alist (... ...))
  852. (match (assoc-ref alist key)
  853. (#f (cache! cache package key thunk))
  854. (value value)))
  855. (#f
  856. (cache! cache package key thunk)))))
  857. ((_ package system body ...)
  858. (cached (=> %derivation-cache) package system body ...))))
  859. (define* (expand-input store package input system #:optional cross-system)
  860. "Expand INPUT, an input tuple, such that it contains only references to
  861. derivation paths or store paths. PACKAGE is only used to provide contextual
  862. information in exceptions."
  863. (define (intern file)
  864. ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
  865. ;; file permissions are preserved.
  866. (add-to-store store (basename file) #t "sha256" file))
  867. (define derivation
  868. (if cross-system
  869. (cut package-cross-derivation store <> cross-system system
  870. #:graft? #f)
  871. (cut package-derivation store <> system #:graft? #f)))
  872. (match input
  873. (((? string? name) (? package? package))
  874. (list name (derivation package)))
  875. (((? string? name) (? package? package)
  876. (? string? sub-drv))
  877. (list name (derivation package)
  878. sub-drv))
  879. (((? string? name)
  880. (and (? string?) (? derivation-path?) drv))
  881. (list name drv))
  882. (((? string? name)
  883. (and (? string?) (? file-exists? file)))
  884. ;; Add FILE to the store. When FILE is in the sub-directory of a
  885. ;; store path, it needs to be added anyway, so it can be used as a
  886. ;; source.
  887. (list name (intern file)))
  888. (((? string? name) (? struct? source))
  889. ;; 'package-source-derivation' calls 'lower-object', which can throw
  890. ;; '&gexp-input-error'. However '&gexp-input-error' lacks source
  891. ;; location info, so we catch and rethrow here (XXX: not optimal
  892. ;; performance-wise).
  893. (guard (c ((gexp-input-error? c)
  894. (raise (condition
  895. (&package-input-error
  896. (package package)
  897. (input (gexp-error-invalid-input c)))))))
  898. (list name (package-source-derivation store source system))))
  899. (x
  900. (raise (condition (&package-input-error
  901. (package package)
  902. (input x)))))))
  903. (define %bag-cache
  904. ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
  905. ;; It significantly speeds things up when doing repeated calls to
  906. ;; 'package->bag' as is the case when building a profile.
  907. (make-weak-key-hash-table 200))
  908. (define* (package->bag package #:optional
  909. (system (%current-system))
  910. (target (%current-target-system))
  911. #:key (graft? (%graft?)))
  912. "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
  913. and return it."
  914. (cached (=> %bag-cache)
  915. package (list system target graft?)
  916. ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
  917. ;; field values can refer to it.
  918. (parameterize ((%current-system system)
  919. (%current-target-system target))
  920. (match (if graft?
  921. (or (package-replacement package) package)
  922. package)
  923. ((and self
  924. ($ <package> name version source build-system
  925. args inputs propagated-inputs native-inputs
  926. outputs))
  927. ;; Even though we prefer to use "@" to separate the package
  928. ;; name from the package version in various user-facing parts
  929. ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
  930. ;; prohibits the use of "@", so use "-" instead.
  931. (or (make-bag build-system (string-append name "-" version)
  932. #:system system
  933. #:target target
  934. #:source source
  935. #:inputs (append (inputs self)
  936. (propagated-inputs self))
  937. #:outputs outputs
  938. #:native-inputs (native-inputs self)
  939. #:arguments (args self))
  940. (raise (if target
  941. (condition
  942. (&package-cross-build-system-error
  943. (package package)))
  944. (condition
  945. (&package-error
  946. (package package)))))))))))
  947. (define %graft-cache
  948. ;; 'eq?' cache mapping package objects to a graft corresponding to their
  949. ;; replacement package.
  950. (make-weak-key-hash-table 200))
  951. (define (input-graft store system)
  952. "Return a procedure that, given a package with a graft, returns a graft, and
  953. #f otherwise."
  954. (match-lambda
  955. ((? package? package)
  956. (let ((replacement (package-replacement package)))
  957. (and replacement
  958. (cached (=> %graft-cache) package system
  959. (let ((orig (package-derivation store package system
  960. #:graft? #f))
  961. (new (package-derivation store replacement system
  962. #:graft? #t)))
  963. (graft
  964. (origin orig)
  965. (replacement new)))))))
  966. (x
  967. #f)))
  968. (define (input-cross-graft store target system)
  969. "Same as 'input-graft', but for cross-compilation inputs."
  970. (match-lambda
  971. ((? package? package)
  972. (let ((replacement (package-replacement package)))
  973. (and replacement
  974. (let ((orig (package-cross-derivation store package target system
  975. #:graft? #f))
  976. (new (package-cross-derivation store replacement
  977. target system
  978. #:graft? #t)))
  979. (graft
  980. (origin orig)
  981. (replacement new))))))
  982. (_
  983. #f)))
  984. (define* (fold-bag-dependencies proc seed bag
  985. #:key (native? #t))
  986. "Fold PROC over the packages BAG depends on. Each package is visited only
  987. once, in depth-first order. If NATIVE? is true, restrict to native
  988. dependencies; otherwise, restrict to target dependencies."
  989. (define bag-direct-inputs*
  990. (if native?
  991. (lambda (bag)
  992. (append (bag-build-inputs bag)
  993. (bag-target-inputs bag)
  994. (if (bag-target bag)
  995. '()
  996. (bag-host-inputs bag))))
  997. bag-host-inputs))
  998. (define nodes
  999. (match (bag-direct-inputs* bag)
  1000. (((labels things _ ...) ...)
  1001. things)))
  1002. (let loop ((nodes nodes)
  1003. (result seed)
  1004. (visited (setq)))
  1005. (match nodes
  1006. (()
  1007. result)
  1008. (((? package? head) . tail)
  1009. (if (set-contains? visited head)
  1010. (loop tail result visited)
  1011. (let ((inputs (bag-direct-inputs* (package->bag head))))
  1012. (loop (match inputs
  1013. (((labels things _ ...) ...)
  1014. (append things tail)))
  1015. (proc head result)
  1016. (set-insert head visited)))))
  1017. ((head . tail)
  1018. (loop tail result visited)))))
  1019. (define* (bag-grafts store bag)
  1020. "Return the list of grafts potentially applicable to BAG. Potentially
  1021. applicable grafts are collected by looking at direct or indirect dependencies
  1022. of BAG that have a 'replacement'. Whether a graft is actually applicable
  1023. depends on whether the outputs of BAG depend on the items the grafts refer
  1024. to (see 'graft-derivation'.)"
  1025. (define system (bag-system bag))
  1026. (define target (bag-target bag))
  1027. (define native-grafts
  1028. (let ((->graft (input-graft store system)))
  1029. (fold-bag-dependencies (lambda (package grafts)
  1030. (match (->graft package)
  1031. (#f grafts)
  1032. (graft (cons graft grafts))))
  1033. '()
  1034. bag)))
  1035. (define target-grafts
  1036. (if target
  1037. (let ((->graft (input-cross-graft store target system)))
  1038. (fold-bag-dependencies (lambda (package grafts)
  1039. (match (->graft package)
  1040. (#f grafts)
  1041. (graft (cons graft grafts))))
  1042. '()
  1043. bag
  1044. #:native? #f))
  1045. '()))
  1046. ;; We can end up with several identical grafts if we stumble upon packages
  1047. ;; that are not 'eq?' but map to the same derivation (this can happen when
  1048. ;; using things like 'package-with-explicit-inputs'.) Hence the
  1049. ;; 'delete-duplicates' call.
  1050. (delete-duplicates
  1051. (append native-grafts target-grafts)))
  1052. (define* (package-grafts store package
  1053. #:optional (system (%current-system))
  1054. #:key target)
  1055. "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
  1056. TARGET."
  1057. (let* ((package (or (package-replacement package) package))
  1058. (bag (package->bag package system target)))
  1059. (bag-grafts store bag)))
  1060. (define* (bag->derivation store bag
  1061. #:optional context)
  1062. "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
  1063. a package object describing the context in which the call occurs, for improved
  1064. error reporting."
  1065. (if (bag-target bag)
  1066. (bag->cross-derivation store bag)
  1067. (let* ((system (bag-system bag))
  1068. (inputs (bag-transitive-inputs bag))
  1069. (input-drvs (map (cut expand-input store context <> system)
  1070. inputs))
  1071. (paths (delete-duplicates
  1072. (append-map (match-lambda
  1073. ((_ (? package? p) _ ...)
  1074. (package-native-search-paths
  1075. p))
  1076. (_ '()))
  1077. inputs))))
  1078. (apply (bag-build bag)
  1079. store (bag-name bag) input-drvs
  1080. #:search-paths paths
  1081. #:outputs (bag-outputs bag) #:system system
  1082. (bag-arguments bag)))))
  1083. (define* (bag->cross-derivation store bag
  1084. #:optional context)
  1085. "Return the derivation to build BAG, which is actually a cross build.
  1086. Optionally, CONTEXT can be a package object denoting the context of the call.
  1087. This is an internal procedure."
  1088. (let* ((system (bag-system bag))
  1089. (target (bag-target bag))
  1090. (host (bag-transitive-host-inputs bag))
  1091. (host-drvs (map (cut expand-input store context <> system target)
  1092. host))
  1093. (target* (bag-transitive-target-inputs bag))
  1094. (target-drvs (map (cut expand-input store context <> system)
  1095. target*))
  1096. (build (bag-transitive-build-inputs bag))
  1097. (build-drvs (map (cut expand-input store context <> system)
  1098. build))
  1099. (all (append build target* host))
  1100. (paths (delete-duplicates
  1101. (append-map (match-lambda
  1102. ((_ (? package? p) _ ...)
  1103. (package-search-paths p))
  1104. (_ '()))
  1105. all)))
  1106. (npaths (delete-duplicates
  1107. (append-map (match-lambda
  1108. ((_ (? package? p) _ ...)
  1109. (package-native-search-paths
  1110. p))
  1111. (_ '()))
  1112. all))))
  1113. (apply (bag-build bag)
  1114. store (bag-name bag)
  1115. #:native-drvs build-drvs
  1116. #:target-drvs (append host-drvs target-drvs)
  1117. #:search-paths paths
  1118. #:native-search-paths npaths
  1119. #:outputs (bag-outputs bag)
  1120. #:system system #:target target
  1121. (bag-arguments bag))))
  1122. (define* (package-derivation store package
  1123. #:optional (system (%current-system))
  1124. #:key (graft? (%graft?)))
  1125. "Return the <derivation> object of PACKAGE for SYSTEM."
  1126. ;; Compute the derivation and cache the result. Caching is important
  1127. ;; because some derivations, such as the implicit inputs of the GNU build
  1128. ;; system, will be queried many, many times in a row.
  1129. (cached package (cons system graft?)
  1130. (let* ((bag (package->bag package system #f #:graft? graft?))
  1131. (drv (bag->derivation store bag package)))
  1132. (if graft?
  1133. (match (bag-grafts store bag)
  1134. (()
  1135. drv)
  1136. (grafts
  1137. (let ((guile (package-derivation store (guile-2.0)
  1138. system #:graft? #f)))
  1139. ;; TODO: As an optimization, we can simply graft the tip
  1140. ;; of the derivation graph since 'graft-derivation'
  1141. ;; recurses anyway.
  1142. (graft-derivation store drv grafts
  1143. #:system system
  1144. #:guile guile))))
  1145. drv))))
  1146. (define* (package-cross-derivation store package target
  1147. #:optional (system (%current-system))
  1148. #:key (graft? (%graft?)))
  1149. "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
  1150. system identifying string)."
  1151. (cached package (list system target graft?)
  1152. (let* ((bag (package->bag package system target #:graft? graft?))
  1153. (drv (bag->derivation store bag package)))
  1154. (if graft?
  1155. (match (bag-grafts store bag)
  1156. (()
  1157. drv)
  1158. (grafts
  1159. (graft-derivation store drv grafts
  1160. #:system system
  1161. #:guile
  1162. (package-derivation store (guile-2.0)
  1163. system #:graft? #f))))
  1164. drv))))
  1165. (define* (package-output store package
  1166. #:optional (output "out") (system (%current-system)))
  1167. "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
  1168. symbolic output name, such as \"out\". Note that this procedure calls
  1169. `package-derivation', which is costly."
  1170. (let ((drv (package-derivation store package system)))
  1171. (derivation->output-path drv output)))
  1172. ;;;
  1173. ;;; Monadic interface.
  1174. ;;;
  1175. (define (set-guile-for-build guile)
  1176. "This monadic procedure changes the Guile currently used to run the build
  1177. code of derivations to GUILE, a package object."
  1178. (lambda (store)
  1179. (let ((guile (package-derivation store guile)))
  1180. (values (%guile-for-build guile) store))))
  1181. (define* (package-file package
  1182. #:optional file
  1183. #:key
  1184. system (output "out") target)
  1185. "Return as a monadic value the absolute file name of FILE within the
  1186. OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
  1187. OUTPUT directory of PACKAGE. When TARGET is true, use it as a
  1188. cross-compilation target triplet."
  1189. (lambda (store)
  1190. (define compute-derivation
  1191. (if target
  1192. (cut package-cross-derivation <> <> target <>)
  1193. package-derivation))
  1194. (let* ((system (or system (%current-system)))
  1195. (drv (compute-derivation store package system))
  1196. (out (derivation->output-path drv output)))
  1197. (values (if file
  1198. (string-append out "/" file)
  1199. out)
  1200. store))))
  1201. (define package->derivation
  1202. (store-lift package-derivation))
  1203. (define package->cross-derivation
  1204. (store-lift package-cross-derivation))
  1205. (define-gexp-compiler (package-compiler (package <package>) system target)
  1206. ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
  1207. ;; TARGET. This is used when referring to a package from within a gexp.
  1208. (if target
  1209. (package->cross-derivation package target system)
  1210. (package->derivation package system)))
  1211. (define* (origin->derivation origin
  1212. #:optional (system (%current-system)))
  1213. "Return the derivation corresponding to ORIGIN."
  1214. (match origin
  1215. (($ <origin> uri method sha256 name (= force ()) #f)
  1216. ;; No patches, no snippet: this is a fixed-output derivation.
  1217. (method uri 'sha256 sha256 name #:system system))
  1218. (($ <origin> uri method sha256 name (= force (patches ...)) snippet
  1219. (flags ...) inputs (modules ...) guile-for-build)
  1220. ;; Patches and/or a snippet.
  1221. (mlet %store-monad ((source (method uri 'sha256 sha256 name
  1222. #:system system))
  1223. (guile (package->derivation (or guile-for-build
  1224. (default-guile))
  1225. system
  1226. #:graft? #f)))
  1227. (patch-and-repack source patches
  1228. #:inputs inputs
  1229. #:snippet snippet
  1230. #:flags flags
  1231. #:system system
  1232. #:modules modules
  1233. #:guile-for-build guile)))))
  1234. (define-gexp-compiler (origin-compiler (origin <origin>) system target)
  1235. ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
  1236. ;; to an origin from within a gexp.
  1237. (origin->derivation origin system))
  1238. (define package-source-derivation ;somewhat deprecated
  1239. (let ((lower (store-lower lower-object)))
  1240. (lambda* (store source #:optional (system (%current-system)))
  1241. "Return the derivation or file corresponding to SOURCE, which can be an
  1242. a file name or any object handled by 'lower-object', such as an <origin>.
  1243. When SOURCE is a file name, return either the interned file name (if SOURCE is
  1244. outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
  1245. (match source
  1246. ((and (? string?) (? direct-store-path?) file)
  1247. file)
  1248. ((? string? file)
  1249. (add-to-store store (basename file) #t "sha256" file))
  1250. (_
  1251. (lower store source system))))))