Installing data files (assets) when 'cargo install' is run

I've learned that any assets required by a binary project can be installed to a specified directory using a customized build.rs file.

I'd like to hear about various other alternatives as well, that the Rust community uses for this case.

I found this ticket and subscribed to it:
Add an install.rs that lets a file perform installation setup, as well as a permanent installation outdir

I'd basically like to install data to a directory relative to the binary, like if the default installation location is $HOME/.cargo/bin, I'd want data to go to $HOME/.cargo/usr/share... (depending on the type of bin project I'm installing).

But also, I'd want my bin to be able to find the data prior to install, and couldn't decide yet on the best way to do that. Maybe defining a constant at compile-time with the hard-coded directory paths? A constant that could be changed when I run cargo install?

1 Like

cargo install is only able to uninstall the single executable it has installed. If you create anything else yourself, it will leave untracked files on user's system.

build.rs is not supposed to write anything outside of OUT_DIR. This might get enforced eventually if Cargo gets build sandboxing.

So I suggest using a different distribution method. You can easily build a .deb package with cargo deb. You can submit your project to Homebrew. You can distribute a zip or tarball with all the files you need.

Alternatively, you could try making a self-contained executable. There's include_bytes! and rust-embed.

5 Likes

Wow, thanks for the heads up on cargo deb. Something I did not know I needed until now which will prove very useful.

1 Like

Thanks @kornel. What do you think about using Cargo custom commands to execute a custom install/uninstall script?

As for distribution methods, some people might wanna check out CDE and AppImage.

Edit: I corrected the irrelevant link to Command Desktop Environment.

Custom commands can't cooperate with cargo uninstall, so you'd have to explain to users to use your custom uninstaller instead of the standard command. Also watch out for naming conflicts, because Cargo puts all binaries into the same folder.

1 Like

This sort of thing will (in my experience) become unmanageable very quickly. Even the simplest cargo install falls over if you've got a binary name collision... so, I strongly suggest you not consider cargo install (or any other custom script) to be a primary means of distribution.

In service of developing and trying out rust-based tools, it does a pretty good job, but for anything more I believe you should look into building native packages for your OS.

I've personally had good luck with cargo-rpm for RPM-based distributions. I haven't tried it, but cargo-bundle attempts to generalize to multiple different operating system package formats. If you're targeting Linux, you could give AppImage or Flatpak a try.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.