blob: 7e9130c8ff4dc71cb74f1465fd155ee5c5967e4b (
plain)
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
|
{
config,
lib,
pkgs,
...
}:
let
settingsFormat = {
type =
with lib.types;
attrsOf (oneOf [
bool
int
str
]);
generate =
name: attrs:
pkgs.writeText name (
lib.strings.concatStringsSep "\n" (
lib.attrsets.mapAttrsToList (key: value: "${key}=${builtins.toJSON value}") attrs
)
);
};
in
{
options = {
services.uhub = lib.mkOption {
default = { };
description = "Uhub ADC hub instances";
type = lib.types.attrsOf (
lib.types.submodule {
options = {
enable = lib.mkEnableOption "hub instance" // {
default = true;
};
enableTLS = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to enable TLS support.";
};
settings = lib.mkOption {
inherit (settingsFormat) type;
description = ''
Configuration of uhub.
See <https://www.uhub.org/doc/config.php> for a list of options.
'';
default = { };
example = {
server_bind_addr = "any";
server_port = 1511;
hub_name = "My Public Hub";
hub_description = "Yet another ADC hub";
max_users = 150;
};
};
plugins = lib.mkOption {
description = "Uhub plugin configuration.";
type =
with lib.types;
listOf (submodule {
options = {
plugin = lib.mkOption {
type = path;
example = lib.literalExpression "$${pkgs.uhub}/plugins/mod_auth_sqlite.so";
description = "Path to plugin file.";
};
settings = lib.mkOption {
description = "Settings specific to this plugin.";
type = with types; attrsOf str;
example = {
file = "/etc/uhub/users.db";
};
};
};
});
default = [ ];
};
};
}
);
};
};
config =
let
hubs = lib.attrsets.filterAttrs (_: cfg: cfg.enable) config.services.uhub;
in
{
environment.etc = lib.attrsets.mapAttrs' (
name: cfg:
let
settings' = cfg.settings // {
tls_enable = cfg.enableTLS;
file_plugins = pkgs.writeText "uhub-plugins.conf" (
lib.strings.concatStringsSep "\n" (
map (
{ plugin, settings }:
''plugin ${plugin} "${
toString (lib.attrsets.mapAttrsToList (key: value: "${key}=${value}") settings)
}"''
) cfg.plugins
)
);
};
in
{
name = "uhub/${name}.conf";
value.source = settingsFormat.generate "uhub-${name}.conf" settings';
}
) hubs;
systemd.services = lib.attrsets.mapAttrs' (name: cfg: {
name = "uhub-${name}";
value =
let
pkg = pkgs.uhub.override { tlsSupport = cfg.enableTLS; };
in
{
description = "high performance peer-to-peer hub for the ADC network";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
reloadIfChanged = true;
serviceConfig = {
Type = "notify";
ExecStart = "${pkg}/bin/uhub -c /etc/uhub/${name}.conf -L";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
DynamicUser = true;
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
};
};
}) hubs;
};
}
|