-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathflake.nix
More file actions
361 lines (325 loc) · 11 KB
/
flake.nix
File metadata and controls
361 lines (325 loc) · 11 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
{
description = "configurations for deploying catcolab";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
agenix.url = "github:ryantm/agenix";
deploy-rs.url = "github:serokell/deploy-rs";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
};
nixos-generators.url = "github:nix-community/nixos-generators";
};
outputs =
{
self,
nixpkgs,
deploy-rs,
fenix,
crane,
nixos-generators,
...
}@inputs:
let
# Linux-specific outputs (NixOS configurations and deploy)
linuxSystem = "x86_64-linux";
devShellSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
nixpkgsFor =
system:
import nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = [
# Override wasm-bindgen-cli to match version used in Cargo.lock
(final: prev: {
wasm-bindgen-cli = prev.buildWasmBindgenCli rec {
src = prev.fetchCrate {
pname = "wasm-bindgen-cli";
version = "0.2.106";
hash = "sha256-M6WuGl7EruNopHZbqBpucu4RWz44/MSdv6f0zkYw+44=";
};
cargoDeps = prev.rustPlatform.fetchCargoVendor {
inherit src;
inherit (src) pname version;
hash = "sha256-ElDatyOwdKwHg3bNH/1pcxKI7LXkhsotlDPQjiLHBwA=";
};
};
})
];
};
rustToolchainFor =
system: fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
sha256 = "sha256-vra6TkHITpwRyA5oBKAHSX0Mi6CBDNQD+ryPSpxFsfg=";
};
pkgsLinux = nixpkgsFor linuxSystem;
rustToolchainLinux = rustToolchainFor linuxSystem;
craneLib = (crane.mkLib pkgsLinux).overrideToolchain rustToolchainLinux;
cargoArtifacts = craneLib.buildDepsOnly {
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
nativeBuildInputs = [
pkgsLinux.pkg-config
];
buildInputs = [
pkgsLinux.openssl
];
};
# Generate devShells for each system
devShellForSystem =
system:
let
pkgs = nixpkgsFor system;
rustToolchain = rustToolchainFor system;
# macOS-specific configurations for libraries
darwinDeps =
if pkgs.stdenv.isDarwin then
[
pkgs.libiconv
]
else
[ ];
nightlyRustfmt = inputs.fenix.packages.${system}.latest.rustfmt;
in
pkgs.mkShell {
name = "catcolab-devshell";
RUSTFMT = "${nightlyRustfmt}/bin/rustfmt";
buildInputs =
with pkgs;
[
darkhttpd
esbuild
lld
netcat
nodejs_24
nix
openssl
pkg-config
pnpm
postgresql
python3
python312Packages.ipykernel
python312Packages.jupyter-core
python312Packages.jupyter-server
python312Packages.requests
python312Packages.websocket-client
rustToolchain
nightlyRustfmt
sqlx-cli
vscode-langservers-extracted
wasm-bindgen-cli
wasm-pack
]
++ darwinDeps
++ [
inputs.agenix.packages.${system}.agenix
inputs.deploy-rs.packages.${system}.default
];
# macOS-specific environment variables for OpenSSL and pkg-config
shellHook = ''
${
if pkgs.stdenv.isDarwin then
''
export OPENSSL_DIR=${pkgs.openssl.dev}
export OPENSSL_LIB_DIR=${pkgs.openssl.out}/lib
export OPENSSL_INCLUDE_DIR=${pkgs.openssl.dev}/include
export PKG_CONFIG_PATH=${pkgs.openssl.dev}/lib/pkgconfig:$PKG_CONFIG_PATH
''
else
""
}
export PATH=$PWD/infrastructure/scripts:$PATH
# Load DATABASE_URL into the environment
if [ -f packages/backend/.env ]; then
export $(grep -v '^#' packages/backend/.env | xargs)
fi
'';
};
# NOTE: this is not currently used, but was painful to build and might be useful in the future.
# Wraps the typical `deploy-rs.lib.${linuxSystem}.activate.nixos` activation function
# with a custom script that can run additional health checks. The script runs on the remote host
# and if it fails the whole deployment will fail.
# use:
# `deploy.nodes.${host}.profiles.system.path = healthcheckWrapper self.nixosConfigurations.host;`
healthcheckWrapper =
nixosConfiguration:
let
defaultNixos = deploy-rs.lib.${linuxSystem}.activate.nixos nixosConfiguration;
healthcheckWrapperScript = pkgsLinux.writeShellScriptBin "healthcheck-wrapper-script" ''
PROFILE=${defaultNixos}
# insert healthchecks
${defaultNixos}/deploy-rs-activate
'';
in
deploy-rs.lib.${linuxSystem}.activate.custom healthcheckWrapperScript
"./bin/healthcheck-wrapper-script";
in
{
# Create devShells for all supported systems
devShells = builtins.listToAttrs (
map (system: {
name = system;
value = {
default = devShellForSystem system;
};
}) devShellSystems
);
# Example of how to build and test individual package built by nix:
# nix build .#packages.x86_64-linux.automerge
# node ./result/main.cjs
packages = {
x86_64-linux = {
catcolabApi = pkgsLinux.callPackage ./infrastructure/catcolab-api.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
backend = pkgsLinux.callPackage ./packages/backend/default.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
migrator = pkgsLinux.callPackage ./packages/migrator/default.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
catlog-wasm-browser = pkgsLinux.callPackage ./packages/catlog-wasm/default.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
document-types-wasm = pkgsLinux.callPackage ./packages/document-types/default.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
julia-fhs = (pkgsLinux.callPackage ./infrastructure/julia.nix { }).julia-fhs;
frontend =
(pkgsLinux.callPackage ./packages/frontend/default.nix {
inherit inputs rustToolchainLinux self;
}).package;
frontend-tests =
(pkgsLinux.callPackage ./packages/frontend/default.nix {
inherit inputs rustToolchainLinux self;
}).tests;
rust-docs = pkgsLinux.callPackage ./infrastructure/rust-docs.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
};
rust-docs-check = pkgsLinux.callPackage ./infrastructure/rust-docs.nix {
inherit craneLib cargoArtifacts;
pkgs = pkgsLinux;
checkMode = true;
};
# VMs built with `nixos-rebuild build-vm` (like `nix build
# .#nixosConfigurations.catcolab-vm.config.system.build.vm`) are not the same
# as "traditional" VMs, which causes deploy-rs to fail when deploying to them.
# https://github.com/serokell/deploy-rs/issues/85#issuecomment-885782350
#
# This is worked around by creating a full featured VM image.
#
# use:
# nix build .#catcolab-vm
# cp result/catcolab-vm.qcow2 catcolab-vm.qcow2
# db-utils vm start
# deploy -s .#catcolab-vm
catcolab-vm = pkgsLinux.stdenv.mkDerivation {
name = "catcolab-vm";
src = nixos-generators.nixosGenerate {
system = "x86_64-linux";
format = "qcow";
modules = [
./infrastructure/hosts/catcolab-vm
];
specialArgs = {
inherit inputs self;
rustToolchain = rustToolchainLinux;
};
};
installPhase = ''
mkdir -p $out
cp $src/nixos.qcow2 $out/catcolab-vm.qcow2
'';
};
};
};
# Create a NixOS configuration for each host
nixosConfigurations = {
catcolab = nixpkgs.lib.nixosSystem {
specialArgs = {
inherit inputs self;
rustToolchain = rustToolchainLinux;
};
system = linuxSystem;
modules = [
./infrastructure/hosts/catcolab
];
pkgs = pkgsLinux;
};
catcolab-next = nixpkgs.lib.nixosSystem {
specialArgs = {
inherit inputs self;
rustToolchain = rustToolchainLinux;
};
system = linuxSystem;
modules = [
./infrastructure/hosts/catcolab-next
];
pkgs = pkgsLinux;
};
catcolab-vm = nixpkgs.lib.nixosSystem {
system = linuxSystem;
modules = [
./infrastructure/hosts/catcolab-vm
];
specialArgs = {
inherit inputs self;
rustToolchain = rustToolchainLinux;
};
};
};
deploy.nodes = {
catcolab = {
hostname = "backend.catcolab.org";
profiles.system = {
sshUser = "catcolab";
user = "root";
path = deploy-rs.lib.${linuxSystem}.activate.nixos self.nixosConfigurations.catcolab;
};
};
catcolab-next = {
hostname = "backend-next.catcolab.org";
profiles.system = {
sshUser = "catcolab";
user = "root";
path = deploy-rs.lib.${linuxSystem}.activate.nixos self.nixosConfigurations.catcolab-next;
};
};
catcolab-vm = {
hostname = "localhost";
fastConnection = true;
profiles.system = {
sshOpts = [
"-p"
"2221"
];
sshUser = "catcolab";
path = deploy-rs.lib.${linuxSystem}.activate.nixos self.nixosConfigurations.catcolab-vm;
user = "root";
};
};
};
checks.x86_64-linux.frontendTests = import ./infrastructure/tests/frontend.nix {
inherit
nixpkgs
inputs
self
linuxSystem
;
rustToolchain = rustToolchainLinux;
};
};
}