From d522b228b302b750d58b56401b5cf8ac6d69d697 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 4 Apr 2024 16:11:37 -0700 Subject: [PATCH] Add `nixfmt` Change-Id: I7f21695e3971cfd02b2cce0dd016ff6eb3389905 --- flake.nix | 11 +++++-- nix-support/nixfmt.nix | 65 ++++++++++++++++++++++++++++++++++++++++++ treefmt.toml | 4 +++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 nix-support/nixfmt.nix diff --git a/flake.nix b/flake.nix index 26540a995..77e34fa7f 100644 --- a/flake.nix +++ b/flake.nix @@ -71,6 +71,9 @@ }; overlays = [ (overlayFor (p: p.${stdenv})) + (final: prev: { + nixfmt = final.callPackage ./nix-support/nixfmt.nix {}; + }) ]; config.permittedInsecurePackages = [ "nix-2.13.6" ]; @@ -296,7 +299,11 @@ }; treefmt = { enable = true; - settings.formatters = [ ]; + settings.formatters = + let + pkgs = nixpkgsFor.${system}.native; + in + [ pkgs.nixfmt ]; }; }; }) pre-commit-hooks.lib; @@ -366,7 +373,7 @@ }).overrideAttrs (prev: { # Required for clang-tidy checks buildInputs = prev.buildInputs - ++ [ pkgs.just ] + ++ [ pkgs.just pkgs.nixfmt ] ++ lib.optional (pre-commit ? enabledPackages) pre-commit.enabledPackages ++ lib.optionals (stdenv.cc.isClang) [ pkgs.llvmPackages.llvm pkgs.llvmPackages.clang-unwrapped.dev ]; nativeBuildInputs = prev.nativeBuildInputs diff --git a/nix-support/nixfmt.nix b/nix-support/nixfmt.nix new file mode 100644 index 000000000..b51e26295 --- /dev/null +++ b/nix-support/nixfmt.nix @@ -0,0 +1,65 @@ +# Copy of `nixfmt-rfc-style` vendored from `nixpkgs` master: +# https://github.com/NixOS/nixpkgs/blob/ab6071eb54cc9b66dda436111d4f569e4e56cbf4/pkgs/by-name/ni/nixfmt-rfc-style/package.nix +{ + haskell, + haskellPackages, + fetchFromGitHub, +}: +let + inherit (haskell.lib.compose) justStaticExecutables; + raw-pkg = haskellPackages.callPackage ( + { + mkDerivation, + base, + cmdargs, + directory, + fetchzip, + filepath, + lib, + megaparsec, + mtl, + parser-combinators, + safe-exceptions, + scientific, + text, + transformers, + unix, + }: + mkDerivation { + pname = "nixfmt"; + version = "0.6.0-unstable-2024-03-14"; + src = fetchFromGitHub { + owner = "serokell"; + repo = "nixfmt"; + rev = "8d13b593fa8d8d6e5075f541f3231222a08e84df"; + hash = "sha256-HtXvzmfN4wk45qiKZ7V+/5WBV7jnTHfd7iBwF4XGl64="; + }; + isLibrary = true; + isExecutable = true; + libraryHaskellDepends = [ + base + megaparsec + mtl + parser-combinators + scientific + text + transformers + ]; + executableHaskellDepends = [ + base + cmdargs + directory + filepath + safe-exceptions + text + unix + ]; + jailbreak = true; + homepage = "https://github.com/serokell/nixfmt"; + description = "An opinionated formatter for Nix"; + license = lib.licenses.mpl20; + mainProgram = "nixfmt"; + } + ) { }; +in +justStaticExecutables raw-pkg diff --git a/treefmt.toml b/treefmt.toml index 81df72cc2..ff7d972be 100644 --- a/treefmt.toml +++ b/treefmt.toml @@ -1 +1,5 @@ [formatter] +# [formatter.nix] +# command = "nixfmt" +# includes = ["*.nix"] +# excludes = ["tests/**"]