-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
416 lines (359 loc) · 12.8 KB
/
flake.nix
File metadata and controls
416 lines (359 loc) · 12.8 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = {nixpkgs, ...}: let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
android_sdk.accept_license = true;
};
};
lib = pkgs.lib;
androidSdk =
(pkgs.androidenv.composeAndroidPackages {
platformVersions = ["36" "35" "34"];
buildToolsVersions = ["35.0.0" "34.0.0"];
ndkVersions = ["27.0.12077973"];
includeNDK = true;
cmakeVersions = ["3.22.1"];
includeCmake = true;
includeEmulator = false;
}).androidsdk;
sdkRoot = "${androidSdk}/libexec/android-sdk";
shell = pkgs.mkShell {
name = "dev-shell";
packages = with pkgs; [
flutter
dart
androidSdk
android-tools
jdk17
just
rustc
cargo
clippy
rustfmt
pkg-config
mold
cargo-watch
sqlite
sqlx-cli
watchexec
github-copilot-cli
];
RUSTFLAGS = "-C link-arg=-fuse-ld=mold";
ANDROID_SDK_ROOT = sdkRoot;
ANDROID_HOME = sdkRoot;
JAVA_HOME = "${pkgs.jdk17}/lib/openjdk";
};
webBuild = pkgs.flutter.buildFlutterApplication {
pname = "blaz-web";
version = "0.1.0";
src = pkgs.lib.cleanSource ./flutter;
autoPubspecLock = ./flutter/pubspec.lock;
targetFlutterPlatform = "web";
};
package = pkgs.rustPlatform.buildRustPackage {
pname = "blaz";
version = "0.1.0";
src = ./backend;
cargoLock = {
lockFile = ./backend/Cargo.lock;
};
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = with pkgs; [
sqlite
openssl
];
# Copy web build before building rust
preBuild = ''
mkdir -p web_build
cp -r ${webBuild}/* web_build/
'';
doCheck = false;
meta = with lib; {
description = "Recipe manager backend";
homepage = "https://github.com/MathieuMoalic/blaz";
license = licenses.gpl3;
maintainers = [];
};
};
# Prebuilt binary from GitHub releases (faster, no compilation needed)
# NOTE: This always points to a previous stable release since we can't
# know the hash until after the release is built. This is intentional
# and keeps deployment fast while source builds always give you latest.
prebuiltPackage = pkgs.stdenvNoCC.mkDerivation rec {
pname = "blaz";
version = "2.7.4";
src = pkgs.fetchurl {
url = "https://github.com/MathieuMoalic/blaz/releases/download/v${version}/blaz-v${version}-x86_64-linux";
sha256 = "sha256-xWwqSFFhB+hhBR3SUces/QjYeb4Am9AIUHwk2idVq7Q=";
};
dontUnpack = true;
installPhase = ''
runHook preInstall
mkdir -p "$out/bin"
cp "$src" "$out/bin/blaz"
chmod +x "$out/bin/blaz"
runHook postInstall
'';
meta = with lib; {
description = "Recipe manager backend (prebuilt binary from latest release)";
homepage = "https://github.com/MathieuMoalic/blaz";
license = licenses.gpl3;
platforms = ["x86_64-linux"];
mainProgram = "blaz";
};
};
service = {
lib,
config,
pkgs,
...
}: let
cfg = config.services.blaz;
in {
options.services.blaz = {
enable = lib.mkEnableOption "Blaz recipe manager backend";
package = lib.mkOption {
type = lib.types.package;
default = package;
defaultText = lib.literalExpression "package (built from source)";
description = "The blaz package to use. Set to prebuiltPackage to use prebuilt binaries from GitHub releases.";
};
bindAddr = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1:8080";
description = "Address to bind the HTTP server to";
};
databasePath = lib.mkOption {
type = lib.types.str;
default = "/var/lib/blaz/blaz.sqlite";
description = "Path to SQLite database file";
};
mediaDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/blaz/media";
description = "Directory to store media files (recipe images)";
};
logFile = lib.mkOption {
type = lib.types.str;
default = "/var/lib/blaz/blaz.log";
description = "Path to log file";
};
corsOrigin = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "https://blaz.yourdomain.com";
description = "CORS allowed origin. If null, allows any origin";
};
verbosity = lib.mkOption {
type = lib.types.int;
default = 0;
description = "Log verbosity level (-2 to 3, where 0 is info)";
};
# Authentication options
passwordHash = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Argon2 password hash for authentication. Generate with: blaz hash-password";
};
passwordHashFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to file containing password hash (for use with sops-nix)";
};
jwtSecret = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "JWT secret for token signing. If not set, generates a random one (tokens won't persist across restarts)";
};
jwtSecretFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to file containing JWT secret (for use with sops-nix)";
};
# LLM options
llmApiKey = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "LLM API key for recipe parsing and macro estimation";
};
llmApiKeyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to file containing LLM API key (for use with sops-nix)";
};
llmApiUrl = lib.mkOption {
type = lib.types.str;
default = "https://openrouter.ai/api/v1";
description = "LLM API URL";
};
systemPromptImport = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Custom system prompt for recipe import (uses default if not set)";
};
systemPromptMacros = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Custom system prompt for macro estimation (uses default if not set)";
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.passwordHash != null || cfg.passwordHashFile != null;
message = "services.blaz.passwordHash or services.blaz.passwordHashFile must be set";
}
{
assertion = !(cfg.passwordHash != null && cfg.passwordHashFile != null);
message = "services.blaz.passwordHash and services.blaz.passwordHashFile are mutually exclusive";
}
{
assertion = !(cfg.jwtSecret != null && cfg.jwtSecretFile != null);
message = "services.blaz.jwtSecret and services.blaz.jwtSecretFile are mutually exclusive";
}
{
assertion = !(cfg.llmApiKey != null && cfg.llmApiKeyFile != null);
message = "services.blaz.llmApiKey and services.blaz.llmApiKeyFile are mutually exclusive";
}
];
users.users.blaz = {
isSystemUser = true;
group = "blaz";
home = "/var/lib/blaz";
createHome = true;
};
users.groups.blaz = {};
# Create directories before service starts (required due to ProtectSystem = strict)
systemd.tmpfiles.rules = [
# Database directory
"d ${dirOf cfg.databasePath} 0750 blaz blaz - -"
# Media directory
"d ${cfg.mediaDir} 0750 blaz blaz - -"
# Log directory and file
"d ${dirOf cfg.logFile} 0750 blaz blaz - -"
"f ${cfg.logFile} 0640 blaz blaz - -"
];
systemd.services.blaz = {
description = "Blaz recipe manager backend";
after = ["network.target"];
wantedBy = ["multi-user.target"];
# Basic environment variables (non-secrets)
environment =
{
BLAZ_BIND_ADDR = cfg.bindAddr;
BLAZ_DATABASE_PATH = cfg.databasePath;
BLAZ_MEDIA_DIR = cfg.mediaDir;
BLAZ_LOG_FILE = cfg.logFile;
BLAZ_LLM_API_URL = cfg.llmApiUrl;
}
// lib.optionalAttrs (cfg.corsOrigin != null) {
BLAZ_CORS_ORIGIN = cfg.corsOrigin;
}
// lib.optionalAttrs (cfg.passwordHash != null) {
BLAZ_PASSWORD_HASH = cfg.passwordHash;
}
// lib.optionalAttrs (cfg.jwtSecret != null) {
BLAZ_JWT_SECRET = cfg.jwtSecret;
}
// lib.optionalAttrs (cfg.llmApiKey != null) {
BLAZ_LLM_API_KEY = cfg.llmApiKey;
}
// lib.optionalAttrs (cfg.systemPromptImport != null) {
BLAZ_SYSTEM_PROMPT_IMPORT = cfg.systemPromptImport;
}
// lib.optionalAttrs (cfg.systemPromptMacros != null) {
BLAZ_SYSTEM_PROMPT_MACROS = cfg.systemPromptMacros;
};
# Script to load secrets from files before starting
script = let
# Load password hash from file if specified
passwordHashLoader =
if cfg.passwordHashFile != null
then ''export BLAZ_PASSWORD_HASH="$(cat ${cfg.passwordHashFile})"''
else "";
# Load JWT secret from file if specified
jwtSecretLoader =
if cfg.jwtSecretFile != null
then ''export BLAZ_JWT_SECRET="$(cat ${cfg.jwtSecretFile})"''
else "";
# Load LLM API key from file if specified
llmApiKeyLoader =
if cfg.llmApiKeyFile != null
then ''export BLAZ_LLM_API_KEY="$(cat ${cfg.llmApiKeyFile})"''
else "";
in ''
${passwordHashLoader}
${jwtSecretLoader}
${llmApiKeyLoader}
exec ${cfg.package}/bin/blaz \
${lib.concatStringsSep " " (
lib.optionals (cfg.verbosity > 0) (lib.genList (_: "-v") cfg.verbosity)
++ lib.optionals (cfg.verbosity < 0) (lib.genList (_: "-q") (- cfg.verbosity))
)}
'';
serviceConfig = {
WorkingDirectory = "/var/lib/blaz";
User = "blaz";
Group = "blaz";
StateDirectory = "blaz";
LogsDirectory = "blaz";
RuntimeDirectory = "blaz";
RuntimeDirectoryMode = "0750";
Restart = "always";
RestartSec = "5s";
CapabilityBoundingSet = "";
RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6";
SystemCallFilter = "~@clock @cpu-emulation @keyring @module @obsolete @raw-io @reboot @swap @resources @privileged @mount @debug";
NoNewPrivileges = "yes";
ProtectClock = "yes";
ProtectKernelLogs = "yes";
ProtectControlGroups = "yes";
ProtectKernelModules = "yes";
SystemCallArchitectures = "native";
RestrictNamespaces = "yes";
RestrictSUIDSGID = "yes";
ProtectHostname = "yes";
ProtectKernelTunables = "yes";
RestrictRealtime = "yes";
ProtectProc = "invisible";
PrivateUsers = "yes";
LockPersonality = "yes";
UMask = "0027";
RemoveIPC = "yes";
ProtectHome = "yes";
PrivateTmp = "yes";
ProtectSystem = "strict";
ProcSubset = "pid";
SocketBindAllow = let
port = lib.last (lib.splitString ":" cfg.bindAddr);
in ["tcp:${port}"];
SocketBindDeny = "any";
LimitNOFILE = 4096;
LimitNPROC = 128;
MemoryMax = "512M";
TasksMax = 256;
ReadWritePaths = [
cfg.mediaDir
(dirOf cfg.databasePath)
];
};
};
};
};
in {
devShells.${system}.default = shell;
nixosModules.blaz-service = service;
packages.${system} = {
default = package;
backend = package;
web = webBuild;
prebuilt = prebuiltPackage;
};
};
}