Skip to content

Code

Pre-requisites

You will need to have the following installed:

Important

We use a Flake-based workflow. You can certainly develop for treefmt without Flakes and leverage much of what is listed below, but it is left up to the reader to determine how to make that work.

Go development

The default devshell provides all the necessary tooling and utilities for working on treefmt.

nix/devshells/default.nix
{
  pkgs,
  perSystem,
  ...
}:
pkgs.mkShellNoCC {
  env.GOROOT = "${pkgs.go}/share/go";

  packages =
    (with pkgs; [
      go
      goreleaser
      golangci-lint
      delve
      pprof
      graphviz
      cobra-cli
      enumer
      perSystem.gomod2nix.default
    ])
    ++ # include formatters for development and testing
    (import ../packages/treefmt/formatters.nix pkgs);
}

Direnv should load this by default when entering the root of the repository.

For the most part, you should be able to develop normally as you would any other Go program.

Important

When you have completed making any changes and have tested it as you would any other Go program, it is important to ensure it works when run as a Nix package.

This can be done with nix run .# -- <args>, which will build the Nix derivation and execute the resultant treefmt binary.

Formatting

We use the latest released version of treefmt and treefmt-nix to format the repository by running nix fmt from the root directory.

nix/formatter.nix
{
  pkgs,
  inputs,
  ...
}:
inputs.treefmt-nix.lib.mkWrapper pkgs {
  projectRootFile = "flake.nix";

  programs = {
    alejandra.enable = true;
    deadnix.enable = true;
    gofumpt.enable = true;
    prettier.enable = true;
    statix.enable = true;
  };

  settings = {
    global.excludes = [
      "LICENSE"
      # let's not mess with the test folder
      "test/*"
      # unsupported extensions
      "*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore,pages}"
    ];

    formatter = {
      deadnix = {
        priority = 1;
      };

      statix = {
        priority = 2;
      };

      alejandra = {
        priority = 3;
      };

      prettier = {
        options = [
          "--tab-width"
          "4"
        ];
        includes = ["*.{css,html,js,json,jsx,md,mdx,scss,ts,yaml}"];
      };
    };
  };
}

Checks

Running nix flake check will build all the devshells and Nix packages, as well as check the formatting with treefmt and any other Flake checks that have been configured.

Documentation

When making changes, it is important to add or update any relevant sections in the documentation within the same pull request.

For more information see the next section.