From cf0d796bee87009f9228814a28b34395caa49fa9 Mon Sep 17 00:00:00 2001 From: Berwn Date: Tue, 16 Jun 2026 16:46:14 +0700 Subject: [PATCH] Add treefmt formatter (nix fmt + flake check gate) --- flake.lock | 23 ++++++++++++++++++++++- flake.nix | 40 +++++++++++++++++++++++++++------------- fmt.nix | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 fmt.nix diff --git a/flake.lock b/flake.lock index e44a41f..32b019e 100644 --- a/flake.lock +++ b/flake.lock @@ -166,7 +166,8 @@ "nixpkgs": [ "clan-core", "nixpkgs" - ] + ], + "treefmt-nix": "treefmt-nix_2" } }, "sops-nix": { @@ -225,6 +226,26 @@ "repo": "treefmt-nix", "type": "github" } + }, + "treefmt-nix_2": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1780220602, + "narHash": "sha256-eynAfOmbmxJnkp7YewvCEbShNnnYJ9gLLqkzsYtBPeM=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "db947814a175b7ca6ded66e21383d938df01c227", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 3999538..59c4da5 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,8 @@ { inputs.clan-core.url = "https://git.clan.lol/clan/clan-core/archive/25.11.tar.gz"; inputs.nixpkgs.follows = "clan-core/nixpkgs"; + inputs.treefmt-nix.url = "github:numtide/treefmt-nix"; + inputs.treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; outputs = { @@ -31,24 +33,36 @@ "age-plugin-fido2-hmac" ]; }; + + systems = [ + "x86_64-linux" + "aarch64-linux" + "aarch64-darwin" + "x86_64-darwin" + ]; + forAllSystems = nixpkgs.lib.genAttrs systems; + pkgsFor = system: clan-core.inputs.nixpkgs.legacyPackages.${system}; + treefmtFor = system: inputs.treefmt-nix.lib.evalModule (pkgsFor system) ./fmt.nix; in { inherit (clan.config) nixosConfigurations nixosModules clanInternals; clan = clan.config; + + # `nix fmt` and the `nix flake check` formatting gate. + formatter = forAllSystems (system: (treefmtFor system).config.build.wrapper); + checks = forAllSystems (system: { + formatting = (treefmtFor system).config.build.check self; + }); + # Add the Clan cli tool to the dev shell. # Use "nix develop" to enter the dev shell. - devShells = - nixpkgs.lib.genAttrs - [ - "x86_64-linux" - "aarch64-linux" - "aarch64-darwin" - "x86_64-darwin" - ] - (system: { - default = clan-core.inputs.nixpkgs.legacyPackages.${system}.mkShell { - packages = [ clan-core.packages.${system}.clan-cli ]; - }; - }); + devShells = forAllSystems (system: { + default = (pkgsFor system).mkShell { + packages = [ + clan-core.packages.${system}.clan-cli + (treefmtFor system).config.build.wrapper + ]; + }; + }); }; } diff --git a/fmt.nix b/fmt.nix new file mode 100644 index 0000000..9ecfa75 --- /dev/null +++ b/fmt.nix @@ -0,0 +1,32 @@ +# treefmt config, evaluated per-system in flake.nix and exposed as +# `nix fmt` (the formatter) plus a `nix flake check` formatting gate. +{ ... }: +{ + projectRootFile = "flake.nix"; + + programs = { + nixfmt.enable = true; + prettier.enable = true; + yamlfmt.enable = true; + shfmt.enable = true; + }; + + settings = { + on-unmatched = "fatal"; + global.excludes = [ + # Secrets and clan-managed state — never reformat. + "sops/*" + "vars/*" + "inventory.json" + + # Generated — don't reformat (regeneration would churn the diff). + "*facter.json" + + # No formatter, or reformatting would corrupt them. + "*.zone" # Knot zone files + "flake.lock" + ".envrc" + ".gitignore" + ]; + }; +}