build(nix): add flake#27
Conversation
Add flake for building/running/installing via Nix. The flake also provides a development shell with all deveplopment dependencies, as well as a check running `cargo fmt`, `cargo clippy`, and `cargo test`. Update docs to cover Nix path.
brhutchins
left a comment
There was a problem hiding this comment.
I see you accepted it already! I've added some PR comments to document the less self-explanatory bits of the flake. Maybe moot now, but might be useful to have in the history.
|
|
||
| inputs = { | ||
| nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | ||
| flake-utils.url = "github:numtide/flake-utils"; |
There was a problem hiding this comment.
Provides a set of utilities for making flakes more ergonomic – here eachDefaultSystem is used to avoid having to write explicit separate derivations for Linux x86, Linux ARM, MacOS Apple Silicon, etc.
| crane = { | ||
| url = "github:ipetkov/crane"; | ||
| }; |
There was a problem hiding this comment.
Crane provides lots of utilities for building Rust projects with Nix: https://crane.dev/index.html
| rust-overlay = { | ||
| url = "github:oxalica/rust-overlay"; | ||
| inputs.nixpkgs.follows = "nixpkgs"; | ||
| }; |
There was a problem hiding this comment.
rust-overlay lets you pin specific Rust toolchain dependencies
| extensions = [ "rustfmt" "clippy" ]; | ||
| }; | ||
|
|
||
| craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; |
There was a problem hiding this comment.
Uses the defined (latest) toolchain for building the project, instead of the default
| src = lib.cleanSourceWith { | ||
| filter = path: type: | ||
| (craneLib.filterCargoSources path type) || | ||
| (lib.hasSuffix ".md" path); | ||
| src = lib.cleanSource ./.; | ||
| }; |
There was a problem hiding this comment.
Tells Nix which files to include when building the project. Anything not included by the filter is treated as absent at build time.
Crane provides a filter that includes all standard Cargo/Rust files. I'm combining with another filter that adds .md files, because that's what tiny-harness stores its prompts in.
| buildInputs = [ | ||
| pkgs.openssl | ||
| ] ++ lib.optionals pkgs.stdenv.isDarwin [ | ||
| pkgs.apple-sdk |
There was a problem hiding this comment.
required for building on Macs
|
|
||
| tinyharness = craneLib.buildPackage (commonArgs // { | ||
| inherit cargoArtifacts; | ||
| doCheck = false; |
There was a problem hiding this comment.
This just means we don't run the checks by default when building, since those are handled by the flake checks
|
|
||
| cargoArtifacts = craneLib.buildDepsOnly commonArgs; | ||
|
|
||
| tinyharness = craneLib.buildPackage (commonArgs // { |
There was a problem hiding this comment.
This defines the actual build for the project
| packages = { | ||
| default = tinyharness; | ||
| tinyharness = tinyharness; | ||
| }; |
There was a problem hiding this comment.
Packages are the actual output from an end user perspective. When you do nix flake run, the default package is what gets called; same if you install via Nix.
| }; | ||
| }; | ||
|
|
||
| devShells.default = craneLib.devShell { |
There was a problem hiding this comment.
This defines all the development dependencies. You run nix develop, or use direnv to automate it when entering the directory, and you have everything you need to work on the project. No need to manually install a Rust toolchain, cargo, LSP, etc.
Thanks, i have done quick research to merge it as fast as i can. |
Add flake for building/running/installing via Nix.
The flake also provides a development shell with all deveplopment dependencies, as well as a check running
cargo fmt,cargo clippy, andcargo test.Update docs to cover Nix path.