Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add formatting and linting #148

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake .#formatting -Lv --fallback
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.\#*
.~*
result*
.direnv
.pre-commit-config.yaml
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 | ✅ |
Expand Down Expand Up @@ -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))
Expand Down
67 changes: 66 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@
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: {
inherit nixpkgs nixpkgs-unstable system;
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 = {
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions ghc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -216,4 +216,4 @@ in
>&2 echo ""
>&2 echo " ${lib.concatStringsSep "\n " CONFIGURE_ARGS}"
'';
})
}