about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/galene.nix
blob: 2fef43753d799a0d186679332c389e275bba5ab5 (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
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
{ config, lib, options, pkgs, ... }:

with lib;
let
  cfg = config.services.galene;
  opt = options.services.galene;
  defaultstateDir = "/var/lib/galene";
  defaultrecordingsDir = "${cfg.stateDir}/recordings";
  defaultgroupsDir = "${cfg.stateDir}/groups";
  defaultdataDir = "${cfg.stateDir}/data";
in
{
  options = {
    services.galene = {
      enable = mkEnableOption "Galene Service.";

      stateDir = mkOption {
        default = defaultstateDir;
        type = types.str;
        description = lib.mdDoc ''
          The directory where Galene stores its internal state. If left as the default
          value this directory will automatically be created before the Galene server
          starts, otherwise the sysadmin is responsible for ensuring the directory
          exists with appropriate ownership and permissions.
        '';
      };

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

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

      insecure = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether Galene should listen in http or in https. If left as the default
          value (false), Galene needs to be fed a private key and a certificate.
        '';
      };

      certFile = mkOption {
        type = types.nullOr types.str;
        default = null;
        example = "/path/to/your/cert.pem";
        description = lib.mdDoc ''
          Path to the server's certificate. The file is copied at runtime to
          Galene's data directory where it needs to reside.
        '';
      };

      keyFile = mkOption {
        type = types.nullOr types.str;
        default = null;
        example = "/path/to/your/key.pem";
        description = lib.mdDoc ''
          Path to the server's private key. The file is copied at runtime to
          Galene's data directory where it needs to reside.
        '';
      };

      httpAddress = mkOption {
        type = types.str;
        default = "";
        description = lib.mdDoc "HTTP listen address for galene.";
      };

      httpPort = mkOption {
        type = types.port;
        default = 8443;
        description = lib.mdDoc "HTTP listen port.";
      };

      staticDir = mkOption {
        type = types.str;
        default = "${cfg.package.static}/static";
        defaultText = literalExpression ''"''${package.static}/static"'';
        example = "/var/lib/galene/static";
        description = lib.mdDoc "Web server directory.";
      };

      recordingsDir = mkOption {
        type = types.str;
        default = defaultrecordingsDir;
        defaultText = literalExpression ''"''${config.${opt.stateDir}}/recordings"'';
        example = "/var/lib/galene/recordings";
        description = lib.mdDoc "Recordings directory.";
      };

      dataDir = mkOption {
        type = types.str;
        default = defaultdataDir;
        defaultText = literalExpression ''"''${config.${opt.stateDir}}/data"'';
        example = "/var/lib/galene/data";
        description = lib.mdDoc "Data directory.";
      };

      groupsDir = mkOption {
        type = types.str;
        default = defaultgroupsDir;
        defaultText = literalExpression ''"''${config.${opt.stateDir}}/groups"'';
        example = "/var/lib/galene/groups";
        description = lib.mdDoc "Web server directory.";
      };

      package = mkOption {
        default = pkgs.galene;
        defaultText = literalExpression "pkgs.galene";
        type = types.package;
        description = lib.mdDoc ''
          Package for running Galene.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.insecure || (cfg.certFile != null && cfg.keyFile != null);
        message = ''
          Galene needs both certFile and keyFile defined for encryption, or
          the insecure flag.
        '';
      }
    ];

    systemd.services.galene = {
      description = "galene";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      preStart = ''
        ${optionalString (cfg.insecure != true) ''
           install -m 700 -o '${cfg.user}' -g '${cfg.group}' ${cfg.certFile} ${cfg.dataDir}/cert.pem
           install -m 700 -o '${cfg.user}' -g '${cfg.group}' ${cfg.keyFile} ${cfg.dataDir}/key.pem
        ''}
      '';

      serviceConfig = mkMerge [
        {
          Type = "simple";
          User = cfg.user;
          Group = cfg.group;
          WorkingDirectory = cfg.stateDir;
          ExecStart = ''${cfg.package}/bin/galene \
          ${optionalString (cfg.insecure) "-insecure"} \
          -data ${cfg.dataDir} \
          -groups ${cfg.groupsDir} \
          -recordings ${cfg.recordingsDir} \
          -static ${cfg.staticDir}'';
          Restart = "always";
          # Upstream Requirements
          LimitNOFILE = 65536;
          StateDirectory = [ ] ++
            optional (cfg.stateDir == defaultstateDir) "galene" ++
            optional (cfg.dataDir == defaultdataDir) "galene/data" ++
            optional (cfg.groupsDir == defaultgroupsDir) "galene/groups" ++
            optional (cfg.recordingsDir == defaultrecordingsDir) "galene/recordings";

          # 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.recordingsDir;
          RemoveIPC = true;
          RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          RestrictSUIDSGID = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
          UMask = "0077";
        }
      ];
    };

    users.users = mkIf (cfg.user == "galene")
      {
        galene = {
          description = "galene Service";
          group = cfg.group;
          isSystemUser = true;
        };
      };

    users.groups = mkIf (cfg.group == "galene") {
      galene = { };
    };
  };
  meta.maintainers = with lib.maintainers; [ rgrunbla ];
}