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