Crates.io | rsubst |
lib.rs | rsubst |
version | |
source | src |
created_at | 2025-03-30 21:42:03.842119+00 |
updated_at | 2025-04-02 11:48:33.811143+00 |
description | A small, envsubst-like utility with conditional logic and advanced templating. |
homepage | https://github.com/jtdowney/rsubst |
repository | https://github.com/jtdowney/rsubst |
max_upload_size | |
id | 1612780 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
rsubst
is a simple templating CLI tool built in Rust, leveraging MiniJinja to provide dynamic substitutions and conditional logic in configuration files and other text-based templates. It serves as a flexible and powerful alternative to simpler tools like envsubst
.
rsubst
was created to simplify Docker container configuration at runtime. Previously, tools like envsubst
were easy to use but lacked support for conditionals and robust escaping, whereas solutions like Jinja2 provided advanced templating but required an additional Python runtime inside containers. rsubst
combines the simplicity of envsubst
with powerful conditional logic from Jinja-style templating, packaged in a lightweight Rust binary that eliminates external dependencies.
if
, else
, elif
)for
loops)To install rsubst
, ensure you have Rust installed and use Cargo:
cargo install --locked rsubst
Build for release with musl libc on Linux:
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
You can use cargo-zigbuild
on other platforms.
You can also build rsubst
using Docker in a multi-stage build. This is especially useful for containerized applications where you want to configure your system at startup using a lightweight templating tool. With this approach, rsubst
is compiled in one layer and copied into the final minimal image, where it can be used in an entrypoint script to render configuration files dynamically before launching your application.
# Build stage
FROM rust:alpine AS rsubst-builder
RUN cargo install --locked rsubst
# Later in the final stage
COPY --from=rsubst-builder /usr/local/cargo/bin/rsubst /usr/local/bin/rsubst
Then in your entrypoint:
#!/bin/sh
set -e
rsubst config.conf.j2 > /app/config.conf
exec "$@"
Basic usage:
rsubst output.conf.j2 > output.conf
With environment variables:
export APP_ENV=production
rsubst output.conf.j2 > output.conf
Given a template file config.conf.j2
:
app_environment={{ APP_ENV }}
Set an environment variable and render the template:
export APP_ENV=staging
rsubst config.conf.j2
Output:
app_environment=staging
Template example (config.conf.j2
):
{% if APP_ENV == "production" %}
debug_mode=false
{% else %}
debug_mode=true
{% endif %}
Usage:
export APP_ENV=production
rsubst config.conf.j2
Output:
debug_mode=false
Template (servers.yaml.j2
):
servers:
{% for server in SERVERS | split(",") -%}
- {{ server }}
{% endfor %}
Usage:
export SERVERS="web01,web02,db01"
rsubst servers.yaml.j2
Output:
servers:
- web01
- web02
- db01
rsubst
is available under the MIT license. See LICENSE
file for more details.