elpa.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2015-2018, 2020-2021, 2023 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. ;;; Copyright © 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  11. ;;;
  12. ;;; This file is part of GNU Guix.
  13. ;;;
  14. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  15. ;;; under the terms of the GNU General Public License as published by
  16. ;;; the Free Software Foundation; either version 3 of the License, or (at
  17. ;;; your option) any later version.
  18. ;;;
  19. ;;; GNU Guix is distributed in the hope that it will be useful, but
  20. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;;; GNU General Public License for more details.
  23. ;;;
  24. ;;; You should have received a copy of the GNU General Public License
  25. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  26. (define-module (guix import elpa)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 rdelim)
  29. #:use-module (ice-9 regex)
  30. #:use-module (web uri)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-9)
  33. #:use-module (srfi srfi-9 gnu)
  34. #:use-module (srfi srfi-11)
  35. #:use-module (srfi srfi-26)
  36. #:use-module (srfi srfi-34)
  37. #:use-module (srfi srfi-35)
  38. #:use-module (guix i18n)
  39. #:use-module (guix diagnostics)
  40. #:use-module ((guix download) #:select (download-to-store))
  41. #:use-module (guix import utils)
  42. #:use-module (guix http-client)
  43. #:use-module (guix git)
  44. #:use-module (guix hash)
  45. #:use-module (guix store)
  46. #:use-module (guix base32)
  47. #:use-module (guix upstream)
  48. #:use-module (guix packages)
  49. #:use-module (guix memoization)
  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 (elpa-dependency->upstream-input dependency)
  248. "Convert DEPENDENCY, an sexp as returned by 'elpa-package-inputs', into an
  249. <upstream-input>."
  250. (match dependency
  251. ((name version)
  252. (and (not (emacs-standard-library? (symbol->string name)))
  253. (upstream-input
  254. (name (symbol->string name))
  255. (downstream-name (elpa-guix-name name))
  256. (type 'propagated)
  257. (min-version (if (pair? version)
  258. (string-join (map number->string version) ".")
  259. #f))
  260. (max-version (match version
  261. (() #f)
  262. ((_) #f)
  263. ((_ _) #f)
  264. (_ min-version))))))))
  265. (define default-files-spec
  266. ;; This contains more than just the things contained in %default-include and
  267. ;; %default-exclude, presumably because this includes source files (*.in,
  268. ;; *.texi, etc.) which have already been processed for releases.
  269. ;;
  270. ;; Taken from:
  271. ;; https://github.com/melpa/melpa/blob/e8dc709d0ab2b4a68c59315f42858bcb86095f11/package-build/package-build.el#L580-L585
  272. '("*.el" "*.el.in" "dir"
  273. "*.info" "*.texi" "*.texinfo"
  274. "doc/dir" "doc/*.info" "doc/*.texi" "doc/*.texinfo"
  275. (:exclude ".dir-locals.el" "test.el" "tests.el" "*-test.el" "*-tests.el")))
  276. (define* (melpa-recipe->maybe-arguments melpa-recipe)
  277. "Extract arguments for the build system from MELPA-RECIPE."
  278. (define (glob->regexp glob)
  279. (string-append
  280. "^"
  281. (regexp-substitute/global #f "\\*\\*?" glob
  282. 'pre
  283. (lambda (m)
  284. (if (string= (match:substring m 0) "**")
  285. ".*"
  286. "[^/]+"))
  287. 'post)
  288. "$"))
  289. (let ((files (assq-ref melpa-recipe ':files)))
  290. (if files
  291. (let* ((with-default (apply append (map (lambda (entry)
  292. (if (eq? ':defaults entry)
  293. default-files-spec
  294. (list entry)))
  295. files)))
  296. (inclusions (remove pair? with-default))
  297. (exclusions (apply append (map (match-lambda
  298. ((':exclude . values)
  299. values)
  300. (_ '()))
  301. with-default))))
  302. `((arguments '(#:include ',(map glob->regexp inclusions)
  303. #:exclude ',(map glob->regexp exclusions)))))
  304. '())))
  305. (define* (elpa-package->sexp pkg #:optional license repo)
  306. "Return the `package' S-expression for the Emacs package PKG, a record of
  307. type '<elpa-package>'."
  308. (define melpa-recipe
  309. ;; XXX: Call 'identity' to work around a Guile 3.0.[5-7] compiler bug:
  310. ;; <https://bugs.gnu.org/48368>.
  311. (and (eq? (identity repo) 'melpa)
  312. (package-name->melpa-recipe (elpa-package-name pkg))))
  313. (define name (elpa-package-name pkg))
  314. (define version (elpa-package-version pkg))
  315. (define source-url (elpa-package-source-url pkg))
  316. (define dependencies-names
  317. (filter-dependencies (elpa-dependencies->names
  318. (elpa-package-inputs pkg))))
  319. (define dependencies
  320. (map (compose string->symbol elpa-name->package-name)
  321. dependencies-names))
  322. (define (maybe-inputs input-type inputs)
  323. (match inputs
  324. (()
  325. '())
  326. ((inputs ...)
  327. (list (list input-type `(list ,@inputs))))))
  328. (define melpa-source
  329. (melpa-recipe->origin melpa-recipe))
  330. (values
  331. `(package
  332. (name ,(elpa-name->package-name name))
  333. (version ,version)
  334. (source ,(or melpa-source
  335. (let ((tarball (with-store store
  336. (download-to-store store source-url))))
  337. `(origin
  338. (method url-fetch)
  339. (uri (string-append ,@(factorize-uri source-url version)))
  340. (sha256
  341. (base32
  342. ,(if tarball
  343. (bytevector->nix-base32-string
  344. (file-hash* tarball #:recursive? #false))
  345. "failed to download package")))))))
  346. (build-system emacs-build-system)
  347. ,@(maybe-inputs 'propagated-inputs dependencies)
  348. ,@(if melpa-source
  349. (melpa-recipe->maybe-arguments melpa-recipe)
  350. '())
  351. (home-page ,(elpa-package-home-page pkg))
  352. (synopsis ,(elpa-package-synopsis pkg))
  353. (description ,(beautify-description (elpa-package-description pkg)))
  354. (license ,license))
  355. dependencies-names))
  356. (define* (elpa->guix-package name #:key (repo 'gnu) version
  357. #:allow-other-keys)
  358. "Fetch the package NAME from REPO and produce a Guix package S-expression."
  359. (match (fetch-elpa-package name repo)
  360. (#false
  361. (values #f '()))
  362. (package
  363. ;; ELPA is known to contain only GPLv3+ code. Other repos may contain
  364. ;; code under other license but there's no license metadata.
  365. (let ((license (and (memq repo '(gnu gnu/http)) 'license:gpl3+)))
  366. (elpa-package->sexp package license repo)))))
  367. ;;;
  368. ;;; Updates.
  369. ;;;
  370. (define (guix-package->elpa-name package)
  371. "Given a Guix package, PACKAGE, return the upstream name on ELPA."
  372. (or (and=> (package-properties package)
  373. (cut assq-ref <> 'upstream-name))
  374. (if (string-prefix? "emacs-" (package-name package))
  375. (string-drop (package-name package) 6)
  376. (package-name package))))
  377. (define* (latest-release package #:key (version #f))
  378. "Return an <upstream-release> for the latest release of PACKAGE."
  379. (define name (guix-package->elpa-name package))
  380. (define repo (elpa-repository package))
  381. (when version
  382. (raise
  383. (formatted-message
  384. (G_ "~a updater doesn't support updating to a specific version, sorry.")
  385. "elpa")))
  386. (match (elpa-package-info name repo)
  387. (#f
  388. ;; No info, perhaps because PACKAGE is not truly an ELPA package.
  389. #f)
  390. (info
  391. (let* ((version (match info
  392. ((name raw-version . _)
  393. (elpa-version->string raw-version))))
  394. (url (match info
  395. ((_ raw-version reqs synopsis kind . rest)
  396. (package-source-url kind name version repo))))
  397. (inputs (match info
  398. ((name raw-version reqs . _)
  399. (filter-map elpa-dependency->upstream-input
  400. (if (eq? 'nil reqs)
  401. '()
  402. reqs))))))
  403. (upstream-source
  404. (package (package-name package))
  405. (version version)
  406. (urls (list url))
  407. (signature-urls (list (string-append url ".sig")))
  408. (inputs inputs))))))
  409. (define elpa-repository
  410. (memoize
  411. (url-predicate (lambda (url)
  412. (let ((uri (string->uri url)))
  413. (and uri
  414. (cond
  415. ((string=? (uri-host uri) "elpa.gnu.org")
  416. 'gnu)
  417. ((string=? (uri-host uri) "elpa.nongnu.org")
  418. 'nongnu)
  419. (else #f))))))))
  420. (define (package-from-elpa-repository? package)
  421. (member (elpa-repository package) '(gnu nongnu)))
  422. (define %elpa-updater
  423. ;; The ELPA updater. We restrict it to packages hosted on elpa.gnu.org
  424. ;; because for other repositories, we typically grab the source elsewhere.
  425. (upstream-updater
  426. (name 'elpa)
  427. (description "Updater for ELPA packages")
  428. (pred package-from-elpa-repository?)
  429. (import latest-release)))
  430. (define elpa-guix-name (cut guix-name "emacs-" <>))
  431. (define* (elpa-recursive-import package-name #:optional (repo 'gnu))
  432. (recursive-import package-name
  433. #:repo->guix-package
  434. (lambda (name . rest)
  435. (apply elpa->guix-package name
  436. #:repo repo
  437. rest))
  438. #:guix-name elpa-guix-name))
  439. ;;; elpa.scm ends here