summary refs log tree commit diff
path: root/nixos/modules/services/misc/uhub.nix
blob: 753580c3e404f14a7a8a6bc3bd453357c9588c93 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.uhub;

  uhubPkg = pkgs.uhub.override { tlsSupport = cfg.enableTLS; };

  pluginConfig = ""
  + optionalString cfg.plugins.authSqlite.enable ''
    plugin ${uhubPkg.mod_auth_sqlite}/mod_auth_sqlite.so "file=${cfg.plugins.authSqlite.file}"
  ''
  + optionalString cfg.plugins.logging.enable ''
    plugin ${uhubPkg.mod_logging}/mod_logging.so ${if cfg.plugins.logging.syslog then "syslog=true" else "file=${cfg.plugins.logging.file}"}
  ''
  + optionalString cfg.plugins.welcome.enable ''
    plugin ${uhubPkg.mod_welcome}/mod_welcome.so "motd=${pkgs.writeText "motd.txt"  cfg.plugins.welcome.motd} rules=${pkgs.writeText "rules.txt" cfg.plugins.welcome.rules}"
  ''
  + optionalString cfg.plugins.history.enable ''
    plugin ${uhubPkg.mod_chat_history}/mod_chat_history.so "history_max=${toString cfg.plugins.history.max} history_default=${toString cfg.plugins.history.default} history_connect=${toString cfg.plugins.history.connect}"
  '';

  uhubConfigFile = pkgs.writeText "uhub.conf" ''
    file_acl=${pkgs.writeText "users.conf" cfg.aclConfig}
    file_plugins=${pkgs.writeText "plugins.conf" pluginConfig}
    server_bind_addr=${cfg.address}
    server_port=${toString cfg.port}
    ${lib.optionalString cfg.enableTLS "tls_enable=yes"}
    ${cfg.hubConfig}
  '';

in

{
  options = {

    services.uhub = {

      enable = mkOption {
        type = types.bool;
        default = false;
	description = "Whether to enable the uhub ADC hub.";
      };

      port = mkOption {
        type = types.int;
        default = 1511;
	description = "TCP port to bind the hub to.";
      };

      address = mkOption {
        type = types.str;
        default = "any";
	description = "Address to bind the hub to.";
      };

      enableTLS = mkOption {
        type = types.bool;
        default = false;
	description = "Whether to enable TLS support.";
      };

      hubConfig = mkOption {
        type = types.lines;
        default = "";
	description = "Contents of uhub configuration file.";
      };

      aclConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Contents of user ACL configuration file.";
      };

      plugins = {

        authSqlite = {
	  enable = mkOption {
            type = types.bool;
            default = false;
            description = "Whether to enable the Sqlite authentication database plugin";
	  };
          file = mkOption {
            type = types.path;
            example = "/var/db/uhub-users";
            description = "Path to user database. Use the uhub-passwd utility to create the database and add/remove users.";
          };
        };

        logging = {
          enable = mkOption {
            type = types.bool;
            default = false;
            description = "Whether to enable the logging plugin.";
          };
          file = mkOption {
            type = types.str;
            default = "";
            description = "Path of log file.";
          };
          syslog = mkOption {
            type = types.bool;
            default = false;
            description = "If true then the system log is used instead of writing to file.";
          };
        };

        welcome = {
          enable = mkOption {
            type = types.bool;
            default = false;
            description = "Whether to enable the welcome plugin.";
          };
          motd = mkOption {
            default = "";
            type = types.lines;
            description = ''
              Welcome message displayed to clients after connecting
              and with the <literal>!motd</literal> command.
            '';
          };
          rules = mkOption {
            default = "";
            type = types.lines;
            description = ''
              Rules message, displayed to clients with the <literal>!rules</literal> command.
            '';
          };
        };

        history = {
          enable = mkOption {
            type = types.bool;
            default = false;
            description = "Whether to enable the history plugin.";
          };
          max = mkOption {
            type = types.int;
            default = 200;
            description = "The maximum number of messages to keep in history";
          };
          default = mkOption {
            type = types.int;
            default = 10;
            description = "When !history is provided without arguments, then this default number of messages are returned.";
          };
          connect = mkOption {
            type = types.int;
            default = 5;
            description = "The number of chat history messages to send when users connect (0 = do not send any history).";
          };
        };

      };
    };

  };

  config = mkIf cfg.enable {

    users = {
      users = singleton {
        name = "uhub";
        uid = config.ids.uids.uhub;
      };
      groups = singleton {
        name = "uhub";
        gid = config.ids.gids.uhub;
      };
    };

    systemd.services.uhub = {
      description = "high performance peer-to-peer hub for the ADC network";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "notify";
        ExecStart  = "${uhubPkg}/bin/uhub -c ${uhubConfigFile} -u uhub -g uhub -L";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
      };
    };
  };

}