深入理解 Cargo 配置文件与 metadata 的使用

可以通过 config.toml 对 cargo 配置

总体原则是:Cargo 会顺着当前目录往上查找,直到找到目标配置文件。例如我们在目录 /projects/foo/bar/baz 下调用 Cargo 命令,那查找路径如下所示:

  • /projects/foo/bar/baz/.cargo/config.toml
  • /projects/foo/bar/.cargo/config.toml
  • /projects/foo/.cargo/config.toml
  • /projects/.cargo/config.toml
  • /.cargo/config.toml
  • $CARGO_HOME/config.toml 默认是 :
    • Windows: %USERPROFILE%\.cargo\config.toml
    • Unix: $HOME/.cargo/config.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
paths = ["/path/to/override"] # 覆盖 `Cargo.toml` 中通过 path 引入的本地依赖

[alias] # 命令别名
b = "build"
c = "check"
t = "test"
r = "run"
rr = "run --release"
space_example = ["run", "--release", "--", "\"command list\""]

[build]
jobs = 1 # 并行构建任务的数量,默认等于 CPU 的核心数
rustc = "rustc" # rust 编译器
rustc-wrapper = "…" # 使用该 wrapper 来替代 rustc
rustc-workspace-wrapper = "…" # 为工作空间的成员使用 该 wrapper 来替代 rustc
rustdoc = "rustdoc" # 文档生成工具
target = "triple" # 为 target triple 构建 ( `cargo install` 会忽略该选项)
target-dir = "target" # 存放编译输出结果的目录
rustflags = ["…", "…"] # 自定义flags,会传递给所有的编译器命令调用
rustdocflags = ["…", "…"] # 自定义flags,传递给 rustdoc
incremental = true # 是否开启增量编译
dep-info-basedir = "…" # path for the base directory for targets in depfiles
pipelining = true # rustc pipelining

[doc]
browser = "chromium" # `cargo doc --open` 使用的浏览器,
# 可以通过 `BROWSER` 环境变量进行重写

[env]
# Set ENV_VAR_NAME=value for any process run by Cargo
ENV_VAR_NAME = "value"
# Set even if already present in environment
ENV_VAR_NAME_2 = { value = "value", force = true }
# Value is relative to .cargo directory containing `config.toml`, make absolute
ENV_VAR_NAME_3 = { value = "relative/path", relative = true }

[cargo-new]
vcs = "none" # 所使用的 VCS ('git', 'hg', 'pijul', 'fossil', 'none')

[http]
debug = false # HTTP debugging
proxy = "host:port" # HTTP 代理,libcurl 格式
ssl-version = "tlsv1.3" # TLS version to use
ssl-version.max = "tlsv1.3" # 最高支持的 TLS 版本
ssl-version.min = "tlsv1.1" # 最小支持的 TLS 版本
timeout = 30 # HTTP 请求的超时时间,秒
low-speed-limit = 10 # 网络超时阈值 (bytes/sec)
cainfo = "cert.pem" # path to Certificate Authority (CA) bundle
check-revoke = true # check for SSL certificate revocation
multiplexing = true # HTTP/2 multiplexing
user-agent = "…" # the user-agent header

[install]
root = "/some/path" # `cargo install` 安装到的目标目录

[net]
retry = 2 # 网络重试次数
git-fetch-with-cli = true # 是否使用 `git` 命令来执行 git 操作
offline = true # 不能访问网络

[patch.<registry>]
# Same keys as for [patch] in Cargo.toml

[profile.<name>] # profile 配置,详情见"如何在 Cargo.toml 中配置 profile" : https://course.rs/cargo/reference/profiles.html#profile设置
opt-level = 0
debug = true
split-debuginfo = '...'
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 16
rpath = false
[profile.<name>.build-override]
[profile.<name>.package.<name>]

[registries.<name>] # 设置其它的注册服务: https://course.rs/cargo/reference/specify-deps.html#从其它注册服务引入依赖包
index = "…" # 注册服务索引列表的 URL
token = "…" # 连接注册服务所需的鉴权 token

[registry]
default = "…" # 默认的注册服务名称: crates.io
token = "…"

[source.<name>] # 注册服务源和替换source definition and replacement
replace-with = "…" # 使用给定的 source 来替换当前的 source,例如使用科大源来替换crates.io源以提升国内的下载速度:[source.crates-io] replace-with = 'ustc'
directory = "…" # path to a directory source
registry = "…" # 注册源的 URL ,例如科大源: [source.ustc] registry = "git://mirrors.ustc.edu.cn/crates.io-index"
local-registry = "…" # path to a local registry source
git = "…" # URL of a git repository source
branch = "…" # branch name for the git repository
tag = "…" # tag name for the git repository
rev = "…" # revision for the git repository

[target.<triple>]
linker = "…" # linker to use
runner = "…" # wrapper to run executables
rustflags = ["…", "…"] # custom flags for `rustc`

[target.<cfg>]
runner = "…" # wrapper to run executables
rustflags = ["…", "…"] # custom flags for `rustc`

[target.<triple>.<links>] # `links` build script override
rustc-link-lib = ["foo"]
rustc-link-search = ["/path/to/foo"]
rustc-flags = ["-L", "/some/path"]
rustc-cfg = ['key="value"']
rustc-env = {key = "value"}
rustc-cdylib-link-arg = ["…"]
metadata_key1 = "value"
metadata_key2 = "value"

[term]
verbose = false # whether cargo provides verbose output
color = 'auto' # whether cargo colorizes output
progress.when = 'auto' # whether cargo shows progress bar
progress.width = 80 # width of progress bar

cargo.toml 配置项

[package.metadata] 的特点

  1. 专用配置区

    • Cargo 对 metadata 区块没有内置的语义,不会直接处理该部分内容。
    • 其目的是让外部工具、插件或构建脚本提取相关信息并据此操作。
  2. 可扩展性

    • 允许用户根据工具需求自由定义配置,具有很高的灵活性。
  3. 常见用途

    • 为特定工具提供配置。
    • 在构建阶段(如构建脚本 build.rs)读取自定义配置。
    • 传递自定义参数或标志。

[package.metadata.deb] 的用途

在你的例子中,[package.metadata.deb] 很可能是为与 .deb 包相关的工具提供配置,例如:

  • Cargo Debian

    • 一个 Cargo 插件,用于构建 Debian 包(.deb 文件)。
    • 需要在 Cargo.toml 中指定元数据,定义包的名称、依赖项、描述等信息。

    示例:

    1
    2
    3
    4
    5
    [package.metadata.deb]
    name = "my-project"
    maintainer = "Your Name <your.email@example.com>"
    description = "A Rust project packaged as a .deb file"
    depends = ["libssl1.1", "zlib1g"]
    • 字段作用
      • name:在 Debian 系统中显示的包名。
      • maintainer:维护者信息。
      • description:包的描述。
      • depends:声明此包运行时依赖的系统库。

如何读取 metadata 配置

  1. 通过构建脚本读取: 如果你定义了 build.rs 脚本,可以通过 CARGO_MANIFEST_DIRtoml 库读取 metadata

    示例 build.rs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    use std::fs;
    use toml::Value;

    fn main() {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let manifest_path = format!("{}/Cargo.toml", manifest_dir);
    let content = fs::read_to_string(manifest_path).unwrap();
    let value: Value = content.parse().unwrap();

    if let Some(metadata) = value.get("package").and_then(|pkg| pkg.get("metadata")) {
    println!("Metadata: {:?}", metadata);
    }
    }
  2. 外部工具读取: 工具如 cargo deb 会自动解析 [package.metadata.deb] 部分,无需额外操作。


常见工具支持的元数据

  • cargo-deb:
    • 需要 metadata.deb 部分配置 Debian 包信息。
  • cargo-nix:
    • 读取 metadata.nix,为 NixOS 包管理提供配置。
  • 自定义工具:
    • 任意工具或脚本可以利用 metadata 部分存储专用配置。

深入理解 Cargo 配置文件与 metadata 的使用
https://abrance.github.io/2025/04/03/mdstorage/domain/rust/cargo配置项/
Author
xiaoy
Posted on
April 3, 2025
Licensed under