-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
159 lines (146 loc) · 4.28 KB
/
Copy pathflake.nix
File metadata and controls
159 lines (146 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
description = "termio — A Go package for terminal I/O primitives (dev shell)";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
git-hooks = {
url = "github:cachix/git-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
git-hooks,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
devTools = with pkgs; [
go
gopls
gotools
golangci-lint
markdownlint-cli
delve
git
];
mkApp =
{
name,
description,
script,
}:
{
type = "app";
program = toString (pkgs.writeShellScript name script);
meta = {
mainProgram = name;
inherit description;
};
};
mkTaggedRaceTest =
{
name,
description,
tags,
coverProfile,
}:
mkApp {
inherit name description;
script = ''
export GOTOOLCHAIN=local
go="${pkgs.go}/bin/go"
mapfile -t testpkgs < <("$go" list -tags=${tags} ./... | grep -vE '/examples(/|$)' || true)
if [ ''${#testpkgs[@]} -eq 0 ]; then
echo "go list: no test packages after filters (tags=${tags})" >&2
exit 1
fi
exec "$go" test -tags=${tags} -race -shuffle=on -covermode=atomic \
-coverpkg=./... -coverprofile=${coverProfile} -timeout 10m "''${testpkgs[@]}"
'';
};
pre-commit-check = git-hooks.lib.${system}.run {
src = ./.;
hooks = {
golangci-lint = {
enable = true;
extraPackages = [ pkgs.go ];
};
markdownlint = {
enable = true;
excludes = [ "node_modules" ];
settings.configuration = builtins.fromJSON (builtins.readFile ./.markdownlint.json);
};
go-mod-tidy = {
enable = true;
name = "go-mod-tidy";
entry = "${pkgs.go}/bin/go mod tidy";
files = "(\\.go|go\\.mod|go\\.sum)$";
pass_filenames = false;
};
nixfmt.enable = true;
};
};
in
{
formatter = pkgs.nixfmt-tree;
checks = {
pre-commit = pre-commit-check;
};
devShells.default = pkgs.mkShell {
name = "termio";
packages = devTools ++ pre-commit-check.enabledPackages;
env = {
GO111MODULE = "on";
CGO_ENABLED = "1";
};
shellHook = ''
${pre-commit-check.shellHook}
export GOPATH="''${GOPATH:-$HOME/go}"
export PATH="$GOPATH/bin:$PATH"
echo "termio dev shell — $(go version)"
'';
};
apps = {
fmt = mkApp {
name = "fmt";
description = "Format Go files (gofumpt + gci via golangci-lint)";
script = ''
exec ${pkgs.golangci-lint}/bin/golangci-lint fmt ./...
'';
};
tidy = mkApp {
name = "tidy";
description = "Run go mod tidy for the module";
script = ''
exec ${pkgs.go}/bin/go mod tidy
'';
};
lint = mkApp {
name = "lint";
description = "Run golangci-lint";
script = ''
exec ${pkgs.golangci-lint}/bin/golangci-lint run ./...
'';
};
lint-md = mkApp {
name = "lint-md";
description = "Lint Markdown files with markdownlint";
script = ''
exec ${pkgs.markdownlint-cli}/bin/markdownlint '**/*.md'
'';
};
test-unit = mkTaggedRaceTest {
name = "test-unit";
description = "Run unit tests with race detector; write coverage-unit.out";
tags = "!integration";
coverProfile = "coverage-unit.out";
};
};
}
);
}