mkChecks converts declarative check configurations into proper Nix derivations that can be used in your flake’s checks output.
Overview
Define checks with simple script strings instead of writing full derivations:Function Signature
Check Configuration
Each check can have the following fields:Shell script to run for the check. Cannot be combined with
checkPhase or forEach.Custom check phase script. Cannot be combined with
script or forEach.Script to run for each file. The file path is available as
$file. Cannot be combined with script or checkPhase.List of packages to add to
nativeBuildInputs. Alias: nativeBuildInputs.Source to check. Can be a derivation or a path.
Root directory for file selection. Used with
fileset or filter.Nix fileset to check. Requires
root to be set.File filter function. Requires
root to be set.Files or directories to exclude from checking.
Environment Variables
All checks run with these environment variables set:HOME: Temporary directory (for tools that need a home)TREEFMT_TREE_ROOT: Current working directory
Examples
Basic Format Check
Multiple Checks
Per-File Checks
Run a check on each file individually:forEach script runs with:
$fileset to the absolute path of each file- Bash globstar enabled (
shopt -s globstar) - Automatic file type filtering
Using Fileset Filtering
Ignoring Files
Check with Custom Phase
Checking a Derivation
You can also add checks to an existing derivation:src is a derivation, mkChecks uses .overrideAttrs to add the check phase.
Usage in Flakes
Combine withmkFlake for multi-system checks:
flake.nix
Implementation Details
Check Phase Generation
The check phase is constructed from:- Environment variable exports (
HOME,TREEFMT_TREE_ROOT) - User’s
checkPhase,script, orforEachscript - For
forEach: Bash loop over all files with globstar
Source Location
Implemented in:libs/mkChecks/default.nix:1
Best Practices
- Use
rootwith filesets: More efficient than copying entire directories - Leverage
forEach: Easier to debug when checks fail on specific files - Keep checks fast: CI runs all checks, so optimize for speed
- Use
depsnotnativeBuildInputs: More concise and clear intent
Related
mkApps
Create flake apps with similar simplicity
mkFlake
Combine with mkFlake for multi-system checks
