packages.scm 66 KB

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