Skip to content

Gerschtli/nix-formatter-pack

Repository files navigation

Nix formatter pack

With this project you can combine the simplicity of nix fmt with several static code analysis and formatting tools.

Currently supported tools:

For the list of available options, see the manual.

Usage

Hint: To get started quickly, have a look at the provided nix templates in ./templates.

For nix fmt

Set the special flake output formatter like the following:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/release-22.05";
    nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
  };

  outputs = { self, nixpkgs, nix-formatter-pack }: {

    formatter.x86_64-linux = nix-formatter-pack.lib.mkFormatter {
      inherit nixpkgs;
      system = "x86_64-linux";
      # or a custom instance of nixpkgs:
      # pkgs = import nixpkgs { inherit system; };

      # extensible with custom modules:
      # extraModules = [ otherFlake.nixFormatterPackModules.default ];

      config = {
        # define custom formatters:
        # formatters.customFormatter.commandFn =
        #   { checkOnly, files, ... }:
        #   ''
        #     ${customFormatter}/bin/customFormatter ${if checkOnly then "--check" else "--fix"} ${files}
        #   '';

        tools = {
          deadnix.enable = true;
          nixpkgs-fmt.enable = true;
          statix.enable = true;
        };
      };
    };

  };
}

With this configuration, nix fmt will format your complete flake with the configured tools. nix fmt -- --check only reports if any tool found an issue and fails with a non-zero exit code if any issue has been found, recommended for usage in CI.

For more information, see nix fmt -- --help.

For nix flake check

It can also be used as a nix flake check like the following:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/release-22.05";
    nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
  };

  outputs = { self, nixpkgs, nix-formatter-pack }: {

    checks.x86_64-linux.nix-formatter-pack = nix-formatter-pack.lib.mkCheck {
      inherit nixpkgs;
      system = "x86_64-linux";
      # or a custom instance of nixpkgs:
      # pkgs = import nixpkgs { inherit system; };

      # extensible with custom modules:
      # extraModules = [ otherFlake.nixFormatterPackModules.default ];

      config = {
        # define custom formatters:
        # formatters.customFormatter.commandFn =
        #   { checkOnly, files, ... }:
        #   ''
        #     ${customFormatter}/bin/customFormatter ${if checkOnly then "--check" else "--fix"} ${files}
        #   '';

        tools = {
          deadnix.enable = true;
          nixpkgs-fmt.enable = true;
          statix.enable = true;
        };
      };

      # specify which files to check
      checkFiles = [ ./. ];
    };

  };
}

About

Collection of several nix formatters

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages