Use treefmt-nix

This commit is contained in:
adisbladis 2023-07-21 13:23:16 +12:00 committed by mergify[bot]
parent f88571cfc9
commit a1673cdc91
5 changed files with 69 additions and 41 deletions

43
dev/treefmt.nix Normal file
View file

@ -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" ];
};
};
}

View file

@ -39,7 +39,28 @@
"root": { "root": {
"inputs": { "inputs": {
"flake-parts": "flake-parts", "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"
} }
} }
}, },

View file

@ -4,6 +4,8 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs/master"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/master";
inputs.flake-parts.url = "github:hercules-ci/flake-parts"; inputs.flake-parts.url = "github:hercules-ci/flake-parts";
inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; 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 = [ nixConfig.extra-substituters = [
"https://cache.garnix.io" "https://cache.garnix.io"
@ -21,6 +23,7 @@
flake-parts.lib.mkFlake { inherit inputs; } flake-parts.lib.mkFlake { inherit inputs; }
{ {
systems = inputs.nixpkgs.lib.systems.flakeExposed; systems = inputs.nixpkgs.lib.systems.flakeExposed;
imports = [ inputs.treefmt-nix.flakeModule ];
perSystem = { pkgs, self', ... }: perSystem = { pkgs, self', ... }:
let let
devShell = self'.devShells.default; devShell = self'.devShells.default;
@ -30,24 +33,9 @@
}; };
in in
{ {
treefmt.imports = [ ./dev/treefmt.nix ];
packages.nix-eval-jobs = pkgs.callPackage ./default.nix drvArgs; packages.nix-eval-jobs = pkgs.callPackage ./default.nix drvArgs;
packages.clangStdenv-nix-eval-jobs = pkgs.callPackage ./default.nix (drvArgs // { stdenv = pkgs.clangStdenv; }); 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; packages.default = self'.packages.nix-eval-jobs;
devShells.default = pkgs.callPackage ./shell.nix drvArgs; devShells.default = pkgs.callPackage ./shell.nix drvArgs;
}; };

View file

@ -23,18 +23,11 @@ in
pkgs.mkShell { pkgs.mkShell {
inherit (nix-eval-jobs) buildInputs; inherit (nix-eval-jobs) buildInputs;
nativeBuildInputs = nix-eval-jobs.nativeBuildInputs ++ [ nativeBuildInputs = nix-eval-jobs.nativeBuildInputs ++ [
pkgs.treefmt
pkgs.llvmPackages.clang # clang-format
pkgs.nixpkgs-fmt
pkgs.nodePackages.prettier
(pkgs.python3.withPackages (ps: [ (pkgs.python3.withPackages (ps: [
ps.pytest ps.pytest
ps.black
])) ]))
]; ];
NODE_PATH = "${pkgs.nodePackages.prettier-plugin-toml}/lib/node_modules";
shellHook = lib.optionalString stdenv.isLinux '' 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}" export NIX_DEBUG_INFO_DIRS="${pkgs.curl.debug}/lib/debug:${nix.debug}/lib/debug''${NIX_DEBUG_INFO_DIRS:+:$NIX_DEBUG_INFO_DIRS}"

View file

@ -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"]