diff --git a/CLAUDE.md b/CLAUDE.md index ec050a3..5ae799e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -96,9 +96,13 @@ install, so the LSP sees the same compiler version as the build. `scripts/rename-project.sh ` replaces every `rust_template` occurrence (crate name in `Cargo.toml`, the `pname`/binary name in `nix/rust.nix` and `flake.nix`, and the `use rust_template::...` imports in `src/main.rs`, `tests/greet.rs`, `benches/greet.rs`), then -regenerates `Cargo.lock`. It does not rename the project directory itself — that's a manual `mv` -afterward (the script prints the exact command). Run `nix flake check` after renaming to confirm -everything still resolves. +regenerates `Cargo.lock` and reformats (`cargo fmt`/`nix fmt`) — the rename can flip `use` +statements out of alphabetical order (e.g. renaming to something that sorts before `criterion`), +which would otherwise fail the `treefmt` check on the next `nix flake check`. It does not rename +the project directory itself — that's a manual `mv` afterward (the script prints the exact +command). If the tree isn't git-tracked yet (e.g. straight after `nix flake init -t`), run +`git add -A` before *any* `nix` command, including the rename script's own `nix fmt` fallback — +flakes only see git-tracked files. ## Keeping this file current diff --git a/README.md b/README.md index cecb204..c3c5019 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,16 @@ way with plain `cargo` or with `nix`. ## Using this template -1. Copy/clone this repo (or, once published, `nix flake init -t `). -2. Rename the placeholder crate name: +1. Copy/clone this repo, or scaffold fresh with `nix flake init -t `. +2. If you used `nix flake init` (no `.git` yet), run `git init && git add -A` — + Nix flakes only see git-tracked files, so every `nix` command below needs this first. +3. Rename the placeholder crate name: ```bash ./scripts/rename-project.sh my_project cd .. && mv rust_template my_project && cd my_project + git add -A # stage the rename before running any nix command again ``` -3. Build whatever you actually came here for — replace `greet` in `src/lib.rs`, extend the CLI in +4. Build whatever you actually came here for — replace `greet` in `src/lib.rs`, extend the CLI in `src/main.rs`, add your own tests and benchmarks. ## Building and running diff --git a/nix/treefmt.nix b/nix/treefmt.nix index b863adb..1895085 100644 --- a/nix/treefmt.nix +++ b/nix/treefmt.nix @@ -4,7 +4,12 @@ _: { treefmt = { projectRootFile = "flake.nix"; + # Edition must match Cargo.toml's `edition = "2024"` — otherwise + # treefmt's rustfmt (from nixpkgs) and `cargo fmt`'s rustfmt (from the + # fenix toolchain, which reads the edition from Cargo.toml) disagree + # on import ordering/grouping and fight each other. programs.rustfmt.enable = true; + programs.rustfmt.edition = "2024"; programs.nixfmt.enable = true; programs.deadnix.enable = true; programs.statix.enable = true; diff --git a/scripts/rename-project.sh b/scripts/rename-project.sh index cbde85e..2a74fcb 100755 --- a/scripts/rename-project.sh +++ b/scripts/rename-project.sh @@ -48,7 +48,22 @@ else echo "warning: cargo not on PATH — run 'cargo generate-lockfile' yourself (e.g. inside 'nix develop')" >&2 fi +# Renaming can reorder `use` statements alphabetically (e.g. "acme_widget" +# now sorts before "criterion"), which rustfmt/treefmt will otherwise flag +# as unformatted on the next check. Reformat immediately so the tree is +# clean right after renaming. +if command -v cargo >/dev/null 2>&1 && cargo fmt --version >/dev/null 2>&1; then + cargo fmt +elif command -v nix >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + nix fmt >/dev/null 2>&1 || echo "warning: 'nix fmt' failed — run it yourself once files are committed" >&2 +else + echo "warning: could not auto-format — run 'cargo fmt' or 'nix fmt' yourself" >&2 +fi + echo "Renamed '${old_name}' -> '${new_name}'." echo echo "Review the diff (git diff), then optionally rename the project directory:" echo " cd .. && mv $(basename "$root") ${new_name}" +echo +echo "If this tree isn't committed to git yet, run 'git add -A' before any" +echo "'nix' command — Nix flakes only see git-tracked files."