From 82ab779a54138fa8dca3dd8cd68117b6646e5817 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Mon, 24 Feb 2025 21:16:00 +0100 Subject: [PATCH] nixfmt-tree: init Motivation: https://github.com/NixOS/nixfmt/issues/273 --- pkgs/by-name/ni/nixfmt-tree/package.nix | 133 ++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 pkgs/by-name/ni/nixfmt-tree/package.nix diff --git a/pkgs/by-name/ni/nixfmt-tree/package.nix b/pkgs/by-name/ni/nixfmt-tree/package.nix new file mode 100644 index 000000000000..d16d79c67460 --- /dev/null +++ b/pkgs/by-name/ni/nixfmt-tree/package.nix @@ -0,0 +1,133 @@ +{ + lib, + runCommand, + buildEnv, + makeWrapper, + writers, + treefmt2, + nixfmt-rfc-style, + nixfmt-tree, + + settings ? { + # The default is warn, which would be too annoying for people who just care about Nix + on-unmatched = "info"; + # Assumes the user is using Git, fails if it's not + tree-root-file = ".git/index"; + + formatter.nixfmt = { + command = "nixfmt"; + includes = [ "*.nix" ]; + }; + }, + runtimePackages ? [ + nixfmt-rfc-style + ], +}: +buildEnv { + name = "nixfmt-tree"; + + # Allows this derivation to be used as a shell providing both treefmt and nixfmt + paths = [ treefmt2 ] ++ runtimePackages; + pathsToLink = [ "/bin" ]; + + nativeBuildInputs = [ + makeWrapper + ]; + + postBuild = '' + wrapProgram $out/bin/treefmt \ + --prefix PATH : $out/bin \ + --add-flags "--config-file ${writers.writeTOML "treefmt.toml" settings}" + ''; + + meta = { + mainProgram = "treefmt"; + description = "Official Nix formatter zero-setup starter using treefmt"; + longDescription = '' + A zero-setup [treefmt](https://treefmt.com/) starter to get started using the [official Nix formatter](https://github.com/NixOS/nixfmt). + + - For `nix fmt` to format all Nix files, add this to the `flake.nix` outputs: + + ```nix + formatter.''${system} = nixpkgs.legacyPackages.''${system}.nixfmt-tree; + ``` + + - The same can be done more efficiently with the `treefmt` command, + which you can get in `nix-shell`/`nix develop` by extending `mkShell` using + + ```nix + mkShell { + packages = [ pkgs.nixfmt-tree ]; + } + ``` + + You can then also use `treefmt` in a pre-commit/pre-push [Git hook](https://git-scm.com/docs/githooks) + and `nixfmt` with your editors format-on-save feature. + + - To check formatting in CI, run the following in a checkout of your Git repository: + ``` + treefmt --ci + ``` + + For more flexibility, you can customise this package using + ```nix + nixfmt-tree.override { + settings = { /* treefmt config */ }; + runtimePackages = [ /* List any formatters here */ ]; + } + ``` + + Alternatively you can switch to the more fully-featured [treefmt-nix](https://github.com/numtide/treefmt-nix). + ''; + # All the code is in this file, so same license as Nixpkgs + license = lib.licenses.mit; + maintainers = lib.teams.formatter.members; + platforms = lib.platforms.all; + }; + + passthru.tests.simple = runCommand "nixfmt-tree-test-simple" { } '' + export XDG_CACHE_HOME=$(mktemp -d) + cat > unformatted.nix < formatted.nix <