A template Rust project with fully functional and no-frills Nix support, as well as builtin VSCode configuration to get IDE experience without any manual setup (just install direnv, open in VSCode and accept the suggestions). It uses crane, via rust-flake.
Note
If you are looking for the original template based on this blog post's use of crate2nix, browse from this tag. The evolution of this template can be gleaned from releases.
You can use omnix1 to initialize this template:
nix --accept-flake-config run github:juspay/omnix -- init github:gignsky/rust-nix-template -o ~/my-rust-project
- There are two CI workflows, and one of them uses Nix which is slower (unless you configure a cache) than the other one based on rustup. Pick one or the other depending on your trade-offs.
This repo uses Flakes from the get-go.
# Dev shell
nix develop
# or run via cargo
nix develop -c cargo run
# build
nix buildWe also provide a justfile for Makefile'esque commands to be run inside of the devShell.
- Run
nix flake updateto update all flake inputs. - Run
nix --accept-flake-config run github:juspay/omnix cito build all outputs. - Run
just checkto run flake checks (automatically skips template.nix which won't be in downstream repos). - Run
just check-templateto run flake checks including template.nix (useful for debugging template issues). - [pre-commit] hooks will automatically be setup in Nix shell. You can also run
pre-commit run -amanually to run the hooks (e.g.: to autoformat the project tree usingrustfmt,nixpkgs-fmt, etc.).
------------------------------ PRE-GEN README STARTS HERE FOR NON-TEMPLATE ---------------------------------------------[
](https://github.com/gignsky/rust-nix-template/actions/workflows/ci-nix.yml)
A command line utility written entirely in rust that creates tarballs from folders in the current working directory and optionally remove the folders that created those tarballs
$ nix run github:gignsky/rust-nix-template -- {optional-arguments (i.e. --help)}
$ nix shell github:gignsky/rust-nix-template
In your flake.nix file:
- add
inputs.rust-nix-template.url = "github:gignsky/rust-nix-template";to your inputs - inside of your
environment.systemPackagesoften defined under:outputs.nixosConfigurations.{hostname}.nixpkgs.lib.nixosSystem.modules - add either:
inputs.rust-nix-template.packages.${system}.rust-nix-templateinputs.rust-nix-template.packages.${system}.default
- Navigate to https://github.com/gignsky/rust-nix-template/releases
- Download the latest (or desired) release for your operating system
- As of the time of this writing only x86_64 binaries are compiled for GNU/Linux and windows
- if using Linux run
chmod +x ./{downloaded-binary} - if using windows you can run the binary from the command prompt or powershell in order to pass arguments, if double-click running the .exe file the default process of the binary will run in the current folder of the binary without the remove flag
- FUTURE FUNCTIONALITY INTENDED: In the future it is intended that the program will produce a dialog box that allows the user to specify which option arguments to be used.
- git clone this repository:
$ git clone https://github.com/gignsky/rust-nix-template.git
- cd into the directory:
$ cd rust-nix-template
- enter development environment:
$ nix develop
- build binary with justfile (uses nix build command) or build with cargo
$ just build- nix build command
- produces a binary in
./result/bin/rust-nix-template
$ cargo build- cargo build command (debug)
- produces a debug binary in
./target/debug/rust-nix-template
$ cargo build --release- cargo build command (release)
- produces a debug binary in
./target/release/rust-nix-template
- in order to use this compiled binary in any location one must add the binary to their $PATH (this is OS specific and not within the scope of this README)
- NOTE: This is not an issue if installed using nix, whether that be flakes and NixOS or nix shell
$ ./{downloaded-binary} {optional-arguments (i.e. --help)}
$ rust-nix-template {optional-arguments (i.e. --help)}
A simple Rust project using Nix
Usage: rust-nix-template [OPTIONS] [NAME]
Arguments:
[NAME] an optional name to greet
Options:
-v whether to be verbose
-h, --help Print help
-V, --version Print version
This repo uses Flakes from the get-go.
# Dev shell
nix develop
# or run via cargo
nix develop -c cargo run
# build
just buildWe also provide a justfile for Makefile'esque commands to be run inside of the devShell.
You can run $ just inside $ nix develop to see all available just outputs
This template includes comprehensive testing infrastructure adapted from .dotfiles configurations:
# Run all tests (Rust + Nix)
just test
# Run only Rust tests
just test-rust
# Run template-specific tests (includes template.nix)
just test-template
# Run clippy linting
just clippy
# Check Rust formatting
just fmt-check
# Simulate CI locally
just ci
# Run pre-commit flake check
just pre-commit-check# Test Rust functionality
cargo test --all-features
cargo clippy --all-targets --all-features -- -D warnings
cargo fmt -- --check
# Test Nix build system
nix flake check --impure --no-build # Skip template.nix
nix flake check # Include template.nix
nix build --no-link
# Test template functionality
nix --accept-flake-config run github:juspay/omnix -- ci --no-buildThe project includes multiple GitHub Actions workflows:
- Cargo CI (
check.yml) - Runs Rust tests, formatting, and linting - Nix CI (
ci-nix.yml) - Runs Nix flake checks and omnix CI - Template Check (
template-check.yml) - Tests template-specific functionality
Pre-commit hooks are automatically configured and include:
rustfmt- Rust code formattingclippy- Rust lintingnixpkgs-fmt- Nix code formattingflake-check- Custom Nix flake validation
- Run
$ just update-flaketo update all flake inputs. - Run
$ just updateto update all flake inputs and cargo dependencies - Run
$ just checkto run flake checks (automatically skips template.nix which won't be in downstream repos). - Run
$ just check-templateto run flake checks including template.nix (useful for debugging template issues). - Run
nix --accept-flake-config run github:juspay/omnix cito build all outputs. - [pre-commit] hooks will automatically be setup in Nix shell. You can also run
pre-commit run -amanually to run the hooks (e.g.: to autoformat the project tree usingrustfmt,nixpkgs-fmt, etc.).
Footnotes
-
If initializing manually, make sure to:
- Change
namein Cargo.toml. - Run
cargo generate-lockfilein the nix shelld
- Change