diff --git a/default.nix b/default.nix index 1ef1189e8..1af82296a 100644 --- a/default.nix +++ b/default.nix @@ -72,4 +72,10 @@ rec { inherit postgresqlVersions; postgrestBuildEnv = env; }; + + style = + pkgs.callPackage nix/style.nix {}; + + lint = + pkgs.callPackage nix/lint.nix {}; } diff --git a/nix/README.md b/nix/README.md index fe24feedc..86d3de901 100644 --- a/nix/README.md +++ b/nix/README.md @@ -61,6 +61,20 @@ nix-shell --run postgrest-test-spec-postgresql-9.5.21 ``` +## Linting and styling code + +The nix-shell also contains scripts for linting and styling the PostgREST +source code: + +```bash +# Linting +nix-shell --run postgrest-lint + +# Styling / auto-formatting code +nix-shell --run postgrest-style + +``` + ## Tour The following is not required for working on PostgREST with Nix, but it will diff --git a/nix/lint.nix b/nix/lint.nix new file mode 100644 index 000000000..e498c03fb --- /dev/null +++ b/nix/lint.nix @@ -0,0 +1,12 @@ +{ writeShellScriptBin, git, silver-searcher, hlint }: + +writeShellScriptBin "postgrest-lint" + '' + set -euo pipefail + + rootdir="$(${git}/bin/git rev-parse --show-toplevel)" + + # Lint Haskell files + ${silver-searcher}/bin/ag -l -g '\.l?hs$' "$rootdir" \ + | xargs ${hlint}/bin/hlint -X QuasiQuotes -X NoPatternSynonyms + '' diff --git a/nix/style.nix b/nix/style.nix new file mode 100644 index 000000000..a8e4f5735 --- /dev/null +++ b/nix/style.nix @@ -0,0 +1,37 @@ +{ writeShellScriptBin +, git +, nixpkgs-fmt +, silver-searcher +, stylish-haskell +, buildEnv +}: +let + style = + writeShellScriptBin "postgrest-style" + '' + set -euo pipefail + + rootdir="$(${git}/bin/git rev-parse --show-toplevel)" + + # Format Nix files + ${nixpkgs-fmt}/bin/nixpkgs-fmt "$rootdir" > /dev/null 2> /dev/null + + # Format Haskell files + ${silver-searcher}/bin/ag -l -g '\.l?hs$' "$rootdir" \ + | xargs ${stylish-haskell}/bin/stylish-haskell -i + ''; + + check = + writeShellScriptBin "postgrest-style-check" + '' + set -euo pipefail + + ${style}/bin/${style.name} + + ${git}/bin/git diff-index --exit-code HEAD -- '*.hs' '*.lhs' '*.nix' + ''; +in +buildEnv { + name = "postgrest-style"; + paths = [ style check ]; +} diff --git a/shell.nix b/shell.nix index 0c89b90a7..5d2723330 100644 --- a/shell.nix +++ b/shell.nix @@ -9,6 +9,8 @@ pkgs.lib.overrideDerivation env ( pkgs.postgresql nixpkgsUpgrade tests + style + lint ]; shellHook =