Skip to content

treefmt

Add treefmt-nix to your inputs:

Terminal window
$ devenv inputs add treefmt-nix github:numtide/treefmt-nix

Check the available integrations in the list of all available integrations.

Add your desired integration to your devenv.nix file. For example, the following code would enable treefmt with the nixpkgs-fmt and rustfmt integrations:

{ inputs, ... }:
{
treefmt = {
enable = true;
config.programs = {
nixfmt.enable = true;
rustfmt.enable = true;
};
};
}
Terminal window
$ devenv shell
Building shell ...
Entering shell ...
treefmt # This would run treefmt on all files.

If you would like to enable treefmt in your git-hooks hooks, simply add:

{ inputs, ... }:
{
git-hooks.hooks = {
treefmt.enable = true;
};
}

This will enable treefmt hooks and automatically change the default package to the one you have defined in your devenv.

It is also possible to use custom formatters with treefmt-nix. For example, the following custom formatter formats JSON files using yq-go:

{ pkgs, lib, ... }
{
treefmt.config.settings.formatter = {
"yq-json" = {
command = "${lib.getExe pkgs.bash}";
options = [
"-euc"
''
for file in "$@"; do
${lib.getExe yq-go} -i --output-format=json $file
done
''
"--" # bash swallows the second argument when using -c
];
includes = [ "*.json" ];
excludes = [ ".git/*" ".devenv/*" ]; # don't mess with git and devenv files
};
};
}