aboutsummaryrefslogtreecommitdiff
path: root/infra/modules/gitserver.nix
blob: f675d2d97bfc3fec547e0fe00ea1989bdde11cbe (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
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
{
  flake.modules.nixos.gitserver =
    {
      config,
      pkgs,
      lib,
      ...
    }:
    let
      cfg = config.infra.gitserver;
      workingDirectory = "/var/lib/git";
    in
    {
      options.infra.gitserver = {
        user = lib.mkOption {
          type = lib.types.str;
          description = "The name of the user and group for managing the gitserver.";
          default = "git";
        };

        enablePrivate = lib.mkOption {
          type = lib.types.bool;
          description = "Whether to enable storing the private monorepo.";
          default = false;
        };

        enablePublic = lib.mkOption {
          type = lib.types.bool;
          description = "Whether to enable storing the public monorepo.";
          default = false;
        };
      };

      config = {
        infra.persist.directories = [
          "${workingDirectory}"
        ];

        users = {
          groups."${cfg.user}" = { };
          users."${cfg.user}" = {
            group = cfg.user;
            description = "git user";
            isSystemUser = true;
            home = workingDirectory;
            shell = "${pkgs.git}/bin/git-shell";
          };
        };

        systemd.services.infra-gitserver-monorepo = {
          description = "Initialize the monorepo if it has not already been done so.";
          wantedBy = [ "multi-user.target" ];

          serviceConfig = {
            Type = "oneshot";
            User = cfg.user;
            Group = cfg.user;
            StateDirectory = "git";
            WorkingDirectory = "~";
          };

          path = [ pkgs.git ];
          enableStrictShellChecks = true;
          script = ''
            ${lib.optionalString cfg.enablePrivate ''
              if [ ! -d "${workingDirectory}/monorepo" ]; then
                git init --bare monorepo
              fi
            ''}

            ${lib.optionalString cfg.enablePublic ''
              if [ ! -d "${workingDirectory}/monorepo-public" ]; then
                git init --bare monorepo-public
              fi
            ''}
          '';
        };

        assertions = [
          {
            assertion = config.infra.tailscale.ssh;
            message = "Tailscale SSH must be enabled when using gitserver.";
          }
          {
            assertion = config.infra.gitserver.enablePrivate || config.infra.gitserver.enablePublic;
            message = "either the private or public monorepo must be enabled in gitserver";
          }
        ];
      };
    };
}