From a1673cdc91ea51bc720cd55db99659b0d05b55bf Mon Sep 17 00:00:00 2001 From: adisbladis Date: Fri, 21 Jul 2023 13:23:16 +1200 Subject: [PATCH] Use treefmt-nix --- dev/treefmt.nix | 43 +++++++++++++++++++++++++++++++++++++++++++ flake.lock | 23 ++++++++++++++++++++++- flake.nix | 20 ++++---------------- shell.nix | 7 ------- treefmt.toml | 17 ----------------- 5 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 dev/treefmt.nix delete mode 100644 treefmt.toml diff --git a/dev/treefmt.nix b/dev/treefmt.nix new file mode 100644 index 0000000..6fb3b1a --- /dev/null +++ b/dev/treefmt.nix @@ -0,0 +1,43 @@ +{ pkgs, lib, ... }: { + # Used to find the project root + projectRootFile = "flake.lock"; + + programs.prettier.enable = true; + programs.prettier.package = pkgs.writeShellScriptBin "prettier" '' + export NODE_PATH=${pkgs.nodePackages.prettier-plugin-toml}/lib/node_modules + exec ${lib.getExe pkgs.nodePackages.prettier} "$@" + ''; + + programs.clang-format.enable = true; + + settings.formatter = { + nix = { + command = "sh"; + options = [ + "-eucx" + '' + ${pkgs.lib.getExe pkgs.nixpkgs-fmt} "$@" + '' + "--" + ]; + includes = [ "*.nix" ]; + excludes = [ ]; + }; + + clang-format = { }; + + prettier.includes = lib.mkForce [ "*.toml" ]; + + python = { + command = "sh"; + options = [ + "-eucx" + '' + ${pkgs.lib.getExe pkgs.python3.pkgs.black} "$@" + '' + "--" # this argument is ignored by bash + ]; + includes = [ "*.py" ]; + }; + }; +} diff --git a/flake.lock b/flake.lock index 8b2437f..1556e0c 100644 --- a/flake.lock +++ b/flake.lock @@ -39,7 +39,28 @@ "root": { "inputs": { "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1689620039, + "narHash": "sha256-BtNwghr05z7k5YMdq+6nbue+nEalvDepuA7qdQMAKoQ=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "719c2977f958c41fa60a928e2fbc50af14844114", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 097cffb..fadf545 100644 --- a/flake.nix +++ b/flake.nix @@ -4,6 +4,8 @@ inputs.nixpkgs.url = "github:NixOS/nixpkgs/master"; inputs.flake-parts.url = "github:hercules-ci/flake-parts"; inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; + inputs.treefmt-nix.url = "github:numtide/treefmt-nix"; + inputs.treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; nixConfig.extra-substituters = [ "https://cache.garnix.io" @@ -21,6 +23,7 @@ flake-parts.lib.mkFlake { inherit inputs; } { systems = inputs.nixpkgs.lib.systems.flakeExposed; + imports = [ inputs.treefmt-nix.flakeModule ]; perSystem = { pkgs, self', ... }: let devShell = self'.devShells.default; @@ -30,24 +33,9 @@ }; in { + treefmt.imports = [ ./dev/treefmt.nix ]; packages.nix-eval-jobs = pkgs.callPackage ./default.nix drvArgs; packages.clangStdenv-nix-eval-jobs = pkgs.callPackage ./default.nix (drvArgs // { stdenv = pkgs.clangStdenv; }); - - checks.treefmt = pkgs.stdenv.mkDerivation { - name = "treefmt-check"; - src = self; - nativeBuildInputs = devShell.nativeBuildInputs; - dontConfigure = true; - - inherit (devShell) NODE_PATH; - - buildPhase = '' - env HOME=$(mktemp -d) treefmt --fail-on-change - ''; - - installPhase = "touch $out"; - }; - packages.default = self'.packages.nix-eval-jobs; devShells.default = pkgs.callPackage ./shell.nix drvArgs; }; diff --git a/shell.nix b/shell.nix index a4bda27..c24738f 100644 --- a/shell.nix +++ b/shell.nix @@ -23,18 +23,11 @@ in pkgs.mkShell { inherit (nix-eval-jobs) buildInputs; nativeBuildInputs = nix-eval-jobs.nativeBuildInputs ++ [ - pkgs.treefmt - pkgs.llvmPackages.clang # clang-format - pkgs.nixpkgs-fmt - pkgs.nodePackages.prettier - (pkgs.python3.withPackages (ps: [ ps.pytest - ps.black ])) ]; - NODE_PATH = "${pkgs.nodePackages.prettier-plugin-toml}/lib/node_modules"; shellHook = lib.optionalString stdenv.isLinux '' export NIX_DEBUG_INFO_DIRS="${pkgs.curl.debug}/lib/debug:${nix.debug}/lib/debug''${NIX_DEBUG_INFO_DIRS:+:$NIX_DEBUG_INFO_DIRS}" diff --git a/treefmt.toml b/treefmt.toml deleted file mode 100644 index 647cb78..0000000 --- a/treefmt.toml +++ /dev/null @@ -1,17 +0,0 @@ -[formatter."c++"] -command = "clang-format" -options = ["-i"] -includes = ["*.c", "*.cpp", "*.cc", "*.h", "*.hpp"] - -[formatter.nix] -command = "nixpkgs-fmt" -includes = ["*.nix"] - -[formatter.toml] -command = "prettier" -options = ["--write"] -includes = ["*.toml"] - -[formatter.python] -command = "black" -includes = ["*.py"]