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. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix import hackage)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 regex)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-1)
  29. #:use-module ((guix download) #:select (download-to-store url-fetch))
  30. #:use-module ((guix utils) #:select (package-name->name+version
  31. canonical-newline-port))
  32. #:use-module (guix http-client)
  33. #:use-module ((guix import utils) #:select (factorize-uri recursive-import))
  34. #:use-module (guix import cabal)
  35. #:use-module (guix store)
  36. #:use-module (gcrypt hash)
  37. #:use-module (guix base32)
  38. #:use-module (guix memoization)
  39. #:use-module (guix upstream)
  40. #:use-module (guix packages)
  41. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  42. #:export (%hackage-url
  43. hackage->guix-package
  44. hackage-recursive-import
  45. %hackage-updater
  46. guix-package->hackage-name
  47. hackage-name->package-name
  48. hackage-fetch
  49. hackage-source-url
  50. hackage-cabal-url
  51. hackage-package?))
  52. (define ghc-standard-libraries
  53. ;; List of libraries distributed with ghc (8.6.5).
  54. ;; Contents of ...-ghc-8.6.5/lib/ghc-8.6.5.
  55. '("ghc"
  56. "cabal" ;; in the output of `ghc-pkg list` Cabal is uppercased, but
  57. ;; hackage-name->package-name takes this into account.
  58. "win32" ;; similarly uppercased
  59. "array"
  60. "base"
  61. "binary"
  62. "bytestring"
  63. "containers"
  64. "deepseq"
  65. "directory"
  66. "filepath"
  67. "ghc"
  68. "ghc-boot"
  69. "ghc-boot-th"
  70. "ghc-compact"
  71. "ghc-heap"
  72. "ghc-prim"
  73. "ghci"
  74. "haskeline"
  75. "hpc"
  76. "integer-gmp"
  77. "libiserv"
  78. "mtl"
  79. "parsec"
  80. "pretty"
  81. "process"
  82. "stm"
  83. "template-haskell"
  84. "terminfo"
  85. "text"
  86. "time"
  87. "transformers"
  88. "unix"
  89. "xhtml"))
  90. (define package-name-prefix "ghc-")
  91. (define %hackage-url
  92. (make-parameter "https://hackage.haskell.org"))
  93. (define (hackage-source-url name version)
  94. "Given a Hackage package NAME and VERSION, return a url to the source
  95. tarball."
  96. (string-append (%hackage-url) "/package/"
  97. name "/" name "-" version ".tar.gz"))
  98. (define* (hackage-cabal-url name #:optional version)
  99. "Given a Hackage package NAME and VERSION, return a url to the corresponding
  100. .cabal file on Hackage. If VERSION is #f or missing, the url for the latest
  101. version is returned."
  102. (if version
  103. (string-append (%hackage-url) "/package/"
  104. name "-" version "/" name ".cabal")
  105. (string-append (%hackage-url) "/package/"
  106. name "/" name ".cabal")))
  107. (define (hackage-name->package-name name)
  108. "Given the NAME of a Cabal package, return the corresponding Guix name."
  109. (if (string-prefix? package-name-prefix name)
  110. (string-downcase name)
  111. (string-append package-name-prefix (string-downcase name))))
  112. (define guix-package->hackage-name
  113. (let ((uri-rx (make-regexp "https?://hackage.haskell.org/package/([^/]+)/.*"))
  114. (name-rx (make-regexp "(.*)-[0-9\\.]+")))
  115. (lambda (package)
  116. "Given a Guix package name, return the corresponding Hackage name."
  117. (let* ((source-url (and=> (package-source package) origin-uri))
  118. (name (match:substring (regexp-exec uri-rx source-url) 1)))
  119. (match (regexp-exec name-rx name)
  120. (#f name)
  121. (m (match:substring m 1)))))))
  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-values (((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*-values (((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-values (((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" 'gpl2)
  154. ("GPL-3" 'gpl3)
  155. ("GPL" "'gpl??")
  156. ("AGPL-3" 'agpl3)
  157. ("AGPL" "'agpl??")
  158. ("LGPL-2.1" 'lgpl2.1)
  159. ("LGPL-3" 'lgpl3)
  160. ("LGPL" "'lgpl??")
  161. ("BSD2" 'bsd-2)
  162. ("BSD3" 'bsd-3)
  163. ("BSD-3-Clause" 'bsd-3)
  164. ("MIT" 'expat)
  165. ("ISC" 'isc)
  166. ("MPL" 'mpl2.0)
  167. ("Apache-2.0" 'asl2.0)
  168. ("PublicDomain" '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-name)
  203. "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
  204. list with the names of dependencies. OWN-NAME is the name of the Cabal
  205. package being processed and is used to filter references to itself."
  206. (filter (lambda (d) (not (member (string-downcase d)
  207. (cons own-name ghc-standard-libraries))))
  208. dependencies))
  209. (define* (hackage-module->sexp cabal cabal-hash
  210. #:key (include-test-dependencies? #t))
  211. "Return the `package' S-expression for a Cabal package. CABAL is the
  212. representation of a Cabal file as produced by 'read-cabal'. CABAL-HASH is
  213. the hash of the Cabal file."
  214. (define name
  215. (cabal-package-name cabal))
  216. (define version
  217. (cabal-package-version cabal))
  218. (define revision
  219. (cabal-package-revision cabal))
  220. (define source-url
  221. (hackage-source-url name version))
  222. (define hackage-dependencies
  223. ((compose (cut filter-dependencies <>
  224. (cabal-package-name cabal))
  225. (cut cabal-dependencies->names <>))
  226. cabal))
  227. (define hackage-native-dependencies
  228. (lset-difference
  229. equal?
  230. ((compose (cut filter-dependencies <>
  231. (cabal-package-name cabal))
  232. ;; FIXME: Check include-test-dependencies?
  233. (lambda (cabal)
  234. (append (if include-test-dependencies?
  235. (cabal-test-dependencies->names cabal)
  236. '())
  237. (cabal-custom-setup-dependencies->names cabal))))
  238. cabal)
  239. hackage-dependencies))
  240. (define dependencies
  241. (map (lambda (name)
  242. (list name (list 'unquote (string->symbol name))))
  243. (map hackage-name->package-name
  244. hackage-dependencies)))
  245. (define native-dependencies
  246. (map (lambda (name)
  247. (list name (list 'unquote (string->symbol name))))
  248. (map hackage-name->package-name
  249. hackage-native-dependencies)))
  250. (define (maybe-inputs input-type inputs)
  251. (match inputs
  252. (()
  253. '())
  254. ((inputs ...)
  255. (list (list input-type
  256. (list 'quasiquote inputs))))))
  257. (define (maybe-arguments)
  258. (match (append (if (not include-test-dependencies?)
  259. '(#:tests? #f)
  260. '())
  261. (if (not (string-null? revision))
  262. `(#:cabal-revision (,revision ,cabal-hash))
  263. '()))
  264. (() '())
  265. (args `((arguments (,'quasiquote ,args))))))
  266. (let ((tarball (with-store store
  267. (download-to-store store source-url))))
  268. (values
  269. `(package
  270. (name ,(hackage-name->package-name name))
  271. (version ,version)
  272. (source (origin
  273. (method url-fetch)
  274. (uri (string-append ,@(factorize-uri source-url version)))
  275. (sha256
  276. (base32
  277. ,(if tarball
  278. (bytevector->nix-base32-string (file-sha256 tarball))
  279. "failed to download tar archive")))))
  280. (build-system haskell-build-system)
  281. ,@(maybe-inputs 'inputs dependencies)
  282. ,@(maybe-inputs 'native-inputs native-dependencies)
  283. ,@(maybe-arguments)
  284. (home-page ,(cabal-package-home-page cabal))
  285. (synopsis ,(cabal-package-synopsis cabal))
  286. (description ,(cabal-package-description cabal))
  287. (license ,(string->license (cabal-package-license cabal))))
  288. (append hackage-dependencies hackage-native-dependencies))))
  289. (define* (hackage->guix-package package-name #:key
  290. (include-test-dependencies? #t)
  291. (port #f)
  292. (cabal-environment '()))
  293. "Fetch the Cabal file for PACKAGE-NAME from hackage.haskell.org, or, if the
  294. called with keyword parameter PORT, from PORT. Return the `package'
  295. S-expression corresponding to that package, or #f on failure.
  296. CABAL-ENVIRONMENT is an alist defining the environment in which the Cabal
  297. conditionals are evaluated. The accepted keys are: \"os\", \"arch\", \"impl\"
  298. and the name of a flag. The value associated with a flag has to be either the
  299. symbol 'true' or 'false'. The value associated with other keys has to conform
  300. to the Cabal file format definition. The default value associated with the
  301. keys \"os\", \"arch\" and \"impl\" is \"linux\", \"x86_64\" and \"ghc\"
  302. respectively."
  303. (let-values (((cabal-meta cabal-hash)
  304. (if port
  305. (read-cabal-and-hash port)
  306. (hackage-fetch-and-hash package-name))))
  307. (and=> cabal-meta (compose (cut hackage-module->sexp <> cabal-hash
  308. #:include-test-dependencies?
  309. include-test-dependencies?)
  310. (cut eval-cabal <> cabal-environment)))))
  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 #f
  315. #:repo->guix-package (lambda (name repo)
  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")))
  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-source-url 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