Skip to content

Checking formatting using treefmt-nix on GitHub Actions

This tutorial explains how to check formatting on GitHub Actions using treefmt-nix.

Add a treefmt-nix configuration

Some of the flake templates, including treefmt, integrates treefmt-nix out of the box. You don't have to add a configuration in this case.

Otherwise, please add a configuration to your flake manually by consulting the documentation. They also provide a flake-parts module, which you can use to set up the thing more easily.

Once set up, your project should support reformatting the entire project by running the following command:

Terminal window
nix fmt

On CI, you can run the following command to check formatting without making modifications:

Terminal window
nix build ".#checks.$(nix eval --expr builtins.currentSystem --impure --raw).treefmt"

Add a GitHub workflow for running the check

  1. Initialize the template from the root directory of the project:

    Terminal window
    nix flake init github:akirak/flake-templates#meta
  2. Adjust the .github/workflows/nix-format.yml file to match your project requirements. In particular, you may have to tweak the triggers (in on section).

With the renovate configuration shipped by the template, the actions will be automatically updated.