Skip to content

Commit

Permalink
Nix: Use nixpkgs-fmt to format everything.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SamirTalwar authored and hasura-bot committed Jun 20, 2022
1 parent bcc34fa commit dd5198d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
38 changes: 32 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
HS_FILES = $(shell git ls-files '*.hs' '*.hs-boot' | grep -v '^contrib/')
CHANGED_HS_FILES = $(shell git diff --diff-filter=d --name-only `git merge-base HEAD origin/main` | grep '.*\(\.hs\|hs-boot\)$$' | grep -v '^contrib/')

NIX_FILES = $(shell git ls-files '*.nix' 'nix/*.nix')

SHELL_FILES = $(shell git ls-files '*.sh')
CHANGED_SHELL_FILES = $(shell git diff --diff-filter=d --name-only `git merge-base HEAD origin/main` | grep '.*\.sh$$')

HLINT = hlint

NIX_FMT = nixpkgs-fmt

ORMOLU_CHECK_VERSION = 0.3.0.0
ORMOLU_ARGS = --cabal-default-extensions
ORMOLU = ormolu
Expand Down Expand Up @@ -56,17 +60,39 @@ check-format-hs-changed: check-ormolu-version
$(ORMOLU) $(ORMOLU_ARGS) --mode check $(CHANGED_HS_FILES); \
fi

# We don't bother checking only changed *.nix files, as there's so few.

.PHONY: format-nix
## format-nix: auto-format Nix source code using `nixpkgs-fmt`
format-nix:
@if command -v $(NIX_FMT) > /dev/null; then \
echo "running $(NIX_FMT)"; \
$(NIX_FMT) $(NIX_FILES); \
else \
echo "$(NIX_FMT) is not installed; skipping"; \
fi

.PHONY: check-format-nix
## check-format-nix: check Nix source code using `nixpkgs-fmt`
check-format-nix:
@if command -v $(NIX_FMT) > /dev/null; then \
echo "running $(NIX_FMT) --check"; \
$(NIX_FMT) --check $(NIX_FILES); \
else \
echo "$(NIX_FMT) is not installed; skipping"; \
fi

.PHONY: format
format: format-hs
format: format-hs format-nix

.PHONY: format-changed
format-changed: format-hs-changed
format-changed: format-hs-changed format-nix

.PHONY: check-format
check-format: check-format-hs
check-format: check-format-hs check-format-nix

.PHONY: check-format-changed
check-format-changed: check-format-hs-changed
check-format-changed: check-format-hs-changed check-format-nix

.PHONY: lint-hs
## lint-hs: lint Haskell code using `hlint`
Expand All @@ -83,13 +109,13 @@ lint-hs-changed:
fi

.PHONY: lint-shell
## lint-shell: lint shell scripts using `shellcheck`
## lint-shell: lint shell scripts using `shellcheck`
lint-shell:
@echo running shellcheck
@$(SHELLCHECK) $(SHELL_FILES)

.PHONY: lint-shell-changed
## lint-shell-changed: lint shell scripts using `shellcheck` (changed files only)
## lint-shell-changed: lint shell scripts using `shellcheck` (changed files only)
lint-shell-changed:
@echo running shellcheck
@if [ -n "$(CHANGED_SHELL_FILES)" ]; then \
Expand Down
10 changes: 7 additions & 3 deletions server/STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ existing code.

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

#### Line Length

Expand Down Expand Up @@ -51,7 +52,7 @@ parseArgumentM userRoleM value = case userRoleM of
Nothing -> ...
```

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

Expand Down Expand Up @@ -294,6 +295,7 @@ newtype AuthenticationToken
-- ^ Invariant: contains Base64-encoded binary data.
}
```

```haskell
-- Good
import Data.Text.Conversions (Base64)
Expand All @@ -303,7 +305,7 @@ newtype AuthenticationToken
}
```

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

```haskell
Expand Down Expand Up @@ -359,6 +361,7 @@ runSelectQuery tables constraints cache shouldPrepare = do
prepared <- if shouldPrepare then prepareQueryPlan plan else pure plan
runQueryPlan prepared
```

```haskell
runSelectQuery tables constraints cache shouldPrepare =
constructQuery >>= executeQuery
Expand Down Expand Up @@ -425,6 +428,7 @@ f = (g .) . h
### Acknowledgement/Credits

Parts of this coding style guide have been adapted from:

- https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md
- https://kowainik.github.io/posts/2019-02-06-style-guide
- https://chrisdone.com/posts/german-naming-convention/

0 comments on commit dd5198d

Please sign in to comment.