stackage.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  5. ;;; Copyright © 2021 Xinglu Chem <public@yoctocell.xyz>
  6. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  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 stackage)
  23. #:use-module (ice-9 match)
  24. #:use-module (json)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-34)
  27. #:use-module (srfi srfi-35)
  28. #:use-module (guix import json)
  29. #:use-module (guix import hackage)
  30. #:use-module (guix import utils)
  31. #:use-module (guix memoization)
  32. #:use-module (guix packages)
  33. #:use-module (guix upstream)
  34. #:use-module (guix diagnostics)
  35. #:use-module (guix i18n)
  36. #:export (%default-lts-version
  37. %stackage-url
  38. stackage->guix-package
  39. stackage-recursive-import
  40. %stackage-updater))
  41. ;;;
  42. ;;; Stackage info fetcher and access functions
  43. ;;;
  44. (define %stackage-url
  45. (make-parameter "https://www.stackage.org"))
  46. ;; Latest LTS version compatible with current GHC.
  47. (define %default-lts-version "18.14")
  48. (define-json-mapping <stackage-lts> make-stackage-lts
  49. stackage-lts?
  50. json->stackage-lts
  51. (snapshot stackage-lts-snapshot "snapshot" json->snapshot)
  52. (packages stackage-lts-packages "packages"
  53. (lambda (vector)
  54. (map json->stackage-package (vector->list vector)))))
  55. (define-json-mapping <snapshot> make-snapshot
  56. stackage-snapshot?
  57. json->snapshot
  58. (name snapshot-name)
  59. (ghc-version snapshot-ghc-version)
  60. (compiler snapshot-compiler))
  61. (define-json-mapping <stackage-package> make-stackage-package
  62. stackage-package?
  63. json->stackage-package
  64. (origin stackage-package-origin)
  65. (name stackage-package-name)
  66. (version stackage-package-version)
  67. (synopsis stackage-package-synopsis))
  68. (define stackage-lts-info-fetch
  69. ;; "Retrieve the information about the LTS Stackage release VERSION."
  70. (memoize
  71. (lambda* (#:optional (version ""))
  72. (let* ((url (string-append (%stackage-url)
  73. "/lts-" (if (string-null? version)
  74. %default-lts-version
  75. version)))
  76. (lts-info (and=> (json-fetch url) json->stackage-lts)))
  77. (or lts-info
  78. (raise (formatted-message (G_ "LTS release version not found: ~a")
  79. version)))))))
  80. (define (lts-package-version packages name)
  81. "Return the version of the package with upstream NAME included in PACKAGES."
  82. (let ((pkg (find (lambda (pkg) (string=? (stackage-package-name pkg) name))
  83. packages)))
  84. (stackage-package-version pkg)))
  85. ;;;
  86. ;;; Importer entry point
  87. ;;;
  88. (define (hackage-name-version name version)
  89. (and version (string-append name "@" version)))
  90. (define stackage->guix-package
  91. (memoize
  92. (lambda* (package-name ; upstream name
  93. #:key
  94. (include-test-dependencies? #t)
  95. (lts-version %default-lts-version)
  96. (packages
  97. (stackage-lts-packages
  98. (stackage-lts-info-fetch lts-version))))
  99. "Fetch Cabal file for PACKAGE-NAME from hackage.haskell.org. The retrieved
  100. version corresponds to the version of PACKAGE-NAME specified in the LTS-VERSION
  101. release at stackage.org. Return the `package' S-expression corresponding to
  102. that package, or #f on failure. PACKAGES-INFO is the alist with the packages
  103. included in the Stackage LTS release."
  104. (let* ((version (lts-package-version packages package-name))
  105. (name-version (hackage-name-version package-name version)))
  106. (if name-version
  107. (hackage->guix-package name-version
  108. #:include-test-dependencies?
  109. include-test-dependencies?)
  110. (raise (formatted-message (G_ "~a: Stackage package not found")
  111. package-name)))))))
  112. (define (stackage-recursive-import package-name . args)
  113. (recursive-import package-name
  114. #:repo->guix-package (lambda* (name #:key repo version)
  115. (apply stackage->guix-package (cons name args)))
  116. #:guix-name hackage-name->package-name))
  117. ;;;
  118. ;;; Updater
  119. ;;;
  120. (define latest-lts-release
  121. (let ((packages
  122. (mlambda ()
  123. (stackage-lts-packages
  124. (stackage-lts-info-fetch %default-lts-version)))))
  125. (lambda* (pkg)
  126. "Return an <upstream-source> for the latest Stackage LTS release of
  127. PACKAGE or #f if the package is not included in the Stackage LTS release."
  128. (let* ((hackage-name (guix-package->hackage-name pkg))
  129. (version (lts-package-version (packages) hackage-name))
  130. (name-version (hackage-name-version hackage-name version)))
  131. (match (and=> name-version hackage-fetch)
  132. (#f
  133. (warning (G_ "failed to parse ~a~%")
  134. (hackage-cabal-url hackage-name))
  135. #f)
  136. (_ (let ((url (hackage-source-url hackage-name version)))
  137. (upstream-source
  138. (package (package-name pkg))
  139. (version version)
  140. (urls (list url))
  141. (input-changes
  142. (changed-inputs
  143. pkg
  144. (stackage->guix-package hackage-name #:packages (packages))))))))))))
  145. (define (stackage-lts-package? package)
  146. "Return whether PACKAGE is available on the default Stackage LTS release."
  147. (and (hackage-package? package)
  148. (let ((packages (stackage-lts-packages
  149. (stackage-lts-info-fetch %default-lts-version)))
  150. (hackage-name (guix-package->hackage-name package)))
  151. (find (lambda (package)
  152. (string=? (stackage-package-name package) hackage-name))
  153. packages))))
  154. (define %stackage-updater
  155. (upstream-updater
  156. (name 'stackage)
  157. (description "Updater for Stackage LTS packages")
  158. (pred stackage-lts-package?)
  159. (latest latest-lts-release)))
  160. ;;; stackage.scm ends here