1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 settingsFormat = {
7 type = with lib.types; attrsOf (oneOf [ bool int str ]);
8 generate = name: attrs:
9 pkgs.writeText name (lib.strings.concatStringsSep "\n"
10 (lib.attrsets.mapAttrsToList
11 (key: value: "${key}=${builtins.toJSON value}") attrs));
12 };
13in {
14 options = {
15
16 services.uhub = mkOption {
17 default = { };
18 description = "Uhub ADC hub instances";
19 type = types.attrsOf (types.submodule {
20 options = {
21
22 enable = mkEnableOption "hub instance" // { default = true; };
23
24 enableTLS = mkOption {
25 type = types.bool;
26 default = false;
27 description = "Whether to enable TLS support.";
28 };
29
30 settings = mkOption {
31 inherit (settingsFormat) type;
32 description = ''
33 Configuration of uhub.
34 See https://www.uhub.org/doc/config.php for a list of options.
35 '';
36 default = { };
37 example = {
38 server_bind_addr = "any";
39 server_port = 1511;
40 hub_name = "My Public Hub";
41 hub_description = "Yet another ADC hub";
42 max_users = 150;
43 };
44 };
45
46 plugins = mkOption {
47 description = "Uhub plugin configuration.";
48 type = with types;
49 listOf (submodule {
50 options = {
51 plugin = mkOption {
52 type = path;
53 example = literalExpression
54 "$${pkgs.uhub}/plugins/mod_auth_sqlite.so";
55 description = "Path to plugin file.";
56 };
57 settings = mkOption {
58 description = "Settings specific to this plugin.";
59 type = with types; attrsOf str;
60 example = { file = "/etc/uhub/users.db"; };
61 };
62 };
63 });
64 default = [ ];
65 };
66
67 };
68 });
69 };
70
71 };
72
73 config = let
74 hubs = lib.attrsets.filterAttrs (_: cfg: cfg.enable) config.services.uhub;
75 in {
76
77 environment.etc = lib.attrsets.mapAttrs' (name: cfg:
78 let
79 settings' = cfg.settings // {
80 tls_enable = cfg.enableTLS;
81 file_plugins = pkgs.writeText "uhub-plugins.conf"
82 (lib.strings.concatStringsSep "\n" (map ({ plugin, settings }:
83 "plugin ${plugin} ${
84 toString
85 (lib.attrsets.mapAttrsToList (key: value: ''"${key}=${value}"'')
86 settings)
87 }") cfg.plugins));
88 };
89 in {
90 name = "uhub/${name}.conf";
91 value.source = settingsFormat.generate "uhub-${name}.conf" settings';
92 }) hubs;
93
94 systemd.services = lib.attrsets.mapAttrs' (name: cfg: {
95 name = "uhub-${name}";
96 value = let pkg = pkgs.uhub.override { tlsSupport = cfg.enableTLS; };
97 in {
98 description = "high performance peer-to-peer hub for the ADC network";
99 after = [ "network.target" ];
100 wantedBy = [ "multi-user.target" ];
101 reloadIfChanged = true;
102 serviceConfig = {
103 Type = "notify";
104 ExecStart = "${pkg}/bin/uhub -c /etc/uhub/${name}.conf -L";
105 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
106 DynamicUser = true;
107 };
108 };
109 }) hubs;
110 };
111
112}