From 6f8bccb94e00cce62b307b584b720d88f85f7d58 Mon Sep 17 00:00:00 2001 From: mangoiv Date: Mon, 6 Mar 2023 17:55:40 +0100 Subject: [PATCH] [feat] add formatting and linting - formatting checks - linting checks - pre-commit-hooks - corresponding devShell - CI entry --- .envrc | 1 + .github/workflows/ci.yml | 6 ++-- .gitignore | 2 ++ README.md | 13 ++++++-- flake.lock | 67 +++++++++++++++++++++++++++++++++++++++- flake.nix | 32 +++++++++++++++++-- ghc.nix | 14 ++++----- 7 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5cf2c26 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake .#formatting -Lv --fallback diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e44108e..0927359 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,9 @@ jobs: name: ghc-nix signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' + - name: Check nix flake + run: nix flake check ghc.nix# -Lv --impure --fallback + - name: Run nix-shell - Boot and Configure run: nix-shell --pure ghc.nix/shell.nix --command "./boot && configure_ghc" @@ -68,6 +71,3 @@ jobs: - name: Run nix develop - Test GHC (by running a testsuite subset) run: nix develop -Lv --fallback ghc.nix# -c bash -c "hadrian/build -j --flavour=quickest test --test-root-dirs=testsuite/tests/programs" - - - name: check formatting - run: nix build -Lv --fallback ghc.nix#formatter.x86_64-linux diff --git a/.gitignore b/.gitignore index 66e03e5..a6b72ff 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .\#* .~* result* +.direnv +.pre-commit-config.yaml diff --git a/README.md b/README.md index 4eab749..c13bc60 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,6 @@ The cache contains Linux x64 binaries of all packages that are used during a def To format all nix code in this repository, run `nix fmt`, to enter a development shell, run `nix develop`. - To change the settings of the `devShell` to your liking, just adjust the `userSettings` attribute-set in the top-level flake. -- To format all nix code in this repo, run `nix fmt`, to enter a development shell, run `nix develop`. ## Legacy nix-commands support @@ -167,7 +166,7 @@ import ./path/to/ghc.nix/shell.nix { ``` be careful to specify the path to the `shell.nix`, not to the `default.nix`. -| attribute-name | description | default | orchestrated `flake.nix` | +| attribute-name | description | default | orchestrated by nix flake | | -- | -- | -- | -- | | `system` | the system this is run on | `builtins.currentSystem` or flake system | ✅ | | `nixpkgs` | the stable `nixpkgs` set used | `nixpkgs` as pinned in the lock-file | ✅ | @@ -198,6 +197,16 @@ there and it should work. (*Note*: at the time of writing `.direnv` is not part of the `.gitignore` in `ghc`, so be careful to not accidentally check it out, it's the local cache of your development shell which makes loading it upon entering the directory instant) +## contributing + +- we check formatting and linting in our CI, so please be careful to run `nix flake check --allow-import-from-derivation --impure` + before submitting changes as a PR +- the tooling to run the linting is provided by a nix `devShell` which you can easily obtain by running `nix develop .#formatting`. + Now you only have to run `pre-commit run --all` to check for linting and to reformat; using this `devShell`, the formatting + will also be checked before committing. You can skip the check by passing `--no-verify` to the `git commit` command +- `ghc.nix` also offers `direnv` integration, so if you have it installed, just run `direnv allow` to automatically load the + formatting `devShell` and the accompanying pre-commit hook. + ## TODO - We currently can't just invoke `nix-build` ([#1](https://github.com/alpmestan/ghc.nix/issues/1)) diff --git a/flake.lock b/flake.lock index f1c5d9e..06c63ed 100644 --- a/flake.lock +++ b/flake.lock @@ -33,6 +33,42 @@ "type": "github" } }, + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660459072, + "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1669101869, @@ -65,12 +101,41 @@ "type": "github" } }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs-unstable" + ], + "nixpkgs-stable": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1677832802, + "narHash": "sha256-XQf+k6mBYTiQUjWRf/0fozy5InAs03O1b30adCpWeXs=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "382bee738397ca005206eefa36922cc10df8a21c", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "all-cabal-hashes": "all-cabal-hashes", "flake-compat": "flake-compat", "nixpkgs": "nixpkgs", - "nixpkgs-unstable": "nixpkgs-unstable" + "nixpkgs-unstable": "nixpkgs-unstable", + "pre-commit-hooks": "pre-commit-hooks" } } }, diff --git a/flake.nix b/flake.nix index bea1347..d027b94 100644 --- a/flake.nix +++ b/flake.nix @@ -13,10 +13,21 @@ url = "github:commercialhaskell/all-cabal-hashes/hackage"; flake = false; }; + + pre-commit-hooks = { + url = "github:cachix/pre-commit-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs-unstable"; + inputs.nixpkgs-stable.follows = "nixpkgs"; + inputs.flake-compat.follows = "flake-compat"; + }; }; - outputs = { self, nixpkgs, nixpkgs-unstable, all-cabal-hashes, ... }: with nixpkgs.lib; let - supportedSystems = systems.flakeExposed; + outputs = { nixpkgs, nixpkgs-unstable, all-cabal-hashes, pre-commit-hooks, ... }: with nixpkgs.lib; let + supportedSystems = + # allow nix flake show and nix flake check when passing --impure + if builtins.hasAttr "currentSystem" builtins + then [ builtins.currentSystem ] + else nixpkgs.lib.systems.flakeExposed; perSystem = genAttrs supportedSystems; defaultSettings = system: { @@ -24,6 +35,16 @@ all-cabal-hashes = all-cabal-hashes.outPath; }; + pre-commit-check = system: pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + nixpkgs-fmt.enable = true; + statix.enable = true; + deadnix.enable = true; + typos.enable = true; + }; + }; + # NOTE: change this according to the settings allowed in the ./ghc.nix file and described # in the `README.md` userSettings = { @@ -35,8 +56,13 @@ devShells = perSystem (system: rec { ghc-nix = import ./ghc.nix (defaultSettings system // userSettings); default = ghc-nix; + + formatting = nixpkgs.legacyPackages.${system}.mkShell { + inherit (pre-commit-check system) shellHook; + }; }); - formatter = perSystem (system: (import nixpkgs { inherit system; }).nixpkgs-fmt); + + checks = perSystem (system: { formatting = pre-commit-check system; }); # NOTE: this attribute is used by the flake-compat code to allow passing arguments to ./ghc.nix legacy = args: import ./ghc.nix (defaultSettings args.system // args); diff --git a/ghc.nix b/ghc.nix index 17bb5b9..a0d3383 100644 --- a/ghc.nix +++ b/ghc.nix @@ -38,8 +38,8 @@ let inherit all-cabal-hashes; overrides = self.lib.composeExtensions - (old.overrides or (_: _: {})) - (hself: hsuper: { + (old.overrides or (_: _: { })) + (_hself: hsuper: { ormolu = if self.system == "aarch64-darwin" then @@ -72,7 +72,7 @@ let if useClang then pkgs.clangStdenv else pkgs.stdenv; - noTest = pkg: haskell.lib.dontCheck pkg; + noTest = haskell.lib.dontCheck; hspkgs = haskell.packages.${bootghc}.override { inherit all-cabal-hashes; @@ -167,8 +167,8 @@ let librarySystemDepends = depsSystem; }); in -(hspkgs.shellFor rec { - packages = pkgset: [ hsdrv ]; +hspkgs.shellFor rec { + packages = _pkgset: [ hsdrv ]; nativeBuildInputs = depsTools; buildInputs = depsSystem; passthru.pkgs = pkgs; @@ -193,7 +193,7 @@ in ]; shellHook = '' - # somehow, CC gets overriden so we set it again here. + # somehow, CC gets overridden so we set it again here. export CC=${stdenv.cc}/bin/cc export GHC=$NIX_GHC export GHCPKG=$NIX_GHCPKG @@ -216,4 +216,4 @@ in >&2 echo "" >&2 echo " ${lib.concatStringsSep "\n " CONFIGURE_ARGS}" ''; -}) +}