Shipping static files with a Rust proect to crates.io

I have a Rust project, that requires several static files, including a complete directory.
The project is meant to be released on crates.io to make it easier to install. How can I add the static files, so that my program can access them in one directory?
Thanks in advance :slight_smile:

You can use the package.include field to add files and directories to the published package:

https://doc.rust-lang.org/cargo/reference/manifest.html#the-exclude-and-include-fields

I tried that, but I did not work. Maybe I also misunderstand the feature, but I need the files to be in the same (or any other known) directory, som my program can access them.

The files must be somewhere in the package directory or a subdirectory. Aren't the files shown when you run cargo package --list?

1 Like

Here are the Cargo.toml and the output of cargo package --list:

[package]
name = "zentrox"
version = "0.1.1-testing-0.4"
edition = "2021"
authors = ["wervice"]
description = "An open source home lab device frontend to administrate your devices"
documentation = "https://github.com/Wervice/zentrox/wiki"
homepage = "https://wervice.github.io/zentrox"
license = "Apache-2.0"
keywords = ["administration", "actix", "remote"]
repository = "https://github.com/Wervice/zentrox"

[dependencies]
actix-cors = "0.7.0"
actix-files = "0.6.6"
actix-multipart = "0.7.2"
actix-rt = "2.10.0"
actix-session = { version = "0.10.0", features = ["cookie-session"] }
actix-web = { version = "4.9.0", features = ["openssl"] }
aes-gcm = "0.10.3"
argon2 = "0.5.3"
base64 = "0.22.1"
dirs = "5.0.1"
flate2 = "1.0.34"
futures = "0.3.30"
hex = "0.4.3"
openssl = { version = "0.10.66" }
rand = "0.8.5"
regex = "1.10.6"
ring = "0.17.8"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.127"
sha2 = "0.10.8"
sysinfo = "0.31.2"
systemstat = "0.2.3"
tar = "0.4.42"
tokio = "1.40.0"
toml = "0.8.19"
toml_edit = "0.22.20"
totp-rs = "5.6.0"
whoami = "1.5.1"

[profile.release]
opt-level = 3
debug = 0

[[bin]]
name = "zentrox"
path = "src/main.rs"

include = ["static/**/*", "install.bash", "ftp.py", "robots.txt"]

and

.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
build.bash
ftp.py
install.bash
notes/auth_note.txt
notes/cert_note.txt
notes/otp_note.txt
robots.txt
selfsigned.pem
src/config_file.rs
src/crypto_utils.rs
src/drives.rs
src/is_admin.rs
src/main.rs
src/otp.rs
src/packages.rs
src/sudo.rs
src/ufw.rs
src/url_decode.rs
src/vault.rs
static/404.html
static/WorkSansVar.ttf
static/_next/static/chunks/23-ca4e025992201270.js
static/_next/static/chunks/855-f9d01bc775d535b9.js
static/_next/static/chunks/app/_not-found/page-dfa4b69b523c3b6a.js
static/_next/static/chunks/app/layout-21f31edd7f0992fb.js
static/_next/static/chunks/fd9d1056-2b1d57ad7889b66a.js
static/_next/static/chunks/framework-f66176bb897dc684.js
static/_next/static/chunks/main-0c81b90f05be42f1.js
static/_next/static/chunks/main-app-f0c1872b9043b9fd.js
static/_next/static/chunks/pages/_app-6a626577ffa902a4.js
static/_next/static/chunks/pages/_error-1be831200e60c5c0.js
static/_next/static/chunks/polyfills-78c92fac7aa8fdd8.js
static/_next/static/css/24e1567b0b879c10.css
static/_next/static/media/26a46d62cd723877-s.woff2
static/_next/static/media/55c55f0601d81cf3-s.woff2
static/_next/static/media/581909926a08bbc8-s.woff2
static/_next/static/media/6d93bde91c0c2823-s.woff2
static/_next/static/media/97e0cb1ae144a2a9-s.woff2
static/_next/static/media/WorkSansVar.22ee5c1e.ttf
static/_next/static/media/a34f9d1faa5f3315-s.p.woff2
static/_next/static/media/df0a9ae256c0569c-s.woff2
static/dashboard.html
static/dashboard.txt
static/index.html
static/index.txt
static/zentrox_dark.svg
update_frontend.bash

The files seem to be included, but when I cargo publish --no-verify --allow-dirty, they do not seem to appear when cargo install-ing the package.
I could also imagine --allow-dirty to cause trubble, but the only files that are not commited to git, are the following

selfsigned.pem # <- Selfsigned certificate
static/404.html # <- Static files
static/index.html
static/dashboard.html
static/index.txt
static/dashboard.txt

and install.bash, ftp.py,... are still missing.
Thank you for your patience

They are part of the package and thus available during the build of a cargo install, but not afterwards.

cargo install is not a general mechanism to ship a package and all accompanying files. It is a way to install binaries built from Rust code and only those.
So you either have to embed those static files at build time (e.g. using include_str) and then have your code at runtime use that included data, or you need another distribution mechanism, such as building your own installer or building a package in the platform's desired format (e.g. a deb or rpm or similar for Linux systems)

4 Likes

This sounds like a reasonable approach, but this requires corss-compiling the project, right?

The files will be bundled along with your source code, but they will not be installed anywhere.

Cargo + crates.io is unsuitable for distribution of software that needs more than a single self-contained executable.

You will need to distribute builds in some other way, through other channels.

For building and packaging see:

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.