crate.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2019, 2020, 2021 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 | #f
  78. (match-lambda
  79. ('null #f)
  80. ((? string? str) str)))
  81. (links crate-version-links)) ;alist
  82. ;; Crate dependency. Each dependency (each edge in the graph) is annotated as
  83. ;; being a "normal" dependency or a development dependency. There also
  84. ;; information about the minimum required version, such as "^0.0.41".
  85. (define-json-mapping <crate-dependency> make-crate-dependency
  86. crate-dependency?
  87. json->crate-dependency
  88. (id crate-dependency-id "crate_id") ;string
  89. (kind crate-dependency-kind "kind" ;'normal | 'dev | 'build
  90. string->symbol)
  91. (requirement crate-dependency-requirement "req")) ;string
  92. ;; Autoload Guile-Semver so we only have a soft dependency.
  93. (module-autoload! (current-module)
  94. '(semver) '(string->semver semver->string semver<?))
  95. (module-autoload! (current-module)
  96. '(semver ranges) '(string->semver-range semver-range-contains?))
  97. (define (lookup-crate name)
  98. "Look up NAME on https://crates.io and return the corresopnding <crate>
  99. record or #f if it was not found."
  100. (let ((json (json-fetch (string-append (%crate-base-url) "/api/v1/crates/"
  101. name))))
  102. (and=> (and json (assoc-ref json "crate"))
  103. (lambda (alist)
  104. ;; The "versions" field of ALIST is simply a list of version IDs
  105. ;; (integers). Here, we squeeze in the actual version
  106. ;; dictionaries that are not part of ALIST but are just more
  107. ;; convenient handled this way.
  108. (let ((versions (or (assoc-ref json "versions") '#())))
  109. (json->crate `(,@alist
  110. ("actual_versions" . ,versions))))))))
  111. (define lookup-crate* (memoize lookup-crate))
  112. (define (crate-version-dependencies version)
  113. "Return the list of <crate-dependency> records of VERSION, a
  114. <crate-version>."
  115. (let* ((path (assoc-ref (crate-version-links version) "dependencies"))
  116. (url (string-append (%crate-base-url) path)))
  117. (match (assoc-ref (or (json-fetch url) '()) "dependencies")
  118. ((? vector? vector)
  119. (delete-duplicates (map json->crate-dependency (vector->list vector))))
  120. (_
  121. '()))))
  122. ;;;
  123. ;;; Converting crates to Guix packages.
  124. ;;;
  125. (define (maybe-cargo-inputs package-names)
  126. (match (package-names->package-inputs package-names)
  127. (()
  128. '())
  129. ((package-inputs ...)
  130. `(#:cargo-inputs ,package-inputs))))
  131. (define (maybe-cargo-development-inputs package-names)
  132. (match (package-names->package-inputs package-names)
  133. (()
  134. '())
  135. ((package-inputs ...)
  136. `(#:cargo-development-inputs ,package-inputs))))
  137. (define (maybe-arguments arguments)
  138. (match arguments
  139. (()
  140. '())
  141. ((args ...)
  142. `((arguments (,'quasiquote ,args))))))
  143. (define (version->semver-prefix version)
  144. "Return the version up to and including the first non-zero part"
  145. (first
  146. (map match:substring
  147. (list-matches "^(0+\\.){,2}[0-9]+" version))))
  148. (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
  149. home-page synopsis description license build?)
  150. "Return the `package' s-expression for a rust package with the given NAME,
  151. VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
  152. and LICENSE."
  153. (define (format-inputs inputs)
  154. (map
  155. (match-lambda
  156. ((name version)
  157. (list (crate-name->package-name name)
  158. (version->semver-prefix version))))
  159. inputs))
  160. (let* ((port (http-fetch (crate-uri name version)))
  161. (guix-name (crate-name->package-name name))
  162. (cargo-inputs (format-inputs cargo-inputs))
  163. (cargo-development-inputs (format-inputs cargo-development-inputs))
  164. (pkg `(package
  165. (name ,guix-name)
  166. (version ,version)
  167. (source (origin
  168. (method url-fetch)
  169. (uri (crate-uri ,name version))
  170. (file-name (string-append name "-" version ".tar.gz"))
  171. (sha256
  172. (base32
  173. ,(bytevector->nix-base32-string (port-sha256 port))))))
  174. (build-system cargo-build-system)
  175. ,@(maybe-arguments (append (if build?
  176. '()
  177. '(#:skip-build? #t))
  178. (maybe-cargo-inputs cargo-inputs)
  179. (maybe-cargo-development-inputs
  180. cargo-development-inputs)))
  181. (home-page ,home-page)
  182. (synopsis ,synopsis)
  183. (description ,(beautify-description description))
  184. (license ,(match license
  185. (() #f)
  186. (#f #f)
  187. ((license) license)
  188. (_ `(list ,@license)))))))
  189. (close-port port)
  190. (package->definition pkg (version->semver-prefix version))))
  191. (define (string->license string)
  192. (filter-map (lambda (license)
  193. (and (not (string-null? license))
  194. (not (any (lambda (elem) (string=? elem license))
  195. '("AND" "OR" "WITH")))
  196. (or (spdx-string->license license)
  197. 'unknown-license!)))
  198. (string-split string (string->char-set " /"))))
  199. (define* (crate->guix-package crate-name #:key version include-dev-deps? repo)
  200. "Fetch the metadata for CRATE-NAME from crates.io, and return the
  201. `package' s-expression corresponding to that package, or #f on failure.
  202. When VERSION is specified, convert it into a semver range and attempt to fetch
  203. the latest version matching this semver range; otherwise fetch the latest
  204. version of CRATE-NAME. If INCLUDE-DEV-DEPS is true then this will also
  205. look up the development dependencs for the given crate."
  206. (define (semver-range-contains-string? range version)
  207. (semver-range-contains? (string->semver-range range)
  208. (string->semver version)))
  209. (define (normal-dependency? dependency)
  210. (or (eq? (crate-dependency-kind dependency) 'build)
  211. (eq? (crate-dependency-kind dependency) 'normal)))
  212. (define crate
  213. (lookup-crate* crate-name))
  214. (define version-number
  215. (and crate
  216. (or version
  217. (crate-latest-version crate))))
  218. ;; find the highest existing package that fulfills the semver <range>
  219. (define (find-package-version name range)
  220. (let* ((semver-range (string->semver-range range))
  221. (versions
  222. (sort
  223. (filter (lambda (version)
  224. (semver-range-contains? semver-range version))
  225. (map (lambda (pkg)
  226. (string->semver (package-version pkg)))
  227. (find-packages-by-name
  228. (crate-name->package-name name))))
  229. semver<?)))
  230. (and (not (null-list? versions))
  231. (semver->string (last versions)))))
  232. ;; find the highest version of a crate that fulfills the semver <range>
  233. (define (find-crate-version crate range)
  234. (let* ((semver-range (string->semver-range range))
  235. (versions
  236. (sort
  237. (filter (lambda (entry)
  238. (semver-range-contains? semver-range (first entry)))
  239. (map (lambda (ver)
  240. (list (string->semver (crate-version-number ver))
  241. ver))
  242. (crate-versions crate)))
  243. (match-lambda* (((semver _) ...)
  244. (apply semver<? semver))))))
  245. (and (not (null-list? versions))
  246. (second (last versions)))))
  247. (define (dependency-name+version dep)
  248. (let* ((name (crate-dependency-id dep))
  249. (req (crate-dependency-requirement dep))
  250. (existing-version (find-package-version name req)))
  251. (if existing-version
  252. (list name existing-version)
  253. (let* ((crate (lookup-crate* name))
  254. (ver (find-crate-version crate req)))
  255. (list name
  256. (crate-version-number ver))))))
  257. (define version*
  258. (and crate
  259. (find-crate-version crate version-number)))
  260. ;; sort and map the dependencies to a list containing
  261. ;; pairs of (name version)
  262. (define (sort-map-dependencies deps)
  263. (sort (map dependency-name+version
  264. deps)
  265. (match-lambda* (((name _) ...)
  266. (apply string-ci<? name)))))
  267. (and crate version*
  268. (let* ((dependencies (crate-version-dependencies version*))
  269. (dep-crates dev-dep-crates (partition normal-dependency? dependencies))
  270. (cargo-inputs (sort-map-dependencies dep-crates))
  271. (cargo-development-inputs (if include-dev-deps?
  272. (sort-map-dependencies dev-dep-crates)
  273. '())))
  274. (values
  275. (make-crate-sexp #:build? include-dev-deps?
  276. #:name crate-name
  277. #:version (crate-version-number version*)
  278. #:cargo-inputs cargo-inputs
  279. #:cargo-development-inputs cargo-development-inputs
  280. #:home-page
  281. (let ((home-page (crate-home-page crate)))
  282. (if (string? home-page)
  283. home-page
  284. (let ((repository (crate-repository crate)))
  285. (if (string? repository)
  286. repository
  287. ""))))
  288. #:synopsis (crate-description crate)
  289. #:description (crate-description crate)
  290. #:license (and=> (crate-version-license version*)
  291. string->license))
  292. (append cargo-inputs cargo-development-inputs)))))
  293. (define* (crate-recursive-import crate-name #:key version)
  294. (recursive-import crate-name
  295. #:repo->guix-package (lambda* params
  296. ;; download development dependencies only for the top level package
  297. (let ((include-dev-deps? (equal? (car params) crate-name))
  298. (crate->guix-package* (memoize crate->guix-package)))
  299. (apply crate->guix-package*
  300. (append params `(#:include-dev-deps? ,include-dev-deps?)))))
  301. #:version version
  302. #:guix-name crate-name->package-name))
  303. (define (guix-package->crate-name package)
  304. "Return the crate name of PACKAGE."
  305. (and-let* ((origin (package-source package))
  306. (uri (origin-uri origin))
  307. (crate-url? uri)
  308. (len (string-length crate-url))
  309. (path (xsubstring uri len))
  310. (parts (string-split path #\/)))
  311. (match parts
  312. ((name _ ...) name))))
  313. (define (crate-name->package-name name)
  314. (guix-name "rust-" name))
  315. ;;;
  316. ;;; Updater
  317. ;;;
  318. (define crate-package?
  319. (url-predicate crate-url?))
  320. (define (latest-release package)
  321. "Return an <upstream-source> for the latest release of PACKAGE."
  322. (let* ((crate-name (guix-package->crate-name package))
  323. (crate (lookup-crate crate-name))
  324. (version (crate-latest-version crate))
  325. (url (crate-uri crate-name version)))
  326. (upstream-source
  327. (package (package-name package))
  328. (version version)
  329. (urls (list url)))))
  330. (define %crate-updater
  331. (upstream-updater
  332. (name 'crate)
  333. (description "Updater for crates.io packages")
  334. (pred crate-package?)
  335. (latest latest-release)))