numtide / treefmt-nix

treefmt nix configuration
https://numtide.github.io/treefmt/
MIT License
247 stars 75 forks source link

Ability to specify a custom formatter? #111

Closed DrRuhe closed 9 months ago

DrRuhe commented 1 year ago

Its obviously nice UX, that treefmt-nix comes with a bunch of formatters set-up. But a thing that seems to be missing is being able to specify a custom formatter, one that has yet to find its way into treefmt-nix.

For example, I tried specifying treefmt to utilize typstfmt in the flake-parts module like this:

treefmt.programs.typstfmt = {
              enable = true;
              command = "${pkgs.typstfmt}/bin/typstfmt";
              options = [];
              excludes = [];
            };

This fails with a:

 error: The option `perSystem.aarch64-darwin.treefmt.programs.typstfmt' does not exist. Definition values:
       - In `/nix/store/fa7bnw9lipymb026q4inj7f27yxcmnwb-source/flake.nix, via option perSystem':
           {
             command = "/nix/store/h5hwc836gma2g0v8mbhx40932pirfqsx-typstfmt-unstable-2023-08-06/bin/typstfmt";
             enable = true;
             excludes = [ ];
             options = [ ];
           ...

Describe the solution you'd like

Ideally it would just work :)

marijanp commented 1 year ago

It's possible. Here is how I added formolu:

treefmt = {
  projectRootFile = ".git/config";
  programs.nixpkgs-fmt.enable = true;
  programs.cabal-fmt.enable = true;
  settings.formatter = {
    "fourmolu" = {
      command = pkgs.haskellPackages.fourmolu;
      options = [
        "--ghc-opt"
        "-XImportQualifiedPost"
        "--ghc-opt"
        "-XTypeApplications"
        "--mode"
        "inplace"
        "--check-idempotence"
      ];
      includes = [ "*.hs" ];
    };
  };
};

You can figure it out by studying other formatter implementations. https://github.com/numtide/treefmt-nix/blob/19dee4bf6001849006a63f3435247316b0488e99/programs/ormolu.nix