1{ config, lib, pkgs, ...}:
2with lib;
3let
4 cfg = config.services.freeswitch;
5 pkg = cfg.package;
6 configDirectory = pkgs.runCommand "freeswitch-config-d" { } ''
7 mkdir -p $out
8 cp -rT ${cfg.configTemplate} $out
9 chmod -R +w $out
10 ${concatStringsSep "\n" (mapAttrsToList (fileName: filePath: ''
11 mkdir -p $out/$(dirname ${fileName})
12 cp ${filePath} $out/${fileName}
13 '') cfg.configDir)}
14 '';
15 configPath = if cfg.enableReload
16 then "/etc/freeswitch"
17 else configDirectory;
18in {
19 options = {
20 services.freeswitch = {
21 enable = mkEnableOption (lib.mdDoc "FreeSWITCH");
22 enableReload = mkOption {
23 default = false;
24 type = types.bool;
25 description = lib.mdDoc ''
26 Issue the `reloadxml` command to FreeSWITCH when configuration directory changes (instead of restart).
27 See [FreeSWITCH documentation](https://freeswitch.org/confluence/display/FREESWITCH/Reloading) for more info.
28 The configuration directory is exposed at {file}`/etc/freeswitch`.
29 See also `systemd.services.*.restartIfChanged`.
30 '';
31 };
32 configTemplate = mkOption {
33 type = types.path;
34 default = "${config.services.freeswitch.package}/share/freeswitch/conf/vanilla";
35 defaultText = literalExpression ''"''${config.services.freeswitch.package}/share/freeswitch/conf/vanilla"'';
36 example = literalExpression ''"''${config.services.freeswitch.package}/share/freeswitch/conf/minimal"'';
37 description = lib.mdDoc ''
38 Configuration template to use.
39 See available templates in [FreeSWITCH repository](https://github.com/signalwire/freeswitch/tree/master/conf).
40 You can also set your own configuration directory.
41 '';
42 };
43 configDir = mkOption {
44 type = with types; attrsOf path;
45 default = { };
46 example = literalExpression ''
47 {
48 "freeswitch.xml" = ./freeswitch.xml;
49 "dialplan/default.xml" = pkgs.writeText "dialplan-default.xml" '''
50 [xml lines]
51 ''';
52 }
53 '';
54 description = lib.mdDoc ''
55 Override file in FreeSWITCH config template directory.
56 Each top-level attribute denotes a file path in the configuration directory, its value is the file path.
57 See [FreeSWITCH documentation](https://freeswitch.org/confluence/display/FREESWITCH/Default+Configuration) for more info.
58 Also check available templates in [FreeSWITCH repository](https://github.com/signalwire/freeswitch/tree/master/conf).
59 '';
60 };
61 package = mkOption {
62 type = types.package;
63 default = pkgs.freeswitch;
64 defaultText = literalExpression "pkgs.freeswitch";
65 description = lib.mdDoc ''
66 FreeSWITCH package.
67 '';
68 };
69 };
70 };
71 config = mkIf cfg.enable {
72 environment.etc.freeswitch = mkIf cfg.enableReload {
73 source = configDirectory;
74 };
75 systemd.services.freeswitch-config-reload = mkIf cfg.enableReload {
76 before = [ "freeswitch.service" ];
77 wantedBy = [ "multi-user.target" ];
78 restartTriggers = [ configDirectory ];
79 serviceConfig = {
80 ExecStart = "/run/current-system/systemd/bin/systemctl try-reload-or-restart freeswitch.service";
81 RemainAfterExit = true;
82 Type = "oneshot";
83 };
84 };
85 systemd.services.freeswitch = {
86 description = "Free and open-source application server for real-time communication";
87 after = [ "network.target" ];
88 wantedBy = [ "multi-user.target" ];
89 serviceConfig = {
90 DynamicUser = true;
91 StateDirectory = "freeswitch";
92 ExecStart = "${pkg}/bin/freeswitch -nf \\
93 -mod ${pkg}/lib/freeswitch/mod \\
94 -conf ${configPath} \\
95 -base /var/lib/freeswitch";
96 ExecReload = "${pkg}/bin/fs_cli -x reloadxml";
97 Restart = "on-failure";
98 RestartSec = "5s";
99 CPUSchedulingPolicy = "fifo";
100 };
101 };
102 environment.systemPackages = [ pkg ];
103 };
104}