{ inputs, ... }:
{
  imports = [
    inputs.treefmt-nix.flakeModule
  ];

  perSystem =
    { pkgs, ... }:
    {
      treefmt.config = {
        projectRootFile = "flake.nix";
        flakeCheck = true;

        programs = {
          # keep-sorted start block=yes newline_separated=no
          isort.enable = true;
          keep-sorted.enable = true;
          nixf-diagnose = {
            enable = true;
            priority = -1;
          };
          nixfmt = {
            enable = true;
            package = pkgs.nixfmt-rfc-style;
          };
          prettier = {
            enable = true;
            excludes = [ "**.md" ];
          };
          ruff = {
            check = true;
            format = true;
          };
          shfmt.enable = true;
          statix = {
            enable = true;
            priority = -2;
            disabled-lints = [
              # We often use `nullable == true`
              "bool_comparison"
            ];
          };
          stylua.enable = true;
          # FIXME: re-enable on darwin, currently broken: taplo with options '[format]' failed to apply: exit status 101
          taplo.enable = pkgs.stdenv.isLinux;
          # keep-sorted end
        };

        settings = {
          global.excludes = [
            ".editorconfig"
            ".envrc"
            ".git-blame-ignore-revs"
            ".gitignore"
            "LICENSE"
            "flake.lock"
            "**.md"
            "**.scm"
            "**.svg"
            "**/man/*.5"
            # Those files are generated by pytest-regression, which then `diff`s them.
            # Formatting them will make the tests fail.
            "docs/gfm-alerts-to-admonitions/tests/**/*.yml"
          ];
          formatter.ruff-format.options = [ "--isolated" ];
          formatter.nixf-diagnose.options = [
            "--auto-fix"
            "--ignore=sema-unused-def-lambda-witharg-formal"
          ];
          formatter.nixf-diagnose.excludes = [
            # sema-unused-def-lambda-noarg-formal
            "ci/rust-analyzer/default.nix"
          ];
        };
      };

    };
}
