craft_parts.plugins.rust_plugin module

The craft Rust plugin.

class craft_parts.plugins.rust_plugin.RustPlugin(*, properties, part_info)

Bases: Plugin

A 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:
get_build_commands()

Return a list of commands to run during the build step.

Return type:

list[str]

get_build_environment()

Return a dictionary with the environment to use in the build step.

Return type:

dict[str, str]

get_build_packages()

Return a set of required packages to install in the build environment.

Return type:

set[str]

get_build_snaps()

Return a set of required snaps to install in the build environment.

Return type:

set[str]

get_pull_commands()

Return a list of commands to run during the pull step.

Return type:

list[str]

properties_class

alias of RustPluginProperties

validator_class

alias of RustPluginEnvironmentValidator

class craft_parts.plugins.rust_plugin.RustPluginEnvironmentValidator(*, part_name, env, properties)

Bases: PluginEnvironmentValidator

Check 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)

validate_environment(*, part_dependencies=None)

Ensure the environment has the dependencies to build Rust applications.

Parameters:

part_dependencies (list[str] | None) – A list of the parts this part depends on.

Return type:

None

class craft_parts.plugins.rust_plugin.RustPluginProperties(**data)

Bases: PluginProperties

The part properties used by the Rust plugin.

Parameters:

data (Any)

after: Optional[Annotated[list[str]]]
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].

plugin: Literal['rust']
rust_cargo_parameters: list[str]

The extra parameters to pass to Cargo.

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, or nightly

  • A 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, rustup must 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 rustup and defaults to the stable channel. However, if Cargo and the Rust compiler are already available in the build environment, the plugin uses those directly and skips the rustup toolchain selection.

rust_features: Annotated[list[str]]

The build features to use for optional dependencies.

Equivalent to the --features option 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.toml and rust-toolchain files.

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 LDFLAGS environment variable.

This key adds LDFLAGS from the environment to the Rust linker directives.

By default, the Cargo build system and Rust compiler don’t respect the LDFLAGS environment variable. If set to true,the plugin forcibly adds the contents listed by LDFLAGS to the Rust linker directives by wrapping and appending its value to RUSTFLAGS.

Squashing LDFLAGS into RUSTFLAGS makes 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-path option 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.

source: str
classmethod validate_rust_channel(value)

Validate the rust-channel property.

Parameters:

value (str | None) – The value to validate.

Return type:

str | None

Returns:

The validated value.