elpa.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2015, 2016, 2017, 2018, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
  5. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  6. ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
  7. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  8. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  9. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  10. ;;;
  11. ;;; This file is part of GNU Guix.
  12. ;;;
  13. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  14. ;;; under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 3 of the License, or (at
  16. ;;; your option) any later version.
  17. ;;;
  18. ;;; GNU Guix is distributed in the hope that it will be useful, but
  19. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  25. (define-module (guix import elpa)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 rdelim)
  28. #:use-module (ice-9 regex)
  29. #:use-module (web uri)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-9)
  32. #:use-module (srfi srfi-9 gnu)
  33. #:use-module (srfi srfi-11)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:use-module ((guix download) #:select (download-to-store))
  38. #:use-module (guix import utils)
  39. #:use-module (guix http-client)
  40. #:use-module (guix git)
  41. #:use-module (guix hash)
  42. #:use-module ((guix serialization) #:select (write-file))
  43. #:use-module (guix store)
  44. #:use-module (guix ui)
  45. #:use-module (guix base32)
  46. #:use-module (guix upstream)
  47. #:use-module (guix packages)
  48. #:use-module (guix memoization)
  49. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  50. #:export (elpa->guix-package
  51. guix-package->elpa-name
  52. %elpa-updater
  53. elpa-recursive-import))
  54. (define (elpa-dependencies->names deps)
  55. "Convert DEPS, a list of symbol/version pairs à la ELPA, to a list of
  56. package names as strings"
  57. (match deps
  58. (((names _ ...) ...)
  59. (map symbol->string names))))
  60. (define emacs-standard-library?
  61. (let ((libs '("emacs" "cl-lib")))
  62. (lambda (lib)
  63. "Return true if LIB is part of Emacs itself. The check is not
  64. exhaustive and only attempts to recognize a subset of packages which in the
  65. past were distributed separately from Emacs."
  66. (member lib libs))))
  67. (define (filter-dependencies names)
  68. "Remove the package names included with Emacs from the list of
  69. NAMES (strings)."
  70. (remove emacs-standard-library? names))
  71. (define (elpa-name->package-name name)
  72. "Given the NAME of an Emacs package, return the corresponding Guix name."
  73. (let ((package-name-prefix "emacs-"))
  74. (if (string-prefix? package-name-prefix name)
  75. (string-downcase name)
  76. (string-append package-name-prefix (string-downcase name)))))
  77. (define* (elpa-url #:optional (repo 'gnu))
  78. "Retrieve the URL of REPO."
  79. (let ((elpa-archives
  80. '((gnu . "https://elpa.gnu.org/packages")
  81. (gnu/http . "http://elpa.gnu.org/packages") ;for testing
  82. (nongnu . "https://elpa.nongnu.org/nongnu")
  83. (melpa-stable . "https://stable.melpa.org/packages")
  84. (melpa . "https://melpa.org/packages"))))
  85. (assq-ref elpa-archives repo)))
  86. (define* (elpa-fetch-archive #:optional (repo 'gnu))
  87. "Retrieve the archive with the list of packages available from REPO."
  88. (let ((url (and=> (elpa-url repo)
  89. (cut string-append <> "/archive-contents"))))
  90. (if url
  91. ;; Use a relatively small TTL for the archive itself.
  92. (let* ((port (http-fetch/cached (string->uri url)
  93. #:ttl (* 6 3600)))
  94. (data (read port)))
  95. (close-port port)
  96. data)
  97. (leave (G_ "~A: currently not supported~%") repo))))
  98. (define* (call-with-downloaded-file url proc #:optional (error-thunk #f))
  99. "Fetch URL, store the content in a temporary file and call PROC with that
  100. file. Returns the value returned by PROC. On error call ERROR-THUNK and
  101. return its value or leave if it's false."
  102. (catch #t
  103. (lambda ()
  104. (proc (http-fetch/cached (string->uri url))))
  105. (lambda (key . args)
  106. (if error-thunk
  107. (error-thunk)
  108. (leave (G_ "~A: download failed~%") url)))))
  109. (define (is-elpa-package? name elpa-pkg-spec)
  110. "Return true if the string NAME corresponds to the name of the package
  111. defined by ELPA-PKG-SPEC, a package specification as in an archive
  112. 'archive-contents' file."
  113. (eq? (first elpa-pkg-spec) (string->symbol name)))
  114. (define* (elpa-package-info name #:optional (repo 'gnu))
  115. "Extract the information about the package NAME from the package archieve of
  116. REPO."
  117. (let* ((archive (elpa-fetch-archive repo))
  118. (pkgs (match archive ((version pkg-spec ...) pkg-spec)))
  119. (info (filter (cut is-elpa-package? name <>) pkgs)))
  120. (if (pair? info) (first info) #f)))
  121. ;; Object to store information about an ELPA package.
  122. (define-record-type <elpa-package>
  123. (make-elpa-package name version inputs synopsis kind home-page description
  124. source-url)
  125. elpa-package?
  126. (name elpa-package-name)
  127. (version elpa-package-version)
  128. (inputs elpa-package-inputs)
  129. (synopsis elpa-package-synopsis)
  130. (kind elpa-package-kind)
  131. (home-page elpa-package-home-page)
  132. (description elpa-package-description)
  133. (source-url elpa-package-source-url))
  134. (set-record-type-printer! <elpa-package>
  135. (lambda (package port)
  136. (format port "#<elpa-package ~a@~a>"
  137. (elpa-package-name package)
  138. (elpa-package-version package))))
  139. (define (elpa-version->string elpa-version)
  140. "Convert the package version as used in Emacs package files into a string."
  141. (if (pair? elpa-version)
  142. (let-values (((ms rest) (match elpa-version
  143. ((ms . rest)
  144. (values ms rest)))))
  145. (fold (lambda (n s) (string-append s "." (number->string n)))
  146. (number->string ms) rest))
  147. #f))
  148. (define (package-home-page alist)
  149. "Extract the package home-page from ALIST."
  150. (or (assq-ref alist ':url) "unspecified"))
  151. (define (ensure-list alist)
  152. "If ALIST is the symbol 'nil return the empty list. Otherwise, return ALIST."
  153. (if (eq? alist 'nil)
  154. '()
  155. alist))
  156. (define (package-source-url kind name version repo)
  157. "Return the source URL of the package described the the strings NAME and
  158. VERSION at REPO. KIND is either the symbol 'single or 'tar."
  159. (case kind
  160. ((single) (full-url repo name ".el" version))
  161. ((tar) (full-url repo name ".tar" version))
  162. (else
  163. #f)))
  164. (define* (full-url repo name suffix #:optional (version #f))
  165. "Return the full URL of the package NAME at REPO and the SUFFIX. Maybe
  166. include VERSION."
  167. (if version
  168. (string-append (elpa-url repo) "/" name "-" version suffix)
  169. (string-append (elpa-url repo) "/" name suffix)))
  170. (define (fetch-package-description kind name repo)
  171. "Fetch the description of package NAME of type KIND from REPO."
  172. (let ((url (full-url repo name "-readme.txt"))
  173. (error-thunk (lambda () "No description available.")))
  174. (call-with-downloaded-file url read-string error-thunk)))
  175. (define* (fetch-elpa-package name #:optional (repo 'gnu))
  176. "Fetch package NAME from REPO."
  177. (let ((pkg (elpa-package-info name repo)))
  178. (match pkg
  179. ((name version reqs synopsis kind . rest)
  180. (let* ((name (symbol->string name))
  181. (ver (elpa-version->string version))
  182. (url (package-source-url kind name ver repo)))
  183. (make-elpa-package name ver
  184. (ensure-list reqs) synopsis kind
  185. (package-home-page (match rest
  186. (() #f)
  187. ((one) one)))
  188. (fetch-package-description kind name repo)
  189. url)))
  190. (_ #f))))
  191. (define* (download-git-repository url ref)
  192. "Fetch the given REF from the Git repository at URL."
  193. (with-store store
  194. (latest-repository-commit store url #:ref ref)))
  195. (define (package-name->melpa-recipe package-name)
  196. "Fetch the MELPA recipe for PACKAGE-NAME, represented as an alist from
  197. keywords to values."
  198. (define recipe-url
  199. (string-append "https://raw.githubusercontent.com/melpa/melpa/master/recipes/"
  200. package-name))
  201. (define (data->recipe data)
  202. (match data
  203. (() '())
  204. ((key value . tail)
  205. (cons (cons key value) (data->recipe tail)))))
  206. (let* ((port (http-fetch/cached (string->uri recipe-url)
  207. #:ttl (* 6 3600)))
  208. (data (read port)))
  209. (close-port port)
  210. (data->recipe (cons ':name data))))
  211. (define (git-repository->origin recipe url)
  212. "Fetch origin details from the Git repository at URL for the provided MELPA
  213. RECIPE."
  214. (define ref
  215. (cond
  216. ((assoc-ref recipe #:branch)
  217. => (lambda (branch) (cons 'branch branch)))
  218. ((assoc-ref recipe #:commit)
  219. => (lambda (commit) (cons 'commit commit)))
  220. (else
  221. '())))
  222. (let-values (((directory commit) (download-git-repository url ref)))
  223. `(origin
  224. (method git-fetch)
  225. (uri (git-reference
  226. (url ,url)
  227. (commit ,commit)))
  228. (sha256
  229. (base32
  230. ,(bytevector->nix-base32-string
  231. (file-hash* directory #:recursive? #true)))))))
  232. (define* (melpa-recipe->origin recipe)
  233. "Fetch origin details from the MELPA recipe and associated repository for
  234. the package named PACKAGE-NAME."
  235. (define (github-repo->url repo)
  236. (string-append "https://github.com/" repo ".git"))
  237. (define (gitlab-repo->url repo)
  238. (string-append "https://gitlab.com/" repo ".git"))
  239. (match (assq-ref recipe ':fetcher)
  240. ('github (git-repository->origin recipe (github-repo->url (assq-ref recipe ':repo))))
  241. ('gitlab (git-repository->origin recipe (gitlab-repo->url (assq-ref recipe ':repo))))
  242. ('git (git-repository->origin recipe (assq-ref recipe ':url)))
  243. (#f #f) ; if we're not using melpa then this stops us printing a warning
  244. (_ (warning (G_ "Unsupported MELPA fetcher: ~a, falling back to unstable MELPA source.~%")
  245. (assq-ref recipe ':fetcher))
  246. #f)))
  247. (define default-files-spec
  248. ;; This contains more than just the things contained in %default-include and
  249. ;; %default-exclude, presumably because this includes source files (*.in,
  250. ;; *.texi, etc.) which have already been processed for releases.
  251. ;;
  252. ;; Taken from:
  253. ;; https://github.com/melpa/melpa/blob/e8dc709d0ab2b4a68c59315f42858bcb86095f11/package-build/package-build.el#L580-L585
  254. '("*.el" "*.el.in" "dir"
  255. "*.info" "*.texi" "*.texinfo"
  256. "doc/dir" "doc/*.info" "doc/*.texi" "doc/*.texinfo"
  257. (:exclude ".dir-locals.el" "test.el" "tests.el" "*-test.el" "*-tests.el")))
  258. (define* (melpa-recipe->maybe-arguments melpa-recipe)
  259. "Extract arguments for the build system from MELPA-RECIPE."
  260. (define (glob->regexp glob)
  261. (string-append
  262. "^"
  263. (regexp-substitute/global #f "\\*\\*?" glob
  264. 'pre
  265. (lambda (m)
  266. (if (string= (match:substring m 0) "**")
  267. ".*"
  268. "[^/]+"))
  269. 'post)
  270. "$"))
  271. (let ((files (assq-ref melpa-recipe ':files)))
  272. (if files
  273. (let* ((with-default (apply append (map (lambda (entry)
  274. (if (eq? ':defaults entry)
  275. default-files-spec
  276. (list entry)))
  277. files)))
  278. (inclusions (remove pair? with-default))
  279. (exclusions (apply append (map (match-lambda
  280. ((':exclude . values)
  281. values)
  282. (_ '()))
  283. with-default))))
  284. `((arguments '(#:include ',(map glob->regexp inclusions)
  285. #:exclude ',(map glob->regexp exclusions)))))
  286. '())))
  287. (define* (elpa-package->sexp pkg #:optional license repo)
  288. "Return the `package' S-expression for the Emacs package PKG, a record of
  289. type '<elpa-package>'."
  290. (define melpa-recipe
  291. ;; XXX: Call 'identity' to work around a Guile 3.0.[5-7] compiler bug:
  292. ;; <https://bugs.gnu.org/48368>.
  293. (and (eq? (identity repo) 'melpa)
  294. (package-name->melpa-recipe (elpa-package-name pkg))))
  295. (define name (elpa-package-name pkg))
  296. (define version (elpa-package-version pkg))
  297. (define source-url (elpa-package-source-url pkg))
  298. (define dependencies-names
  299. (filter-dependencies (elpa-dependencies->names
  300. (elpa-package-inputs pkg))))
  301. (define dependencies
  302. (map (compose string->symbol elpa-name->package-name)
  303. dependencies-names))
  304. (define (maybe-inputs input-type inputs)
  305. (match inputs
  306. (()
  307. '())
  308. ((inputs ...)
  309. (list (list input-type `(list ,@inputs))))))
  310. (define melpa-source
  311. (melpa-recipe->origin melpa-recipe))
  312. (values
  313. `(package
  314. (name ,(elpa-name->package-name name))
  315. (version ,version)
  316. (source ,(or melpa-source
  317. (let ((tarball (with-store store
  318. (download-to-store store source-url))))
  319. `(origin
  320. (method url-fetch)
  321. (uri (string-append ,@(factorize-uri source-url version)))
  322. (sha256
  323. (base32
  324. ,(if tarball
  325. (bytevector->nix-base32-string
  326. (file-hash* tarball #:recursive? #false))
  327. "failed to download package")))))))
  328. (build-system emacs-build-system)
  329. ,@(maybe-inputs 'propagated-inputs dependencies)
  330. ,@(if melpa-source
  331. (melpa-recipe->maybe-arguments melpa-recipe)
  332. '())
  333. (home-page ,(elpa-package-home-page pkg))
  334. (synopsis ,(elpa-package-synopsis pkg))
  335. (description ,(beautify-description (elpa-package-description pkg)))
  336. (license ,license))
  337. dependencies-names))
  338. (define* (elpa->guix-package name #:key (repo 'gnu) version)
  339. "Fetch the package NAME from REPO and produce a Guix package S-expression."
  340. (match (fetch-elpa-package name repo)
  341. (#false
  342. (values #f '()))
  343. (package
  344. ;; ELPA is known to contain only GPLv3+ code. Other repos may contain
  345. ;; code under other license but there's no license metadata.
  346. (let ((license (and (memq repo '(gnu gnu/http)) 'license:gpl3+)))
  347. (elpa-package->sexp package license repo)))))
  348. ;;;
  349. ;;; Updates.
  350. ;;;
  351. (define (guix-package->elpa-name package)
  352. "Given a Guix package, PACKAGE, return the upstream name on ELPA."
  353. (or (and=> (package-properties package)
  354. (cut assq-ref <> 'upstream-name))
  355. (if (string-prefix? "emacs-" (package-name package))
  356. (string-drop (package-name package) 6)
  357. (package-name package))))
  358. (define (latest-release package)
  359. "Return an <upstream-release> for the latest release of PACKAGE."
  360. (define name (guix-package->elpa-name package))
  361. (define repo (elpa-repository package))
  362. (match (elpa-package-info name repo)
  363. (#f
  364. ;; No info, perhaps because PACKAGE is not truly an ELPA package.
  365. #f)
  366. (info
  367. (let* ((version (match info
  368. ((name raw-version . _)
  369. (elpa-version->string raw-version))))
  370. (url (match info
  371. ((_ raw-version reqs synopsis kind . rest)
  372. (package-source-url kind name version repo)))))
  373. (upstream-source
  374. (package (package-name package))
  375. (version version)
  376. (urls (list url))
  377. (signature-urls (list (string-append url ".sig"))))))))
  378. (define elpa-repository
  379. (memoize
  380. (url-predicate (lambda (url)
  381. (let ((uri (string->uri url)))
  382. (and uri
  383. (cond
  384. ((string=? (uri-host uri) "elpa.gnu.org")
  385. 'gnu)
  386. ((string=? (uri-host uri) "elpa.nongnu.org")
  387. 'nongnu)
  388. (else #f))))))))
  389. (define (package-from-elpa-repository? package)
  390. (member (elpa-repository package) '(gnu nongnu)))
  391. (define %elpa-updater
  392. ;; The ELPA updater. We restrict it to packages hosted on elpa.gnu.org
  393. ;; because for other repositories, we typically grab the source elsewhere.
  394. (upstream-updater
  395. (name 'elpa)
  396. (description "Updater for ELPA packages")
  397. (pred package-from-elpa-repository?)
  398. (latest latest-release)))
  399. (define elpa-guix-name (cut guix-name "emacs-" <>))
  400. (define* (elpa-recursive-import package-name #:optional (repo 'gnu))
  401. (recursive-import package-name
  402. #:repo repo
  403. #:repo->guix-package elpa->guix-package
  404. #:guix-name elpa-guix-name))
  405. ;;; elpa.scm ends here