Integrate treefmt-nix with Nix Flakes
mainTo use treefmt-nix with Flakes, import the library via inputs.treefmt-nix.url = "github:numtide/treefmt-nix".
You can use treefmt-nix.lib.evalModule to evaluate your configuration (defined in a treefmt.nix file) across multiple systems. This allows you to export a formatter for use with nix fmt and checks for use with nix flake check in CI.
- Define your configuration in
treefmt.nix. - Use
evalModulein yourflake.nixto generate the system-specific configurations. - Run
nix fmtto format your project using the wrappedtreefmtbinary.
# flake.nix
{
inputs.treefmt-nix.url = "github:numtide/treefmt-nix";
inputs.systems.url = "github:nix-systems/default";
outputs = { self, nixpkgs, systems, treefmt-nix }:
let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./treefmt.nix);
in
{
# for `nix fmt`
formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
# for `nix flake check`
checks = eachSystem (pkgs: {
formatting = treefmtEval.${pkgs.system}.config.build.check self;
});
};