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 (guix 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 (string->license (assoc-ref crate "license")))
63 (path (string-append "/" version "/dependencies"))
64 (deps-json (json-fetch (string-append crate-url name path)))
65 (deps (assoc-ref deps-json "dependencies"))
66 (input-crates (filter (crate-kind-predicate "normal") deps))
69 (not ((crate-kind-predicate "normal") dep))) deps))
70 (inputs (crates->inputs input-crates))
71 (native-inputs (crates->inputs native-input-crates))
72 (home-page (match homepage
75 (callback #:name name #:version version
76 #:inputs inputs #:native-inputs native-inputs
77 #:home-page home-page #:synopsis synopsis
78 #:description description #:license license)))
80 (define* (make-crate-sexp #:key name version inputs native-inputs
81 home-page synopsis description license
83 "Return the `package' s-expression for a rust package with the given NAME,
84 VERSION, INPUTS, NATIVE-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
85 (let* ((port (http-fetch (crate-uri name version)))
86 (guix-name (crate-name->package-name name))
87 (inputs (map crate-name->package-name inputs))
88 (native-inputs (map crate-name->package-name native-inputs))
94 (uri (crate-uri ,name version))
95 (file-name (string-append name "-" version ".tar.gz"))
98 ,(bytevector->nix-base32-string (port-sha256 port))))))
99 (build-system cargo-build-system)
100 ,@(maybe-native-inputs native-inputs "src")
101 ,@(maybe-inputs inputs "src")
102 (home-page ,(match home-page
106 (description ,(beautify-description description))
107 (license ,(match license
110 (_ `(list ,@license)))))))
114 (define (crate->guix-package crate-name)
115 "Fetch the metadata for CRATE-NAME from crates.io, and return the
116 `package' s-expression corresponding to that package, or #f on failure."
117 (crate-fetch crate-name make-crate-sexp))
119 (define (guix-package->crate-name package)
120 "Return the crate name of PACKAGE."
121 (and-let* ((origin (package-source package))
122 (uri (origin-uri origin))
124 (len (string-length crate-url))
125 (path (xsubstring uri len))
126 (parts (string-split path #\/)))
128 ((name _ ...) name))))
130 (define (crate-name->package-name name)
131 (string-append "rust-" (string-join (string-split name #\_) "-")))
137 (define (crate-package? package)
138 "Return true if PACKAGE is a Rust crate from crates.io."
139 (let ((source-url (and=> (package-source package) origin-uri))
140 (fetch-method (and=> (package-source package) origin-method)))
141 (and (eq? fetch-method download:url-fetch)
144 (crate-url? source-url))
146 (any crate-url? source-url))))))
148 (define (latest-release package)
149 "Return an <upstream-source> for the latest release of PACKAGE."
150 (let* ((crate-name (guix-package->crate-name package))
151 (callback (lambda* (#:key version #:allow-other-keys) version))
152 (version (crate-fetch crate-name callback))
153 (url (crate-uri crate-name version)))
155 (package (package-name package))
159 (define %crate-updater
162 (description "Updater for crates.io packages")
163 (pred crate-package?)
164 (latest latest-release)))