devenv icon indicating copy to clipboard operation
devenv copied to clipboard

Integrate treefmt-nix

Open bobvanderlinden opened this issue 2 years ago • 4 comments

treefmt is a CLI tool to format all files in a repository. It recurses through a directory and calls the right formatter based on the type of file it finds.

treefmt requires a configuration file that tells it what to execute for each file type. It doesn't include this configuration by default, so it takes some time to get the configuration right and requires knowledge of the different kinds of formatters that you'd like the use. Knowledge like which CLI flags to use.

treefmt-nix is a Nix module system to build a treefmt configuration. It includes options for most of the different formatters and allows just enabling them. The same kind of module system devenv uses.

For instance:

treefmt-nix.mkWrapper nixpkgs {
  # Used to find the project root
  projectRootFile = ".git/config";
  # Enable the terraform formatter
  programs.terraform.enable = true;
  # Override the default package
  programs.terraform.package = nixpkgs.terraform_1;
  # Override the default settings generated by the above option
  settings.formatter.terraform.excludes = [ "hello.tf" ];
}

It would be nice if this could be integrated into devenv, so that you could have options in devenv like:

services.treefmt.enable = true;
services.treefmt.programs.terraform.enable = true;

This probably requires adding treefmt-nix as a Nix flake input to devenv. Is this something we'd want?

bobvanderlinden avatar Apr 17 '23 07:04 bobvanderlinden

@zimbatm do you think this is a good idea?

bobvanderlinden avatar Apr 17 '23 07:04 bobvanderlinden

Sounds good! Probably the integration can be done similar to https://github.com/numtide/treefmt-nix/blob/main/flake-module.nix

zimbatm avatar Apr 17 '23 08:04 zimbatm

The perfect combination of nix flake + devenv + direnv saves 90% of the development environment configuration time for newcomers who have just entered the project.

After integrating treefmt, it will further increase to 99%.

The last 1% will be implemented in treefmt to "watch file changes and automate formats" obtained after the features.

I-Want-ToBelieve avatar Oct 14 '23 10:10 I-Want-ToBelieve

I took care of this integration in my last PR. Let me know if anything needs to be changed.

UseTheFork avatar Jun 24 '24 21:06 UseTheFork

This needs someone to pick up https://github.com/cachix/devenv/pull/1679#issuecomment-2674598566

domenkozar avatar Oct 20 '25 18:10 domenkozar

Added in https://github.com/cachix/devenv/pull/2238, thanks everyone ❤️

domenkozar avatar Oct 25 '25 21:10 domenkozar