I should of written an update for quite some time. While I’ve been experimenting with marketing analytics, learning about data science, business development, doing DevOps with GitLab CI and various other things, I wanted to write up my learning when I had a chance to internalize everything. However what made me decide to write an update is this tweet from Chris Krycho. Chris runs the amazing New Rustacean podcast, which is a must listen for anyone interested in learning about programming in Rust.
How does one package a Rust app?
Chris asked about finding a good way to distribute Rust binaries across Linux distros:
Hey @rustlang Twitter—let’s say I wanted to pre-build binaries for distributing a tool (via package managers, for example), and I don’t have easy access to a RedHat environment but want to support it. What’s the story here for cross-compiling from e.g. Ubuntu?
— Chris Krycho (@chriskrycho) March 14, 2019
Interestingly enough, I recently figured out how to package a small Rust CLI utility that I’m working on. My response was:
I would check out creating a snap package (https://t.co/UYibrbPN7c). It worked alright for me, even though the format encourages sandboxing of apps which takes a bit to wrap your mind a bit. Or create a deb using cargo-deb and use the alien utility to transform that to a rpm.
— Dorian Pula (@dorianpula) March 14, 2019
This post elaborates on what I meant with my reply.
Building snap packages using snapcraft
The fine folks at Canonical (the makers of Ubuntu Linux), have created something called snap packages. These packages and associated package manager help developers distribute applications (desktop, cloud, etc.) in a safe, isolated manner. I currently have slack installed this way. snaps isolate apps by having the package maintainer declare the capabilities an app requires (network access, access to system files, the GPU, home directory, etc.), and then ensuring the apps can not escape this sandbox.
Getting setup with packaging a Rust crate was not too hard:
- Install snapcraft:
sudo snap install snapcraft --classic
- Create a snap template inside your crate project:
- Edit the generated
snap/snapcraft.ymlas per the documentation.
- Build the snap using
- Install the resulting snap with
sudo snap install my-cool-rust-bin_x.y.z.snap --devmode --dangerous(this assuming you are experimenting with building a snap)
- Add you should be able to distribute your app as a snap now. (See the caveats below.)
Caveats working with snaps
Now there a bunch of caveats when working with snaps. And for my own Rust utility, I found these too taxing and I decided to go with creating a standard Debian package instead. However if I wanted to target multiple distributions and my app didn’t have a very unorthodox setup (my app relies on using the Chrome WebDriver to control a networked device managed by dd-wrt), I would probably have gone with a snap instead:
- You need to know what capabilities your app needs: file access, network, etc. and you need to declare the appropriate interfaces in your snapcraft.yml
- Using something other than my local system (be it a Docker based build or using a different base like base18), failed terribly at least for Rust.
- Whether or not I’d have more success if my base system was the recommended Ubuntu 16.04 and not 18.04 is an outstanding mystery.
- The snap confinement, even on the much more liberal
devmode, works very well. No amount of coaxing on my part, let me use system paths when trying to spawn a process. This could just be me though, as not declaring network access did not block my app.
- The docs could of been clearer about what was the latest recommended approach. (Still way clearer than the documentation for creating a DEB or RPM from scratch.)
- Knowing which libraries (for the type of base system) your app needs takes a bit of experimentation. (e.g. I needed libssl1.0 for some builds and libssl1.1)
- I have no idea how the
classicalconfinement should work, and it is not recommended either way.
The end result for me was a working snap package, but an app that would not work when called from an installed snap. However I think snap packages are probably the way to go moving forward (or a similar format like flatpak). Since I only plan on targeting local Ubuntu 18.04 installs, I ended up creating a Debian package instead.
Building a Debian package with Cargo
I found a nice utility for creating a deb out of a Rust crate, called
cargo-deb. After installing the crate with cargo:
cargo install cargo-deb, I simply ran
cargo deb and I was done.
cargo-deb looked into my Cargo.toml for the metadata, ran a build and a few moments later I was the proud owner of a Debian package. Since my app relies on the
chromium-chromedriver packages, I added a small section in my Cargo.toml as so:
depends = "$auto, chromium-browser, chromium-chromedriver"
$auto is something that the Debian packaging mechanism needs, and that is the comma separated format that DEBs use.
Building a RPM from a DEB package
Now this the part that I didn’t do this time around. However I figured out how to create RPM packages from DEB packages a few months ago. The trick is to use the
alien utility to create a RPM out of a DEB:
sudo alien --to-rpm --scripts --verbose my-cool-rust-bin.deb
For the record, I did not try to improve or debug the resulting RPM. (This entire effort was for a product that failed to launch.) However as part of my tests I was able install it and run it from on CentOS VM.
Anyways, I would recommend the
alien approach, if you are not planning to distribute a Rust app across a multitude of Linux distros. I would recommend dipping into
snap if you plan on distributing something more commercial and wide-spread like a slack or kubectl. And I hope that helps you on your Rust app packaging for Linux journey!