about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/networking/legit.nix
blob: ff8e0dd4f93caddd34be49dce4709d11264dd380 (plain) (blame)
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
{ config, lib, pkgs, ... }:

let
  inherit (lib)
    literalExpression
    mkEnableOption
    mdDoc
    mkIf
    mkOption
    mkPackageOption
    optionalAttrs
    optional
    types;

  cfg = config.services.legit;

  yaml = pkgs.formats.yaml { };
  configFile = yaml.generate "legit.yaml" cfg.settings;

  defaultStateDir = "/var/lib/legit";
  defaultStaticDir = "${cfg.settings.repo.scanPath}/static";
  defaultTemplatesDir = "${cfg.settings.repo.scanPath}/templates";
in
{
  options.services.legit = {
    enable = mkEnableOption (mdDoc "legit git web frontend");

    package = mkPackageOption pkgs "legit-web" { };

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

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

    settings = mkOption {
      default = { };
      description = mdDoc ''
        The primary legit configuration. See the
        [sample configuration](https://github.com/icyphox/legit/blob/master/config.yaml)
        for possible values.
      '';
      type = types.submodule {
        options.repo = {
          scanPath = mkOption {
            type = types.path;
            default = defaultStateDir;
            description = mdDoc "Directory where legit will scan for repositories.";
          };
          readme = mkOption {
            type = types.listOf types.str;
            default = [ ];
            description = mdDoc "Readme files to look for.";
          };
          mainBranch = mkOption {
            type = types.listOf types.str;
            default = [ "main" "master" ];
            description = mdDoc "Main branch to look for.";
          };
          ignore = mkOption {
            type = types.listOf types.str;
            default = [ ];
            description = mdDoc "Repositories to ignore.";
          };
        };
        options.dirs = {
          templates = mkOption {
            type = types.path;
            default = "${pkgs.legit-web}/lib/legit/templates";
            defaultText = literalExpression ''"''${pkgs.legit-web}/lib/legit/templates"'';
            description = mdDoc "Directories where template files are located.";
          };
          static = mkOption {
            type = types.path;
            default = "${pkgs.legit-web}/lib/legit/static";
            defaultText = literalExpression ''"''${pkgs.legit-web}/lib/legit/static"'';
            description = mdDoc "Directories where static files are located.";
          };
        };
        options.meta = {
          title = mkOption {
            type = types.str;
            default = "legit";
            description = mdDoc "Website title.";
          };
          description = mkOption {
            type = types.str;
            default = "git frontend";
            description = mdDoc "Website description.";
          };
        };
        options.server = {
          name = mkOption {
            type = types.str;
            default = "localhost";
            description = mdDoc "Server name.";
          };
          host = mkOption {
            type = types.str;
            default = "127.0.0.1";
            description = mdDoc "Host address.";
          };
          port = mkOption {
            type = types.port;
            default = 5555;
            description = mdDoc "Legit port.";
          };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    users.groups = optionalAttrs (cfg.group == "legit") {
      "${cfg.group}" = { };
    };

    users.users = optionalAttrs (cfg.user == "legit") {
      "${cfg.user}" = {
        group = cfg.group;
        isSystemUser = true;
      };
    };

    systemd.services.legit = {
      description = "legit git frontend";

      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ configFile ];

      serviceConfig = {
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        ExecStart = "${cfg.package}/bin/legit -config ${configFile}";
        Restart = "always";

        WorkingDirectory = cfg.settings.repo.scanPath;
        StateDirectory = [ ] ++
          optional (cfg.settings.repo.scanPath == defaultStateDir) "legit" ++
          optional (cfg.settings.dirs.static == defaultStaticDir) "legit/static" ++
          optional (cfg.settings.dirs.templates == defaultTemplatesDir) "legit/templates";

        # Hardening
        CapabilityBoundingSet = [ "" ];
        DeviceAllow = [ "" ];
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        ReadWritePaths = cfg.settings.repo.scanPath;
        RemoveIPC = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "@system-service" "~@privileged" ];
        UMask = "0077";
      };
    };
  };
}