hackage.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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-name)
  208. "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
  209. list with the names of dependencies. OWN-NAME is the name of the Cabal
  210. package being processed and is used to filter references to itself."
  211. (filter (lambda (d) (not (member (string-downcase d)
  212. (cons own-name ghc-standard-libraries))))
  213. dependencies))
  214. (define* (hackage-module->sexp cabal cabal-hash
  215. #:key (include-test-dependencies? #t))
  216. "Return the `package' S-expression for a Cabal package. CABAL is the
  217. representation of a Cabal file as produced by 'read-cabal'. CABAL-HASH is
  218. the hash of the Cabal file."
  219. (define name
  220. (cabal-package-name cabal))
  221. (define version
  222. (cabal-package-version cabal))
  223. (define revision
  224. (cabal-package-revision cabal))
  225. (define source-url
  226. (hackage-source-url name version))
  227. (define hackage-dependencies
  228. (filter-dependencies (cabal-dependencies->names cabal)
  229. (cabal-package-name cabal)))
  230. (define hackage-native-dependencies
  231. (lset-difference
  232. equal?
  233. (filter-dependencies
  234. (append (if include-test-dependencies?
  235. (cabal-test-dependencies->names cabal)
  236. '())
  237. (cabal-custom-setup-dependencies->names cabal))
  238. (cabal-package-name cabal))
  239. hackage-dependencies))
  240. (define dependencies
  241. (map string->symbol
  242. (map hackage-name->package-name
  243. hackage-dependencies)))
  244. (define native-dependencies
  245. (map string->symbol
  246. (map hackage-name->package-name
  247. hackage-native-dependencies)))
  248. (define (maybe-inputs input-type inputs)
  249. (match inputs
  250. (()
  251. '())
  252. ((inputs ...)
  253. (list (list input-type
  254. `(list ,@inputs))))))
  255. (define (maybe-arguments)
  256. (match (append (if (not include-test-dependencies?)
  257. '(#:tests? #f)
  258. '())
  259. (if (not (string-null? revision))
  260. `(#:cabal-revision (,revision ,cabal-hash))
  261. '()))
  262. (() '())
  263. (args `((arguments (,'quasiquote ,args))))))
  264. (let ((tarball (with-store store
  265. (download-to-store store source-url))))
  266. (values
  267. `(package
  268. (name ,(hackage-name->package-name name))
  269. (version ,version)
  270. (source (origin
  271. (method url-fetch)
  272. (uri (hackage-uri ,name version))
  273. (sha256
  274. (base32
  275. ,(if tarball
  276. (bytevector->nix-base32-string (file-sha256 tarball))
  277. "failed to download tar archive")))))
  278. (build-system haskell-build-system)
  279. ,@(maybe-inputs 'inputs dependencies)
  280. ,@(maybe-inputs 'native-inputs native-dependencies)
  281. ,@(maybe-arguments)
  282. (home-page ,(cabal-package-home-page cabal))
  283. (synopsis ,(cabal-package-synopsis cabal))
  284. (description ,(beautify-description (cabal-package-description cabal)))
  285. (license ,(string->license (cabal-package-license cabal))))
  286. (append hackage-dependencies hackage-native-dependencies))))
  287. (define* (hackage->guix-package package-name #:key
  288. (include-test-dependencies? #t)
  289. (port #f)
  290. (cabal-environment '()))
  291. "Fetch the Cabal file for PACKAGE-NAME from hackage.haskell.org, or, if the
  292. called with keyword parameter PORT, from PORT. Return the `package'
  293. S-expression corresponding to that package, or #f on failure.
  294. CABAL-ENVIRONMENT is an alist defining the environment in which the Cabal
  295. conditionals are evaluated. The accepted keys are: \"os\", \"arch\", \"impl\"
  296. and the name of a flag. The value associated with a flag has to be either the
  297. symbol 'true' or 'false'. The value associated with other keys has to conform
  298. to the Cabal file format definition. The default value associated with the
  299. keys \"os\", \"arch\" and \"impl\" is \"linux\", \"x86_64\" and \"ghc\"
  300. respectively."
  301. (let ((cabal-meta cabal-hash
  302. (if port
  303. (read-cabal-and-hash port)
  304. (hackage-fetch-and-hash package-name))))
  305. (if cabal-meta
  306. (hackage-module->sexp (eval-cabal cabal-meta cabal-environment)
  307. cabal-hash
  308. #:include-test-dependencies?
  309. include-test-dependencies?)
  310. (values #f '()))))
  311. (define hackage->guix-package/m ;memoized variant
  312. (memoize hackage->guix-package))
  313. (define* (hackage-recursive-import package-name . args)
  314. (recursive-import package-name
  315. #:repo->guix-package (lambda* (name #:key repo version)
  316. (apply hackage->guix-package/m
  317. (cons name args)))
  318. #:guix-name hackage-name->package-name))
  319. (define hackage-package?
  320. (let ((hackage-rx (make-regexp "(https?://hackage.haskell.org|mirror://hackage/)")))
  321. (url-predicate (cut regexp-exec hackage-rx <>))))
  322. (define (latest-release package)
  323. "Return an <upstream-source> for the latest release of PACKAGE."
  324. (let* ((hackage-name (guix-package->hackage-name package))
  325. (cabal-meta (hackage-fetch hackage-name)))
  326. (match cabal-meta
  327. (#f
  328. (format (current-error-port)
  329. "warning: failed to parse ~a~%"
  330. (hackage-cabal-url hackage-name))
  331. #f)
  332. ((_ *** ("version" (version)))
  333. (let ((url (hackage-uri hackage-name version)))
  334. (upstream-source
  335. (package (package-name package))
  336. (version version)
  337. (urls (list url))))))))
  338. (define %hackage-updater
  339. (upstream-updater
  340. (name 'hackage)
  341. (description "Updater for Hackage packages")
  342. (pred hackage-package?)
  343. (latest latest-release)))
  344. ;;; cabal.scm ends here