1{
2 description = "atproto github";
3
4 inputs = {
5 nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
6 indigo = {
7 url = "github:oppiliappan/indigo";
8 flake = false;
9 };
10 htmx-src = {
11 url = "https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js";
12 flake = false;
13 };
14 lucide-src = {
15 url = "https://github.com/lucide-icons/lucide/releases/download/0.483.0/lucide-icons-0.483.0.zip";
16 flake = false;
17 };
18 inter-fonts-src = {
19 url = "https://github.com/rsms/inter/releases/download/v4.1/Inter-4.1.zip";
20 flake = false;
21 };
22 ibm-plex-mono-src = {
23 url = "https://github.com/IBM/plex/releases/download/%40ibm%2Fplex-mono%401.1.0/ibm-plex-mono.zip";
24 flake = false;
25 };
26 gitignore = {
27 url = "github:hercules-ci/gitignore.nix";
28 inputs.nixpkgs.follows = "nixpkgs";
29 };
30 };
31
32 outputs =
33 { self
34 , nixpkgs
35 , indigo
36 , htmx-src
37 , lucide-src
38 , gitignore
39 , inter-fonts-src
40 , ibm-plex-mono-src
41 ,
42 }:
43 let
44 supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
45 forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
46 nixpkgsFor = forAllSystems (system:
47 import nixpkgs {
48 inherit system;
49 overlays = [ self.overlays.default ];
50 });
51 inherit (gitignore.lib) gitignoreSource;
52 in
53 {
54 overlays.default = final: prev:
55 let
56 goModHash = "sha256-EilWxfqrcKDaSR5zA3ZuDSCq7V+/IfWpKPu8HWhpndA=";
57 buildCmdPackage = name:
58 final.buildGoModule {
59 pname = name;
60 version = "0.1.0";
61 src = gitignoreSource ./.;
62 subPackages = [ "cmd/${name}" ];
63 vendorHash = goModHash;
64 CGO_ENABLED = 0;
65 };
66 in
67 {
68 indigo-lexgen = final.buildGoModule {
69 pname = "indigo-lexgen";
70 version = "0.1.0";
71 src = indigo;
72 subPackages = [ "cmd/lexgen" ];
73 vendorHash = "sha256-pGc29fgJFq8LP7n/pY1cv6ExZl88PAeFqIbFEhB3xXs=";
74 doCheck = false;
75 };
76
77 appview = with final;
78 final.pkgsStatic.buildGoModule {
79 pname = "appview";
80 version = "0.1.0";
81 src = gitignoreSource ./.;
82 postUnpack = ''
83 pushd source
84 mkdir -p appview/pages/static/{fonts,icons}
85 cp -f ${htmx-src} appview/pages/static/htmx.min.js
86 cp -rf ${lucide-src}/*.svg appview/pages/static/icons/
87 cp -f ${inter-fonts-src}/web/InterVariable*.woff2 appview/pages/static/fonts/
88 cp -f ${inter-fonts-src}/web/InterDisplay*.woff2 appview/pages/static/fonts/
89 cp -f ${ibm-plex-mono-src}/fonts/complete/woff2/IBMPlexMono-Regular.woff2 appview/pages/static/fonts/
90 ${pkgs.tailwindcss}/bin/tailwindcss -i input.css -o appview/pages/static/tw.css
91 popd
92 '';
93 doCheck = false;
94 subPackages = [ "cmd/appview" ];
95 vendorHash = goModHash;
96 CGO_ENABLED = 1;
97 stdenv = pkgsStatic.stdenv;
98 };
99
100 knotserver = with final;
101 final.pkgsStatic.buildGoModule {
102 pname = "knotserver";
103 version = "0.1.0";
104 src = gitignoreSource ./.;
105 nativeBuildInputs = [ final.makeWrapper ];
106 subPackages = [ "cmd/knotserver" ];
107 vendorHash = goModHash;
108 installPhase = ''
109 runHook preInstall
110
111 mkdir -p $out/bin
112 cp $GOPATH/bin/knotserver $out/bin/knotserver
113
114 wrapProgram $out/bin/knotserver \
115 --prefix PATH : ${pkgs.git}/bin
116
117 runHook postInstall
118 '';
119 CGO_ENABLED = 1;
120 };
121 knotserver-unwrapped = final.pkgsStatic.buildGoModule {
122 pname = "knotserver";
123 version = "0.1.0";
124 src = gitignoreSource ./.;
125 subPackages = [ "cmd/knotserver" ];
126 vendorHash = goModHash;
127 CGO_ENABLED = 1;
128 };
129 repoguard = buildCmdPackage "repoguard";
130 keyfetch = buildCmdPackage "keyfetch";
131 };
132 packages = forAllSystems (system: {
133 inherit
134 (nixpkgsFor."${system}")
135 indigo-lexgen
136 appview
137 knotserver
138 knotserver-unwrapped
139 repoguard
140 keyfetch
141 ;
142 });
143 defaultPackage = forAllSystems (system: nixpkgsFor.${system}.appview);
144 formatter = forAllSystems (system: nixpkgsFor."${system}".alejandra);
145 devShells = forAllSystems (system:
146 let
147 pkgs = nixpkgsFor.${system};
148 staticShell = pkgs.mkShell.override {
149 stdenv = pkgs.pkgsStatic.stdenv;
150 };
151 in
152 {
153 default = staticShell {
154 nativeBuildInputs = [
155 pkgs.go
156 pkgs.air
157 pkgs.gopls
158 pkgs.httpie
159 pkgs.nodejs
160 pkgs.indigo-lexgen
161 pkgs.litecli
162 pkgs.websocat
163 pkgs.tailwindcss
164 pkgs.nixos-shell
165 ];
166 shellHook = ''
167 mkdir -p appview/pages/static/{fonts,icons}
168 cp -f ${htmx-src} appview/pages/static/htmx.min.js
169 cp -rf ${lucide-src}/*.svg appview/pages/static/icons/
170 cp -f ${inter-fonts-src}/web/InterVariable*.woff2 appview/pages/static/fonts/
171 cp -f ${inter-fonts-src}/web/InterDisplay*.woff2 appview/pages/static/fonts/
172 cp -f ${ibm-plex-mono-src}/fonts/complete/woff2/IBMPlexMono-Regular.woff2 appview/pages/static/fonts/
173 '';
174 };
175 });
176 apps = forAllSystems (system:
177 let
178 pkgs = nixpkgsFor."${system}";
179 air-watcher = name:
180 pkgs.writeShellScriptBin "run"
181 ''
182 TANGLED_DEV=true ${pkgs.air}/bin/air -c /dev/null \
183 -build.cmd "${pkgs.tailwindcss}/bin/tailwindcss -i input.css -o ./appview/pages/static/tw.css && ${pkgs.go}/bin/go build -o ./out/${name}.out ./cmd/${name}/main.go" \
184 -build.bin "./out/${name}.out" \
185 -build.include_ext "go"
186 '';
187 tailwind-watcher =
188 pkgs.writeShellScriptBin "run"
189 ''
190 ${pkgs.tailwindcss}/bin/tailwindcss -w -i input.css -o ./appview/pages/static/tw.css
191 '';
192 in
193 {
194 watch-appview = {
195 type = "app";
196 program = ''${air-watcher "appview"}/bin/run'';
197 };
198 watch-knotserver = {
199 type = "app";
200 program = ''${air-watcher "knotserver"}/bin/run'';
201 };
202 watch-tailwind = {
203 type = "app";
204 program = ''${tailwind-watcher}/bin/run'';
205 };
206 });
207
208 nixosModules.appview =
209 { config
210 , pkgs
211 , lib
212 , ...
213 }:
214 with lib; {
215 options = {
216 services.tangled-appview = {
217 enable = mkOption {
218 type = types.bool;
219 default = false;
220 description = "Enable tangled appview";
221 };
222 port = mkOption {
223 type = types.int;
224 default = 3000;
225 description = "Port to run the appview on";
226 };
227 cookie_secret = mkOption {
228 type = types.str;
229 default = "00000000000000000000000000000000";
230 description = "Cookie secret";
231 };
232 };
233 };
234
235 config = mkIf config.services.tangled-appview.enable {
236 systemd.services.tangled-appview = {
237 description = "tangled appview service";
238 wantedBy = [ "multi-user.target" ];
239
240 serviceConfig = {
241 ListenStream = "0.0.0.0:${toString config.services.tangled-appview.port}";
242 ExecStart = "${self.packages.${pkgs.system}.appview}/bin/appview";
243 Restart = "always";
244 };
245
246 environment = {
247 TANGLED_DB_PATH = "appview.db";
248 TANGLED_COOKIE_SECRET = config.services.tangled-appview.cookie_secret;
249 };
250 };
251 };
252 };
253
254 nixosModules.knotserver =
255 { config
256 , pkgs
257 , lib
258 , ...
259 }:
260 let
261 cfg = config.services.tangled-knotserver;
262 in
263 with lib; {
264 options = {
265 services.tangled-knotserver = {
266 enable = mkOption {
267 type = types.bool;
268 default = false;
269 description = "Enable a tangled knotserver";
270 };
271
272 appviewEndpoint = mkOption {
273 type = types.str;
274 default = "https://tangled.sh";
275 description = "Appview endpoint";
276 };
277
278 gitUser = mkOption {
279 type = types.str;
280 default = "git";
281 description = "User that hosts git repos and performs git operations";
282 };
283
284 openFirewall = mkOption {
285 type = types.bool;
286 default = true;
287 description = "Open port 22 in the firewall for ssh";
288 };
289
290 stateDir = mkOption {
291 type = types.path;
292 default = "/home/${cfg.gitUser}";
293 description = "Tangled knot data directory";
294 };
295
296 repo = {
297 scanPath = mkOption {
298 type = types.path;
299 default = cfg.stateDir;
300 description = "Path where repositories are scanned from";
301 };
302
303 mainBranch = mkOption {
304 type = types.str;
305 default = "main";
306 description = "Default branch name for repositories";
307 };
308 };
309
310 server = {
311 listenAddr = mkOption {
312 type = types.str;
313 default = "0.0.0.0:5555";
314 description = "Address to listen on";
315 };
316
317 internalListenAddr = mkOption {
318 type = types.str;
319 default = "127.0.0.1:5444";
320 description = "Internal address for inter-service communication";
321 };
322
323 secretFile = mkOption {
324 type = lib.types.path;
325 example = "KNOT_SERVER_SECRET=<hash>";
326 description = "File containing secret key provided by appview (required)";
327 };
328
329 dbPath = mkOption {
330 type = types.path;
331 default = "${cfg.stateDir}/knotserver.db";
332 description = "Path to the database file";
333 };
334
335 hostname = mkOption {
336 type = types.str;
337 example = "knot.tangled.sh";
338 description = "Hostname for the server (required)";
339 };
340
341 dev = mkOption {
342 type = types.bool;
343 default = false;
344 description = "Enable development mode (disables signature verification)";
345 };
346 };
347 };
348 };
349
350 config = mkIf cfg.enable {
351 environment.systemPackages = with pkgs; [ git ];
352
353 system.activationScripts.gitConfig = ''
354 mkdir -p "${cfg.repo.scanPath}"
355 chown -R ${cfg.gitUser}:${cfg.gitUser} \
356 "${cfg.repo.scanPath}"
357
358 mkdir -p "${cfg.stateDir}/.config/git"
359 cat > "${cfg.stateDir}/.config/git/config" << EOF
360 [user]
361 name = Git User
362 email = git@example.com
363 EOF
364 chown -R ${cfg.gitUser}:${cfg.gitUser} \
365 "${cfg.stateDir}"
366 '';
367
368 users.users.${cfg.gitUser} = {
369 isSystemUser = true;
370 useDefaultShell = true;
371 home = cfg.stateDir;
372 createHome = true;
373 group = cfg.gitUser;
374 };
375
376 users.groups.${cfg.gitUser} = { };
377
378 services.openssh = {
379 enable = true;
380 extraConfig = ''
381 Match User ${cfg.gitUser}
382 AuthorizedKeysCommand /etc/ssh/keyfetch_wrapper
383 AuthorizedKeysCommandUser nobody
384 '';
385 };
386
387 environment.etc."ssh/keyfetch_wrapper" = {
388 mode = "0555";
389 text = ''
390 #!${pkgs.stdenv.shell}
391 ${self.packages.${pkgs.system}.keyfetch}/bin/keyfetch \
392 -repoguard-path ${self.packages.${pkgs.system}.repoguard}/bin/repoguard \
393 -internal-api "http://${cfg.server.internalListenAddr}" \
394 -git-dir "${cfg.repo.scanPath}" \
395 -log-path /tmp/repoguard.log
396 '';
397 };
398
399 systemd.services.knotserver = {
400 description = "knotserver service";
401 after = [ "network.target" "sshd.service" ];
402 wantedBy = [ "multi-user.target" ];
403 serviceConfig = {
404 User = cfg.gitUser;
405 WorkingDirectory = cfg.stateDir;
406 Environment = [
407 "KNOT_REPO_SCAN_PATH=${cfg.repo.scanPath}"
408 "KNOT_REPO_MAIN_BRANCH=${cfg.repo.mainBranch}"
409 "APPVIEW_ENDPOINT=${cfg.appviewEndpoint}"
410 "KNOT_SERVER_INTERNAL_LISTEN_ADDR=${cfg.server.internalListenAddr}"
411 "KNOT_SERVER_LISTEN_ADDR=${cfg.server.listenAddr}"
412 "KNOT_SERVER_DB_PATH=${cfg.server.dbPath}"
413 "KNOT_SERVER_HOSTNAME=${cfg.server.hostname}"
414 ];
415 EnvironmentFile = cfg.server.secretFile;
416 ExecStart = "${self.packages.${pkgs.system}.knotserver}/bin/knotserver";
417 Restart = "always";
418 };
419 };
420
421 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 22 ];
422 };
423 };
424
425 nixosConfigurations.knotVM = nixpkgs.lib.nixosSystem {
426 system = "x86_64-linux";
427 modules = [
428 self.nixosModules.knotserver
429 ({ config
430 , pkgs
431 , ...
432 }: {
433 virtualisation.memorySize = 2048;
434 virtualisation.diskSize = 10 * 1024;
435 virtualisation.cores = 2;
436 services.getty.autologinUser = "root";
437 environment.systemPackages = with pkgs; [ curl vim git ];
438 systemd.tmpfiles.rules =
439 let
440 u = config.services.tangled-knotserver.gitUser;
441 g = config.services.tangled-knotserver.gitUser;
442 in
443 [
444 "d /var/lib/knotserver 0770 ${u} ${g} - -" # Create the directory first
445 "f+ /var/lib/knotserver/secret 0660 ${u} ${g} - KNOT_SERVER_SECRET=679f15000084699abc6a20d3ef449efa3656583f38e456a08f0638250688ff2e"
446 ];
447 services.tangled-knotserver = {
448 enable = true;
449 server = {
450 secretFile = "/var/lib/knotserver/secret";
451 hostname = "localhost:6000";
452 listenAddr = "0.0.0.0:6000";
453 };
454 };
455 })
456 ];
457 };
458 };
459}