Skip to content

Commit d58033c

Browse files
committed
Split pre-commit hooks into pure and impure
1 parent 5f7b320 commit d58033c

File tree

10 files changed

+127
-66
lines changed

10 files changed

+127
-66
lines changed

.last-exported-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Last exported commit from parent repo: 5449b680a91e43bb36b14f7364207614960375e8
1+
Last exported commit from parent repo: e283eef4787456402415cacd138093dbb080f812

src/Bootstrap/Data/Bootstrappable/FlakeNix.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ instance IsNixExpr FlakeNix where
183183
<> flakeNixExtraBindings
184184
)
185185
( ESet False $
186-
[[nixbinding|checks.pre-commit-check = preCommitHooks.hooks;|] | usingHooks]
186+
[[nixbinding|checks.pre-commit-check = preCommitHooks.pureHooks;|] | usingHooks]
187187
<> [ [nixbinding|devShell = self.devShells.${system}.default;|],
188188
[nixproperty|devShells.default|]
189189
|= mkShell

src/Bootstrap/Data/Bootstrappable/GitlabCIConfig.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ instance Bootstrappable GitlabCIConfig where
6262
" script: \""
6363
<> ( if gitlabCIConfigUseFlakes
6464
then "nix build '.#runChecks'"
65-
else "nix-build nix/pre-commit-hooks.nix -A hooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
65+
else "nix-build nix/pre-commit-hooks.nix -A pureHooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
6666
)
6767
<> "\""
6868
]

src/Bootstrap/Data/Bootstrappable/NixPreCommitHookConfig.hs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ import Bootstrap.Data.ProjectType
3030
)
3131
import Bootstrap.Nix.Expr
3232
( Binding (BNameValue),
33-
Expr (EGrouping, EIdent, EList, ELit, ESet, EWith),
33+
Expr (EGrouping, EIdent, ELetIn, EList, ELit, ESet, EWith),
3434
FunctionArgs (FASet),
3535
Identifier (Identifier),
3636
IsNixExpr (toNixExpr),
3737
Literal (LBool, LString),
3838
Property (PCons, PIdent),
3939
nix,
40+
nixargs,
4041
nixbinding,
4142
nixident,
4243
nixproperty,
@@ -45,6 +46,8 @@ import Bootstrap.Nix.Expr
4546
(|:),
4647
(|=),
4748
)
49+
import Control.Lens (Field2 (_2), filtered, folded, (^..))
50+
import Relude.Extra.Tuple (dup)
4851

4952
data NixPreCommitHookConfig = NixPreCommitHookConfig
5053
{ nixPreCommitHookConfigHooks :: [PreCommitHook],
@@ -64,25 +67,53 @@ instance IsNixExpr NixPreCommitHookConfig where
6467
:| [[nixident|nixpkgs|] | nixPreCommitHookConfigRequiresNixpkgs]
6568
<> [[nixident|system|] | nixPreCommitHookConfigUsingFlakeLib]
6669
)
67-
|: ESet
68-
False
69-
[ [nixproperty|hooks|] |= run |* ESet False runArgs,
70-
[nixproperty|tools|]
71-
|= if nixPreCommitHookConfigRequiresNixpkgs
72-
then defaultToolsExpr |++ nixpkgsToolsExpr
73-
else defaultToolsExpr
74-
]
70+
|: ELetIn
71+
( [nixbinding|# Function to make a set of pre-commit hooks|]
72+
:| [ [nixproperty|makeHooks|]
73+
|= ( [nixargs|hooks:|]
74+
|: ( run
75+
|* [nix|{
76+
inherit hooks;
77+
src = ../.;
78+
}|]
79+
)
80+
),
81+
[nixbinding|# Hooks which don't depend on running in a dev environment|],
82+
[nixproperty|pureHooks|] |= ESet False pureHooks,
83+
[nixbinding|# Hooks which can run on pre-commit but not in CI|],
84+
[nixproperty|impureHooks|] |= ESet False impureHooks
85+
]
86+
)
87+
( ESet
88+
False
89+
[ [nixbinding|pureHooks = makeHooks pureHooks;|],
90+
[nixbinding|allHooks = makeHooks (pureHooks // impureHooks);|],
91+
[nixproperty|tools|]
92+
|= if nixPreCommitHookConfigRequiresNixpkgs
93+
then defaultToolsExpr |++ nixpkgsToolsExpr
94+
else defaultToolsExpr
95+
]
96+
)
7597
where
98+
allHooks :: [(Bool, Binding)]
99+
allHooks =
100+
sort nixPreCommitHookConfigHooks
101+
<&> \case
102+
(_, Nothing) -> Nothing
103+
(a, Just b) -> Just (a, b)
104+
. bimap preCommitHookIsPure toBinding
105+
. dup
106+
& catMaybes
107+
pureHooks :: [Binding]
108+
impureHooks :: [Binding]
109+
(pureHooks, impureHooks) =
110+
let hooksByCondition c = allHooks ^.. folded . filtered c . _2
111+
in (hooksByCondition fst, hooksByCondition (not . fst))
76112
run :: Expr
77113
run =
78114
if nixPreCommitHookConfigUsingFlakeLib
79115
then [nix|pre-commit-hooks-lib.lib.${system}.run|]
80116
else [nix|pre-commit-hooks-lib.run|]
81-
runArgs :: [Binding]
82-
runArgs =
83-
[ [nixbinding|src = ../.;|],
84-
[nixproperty|hooks|] |= ESet False (catMaybes (toBinding <$> sort nixPreCommitHookConfigHooks))
85-
]
86117
mkToolsExpr :: Expr -> (PreCommitHookTool -> Bool) -> Expr
87118
mkToolsExpr parentSet condition =
88119
EGrouping . EWith parentSet $
@@ -160,6 +191,9 @@ preCommitHookToolIsFromNixpkgs :: PreCommitHookTool -> Bool
160191
preCommitHookToolIsFromNixpkgs (NixpkgsPreCommitHookTool _) = True
161192
preCommitHookToolIsFromNixpkgs _ = False
162193

194+
preCommitHookIsPure :: PreCommitHook -> Bool
195+
preCommitHookIsPure h = h /= elmReview
196+
163197
-- | Gets the attribute name within its containing set (not including the containing set)
164198
preCommitHookToolSubAttrName :: PreCommitHookTool -> Maybe Text
165199
preCommitHookToolSubAttrName (DefaultPreCommitHookTool attrName) = Just attrName

src/Bootstrap/Nix/Expr/MkShell.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ shellHookFor pchc pt = case (pchc, projectSuperType pt) of
5050

5151
shellHookBinding :: ShellHook -> Binding
5252
shellHookBinding = \case
53-
ShellHookFromPreCommit -> [nixbinding|inherit (preCommitHooks.hooks) shellHook;|]
53+
ShellHookFromPreCommit -> [nixbinding|inherit (preCommitHooks.allHooks) shellHook;|]
5454
x -> withComponents $ shellHookComponentBinding x
5555
where
5656
withComponents :: [Text] -> Binding
@@ -60,6 +60,6 @@ shellHookBinding = \case
6060
(LMultilineString $ mconcat (("\n " <>) <$> xs) <> "\n ")
6161
shellHookComponentBinding :: ShellHook -> [Text]
6262
shellHookComponentBinding = \case
63-
ShellHookFromPreCommit -> ["${preCommitHooks.hooks.shellHook}"]
63+
ShellHookFromPreCommit -> ["${preCommitHooks.allHooks.shellHook}"]
6464
ShellHookRust -> ["export RUST_SRC_PATH=${nixpkgs.rustPlatform.rustLibSrc}"]
6565
ShellHookCombined xs -> sconcat $ shellHookComponentBinding <$> xs

test/Bootstrap/Data/Bootstrappable/FlakeNixSpec.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ spec = describe "flake.nix rendering" do
134134
inherit pre-commit-hooks-lib system;
135135
};
136136
in {
137-
checks.pre-commit-check = preCommitHooks.hooks;
137+
checks.pre-commit-check = preCommitHooks.pureHooks;
138138
devShell = self.devShells.${system}.default;
139139
devShells.default = nixpkgs.mkShell {
140140
buildInputs =
@@ -144,7 +144,7 @@ spec = describe "flake.nix rendering" do
144144
nodejs
145145
rnix-lsp
146146
]);
147-
inherit (preCommitHooks.hooks) shellHook;
147+
inherit (preCommitHooks.allHooks) shellHook;
148148
};
149149
# runChecks is a hack required to allow checks to run on a single system
150150
# when using Import from Deviation (https://discourse.nixos.org/t/nix-flake-check-for-current-system-only/18366)
@@ -195,11 +195,11 @@ spec = describe "flake.nix rendering" do
195195
inherit pre-commit-hooks-lib nixpkgs system;
196196
};
197197
in {
198-
checks.pre-commit-check = preCommitHooks.hooks;
198+
checks.pre-commit-check = preCommitHooks.pureHooks;
199199
devShell = self.devShells.${system}.default;
200200
devShells.default = nixpkgs.mkShell {
201201
buildInputs = preCommitHooks.tools ++ (with nixpkgs; [go rnix-lsp]);
202-
inherit (preCommitHooks.hooks) shellHook;
202+
inherit (preCommitHooks.allHooks) shellHook;
203203
};
204204
defaultPackage = self.packages.${system}.default;
205205
packages.default = import nix/build.nix {
@@ -291,7 +291,7 @@ spec = describe "flake.nix rendering" do
291291
inherit pre-commit-hooks-lib system;
292292
};
293293
in {
294-
checks.pre-commit-check = preCommitHooks.hooks;
294+
checks.pre-commit-check = preCommitHooks.pureHooks;
295295
devShell = self.devShells.${system}.default;
296296
devShells.default = nixpkgs.mkShell {
297297
buildInputs = preCommitHooks.tools ++ (with nixpkgs; [libiconv rnix-lsp]);
@@ -302,7 +302,7 @@ spec = describe "flake.nix rendering" do
302302
];
303303
shellHook = ''
304304
export RUST_SRC_PATH=${nixpkgs.rustPlatform.rustLibSrc}
305-
${preCommitHooks.hooks.shellHook}
305+
${preCommitHooks.allHooks.shellHook}
306306
'';
307307
};
308308
defaultPackage = self.packages.${system}.default;

test/Bootstrap/Data/Bootstrappable/GitlabCIConfigSpec.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ check-dev-environment:
9898

9999
pre-commit-check:
100100
stage: build
101-
script: "nix-build nix/pre-commit-hooks.nix -A hooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
101+
script: "nix-build nix/pre-commit-hooks.nix -A pureHooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
102102

103103
build-site:
104104
stage: build
@@ -232,7 +232,7 @@ check-dev-environment:
232232

233233
pre-commit-check:
234234
stage: build
235-
script: "nix-build nix/pre-commit-hooks.nix -A hooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
235+
script: "nix-build nix/pre-commit-hooks.nix -A pureHooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
236236

237237
build:
238238
stage: build
@@ -260,6 +260,6 @@ check-dev-environment:
260260

261261
pre-commit-check:
262262
stage: build
263-
script: "nix-build nix/pre-commit-hooks.nix -A hooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
263+
script: "nix-build nix/pre-commit-hooks.nix -A pureHooks --arg pre-commit-hooks-lib 'import (import nix/sources.nix {}).pre-commit-hooks'"
264264
|]
265265
)

test/Bootstrap/Data/Bootstrappable/NixPreCommitHookConfigSpec.hs

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,34 @@ spec = describe "nix/pre-commit-hooks.nix rendering" do
3131
pre-commit-hooks-lib,
3232
nixpkgs,
3333
system,
34-
}: {
35-
hooks = pre-commit-hooks-lib.lib.${system}.run {
36-
src = ../.;
37-
hooks = {
38-
alejandra.enable = true;
39-
go-fmt = {
40-
enable = true;
41-
entry = "${nixpkgs.go}/bin/go fmt";
42-
files = "\\.go$";
43-
pass_filenames = false;
44-
};
45-
go-test = {
46-
enable = true;
47-
entry = "${nixpkgs.go}/bin/go test";
48-
files = "\\.(go|mod)$";
49-
pass_filenames = false;
50-
};
34+
}: let
35+
# Function to make a set of pre-commit hooks
36+
makeHooks = hooks:
37+
pre-commit-hooks-lib.lib.${system}.run {
38+
inherit hooks;
39+
src = ../.;
40+
};
41+
# Hooks which don't depend on running in a dev environment
42+
pureHooks = {
43+
alejandra.enable = true;
44+
go-fmt = {
45+
enable = true;
46+
entry = "${nixpkgs.go}/bin/go fmt";
47+
files = "\\.go$";
48+
pass_filenames = false;
49+
};
50+
go-test = {
51+
enable = true;
52+
entry = "${nixpkgs.go}/bin/go test";
53+
files = "\\.(go|mod)$";
54+
pass_filenames = false;
5155
};
5256
};
57+
# Hooks which can run on pre-commit but not in CI
58+
impureHooks = {};
59+
in {
60+
pureHooks = makeHooks pureHooks;
61+
allHooks = makeHooks (pureHooks // impureHooks);
5362
tools = (with pre-commit-hooks-lib.packages.${system}; [alejandra]) ++ (with nixpkgs; [go]);
5463
}
5564
|]
@@ -58,14 +67,23 @@ spec = describe "nix/pre-commit-hooks.nix rendering" do
5867
bootstrapContent (nixPreCommitHookConfigFor rcDefault $ Node NPM)
5968
>>= ( `shouldBe`
6069
Right
61-
[r|{pre-commit-hooks-lib}: {
62-
hooks = pre-commit-hooks-lib.run {
63-
src = ../.;
64-
hooks = {
65-
alejandra.enable = true;
66-
prettier.enable = true;
70+
[r|{pre-commit-hooks-lib}: let
71+
# Function to make a set of pre-commit hooks
72+
makeHooks = hooks:
73+
pre-commit-hooks-lib.run {
74+
inherit hooks;
75+
src = ../.;
6776
};
77+
# Hooks which don't depend on running in a dev environment
78+
pureHooks = {
79+
alejandra.enable = true;
80+
prettier.enable = true;
6881
};
82+
# Hooks which can run on pre-commit but not in CI
83+
impureHooks = {};
84+
in {
85+
pureHooks = makeHooks pureHooks;
86+
allHooks = makeHooks (pureHooks // impureHooks);
6987
tools = with pre-commit-hooks-lib; [alejandra prettier];
7088
}
7189
|]
@@ -81,19 +99,28 @@ spec = describe "nix/pre-commit-hooks.nix rendering" do
8199
[r|{
82100
pre-commit-hooks-lib,
83101
nixpkgs,
84-
}: {
85-
hooks = pre-commit-hooks-lib.run {
86-
src = ../.;
87-
hooks = {
88-
alejandra.enable = true;
89-
google-java-format = {
90-
enable = true;
91-
entry = "${nixpkgs.google-java-format}/bin/google-java-format -i";
92-
files = "\\.java$";
93-
pass_filenames = true;
94-
};
102+
}: let
103+
# Function to make a set of pre-commit hooks
104+
makeHooks = hooks:
105+
pre-commit-hooks-lib.run {
106+
inherit hooks;
107+
src = ../.;
108+
};
109+
# Hooks which don't depend on running in a dev environment
110+
pureHooks = {
111+
alejandra.enable = true;
112+
google-java-format = {
113+
enable = true;
114+
entry = "${nixpkgs.google-java-format}/bin/google-java-format -i";
115+
files = "\\.java$";
116+
pass_filenames = true;
95117
};
96118
};
119+
# Hooks which can run on pre-commit but not in CI
120+
impureHooks = {};
121+
in {
122+
pureHooks = makeHooks pureHooks;
123+
allHooks = makeHooks (pureHooks // impureHooks);
97124
tools = (with pre-commit-hooks-lib; [alejandra]) ++ (with nixpkgs; [google-java-format]);
98125
}
99126
|]

test/Bootstrap/Data/Bootstrappable/NixShellSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ in
8181
niv
8282
rnix-lsp
8383
]);
84-
inherit (preCommitHooks.hooks) shellHook;
84+
inherit (preCommitHooks.allHooks) shellHook;
8585
}
8686
|]
8787
)
@@ -158,7 +158,7 @@ in
158158
in
159159
nixpkgs.mkShell {
160160
buildInputs = preCommitHooks.tools ++ [pythonPackages] ++ (with nixpkgs; [niv rnix-lsp]);
161-
inherit (preCommitHooks.hooks) shellHook;
161+
inherit (preCommitHooks.allHooks) shellHook;
162162
}
163163
|]
164164
)

test/Bootstrap/Nix/Expr/MkShellSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spec = do
2929
mkShell (BuildInputSpec [] [] (PreCommitHooksConfig True) PSTMinimal [])
3030
`shouldBe` [nix|nixpkgs.mkShell {
3131
buildInputs = preCommitHooks.tools ++ (with nixpkgs; []);
32-
inherit (preCommitHooks.hooks) shellHook;
32+
inherit (preCommitHooks.allHooks) shellHook;
3333
}|]
3434
it "gives a proper shell hook for Rust projects" do
3535
mkShell (BuildInputSpec [] [] (PreCommitHooksConfig False) PSTRust [])
@@ -45,6 +45,6 @@ spec = do
4545
buildInputs = preCommitHooks.tools ++ (with nixpkgs; []);
4646
shellHook = ''
4747
export RUST_SRC_PATH=${nixpkgs.rustPlatform.rustLibSrc}
48-
${preCommitHooks.hooks.shellHook}
48+
${preCommitHooks.allHooks.shellHook}
4949
'';
5050
}|]

0 commit comments

Comments
 (0)