hackage.scm 14 KB

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