aboutsummaryrefslogtreecommitdiff
path: root/modules/webapps/etherpad-lite.nix
blob: 3e951c5118bb10022b7c2096579b958e721ed998 (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
{ lib, pkgs, config, ... }:
let
  name = "etherpad-lite";
  cfg = config.services.etherpad-lite;

  uid = config.ids.uids.etherpad-lite;
  gid = config.ids.gids.etherpad-lite;
in
{
  options.services.etherpad-lite = {
    enable = lib.mkEnableOption "Enable Etherpad lite’s service";
    user = lib.mkOption {
      type = lib.types.str;
      default = name;
      description = "User account under which Etherpad lite runs";
    };
    group = lib.mkOption {
      type = lib.types.str;
      default = name;
      description = "Group under which Etherpad lite runs";
    };
    dataDir = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/${name}";
      description = ''
        The directory where Etherpad lite stores its data.
      '';
    };
    configFile = lib.mkOption {
      type = lib.types.path;
      description = ''
        The config file path for Etherpad lite.
        '';
    };
    sessionKeyFile = lib.mkOption {
      type = lib.types.path;
      description = ''
        The Session key file path for Etherpad lite.
        '';
    };
    apiKeyFile = lib.mkOption {
      type = lib.types.path;
      description = ''
        The API key file path for Etherpad lite.
        '';
    };
    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.webapps.etherpad-lite;
      description = ''
        Etherpad lite package to use.
        '';
    };
    modules = lib.mkOption {
      type = lib.types.listOf lib.types.package;
      default = [];
      description = ''
        Etherpad lite modules to use.
        '';
    };
    # Output variables
    workdir = lib.mkOption {
      type = lib.types.package;
      default = cfg.package.withModules cfg.modules;
      description = ''
      Adjusted Etherpad lite package with plugins
      '';
      readOnly = true;
    };
    systemdStateDirectory = lib.mkOption {
      type = lib.types.str;
      # Use ReadWritePaths= instead if varDir is outside of /var/lib
      default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
        lib.strings.removePrefix "/var/lib/" cfg.dataDir;
      description = ''
      Adjusted Etherpad lite data directory for systemd
      '';
      readOnly = true;
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.etherpad-lite = {
      description = "Etherpad-lite";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "postgresql.service" ];
      wants = [ "postgresql.service" ];

      environment.NODE_ENV = "production";
      environment.HOME = cfg.workdir;

      path = [ pkgs.nodejs ];

      script = ''
        exec ${pkgs.nodejs}/bin/node ${cfg.workdir}/src/node/server.js \
          --sessionkey ${cfg.sessionKeyFile} \
          --apikey ${cfg.apiKeyFile} \
          --settings ${cfg.configFile}
      '';

      serviceConfig = {
        DynamicUser = true;
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.workdir;
        PrivateTmp = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        ProtectHome = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        Restart = "always";
        Type = "simple";
        TimeoutSec = 60;
        StateDirectory= cfg.systemdStateDirectory;
        ExecStartPre = [
          "+${pkgs.coreutils}/bin/install -d -m 0755 -o ${cfg.user} -g ${cfg.group} ${cfg.dataDir}/ep_initialized"
          "+${pkgs.coreutils}/bin/chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir} ${cfg.configFile} ${cfg.sessionKeyFile} ${cfg.apiKeyFile}"
        ];
      };
    };

  };
}