-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
315 lines (284 loc) · 10.8 KB
/
flake.nix
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
description = "Sanitas Flake for reproducible builds and environments!";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
devenv = {
url = "github:cachix/devenv";
inputs.nixpkgs.follows = "nixpkgs";
};
};
nixConfig = {
extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
extra-substituters = "https://devenv.cachix.org";
};
outputs = {
self,
nixpkgs,
systems,
devenv,
...
} @ inputs: let
forEachSystem = nixpkgs.lib.genAttrs (import systems);
postgresPort = 6969;
postgresHost = "0.0.0.0";
strFromDBFile = file: builtins.readFile ./database/${file};
dbInitFile = builtins.concatStringsSep "\n" [(strFromDBFile "init.sql") (strFromDBFile "tables.sql") (strFromDBFile "inserts.sql")];
in {
packages = forEachSystem (
system: let
pkgs = import nixpkgs {inherit system;};
backendRequiredPkgs = [
pkgs.awscli2
pkgs.aws-sam-cli
];
in {
# For setting up devenv
devenv-up = self.devShells.${system}.default.config.procfileScript;
restartServices = pkgs.writeShellApplication {
name = "Sanitas dev server restarter";
runtimeInputs = with pkgs; [ansi];
text = ''
echo -e "$(ansi yellow)"WARNING:"$(ansi reset)" This script must be run on the project root directory!
echo "Trying to remove old .devenv..."
rm ./.devenv/state/postgres || rm -r ./.devenv/state/postgres || true
echo "Entering devshell..."
nix develop --impure . -c devenv up
'';
};
integrationTests = pkgs.writeShellApplication {
name = "Sanitas integration tests";
runtimeInputs = [pkgs.docker pkgs.process-compose pkgs.unixtools.ifconfig pkgs.coreutils] ++ backendRequiredPkgs;
text = let
PGDATA = ''"$PWD/.devenv/state/postgres"'';
startBackend = let
ipCommand =
if builtins.elem system ["x86_64-darwin" "aarch64-darwin"]
then "ifconfig en0 | grep 'inet ' | cut -d' ' -f2"
else "ip route get 1.2.3.4 | cut -d' ' -f7";
in ''
set -euo pipefail
cd sanitas_backend/
# echo Starting backend
# echo Getting ifconfig
# ifconfig en0
# echo Grepping for inet
# ifconfig en0 | grep 'inet '
# echo Using AWK
# awk --version
# echo -e "ifconfig en0 | grep 'inet ' | cut -d' ' -f2"
# ifconfig en0 | grep 'inet ' | cut -d' ' -f2
echo Building backend
sam build
sam local start-api --debug --add-host=hostpc:$(${ipCommand}) --warm-containers eager
'';
startPostgres = ''
set -euo pipefail
echo PWD = "$PWD"
echo PGDATA = ${PGDATA}
rm -rf "${PGDATA}"
mkdir -p "${PGDATA}"
export PATH=${pkgs.postgresql}/bin:${pkgs.coreutils}/bin
export PGDATA=${PGDATA}
export PGHOST=${PGDATA}
echo "The PGDATA variable is" $PGDATA
echo "The PGHOST variable is" $PGHOST
initdb --locale=C --encoding=UTF8
POSTGRES_RUN_INITIAL_SCRIPT="true"
echo
echo "PostgreSQL initdb process complete."
echo
# Setup pg_hba.conf
echo "Setting up pg_hba"
cp ${./pg_hba.conf} "${PGDATA}/pg_hba.conf"
echo "HBA setup complete!"
echo
echo "PostgreSQL is setting up the initial database."
echo
echo "Listing files"
ls ${PGDATA}
echo "Who am I? $(whoami)"
echo Starting server with command: pg_ctl -w start -o "-c unix_socket_directories=${PGDATA} -c listen_addresses=* -p ${builtins.toString postgresPort}"
pg_ctl -w start -o "-c unix_socket_directories=${PGDATA} -c listen_addresses=* -p ${builtins.toString postgresPort}"
echo "Initializing DB"
echo "${dbInitFile}" | psql --dbname postgres -p ${builtins.toString postgresPort}
echo "Sanitas postgres is now running!"
'';
stopPostgres = ''
set -euo pipefail
export PGDATA=${PGDATA}
export PGHOST=${PGDATA}
echo "The PGDATA variable is" $PGDATA
echo "The PGHOST variable is" $PGHOST
pg_ctl -m fast -w stop
'';
testBackend = "cd ./sanitas_backend/ && npm test -- --runInBand";
processComposeConfig = {
version = "0.5";
is_tui_disabled = true;
log_location = "./integration_tests.log";
processes = {
DB = {
command = startPostgres;
ready_log_line = "Sanitas postgres is now running!";
availability.restart = "exit_on_failure";
is_daemon = true;
shutdown = {
command = stopPostgres;
};
};
Backend = {
command = startBackend;
ready_log_line = "Running on http";
availability.restart = "exit_on_failure";
};
Test = {
command = testBackend;
availability = {
exit_on_end = true;
};
depends_on = {
DB = {
condition = "process_log_ready";
};
Backend = {
condition = "process_log_ready";
};
};
};
};
};
processComposeConfigFile = pkgs.writeText "SanitasProcessComposeConfig.yaml" (pkgs.lib.generators.toYAML {} processComposeConfig);
in ''
echo ${processComposeConfigFile}
timeout --kill-after=1m 7m process-compose -f ${processComposeConfigFile}
'';
};
buildJSDoc = pkgs.writeShellApplication {
name = "Sanitas JSdoc builder";
runtimeInputs = with pkgs; [nodePackages.jsdoc ansi];
text = ''
echo -e "$(ansi yellow)"WARNING:"$(ansi reset)" We need sudo to copy the font from the nix store!
sudo echo "Sudo thanks! ;-)"
echo -e "$(ansi yellow)"WARNING:"$(ansi reset)" This script must be run on the project root directory!
sudo jsdoc -c conf.json --recurse .
'';
};
}
);
devShells = forEachSystem (system: let
pkgs = import nixpkgs {inherit system;};
requiredPkgs = with pkgs; [
jq
];
frontendRequiredPkgs = with pkgs; [
nodejs_20
yarn-berry
];
backendRequiredPkgs = [
pkgs.awscli2
pkgs.aws-sam-cli
];
in {
cicdFrontend = pkgs.mkShell {
packages = requiredPkgs ++ frontendRequiredPkgs;
};
cicdBackend = pkgs.mkShell {
packages = requiredPkgs ++ backendRequiredPkgs;
};
default = devenv.lib.mkShell {
inherit pkgs inputs;
modules = [
{
packages =
[
# Load and Stress testing
pkgs.k6
# General
pkgs.nodePackages.jsdoc
# Database
pkgs.postgresql
pkgs.sqlfluff # SQL linter and formatter
]
++ requiredPkgs
++ frontendRequiredPkgs
++ backendRequiredPkgs;
process = {
process-compose = pkgs.lib.mkOptionDefault {
# tui = false;
};
};
services.postgres = {
enable = true;
listen_addresses = postgresHost;
port = postgresPort;
initialScript = dbInitFile;
hbaConf = builtins.readFile ./pg_hba.conf;
settings = {
log_connections = true;
log_statement = "all";
logging_collector = true;
log_disconnections = true;
log_destination = "stderr";
};
};
processes = {
frontend = {
exec = "cd sanitas_frontend/ && yarn && yarn dev";
};
storybook = {
exec = "cd sanitas_frontend/ && yarn && yarn storybook";
};
backend.exec = let
ipCommand =
if builtins.elem system ["x86_64-darwin" "aarch64-darwin"]
then "ifconfig en0 | grep 'inet ' | awk '{print $2}'"
else "ip route get 1.2.3.4 | awk '{print $7}'";
in "cd sanitas_backend/ && sam build && sam local start-api --debug --add-host=hostpc:$(${ipCommand})";
};
pre-commit = {
hooks = {
# Formatters
alejandra.enable = true; # Nix
mdformat = {
enable = true;
name = "mdformat";
description = "A common mark compliant markdown formatter";
files = "\.md$";
entry = "${pkgs.python310Packages.mdformat}/bin/mdformat";
};
sqlFormatter = {
enable = true;
name = "SQLFluff - Formatter";
description = "A multidialect SQL linter and formatter";
files = "\.sql$";
entry = "${pkgs.sqlfluff}/bin/sqlfluff format --dialect postgres";
};
yamlFormatter = {
enable = true;
name = "yamlfmt";
description = "Google Yaml formatter";
files = "\.ya?ml$";
entry = "${pkgs.yamlfmt}/bin/yamlfmt -formatter type=basic,max_line_length=65,include_document_start=true,retain_line_breaks_single=true,pad_line_comments=2";
};
# Linters
actionlint.enable = true;
yamllint.enable = true;
commitizen.enable = true;
markdownlint.enable = true;
statix.enable = true;
sqlLinter = {
enable = true;
name = "SQLFluff - Linter";
description = "A multidialect SQL linter and formatter";
files = "\.sql$";
entry = "${pkgs.sqlfluff}/bin/sqlfluff lint --dialect postgres";
};
};
};
}
];
};
});
};
}