nix-dev-env
Skill1 starsChanged 40 days agoMITView on GitHub
Nix dev shells — mkShell, devShells.default, direnv with use flake, writeShellApplication for lint-checked scripts, formatters (alejandra, nixpkgs-fmt), linters (statix, deadnix), and pre-commit-hooks.nix. Use when building or debugging a dev environment in a flake, wiring direnv, or setting up Nix quality gates. Triggers: devShell, mkShell, direnv, .envrc, writeShellApplication, alejandra, statix, deadnix, pre-commit.
In this repository42 other files it gives its agents.
- AGENTS.mdAGENTS.md
- addressing-findingsSkill
- auditSkill
- codebase-onboardingSkill
- codex-reviewSkill
- compatibilitySkill
- debuggingSkill
- developSkill
- distributed-racesSkill
- memorySkill
- nix-flakesSkill
- nixosSkill
- nix-packagingSkill
- nix-reviewSkill
- refactoringSkill
- retroSkill
- review-initSkill
- review-rulesSkill
- rust-architecture-reviewSkill
- rust-architectureSkill
- rust-cliSkill
- rust-cloud-nativeSkill
- rust-concurrencySkill
- rust-ecosystemSkill
- rust-embeddedSkill
- rust-errorsSkill
- rust-fintechSkill
- rust-idiomsSkill
- rust-macrosSkill
- rust-mlSkill
- rust-navigationSkill
- rust-ownershipSkill
- rust-performanceSkill
- rust-pluginsSkill
- rust-reviewSkill
- rust-securitySkill
- rust-testingSkill
- rust-traitsSkill
- rust-unsafeSkill
- rust-webSkill
- specsSkill
- sql-reviewSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 46
Prefer packages over buildInputs/nativeBuildInputs in a pure dev-shell context.
- Very specific: line 85
After this, entering the directory loads devShells.default automatically.
- Very specific: line 134
Prefer writeShellApplication over writeScriptBin whenever the script is more than one line — shellcheck catches quoting bugs before they …
- Very specific: line 156
Run both in CI as a gate — they are fast and have zero false positives on well-written Nix.
- Very specific: line 202
If your shell includes an unfree package (e.g.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Nix Dev Environments
A reproducible dev shell is a pkgs.mkShell derivation exposed as devShells.default in
your flake. Everyone who enters the shell gets identical tools, at the exact same versions,
without global installs.
When to Use
- Defining or debugging a
devShells.defaultin a flake - Wiring
direnvso the shell activates oncd - Writing helper scripts that should be lint-checked (
writeShellApplication) - Adding Nix formatters or linters as a quality gate
- Hooking
statix/deadnix/formatters intopre-commitviapre-commit-hooks.nix
pkgs.mkShell — the shell derivation
mkShell is a thin wrapper around stdenv.mkDerivation that is only ever built for the
shell, never installed. The two fields you reach for most:
pkgs.mkShell {
packages = [
pkgs.rustup
pkgs.cargo-nextest
pkgs.alejandra # Nix formatter
pkgs.statix # Nix linter
pkgs.deadnix # dead-code linter
];
shellHook = ''
echo "rust $(rustc --version 2>/dev/null || echo 'not yet installed')"
export RUST_BACKTRACE=1
'';
}packages(preferred over the legacybuildInputs) — tools placed onPATH.shellHook— bash snippet that runs each time the shell is entered; good for environment variable exports and one-time setup messages.- Prefer
packagesoverbuildInputs/nativeBuildInputsin a pure dev-shell context; the distinction only matters inside actual derivation builds.
Exposing as devShells.default
Wire the shell as a flake output so nix develop and direnv can find it:
# flake.nix (flake-parts style — see nix-flakes for the full flake skeleton)
perSystem = { pkgs, ... }: {
devShells.default = pkgs.mkShell {
packages = [ pkgs.cargo pkgs.rustfmt pkgs.clippy pkgs.alejandra pkgs.statix ];
shellHook = ''
export CARGO_TERM_COLOR=always
'';
};
};nix develop with no arguments enters devShells.default for the current system.
Named shells (devShells.ci, devShells.docs) follow the same pattern and are entered
with nix develop .#ci.
direnv — automatic shell entry on cd
direnv reads .envrc when you enter a directory and activates the shell without a
manual nix develop. Add .envrc to the project root:
# .envrc
use flakeThen allow it once:
direnv allowAfter this, entering the directory loads devShells.default automatically; leaving
unloads it. Commit .envrc; add .direnv/ to .gitignore.
use flake requires nix-direnv (a faster, cached loader). Install via your system
nix configuration or home-manager:
# home-manager fragment
programs.direnv = {
enable = true;
nix-direnv.enable = true;
};Without nix-direnv, direnv falls back to a slow eval on every entry. With it,
the shell is rebuilt only when flake.lock or the relevant output changes.
writeShellApplication — lint-checked scripts
pkgs.writeShellApplication is the correct primitive for shell scripts that live in
the flake. Unlike writeScriptBin, it runs the script through shellcheck at build
time and enforces set -euo pipefail — lint errors become build failures.
perSystem = { pkgs, ... }:
let
check = pkgs.writeShellApplication {
name = "check";
runtimeInputs = [ pkgs.statix pkgs.deadnix pkgs.alejandra ];
text = ''
statix check .
deadnix --fail .
alejandra --check .
'';
};
in {
devShells.default = pkgs.mkShell {
packages = [ check pkgs.cargo pkgs.rustfmt pkgs.clippy ];
};
# also expose as a runnable app
apps.check = { type = "app"; program = "${check}/bin/check"; };
};Key points:
runtimeInputs— tools available inside the script; they don't leak ontoPATHoutside it.text— the script body without a shebang;writeShellApplicationadds#!/usr/bin/env bashandset -euo pipefailautomatically.- Prefer
writeShellApplicationoverwriteScriptBinwhenever the script is more than one line — shellcheck catches quoting bugs before they reach CI.
Nix formatters and linters — the quality gate
Two formatters cover .nix files:
| Tool | Style | Command |
alejandra | opinionated, no config | alejandra . |
nixpkgs-fmt | community standard | nixpkgs-fmt . |
Pick one per project and stick with it. alejandra is the current default in new
projects; nixpkgs-fmt is what nixpkgs itself uses.
Two linters catch correctness and dead-code issues:
| Tool | What it finds | Command |
statix | anti-patterns, redundant rec, deprecated syntax | statix check . |
deadnix | unused let bindings and function arguments | deadnix --fail . |
Run both in CI as a gate — they are fast and have zero false positives on well-written
Nix. The CI integration and review workflow for these tools belongs to nix-review.
pre-commit-hooks.nix — hooks wired from the flake
pre-commit-hooks.nix generates a pre-commit configuration as a Nix derivation,
keeping hook versions pinned with the rest of the flake.
# flake inputs
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
};# perSystem block
perSystem = { pkgs, system, ... }:
let
hooks = inputs.pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
alejandra.enable = true;
statix.enable = true;
deadnix.enable = true;
rustfmt.enable = true;
};
};
in {
devShells.default = pkgs.mkShell {
packages = [ pkgs.cargo pkgs.rustfmt pkgs.clippy ];
inherit (hooks) shellHook; # installs hooks on shell entry
};
checks.pre-commit = hooks; # also runs in `nix flake check`
};inherit (hooks) shellHook installs the hooks when the developer enters the shell —
no separate pre-commit install step required. Adding the result to checks means
nix flake check validates the hook configuration without running the hooks.
Gotcha: allowUnfree does not apply to nix develop
If your shell includes an unfree package (e.g. pkgs.vscode, pkgs.cuda), nix develop
will refuse with "unfree package … refused" even if your system config sets
nixpkgs.config.allowUnfree = true. The system config is not visible to flake evaluation.
Fix — import nixpkgs with config explicitly:
perSystem = { system, ... }:
let
pkgs = import inputs.nixpkgs {
inherit system;
config.allowUnfree = true;
};
in {
devShells.default = pkgs.mkShell {
packages = [ pkgs.vscode pkgs.cargo ];
};
};Alternative — environment variable (no flake change needed):
NIXPKGS_ALLOW_UNFREE=1 nix develop --impure--impure is required when the flake evaluation reads environment variables. This is
handy for one-off overrides; prefer the explicit import for a permanent fix checked
into the flake.
Root cause: nixpkgs.legacyPackages (the default nixpkgs surface in flake-parts and
flake-utils) does not inherit config from NixOS module options.
Boundaries
- Flake structure,
inputs/outputsschema,flake.lock,flake-parts/flake-utilswiring, and exposingdevShellsas flake outputs →nix-flakes. - Derivations, overlays, overrides,
callPackage, and packaging Rust binaries for distribution →nix-packaging. - Running
statix check/deadnixin CI pipelines and using them as review gates →nix-review.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

