hackage.scm 14 KB

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