all repos

mugit @ v0.1.0

🐮 git server that your cow will love

mugit/flake.nix(view 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{
  description = "a git server that your cow will love";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        version = self.rev or "dev";
      in
      {
        packages = {
          default = self.packages.${system}.mugit;
          mugit = pkgs.buildGoModule {
            pname = "mugit";
            version = version;
            src = ./.;
            vendorHash = "sha256-VcNnosr9Co+MFEA36s4BIDmg/bx+/mTIHdgOaGJKhbc=";
            ldflags = [
              "-s"
              "-w"
              "-X main.version=${version}"
            ];
            meta = with pkgs.lib; {
              homepage = "https://github.com/olexsmir/mugit";
              license = licenses.mit;
            };
          };
        };
      }
    )
    // {
      nixosModules.default =
        {
          config,
          lib,
          pkgs,
          ...
        }:
        with lib;
        let
          cfg = config.services.mugit;
          format = pkgs.formats.yaml { };
          configFile =
            if cfg.configFile != null then cfg.configFile else format.generate "config.yaml" cfg.config;

          mugitWrapper = pkgs.symlinkJoin {
            name = "mugit";
            paths = [ cfg.package ];
            buildInputs = [ pkgs.makeWrapper ];
            postBuild = ''
              wrapProgram $out/bin/mugit \
                --add-flags "--config ${configFile}"
            '';
          };

          mugitWithCompletions = pkgs.stdenv.mkDerivation {
            pname = "mugit";
            version = cfg.package.version;
            src = mugitWrapper;
            nativeBuildInputs = [ pkgs.installShellFiles ];
            installPhase = ''
              mkdir -p $out/bin
              cp -r $src/bin/* $out/bin/

              installShellCompletion --cmd mugit \
                --bash <($out/bin/mugit completion bash) \
                --zsh <($out/bin/mugit completion zsh) \
                --fish <($out/bin/mugit completion fish)
            '';
          };
        in
        {
          options.services.mugit = {
            enable = mkEnableOption "mugit service";

            package = mkOption {
              type = types.package;
              default = self.packages.${pkgs.system}.mugit;
              defaultText = literalExpression "self.packages.\${pkgs.system}.mugit";
              description = "The mugit package to use.";
            };

            openFirewall = mkOption {
              type = types.bool;
              default = false;
              description = "Whether to open the firewall for mugit. Can only be used with `config`, not `configFile`.";
            };

            exposeCli = mkOption {
              type = types.bool;
              default = true;
              description = "Whether to expose the mugit CLI to all users with the service configuration.";
            };

            dataDir = mkOption {
              type = types.path;
              default = "/var/lib/mugit";
              description = "Directory where mugit stores its data.";
            };

            configFile = mkOption {
              type = types.nullOr types.path;
              default = null;
              description = "Path to an existing mugit configuration file. Mutually exclusive with `config`.";
            };

            config = mkOption {
              type = format.type;
              default = { };
              description = ''
                Configuration for mugit. See documentation for available options.
                https://github.com/olexsmir/mugit/blob/main/README.md
              '';
              example = literalExpression ''
                {
                  server.port = 8080;
                  repo.dir = "/var/lib/mugit";
                }
              '';
            };

            user = mkOption {
              type = types.str;
              default = "mugit";
              description = "User account under which mugit runs.";
            };

            group = mkOption {
              type = types.str;
              default = "mugit";
              description = "Group under which mugit runs.";
            };

          };

          config = mkIf cfg.enable {
            assertions = [
              {
                assertion = !(cfg.config != { } && cfg.configFile != null);
                message = "services.mugit: `config` and `configFile` are mutually exclusive. Only one can be set.";
              }
              {
                assertion = !(cfg.openFirewall && cfg.configFile != null);
                message = "services.mugit: `openFirewall` cannot be used with `configFile`. Set firewall rules manually or use `config` instead.";
              }
            ];

            environment.systemPackages = mkIf cfg.exposeCli [ mugitWithCompletions ];

            networking.firewall = mkIf cfg.openFirewall {
              allowedTCPPorts =
                let
                  serverPort = cfg.config.server.port or 8080;
                  sshPort = cfg.config.ssh.port or 2222;
                  sshEnabled = cfg.config.ssh.enable or false;
                in
                [ serverPort ] ++ lib.optional sshEnabled sshPort;
            };

            users.users.${cfg.user} = {
              isSystemUser = true;
              group = cfg.group;
              home = cfg.dataDir;
              createHome = true;
              description = "mugit service user";
            };

            users.groups.${cfg.group} = { };

            systemd.services.mugit = {
              description = "mugit service";
              wantedBy = [ "multi-user.target" ];
              after = [ "network.target" ];
              path = [ pkgs.git ];

              serviceConfig =
                let
                  serverPort = cfg.config.server.port or 8080;
                  sshPort = cfg.config.ssh.port or 2222;
                  sshEnabled = cfg.config.ssh.enable or false;
                  needsPrivPort = serverPort < 1024 || (sshEnabled && sshPort < 1024);
                in
                {
                  Type = "simple";
                  User = cfg.user;
                  Group = cfg.group;
                  WorkingDirectory = cfg.dataDir;
                  StateDirectory = "mugit";
                  ExecStart = "${cfg.package}/bin/mugit serve --config ${configFile}";
                  Restart = "on-failure";
                  RestartSec = "5s";
                  NoNewPrivileges = true;
                  PrivateTmp = true;
                  ProtectSystem = "strict";
                  ProtectHome = true;
                  ReadWritePaths = [ cfg.dataDir ];
                  ProtectKernelTunables = true;
                  ProtectKernelModules = true;
                  ProtectControlGroups = true;
                }
                // lib.optionalAttrs needsPrivPort {
                  AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
                };
            };
          };
        };
    };
}