···
+
{ config, lib, pkgs, ... }:
+
cfg = config.services.asterisk;
+
asteriskUser = "asterisk";
+
varlibdir = "/var/lib/asterisk";
+
spooldir = "/var/spool/asterisk";
+
logdir = "/var/log/asterisk";
+
asteriskEtc = pkgs.stdenv.mkDerivation
+
((mapAttrs' (name: value: nameValuePair
+
# Fudge the names to make bash happy
+
((replaceChars ["."] ["_"] name) + "_")
+
confFilesString = concatStringsSep " " (
+
attrNames cfg.confFiles
+
# Default asterisk.conf file
+
# (Notice that astetcdir will be set to the path of this derivation)
+
astmoddir => ${pkgs.asterisk}/lib/asterisk/modules
+
astvarlibdir => /var/lib/asterisk
+
astdbdir => /var/lib/asterisk
+
astkeydir => /var/lib/asterisk
+
astdatadir => /var/lib/asterisk
+
astagidir => /var/lib/asterisk/agi-bin
+
astspooldir => /var/spool/asterisk
+
astrundir => /var/run/asterisk
+
astlogdir => /var/log/asterisk
+
astsbindir => ${pkgs.asterisk}/sbin
+
extraConf = cfg.extraConfig;
+
# Loading all modules by default is considered sensible by the authors of
+
# "Asterisk: The Definitive Guide". Secure sites will likely want to
+
# specify their own "modules.conf" in the confFiles option.
+
# Use syslog for logging so logs can be viewed with journalctl
+
syslog.local0 => notice,warning,error
+
# Create asterisk.conf, pointing astetcdir to the path of this derivation
+
echo "$asteriskConf" | sed "s|@out@|$out|g" > "$out"/asterisk.conf
+
echo "$extraConf" >> "$out"/asterisk.conf
+
echo "$modulesConf" > "$out"/modules.conf
+
echo "$loggerConf" > "$out"/logger.conf
+
# Config files specified in confFiles option override all other files
+
for i in $confFilesString; do
+
conf=$(echo "$i"_ | sed 's/\./_/g')
+
echo "''${!conf}" > "$out"/"$i"
+
Whether to enable the Asterisk PBX server.
+
extraConfig = mkOption {
+
Extra configuration options appended to the default
+
<literal>asterisk.conf</literal> file.
+
type = types.attrsOf types.str;
+
example = literalExample
+
"extensions.conf" = '''
+
; Dial 100 for "hello, world"
+
exten => 100,1,Answer()
+
same => n,Playback(hello-world)
+
allowguest=no ; Require authentication
+
context=unauthorized ; Send unauthorized users to /dev/null
+
srvlookup=no ; Don't do DNS lookup
+
udpbindaddr=0.0.0.0 ; Listen on all interfaces
+
nat=force_rport,comedia ; Assume device is behind NAT
+
type=friend ; Match on username first, IP second
+
context=softphones ; Send to softphones context in
+
host=dynamic ; Device will register with asterisk
+
disallow=all ; Manually specify codecs to allow
+
secret=GhoshevFew ; Change this password!
+
; Add debug output to log
+
syslog.local0 => notice,warning,error,debug
+
Sets the content of config files (typically ending with
+
<literal>.conf</literal>) in the Asterisk configuration directory.
+
Note that if you want to change <literal>asterisk.conf</literal>, it
+
is preferable to use the <option>services.asterisk.extraConfig</option>
+
option over this option. If <literal>"asterisk.conf"</literal> is
+
specified with the <option>confFiles</option> option (not recommended),
+
you must be prepared to set your own <literal>astetcdir</literal>
+
<link xlink:href="http://www.asterisk.org/community/documentation"/>
+
for more examples of what is possible here.
+
extraArguments = mkOption {
+
type = types.listOf types.str;
+
[ "-vvvddd" "-e" "1024" ];
+
Additional command line arguments to pass to Asterisk.
+
config = mkIf cfg.enable {
+
users.extraUsers = singleton
+
uid = config.ids.uids.asterisk;
+
description = "Asterisk daemon user";
+
systemd.services.asterisk = {
+
wantedBy = [ "multi-user.target" ];
+
# Copy skeleton directory tree to /var
+
for d in '${varlibdir}' '${spooldir}' '${logdir}'; do
+
# TODO: Make exceptions for /var directories that likely should be updated
+
cp --recursive ${pkgs.asterisk}/"$d" "$d"
+
chown --recursive ${asteriskUser} "$d"
+
find "$d" -type d | xargs chmod 0755
+
# FIXME: This doesn't account for arguments with spaces
+
argString = concatStringsSep " " cfg.extraArguments;
+
"${pkgs.asterisk}/bin/asterisk -U ${asteriskUser} -C ${asteriskEtc}/asterisk.conf ${argString} -F";
+
PIDFile = "/var/run/asterisk/asterisk.pid";