Nestix is a structural formatter for Nix. It rewrites structure (currently focused on attribute sets) rather than acting as a full pretty-printer.
It is designed to be used alongside a conventional Nix formatter such as alejandra or nixfmt:
- Run alejandra or nixfmt to standardize whitespace and line breaking.
- Run Nestix to normalize and (where appropriate) flatten attribute-set structure.
Run Nestix after your usual Nix formatter. Example (single file):
alejandra file.nix # or nixfmt
nestix file.nixFormat a repository (recursively formats *.nix files in place):
nestix .Check mode (exits with code 2 if changes would be made):
nestix --check .Read from stdin and write to stdout:
cat file.nix | nestixNestix focuses on transformations that conventional formatters typically do not perform:
- Flattening small attribute-set subtrees into dotted attribute assignments (when it improves readability).
- Normalizing indentation when Nestix needs to move code.
Example (flattening an attribute set):
# Before
{
attr1 = {
attr2 = {
attr3 = true;
};
attr4 = true;
};
}
# After
{
attr1.attr2.attr3 = true;
attr1.attr4 = true;
}Nestix does not blindly flatten everything. It keeps (or expands) nesting when flattening would reduce readability.
- Replace alejandra or nixfmt: Nestix does not aim to make every Nix expression "pretty" on its own.
- Impose a style guide: Nestix is unopinionated and attempts to preserve the existing style (alignment, indentation, etc).
Run without installing:
nix run github:Noah765/nestixInstall into your profile:
nix profile install github:Noah765/nestixcargo install --git https://github.com/Noah765/nestixMinimal NixOS system flake snippet
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.nestix = {
url = "github:Noah765/nestix";
inputs.nixpkgs.follows = "nixpkgs";
};
outputs = {
nixpkgs,
nestix,
...
}: let
system = "x86_64-linux";
in {
nixosConfigurations.example = nixpkgs.lib.nixosSystem {
inherit system;
modules = [
{
environment.systemPackages = [nestix.packages.${system}.default];
}
];
};
};
}nestix [OPTIONS] [INCLUDE]...
- If no
INCLUDEpaths are provided, Nestix reads Nix code from stdin. - If paths are provided, Nestix traverses them recursively and formats
*.nixfiles in place.
Common flags:
--check: Do not write changes; print files that need formatting; exit with code2if any do.--exclude <PATH>: Skip a path (may be repeated).
See nestix --help for the full CLI help text.
Nestix is intended to be fast enough to run over large file trees. As a rough datapoint, nestix /path/to/nixpkgs takes about 8 seconds on an AMD Ryzen 5 3600.
- Nix dev shell:
nix develop - Build:
cargo build --release - Test:
cargo test
If you encounter a bug or would like a new formatting rule, please open an issue. Small, focused, reproducible snippets are extremely helpful.