1 { config, lib, pkgs, ...}:
3 cfg = config.services.freeswitch;
5 configDirectory = pkgs.runCommand "freeswitch-config-d" { } ''
7 cp -rT ${cfg.configTemplate} $out
9 ${lib.concatStringsSep "\n" (lib.mapAttrsToList (fileName: filePath: ''
10 mkdir -p $out/$(dirname ${fileName})
11 cp ${filePath} $out/${fileName}
14 configPath = if cfg.enableReload
15 then "/etc/freeswitch"
19 services.freeswitch = {
20 enable = lib.mkEnableOption "FreeSWITCH";
21 enableReload = lib.mkOption {
23 type = lib.types.bool;
25 Issue the `reloadxml` command to FreeSWITCH when configuration directory changes (instead of restart).
26 See [FreeSWITCH documentation](https://freeswitch.org/confluence/display/FREESWITCH/Reloading) for more info.
27 The configuration directory is exposed at {file}`/etc/freeswitch`.
28 See also `systemd.services.*.restartIfChanged`.
31 configTemplate = lib.mkOption {
32 type = lib.types.path;
33 default = "${config.services.freeswitch.package}/share/freeswitch/conf/vanilla";
34 defaultText = lib.literalExpression ''"''${config.services.freeswitch.package}/share/freeswitch/conf/vanilla"'';
35 example = lib.literalExpression ''"''${config.services.freeswitch.package}/share/freeswitch/conf/minimal"'';
37 Configuration template to use.
38 See available templates in [FreeSWITCH repository](https://github.com/signalwire/freeswitch/tree/master/conf).
39 You can also set your own configuration directory.
42 configDir = lib.mkOption {
43 type = with lib.types; attrsOf path;
45 example = lib.literalExpression ''
47 "freeswitch.xml" = ./freeswitch.xml;
48 "dialplan/default.xml" = pkgs.writeText "dialplan-default.xml" '''
54 Override file in FreeSWITCH config template directory.
55 Each top-level attribute denotes a file path in the configuration directory, its value is the file path.
56 See [FreeSWITCH documentation](https://freeswitch.org/confluence/display/FREESWITCH/Default+Configuration) for more info.
57 Also check available templates in [FreeSWITCH repository](https://github.com/signalwire/freeswitch/tree/master/conf).
60 package = lib.mkPackageOption pkgs "freeswitch" { };
63 config = lib.mkIf cfg.enable {
64 environment.etc.freeswitch = lib.mkIf cfg.enableReload {
65 source = configDirectory;
67 systemd.services.freeswitch-config-reload = lib.mkIf cfg.enableReload {
68 before = [ "freeswitch.service" ];
69 wantedBy = [ "multi-user.target" ];
70 restartTriggers = [ configDirectory ];
72 ExecStart = "/run/current-system/systemd/bin/systemctl try-reload-or-restart freeswitch.service";
73 RemainAfterExit = true;
77 systemd.services.freeswitch = {
78 description = "Free and open-source application server for real-time communication";
79 after = [ "network.target" ];
80 wantedBy = [ "multi-user.target" ];
83 StateDirectory = "freeswitch";
84 ExecStart = "${pkg}/bin/freeswitch -nf \\
85 -mod ${pkg}/lib/freeswitch/mod \\
86 -conf ${configPath} \\
87 -base /var/lib/freeswitch";
88 ExecReload = "${pkg}/bin/fs_cli -x reloadxml";
89 Restart = "on-failure";
91 CPUSchedulingPolicy = "fifo";
94 environment.systemPackages = [ pkg ];