crate.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. ;;; Copyright © 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  7. ;;; Copyright © 2023 Simon Tournier <zimon.toutoune@gmail.com>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix import crate)
  24. #:use-module (guix base32)
  25. #:use-module (guix build-system cargo)
  26. #:use-module (gcrypt hash)
  27. #:use-module (guix http-client)
  28. #:use-module (guix import json)
  29. #:use-module (guix import utils)
  30. #:use-module (guix memoization)
  31. #:use-module (guix packages)
  32. #:use-module (guix upstream)
  33. #:use-module (guix utils)
  34. #:use-module (gnu packages)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 regex)
  37. #:use-module (json)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-2)
  40. #:use-module (srfi srfi-26)
  41. #:use-module (srfi srfi-71)
  42. #:export (crate->guix-package
  43. guix-package->crate-name
  44. string->license
  45. crate-recursive-import
  46. %crate-updater))
  47. ;;;
  48. ;;; Interface to https://crates.io/api/v1.
  49. ;;;
  50. ;; Crates. A crate is essentially a "package". It can have several
  51. ;; "versions", each of which has its own set of dependencies, license,
  52. ;; etc.--see <crate-version> below.
  53. (define-json-mapping <crate> make-crate crate?
  54. json->crate
  55. (name crate-name) ;string
  56. (latest-version crate-latest-version "max_version") ;string
  57. (home-page crate-home-page "homepage") ;string | #nil
  58. (repository crate-repository) ;string
  59. (description crate-description) ;string
  60. (keywords crate-keywords ;list of strings
  61. "keywords" vector->list)
  62. (categories crate-categories ;list of strings
  63. "categories" vector->list)
  64. (versions crate-versions "actual_versions" ;list of <crate-version>
  65. (lambda (vector)
  66. (map json->crate-version
  67. (vector->list vector))))
  68. (links crate-links)) ;alist
  69. ;; Crate version.
  70. (define-json-mapping <crate-version> make-crate-version crate-version?
  71. json->crate-version
  72. (id crate-version-id) ;integer
  73. (number crate-version-number "num") ;string
  74. (download-path crate-version-download-path "dl_path") ;string
  75. (readme-path crate-version-readme-path "readme_path") ;string
  76. (license crate-version-license "license" ;string | #f
  77. (match-lambda
  78. ('null #f)
  79. ((? string? str) str)))
  80. (links crate-version-links)) ;alist
  81. ;; Crate dependency. Each dependency (each edge in the graph) is annotated as
  82. ;; being a "normal" dependency or a development dependency. There also
  83. ;; information about the minimum required version, such as "^0.0.41".
  84. (define-json-mapping <crate-dependency> make-crate-dependency
  85. crate-dependency?
  86. json->crate-dependency
  87. (id crate-dependency-id "crate_id") ;string
  88. (kind crate-dependency-kind "kind" ;'normal | 'dev | 'build
  89. string->symbol)
  90. (requirement crate-dependency-requirement "req")) ;string
  91. ;; Autoload Guile-Semver so we only have a soft dependency.
  92. (module-autoload! (current-module)
  93. '(semver) '(string->semver semver->string semver<?))
  94. (module-autoload! (current-module)
  95. '(semver ranges) '(string->semver-range semver-range-contains?))
  96. (define (lookup-crate name)
  97. "Look up NAME on https://crates.io and return the corresopnding <crate>
  98. record or #f if it was not found."
  99. (let ((json (json-fetch (string-append (%crate-base-url) "/api/v1/crates/"
  100. name))))
  101. (and=> (and json (assoc-ref json "crate"))
  102. (lambda (alist)
  103. ;; The "versions" field of ALIST is simply a list of version IDs
  104. ;; (integers). Here, we squeeze in the actual version
  105. ;; dictionaries that are not part of ALIST but are just more
  106. ;; convenient handled this way.
  107. (let ((versions (or (assoc-ref json "versions") '#())))
  108. (json->crate `(,@alist
  109. ("actual_versions" . ,versions))))))))
  110. (define lookup-crate* (memoize lookup-crate))
  111. (define (crate-version-dependencies version)
  112. "Return the list of <crate-dependency> records of VERSION, a
  113. <crate-version>."
  114. (let* ((path (assoc-ref (crate-version-links version) "dependencies"))
  115. (url (string-append (%crate-base-url) path)))
  116. (match (assoc-ref (or (json-fetch url) '()) "dependencies")
  117. ((? vector? vector)
  118. (delete-duplicates (map json->crate-dependency (vector->list vector))))
  119. (_
  120. '()))))
  121. ;;;
  122. ;;; Converting crates to Guix packages.
  123. ;;;
  124. (define (maybe-cargo-inputs package-names)
  125. (match (package-names->package-inputs package-names)
  126. (()
  127. '())
  128. ((package-inputs ...)
  129. `(#:cargo-inputs ,package-inputs))))
  130. (define (maybe-cargo-development-inputs package-names)
  131. (match (package-names->package-inputs package-names)
  132. (()
  133. '())
  134. ((package-inputs ...)
  135. `(#:cargo-development-inputs ,package-inputs))))
  136. (define (maybe-arguments arguments)
  137. (match arguments
  138. (()
  139. '())
  140. ((args ...)
  141. `((arguments (,'quasiquote ,args))))))
  142. (define (version->semver-prefix version)
  143. "Return the version up to and including the first non-zero part"
  144. (first
  145. (map match:substring
  146. (list-matches "^(0+\\.){,2}[0-9]+" version))))
  147. (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
  148. home-page synopsis description license build?)
  149. "Return the `package' s-expression for a rust package with the given NAME,
  150. VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
  151. and LICENSE."
  152. (define (format-inputs inputs)
  153. (map
  154. (match-lambda
  155. ((name version)
  156. (list (crate-name->package-name name)
  157. (version->semver-prefix version))))
  158. inputs))
  159. (let* ((port (http-fetch (crate-uri name version)))
  160. (guix-name (crate-name->package-name name))
  161. (cargo-inputs (format-inputs cargo-inputs))
  162. (cargo-development-inputs (format-inputs cargo-development-inputs))
  163. (pkg `(package
  164. (name ,guix-name)
  165. (version ,version)
  166. (source (origin
  167. (method url-fetch)
  168. (uri (crate-uri ,name version))
  169. (file-name (string-append name "-" version ".tar.gz"))
  170. (sha256
  171. (base32
  172. ,(bytevector->nix-base32-string (port-sha256 port))))))
  173. (build-system cargo-build-system)
  174. ,@(maybe-arguments (append (if build?
  175. '()
  176. '(#:skip-build? #t))
  177. (maybe-cargo-inputs cargo-inputs)
  178. (maybe-cargo-development-inputs
  179. cargo-development-inputs)))
  180. (home-page ,home-page)
  181. (synopsis ,synopsis)
  182. (description ,(beautify-description description))
  183. (license ,(match license
  184. (() #f)
  185. (#f #f)
  186. ((license) license)
  187. (_ `(list ,@license)))))))
  188. (close-port port)
  189. (package->definition pkg (version->semver-prefix version))))
  190. (define (string->license string)
  191. (filter-map (lambda (license)
  192. (and (not (string-null? license))
  193. (not (any (lambda (elem) (string=? elem license))
  194. '("AND" "OR" "WITH")))
  195. (or (spdx-string->license license)
  196. 'unknown-license!)))
  197. (string-split string (string->char-set " /"))))
  198. (define* (crate->guix-package crate-name #:key version include-dev-deps?
  199. #:allow-other-keys)
  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. (if (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. (values #f '())))
  294. (define* (crate-recursive-import crate-name #:key version)
  295. (recursive-import crate-name
  296. #:repo->guix-package (lambda* params
  297. ;; download development dependencies only for the top level package
  298. (let ((include-dev-deps? (equal? (car params) crate-name))
  299. (crate->guix-package* (memoize crate->guix-package)))
  300. (apply crate->guix-package*
  301. (append params `(#:include-dev-deps? ,include-dev-deps?)))))
  302. #:version version
  303. #:guix-name crate-name->package-name))
  304. (define (guix-package->crate-name package)
  305. "Return the crate name of PACKAGE."
  306. (and-let* ((origin (package-source package))
  307. (uri (origin-uri origin))
  308. (crate-url? uri)
  309. (len (string-length crate-url))
  310. (path (xsubstring uri len))
  311. (parts (string-split path #\/)))
  312. (match parts
  313. ((name _ ...) name))))
  314. (define (crate-name->package-name name)
  315. (guix-name "rust-" name))
  316. ;;;
  317. ;;; Updater
  318. ;;;
  319. (define crate-package?
  320. (url-predicate crate-url?))
  321. (define* (import-release package #:key (version #f))
  322. "Return an <upstream-source> for the latest release of PACKAGE. Optionally
  323. include a VERSION string to fetch a specific version."
  324. (let* ((crate-name (guix-package->crate-name package))
  325. (crate (lookup-crate crate-name))
  326. (version (or version (crate-latest-version crate)))
  327. (url (crate-uri crate-name version)))
  328. (upstream-source
  329. (package (package-name package))
  330. (version version)
  331. (urls (list url)))))
  332. (define %crate-updater
  333. (upstream-updater
  334. (name 'crate)
  335. (description "Updater for crates.io packages")
  336. (pred crate-package?)
  337. (import import-release)))