Skip to content

Commit dd5198d

Browse files
SamirTalwarhasura-bot
authored andcommitted
Nix: Use nixpkgs-fmt to format everything.
You can now run `nix fmt` (or `make format-changed`) to reformat Nix files. This is not enforced by CI. PR-URL: hasura/graphql-engine-mono#4754 GitOrigin-RevId: a2e7cbe6c037d68ba6303278616314de60b6aa72
1 parent bcc34fa commit dd5198d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Makefile

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
HS_FILES = $(shell git ls-files '*.hs' '*.hs-boot' | grep -v '^contrib/')
44
CHANGED_HS_FILES = $(shell git diff --diff-filter=d --name-only `git merge-base HEAD origin/main` | grep '.*\(\.hs\|hs-boot\)$$' | grep -v '^contrib/')
55

6+
NIX_FILES = $(shell git ls-files '*.nix' 'nix/*.nix')
7+
68
SHELL_FILES = $(shell git ls-files '*.sh')
79
CHANGED_SHELL_FILES = $(shell git diff --diff-filter=d --name-only `git merge-base HEAD origin/main` | grep '.*\.sh$$')
810

911
HLINT = hlint
1012

13+
NIX_FMT = nixpkgs-fmt
14+
1115
ORMOLU_CHECK_VERSION = 0.3.0.0
1216
ORMOLU_ARGS = --cabal-default-extensions
1317
ORMOLU = ormolu
@@ -56,17 +60,39 @@ check-format-hs-changed: check-ormolu-version
5660
$(ORMOLU) $(ORMOLU_ARGS) --mode check $(CHANGED_HS_FILES); \
5761
fi
5862

63+
# We don't bother checking only changed *.nix files, as there's so few.
64+
65+
.PHONY: format-nix
66+
## format-nix: auto-format Nix source code using `nixpkgs-fmt`
67+
format-nix:
68+
@if command -v $(NIX_FMT) > /dev/null; then \
69+
echo "running $(NIX_FMT)"; \
70+
$(NIX_FMT) $(NIX_FILES); \
71+
else \
72+
echo "$(NIX_FMT) is not installed; skipping"; \
73+
fi
74+
75+
.PHONY: check-format-nix
76+
## check-format-nix: check Nix source code using `nixpkgs-fmt`
77+
check-format-nix:
78+
@if command -v $(NIX_FMT) > /dev/null; then \
79+
echo "running $(NIX_FMT) --check"; \
80+
$(NIX_FMT) --check $(NIX_FILES); \
81+
else \
82+
echo "$(NIX_FMT) is not installed; skipping"; \
83+
fi
84+
5985
.PHONY: format
60-
format: format-hs
86+
format: format-hs format-nix
6187

6288
.PHONY: format-changed
63-
format-changed: format-hs-changed
89+
format-changed: format-hs-changed format-nix
6490

6591
.PHONY: check-format
66-
check-format: check-format-hs
92+
check-format: check-format-hs check-format-nix
6793

6894
.PHONY: check-format-changed
69-
check-format-changed: check-format-hs-changed
95+
check-format-changed: check-format-hs-changed check-format-nix
7096

7197
.PHONY: lint-hs
7298
## lint-hs: lint Haskell code using `hlint`
@@ -83,13 +109,13 @@ lint-hs-changed:
83109
fi
84110

85111
.PHONY: lint-shell
86-
## lint-shell: lint shell scripts using `shellcheck`
112+
## lint-shell: lint shell scripts using `shellcheck`
87113
lint-shell:
88114
@echo running shellcheck
89115
@$(SHELLCHECK) $(SHELL_FILES)
90116

91117
.PHONY: lint-shell-changed
92-
## lint-shell-changed: lint shell scripts using `shellcheck` (changed files only)
118+
## lint-shell-changed: lint shell scripts using `shellcheck` (changed files only)
93119
lint-shell-changed:
94120
@echo running shellcheck
95121
@if [ -n "$(CHANGED_SHELL_FILES)" ]; then \

server/STYLE.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ existing code.
99

1010
We use [ormolu](https://github.com/tweag/ormolu) to format our code. The
1111
top-level `Makefile` has targets `format` and `check-format` that can be
12-
used for this.
12+
used for this. These targets will also format Nix files if `nixpkgs-fmt`
13+
is installed, but this is optional.
1314

1415
#### Line Length
1516

@@ -51,7 +52,7 @@ parseArgumentM userRoleM value = case userRoleM of
5152
Nothing -> ...
5253
```
5354

54-
Instead, where possible, prefer names that convey *why* the value is wrapped in
55+
Instead, where possible, prefer names that convey _why_ the value is wrapped in
5556
a `Maybe`, or, if `Nothing` is just an ordinary member of the value’s domain,
5657
don’t include any special indication in the name at all:
5758

@@ -294,6 +295,7 @@ newtype AuthenticationToken
294295
-- ^ Invariant: contains Base64-encoded binary data.
295296
}
296297
```
298+
297299
```haskell
298300
-- Good
299301
import Data.Text.Conversions (Base64)
@@ -303,7 +305,7 @@ newtype AuthenticationToken
303305
}
304306
```
305307

306-
However, *do* use comments on functions and datatypes to clarify information
308+
However, _do_ use comments on functions and datatypes to clarify information
307309
that cannot easily be communicated via names or types:
308310

309311
```haskell
@@ -359,6 +361,7 @@ runSelectQuery tables constraints cache shouldPrepare = do
359361
prepared <- if shouldPrepare then prepareQueryPlan plan else pure plan
360362
runQueryPlan prepared
361363
```
364+
362365
```haskell
363366
runSelectQuery tables constraints cache shouldPrepare =
364367
constructQuery >>= executeQuery
@@ -425,6 +428,7 @@ f = (g .) . h
425428
### Acknowledgement/Credits
426429

427430
Parts of this coding style guide have been adapted from:
431+
428432
- https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md
429433
- https://kowainik.github.io/posts/2019-02-06-style-guide
430434
- https://chrisdone.com/posts/german-naming-convention/

0 commit comments

Comments
 (0)