aboutsummaryrefslogtreecommitdiff
path: root/modules/services/nixos/copilot-api.nix
blob: d9d4da33397477b77539584353c6cd6149ff10ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.collinux.services.copilot-api;
in {
  config = lib.mkIf cfg.enable {
    users.groups."copilot-api" = {};
    users.users."copilot-api" = {
      isSystemUser = true;
      group = "copilot-api";
      home = "/var/lib/copilot-api";
      createHome = true;
    };

    systemd.services."copilot-api" = {
      description = "GitHub Copilot API Proxy";
      restartIfChanged = true;
      wants = ["network-online.target"];
      after = ["network-online.target"];
      wantedBy = ["multi-user.target"];

      serviceConfig = {
        User = "copilot-api";
        Group = "copilot-api";
        Type = "simple";
        Restart = "on-failure";
        RestartSec = "5s";

        # Load environment variables from file (e.g., GH_TOKEN)
        EnvironmentFile = lib.mkIf (cfg.githubToken != null) cfg.githubToken;

        # Security hardening
        PrivateTmp = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        RestrictSUIDSGID = true;

        # Allow writing to state directory
        StateDirectory = "copilot-api";
        WorkingDirectory = "/var/lib/copilot-api";

        ExecStart = let
          copilot-api = pkgs.callPackage ../../../pkgs/copilot-api {};
          # Use bash to read token from environment and pass to command
          startScript = pkgs.writeShellScript "copilot-api-start" ''
            exec ${copilot-api}/bin/copilot-api start \
              --port ${toString cfg.port} \
              --host ${cfg.listenAddr} \
              ${lib.optionalString (cfg.githubToken != null) "--github-token \"$GH_TOKEN\""}
          '';
        in "${startScript}";
      };
    };
  };
}