diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..fa2c370 --- /dev/null +++ b/.envrc @@ -0,0 +1,11 @@ +# Automatically enter the Nix dev shell on `cd`. Requires direnv: https://direnv.net/ +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi + +use flake + +watch_file flake.nix +watch_file flake.lock +watch_file nix/*.nix +watch_file Cargo.toml diff --git a/CLAUDE.md b/CLAUDE.md index 8adde68..ec050a3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -50,6 +50,10 @@ Both paths are meant to converge on the same result — the Nix devShell and the are built from one shared toolchain/build-args definition (see Architecture below), so what compiles under `nix build` should also compile under plain `cargo build` inside `nix develop`. +`justfile` wraps the commands above (`just build`, `just test`, `just ci`, `just nix-check`, ...; +run `just` with no args to list them) — available inside `nix develop`. `.envrc` (`use flake`) lets +direnv users skip typing `nix develop` entirely. + ## IDE / LSP support `rust-analyzer` is included in the fenix toolchain (`nix/rust.nix`), and `nix develop`'s @@ -74,7 +78,8 @@ install, so the LSP sees the same compiler version as the build. treefmt-nix, wired into both `nix fmt` and the `formatting` check in `flake.nix`. - **`flake.nix`** ties it together with `flake-parts`: it imports the treefmt module, evaluates `nix/rust.nix` per-system, and exposes `packages.default`, `checks`, `apps.default`, and - `devShells.default` from its outputs. + `devShells.default` from its outputs. It also declares `flake.templates.default`, so once this + repo is published, `nix flake init -t ` scaffolds a new project straight from it. - **`src/lib.rs`** holds the actual logic (currently just `greet`); `src/main.rs` is a thin `clap` wrapper around it. Keep new functionality in the lib crate, not `main.rs`, so it stays reachable from `tests/` and `benches/` (both depend on the `rust_template` lib crate by name, not on the diff --git a/README.md b/README.md index fd8fc19..cecb204 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ way with plain `cargo` or with `nix`. ## Using this template -1. Copy/clone this repo. +1. Copy/clone this repo (or, once published, `nix flake init -t `). 2. Rename the placeholder crate name: ```bash ./scripts/rename-project.sh my_project @@ -34,4 +34,9 @@ nix flake check # clippy, fmt, doc, audit, tests — all in one go nix fmt # format Rust, Nix, and TOML files ``` +If you have [direnv](https://direnv.net/) installed, `direnv allow` activates the dev shell +automatically on `cd` (see `.envrc`). + +`just` (available inside `nix develop`) wraps the common commands above — run `just` to list them. + See `CLAUDE.md` for more detail on the project layout and how the pieces fit together. diff --git a/flake.nix b/flake.nix index 872bc4a..842081f 100644 --- a/flake.nix +++ b/flake.nix @@ -76,5 +76,10 @@ devShells.default = import ./nix/devshell.nix { inherit pkgs lib rust; }; }; + + flake.templates.default = { + path = ./.; + description = "Rust CLI + lib template with crane, fenix, and treefmt-nix"; + }; }; } diff --git a/justfile b/justfile new file mode 100644 index 0000000..0c5faac --- /dev/null +++ b/justfile @@ -0,0 +1,71 @@ +# justfile — task runner. See: https://just.systems/ +# Available via `nix develop` (or install `just` yourself). + +default: + @just --list + +# --- cargo --- + +build: + cargo build + +build-release: + cargo build --release + +run *args: + cargo run -- {{ args }} + +test: + cargo test + +bench: + cargo bench + +clippy: + cargo clippy --all-targets -- --deny warnings + +fmt: + nix fmt + +fmt-check: + nix fmt -- --fail-on-change + +audit: + cargo audit + +doc: + cargo doc --no-deps + +doc-open: + cargo doc --no-deps --open + +# Everything nix flake check runs, but via cargo for a faster local loop +ci: fmt-check clippy test audit doc + +clean: + cargo clean + rm -rf result result-* + +# Background code checker (bacon) +watch: + bacon + +# --- nix --- + +nix-build: + nix build + +nix-check: + nix flake check + +nix-develop: + nix develop + +nix-update: + nix flake update + +# --- template --- + +# Rename the "rust_template" placeholder to (snake_case) +rename name: + ./scripts/rename-project.sh {{ name }} diff --git a/nix/devshell.nix b/nix/devshell.nix index 46d390d..75bd40a 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -18,6 +18,7 @@ rust.craneLib.devShell { cargo-expand taplo bacon + just ] ++ lib.optionals pkgs.stdenv.isLinux [ mold ] ++ lib.optionals pkgs.stdenv.isDarwin [