elpa.scm 17 KB

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