hackage.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. ;;;
  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 import hackage)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 regex)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-11)
  30. #:use-module (srfi srfi-1)
  31. #:use-module ((guix download) #:select (download-to-store url-fetch))
  32. #:use-module ((guix utils) #:select (package-name->name+version
  33. canonical-newline-port))
  34. #:use-module (guix http-client)
  35. #:use-module (guix import utils)
  36. #:use-module (guix import cabal)
  37. #:use-module (guix store)
  38. #:use-module (gcrypt hash)
  39. #:use-module (guix base32)
  40. #:use-module (guix memoization)
  41. #:use-module (guix upstream)
  42. #:use-module (guix packages)
  43. #:autoload (guix build-system haskell) (hackage-uri)
  44. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  45. #:export (%hackage-url
  46. hackage->guix-package
  47. hackage-recursive-import
  48. %hackage-updater
  49. guix-package->hackage-name
  50. hackage-name->package-name
  51. hackage-fetch
  52. hackage-source-url
  53. hackage-cabal-url
  54. hackage-package?))
  55. (define ghc-standard-libraries
  56. ;; List of libraries distributed with ghc (as of 8.10.7).
  57. ;; Contents of …-ghc-8.10.7/lib/ghc-8.10.7
  58. '("ghc"
  59. "cabal" ;; in the output of `ghc-pkg list` Cabal is uppercased, but
  60. ;; hackage-name->package-name takes this into account.
  61. "win32" ;; similarly uppercased
  62. "array"
  63. "base"
  64. "binary"
  65. "bytestring"
  66. "containers"
  67. "deepseq"
  68. "directory"
  69. "exceptions"
  70. "filepath"
  71. "ghc"
  72. "ghc-boot"
  73. "ghc-boot-th"
  74. "ghc-compact"
  75. "ghc-heap"
  76. "ghc-prim"
  77. "ghci"
  78. "haskeline"
  79. "hpc"
  80. "integer-gmp"
  81. "libiserv"
  82. "mtl"
  83. "parsec"
  84. "pretty"
  85. "process"
  86. "stm"
  87. "template-haskell"
  88. "terminfo"
  89. "text"
  90. "time"
  91. "transformers"
  92. "unix"
  93. "xhtml"))
  94. (define package-name-prefix "ghc-")
  95. (define %hackage-url
  96. (make-parameter "https://hackage.haskell.org"))
  97. (define (hackage-source-url name version)
  98. "Given a Hackage package NAME and VERSION, return a url to the source
  99. tarball."
  100. (string-append (%hackage-url) "/package/"
  101. name "/" name "-" version ".tar.gz"))
  102. (define* (hackage-cabal-url name #:optional version)
  103. "Given a Hackage package NAME and VERSION, return a url to the corresponding
  104. .cabal file on Hackage. If VERSION is #f or missing, the url for the latest
  105. version is returned."
  106. (if version
  107. (string-append (%hackage-url) "/package/"
  108. name "-" version "/" name ".cabal")
  109. (string-append (%hackage-url) "/package/"
  110. name "/" name ".cabal")))
  111. (define (hackage-name->package-name name)
  112. "Given the NAME of a Cabal package, return the corresponding Guix name."
  113. (if (string-prefix? package-name-prefix name)
  114. (string-downcase name)
  115. (string-append package-name-prefix (string-downcase name))))
  116. (define guix-package->hackage-name
  117. (let ((uri-rx (make-regexp "(https?://hackage.haskell.org|mirror://hackage)/package/([^/]+)/.*"))
  118. (name-rx (make-regexp "(.*)-[0-9\\.]+")))
  119. (lambda (package)
  120. "Given a Guix package name, return the corresponding Hackage name."
  121. (let* ((source-url (and=> (package-source package) origin-uri))
  122. (name (match:substring (regexp-exec uri-rx source-url) 2)))
  123. (match (regexp-exec name-rx name)
  124. (#f name)
  125. (m (match:substring m 1)))))))
  126. (define (read-cabal-and-hash port)
  127. "Read a Cabal file from PORT and return it and its hash in nix-base32
  128. format as two values."
  129. (let-values (((port get-hash) (open-sha256-input-port port)))
  130. (values (read-cabal (canonical-newline-port port))
  131. (bytevector->nix-base32-string (get-hash)))))
  132. (define (hackage-fetch-and-hash name-version)
  133. "Fetch the latest Cabal revision for the package NAME-VERSION, and return
  134. two values: the parsed Cabal file and its hash in nix-base32 format. If the
  135. version part is omitted from the package name, then fetch the latest
  136. version. On failure, both return values will be #f."
  137. (guard (c ((and (http-get-error? c)
  138. (= 404 (http-get-error-code c)))
  139. (values #f #f))) ;"expected" if package is unknown
  140. (let*-values (((name version) (package-name->name+version name-version))
  141. ((url) (hackage-cabal-url name version))
  142. ((port _) (http-fetch url))
  143. ((cabal hash) (read-cabal-and-hash port)))
  144. (close-port port)
  145. (values cabal hash))))
  146. (define (hackage-fetch name-version)
  147. "Return the Cabal file for the package NAME-VERSION, or #f on failure. If
  148. the version part is omitted from the package name, then return the latest
  149. version."
  150. (let-values (((cabal hash) (hackage-fetch-and-hash name-version)))
  151. cabal))
  152. (define string->license
  153. ;; List of valid values from
  154. ;; https://www.haskell.org
  155. ;; /cabal/release/cabal-latest/doc/API/Cabal/Distribution-License.html.
  156. (match-lambda
  157. ("GPL-2" 'license:gpl2)
  158. ("GPL-3" 'license:gpl3)
  159. ("GPL" "'gpl??")
  160. ("AGPL-3" 'license:agpl3)
  161. ("AGPL" "'agpl??")
  162. ("LGPL-2.1" 'license:lgpl2.1)
  163. ("LGPL-3" 'license:lgpl3)
  164. ("LGPL" "'lgpl??")
  165. ("BSD2" 'license:bsd-2)
  166. ("BSD3" 'license:bsd-3)
  167. ("BSD-3-Clause" 'license:bsd-3)
  168. ("MIT" 'license:expat)
  169. ("ISC" 'license:isc)
  170. ("MPL" 'license:mpl2.0)
  171. ("Apache-2.0" 'license:asl2.0)
  172. ("PublicDomain" 'license:public-domain)
  173. ((x) (string->license x))
  174. ((lst ...) `(list ,@(map string->license lst)))
  175. (_ #f)))
  176. (define (cabal-dependencies->names cabal)
  177. "Return the list of dependencies names from the CABAL package object,
  178. not including test suite dependencies or custom-setup dependencies."
  179. (let* ((lib (cabal-package-library cabal))
  180. (lib-deps (if (pair? lib)
  181. (map cabal-dependency-name
  182. (append-map cabal-library-dependencies lib))
  183. '()))
  184. (exe (cabal-package-executables cabal))
  185. (exe-deps (if (pair? exe)
  186. (map cabal-dependency-name
  187. (append-map cabal-executable-dependencies exe))
  188. '())))
  189. (delete-duplicates (append lib-deps exe-deps))))
  190. (define (cabal-test-dependencies->names cabal)
  191. "Return the list of test suite dependencies from the CABAL package
  192. object."
  193. (let* ((ts (cabal-package-test-suites cabal))
  194. (ts-deps (if (pair? ts)
  195. (map cabal-dependency-name
  196. (append-map cabal-test-suite-dependencies ts))
  197. '())))
  198. ts-deps))
  199. (define (cabal-custom-setup-dependencies->names cabal)
  200. "Return the list of custom-setup dependencies from the CABAL package
  201. object."
  202. (let* ((custom-setup-dependencies (or (and=> (cabal-package-custom-setup cabal)
  203. cabal-custom-setup-dependencies)
  204. '())))
  205. (map cabal-dependency-name custom-setup-dependencies)))
  206. (define (filter-dependencies dependencies own-name)
  207. "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
  208. list with the names of dependencies. OWN-NAME is the name of the Cabal
  209. package being processed and is used to filter references to itself."
  210. (filter (lambda (d) (not (member (string-downcase d)
  211. (cons own-name ghc-standard-libraries))))
  212. dependencies))
  213. (define* (hackage-module->sexp cabal cabal-hash
  214. #:key (include-test-dependencies? #t))
  215. "Return the `package' S-expression for a Cabal package. CABAL is the
  216. representation of a Cabal file as produced by 'read-cabal'. CABAL-HASH is
  217. the hash of the Cabal file."
  218. (define name
  219. (cabal-package-name cabal))
  220. (define version
  221. (cabal-package-version cabal))
  222. (define revision
  223. (cabal-package-revision cabal))
  224. (define source-url
  225. (hackage-source-url name version))
  226. (define hackage-dependencies
  227. ((compose (cut filter-dependencies <>
  228. (cabal-package-name cabal))
  229. (cut cabal-dependencies->names <>))
  230. cabal))
  231. (define hackage-native-dependencies
  232. (lset-difference
  233. equal?
  234. ((compose (cut filter-dependencies <>
  235. (cabal-package-name cabal))
  236. ;; FIXME: Check include-test-dependencies?
  237. (lambda (cabal)
  238. (append (if include-test-dependencies?
  239. (cabal-test-dependencies->names cabal)
  240. '())
  241. (cabal-custom-setup-dependencies->names cabal))))
  242. cabal)
  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-values (((cabal-meta cabal-hash)
  306. (if port
  307. (read-cabal-and-hash port)
  308. (hackage-fetch-and-hash package-name))))
  309. (and=> cabal-meta (compose (cut hackage-module->sexp <> cabal-hash
  310. #:include-test-dependencies?
  311. include-test-dependencies?)
  312. (cut eval-cabal <> cabal-environment)))))
  313. (define hackage->guix-package/m ;memoized variant
  314. (memoize hackage->guix-package))
  315. (define* (hackage-recursive-import package-name . args)
  316. (recursive-import package-name
  317. #:repo->guix-package (lambda* (name #:key repo version)
  318. (apply hackage->guix-package/m
  319. (cons name args)))
  320. #:guix-name hackage-name->package-name))
  321. (define hackage-package?
  322. (let ((hackage-rx (make-regexp "(https?://hackage.haskell.org|mirror://hackage/)")))
  323. (url-predicate (cut regexp-exec hackage-rx <>))))
  324. (define (latest-release package)
  325. "Return an <upstream-source> for the latest release of PACKAGE."
  326. (let* ((hackage-name (guix-package->hackage-name package))
  327. (cabal-meta (hackage-fetch hackage-name)))
  328. (match cabal-meta
  329. (#f
  330. (format (current-error-port)
  331. "warning: failed to parse ~a~%"
  332. (hackage-cabal-url hackage-name))
  333. #f)
  334. ((_ *** ("version" (version)))
  335. (let ((url (hackage-uri hackage-name version)))
  336. (upstream-source
  337. (package (package-name package))
  338. (version version)
  339. (urls (list url))))))))
  340. (define %hackage-updater
  341. (upstream-updater
  342. (name 'hackage)
  343. (description "Updater for Hackage packages")
  344. (pred hackage-package?)
  345. (latest latest-release)))
  346. ;;; cabal.scm ends here