Home

infra @main - refs - log -
-
https://git.jolheiser.com/infra.git
dragonwell flake
infra / modules / git-bug / 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
  pkgs,
  config,
  lib,
  ...
}:
let
  cfg = config.services.git-bug;
  pkg = pkgs.git-bug;
  instanceOptions =
    { name, config, ... }:
    let
      inherit (lib) mkEnableOption mkOption types;
    in
    {
      enable = mkEnableOption "Enable git-bug web UI";

      package = mkOption {
        type = types.package;
        description = "git-bug package to use";
        default = pkg;
      };

      repoDir = mkOption {
        type = types.str;
        description = "git repo for bugs";
      };

      authorUser = mkOption {
        type = types.str;
        description = "git-bug user to use";
        default = "git-bug";
      };

      authorEmail = mkOption {
        type = types.str;
        description = "git-bug user email to use";
        default = "bugs@example.com";
      };

      authorAvatar = mkOption {
        type = types.str;
        description = "git-bug user avatar to use";
        default = "";
      };

      host = mkOption {
        type = types.str;
        description = "Network address or hostname to listen to";
        default = "127.0.0.1";
      };

      port = mkOption {
        type = types.port;
        description = "Port to listen to (default to random available port)";
        default = 0;
      };

      readOnly = mkOption {
        type = types.bool;
        description = "Whether to run the web UI in read-only mode";
        default = false;
      };

      logErrors = mkOption {
        type = types.bool;
        description = "Whether to log errors";
        default = false;
      };

      query = mkOption {
        type = types.str;
        description = "The query to open in the web UI bug list";
        default = "";
      };

      user = mkOption {
        type = types.str;
        description = "user account under which git-bug runs";
        default = "git-bug-${name}";
      };

      group = mkOption {
        type = types.str;
        description = "Group account under which git-bug runs";
        default = "git-bug-${name}";
      };
    };
in
{
  options.services.git-bug = lib.mkOption {
    type = lib.types.attrsOf (lib.types.submodule instanceOptions);
    description = "Attribute set of git-bug instances";
    default = { };
  };
  config = lib.mkIf (cfg != { }) {
    users.users = lib.mapAttrs' (
      name: instanceCfg:
      lib.nameValuePair instanceCfg.user {
        home = instanceCfg.repoDir;
        group = instanceCfg.group;
        isSystemUser = true;
        isNormalUser = false;
        description = "user for git-bug ${name} service";
      }
    ) (lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg);

    users.groups = lib.mapAttrs' (name: instanceCfg: lib.nameValuePair instanceCfg.group { }) (
      lib.filterAttrs (name: instanceCfg: instanceCfg.enable) cfg
    );

    systemd.services = lib.foldl' (
      acc: name:
      let
        instanceCfg = cfg.${name};
      in
      lib.recursiveUpdate acc (
        lib.optionalAttrs instanceCfg.enable {
          "git-bug-${name}" = {
            enable = true;
            description = "git-bug instance ${name}";
            wantedBy = [ "multi-user.target" ];
            after = [ "network.target" ];
            path = [ instanceCfg.package ];
            serviceConfig = {
              User = instanceCfg.user;
              Group = instanceCfg.group;
              Restart = "always";
              RestartSec = "15";
              WorkingDirectory = instanceCfg.repoDir;
              ReadWritePaths = [ instanceCfg.repoDir ];
              CapabilityBoundingSet = "";
              NoNewPrivileges = true;
              ProtectSystem = "strict";
              ProtectHome = true;
              PrivateTmp = true;
              PrivateDevices = true;
              PrivateUsers = true;
              ProtectHostname = true;
              ProtectClock = true;
              ProtectKernelTunables = true;
              ProtectKernelModules = true;
              ProtectKernelLogs = true;
              ProtectControlGroups = true;
              RestrictAddressFamilies = [
                "AF_UNIX"
                "AF_INET"
                "AF_INET6"
              ];
              RestrictNamespaces = true;
              LockPersonality = true;
              MemoryDenyWriteExecute = true;
              RestrictRealtime = true;
              RestrictSUIDSGID = true;
              RemoveIPC = true;
              PrivateMounts = true;
              SystemCallArchitectures = "native";
              ExecStart =
                let
                  args = [
                    "--host=${instanceCfg.host}"
                    "--no-open"
                    "--port=${builtins.toString instanceCfg.port}"
                    (lib.optionalString instanceCfg.readOnly "--read-only")
                    (lib.optionalString instanceCfg.logErrors "--log-errors")
                    "--query=${instanceCfg.query}"
                  ];
                in
                "${lib.getExe instanceCfg.package} ${builtins.concatStringsSep " " args}";
              ExecStartPre = pkgs.writeShellScript "git-bug-${name}-author" ''
                human_id=$(git-bug user --format json | jq -r '.[] | select(.name == "${instanceCfg.authorUser}") | .human_id' 2>/dev/null || echo "")
                if [ -n "$human_id" ] && [ "$human_id" != "null" ]; then
                    git-bug user adopt "$human_id"
                else
                    git-bug user new --name "${instanceCfg.authorUser}" --email "${instanceCfg.authorUser}" ${
                      lib.optionalString (instanceCfg != "") "--avatar \"${instanceCfg.authorAvatar}\""
                    }
                fi
              '';
            };
          };
        }
      )
    );
  };
}