crate.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2019 Martin Becze <mjbecze@riseup.net>
  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 crate)
  21. #:use-module (guix base32)
  22. #:use-module (guix build-system cargo)
  23. #:use-module ((guix download) #:prefix download:)
  24. #:use-module (gcrypt hash)
  25. #:use-module (guix http-client)
  26. #:use-module (guix import json)
  27. #:use-module (guix import utils)
  28. #:use-module ((guix licenses) #:prefix license:)
  29. #:use-module (guix monads)
  30. #:use-module (guix packages)
  31. #:use-module (guix upstream)
  32. #:use-module (guix utils)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 regex)
  35. #:use-module (json)
  36. #:use-module (srfi srfi-1)
  37. #:use-module (srfi srfi-2)
  38. #:use-module (srfi srfi-26)
  39. #:export (crate->guix-package
  40. guix-package->crate-name
  41. string->license
  42. crate-recursive-import
  43. %crate-updater))
  44. ;;;
  45. ;;; Interface to https://crates.io/api/v1.
  46. ;;;
  47. ;; Crates. A crate is essentially a "package". It can have several
  48. ;; "versions", each of which has its own set of dependencies, license,
  49. ;; etc.--see <crate-version> below.
  50. (define-json-mapping <crate> make-crate crate?
  51. json->crate
  52. (name crate-name) ;string
  53. (latest-version crate-latest-version "max_version") ;string
  54. (home-page crate-home-page "homepage") ;string | #nil
  55. (repository crate-repository) ;string
  56. (description crate-description) ;string
  57. (keywords crate-keywords ;list of strings
  58. "keywords" vector->list)
  59. (categories crate-categories ;list of strings
  60. "categories" vector->list)
  61. (versions crate-versions "actual_versions" ;list of <crate-version>
  62. (lambda (vector)
  63. (map json->crate-version
  64. (vector->list vector))))
  65. (links crate-links)) ;alist
  66. ;; Crate version.
  67. (define-json-mapping <crate-version> make-crate-version crate-version?
  68. json->crate-version
  69. (id crate-version-id) ;integer
  70. (number crate-version-number "num") ;string
  71. (download-path crate-version-download-path "dl_path") ;string
  72. (readme-path crate-version-readme-path "readme_path") ;string
  73. (license crate-version-license "license") ;string
  74. (links crate-version-links)) ;alist
  75. ;; Crate dependency. Each dependency (each edge in the graph) is annotated as
  76. ;; being a "normal" dependency or a development dependency. There also
  77. ;; information about the minimum required version, such as "^0.0.41".
  78. (define-json-mapping <crate-dependency> make-crate-dependency
  79. crate-dependency?
  80. json->crate-dependency
  81. (id crate-dependency-id "crate_id") ;string
  82. (kind crate-dependency-kind "kind" ;'normal | 'dev
  83. string->symbol)
  84. (requirement crate-dependency-requirement "req")) ;string
  85. (define (lookup-crate name)
  86. "Look up NAME on https://crates.io and return the corresopnding <crate>
  87. record or #f if it was not found."
  88. (let ((json (json-fetch (string-append (%crate-base-url) "/api/v1/crates/"
  89. name))))
  90. (and=> (and json (assoc-ref json "crate"))
  91. (lambda (alist)
  92. ;; The "versions" field of ALIST is simply a list of version IDs
  93. ;; (integers). Here, we squeeze in the actual version
  94. ;; dictionaries that are not part of ALIST but are just more
  95. ;; convenient handled this way.
  96. (let ((versions (or (assoc-ref json "versions") '#())))
  97. (json->crate `(,@alist
  98. ("actual_versions" . ,versions))))))))
  99. (define (crate-version-dependencies version)
  100. "Return the list of <crate-dependency> records of VERSION, a
  101. <crate-version>."
  102. (let* ((path (assoc-ref (crate-version-links version) "dependencies"))
  103. (url (string-append (%crate-base-url) path)))
  104. (match (assoc-ref (or (json-fetch url) '()) "dependencies")
  105. ((? vector? vector)
  106. (delete-duplicates (map json->crate-dependency (vector->list vector))))
  107. (_
  108. '()))))
  109. ;;;
  110. ;;; Converting crates to Guix packages.
  111. ;;;
  112. (define (maybe-cargo-inputs package-names)
  113. (match (package-names->package-inputs package-names)
  114. (()
  115. '())
  116. ((package-inputs ...)
  117. `(#:cargo-inputs ,package-inputs))))
  118. (define (maybe-cargo-development-inputs package-names)
  119. (match (package-names->package-inputs package-names)
  120. (()
  121. '())
  122. ((package-inputs ...)
  123. `(#:cargo-development-inputs ,package-inputs))))
  124. (define (maybe-arguments arguments)
  125. (match arguments
  126. (()
  127. '())
  128. ((args ...)
  129. `((arguments (,'quasiquote ,args))))))
  130. (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
  131. home-page synopsis description license
  132. #:allow-other-keys)
  133. "Return the `package' s-expression for a rust package with the given NAME,
  134. VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
  135. and LICENSE."
  136. (let* ((port (http-fetch (crate-uri name version)))
  137. (guix-name (crate-name->package-name name))
  138. (cargo-inputs (map crate-name->package-name cargo-inputs))
  139. (cargo-development-inputs (map crate-name->package-name
  140. cargo-development-inputs))
  141. (pkg `(package
  142. (name ,guix-name)
  143. (version ,version)
  144. (source (origin
  145. (method url-fetch)
  146. (uri (crate-uri ,name version))
  147. (file-name (string-append name "-" version ".tar.gz"))
  148. (sha256
  149. (base32
  150. ,(bytevector->nix-base32-string (port-sha256 port))))))
  151. (build-system cargo-build-system)
  152. ,@(maybe-arguments (append (maybe-cargo-inputs cargo-inputs)
  153. (maybe-cargo-development-inputs
  154. cargo-development-inputs)))
  155. (home-page ,(match home-page
  156. (() "")
  157. (_ home-page)))
  158. (synopsis ,synopsis)
  159. (description ,(beautify-description description))
  160. (license ,(match license
  161. (() #f)
  162. ((license) license)
  163. (_ `(list ,@license)))))))
  164. (close-port port)
  165. pkg))
  166. (define (string->license string)
  167. (filter-map (lambda (license)
  168. (and (not (string-null? license))
  169. (not (any (lambda (elem) (string=? elem license))
  170. '("AND" "OR" "WITH")))
  171. (or (spdx-string->license license)
  172. 'unknown-license!)))
  173. (string-split string (string->char-set " /"))))
  174. (define* (crate->guix-package crate-name #:optional version)
  175. "Fetch the metadata for CRATE-NAME from crates.io, and return the
  176. `package' s-expression corresponding to that package, or #f on failure.
  177. When VERSION is specified, attempt to fetch that version; otherwise fetch the
  178. latest version of CRATE-NAME."
  179. (define (normal-dependency? dependency)
  180. (eq? (crate-dependency-kind dependency) 'normal))
  181. (define crate
  182. (lookup-crate crate-name))
  183. (define version-number
  184. (and crate
  185. (or version
  186. (crate-latest-version crate))))
  187. (define version*
  188. (and crate
  189. (find (lambda (version)
  190. (string=? (crate-version-number version)
  191. version-number))
  192. (crate-versions crate))))
  193. (and crate version*
  194. (let* ((dependencies (crate-version-dependencies version*))
  195. (dep-crates (filter normal-dependency? dependencies))
  196. (dev-dep-crates (remove normal-dependency? dependencies))
  197. (cargo-inputs (sort (map crate-dependency-id dep-crates)
  198. string-ci<?))
  199. (cargo-development-inputs
  200. (sort (map crate-dependency-id dev-dep-crates)
  201. string-ci<?)))
  202. (values
  203. (make-crate-sexp #:name crate-name
  204. #:version (crate-version-number version*)
  205. #:cargo-inputs cargo-inputs
  206. #:cargo-development-inputs cargo-development-inputs
  207. #:home-page (or (crate-home-page crate)
  208. (crate-repository crate))
  209. #:synopsis (crate-description crate)
  210. #:description (crate-description crate)
  211. #:license (and=> (crate-version-license version*)
  212. string->license))
  213. (append cargo-inputs cargo-development-inputs)))))
  214. (define* (crate-recursive-import crate-name #:optional version)
  215. (recursive-import crate-name #f
  216. #:repo->guix-package
  217. (lambda (name repo)
  218. (let ((version (and (string=? name crate-name)
  219. version)))
  220. (crate->guix-package name version)))
  221. #:guix-name crate-name->package-name))
  222. (define (guix-package->crate-name package)
  223. "Return the crate name of PACKAGE."
  224. (and-let* ((origin (package-source package))
  225. (uri (origin-uri origin))
  226. (crate-url? uri)
  227. (len (string-length crate-url))
  228. (path (xsubstring uri len))
  229. (parts (string-split path #\/)))
  230. (match parts
  231. ((name _ ...) name))))
  232. (define (crate-name->package-name name)
  233. (string-append "rust-" (string-join (string-split name #\_) "-")))
  234. ;;;
  235. ;;; Updater
  236. ;;;
  237. (define crate-package?
  238. (url-predicate crate-url?))
  239. (define (latest-release package)
  240. "Return an <upstream-source> for the latest release of PACKAGE."
  241. (let* ((crate-name (guix-package->crate-name package))
  242. (crate (lookup-crate crate-name))
  243. (version (crate-latest-version crate))
  244. (url (crate-uri crate-name version)))
  245. (upstream-source
  246. (package (package-name package))
  247. (version version)
  248. (urls (list url)))))
  249. (define %crate-updater
  250. (upstream-updater
  251. (name 'crates)
  252. (description "Updater for crates.io packages")
  253. (pred crate-package?)
  254. (latest latest-release)))