[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug#77093] [PATCH rust-team v3 14/17] build-system: cargo: Add ‘cargo-i
From: |
Hilton Chain |
Subject: |
[bug#77093] [PATCH rust-team v3 14/17] build-system: cargo: Add ‘cargo-inputs’. |
Date: |
Sun, 23 Mar 2025 15:28:36 +0800 |
* gnu/packages/rust-crates.scm: New file.
* gnu/packages/rust-sources.scm: New file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Regisiter them.
* guix/build-system/cargo.scm (crate-source,cargo-inputs): New procedures.
* guix/import/crate.scm: Hide ‘crate-source’ from (guix build-system cargo).
* etc/teams/rust/audit-rust-crates: New file.
* etc/teams/rust/cleanup-crates.sh: New file.
* etc/teams/rust/rust-crates.tmpl: New file.
Change-Id: I2f2d705a3e376ed3c646f31b824052a2278d4fb3
---
etc/teams/rust/audit-rust-crates | 70 ++++++++++++++++++++++++++++++++
etc/teams/rust/cleanup-crates.sh | 37 +++++++++++++++++
etc/teams/rust/rust-crates.tmpl | 42 +++++++++++++++++++
gnu/local.mk | 2 +
gnu/packages/rust-crates.scm | 42 +++++++++++++++++++
gnu/packages/rust-sources.scm | 29 +++++++++++++
guix/build-system/cargo.scm | 47 ++++++++++++++++++++-
guix/import/crate.scm | 2 +-
8 files changed, 269 insertions(+), 2 deletions(-)
create mode 100755 etc/teams/rust/audit-rust-crates
create mode 100755 etc/teams/rust/cleanup-crates.sh
create mode 100644 etc/teams/rust/rust-crates.tmpl
create mode 100644 gnu/packages/rust-crates.scm
create mode 100644 gnu/packages/rust-sources.scm
diff --git a/etc/teams/rust/audit-rust-crates b/etc/teams/rust/audit-rust-crates
new file mode 100755
index 0000000000..d5546fd1e1
--- /dev/null
+++ b/etc/teams/rust/audit-rust-crates
@@ -0,0 +1,70 @@
+#!/usr/bin/env -S gawk -f
+# GNU Guix --- Functional package management for GNU
+# Copyright © 2025 Efraim Flashner <efraim@flashner.co.il>
+#
+# This file is part of GNU Guix.
+#
+# GNU Guix is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# GNU Guix is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+# To run:
+# ./etc/teams/rust/audit-rust-crates ./path/to/file.scm
+# Prints the output of cargo-audit to the shell.
+
+# Make sure we have cargo-audit in our PATH
+BEGIN {
+ if (system("which cargo-audit 1> /dev/null"))
+ exit 1;
+ # Parse a record at a time.
+ RS = "\n\n"
+ cargoAudit = "cargo-audit audit --file -"
+}
+
+# Check the crate-source origin-only inputs
+/crate-source/ {
+ for(i=3; i <= NF-2; i++) {
+ if($i == "(crate-source") {
+ cargoLock = cargoLock "[[package]]\nname = " $(i+1) "\nversion = "
$(i+2) "\n"
+ next
+ }
+ }
+}
+
+# Check the crates packaged from crates.io tarballs
+/crate-uri/ {
+ for(i=3; i <= NF; i++) {
+ if($i == "(version")
+ crateVersion = $(i+1)
+ if($i == "(crate-uri")
+ crateName = $(i+1)
+ }
+ gsub(/)/, "", crateVersion)
+ cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = "
crateVersion "\n"
+}
+
+# The xxxx-cargo-input variables have a set style
+# TODO: Replace the last dash between the name and the version with a space!
+# This doesn't take into account swapping between "-" and "_" so we skip it.
+#( $2 ~ /-cargo-inputs/ ) {
+# sub(/-cargo-inputs/, "", $2)
+# gsub(/)/, "", $0)
+# gsub(/rust-/, "", $0)
+# #gensub(/([[:alpha:]])-([[:digit:]]+)/, "\\1 \\2", "g", $i)
+# print "[[package]]\nname = \"" $2 "\"\nversion = \"1.0.0\"\ndependencies
= ["
+# for (i = 4; i <= NF; i++) {
+# print "\"" $i "\","
+# }
+# print "]"
+#}
+
+END { print cargoLock | cargoAudit }
diff --git a/etc/teams/rust/cleanup-crates.sh b/etc/teams/rust/cleanup-crates.sh
new file mode 100755
index 0000000000..eca37ca00c
--- /dev/null
+++ b/etc/teams/rust/cleanup-crates.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# GNU Guix --- Functional package management for GNU
+# Copyright © 2025 Hilton Chain <hako@ultrarare.space>
+#
+# This file is part of GNU Guix.
+#
+# GNU Guix is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# GNU Guix is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+FILE=gnu/packages/rust-crates.scm
+PATTERN='^(define rust-'
+
+grep "$PATTERN" $FILE | cut -d' ' -f2 | while IFS= read -r crate
+do
+ if [ "$(grep -wc "$crate" $FILE)" -eq 1 ]; then
+ echo "\
+(begin
+ (use-modules (guix utils))
+ (let ((source-properties
+ (find-definition-location \"$FILE\" '$crate #:define-prefix 'define)))
+ (and=> source-properties delete-expression)))" |
+ guix repl -t machine
+ fi
+done
+
+# Delete extra newlines.
+sed --in-place ':a;N;$!ba;s/\n\n\+/\n\n/g' $FILE
diff --git a/etc/teams/rust/rust-crates.tmpl b/etc/teams/rust/rust-crates.tmpl
new file mode 100644
index 0000000000..98053b7151
--- /dev/null
+++ b/etc/teams/rust/rust-crates.tmpl
@@ -0,0 +1,42 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2025 Hilton Chain <hako@ultrarare.space>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu packages rust-crates)
+ #:use-module (guix gexp)
+ #:use-module (guix packages)
+ #:use-module (guix download)
+ #:use-module (guix git-download)
+ #:use-module (guix build-system cargo))
+
+;;;
+;;; This file is managed by ‘guix import’. DO NOT add definitions manually.
+;;;
+
+;;;
+;;; Rust dependencies fetched from crates.io and non-workspace development
+;;; snapshots.
+;;;
+
+(define qqqq-separator 'begin-of-crates)
+
+(define ssss-separator 'end-of-crates)
+
+
+;;;
+;;; Cargo inputs.
+;;;
diff --git a/gnu/local.mk b/gnu/local.mk
index 02de02e65f..8d8bd6ebbe 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -616,6 +616,8 @@ GNU_SYSTEM_MODULES = \
%D%/packages/rush.scm \
%D%/packages/rust.scm \
%D%/packages/rust-apps.scm \
+ %D%/packages/rust-crates.scm \
+ %D%/packages/rust-sources.scm \
%D%/packages/samba.scm \
%D%/packages/sagemath.scm \
%D%/packages/sawfish.scm \
diff --git a/gnu/packages/rust-crates.scm b/gnu/packages/rust-crates.scm
new file mode 100644
index 0000000000..98053b7151
--- /dev/null
+++ b/gnu/packages/rust-crates.scm
@@ -0,0 +1,42 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2025 Hilton Chain <hako@ultrarare.space>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu packages rust-crates)
+ #:use-module (guix gexp)
+ #:use-module (guix packages)
+ #:use-module (guix download)
+ #:use-module (guix git-download)
+ #:use-module (guix build-system cargo))
+
+;;;
+;;; This file is managed by ‘guix import’. DO NOT add definitions manually.
+;;;
+
+;;;
+;;; Rust dependencies fetched from crates.io and non-workspace development
+;;; snapshots.
+;;;
+
+(define qqqq-separator 'begin-of-crates)
+
+(define ssss-separator 'end-of-crates)
+
+
+;;;
+;;; Cargo inputs.
+;;;
diff --git a/gnu/packages/rust-sources.scm b/gnu/packages/rust-sources.scm
new file mode 100644
index 0000000000..bf9b91a671
--- /dev/null
+++ b/gnu/packages/rust-sources.scm
@@ -0,0 +1,29 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2025 Hilton Chain <hako@ultrarare.space>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu packages rust-sources)
+ #:use-module (guix gexp)
+ #:use-module (guix packages)
+ #:use-module (guix download)
+ #:use-module (guix git-download)
+ #:use-module (guix build-system cargo))
+
+;;;
+;;; Cargo workspaces and Rust dependencies requiring external inputs to
+;;; unbundle.
+;;;
diff --git a/guix/build-system/cargo.scm b/guix/build-system/cargo.scm
index 7a07003262..bbfa2f933b 100644
--- a/guix/build-system/cargo.scm
+++ b/guix/build-system/cargo.scm
@@ -31,6 +31,7 @@ (define-module (guix build-system cargo)
#:use-module (guix gexp)
#:use-module (guix monads)
#:use-module (guix packages)
+ #:use-module (guix download)
#:use-module (guix platform)
#:use-module (guix build-system)
#:use-module (guix build-system gnu)
@@ -45,7 +46,9 @@ (define-module (guix build-system cargo)
crate-url
crate-url?
crate-uri
- crate-name->package-name))
+ crate-name->package-name
+ crate-source
+ cargo-inputs))
(define %crate-base-url
(make-parameter "https://crates.io"))
@@ -62,6 +65,48 @@ (define (crate-uri name version)
(define (crate-name->package-name name)
(downstream-package-name "rust-" name))
+(define* (crate-source name version hash #:key (patches '()) (snippet #f))
+ (origin
+ (method url-fetch)
+ (uri (crate-uri name version))
+ (file-name
+ (string-append (crate-name->package-name name) "-" version ".tar.gz"))
+ (sha256 (base32 hash))
+ (modules '((guix build utils)))
+ (patches patches)
+ (snippet snippet)))
+
+(define* (cargo-inputs name #:key (crates-module '(gnu packages rust-crates))
+ (sources-module '(gnu packages rust-sources)))
+
+ "Given symbol NAME, resolve variable 'NAME-cargo-inputs', an input list, in
+CRATES-MODULE, return its copy with #f removed and symbols resolved to
+variables defined in SOURCES-MODULE if the input list exists, otherwise return
+an empty list."
+ (let loop ((inputs
+ (catch #t
+ (lambda ()
+ (module-ref (resolve-interface crates-module)
+ (symbol-append name '-cargo-inputs)))
+ (const '())))
+ (result '()))
+ (if (null? inputs)
+ result
+ (match inputs
+ ((input . rest)
+ (loop rest
+ (cond
+ ;; #f, remove it.
+ ((not input)
+ result)
+ ;; Symbol, resolve it in SOURCES-MODULE.
+ ((symbol? input)
+ (cons (module-ref (resolve-interface sources-module) input)
+ result))
+ ;; Else: keep it.
+ (else
+ (cons input result)))))))))
+
(define (default-rust target)
"Return the default Rust package."
;; Lazily resolve the binding to avoid a circular dependency.
diff --git a/guix/import/crate.scm b/guix/import/crate.scm
index 14e6e28c5b..a6f247bbae 100644
--- a/guix/import/crate.scm
+++ b/guix/import/crate.scm
@@ -27,7 +27,7 @@
(define-module (guix import crate)
#:use-module (guix base32)
- #:use-module (guix build-system cargo)
+ #:use-module ((guix build-system cargo) #:hide (crate-source))
#:use-module (guix diagnostics)
#:use-module (gcrypt hash)
#:use-module (guix http-client)
--
2.49.0
- [bug#77093] [PATCH rust-team v3 04/17] build-system: cargo: Support installing Cargo workspace., (continued)
- [bug#77093] [PATCH rust-team v3 04/17] build-system: cargo: Support installing Cargo workspace., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 03/17] build-system: cargo: Support packaging Cargo workspace., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 05/17] build/cargo: Set default value of arguments for build phases., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 06/17] build/cargo: Don't try to unpack sanity-check.py., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 07/17] build/cargo: Support non-workspace directory source inputs., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 09/17] scripts: import: Add two newlines for ‘--insert’ option., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 12/17] scripts: import: Skip existing definition for ‘--insert’ option., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 15/17] import: crate: Add Cargo.lock parser., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 08/17] scripts: import: Document argument for ‘--insert’ option in help message., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 11/17] scripts: import: Pass "--insert" to importers., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 14/17] build-system: cargo: Add ‘cargo-inputs’.,
Hilton Chain <=
- [bug#77093] [PATCH rust-team v3 16/17] import: crate: Add ‘--lockfile’ option., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 10/17] scripts: import: Support expressions defined by 'define., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 13/17] import: crate: crate-name->package-name: Move to (guix build-system cargo)., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team v3 17/17] doc: Document lockfile importer based Rust packaging workflow., Hilton Chain, 2025/03/23
- [bug#77093] [PATCH rust-team 00/18] New Rust packaging workflow based on lockfile importer., Ludovic Courtès, 2025/03/25