Skip to content

Checking formatting using treefmt-nix on GitHub Actions

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

Some of the flake templates, including treefmt and flake-parts, integrate 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

Section titled “Add a GitHub workflow for running the check”
  1. Initialize the template from the root directory of the project:

    Terminal window
    nix flake init -t 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).

If you pin GitHub Actions in your workflow, use a dependency update tool such as Dependabot to keep those references current automatically.