hackage.scm 15 KB

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