diff --git a/flake.lock b/flake.lock index 6d665aa5..11212421 100644 --- a/flake.lock +++ b/flake.lock @@ -125,7 +125,8 @@ "fenix": "fenix", "flake-compat": "flake-compat", "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" } }, "rust-analyzer-src": { @@ -144,6 +145,26 @@ "repo": "rust-analyzer", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1760120816, + "narHash": "sha256-gq9rdocpmRZCwLS5vsHozwB6b5nrOBDNc2kkEaTXHfg=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "761ae7aff00907b607125b2f57338b74177697ed", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 7d68191a..a4d1886f 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,11 @@ flake = false; }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + # for default.nix flake-compat = { url = "github:edolstra/flake-compat?ref=master"; diff --git a/nix/default.nix b/nix/default.nix index b0f9c444..d20a3e02 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -6,5 +6,6 @@ ./tests ./hydra.nix + ./fmt.nix ]; } diff --git a/nix/fmt.nix b/nix/fmt.nix new file mode 100644 index 00000000..8cfb4581 --- /dev/null +++ b/nix/fmt.nix @@ -0,0 +1,37 @@ +{ inputs, ... }: +{ + # load the flake module from upstream + imports = [ inputs.treefmt-nix.flakeModule ]; + + perSystem = + { self', lib, ... }: + { + treefmt = { + # repo root as project root + projectRoot = inputs.self; + + # the formatters + programs = { + nixfmt.enable = true; + typos = { + enable = true; + configFile = "${inputs.self}/.typos.toml"; + }; + taplo = { + enable = true; + settings = lib.importTOML "${inputs.self}/taplo.toml"; + }; + }; + + settings.formatter.rustfmt = { + command = "${lib.getExe' self'.packages.dev-toolchain "rustfmt"}"; + includes = [ "**/*.rs" ]; + options = [ + "--unstable-features" + "--edition=2024" + "--config-path=${inputs.self}/rustfmt.toml" + ]; + }; + }; + }; +} diff --git a/taplo.toml b/taplo.toml new file mode 100644 index 00000000..19feb4d2 --- /dev/null +++ b/taplo.toml @@ -0,0 +1,6 @@ +[formatting] +align_entries = true # Align entries vertically. Entries that have table headers, comments, or blank lines between them are not aligned. + +reorder_arrays = true # Alphabetically reorder array values that are not separated by blank lines. +reorder_inline_tables = true # Alphabetically reorder inline tables. +reorder_keys = true # Alphabetically reorder keys that are not separated by blank lines.