bazel-cache.nix (2155B)
1 { 2 config, 3 lib, 4 pkgs, 5 haravara, 6 ... 7 }: let 8 cfg = config.services.haravara.bazel-cache; 9 in { 10 options.services.haravara.bazel-cache = { 11 enable = lib.mkEnableOption "Bazel Remote Cache"; 12 13 listenAddress = lib.mkOption { 14 type = lib.types.str; 15 default = "127.0.0.1"; 16 description = "Address for the Bazel cache to listen on"; 17 }; 18 19 port = lib.mkOption { 20 type = lib.types.port; 21 default = 9820; 22 description = "Port for the Bazel cache to listen on"; 23 }; 24 25 dataDir = lib.mkOption { 26 type = lib.types.str; 27 default = "/var/lib/bazel-cache"; 28 description = "Directory for storing cached data"; 29 }; 30 31 domain = lib.mkOption { 32 type = lib.types.str; 33 description = "Domain for the Bazel cache instance"; 34 }; 35 basicAuthUser = lib.mkOption { 36 type = lib.types.str; 37 description = "Username for basic HTTP authentication"; 38 }; 39 basicAuthHash = lib.mkOption { 40 type = lib.types.str; 41 description = "Bcrypt password hash for basic HTTP authentication"; 42 }; 43 }; 44 45 config = lib.mkIf cfg.enable { 46 systemd.services.bazel-remote = { 47 description = "Bazel Remote Cache"; 48 wantedBy = ["multi-user.target"]; 49 after = ["network.target"]; 50 serviceConfig = { 51 ExecStart = '' 52 ${pkgs.bazel-remote}/bin/bazel-remote \ 53 --host ${cfg.listenAddress} \ 54 --port ${toString cfg.port} \ 55 --dir ${cfg.dataDir} \ 56 --max_size 1024 57 ''; 58 User = "bazel-remote"; 59 Group = "bazel-remote"; 60 StateDirectory = "bazel-cache"; 61 Restart = "on-failure"; 62 }; 63 }; 64 65 users = { 66 users.bazel-remote = { 67 isSystemUser = true; 68 group = "bazel-remote"; 69 }; 70 groups.bazel-remote = {}; 71 }; 72 73 services.caddy.virtualHosts."${cfg.domain}".extraConfig = '' 74 basic_auth { 75 ${cfg.basicAuthUser} ${cfg.basicAuthHash} 76 } 77 handle / { 78 redir https://${haravara.stripSubdomain cfg.domain} 79 } 80 handle { 81 reverse_proxy ${cfg.listenAddress}:${toString cfg.port} 82 } 83 ''; 84 }; 85 }