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