openSUSE:Packaging Rust Software
Build Service Tutorial · Tips & Tricks · Cross Distribution Howto · Packaging checks · Desktop menu categories · RPM Macros · Scriptlets · Init scripts · How to write good changes
Maintaining/Creating packages that use Rust code
Dependencies
You should install the following packages to help build rust packages for OpenSUSE.
zypper install obs-service-cargo osc obs-service-tar obs-service-obs_scm \
obs-service-recompress obs-service-set_version obs-service-format_spec_file \
cargo cargo-packaging sudo
Optionally, you can install cargo lock2rpmprovides to help generate the license string.
cargo install cargo-lock2rpmprovides
If you want to cache build artefacts, as of osc-0.173.0 you can use sccache to help reduce repeat build times.
# ~/.config/osc/oscrc sccache = 1 sccache_uri = file:///var/cache/obs_sccache.tar
Alternately you can use a network sccache
# ~/.config/osc/oscrc sccache = 1 sccache_uri = redis://127.0.0.1:6379
Setting up the redis cache is beyond the scope of this page.
Creating the Package
You should checkout your blank package with:
osc co home:<username>/package cd home:<username>/package
A skeleton RPM spec file for a rust package is:
Name: hellorust # This will be set by osc services, that will run after this. Version: 0.0.0 Release: 0 Summary: A hello world with a number of the day printer # If you know the license, put it's SPDX string here. # Alternately, you can use cargo lock2rpmprovides to help generate this. License: Unknown Url: https://github.com/Firstyear/hellorust Source0: %{name}-%{version}.tar.zst Source1: vendor.tar.zst # Pull in the latest rust/cargo toolchain BuildRequires: cargo # This contains rpm macros to assist with building BuildRequires: cargo-packaging # Disable this line if you wish to support all platforms. # In most situations, you will likely only target tier1 arches for user facing components. ExclusiveArch: %{rust_tier1_arches} %description A hello world with a number of the day printer. %prep # The number passed to -a (a stands for "after") should be equivalent to the Source tag number # of the vendor tarball, 1 in this case (from Source1). %autosetup -p1 -a1 # Remove exec bits to prevent an issue in fedora shebang checking. Uncomment only if required. # find vendor -type f -name \*.rs -exec chmod -x '{}' \; %build %{cargo_build} %install # using cargo_install (only supports bindir) %{cargo_install} # manual process, this can place binaries in other directories like sbindin # install -D -d -m 0755 %{buildroot}%{_bindir} # install -m 0755 %{_builddir}/%{name}-%{version}/target/release/%{name} %{buildroot}%{_bindir}/%{name} %check %{cargo_test} %files # %license LICENSE-FILE %{_bindir}/%{name} %changelog
Some projects may need custom rust flags as build arguments, which can be set with the following variable at the head of the spec file.
%define __rustflags --cfg tokio_unstable
But next we will create a service file that allows OBS to help get our sources and bundle them for us. This should go in a file called _service
<services>
<service mode="manual" name="tar_scm">
<param name="url">https://github.com/Firstyear/hellorust.git</param>
<param name="versionformat">@PARENT_TAG@</param>
<param name="scm">git</param>
<param name="revision">v0.1.1</param>
<param name="match-tag">*</param>
<param name="versionrewrite-pattern">v(\d+\.\d+\.\d+)</param>
<param name="versionrewrite-replacement">\1</param>
<param name="changesgenerate">enable</param>
<param name="changesauthor"> YOUR EMAIL HERE </param>
</service>
<service mode="manual" name="recompress">
<param name="file">*.tar</param>
<param name="compression">zst</param>
</service>
<service mode="manual" name="set_version"/>
<service name="cargo_vendor" mode="manual">
<param name="src">hellorust</param>
<param name="compression">zst</param>
<param name="update">true</param>
</service>
</services>
This service file does a lot of the work for us:
- It will fetch the sources from git, based on the version we set.
- It will turn them into a tar.zst for us.
- It will update the changelog for the rpm, and set the correct version in the spec file.
- It will download our rust dependencies, and then bundle them to vendor.tar.zst.
- It scans our project for any known vulnerabilities These come from the RustSec advisory database.
You can run this with:
osc service ra
Optionally, you can now run the lock2rpmprovides:
cd hellorust cargo lock2rpmprovides
This will generate a license string you can copy into the spec file.
You can then add the needed sources and commit to OBS
osc add _service _servicedata cargo_config hellorust-0.1.1~git0.db340ad.tar.zst hellorust.spec vendor.tar.zst osc ci
If you run into any issues while building your package that mention crate dependency conflicts then you may disable the automatic update of dependencies like so:
<param name="update">false</param>
to see if that fixes it. Since crates on git forges will normally only run CI over their older Cargo.lock files rather than the newest semver compatible dependencies.
From here, you can follow the How to contribute to Factory guide.
Projects with Cargo workspaces
Workspaces is a Cargo feature to build multiple projects from the same git tree. You can make the following changes to your spec to build them in the same project:
BuildRequires: cargo-packaging >= 1.2.0
[...]
%build
%{cargo_build} --all
[...]
%install
%{cargo_install -p project1}
%{cargo_install -p project2}
(Optional) Using sccache to reduce rebuild times on OBS
While you may have client side sccache configured, it can also be configured on OBS to reduce server-side rebuild times in home projects and devel projects.
NOTE: This only works with Factory packages - the "new and improved" git workflow has no support for sccache that we are aware of.
In your project config (eg https://build.opensuse.org/projects/devel:languages:rust/prjconf ) add the options
BuildFlags: ccachetype:sccache BuildFlags: useccache:<name of package here>
Unfortunately you need to name each package individually that you want cached. Additionally by setting ccachetype:sccache, you disable ccache on all other projects implicitly and need to name them with useccache. This is a known limitation of OBS.
Updating your Package
Once your package has been accepted, you can then update it with the following steps:
osc bco devel:project/pkgname cd home:username:branches:devel:project/pkgname osc service ra osc status
Then inspect the changes, and osc rm any old files, and osc add any updated source files.
From there you can then do a build and a ci/sr
osc build osc ci osc results osc sr
Supporting Fedora/CentOS
If you plan to support other distros beside Tumbleweed and Leap, you may need to make some changes to your spec files.
Notably, you should avoid using the rust-packaging macros from these distributions if possible.
The macro `cargo_prep` especially is known to break vendoring. Instead you need to manually add:
%if 0%{?fedora}
/usr/bin/mkdir -p target/rpm
/usr/bin/ln -s rpm target/release
cat >> Cargo.toml <<'EOF'
[build]
rustc = "/usr/bin/rustc"
rustdoc = "/usr/bin/rustdoc"
[profile.rpm] inherits = "release" opt-level = 3 codegen-units = 1 debug = 2 strip = "none" EOF %endif
Patching vendored crates
Sometimes it is necessary to patch vendored crates for one reason or another, usually because of CVEs that are not yet in an upstream release.
It is exceedingly rare that you will ever need to do this. You should consider running the osc service and updating the package and vendor tarball when upstream releases the requires fixes.
If you are using the "cargo_vendor" service, you can use this parameter to execute minor version updates of individual dependencies:
<parameter name="update-crate"> <description>Set of specific crates to update. If not empty, it will set the global update flag to false. You can specify a valid version string by adding a `@` after the crate name e.g. `foo@1.2.3`. You can also do recursive updates of a crate by appending `recursive` to `@` e.g. `foo@recursive`. However, recursive can't be used with precise. You can specify a manifest path to update a package with `+` e.g. `foo@1.0+foo/better/Cargo.toml`. See `cargo help update` for info about how to update specific crates.</description> </parameter>
If however, you have to make a more complex update (major version update, applying hand-crafted patches to dependencies, etc.), it can be done in the following way:
mkdir /tmp/source-dir tar -xf hellorust-0.1.1\~0.tar.zst -C /tmp/source-dir tar -xf vendor.tar.zst -C /tmp/source-dir/hellorust-0.1.1\~0/ cd /tmp/source-dir/hellorust-0.1.1\~0/ git init git add ./* git add ./.cargo git commit -a -m 'Initial'
Now you can run optionally an initial
cargo tree | less
to identify which sub(-subsub)package you need to change/bump.
Depending on what you have to do, continue with the corresponding instructions below.
Forcing newer versions of direct dependency manually
Assume, we have a dependency-tree like this:
your_package v1.0 ├── dep_lvl_1 v4.4 ...
and you want to bump dep_lvl_1 to v4.5
Edit your Cargo.toml-file, to make cargo use v4.5 for dep_lvl_1.
Verify with:
cargo tree | less
To download the dependency and vendor it:
cargo vendor --versioned-dirs
Jump to Final steps for all.
Forcing newer versions of sub-dependency
Assume, we have a dependency-tree like this:
your_package v1.0 ├── dep_lvl_1 v4.4 │ ├── dep_lvl_2 v1.3 ...
and you want to change dep_lvl_2 to v1.4. Because of how rust resolves dependencies, we can make dep_lvl_2 a direct dependency of your_package, and with that force a precise version to be used which dep_lvl_1 will inherit.
Edit your Cargo.toml-file, and add the following:
dep_lvl_2 = "=1.4"
Verify with:
cargo tree | less
To download the dependency and vendor it:
cargo vendor --versioned-dirs
Jump to Final steps for all.
Actually modifying dependency-code
Assume, we have a dependency-tree like this:
your_package v1.0 ├── dep_lvl_1 v4.4 │ ├── dep_lvl_2 v1.3 ...
and you want to modify the code of dep_lvl_2 directly.
Move the vendored source into a different location.
mkdir sources mv vendor/dep_lvl_2 sources/
Now modify the content of sources/dep_lvl_2 as required.
Add to the toplevel Cargo.toml-file
[patch.crates-io]
dep_lvl_2 = { path="sources/dep_lvl_2" }
Now regenerate the lockfile
cargo update --offline
Jump to Final steps for all.
Final steps for all
If you ran cargo vendor --versioned-dirs before coming here, check if you have a .cargo/config.toml-file. If not, create the directory and copy the cargo_config-file from your additional sources there.
Verify you have the correct versions for everything with
cargo tree | less
Now create a new commit and the patch file
git commit -a -m 'Modifications' git format-patch HEAD~1
This patch can then be included into your spec file with:
Url: https://github.com/Firstyear/hellorust Source0: %{name}-%{version}.tar.zst Source1: vendor.tar.zst Patch1: 0001-update.patch ... %BuildRequires git ... %prep %autosetup -Sgit -p1 -a1
References
- openSUSE:Package_dependencies Advanced Package Dependencies
- obs-service-cargo_vendor
- obs-service-cargo_audit