Monorepo for Tangled
tangled.org
1{
2 description = "atproto github";
3
4 inputs = {
5 nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6 fenix = {
7 url = "github:nix-community/fenix";
8 inputs.nixpkgs.follows = "nixpkgs";
9 };
10 gomod2nix = {
11 url = "github:nix-community/gomod2nix";
12 inputs.nixpkgs.follows = "nixpkgs";
13 };
14 flake-compat = {
15 url = "https://git.lix.systems/lix-project/flake-compat/archive/main.tar.gz";
16 flake = false;
17 };
18 indigo = {
19 url = "github:oppiliappan/indigo";
20 flake = false;
21 };
22 htmx-src = {
23 url = "https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js";
24 flake = false;
25 };
26 htmx-ws-src = {
27 # strange errors in console that i can't really make out
28 # url = "https://unpkg.com/htmx.org@2.0.4/dist/ext/ws.js";
29 url = "https://cdn.jsdelivr.net/npm/htmx-ext-ws@2.0.2";
30 flake = false;
31 };
32 lucide-src = {
33 url = "https://github.com/lucide-icons/lucide/releases/download/0.536.0/lucide-icons-0.536.0.zip";
34 flake = false;
35 };
36 inter-fonts-src = {
37 url = "https://github.com/rsms/inter/releases/download/v4.1/Inter-4.1.zip";
38 flake = false;
39 };
40 actor-typeahead-src = {
41 url = "git+https://tangled.org/@jakelazaroff.com/actor-typeahead";
42 flake = false;
43 };
44 mermaid-src = {
45 url = "https://cdn.jsdelivr.net/npm/mermaid@11.12.3/dist/mermaid.min.js";
46 flake = false;
47 };
48 ibm-plex-mono-src = {
49 url = "https://github.com/IBM/plex/releases/download/%40ibm%2Fplex-mono%401.1.0/ibm-plex-mono.zip";
50 flake = false;
51 };
52 sqlite-lib-src = {
53 url = "https://sqlite.org/2024/sqlite-amalgamation-3450100.zip";
54 flake = false;
55 };
56 };
57
58 outputs = {
59 self,
60 nixpkgs,
61 fenix,
62 gomod2nix,
63 indigo,
64 htmx-src,
65 htmx-ws-src,
66 lucide-src,
67 inter-fonts-src,
68 sqlite-lib-src,
69 ibm-plex-mono-src,
70 actor-typeahead-src,
71 mermaid-src,
72 ...
73 }: let
74 supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];
75 forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
76 nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system});
77
78 mkPackageSet = pkgs:
79 pkgs.lib.makeScope pkgs.newScope (self: {
80 src = let
81 fs = pkgs.lib.fileset;
82 in
83 fs.toSource {
84 root = ./.;
85 fileset = fs.difference (fs.intersection (fs.gitTracked ./.) (fs.fileFilter (file: !(file.hasExt "nix")) ./.)) (fs.maybeMissing ./.jj);
86 };
87 buildGoApplication =
88 (self.callPackage "${gomod2nix}/builder" {
89 gomod2nix = gomod2nix.legacyPackages.${pkgs.stdenv.hostPlatform.system}.gomod2nix;
90 }).buildGoApplication;
91 rustPlatform = pkgs.makeRustPlatform {
92 inherit (fenix.packages.${pkgs.system}.stable) rustc cargo;
93 };
94 modules = ./nix/gomod2nix.toml;
95 sqlite-lib = self.callPackage ./nix/pkgs/sqlite-lib.nix {
96 inherit sqlite-lib-src;
97 };
98 lexgen = self.callPackage ./nix/pkgs/lexgen.nix {inherit indigo;};
99 goat = self.callPackage ./nix/pkgs/goat.nix {inherit indigo;};
100 appview-static-files = self.callPackage ./nix/pkgs/appview-static-files.nix {
101 inherit htmx-src htmx-ws-src lucide-src inter-fonts-src ibm-plex-mono-src actor-typeahead-src mermaid-src;
102 };
103 appview = self.callPackage ./nix/pkgs/appview.nix {};
104 blog = self.callPackage ./nix/pkgs/blog.nix {};
105 docs = self.callPackage ./nix/pkgs/docs.nix {
106 inherit inter-fonts-src ibm-plex-mono-src lucide-src;
107 inherit (pkgs) pagefind;
108 };
109 spindle = self.callPackage ./nix/pkgs/spindle.nix {};
110 knot-unwrapped = self.callPackage ./nix/pkgs/knot-unwrapped.nix {};
111 knot = self.callPackage ./nix/pkgs/knot.nix {};
112 dolly = self.callPackage ./nix/pkgs/dolly.nix {};
113 tap = self.callPackage ./nix/pkgs/tap.nix {};
114 knotmirror = self.callPackage ./nix/pkgs/knotmirror.nix {};
115 bobbin = self.callPackage ./nix/pkgs/bobbin.nix {};
116 });
117 in {
118 overlays.default = final: prev: {
119 inherit (mkPackageSet final) lexgen goat sqlite-lib spindle knot-unwrapped knot appview docs dolly tap knotmirror bobbin;
120 };
121
122 packages = forAllSystems (system: let
123 pkgs = nixpkgsFor.${system};
124 packages = mkPackageSet pkgs;
125 staticPackages = mkPackageSet pkgs.pkgsStatic;
126 crossPackages = mkPackageSet pkgs.pkgsCross.gnu64.pkgsStatic;
127 in {
128 inherit
129 (packages)
130 appview
131 appview-static-files
132 blog
133 lexgen
134 goat
135 spindle
136 knot
137 knot-unwrapped
138 sqlite-lib
139 docs
140 dolly
141 tap
142 knotmirror
143 bobbin
144 ;
145
146 pkgsStatic-appview = staticPackages.appview;
147 pkgsStatic-knot = staticPackages.knot;
148 pkgsStatic-knot-unwrapped = staticPackages.knot-unwrapped;
149 pkgsStatic-spindle = staticPackages.spindle;
150 pkgsStatic-sqlite-lib = staticPackages.sqlite-lib;
151 pkgsStatic-dolly = staticPackages.dolly;
152
153 pkgsCross-gnu64-pkgsStatic-appview = crossPackages.appview;
154 pkgsCross-gnu64-pkgsStatic-knot = crossPackages.knot;
155 pkgsCross-gnu64-pkgsStatic-knot-unwrapped = crossPackages.knot-unwrapped;
156 pkgsCross-gnu64-pkgsStatic-spindle = crossPackages.spindle;
157 pkgsCross-gnu64-pkgsStatic-dolly = crossPackages.dolly;
158
159 treefmt-wrapper = pkgs.treefmt.withConfig {
160 settings.formatter = {
161 alejandra = {
162 command = pkgs.lib.getExe pkgs.alejandra;
163 includes = ["*.nix"];
164 };
165
166 gofmt = {
167 command = pkgs.lib.getExe' pkgs.go "gofmt";
168 options = ["-w"];
169 includes = ["*.go"];
170 };
171
172 rustfmt = {
173 command = pkgs.lib.getExe' fenix.packages.${system}.stable.rustfmt "rustfmt";
174 options = ["--edition" "2024"];
175 includes = ["*.rs"];
176 excludes = ["**/src/_lex/**"];
177 };
178
179 # prettier = let
180 # wrapper = pkgs.runCommandLocal "prettier-wrapper" {nativeBuildInputs = [pkgs.makeWrapper];} ''
181 # makeWrapper ${pkgs.prettier}/bin/prettier "$out" --add-flags "--plugin=${pkgs.prettier-plugin-go-template}/lib/node_modules/prettier-plugin-go-template/lib/index.js"
182 # '';
183 # in {
184 # command = wrapper;
185 # options = ["-w"];
186 # includes = ["*.html"];
187 # # causes Go template plugin errors: https://github.com/NiklasPor/prettier-plugin-go-template/issues/120
188 # excludes = ["appview/pages/templates/layouts/repobase.html" "appview/pages/templates/repo/tags.html"];
189 # };
190 };
191 };
192 });
193 defaultPackage = forAllSystems (system: self.packages.${system}.appview);
194 devShells = forAllSystems (system: let
195 pkgs = nixpkgsFor.${system};
196 packages' = self.packages.${system};
197 staticShell = args:
198 (pkgs.mkShell.override {
199 stdenv = pkgs.pkgsStatic.stdenv;
200 }) (args
201 // {
202 nativeBuildInputs =
203 args.nativeBuildInputs
204 ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
205 pkgs.darwin.cctools
206 ];
207 });
208 in {
209 default = staticShell {
210 nativeBuildInputs = [
211 pkgs.go
212 pkgs.air
213 pkgs.gopls
214 pkgs.httpie
215 pkgs.litecli
216 pkgs.websocat
217 pkgs.tailwindcss
218 pkgs.nixos-shell
219 pkgs.redis
220 pkgs.worker-build
221 pkgs.cargo-generate
222 (fenix.packages.${system}.combine [
223 fenix.packages.${system}.stable.cargo
224 fenix.packages.${system}.stable.rustc
225 fenix.packages.${system}.stable.rust-src
226 fenix.packages.${system}.stable.clippy
227 fenix.packages.${system}.stable.rustfmt
228 fenix.packages.${system}.targets.wasm32-unknown-unknown.stable.rust-std
229 ])
230 pkgs.coreutils # for those of us who are on systems that use busybox (alpine)
231 packages'.lexgen
232 packages'.treefmt-wrapper
233 packages'.tap
234 ];
235 shellHook = ''
236 mkdir -p appview/pages/static
237 # temporary self-heal for workspaces that copied static assets as read-only
238 [ -d appview/pages/static/icons ] && [ ! -w appview/pages/static/icons ] && chmod -R u+rwX appview/pages/static
239 # no preserve is needed because watch-tailwind will want to be able to overwrite
240 cp -fr --no-preserve=ownership,mode ${packages'.appview-static-files}/* appview/pages/static
241 export TANGLED_OAUTH_CLIENT_KID="$(date +%s)"
242 export TANGLED_OAUTH_CLIENT_SECRET="$(${packages'.goat}/bin/goat key generate -t P-256 | grep -A1 "Secret Key" | tail -n1 | awk '{print $1}')"
243 # Make xcrun (Nix stub) able to find ld from the system Command Line Tools.
244 # Without this, worker-build/cargo fails with "error: tool 'ld' not found".
245 if [ -d /Library/Developer/CommandLineTools/usr/bin ]; then
246 export PATH=/Library/Developer/CommandLineTools/usr/bin:$PATH
247 fi
248 '';
249 env.CGO_ENABLED = 1;
250 };
251 });
252 apps = forAllSystems (system: let
253 pkgs = nixpkgsFor."${system}";
254 packages' = self.packages.${system};
255 air-watcher = name: arg:
256 pkgs.writeShellScriptBin "run"
257 ''
258 export PATH=${pkgs.go}/bin:$PATH
259 ${pkgs.air}/bin/air -c ./.air/${name}.toml \
260 -build.args_bin "${arg}"
261 '';
262 tailwind-watcher =
263 pkgs.writeShellScriptBin "run"
264 ''
265 export BROWSERSLIST_IGNORE_OLD_DATA=true
266 ${pkgs.tailwindcss}/bin/tailwindcss --watch=always -i input.css -o ./appview/pages/static/tw.css
267 '';
268 in {
269 watch-appview = {
270 type = "app";
271 program = toString (pkgs.writeShellScript "watch-appview" ''
272 echo "copying static files to appview/pages/static..."
273 mkdir -p appview/pages/static
274 ${pkgs.coreutils}/bin/cp -fr --no-preserve=ownership,mode ${packages'.appview-static-files}/* appview/pages/static
275 ${air-watcher "appview" ""}/bin/run
276 '');
277 };
278 watch-knot = {
279 type = "app";
280 program = ''${air-watcher "knot" "server"}/bin/run'';
281 };
282 watch-spindle = {
283 type = "app";
284 program = ''${air-watcher "spindle" ""}/bin/run'';
285 };
286 watch-blog = {
287 type = "app";
288 program = toString (pkgs.writeShellScript "watch-blog" ''
289 echo "copying static files to appview/pages/static..."
290 mkdir -p appview/pages/static
291 ${pkgs.coreutils}/bin/cp -fr --no-preserve=ownership,mode ${packages'.appview-static-files}/* appview/pages/static
292 ${air-watcher "blog" "serve"}/bin/run
293 '');
294 };
295 watch-tailwind = {
296 type = "app";
297 program = ''${tailwind-watcher}/bin/run'';
298 };
299 serve-docs = {
300 type = "app";
301 program = toString (pkgs.writeShellScript "serve-docs" ''
302 echo "building docs..."
303 docsOut=$(nix build --no-link --print-out-paths .#docs)
304 echo "serving docs at http://localhost:1414"
305 cd "$docsOut"
306 exec ${pkgs.python3}/bin/python3 -m http.server 1414
307 '');
308 };
309 vm = let
310 guestSystem =
311 if pkgs.stdenv.hostPlatform.isAarch64
312 then "aarch64-linux"
313 else "x86_64-linux";
314 in {
315 type = "app";
316 program =
317 (pkgs.writeShellApplication {
318 name = "launch-vm";
319 text = ''
320 rootDir=$(jj --ignore-working-copy root || git rev-parse --show-toplevel) || (echo "error: can't find repo root?"; exit 1)
321 cd "$rootDir"
322
323 # Reset TMPDIR in case it points to a stale nix-shell temp dir
324 export TMPDIR=/tmp
325
326 mkdir -p nix/vm-data/{knot,repos,spindle,spindle-logs}
327
328 export TANGLED_VM_DATA_DIR="$rootDir/nix/vm-data"
329 exec ${pkgs.lib.getExe
330 (import ./nix/vm.nix {
331 inherit nixpkgs self;
332 system = guestSystem;
333 hostSystem = system;
334 }).config.system.build.vm}
335 '';
336 })
337 + /bin/launch-vm;
338 };
339 gomod2nix = {
340 type = "app";
341 program = toString (pkgs.writeShellScript "gomod2nix" ''
342 ${gomod2nix.legacyPackages.${system}.gomod2nix}/bin/gomod2nix generate --outdir ./nix
343 '');
344 };
345 lexgen = {
346 type = "app";
347 program =
348 (pkgs.writeShellApplication {
349 name = "lexgen";
350 text = ''
351 if ! command -v lexgen > /dev/null; then
352 echo "error: must be executed from devshell"
353 exit 1
354 fi
355
356 rootDir=$(jj --ignore-working-copy root || git rev-parse --show-toplevel) || (echo "error: can't find repo root?"; exit 1)
357 cd "$rootDir"
358
359 rm -f api/tangled/*
360 lexgen --build-file lexicon-build-config.json lexicons
361 sed -i.bak 's/\tutil/\/\/\tutil/' api/tangled/*
362 # lexgen generates incomplete Marshaler/Unmarshaler for union types
363 find api/tangled/*.go -not -name "cbor_gen.go" -exec \
364 sed -i '/^func.*\(MarshalCBOR\|UnmarshalCBOR\)/,/^}/ s/^/\/\/ /' {} +
365 ${pkgs.gotools}/bin/goimports -w api/tangled/*
366 go run ./cmd/cborgen/
367 lexgen --build-file lexicon-build-config.json lexicons
368 rm api/tangled/*.bak
369 '';
370 })
371 + /bin/lexgen;
372 };
373 });
374
375 nixosModules.appview = {
376 lib,
377 pkgs,
378 ...
379 }: {
380 imports = [./nix/modules/appview.nix];
381
382 services.tangled.appview.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.appview;
383 };
384 nixosModules.knotmirror = {
385 lib,
386 pkgs,
387 ...
388 }: {
389 imports = [./nix/modules/knotmirror.nix];
390
391 services.tangled.knotmirror.tap-package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.tap;
392 services.tangled.knotmirror.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.knotmirror;
393 };
394 nixosModules.knot = {
395 lib,
396 pkgs,
397 ...
398 }: {
399 imports = [./nix/modules/knot.nix];
400
401 services.tangled.knot.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.knot;
402 };
403 nixosModules.spindle = {
404 lib,
405 pkgs,
406 ...
407 }: {
408 imports = [./nix/modules/spindle.nix];
409
410 services.tangled.spindle.package = lib.mkDefault self.packages.${pkgs.stdenv.hostPlatform.system}.spindle;
411 };
412
413 formatter = forAllSystems (system: self.packages.${system}.treefmt-wrapper);
414 };
415}