Description
Official Nix formatter zero-setup starter using treefmt.
A zero-setup treefmt starter to get started using the official Nix formatter.
For
nix fmt
to format all Nix files, add this to theflake.nix
outputs:formatter.${system} = nixpkgs.legacyPackages.${system}.nixfmt-tree;
The same can be done more efficiently with the
treefmt
command, which you can get innix-shell
/nix develop
by extendingmkShell
usingmkShell { packages = [ pkgs.nixfmt-tree ]; }
You can then also use
treefmt
in a pre-commit/pre-push Git hook andnixfmt
with your editors format-on-save feature.To check formatting in CI, run the following in a checkout of your Git repository:
treefmt --ci
For more flexibility, you can customise this package using
nixfmt-tree.override {
settings = { /* treefmt config */ };
runtimePackages = [ /* List any formatters here */ ];
}
Alternatively you can switch to the more fully-featured treefmt-nix.