|
| 1 | +{ |
| 2 | + config, |
| 3 | + pkgs, |
| 4 | + lib, |
| 5 | + ... |
| 6 | +}: let |
| 7 | + cfg = config.services.graphite-mcp; |
| 8 | + |
| 9 | + preStartScript = pkgs.writeShellScript "graphite-mcp-prestart" '' |
| 10 | + mkdir -p "$HOME/.config/graphite" |
| 11 | + token=$(cat "${cfg.authTokenFile}") |
| 12 | + printf '{"authToken":"%s"}' "$token" > "$HOME/.config/graphite/user_config" |
| 13 | + chmod 600 "$HOME/.config/graphite/user_config" |
| 14 | + ''; |
| 15 | + |
| 16 | + startScript = pkgs.writeShellScript "graphite-mcp-start" '' |
| 17 | + exec ${lib.getExe pkgs.mcp-proxy} \ |
| 18 | + --host ${cfg.host} \ |
| 19 | + --port ${toString cfg.port} \ |
| 20 | + --transport streamablehttp \ |
| 21 | + -- ${lib.getExe pkgs.graphite-cli} mcp |
| 22 | + ''; |
| 23 | +in { |
| 24 | + options.services.graphite-mcp = { |
| 25 | + enable = lib.mkEnableOption "Graphite MCP server (stdio→HTTP bridge)"; |
| 26 | + |
| 27 | + port = lib.mkOption { |
| 28 | + type = lib.types.port; |
| 29 | + default = 8094; |
| 30 | + description = "Port for the streamable HTTP transport."; |
| 31 | + }; |
| 32 | + |
| 33 | + host = lib.mkOption { |
| 34 | + type = lib.types.str; |
| 35 | + default = "127.0.0.1"; |
| 36 | + description = "Address to bind the HTTP server to."; |
| 37 | + }; |
| 38 | + |
| 39 | + authTokenFile = lib.mkOption { |
| 40 | + type = lib.types.path; |
| 41 | + description = "Path to file containing the Graphite auth token."; |
| 42 | + }; |
| 43 | + |
| 44 | + openFirewall = lib.mkEnableOption "opening firewall ports for Graphite MCP"; |
| 45 | + }; |
| 46 | + |
| 47 | + config = lib.mkIf cfg.enable { |
| 48 | + users.users.graphite-mcp = { |
| 49 | + isSystemUser = true; |
| 50 | + group = "graphite-mcp"; |
| 51 | + home = "/var/lib/graphite-mcp"; |
| 52 | + }; |
| 53 | + users.groups.graphite-mcp = {}; |
| 54 | + |
| 55 | + systemd.services.graphite-mcp = { |
| 56 | + description = "Graphite MCP Server"; |
| 57 | + after = ["network-online.target"]; |
| 58 | + wants = ["network-online.target"]; |
| 59 | + wantedBy = ["multi-user.target"]; |
| 60 | + |
| 61 | + path = [pkgs.git]; |
| 62 | + |
| 63 | + environment = { |
| 64 | + HOME = "/var/lib/graphite-mcp"; |
| 65 | + }; |
| 66 | + |
| 67 | + serviceConfig = { |
| 68 | + ExecStartPre = "${preStartScript}"; |
| 69 | + ExecStart = "${startScript}"; |
| 70 | + User = "graphite-mcp"; |
| 71 | + Group = "graphite-mcp"; |
| 72 | + WorkingDirectory = "/var/lib/graphite-mcp"; |
| 73 | + StateDirectory = "graphite-mcp"; |
| 74 | + Restart = "on-failure"; |
| 75 | + RestartSec = 5; |
| 76 | + |
| 77 | + # Hardening |
| 78 | + NoNewPrivileges = true; |
| 79 | + PrivateDevices = true; |
| 80 | + PrivateTmp = true; |
| 81 | + ProtectHome = "tmpfs"; |
| 82 | + BindPaths = ["/var/lib/graphite-mcp"]; |
| 83 | + ProtectSystem = "strict"; |
| 84 | + ReadWritePaths = ["/var/lib/graphite-mcp"]; |
| 85 | + ProtectKernelTunables = true; |
| 86 | + ProtectKernelModules = true; |
| 87 | + ProtectControlGroups = true; |
| 88 | + RestrictSUIDSGID = true; |
| 89 | + }; |
| 90 | + }; |
| 91 | + |
| 92 | + networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [cfg.port]; |
| 93 | + }; |
| 94 | +} |
0 commit comments