-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflake.nix
287 lines (248 loc) · 8.31 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
{
description = "A Nix flake for SafeHaven dev shell";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
alejandra = {
url = "github:kamadorueda/alejandra/3.0.0";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
fenix,
alejandra,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [fenix.overlays.default];
};
# NodeJS environment
fixedNode = pkgs.nodejs_20;
fixedNodePackages = pkgs.nodePackages.override {
nodejs = fixedNode;
};
# Rust environment
rustVer = fenix.packages.${system}.complete;
rustChan = rustVer.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
];
checkProject = pkgs.writeShellScriptBin "check_project" ''
set -e
pushd backend
echo "::group::sqlx migrations checks"
echo "Create the database"
cargo sqlx database create
echo "Run the migrations"
cargo sqlx migrate run
echo "Check the migrations"
cargo sqlx prepare --check
echo "::endgroup::"
echo "::group::Backend tests"
cargo test
echo "::endgroup::"
echo "::group::Backend lint"
cargo fmt -- --check
cargo clippy -- -D warnings
echo "::endgroup::"
echo "::group::OpenAPI sync checks"
cargo run -- openapi ../frontend/calc-openapi.json
if ! diff ../frontend/calc-openapi.json ../frontend/openapi.json; then
echo "OpenAPI has changed, please run 'cargo run -- openapi ../frontend/openapi.json' in the frontend"
exit 1
fi
rm ../frontend/calc-openapi.json
echo "::endgroup::"
popd
pushd frontend
echo "::group::Frontend checks"
npm ci
npm run generate-api
npm run lint
echo "::endgroup::"
popd
'';
regenApi = pkgs.writeShellScriptBin "regen_api" ''
set -e
pushd backend
cargo run -- openapi ../frontend/openapi.json
popd
pushd frontend
npm run generate-api
popd
'';
nginxConfigFile = pkgs.writeText "" ''
daemon off;
error_log stderr info;
pid /tmp/nginx.pid;
worker_processes auto;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
access_log /dev/stdout;
server {
listen 4000;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /api {
proxy_pass http://localhost:28669;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
'';
processConfigFile = let
processes = {
backend = {
command = "cargo run -- serve";
working_dir = "./backend";
};
frontend = {
command = "npm run dev";
working_dir = "./frontend";
};
reverse = {
command = "${pkgs.nginx}/bin/nginx -c ${nginxConfigFile}";
};
};
in
pkgs.writeText "process.yaml" (builtins.toJSON {
version = "0.5";
processes = processes;
});
pgHba = pkgs.writeText "pg_hba.conf" ''
local all all trust
host all all 0.0.0.0/0 trust
host all all ::/0 trust
'';
pgInitScript = pkgs.writeTextFile {
name = "pg_init_script";
text = ''
#!/bin/sh
set -e
# Copy the pg_hba.conf file
cp /wanted_pg_hba.conf /var/lib/postgresql/data/pg_hba.conf
# Create the database
psql -U postgres -c "CREATE DATABASE safehaven;"
psql -U postgres -d safehaven -c "CREATE EXTENSION postgis;"
'';
executable = true;
};
startDockerPostgresql = pkgs.writeShellScriptBin "start_docker_postgresql" ''
set -e
docker run --rm -d \
--name safehaven-postgres \
-e POSTGRES_PASSWORD=postgres \
-v $PWD/.pgdata:/var/lib/postgresql/data \
-v ${pgHba}:/wanted_pg_hba.conf \
-v ${pgInitScript}:/docker-entrypoint-initdb.d/init.sh \
-p 5432:5432 \
postgis/postgis:16-3.4-alpine
'';
startDevEnv = pkgs.writeShellScriptBin "start_dev_env" ''
set -e
${pkgs.process-compose}/bin/process-compose -f ${processConfigFile}
'';
# Version when compiling the packages
version = builtins.readFile ./container_release;
# Backend derivation
backend =
(pkgs.makeRustPlatform {
cargo = rustVer.toolchain;
rustc = rustVer.toolchain;
})
.buildRustPackage rec {
inherit version;
name = "safehaven-backend";
src = ./backend;
# When modifying cargo dependencies, replace the hash with pkgs.lib.fakeHash
# then run `nix build .#backend`. Use the hash in the error to replace the value.
cargoHash = "sha256-ffaFgV5i4T0miX3k2uTiAU5P+zT7EHvXax9foZXTibQ=";
};
# Frontend derivation
frontend = pkgs.buildNpmPackage rec {
inherit version;
name = "safehaven-frontend";
src = ./frontend;
nodejs = fixedNode;
# When modifying npm dependencies, replace the hash with pkgs.lib.fakeHash
# then run `nix build .#frontend`. Use the hash in the error to replace the value.
npmDepsHash = "sha256-/sTtZfsGtbkiZNvY5QfsInABvtTjCmQYiii3mgqs9XU=";
installPhase = ''
runHook preInstall
mkdir -p $out/usr/share/safehaven/static
cp -rv dist/* $out/usr/share/safehaven/static
runHook postInstall
'';
};
# Docker image
dockerImage = pkgs.dockerTools.streamLayeredImage {
name = "ghcr.io/safehavenmaps/safehaven";
tag = version;
contents = [
backend
frontend
];
config = {
Cmd = ["/bin/safehaven" "serve"];
Env = [
"SH__SERVE_PUBLIC_PATH=/usr/share/safehaven/static"
];
ExposedPorts = {"28669/tcp" = {};};
};
};
in
with pkgs; {
packages = {inherit backend frontend dockerImage;};
devShells.default = mkShell {
buildInputs = [
rustChan
# Various scripts
checkProject
regenApi
startDevEnv
startDockerPostgresql
# Backend
sqlx-cli
# Front
fixedNode
# Nix formatting
alejandra.defaultPackage.${system}
# Process composing
process-compose
# PostgreSQL and PostGIS
(postgresql_16.withPackages (p: with p; [postgis]))
];
DATABASE_URL = "postgres://postgres:postgres@localhost:5432/safehaven";
};
}
);
}