hackage.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
  4. ;;; Copyright © 2016 Nikita <nikita@n0.is>
  5. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  6. ;;; Copyright © 2019 Robert Vollmert <rob@vllmrt.net>
  7. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  8. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  9. ;;; Copyright © 2019 Simon Tournier <zimon.toutoune@gmail.com>
  10. ;;;
  11. ;;; This file is part of GNU Guix.
  12. ;;;
  13. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  14. ;;; under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 3 of the License, or (at
  16. ;;; your option) any later version.
  17. ;;;
  18. ;;; GNU Guix is distributed in the hope that it will be useful, but
  19. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  25. (define-module (guix import hackage)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 regex)
  28. #:use-module (srfi srfi-71)
  29. #:use-module (srfi srfi-34)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-1)
  32. #:use-module ((guix download) #:select (download-to-store url-fetch))
  33. #:use-module ((guix utils) #:select (package-name->name+version
  34. canonical-newline-port))
  35. #:use-module (guix http-client)
  36. #:use-module (guix import utils)
  37. #:use-module (guix import cabal)
  38. #:use-module (guix store)
  39. #:use-module (gcrypt hash)
  40. #:use-module (guix base32)
  41. #:use-module (guix memoization)
  42. #:use-module (guix upstream)
  43. #:use-module (guix packages)
  44. #:autoload (guix build-system haskell) (hackage-uri)
  45. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  46. #:export (%hackage-url
  47. hackage->guix-package
  48. hackage-recursive-import
  49. %hackage-updater
  50. guix-package->hackage-name
  51. hackage-name->package-name
  52. hackage-fetch
  53. hackage-source-url
  54. hackage-cabal-url
  55. hackage-package?))
  56. (define ghc-standard-libraries
  57. ;; List of libraries distributed with ghc (as of 8.10.7).
  58. ;; Contents of …-ghc-8.10.7/lib/ghc-8.10.7
  59. '("ghc"
  60. "cabal" ;; in the output of `ghc-pkg list` Cabal is uppercased, but
  61. ;; hackage-name->package-name takes this into account.
  62. "win32" ;; similarly uppercased
  63. "array"
  64. "base"
  65. "binary"
  66. "bytestring"
  67. "containers"
  68. "deepseq"
  69. "directory"
  70. "exceptions"
  71. "filepath"
  72. "ghc"
  73. "ghc-boot"
  74. "ghc-boot-th"
  75. "ghc-compact"
  76. "ghc-heap"
  77. "ghc-prim"
  78. "ghci"
  79. "haskeline"
  80. "hpc"
  81. "integer-gmp"
  82. "libiserv"
  83. "mtl"
  84. "parsec"
  85. "pretty"
  86. "process"
  87. "stm"
  88. "template-haskell"
  89. "terminfo"
  90. "text"
  91. "time"
  92. "transformers"
  93. "unix"
  94. "xhtml"))
  95. (define package-name-prefix "ghc-")
  96. (define %hackage-url
  97. (make-parameter "https://hackage.haskell.org"))
  98. (define (hackage-source-url name version)
  99. "Given a Hackage package NAME and VERSION, return a url to the source
  100. tarball."
  101. (string-append (%hackage-url) "/package/"
  102. name "/" name "-" version ".tar.gz"))
  103. (define* (hackage-cabal-url name #:optional version)
  104. "Given a Hackage package NAME and VERSION, return a url to the corresponding
  105. .cabal file on Hackage. If VERSION is #f or missing, the url for the latest
  106. version is returned."
  107. (if version
  108. (string-append (%hackage-url) "/package/"
  109. name "-" version "/" name ".cabal")
  110. (string-append (%hackage-url) "/package/"
  111. name "/" name ".cabal")))
  112. (define (hackage-name->package-name name)
  113. "Given the NAME of a Cabal package, return the corresponding Guix name."
  114. (if (string-prefix? package-name-prefix name)
  115. (string-downcase name)
  116. (string-append package-name-prefix (string-downcase name))))
  117. (define guix-package->hackage-name
  118. (let ((uri-rx (make-regexp "(https?://hackage.haskell.org|mirror://hackage)/package/([^/]+)/.*"))
  119. (name-rx (make-regexp "(.*)-[0-9\\.]+")))
  120. (lambda (package)
  121. "Given a Guix package name, return the corresponding Hackage name."
  122. (let* ((source-url (and=> (package-source package) origin-uri))
  123. (name (match:substring (regexp-exec uri-rx source-url) 2)))
  124. (match (regexp-exec name-rx name)
  125. (#f name)
  126. (m (match:substring m 1)))))))
  127. (define (read-cabal-and-hash port)
  128. "Read a Cabal file from PORT and return it and its hash in nix-base32
  129. format as two values."
  130. (let ((port get-hash (open-sha256-input-port port)))
  131. (values (read-cabal (canonical-newline-port port))
  132. (bytevector->nix-base32-string (get-hash)))))
  133. (define (hackage-fetch-and-hash name-version)
  134. "Fetch the latest Cabal revision for the package NAME-VERSION, and return
  135. two values: the parsed Cabal file and its hash in nix-base32 format. If the
  136. version part is omitted from the package name, then fetch the latest
  137. version. On failure, both return values will be #f."
  138. (guard (c ((and (http-get-error? c)
  139. (= 404 (http-get-error-code c)))
  140. (values #f #f))) ;"expected" if package is unknown
  141. (let* ((name version (package-name->name+version name-version))
  142. (url (hackage-cabal-url name version))
  143. (port _ (http-fetch url))
  144. (cabal hash (read-cabal-and-hash port)))
  145. (close-port port)
  146. (values cabal hash))))
  147. (define (hackage-fetch name-version)
  148. "Return the Cabal file for the package NAME-VERSION, or #f on failure. If
  149. the version part is omitted from the package name, then return the latest
  150. version."
  151. (let ((cabal hash (hackage-fetch-and-hash name-version)))
  152. cabal))
  153. (define string->license
  154. ;; List of valid values from
  155. ;; https://www.haskell.org
  156. ;; /cabal/release/cabal-latest/doc/API/Cabal/Distribution-License.html.
  157. (match-lambda
  158. ("GPL-2" 'license:gpl2)
  159. ("GPL-3" 'license:gpl3)
  160. ("GPL" "'gpl??")
  161. ("AGPL-3" 'license:agpl3)
  162. ("AGPL" "'agpl??")
  163. ("LGPL-2.1" 'license:lgpl2.1)
  164. ("LGPL-3" 'license:lgpl3)
  165. ("LGPL" "'lgpl??")
  166. ("BSD2" 'license:bsd-2)
  167. ("BSD3" 'license:bsd-3)
  168. ("BSD-3-Clause" 'license:bsd-3)
  169. ("MIT" 'license:expat)
  170. ("ISC" 'license:isc)
  171. ("MPL" 'license:mpl2.0)
  172. ("Apache-2.0" 'license:asl2.0)
  173. ("PublicDomain" 'license:public-domain)
  174. ((x) (string->license x))
  175. ((lst ...) `(list ,@(map string->license lst)))
  176. (_ #f)))
  177. (define (cabal-dependencies->names cabal)
  178. "Return the list of dependencies names from the CABAL package object,
  179. not including test suite dependencies or custom-setup dependencies."
  180. (let* ((lib (cabal-package-library cabal))
  181. (lib-deps (if (pair? lib)
  182. (map cabal-dependency-name
  183. (append-map cabal-library-dependencies lib))
  184. '()))
  185. (exe (cabal-package-executables cabal))
  186. (exe-deps (if (pair? exe)
  187. (map cabal-dependency-name
  188. (append-map cabal-executable-dependencies exe))
  189. '())))
  190. (delete-duplicates (append lib-deps exe-deps))))
  191. (define (cabal-test-dependencies->names cabal)
  192. "Return the list of test suite dependencies from the CABAL package
  193. object."
  194. (let* ((ts (cabal-package-test-suites cabal))
  195. (ts-deps (if (pair? ts)
  196. (map cabal-dependency-name
  197. (append-map cabal-test-suite-dependencies ts))
  198. '())))
  199. ts-deps))
  200. (define (cabal-custom-setup-dependencies->names cabal)
  201. "Return the list of custom-setup dependencies from the CABAL package
  202. object."
  203. (let* ((custom-setup-dependencies (or (and=> (cabal-package-custom-setup cabal)
  204. cabal-custom-setup-dependencies)
  205. '())))
  206. (map cabal-dependency-name custom-setup-dependencies)))
  207. (define (filter-dependencies dependencies own-names)
  208. "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
  209. list with the names of dependencies. OWN-NAMES is the name of the Cabal
  210. package being processed and its internal libaries and is used to filter
  211. references to itself."
  212. (let ((ignored-dependencies (map string-downcase
  213. (append own-names ghc-standard-libraries))))
  214. (filter (lambda (d) (not (member (string-downcase d) ignored-dependencies)))
  215. dependencies)))
  216. (define* (hackage-module->sexp cabal cabal-hash
  217. #:key (include-test-dependencies? #t))
  218. "Return the `package' S-expression for a Cabal package. CABAL is the
  219. representation of a Cabal file as produced by 'read-cabal'. CABAL-HASH is
  220. the hash of the Cabal file."
  221. (define name
  222. (cabal-package-name cabal))
  223. (define version
  224. (cabal-package-version cabal))
  225. (define revision
  226. (cabal-package-revision cabal))
  227. (define source-url
  228. (hackage-source-url name version))
  229. (define own-names (cons (cabal-package-name cabal)
  230. (filter (lambda (x) (not (eqv? x #f)))
  231. (map cabal-library-name (cabal-package-library cabal)))))
  232. (define hackage-dependencies
  233. (filter-dependencies (cabal-dependencies->names cabal) own-names))
  234. (define hackage-native-dependencies
  235. (lset-difference
  236. equal?
  237. (filter-dependencies
  238. (append (if include-test-dependencies?
  239. (cabal-test-dependencies->names cabal)
  240. '())
  241. (cabal-custom-setup-dependencies->names cabal))
  242. own-names)
  243. hackage-dependencies))
  244. (define dependencies
  245. (map string->symbol
  246. (map hackage-name->package-name
  247. hackage-dependencies)))
  248. (define native-dependencies
  249. (map string->symbol
  250. (map hackage-name->package-name
  251. hackage-native-dependencies)))
  252. (define (maybe-inputs input-type inputs)
  253. (match inputs
  254. (()
  255. '())
  256. ((inputs ...)
  257. (list (list input-type
  258. `(list ,@inputs))))))
  259. (define (maybe-arguments)
  260. (match (append (if (not include-test-dependencies?)
  261. '(#:tests? #f)
  262. '())
  263. (if (not (string-null? revision))
  264. `(#:cabal-revision (,revision ,cabal-hash))
  265. '()))
  266. (() '())
  267. (args `((arguments (,'quasiquote ,args))))))
  268. (let ((tarball (with-store store
  269. (download-to-store store source-url))))
  270. (values
  271. `(package
  272. (name ,(hackage-name->package-name name))
  273. (version ,version)
  274. (source (origin
  275. (method url-fetch)
  276. (uri (hackage-uri ,name version))
  277. (sha256
  278. (base32
  279. ,(if tarball
  280. (bytevector->nix-base32-string (file-sha256 tarball))
  281. "failed to download tar archive")))))
  282. (build-system haskell-build-system)
  283. ,@(maybe-inputs 'inputs dependencies)
  284. ,@(maybe-inputs 'native-inputs native-dependencies)
  285. ,@(maybe-arguments)
  286. (home-page ,(cabal-package-home-page cabal))
  287. (synopsis ,(cabal-package-synopsis cabal))
  288. (description ,(beautify-description (cabal-package-description cabal)))
  289. (license ,(string->license (cabal-package-license cabal))))
  290. (append hackage-dependencies hackage-native-dependencies))))
  291. (define* (hackage->guix-package package-name #:key
  292. (include-test-dependencies? #t)
  293. (port #f)
  294. (cabal-environment '()))
  295. "Fetch the Cabal file for PACKAGE-NAME from hackage.haskell.org, or, if the
  296. called with keyword parameter PORT, from PORT. Return the `package'
  297. S-expression corresponding to that package, or #f on failure.
  298. CABAL-ENVIRONMENT is an alist defining the environment in which the Cabal
  299. conditionals are evaluated. The accepted keys are: \"os\", \"arch\", \"impl\"
  300. and the name of a flag. The value associated with a flag has to be either the
  301. symbol 'true' or 'false'. The value associated with other keys has to conform
  302. to the Cabal file format definition. The default value associated with the
  303. keys \"os\", \"arch\" and \"impl\" is \"linux\", \"x86_64\" and \"ghc\"
  304. respectively."
  305. (let ((cabal-meta cabal-hash
  306. (if port
  307. (read-cabal-and-hash port)
  308. (hackage-fetch-and-hash package-name))))
  309. (if cabal-meta
  310. (hackage-module->sexp (eval-cabal cabal-meta cabal-environment)
  311. cabal-hash
  312. #:include-test-dependencies?
  313. include-test-dependencies?)
  314. (values #f '()))))
  315. (define hackage->guix-package/m ;memoized variant
  316. (memoize hackage->guix-package))
  317. (define* (hackage-recursive-import package-name . args)
  318. (recursive-import package-name
  319. #:repo->guix-package (lambda* (name #:key repo version)
  320. (apply hackage->guix-package/m
  321. (cons name args)))
  322. #:guix-name hackage-name->package-name))
  323. (define hackage-package?
  324. (let ((hackage-rx (make-regexp "(https?://hackage.haskell.org|mirror://hackage/)")))
  325. (url-predicate (cut regexp-exec hackage-rx <>))))
  326. (define (latest-release package)
  327. "Return an <upstream-source> for the latest release of PACKAGE."
  328. (let* ((hackage-name (guix-package->hackage-name package))
  329. (cabal-meta (hackage-fetch hackage-name)))
  330. (match cabal-meta
  331. (#f
  332. (format (current-error-port)
  333. "warning: failed to parse ~a~%"
  334. (hackage-cabal-url hackage-name))
  335. #f)
  336. ((_ *** ("version" (version)))
  337. (let ((url (hackage-uri hackage-name version)))
  338. (upstream-source
  339. (package (package-name package))
  340. (version version)
  341. (urls (list url))))))))
  342. (define %hackage-updater
  343. (upstream-updater
  344. (name 'hackage)
  345. (description "Updater for Hackage packages")
  346. (pred hackage-package?)
  347. (latest latest-release)))
  348. ;;; cabal.scm ends here