-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
522 lines (474 loc) · 17.7 KB
/
flake.nix
File metadata and controls
522 lines (474 loc) · 17.7 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
{
description = "Personal Website for Conner Ohnesorge";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
bun2nix.url = "github:baileyluTCD/bun2nix";
flake-utils.url = "github:numtide/flake-utils";
treefmt-nix.url = "github:numtide/treefmt-nix";
};
outputs = inputs @ {
self,
flake-utils,
treefmt-nix,
...
}:
flake-utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
] (
system: let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
(final: prev: {final.go = prev.go_1_24;})
];
config.allowUnfree = true;
};
buildWithSpecificGo = pkg:
pkg.override {
buildGoModule = pkgs.buildGo124Module;
};
rooted = text:
builtins.concatStringsSep "\n" [
''
REPO_ROOT="$(git rev-parse --show-toplevel)"
''
text
];
scripts = {
dx = {
text = rooted ''$EDITOR "$REPO_ROOT"/flake.nix'';
description = "Edit flake.nix";
};
gx = {
text = rooted ''$EDITOR "$REPO_ROOT"/go.mod'';
description = "Edit go.mod";
};
clean = {
text = ''git clean -fdx'';
description = "Clean Project";
};
tests = {
text = rooted ''
cd "$REPO_ROOT"
templ generate "$REPO_ROOT"
go test -v ./...
cd -
'';
runtimeInputs = with pkgs; [go bun templ];
description = "Run all go tests";
};
lint = {
text = rooted ''
gum log --structured --level debug "cmd" txt "templ generate '$REPO_ROOT'"
templ generate "$REPO_ROOT"
gum log --structured --level debug "cmd" txt "golangci-lint run '$REPO_ROOT'"
golangci-lint run "$REPO_ROOT"
gum log --structured --level debug "cmd" txt "statix check '$REPO_ROOT'"
statix check "$REPO_ROOT"
gum log --structured --level debug "cmd" txt "deadnix '$REPO_ROOT'"
deadnix "$REPO_ROOT"/flake.nix
gum log --structured --level debug "cmd" txt "nix flake check"
nix flake check
'';
runtimeInputs = with pkgs; [golangci-lint statix deadnix templ rustc cargo gum];
description = "Run Nix/Go/Rust Linting Steps.";
};
generate-css = {
text = rooted ''
templ generate --log-level error "$REPO_ROOT"
go run "$REPO_ROOT"/cmd/update-css --cwd "$REPO_ROOT"
tailwindcss -i "$REPO_ROOT"/input.css \
-o "$REPO_ROOT"/cmd/conneroh/_static/dist/style.css \
--cwd "$REPO_ROOT"
'';
runtimeInputs = with pkgs; [tailwindcss templ go];
description = "Update the generated html and css files.";
};
generate-db = {
text = rooted ''doppler run -- go run "$REPO_ROOT"/cmd/update'';
runtimeInputs = with pkgs; [doppler];
description = "Update the generated go files from the md docs.";
};
generate-reload = {
text = ''
generate-css &
generate-db &
'';
runtimeInputs = with self.packages."${system}"; [generate-db generate-css];
description = "Code Generation Steps for specific directory changes.";
};
generate-js = {
text = rooted ''
bun build "$REPO_ROOT"/index.js \
--minify --minify-syntax --minify-whitespace --minify-identifiers \
--outdir "$REPO_ROOT"/cmd/conneroh/_static/dist/
'';
runtimeInputs = with pkgs; [bun git];
description = "Generate JS files";
};
generate-all = {
text = ''
generate-css &
generate-db &
generate-js &
wait
'';
runtimeInputs = with self.packages."${system}"; [
generate-css
generate-db
generate-js
];
description = "Generate all artifacts in parallel.";
};
run = {
text = rooted ''cd "$REPO_ROOT" && air'';
env.DEBUG = "true";
runtimeInputs = with pkgs; [air git];
description = "Run the application with air for hot reloading";
};
};
scriptPackages =
pkgs.lib.mapAttrs
(
name: script:
pkgs.writeShellApplication {
inherit name;
inherit (script) text;
runtimeInputs = script.runtimeInputs or [];
runtimeEnv = script.env or {};
}
)
scripts;
in {
devShells = let
shellHook = ''
echo "Available commands:"
${pkgs.lib.concatStringsSep "\n" (
pkgs.lib.mapAttrsToList (name: script: ''echo " ${name} - ${script.description}"'') scripts
)}
'';
shell-packages = with pkgs;
[
alejandra # Nix
nixd
nil
statix
deadnix
inputs.bun2nix.packages.${system}.default
go_1_24 # Go Tools
air
templ
golangci-lint
(buildWithSpecificGo revive)
(buildWithSpecificGo gopls)
(buildWithSpecificGo templ)
(buildWithSpecificGo golines)
(buildWithSpecificGo golangci-lint-langserver)
(buildWithSpecificGo gomarkdoc)
(buildWithSpecificGo gotests)
(buildWithSpecificGo gotools)
(buildWithSpecificGo reftools)
pprof
graphviz
awscli2
doppler
tailwindcss # Web
tailwindcss-language-server
bun
yaml-language-server
nodePackages.typescript-language-server
nodePackages.prettier
svgcleaner
harper
htmx-lsp
vscode-langservers-extracted
sqlite
# Testing
nodejs_20
flyctl # Infra
openssl.dev
skopeo
(
pkgs.buildGoModule (finalAttrs: {
pname = "copygen";
version = "0.4.1";
src = pkgs.fetchFromGitHub {
owner = "switchupcb";
repo = "copygen";
rev = "v${finalAttrs.version}";
sha256 = "sha256-gdoUvTla+fRoYayUeuRha8Dkix9ACxlt0tkac0CRqwA=";
};
vendorHash = "sha256-dOIGGZWtr8F82YJRXibdw3MvohLFBQxD+Y4OkZIJc2s=";
subPackages = ["."];
proxyVendor = true;
ldflags = [
"-s"
"-w"
"-X main.version=${finalAttrs.version}"
];
meta = with lib; {
description = "Copygen";
homepage = "https://github.com/switchupcb/copygen";
license = licenses.mit;
mainProgram = "copygen";
};
})
)
]
++ builtins.attrValues scriptPackages;
in {
default = pkgs.mkShell {
inherit shellHook;
packages = shell-packages;
};
};
apps = {
pr-preview = {
type = "app";
meta.description = "A preview server for pull requests";
program = "${self.packages.${system}.pr-preview}/bin/pr-preview";
};
deployPackage = {
type = "app";
meta.description = "Deploys the conneroh.com Docker image to Fly.io";
program = "${self.packages.${system}.deployPackage}/bin/deployPackage";
};
runTests = {
type = "app";
meta.description = "Run all tests (unit and browser)";
program = "${self.packages.${system}.tests}/bin/tests";
};
write-fly-toml = {
type = "app";
meta.description = "Writes the fly.toml configuration file to the repository root";
program = "${self.packages.${system}.write-fly-toml}/bin/write-fly-toml";
};
};
packages = let
internal_port = 8080;
force_https = true;
processes = ["app"];
version = self.shortRev or "dirty";
src = pkgs.lib.cleanSourceWith {
src = self;
filter = path: type: let
baseName = baseNameOf path;
path' = toString path;
isExcluded =
(baseName == ".direnv")
|| (baseName == ".git")
|| (baseName == "node_modules")
|| (baseName == "data" && type == "directory")
|| (builtins.match ".*/internal/data(/.*|$)" path' != null)
|| (baseName == "result")
|| (pkgs.lib.hasSuffix ".swp" baseName)
|| (pkgs.lib.hasSuffix "~" baseName);
in
!isExcluded;
name = "source";
};
preBuild = ''
${pkgs.templ}/bin/templ generate
${pkgs.tailwindcss}/bin/tailwindcss \
--minify \
-i ./input.css \
-o ./cmd/conneroh/_static/dist/style.css \
--cwd .
'';
settingsFormat = pkgs.formats.toml {};
flyProdConfig = {
app = "conneroh-com";
primary_region = "ord";
http_service = {
inherit internal_port force_https processes;
auto_stop_machines = "stop";
auto_start_machines = true;
min_machines_running = 0;
};
vm = [
{
memory = "512mb";
cpu_kind = "shared";
cpus = 1;
}
];
};
flyProdToml = settingsFormat.generate "fly.toml" flyProdConfig;
in
{
write-fly-toml = pkgs.writeShellApplication {
name = "write-fly-toml";
runtimeInputs = with pkgs; [coreutils];
text = ''
cp ${flyProdToml} ./fly.toml
echo "fly.toml written to $(pwd)/fly.toml"
'';
};
conneroh = pkgs.buildGoModule {
inherit src version preBuild;
vendorHash = "sha256-447MwdXuirsxql/A+BvUoQHW+FhiWkfCtet4eyCa5qI=";
name = "conneroh.com";
goSum = ./go.sum;
subPackages = ["."];
};
C-conneroh = pkgs.dockerTools.buildImage {
created = "now";
tag = "latest";
name = "conneroh";
config = {
WorkingDir = "/root";
Cmd = ["/bin/conneroh.com"];
ExposedPorts = {"8080/tcp" = {};};
Env = [
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
"NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
};
copyToRoot = [
self.packages."${system}".conneroh
(pkgs.runCommand "database-files" {} ''
mkdir -p $out/root
cp ${./master.db} $out/root/master.db
'')
];
};
deployPackage = pkgs.writeShellApplication {
name = "deployPackage";
runtimeInputs = with pkgs; [doppler skopeo flyctl cacert];
bashOptions = ["errexit" "pipefail"];
text = ''
set -e
[ -z "$FLY_AUTH_TOKEN" ] && FLY_AUTH_TOKEN="$(doppler secrets get --plain FLY_AUTH_TOKEN)"
TOKEN="$FLY_AUTH_TOKEN"
export FLY_NAME="conneroh-com"
export CONFIG_FILE=${flyProdToml}
REGISTRY="registry.fly.io/$FLY_NAME"
echo "Copying image to Fly.io... to $REGISTRY"
skopeo copy \
--insecure-policy \
docker-archive:"${self.packages."${system}".C-conneroh}" \
"docker://$REGISTRY:latest" \
--dest-creds x:"$TOKEN" \
--format v2s2
echo "Deploying to Fly.io..."
fly deploy \
--remote-only \
-c "$CONFIG_FILE" \
-i "$REGISTRY:latest" \
-t "$TOKEN"
'';
};
pr-preview = pkgs.writeShellScriptBin "pr-preview" ''
set -euo pipefail
export PATH="${
pkgs.lib.makeBinPath (with pkgs; [flyctl skopeo jq git gnused coreutils])
}:$PATH"
readonly APP_PREFIX="pr"
readonly FLY_ORG="''${FLY_ORG:-personal}"
readonly FLY_REGION="''${FLY_REGION:-ord}"
[ -z "$MASTER_FLY_AUTH_TOKEN" ] && MASTER_FLY_AUTH_TOKEN="$(doppler secrets get --plain MASTER_FLY_AUTH_TOKEN)"
generate_app_name() {
local pr_number="$1"
echo "''${APP_PREFIX}-''${pr_number}-conneroh-com" | tr '[:upper:]' '[:lower:]'
}
destroy_pr_app() {
local pr_number="$1"
local app_name
app_name=$(generate_app_name "$pr_number")
echo "Destroying app: ''${app_name}"
if flyctl apps list --json -t "$MASTER_FLY_AUTH_TOKEN" | jq -e ".[] | select(.Name == \"''${app_name}\")" > /dev/null; then
flyctl apps destroy -t "$MASTER_FLY_AUTH_TOKEN" "''${app_name}" --yes
echo "App ''${app_name} destroyed successfully"
else
echo "App ''${app_name} not found, nothing to destroy"
fi
}
deploy_pr_app() {
local pr_number="$1"
shift
local app_name
app_name=$(generate_app_name "$pr_number")
echo "Deploying PR #''${pr_number} to app: ''${app_name}"
# Check if app exists
if ! flyctl apps list -t "$MASTER_FLY_AUTH_TOKEN" --json | jq -e ".[] | select(.Name == \"''${app_name}\")" > /dev/null; then
echo "Creating new app: ''${app_name}"
flyctl apps create -t "$MASTER_FLY_AUTH_TOKEN" "''${app_name}" --org "''${FLY_ORG}"
fi
# Create fly.toml for PR preview
cat > fly.pr.toml <<EOF
app = "''${app_name}"
primary_region = "''${FLY_REGION}"
[http_service]
internal_port = ${toString internal_port}
force_https = true
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
[[vm]]
memory = "512M"
cpu_kind = "shared"
cpus = 1
EOF
local registry="registry.fly.io/''${app_name}"
echo "Copying image to ''${registry}..."
skopeo copy \
--insecure-policy \
docker-archive:"${self.packages."${system}".C-conneroh}" \
"docker://''${registry}:latest" \
--dest-creds x:"''${MASTER_FLY_AUTH_TOKEN}"
echo "Deploying PR #''${pr_number} to app: ''${app_name}"
flyctl deploy \
--app "''${app_name}" \
--config fly.pr.toml \
--image "''${registry}:latest" \
--remote-only \
-t "$MASTER_FLY_AUTH_TOKEN"
"$@"
echo "Deployment complete!"
echo "URL: https://''${app_name}.fly.dev"
flyctl status --app "''${app_name}" --json -t "$MASTER_FLY_AUTH_TOKEN" | jq '{
app: .Name,
url: "https://\(.Name).fly.dev",
version: .DeploymentStatus.Version,
status: .DeploymentStatus.Status
}'
}
# Main command handling
case "''${1:-}" in
deploy)
shift
deploy_pr_app "$@"
;;
destroy)
shift
destroy_pr_app "$@"
;;
*)
echo "Usage: pr-preview {deploy|destroy} <pr_number> [additional args]"
exit 1
;;
esac
'';
}
// pkgs.lib.genAttrs (builtins.attrNames scripts) (
name: scriptPackages.${name}
);
formatter = let
treefmtModule = {
projectRootFile = "flake.nix";
programs = {
alejandra.enable = true; # Nix formatter
golines.enable = true; # Golang formatter
gofumpt.enable = true; # Golang formatter
rustfmt.enable = true; # Rust formatter
};
};
in
treefmt-nix.lib.mkWrapper pkgs treefmtModule;
}
);
}