Home

git-pr-nix @main - refs - log -
-
https://git.jolheiser.com/git-pr-nix.git
git-pr nix
git-pr-nix / module / default.nix
- raw
  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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.git-pr;
  pkg = pkgs.callPackage ../pkg { inherit pkgs; };
  tomlFormat = pkgs.formats.toml { };
  configFile = tomlFormat.generate "git-pr-config" (
    lib.recursiveUpdate {
      inherit (cfg)
        url
        admins
        host
        theme
        ;
      data_dir = cfg.dataDir;
      ssh_port = cfg.sshPort;
      web_port = cfg.webPort;
      time_format = cfg.timeFormat;
      repo = builtins.map (repo: {
        inherit (repo) id desc;
        clone_addr = repo.cloneAddr;
      }) cfg.repos;
    } cfg.extraConfig
  );
in
{
  options.services.git-pr = {
    enable = lib.mkEnableOption "Git PR service";

    url = lib.mkOption {
      type = lib.types.str;
      default = "pr.pico.sh";
      description = "URL used for help commands";
    };

    timeFormat = lib.mkOption {
      type = lib.types.str;
      default = "2006-01-02T15:04:05Z07:00"; # RFC3339
      description = "Time format used for timestamps";
    };

    dataDir = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/git-pr";
      description = "Directory to store data, including SQLite DB, TOML file, git repos, and SSH host keys";
    };

    admins = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [ ];
      description = "List of admin SSH public keys";
    };

    sshPort = lib.mkOption {
      type = lib.types.port;
      default = 2222;
      description = "Port for SSH service";
    };

    enableWeb = lib.mkEnableOption "Git PR web interface";

    webPort = lib.mkOption {
      type = lib.types.port;
      default = 3000;
      description = "Port for web service";
    };

    host = lib.mkOption {
      type = lib.types.str;
      default = "0.0.0.0";
      description = "Host to bind services to";
    };

    theme = lib.mkOption {
      type = lib.types.str;
      default = "dracula";
      description = "Theme for the web interface";
    };

    repos = lib.mkOption {
      type = lib.types.listOf (
        lib.types.submodule {
          options = {
            id = lib.mkOption {
              type = lib.types.str;
              description = "Repository ID";
            };
            cloneAddr = lib.mkOption {
              type = lib.types.str;
              description = "Git clone address";
            };
            desc = lib.mkOption {
              type = lib.types.str;
              description = "Repository description";
            };
          };
        }
      );
      default = [ ];
      description = "List of repositories to manage";
    };

    package = lib.mkOption {
      type = lib.types.package;
      default = pkg;
      description = "The Git PR package to use";
    };

    extraConfig = lib.mkOption {
      type = lib.types.attrs;
      default = { };
      description = "Additional configuration options to be added to the TOML file";
    };

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Open firewall for web and ssh ports";
    };
  };

  config = lib.mkIf cfg.enable {
    # SSH service
    systemd.services.git-pr-ssh = {
      description = "Git PR SSH Service";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        ExecStart = "${cfg.package}/bin/ssh --config ${configFile}";
        Restart = "always";
        User = "git-pr";
        Group = "git-pr";
      };
    };

    # Web service
    systemd.services.git-pr-web = {
      description = "Git PR Web Service";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        ExecStart = "${cfg.package}/bin/web --config ${configFile}";
        Restart = "always";
        User = "git-pr";
        Group = "git-pr";
      };
    };

    # Create user and group
    users.users.git-pr = {
      isSystemUser = true;
      group = "git-pr";
      home = cfg.dataDir;
      createHome = true;
    };

    users.groups.git-pr = { };

    networking.firewall = lib.mkIf cfg.openFirewall {
      allowedTCPPorts = [
        cfg.webPort
        cfg.sshPort
      ];
    };
  };
}