craft_parts.plugins.rust_plugin module¶
The craft Rust plugin.
- class craft_parts.plugins.rust_plugin.RustPlugin(*, properties, part_info)¶
Bases:
PluginA craft plugin for Rust applications.
This Rust plugin is useful for building Rust based parts.
Rust uses cargo to drive the build.
This plugin uses the common plugin keys as well as those for “sources”. For more information check the ‘plugins’ topic for the former and the ‘sources’ topic for the latter.
- Additionally, this plugin uses the following plugin-specific keys:
rust-channel (string, default “stable”) Used to select which Rust channel or version to use. It can be one of “stable”, “beta”, “nightly” or a version number. If you don’t want this plugin to install Rust toolchain for you, you can put “none” for this option.
rust-ignore-toolchain-file (boolean, default False) Whether to ignore the rust-toolchain.toml file. The upstream project can use this file to specify which Rust toolchain to use and which component to install. If you don’t want to follow the upstream project’s specifications, you can put true for this option to ignore the toolchain file.
rust-features (list of strings) Features used to build optional dependencies. You can specify [“*”] for all features (including all the optional ones).
rust-path (list of strings, default [.]) Build specific crates inside the workspace
rust-no-default-features (boolean, default False) Whether to disable the default features in this crate. Equivalent to setting –no-default-features on the commandline.
rust-use-global-lto (boolean, default False) Whether to use global LTO. This option may significantly impact the build performance but reducing the final binary size. This will forcibly enable LTO for all the crates you specified, regardless of whether you have LTO enabled in the Cargo.toml file
rust-cargo-parameters (list of strings) Append additional parameters to the Cargo command line.
rust-inherit-ldflags (boolean, default False) Whether to inherit the LDFLAGS from the environment. This option will add the LDFLAGS from the environment to the Rust linker directives.
- Parameters:
properties (
PluginProperties)part_info (
PartInfo)
- get_build_commands()¶
Return a list of commands to run during the build step.
- get_build_environment()¶
Return a dictionary with the environment to use in the build step.
- get_build_packages()¶
Return a set of required packages to install in the build environment.
- get_build_snaps()¶
Return a set of required snaps to install in the build environment.
- properties_class¶
alias of
RustPluginProperties
- validator_class¶
alias of
RustPluginEnvironmentValidator
- class craft_parts.plugins.rust_plugin.RustPluginEnvironmentValidator(*, part_name, env, properties)¶
Bases:
PluginEnvironmentValidatorCheck the execution environment for the Rust plugin.
- Parameters:
part_name (
str) – The part whose build environment is being validated.env (
str) – A string containing the build step environment setup.properties (
PluginProperties)
- class craft_parts.plugins.rust_plugin.RustPluginProperties(**data)¶
Bases:
PluginPropertiesThe part properties used by the Rust plugin.
- Parameters:
data (
Any)
- model_config: ClassVar[ConfigDict] = {'alias_generator': <function PluginProperties.<lambda>>, 'extra': 'forbid', 'frozen': True, 'validate_assignment': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
-
rust_channel:
str|None¶ The Rust channel or version to use.
Accepted values:
A version number, such as
"1.99"A channel name, either
stable,beta, ornightlyA nightly version number, in the format
nightly-YYYY-MM-DD"none"to stop the plugin from installing the Rust toolchain
If this key is set to a channel or version,
rustupmust also be available in the build environment so the plugin can install or select the requested toolchain.If this key is left unset, the plugin uses
rustupand defaults to thestablechannel. However, if Cargo and the Rust compiler are already available in the build environment, the plugin uses those directly and skips therustuptoolchain selection.
-
rust_features:
Annotated[list[str]]¶ The build features to use for optional dependencies.
Equivalent to the
--featuresoption in Cargo. Set to["*"]to select all the features available in the project.This option doesn’t override any default features specified by the project itself.
To override the default features, see the rust-no-default-features key.
-
rust_ignore_toolchain_file:
bool¶ Whether to ignore the
rust-toolchain.tomlandrust-toolchainfiles.The part source may use this file to specify which Rust toolchain to use and which component to install. To bypass the upstream project’s specifications, set to
true.
-
rust_inherit_ldflags:
bool¶ Whether to inherit the
LDFLAGSenvironment variable.This key adds
LDFLAGSfrom the environment to the Rust linker directives.By default, the Cargo build system and Rust compiler don’t respect the
LDFLAGSenvironment variable. If set totrue,the plugin forcibly adds the contents listed byLDFLAGSto the Rust linker directives by wrapping and appending its value toRUSTFLAGS.Squashing
LDFLAGSintoRUSTFLAGSmakes the Rust binary in a classically-confined snap respect the snap linkage, so that the binary won’t find the libraries in the host filesystem. Here’s an example part that does this:parts: my-classic-app: plugin: rust source: . rust-inherit-ldflags: true build-environment: - LDFLAGS: > -Wl,-rpath=\$ORIGIN/lib:/snap/core24/current/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR -Wl,-dynamic-linker=$(find /snap/core24/current/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR -name 'ld*.so.*' -print | head -n1)
-
rust_no_default_features:
bool¶ Whether to ignore the features specified by the project.
If set, the rust-features key can specify an exhaustive list of features to override.
-
rust_path:
Annotated[list[str]]¶ The path to the directory containing the manifest.
Equivalent to the
--manifest-pathoption in Cargo.Defaults to
".".
-
rust_use_global_lto:
bool¶ Whether to use global LTO.
This option may significantly impact the build performance but reduce the final binary size and improve runtime performance. This will forcibly enable LTO for all the crates you specified, regardless of whether the projects have the LTO option enabled in the manifest.
This is equivalent to declaring
lto = "fat"in the manifest.To further improve runtime performance, see Performance tuning.