1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 David Craven <david@craven.ch>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix import crate)
20 #:use-module (guix base32)
21 #:use-module (guix build-system cargo)
22 #:use-module ((guix download) #:prefix download:)
23 #:use-module (gcrypt hash)
24 #:use-module (guix http-client)
25 #:use-module (guix import json)
26 #:use-module (guix import utils)
27 #:use-module ((guix licenses) #:prefix license:)
28 #:use-module (guix monads)
29 #:use-module (guix packages)
30 #:use-module (guix upstream)
31 #:use-module (guix utils)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 pretty-print) ; recursive
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-2)
37 #:use-module (srfi srfi-26)
38 #:export (crate->guix-package
39 guix-package->crate-name
42 (define (crate-fetch crate-name callback)
43 "Fetch the metadata for CRATE-NAME from crates.io and call the callback."
45 (define (crates->inputs crates)
46 (sort (map (cut assoc-ref <> "crate_id") crates) string-ci<?))
48 (define (string->license string)
49 (map spdx-string->license (string-split string #\/)))
51 (define (crate-kind-predicate kind)
52 (lambda (dep) (string=? (assoc-ref dep "kind") kind)))
54 (and-let* ((crate-json (json-fetch (string-append crate-url crate-name)))
55 (crate (assoc-ref crate-json "crate"))
56 (name (assoc-ref crate "name"))
57 (version (assoc-ref crate "max_version"))
58 (homepage (assoc-ref crate "homepage"))
59 (repository (assoc-ref crate "repository"))
60 (synopsis (assoc-ref crate "description"))
61 (description (assoc-ref crate "description"))
62 (license (or (and=> (assoc-ref crate "license")
64 '())) ;missing license info
65 (path (string-append "/" version "/dependencies"))
66 (deps-json (json-fetch (string-append crate-url name path)))
67 (deps (vector->list (assoc-ref deps-json "dependencies")))
68 (dep-crates (filter (crate-kind-predicate "normal") deps))
71 (not ((crate-kind-predicate "normal") dep))) deps))
72 (cargo-inputs (crates->inputs dep-crates))
73 (cargo-development-inputs (crates->inputs dev-dep-crates))
74 (home-page (match homepage
77 (callback #:name name #:version version
78 #:cargo-inputs cargo-inputs
79 #:cargo-development-inputs cargo-development-inputs
80 #:home-page home-page #:synopsis synopsis
81 #:description description #:license license)))
83 (define (maybe-cargo-inputs package-names)
84 (match (package-names->package-inputs package-names)
88 `(#:cargo-inputs ,package-inputs))))
90 (define (maybe-cargo-development-inputs package-names)
91 (match (package-names->package-inputs package-names)
95 `(#:cargo-development-inputs ,package-inputs))))
97 (define (maybe-arguments arguments)
102 `((arguments (,'quasiquote ,args))))))
104 (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
105 home-page synopsis description license
107 "Return the `package' s-expression for a rust package with the given NAME,
108 VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
110 (let* ((port (http-fetch (crate-uri name version)))
111 (guix-name (crate-name->package-name name))
112 (cargo-inputs (map crate-name->package-name cargo-inputs))
113 (cargo-development-inputs (map crate-name->package-name
114 cargo-development-inputs))
120 (uri (crate-uri ,name version))
121 (file-name (string-append name "-" version ".tar.gz"))
124 ,(bytevector->nix-base32-string (port-sha256 port))))))
125 (build-system cargo-build-system)
126 ,@(maybe-arguments (append (maybe-cargo-inputs cargo-inputs)
127 (maybe-cargo-development-inputs
128 cargo-development-inputs)))
129 (home-page ,(match home-page
133 (description ,(beautify-description description))
134 (license ,(match license
137 (_ `(list ,@license)))))))
141 (define (crate->guix-package crate-name)
142 "Fetch the metadata for CRATE-NAME from crates.io, and return the
143 `package' s-expression corresponding to that package, or #f on failure."
144 (crate-fetch crate-name make-crate-sexp))
146 (define (guix-package->crate-name package)
147 "Return the crate name of PACKAGE."
148 (and-let* ((origin (package-source package))
149 (uri (origin-uri origin))
151 (len (string-length crate-url))
152 (path (xsubstring uri len))
153 (parts (string-split path #\/)))
155 ((name _ ...) name))))
157 (define (crate-name->package-name name)
158 (string-append "rust-" (string-join (string-split name #\_) "-")))
164 (define (crate-package? package)
165 "Return true if PACKAGE is a Rust crate from crates.io."
166 (let ((source-url (and=> (package-source package) origin-uri))
167 (fetch-method (and=> (package-source package) origin-method)))
168 (and (eq? fetch-method download:url-fetch)
171 (crate-url? source-url))
173 (any crate-url? source-url))))))
175 (define (latest-release package)
176 "Return an <upstream-source> for the latest release of PACKAGE."
177 (let* ((crate-name (guix-package->crate-name package))
178 (callback (lambda* (#:key version #:allow-other-keys) version))
179 (version (crate-fetch crate-name callback))
180 (url (crate-uri crate-name version)))
182 (package (package-name package))
186 (define %crate-updater
189 (description "Updater for crates.io packages")
190 (pred crate-package?)
191 (latest latest-release)))