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 "FreeSWITCH";
22 enableReload = mkOption {
23 default = false;
24 type = types.bool;
25 description = ''
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 = ''
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 = ''
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 = mkPackageOption pkgs "freeswitch" { };
62 };
63 };
64 config = mkIf cfg.enable {
65 environment.etc.freeswitch = mkIf cfg.enableReload {
66 source = configDirectory;
67 };
68 systemd.services.freeswitch-config-reload = mkIf cfg.enableReload {
69 before = [ "freeswitch.service" ];
70 wantedBy = [ "multi-user.target" ];
71 restartTriggers = [ configDirectory ];
72 serviceConfig = {
73 ExecStart = "/run/current-system/systemd/bin/systemctl try-reload-or-restart freeswitch.service";
74 RemainAfterExit = true;
75 Type = "oneshot";
76 };
77 };
78 systemd.services.freeswitch = {
79 description = "Free and open-source application server for real-time communication";
80 after = [ "network.target" ];
81 wantedBy = [ "multi-user.target" ];
82 serviceConfig = {
83 DynamicUser = true;
84 StateDirectory = "freeswitch";
85 ExecStart = "${pkg}/bin/freeswitch -nf \\
86 -mod ${pkg}/lib/freeswitch/mod \\
87 -conf ${configPath} \\
88 -base /var/lib/freeswitch";
89 ExecReload = "${pkg}/bin/fs_cli -x reloadxml";
90 Restart = "on-failure";
91 RestartSec = "5s";
92 CPUSchedulingPolicy = "fifo";
93 };
94 };
95 environment.systemPackages = [ pkg ];
96 };
97}