8 cfg = config.services.asterisk;
10 asteriskUser = "asterisk";
11 asteriskGroup = "asterisk";
13 varlibdir = "/var/lib/asterisk";
14 spooldir = "/var/spool/asterisk";
15 logdir = "/var/log/asterisk";
17 # Add filecontents from files of useTheseDefaultConfFiles to confFiles, do not override
18 defaultConfFiles = lib.subtractLists (lib.attrNames cfg.confFiles) cfg.useTheseDefaultConfFiles;
21 # Default asterisk.conf file
22 "asterisk.conf".text = ''
24 astetcdir => /etc/asterisk
25 astmoddir => ${cfg.package}/lib/asterisk/modules
26 astvarlibdir => /var/lib/asterisk
27 astdbdir => /var/lib/asterisk
28 astkeydir => /var/lib/asterisk
29 astdatadir => /var/lib/asterisk
30 astagidir => /var/lib/asterisk/agi-bin
31 astspooldir => /var/spool/asterisk
32 astrundir => /run/asterisk
33 astlogdir => /var/log/asterisk
34 astsbindir => ${cfg.package}/sbin
38 # Loading all modules by default is considered sensible by the authors of
39 # "Asterisk: The Definitive Guide". Secure sites will likely want to
40 # specify their own "modules.conf" in the confFiles option.
41 "modules.conf".text = ''
46 # Use syslog for logging so logs can be viewed with journalctl
47 "logger.conf".text = ''
51 syslog.local0 => notice,warning,error
54 // lib.mapAttrs (name: text: { inherit text; }) cfg.confFiles
56 map (x: lib.nameValuePair x { source = cfg.package + "/etc/asterisk/" + x; }) defaultConfFiles
64 enable = lib.mkOption {
65 type = lib.types.bool;
68 Whether to enable the Asterisk PBX server.
72 extraConfig = lib.mkOption {
74 type = lib.types.lines;
81 Extra configuration options appended to the default
86 confFiles = lib.mkOption {
88 type = lib.types.attrsOf lib.types.str;
89 example = lib.literalExpression ''
91 "extensions.conf" = '''
93 ; Dial 100 for "hello, world"
94 exten => 100,1,Answer()
96 same => n,Playback(hello-world)
106 allowguest=no ; Require authentication
107 context=unauthorized ; Send unauthorized users to /dev/null
108 srvlookup=no ; Don't do DNS lookup
109 udpbindaddr=0.0.0.0 ; Listen on all interfaces
110 nat=force_rport,comedia ; Assume device is behind NAT
113 type=friend ; Match on username first, IP second
114 context=softphones ; Send to softphones context in
115 ; extensions.conf file
116 host=dynamic ; Device will register with asterisk
117 disallow=all ; Manually specify codecs to allow
123 secret=GhoshevFew ; Change this password!
129 ; Add debug output to log
130 syslog.local0 => notice,warning,error,debug
135 Sets the content of config files (typically ending with
136 `.conf`) in the Asterisk configuration directory.
138 Note that if you want to change `asterisk.conf`, it
139 is preferable to use the {option}`services.asterisk.extraConfig`
140 option over this option. If `"asterisk.conf"` is
141 specified with the {option}`confFiles` option (not recommended),
142 you must be prepared to set your own `astetcdir`
146 <https://www.asterisk.org/community/documentation/>
147 for more examples of what is possible here.
151 useTheseDefaultConfFiles = lib.mkOption {
174 "res_config_sqlite3.conf"
180 type = lib.types.listOf lib.types.str;
186 Sets these config files to the default content. The default value for
187 this option contains all necesscary files to avoid errors at startup.
188 This does not override settings via {option}`services.asterisk.confFiles`.
192 extraArguments = lib.mkOption {
194 type = lib.types.listOf lib.types.str;
201 Additional command line arguments to pass to Asterisk.
204 package = lib.mkPackageOption pkgs "asterisk" { };
208 config = lib.mkIf cfg.enable {
209 environment.systemPackages = [ cfg.package ];
211 environment.etc = lib.mapAttrs' (
212 name: value: lib.nameValuePair "asterisk/${name}" value
215 users.users.asterisk = {
217 group = asteriskGroup;
218 uid = config.ids.uids.asterisk;
219 description = "Asterisk daemon user";
223 users.groups.asterisk = {
224 name = asteriskGroup;
225 gid = config.ids.gids.asterisk;
228 systemd.services.asterisk = {
233 wantedBy = [ "multi-user.target" ];
235 # Do not restart, to avoid disruption of running calls. Restart unit by yourself!
236 restartIfChanged = false;
239 # Copy skeleton directory tree to /var
240 for d in '${varlibdir}' '${spooldir}' '${logdir}'; do
241 # TODO: Make exceptions for /var directories that likely should be updated
242 if [ ! -e "$d" ]; then
244 cp --recursive ${cfg.package}/"$d"/* "$d"/
245 chown --recursive ${asteriskUser}:${asteriskGroup} "$d"
246 find "$d" -type d | xargs chmod 0755
254 # FIXME: This doesn't account for arguments with spaces
255 argString = lib.concatStringsSep " " cfg.extraArguments;
257 "${cfg.package}/bin/asterisk -U ${asteriskUser} -C /etc/asterisk/asterisk.conf ${argString} -F";
259 ${cfg.package}/bin/asterisk -x "core reload"
262 PIDFile = "/run/asterisk/asterisk.pid";