tvl-depot/tools/depotfmt.nix
Vincent Ambo e459a6cf3b feat(tools/nixery): Absorb Nixery into depot
This absorbs a josh-filtered Nix subtree into depot, at
//tools/nixery.

This subtree was created through `josh-filter ':prefix=tools/nixery'`,
which allows a filter on tools/nixery to yield the same commit hashes
as the original Nixery repository (allowing for history continuity).

Change-Id: Icc1a99bf1248226b91f437b0a90361d36fb0d327
2022-04-20 16:04:17 +02:00

61 lines
1.6 KiB
Nix

# Builds treefmt for depot, with a hardcoded configuration that
# includes the right paths to formatters.
{ depot, pkgs, ... }:
let
# terraform fmt can't handle multiple paths at once, but treefmt
# expects this
terraformat = pkgs.writeShellScript "terraformat" ''
echo "$@" | xargs -n1 ${pkgs.terraform}/bin/terraform fmt
'';
config = pkgs.writeText "depot-treefmt-config" ''
[formatter.go]
command = "${pkgs.go}/bin/gofmt"
options = [ "-w" ]
includes = ["*.go"]
[formatter.tf]
command = "${terraformat}"
includes = [ "*.tf" ]
[formatter.nix]
command = "${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt"
includes = [ "*.nix" ]
excludes = [
"third_party/nix/tests/*",
"third_party/nix/src/tests/*",
"tools/nixery/*"
]
[formatter.rust]
command = "${pkgs.rustfmt}/bin/rustfmt"
includes = [ "*.rs" ]
excludes = [
"users/tazjin/*",
]
'';
# helper tool for formatting the depot interactively
depotfmt = pkgs.writeShellScriptBin "depotfmt" ''
exec ${pkgs.treefmt}/bin/treefmt ''${@} \
--config-file ${config} \
--tree-root $(${pkgs.git}/bin/git rev-parse --show-toplevel)
'';
# wrapper script for running formatting checks in CI
check = pkgs.writeShellScript "depotfmt-check" ''
${pkgs.treefmt}/bin/treefmt \
--clear-cache \
--fail-on-change \
--config-file ${config} \
--tree-root .
'';
in
depotfmt.overrideAttrs (_: {
passthru.meta.ci.extraSteps.check = {
label = "depot formatting check";
command = check;
alwaysRun = true;
};
})