crate.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, 2020 Martin Becze <mjbecze@riseup.net>
  5. ;;; Copyright © 2021 Nicolas Goaziou <mail@nicolasgoaziou.fr>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix import crate)
  22. #:use-module (guix base32)
  23. #:use-module (guix build-system cargo)
  24. #:use-module ((guix download) #:prefix download:)
  25. #:use-module (gcrypt hash)
  26. #:use-module (guix http-client)
  27. #:use-module (guix import json)
  28. #:use-module (guix import utils)
  29. #:use-module ((guix licenses) #:prefix license:)
  30. #:use-module (guix memoization)
  31. #:use-module (guix monads)
  32. #:use-module (guix packages)
  33. #:use-module (guix upstream)
  34. #:use-module (guix utils)
  35. #:use-module (gnu packages)
  36. #:use-module (ice-9 match)
  37. #:use-module (ice-9 regex)
  38. #:use-module (json)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-2)
  41. #:use-module (srfi srfi-26)
  42. #:use-module (srfi srfi-71)
  43. #:export (crate->guix-package
  44. guix-package->crate-name
  45. string->license
  46. crate-recursive-import
  47. %crate-updater))
  48. ;;;
  49. ;;; Interface to https://crates.io/api/v1.
  50. ;;;
  51. ;; Crates. A crate is essentially a "package". It can have several
  52. ;; "versions", each of which has its own set of dependencies, license,
  53. ;; etc.--see <crate-version> below.
  54. (define-json-mapping <crate> make-crate crate?
  55. json->crate
  56. (name crate-name) ;string
  57. (latest-version crate-latest-version "max_version") ;string
  58. (home-page crate-home-page "homepage") ;string | #nil
  59. (repository crate-repository) ;string
  60. (description crate-description) ;string
  61. (keywords crate-keywords ;list of strings
  62. "keywords" vector->list)
  63. (categories crate-categories ;list of strings
  64. "categories" vector->list)
  65. (versions crate-versions "actual_versions" ;list of <crate-version>
  66. (lambda (vector)
  67. (map json->crate-version
  68. (vector->list vector))))
  69. (links crate-links)) ;alist
  70. ;; Crate version.
  71. (define-json-mapping <crate-version> make-crate-version crate-version?
  72. json->crate-version
  73. (id crate-version-id) ;integer
  74. (number crate-version-number "num") ;string
  75. (download-path crate-version-download-path "dl_path") ;string
  76. (readme-path crate-version-readme-path "readme_path") ;string
  77. (license crate-version-license "license") ;string
  78. (links crate-version-links)) ;alist
  79. ;; Crate dependency. Each dependency (each edge in the graph) is annotated as
  80. ;; being a "normal" dependency or a development dependency. There also
  81. ;; information about the minimum required version, such as "^0.0.41".
  82. (define-json-mapping <crate-dependency> make-crate-dependency
  83. crate-dependency?
  84. json->crate-dependency
  85. (id crate-dependency-id "crate_id") ;string
  86. (kind crate-dependency-kind "kind" ;'normal | 'dev | 'build
  87. string->symbol)
  88. (requirement crate-dependency-requirement "req")) ;string
  89. ;; Autoload Guile-Semver so we only have a soft dependency.
  90. (module-autoload! (current-module)
  91. '(semver) '(string->semver semver->string semver<?))
  92. (module-autoload! (current-module)
  93. '(semver ranges) '(string->semver-range semver-range-contains?))
  94. (define (lookup-crate name)
  95. "Look up NAME on https://crates.io and return the corresopnding <crate>
  96. record or #f if it was not found."
  97. (let ((json (json-fetch (string-append (%crate-base-url) "/api/v1/crates/"
  98. name))))
  99. (and=> (and json (assoc-ref json "crate"))
  100. (lambda (alist)
  101. ;; The "versions" field of ALIST is simply a list of version IDs
  102. ;; (integers). Here, we squeeze in the actual version
  103. ;; dictionaries that are not part of ALIST but are just more
  104. ;; convenient handled this way.
  105. (let ((versions (or (assoc-ref json "versions") '#())))
  106. (json->crate `(,@alist
  107. ("actual_versions" . ,versions))))))))
  108. (define lookup-crate* (memoize lookup-crate))
  109. (define (crate-version-dependencies version)
  110. "Return the list of <crate-dependency> records of VERSION, a
  111. <crate-version>."
  112. (let* ((path (assoc-ref (crate-version-links version) "dependencies"))
  113. (url (string-append (%crate-base-url) path)))
  114. (match (assoc-ref (or (json-fetch url) '()) "dependencies")
  115. ((? vector? vector)
  116. (delete-duplicates (map json->crate-dependency (vector->list vector))))
  117. (_
  118. '()))))
  119. ;;;
  120. ;;; Converting crates to Guix packages.
  121. ;;;
  122. (define (maybe-cargo-inputs package-names)
  123. (match (package-names->package-inputs package-names)
  124. (()
  125. '())
  126. ((package-inputs ...)
  127. `(#:cargo-inputs ,package-inputs))))
  128. (define (maybe-cargo-development-inputs package-names)
  129. (match (package-names->package-inputs package-names)
  130. (()
  131. '())
  132. ((package-inputs ...)
  133. `(#:cargo-development-inputs ,package-inputs))))
  134. (define (maybe-arguments arguments)
  135. (match arguments
  136. (()
  137. '())
  138. ((args ...)
  139. `((arguments (,'quasiquote ,args))))))
  140. (define (version->semver-prefix version)
  141. "Return the version up to and including the first non-zero part"
  142. (first
  143. (map match:substring
  144. (list-matches "^(0+\\.){,2}[0-9]+" version))))
  145. (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
  146. home-page synopsis description license build?)
  147. "Return the `package' s-expression for a rust package with the given NAME,
  148. VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
  149. and LICENSE."
  150. (define (format-inputs inputs)
  151. (map
  152. (match-lambda
  153. ((name version)
  154. (list (crate-name->package-name name)
  155. (version->semver-prefix version))))
  156. inputs))
  157. (let* ((port (http-fetch (crate-uri name version)))
  158. (guix-name (crate-name->package-name name))
  159. (cargo-inputs (format-inputs cargo-inputs))
  160. (cargo-development-inputs (format-inputs cargo-development-inputs))
  161. (pkg `(package
  162. (name ,guix-name)
  163. (version ,version)
  164. (source (origin
  165. (method url-fetch)
  166. (uri (crate-uri ,name version))
  167. (file-name (string-append name "-" version ".tar.gz"))
  168. (sha256
  169. (base32
  170. ,(bytevector->nix-base32-string (port-sha256 port))))))
  171. (build-system cargo-build-system)
  172. ,@(maybe-arguments (append (if build?
  173. '()
  174. '(#:skip-build? #t))
  175. (maybe-cargo-inputs cargo-inputs)
  176. (maybe-cargo-development-inputs
  177. cargo-development-inputs)))
  178. (home-page ,home-page)
  179. (synopsis ,synopsis)
  180. (description ,(beautify-description description))
  181. (license ,(match license
  182. (() #f)
  183. ((license) license)
  184. (_ `(list ,@license)))))))
  185. (close-port port)
  186. (package->definition pkg (version->semver-prefix version))))
  187. (define (string->license string)
  188. (filter-map (lambda (license)
  189. (and (not (string-null? license))
  190. (not (any (lambda (elem) (string=? elem license))
  191. '("AND" "OR" "WITH")))
  192. (or (spdx-string->license license)
  193. 'unknown-license!)))
  194. (string-split string (string->char-set " /"))))
  195. (define* (crate->guix-package crate-name #:key version include-dev-deps? repo)
  196. "Fetch the metadata for CRATE-NAME from crates.io, and return the
  197. `package' s-expression corresponding to that package, or #f on failure.
  198. When VERSION is specified, convert it into a semver range and attempt to fetch
  199. the latest version matching this semver range; otherwise fetch the latest
  200. version of CRATE-NAME. If INCLUDE-DEV-DEPS is true then this will also
  201. look up the development dependencs for the given crate."
  202. (define (semver-range-contains-string? range version)
  203. (semver-range-contains? (string->semver-range range)
  204. (string->semver version)))
  205. (define (normal-dependency? dependency)
  206. (or (eq? (crate-dependency-kind dependency) 'build)
  207. (eq? (crate-dependency-kind dependency) 'normal)))
  208. (define crate
  209. (lookup-crate* crate-name))
  210. (define version-number
  211. (and crate
  212. (or version
  213. (crate-latest-version crate))))
  214. ;; find the highest existing package that fulfills the semver <range>
  215. (define (find-package-version name range)
  216. (let* ((semver-range (string->semver-range range))
  217. (versions
  218. (sort
  219. (filter (lambda (version)
  220. (semver-range-contains? semver-range version))
  221. (map (lambda (pkg)
  222. (string->semver (package-version pkg)))
  223. (find-packages-by-name
  224. (crate-name->package-name name))))
  225. semver<?)))
  226. (and (not (null-list? versions))
  227. (semver->string (last versions)))))
  228. ;; find the highest version of a crate that fulfills the semver <range>
  229. (define (find-crate-version crate range)
  230. (let* ((semver-range (string->semver-range range))
  231. (versions
  232. (sort
  233. (filter (lambda (entry)
  234. (semver-range-contains? semver-range (first entry)))
  235. (map (lambda (ver)
  236. (list (string->semver (crate-version-number ver))
  237. ver))
  238. (crate-versions crate)))
  239. (match-lambda* (((semver _) ...)
  240. (apply semver<? semver))))))
  241. (and (not (null-list? versions))
  242. (second (last versions)))))
  243. (define (dependency-name+version dep)
  244. (let* ((name (crate-dependency-id dep))
  245. (req (crate-dependency-requirement dep))
  246. (existing-version (find-package-version name req)))
  247. (if existing-version
  248. (list name existing-version)
  249. (let* ((crate (lookup-crate* name))
  250. (ver (find-crate-version crate req)))
  251. (list name
  252. (crate-version-number ver))))))
  253. (define version*
  254. (and crate
  255. (find-crate-version crate version-number)))
  256. ;; sort and map the dependencies to a list containing
  257. ;; pairs of (name version)
  258. (define (sort-map-dependencies deps)
  259. (sort (map dependency-name+version
  260. deps)
  261. (match-lambda* (((name _) ...)
  262. (apply string-ci<? name)))))
  263. (and crate version*
  264. (let* ((dependencies (crate-version-dependencies version*))
  265. (dep-crates dev-dep-crates (partition normal-dependency? dependencies))
  266. (cargo-inputs (sort-map-dependencies dep-crates))
  267. (cargo-development-inputs (if include-dev-deps?
  268. (sort-map-dependencies dev-dep-crates)
  269. '())))
  270. (values
  271. (make-crate-sexp #:build? include-dev-deps?
  272. #:name crate-name
  273. #:version (crate-version-number version*)
  274. #:cargo-inputs cargo-inputs
  275. #:cargo-development-inputs cargo-development-inputs
  276. #:home-page
  277. (let ((home-page (crate-home-page crate)))
  278. (if (string? home-page)
  279. home-page
  280. (let ((repository (crate-repository crate)))
  281. (if (string? repository)
  282. repository
  283. ""))))
  284. #:synopsis (crate-description crate)
  285. #:description (crate-description crate)
  286. #:license (and=> (crate-version-license version*)
  287. string->license))
  288. (append cargo-inputs cargo-development-inputs)))))
  289. (define* (crate-recursive-import crate-name #:key version)
  290. (recursive-import crate-name
  291. #:repo->guix-package (lambda* params
  292. ;; download development dependencies only for the top level package
  293. (let ((include-dev-deps? (equal? (car params) crate-name))
  294. (crate->guix-package* (memoize crate->guix-package)))
  295. (apply crate->guix-package*
  296. (append params `(#:include-dev-deps? ,include-dev-deps?)))))
  297. #:version version
  298. #:guix-name crate-name->package-name))
  299. (define (guix-package->crate-name package)
  300. "Return the crate name of PACKAGE."
  301. (and-let* ((origin (package-source package))
  302. (uri (origin-uri origin))
  303. (crate-url? uri)
  304. (len (string-length crate-url))
  305. (path (xsubstring uri len))
  306. (parts (string-split path #\/)))
  307. (match parts
  308. ((name _ ...) name))))
  309. (define (crate-name->package-name name)
  310. (guix-name "rust-" name))
  311. ;;;
  312. ;;; Updater
  313. ;;;
  314. (define crate-package?
  315. (url-predicate crate-url?))
  316. (define (latest-release package)
  317. "Return an <upstream-source> for the latest release of PACKAGE."
  318. (let* ((crate-name (guix-package->crate-name package))
  319. (crate (lookup-crate crate-name))
  320. (version (crate-latest-version crate))
  321. (url (crate-uri crate-name version)))
  322. (upstream-source
  323. (package (package-name package))
  324. (version version)
  325. (urls (list url)))))
  326. (define %crate-updater
  327. (upstream-updater
  328. (name 'crate)
  329. (description "Updater for crates.io packages")
  330. (pred crate-package?)
  331. (latest latest-release)))