summary refs log tree commit diff
path: root/nixos/modules/services/editors/infinoted.nix
blob: 16fe52a232bd4bc04af94b038f6a99632ac34973 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.infinoted;
in {
  options.services.infinoted = {
    enable = mkEnableOption "infinoted";

    package = mkOption {
      type = types.package;
      default = pkgs.libinfinity;
      defaultText = literalExpression "pkgs.libinfinity";
      description = ''
        Package providing infinoted
      '';
    };

    keyFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Private key to use for TLS
      '';
    };

    certificateFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Server certificate to use for TLS
      '';
    };

    certificateChain = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        Chain of CA-certificates to which our `certificateFile` is relative.
        Optional for TLS.
      '';
    };

    securityPolicy = mkOption {
      type = types.enum ["no-tls" "allow-tls" "require-tls"];
      default = "require-tls";
      description = ''
        How strictly to enforce clients connection with TLS.
      '';
    };

    port = mkOption {
      type = types.port;
      default = 6523;
      description = ''
        Port to listen on
      '';
    };

    rootDirectory = mkOption {
      type = types.path;
      default = "/var/lib/infinoted/documents/";
      description = ''
        Root of the directory structure to serve
      '';
    };

    plugins = mkOption {
      type = types.listOf types.str;
      default = [ "note-text" "note-chat" "logging" "autosave" ];
      description = ''
        Plugins to enable
      '';
    };

    passwordFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        File to read server-wide password from
      '';
    };

    extraConfig = mkOption {
      type = types.lines;
      default = ''
        [autosave]
        interval=10
      '';
      description = ''
        Additional configuration to append to infinoted.conf
      '';
    };

    user = mkOption {
      type = types.str;
      default = "infinoted";
      description = ''
        What to call the dedicated user under which infinoted is run
      '';
    };

    group = mkOption {
      type = types.str;
      default = "infinoted";
      description = ''
        What to call the primary group of the dedicated user under which infinoted is run
      '';
    };
  };

  config = mkIf (cfg.enable) {
    users.users = optionalAttrs (cfg.user == "infinoted")
      { infinoted = {
          description = "Infinoted user";
          group = cfg.group;
          isSystemUser = true;
        };
      };
    users.groups = optionalAttrs (cfg.group == "infinoted")
      { infinoted = { };
      };

    systemd.services.infinoted =
      { description = "Gobby Dedicated Server";

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

        serviceConfig = {
          Type = "simple";
          Restart = "always";
          ExecStart = "${cfg.package.infinoted} --config-file=/var/lib/infinoted/infinoted.conf";
          User = cfg.user;
          Group = cfg.group;
          PermissionsStartOnly = true;
        };
        preStart = ''
          mkdir -p /var/lib/infinoted
          install -o ${cfg.user} -g ${cfg.group} -m 0600 /dev/null /var/lib/infinoted/infinoted.conf
          cat >>/var/lib/infinoted/infinoted.conf <<EOF
          [infinoted]
          ${optionalString (cfg.keyFile != null) "key-file=${cfg.keyFile}"}
          ${optionalString (cfg.certificateFile != null) "certificate-file=${cfg.certificateFile}"}
          ${optionalString (cfg.certificateChain != null) "certificate-chain=${cfg.certificateChain}"}
          port=${toString cfg.port}
          security-policy=${cfg.securityPolicy}
          root-directory=${cfg.rootDirectory}
          plugins=${concatStringsSep ";" cfg.plugins}
          ${optionalString (cfg.passwordFile != null) "password=$(head -n 1 ${cfg.passwordFile})"}

          ${cfg.extraConfig}
          EOF

          install -o ${cfg.user} -g ${cfg.group} -m 0750 -d ${cfg.rootDirectory}
        '';
      };
  };
}