elpa.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2015, 2016, 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix import elpa)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 rdelim)
  23. #:use-module (web uri)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (srfi srfi-9 gnu)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-26)
  29. #:use-module ((guix download) #:select (download-to-store))
  30. #:use-module (guix import utils)
  31. #:use-module (guix http-client)
  32. #:use-module (guix store)
  33. #:use-module (guix ui)
  34. #:use-module (gcrypt hash)
  35. #:use-module (guix base32)
  36. #:use-module (guix upstream)
  37. #:use-module (guix packages)
  38. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  39. #:export (elpa->guix-package
  40. %elpa-updater
  41. elpa-recursive-import))
  42. (define (elpa-dependencies->names deps)
  43. "Convert DEPS, a list of symbol/version pairs à la ELPA, to a list of
  44. package names as strings"
  45. (match deps
  46. (((names _ ...) ...)
  47. (map symbol->string names))))
  48. (define emacs-standard-library?
  49. (let ((libs '("emacs" "cl-lib")))
  50. (lambda (lib)
  51. "Return true if LIB is part of Emacs itself. The check is not
  52. exhaustive and only attempts to recognize a subset of packages which in the
  53. past were distributed separately from Emacs."
  54. (member lib libs))))
  55. (define (filter-dependencies names)
  56. "Remove the package names included with Emacs from the list of
  57. NAMES (strings)."
  58. (remove emacs-standard-library? names))
  59. (define (elpa-name->package-name name)
  60. "Given the NAME of an Emacs package, return the corresponding Guix name."
  61. (let ((package-name-prefix "emacs-"))
  62. (if (string-prefix? package-name-prefix name)
  63. (string-downcase name)
  64. (string-append package-name-prefix (string-downcase name)))))
  65. (define* (elpa-url #:optional (repo 'gnu))
  66. "Retrieve the URL of REPO."
  67. (let ((elpa-archives
  68. '((gnu . "https://elpa.gnu.org/packages")
  69. (gnu/http . "http://elpa.gnu.org/packages") ;for testing
  70. (melpa-stable . "https://stable.melpa.org/packages")
  71. (melpa . "https://melpa.org/packages"))))
  72. (assq-ref elpa-archives repo)))
  73. (define* (elpa-fetch-archive #:optional (repo 'gnu))
  74. "Retrieve the archive with the list of packages available from REPO."
  75. (let ((url (and=> (elpa-url repo)
  76. (cut string-append <> "/archive-contents"))))
  77. (if url
  78. ;; Use a relatively small TTL for the archive itself.
  79. (let* ((port (http-fetch/cached (string->uri url)
  80. #:ttl (* 6 3600)))
  81. (data (read port)))
  82. (close-port port)
  83. data)
  84. (leave (G_ "~A: currently not supported~%") repo))))
  85. (define* (call-with-downloaded-file url proc #:optional (error-thunk #f))
  86. "Fetch URL, store the content in a temporary file and call PROC with that
  87. file. Returns the value returned by PROC. On error call ERROR-THUNK and
  88. return its value or leave if it's false."
  89. (catch #t
  90. (lambda ()
  91. (proc (http-fetch/cached (string->uri url))))
  92. (lambda (key . args)
  93. (if error-thunk
  94. (error-thunk)
  95. (leave (G_ "~A: download failed~%") url)))))
  96. (define (is-elpa-package? name elpa-pkg-spec)
  97. "Return true if the string NAME corresponds to the name of the package
  98. defined by ELPA-PKG-SPEC, a package specification as in an archive
  99. 'archive-contents' file."
  100. (eq? (first elpa-pkg-spec) (string->symbol name)))
  101. (define* (elpa-package-info name #:optional (repo 'gnu))
  102. "Extract the information about the package NAME from the package archieve of
  103. REPO."
  104. (let* ((archive (elpa-fetch-archive repo))
  105. (pkgs (match archive ((version pkg-spec ...) pkg-spec)))
  106. (info (filter (cut is-elpa-package? name <>) pkgs)))
  107. (if (pair? info) (first info) #f)))
  108. ;; Object to store information about an ELPA package.
  109. (define-record-type <elpa-package>
  110. (make-elpa-package name version inputs synopsis kind home-page description
  111. source-url)
  112. elpa-package?
  113. (name elpa-package-name)
  114. (version elpa-package-version)
  115. (inputs elpa-package-inputs)
  116. (synopsis elpa-package-synopsis)
  117. (kind elpa-package-kind)
  118. (home-page elpa-package-home-page)
  119. (description elpa-package-description)
  120. (source-url elpa-package-source-url))
  121. (set-record-type-printer! <elpa-package>
  122. (lambda (package port)
  123. (format port "#<elpa-package ~a@~a>"
  124. (elpa-package-name package)
  125. (elpa-package-version package))))
  126. (define (elpa-version->string elpa-version)
  127. "Convert the package version as used in Emacs package files into a string."
  128. (if (pair? elpa-version)
  129. (let-values (((ms rest) (match elpa-version
  130. ((ms . rest)
  131. (values ms rest)))))
  132. (fold (lambda (n s) (string-append s "." (number->string n)))
  133. (number->string ms) rest))
  134. #f))
  135. (define (package-home-page alist)
  136. "Extract the package home-page from ALIST."
  137. (or (assq-ref alist ':url) "unspecified"))
  138. (define (ensure-list alist)
  139. "If ALIST is the symbol 'nil return the empty list. Otherwise, return ALIST."
  140. (if (eq? alist 'nil)
  141. '()
  142. alist))
  143. (define (package-source-url kind name version repo)
  144. "Return the source URL of the package described the the strings NAME and
  145. VERSION at REPO. KIND is either the symbol 'single or 'tar."
  146. (case kind
  147. ((single) (full-url repo name ".el" version))
  148. ((tar) (full-url repo name ".tar" version))
  149. (else
  150. #f)))
  151. (define* (full-url repo name suffix #:optional (version #f))
  152. "Return the full URL of the package NAME at REPO and the SUFFIX. Maybe
  153. include VERSION."
  154. (if version
  155. (string-append (elpa-url repo) "/" name "-" version suffix)
  156. (string-append (elpa-url repo) "/" name suffix)))
  157. (define (fetch-package-description kind name repo)
  158. "Fetch the description of package NAME of type KIND from REPO."
  159. (let ((url (full-url repo name "-readme.txt"))
  160. (error-thunk (lambda () "No description available.")))
  161. (call-with-downloaded-file url read-string error-thunk)))
  162. (define* (fetch-elpa-package name #:optional (repo 'gnu))
  163. "Fetch package NAME from REPO."
  164. (let ((pkg (elpa-package-info name repo)))
  165. (match pkg
  166. ((name version reqs synopsis kind . rest)
  167. (let* ((name (symbol->string name))
  168. (ver (elpa-version->string version))
  169. (url (package-source-url kind name ver repo)))
  170. (make-elpa-package name ver
  171. (ensure-list reqs) synopsis kind
  172. (package-home-page (match rest
  173. (() #f)
  174. ((one) one)))
  175. (fetch-package-description kind name repo)
  176. url)))
  177. (_ #f))))
  178. (define* (elpa-package->sexp pkg #:optional license)
  179. "Return the `package' S-expression for the Emacs package PKG, a record of
  180. type '<elpa-package>'."
  181. (define name (elpa-package-name pkg))
  182. (define version (elpa-package-version pkg))
  183. (define source-url (elpa-package-source-url pkg))
  184. (define dependencies-names
  185. (filter-dependencies (elpa-dependencies->names
  186. (elpa-package-inputs pkg))))
  187. (define dependencies
  188. (map (lambda (n)
  189. (let ((new-n (elpa-name->package-name n)))
  190. (list new-n (list 'unquote (string->symbol new-n)))))
  191. dependencies-names))
  192. (define (maybe-inputs input-type inputs)
  193. (match inputs
  194. (()
  195. '())
  196. ((inputs ...)
  197. (list (list input-type
  198. (list 'quasiquote inputs))))))
  199. (let ((tarball (with-store store
  200. (download-to-store store source-url))))
  201. (values
  202. `(package
  203. (name ,(elpa-name->package-name name))
  204. (version ,version)
  205. (source (origin
  206. (method url-fetch)
  207. (uri (string-append ,@(factorize-uri source-url version)))
  208. (sha256
  209. (base32
  210. ,(if tarball
  211. (bytevector->nix-base32-string (file-sha256 tarball))
  212. "failed to download package")))))
  213. (build-system emacs-build-system)
  214. ,@(maybe-inputs 'propagated-inputs dependencies)
  215. (home-page ,(elpa-package-home-page pkg))
  216. (synopsis ,(elpa-package-synopsis pkg))
  217. (description ,(elpa-package-description pkg))
  218. (license ,license))
  219. dependencies-names)))
  220. (define* (elpa->guix-package name #:optional (repo 'gnu))
  221. "Fetch the package NAME from REPO and produce a Guix package S-expression."
  222. (match (fetch-elpa-package name repo)
  223. (#f #f)
  224. (package
  225. ;; ELPA is known to contain only GPLv3+ code. Other repos may contain
  226. ;; code under other license but there's no license metadata.
  227. (let ((license (and (memq repo '(gnu gnu/http)) 'license:gpl3+)))
  228. (elpa-package->sexp package license)))))
  229. ;;;
  230. ;;; Updates.
  231. ;;;
  232. (define (latest-release package)
  233. "Return an <upstream-release> for the latest release of PACKAGE."
  234. (define name
  235. (if (string-prefix? "emacs-" (package-name package))
  236. (string-drop (package-name package) 6)
  237. (package-name package)))
  238. (let* ((repo 'gnu)
  239. (info (elpa-package-info name repo))
  240. (version (match info
  241. ((name raw-version . _)
  242. (elpa-version->string raw-version))))
  243. (url (match info
  244. ((_ raw-version reqs synopsis kind . rest)
  245. (package-source-url kind name version repo)))))
  246. (upstream-source
  247. (package (package-name package))
  248. (version version)
  249. (urls (list url))
  250. (signature-urls (list (string-append url ".sig"))))))
  251. (define package-from-gnu.org?
  252. (url-predicate (lambda (url)
  253. (let ((uri (string->uri url)))
  254. (and uri
  255. (string=? (uri-host uri) "elpa.gnu.org"))))))
  256. (define %elpa-updater
  257. ;; The ELPA updater. We restrict it to packages hosted on elpa.gnu.org
  258. ;; because for other repositories, we typically grab the source elsewhere.
  259. (upstream-updater
  260. (name 'elpa)
  261. (description "Updater for ELPA packages")
  262. (pred package-from-gnu.org?)
  263. (latest latest-release)))
  264. (define elpa-guix-name (cut guix-name "emacs-" <>))
  265. (define* (elpa-recursive-import package-name #:optional (repo 'gnu))
  266. (recursive-import package-name repo
  267. #:repo->guix-package elpa->guix-package
  268. #:guix-name elpa-guix-name))
  269. ;;; elpa.scm ends here