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