Skip to content

srid/treefmt-nix

 
 

Repository files navigation

treefmt nix configuration

This repo is for users that use both Nix and treefmt.

It contains a mapping from programs to treefmt.toml formatter entries, maintained by the community.

Example

This returns a derivation that contains a treefmt binary wrapped with config. The passed configuration sets up terraform to format *.tf files:

{ nixpkgs, treefmt-nix, ... }:
treefmt-nix.lib.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_0;
  # Override the default settings generated by the above option
  settings.formatter.terraform.excludes = ["hello.tf"];
}

Usage

This repo contains a top-level default.nix that returns the library helper functions. The main function is mkWrapper, with the underlying utility functions also exposed for extended flexibility (eg: mkConfigFile, evalModule, all-modules, ...).

Nix classic

Import the repo using niv or some other tool.

Flakes

The library is exposes as the lib attribute. Eg:

{
  inputs.treefmt-nix.url = "github:numtide/treefmt-nix";

  outputs = { self, nixpkgs, treefmt-nix }: {
    formatter.x86_64-linux = treefmt-nix.lib.mkWrapper
      nixpkgs.legacyPackages.x86_64-linux
      {
        projectRootFile = "flake.nix";
        programs.nixpkgs-fmt.enable = true;
      };
  };
}

flake-parts

This flake exposes a flake-parts module as well. To use it:

  1. Add inputs.treefmt-nix.flakeModule to the "imports" list of your flake-parts call.
  2. Add treefmt = { .. } (containing the configuration above) to your "perSystem" section.
  3. Add config.treefmt.build.wrapper to the nativeBuildInputs of your devShell. This will make the treefmt command available in the shell using the specified configuration.
    • You can also use config.treefmt.build.programs to get access to the individual programs, which could be useful to provide them to your IDE or editor.

For an example, see haskell-template's flake.nix.

Supported programs

  • alejandra
  • black
  • cabal-fmt
  • elm-format
  • gofmt
  • gofumpt
  • nixpkgs-fmt
  • ormolu
  • prettier
  • purs-tidy
  • rufo
  • rustfmt
  • scalafmt
  • shellcheck
  • shfmt
  • stylish-haskell
  • stylua
  • terraform

Adding new formatters

To add a new formatter, look in the programs/ folder to see how those are structured.

We happily accept new PR as long as they adhere to the formatter spec. In terms of defaults, we don't try to be edgy. Please pick defaults that are standard in your community.

License

MIT

About

treefmt nix configuration

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Nix 99.4%
  • Shell 0.6%