packages.scm 65 KB

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