summaryrefslogtreecommitdiff
path: root/pkgs/servers/asterisk/default.nix
blob: efb9949e2cdc115d7aab9d92937a50931d7009fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{
  stdenv,
  lib,
  fetchurl,
  fetchsvn,
  fetchFromGitHub,
  jansson,
  libedit,
  libxml2,
  libxslt,
  ncurses,
  openssl,
  sqlite,
  util-linux,
  dmidecode,
  libuuid,
  newt,
  lua,
  speex,
  libopus,
  opusfile,
  libogg,
  srtp,
  wget,
  curl,
  iksemel,
  pkg-config,
  autoconf,
  libtool,
  automake,
  python3,
  writeScript,
  withOpus ? true,
  ldapSupport ? false,
  openldap,
}:

let
  common =
    {
      version,
      sha256,
      externals,
      pjsip_patches ? [ ],
    }:
    stdenv.mkDerivation {
      inherit version;
      pname = "asterisk" + lib.optionalString ldapSupport "-ldap";

      buildInputs = [
        jansson
        libedit
        libxml2
        libxslt
        ncurses
        openssl
        sqlite
        dmidecode
        libuuid
        newt
        lua
        speex
        srtp
        wget
        curl
        iksemel
      ]
      ++ lib.optionals withOpus [
        libopus
        opusfile
        libogg
      ]
      ++ lib.optionals ldapSupport [ openldap ];
      nativeBuildInputs = [
        util-linux
        pkg-config
        autoconf
        libtool
        automake
      ];

      patches = [
        # We want the Makefile to install the default /var skeleton
        # under ${out}/var but we also want to use /var at runtime.
        # This patch changes the runtime behavior to look for state
        # directories in /var rather than ${out}/var.
        ./runtime-vardirs.patch
      ]
      ++ lib.optional withOpus "${asterisk-opus}/asterisk.patch";

      postPatch = ''
        echo "PJPROJECT_CONFIG_OPTS += --prefix=$out" >> third-party/pjproject/Makefile.rules
      '';

      src = fetchurl {
        url = "https://downloads.asterisk.org/pub/telephony/asterisk/old-releases/asterisk-${version}.tar.gz";
        inherit sha256;
      };

      # The default libdir is $PREFIX/usr/lib, which causes problems when paths
      # compiled into Asterisk expect ${out}/usr/lib rather than ${out}/lib.

      # Copy in externals to avoid them being downloaded;
      # they have to be copied, because the modification date is checked.
      # If you are getting a permission denied error on this dir,
      # you're likely missing an automatically downloaded dependency
      preConfigure = ''
        mkdir externals_cache

        ${lib.concatStringsSep "\n" (
          lib.mapAttrsToList (dst: src: "cp -r --no-preserve=mode ${src} ${dst}") externals
        )}

        ${lib.optionalString (externals ? "addons/mp3") "bash contrib/scripts/get_mp3_source.sh || true"}

        chmod -w externals_cache
        ${lib.optionalString withOpus ''
          cp ${asterisk-opus}/include/asterisk/* ./include/asterisk
          cp ${asterisk-opus}/codecs/* ./codecs
          cp ${asterisk-opus}/formats/* ./formats
        ''}
        ${lib.concatMapStringsSep "\n" (patch: ''
          cp ${patch} ./third-party/pjproject/patches/${patch.name}
        '') pjsip_patches}
        ./bootstrap.sh
      '';

      configureFlags = [
        "--libdir=\${out}/lib"
        "--with-lua=${lua}/lib"
        "--with-pjproject-bundled"
        "--with-externals-cache=$(PWD)/externals_cache"
      ];

      preBuild = ''
        cat third-party/pjproject/source/pjlib-util/src/pjlib-util/scanner.c
        make menuselect.makeopts
        ${lib.optionalString (externals ? "addons/mp3") ''
          substituteInPlace menuselect.makeopts --replace 'format_mp3 ' ""
        ''}
        ${lib.optionalString withOpus ''
          substituteInPlace menuselect.makeopts --replace 'codec_opus_open_source ' ""
          substituteInPlace menuselect.makeopts --replace 'format_ogg_opus_open_source ' ""
        ''}
      '';

      postInstall = ''
        # Install sample configuration files for this version of Asterisk
        make samples
        make install-headers
      '';

      meta = {
        description = "Software implementation of a telephone private branch exchange (PBX)";
        homepage = "https://www.asterisk.org/";
        license = lib.licenses.gpl2Only;
        mainProgram = "asterisk";
        maintainers = with lib.maintainers; [
          auntie
          DerTim1
          yorickvp
        ];
      };
    };

  pjproject = fetchurl {
    url = "https://raw.githubusercontent.com/asterisk/third-party/master/pjproject/2.15.1/pjproject-2.15.1.tar.bz2";
    hash = "sha256-WLuDzsTUMfSNAG5FXYIWaEUPjPa2yV8JDe9HBi+jpgw=";
  };

  mp3-204 = fetchsvn {
    url = "http://svn.digium.com/svn/thirdparty/mp3/trunk";
    rev = "204";
    hash = "sha256-Viec0LwFPfR7ewy+2hc/LIUR1LiDZfYos3RUbc+tyNk=";
  };

  asterisk-opus = fetchFromGitHub {
    owner = "traud";
    repo = "asterisk-opus";
    # No releases, points to master as of 2022-04-06
    rev = "a959f072d3f364be983dd27e6e250b038aaef747";
    hash = "sha256-CASlTvTahOg9D5jccF/IN10LP/U8rRy9BFCSaHGQfCw=";
  };

  # auto-generated by update.py
  versions = lib.mapAttrs (
    _:
    { version, sha256 }:
    common {
      inherit version sha256;
      externals = {
        "externals_cache/${pjproject.name}" = pjproject;
        "addons/mp3" = mp3-204;
      };
    }
  ) (lib.importJSON ./versions.json);

  updateScript_python = python3.withPackages (
    p: with p; [
      packaging
      beautifulsoup4
      requests
    ]
  );
  updateScript = writeScript "asterisk-update" ''
    #!/usr/bin/env bash
    exec ${updateScript_python}/bin/python ${toString ./update.py}
  '';

in
{
  # Supported releases (as of 2025-10-19).
  # Source: https://wiki.asterisk.org/wiki/display/AST/Asterisk+Versions
  # Exact version can be found at https://www.asterisk.org/downloads/asterisk/all-asterisk-versions/
  #
  # Series  Type       Rel. Date   Sec. Fixes  EOL
  # 18.x    LTS        2020-10-20  2024-10-20  2025-10-20 (dropped)
  # 19.x    Standard   2021-11-02  2022-11-02  2023-11-02 (dropped)
  # 20.x    LTS        2022-11-02  2026-10-19  2027-10-19
  # 21.x    Standard   2023-10-18  2025-10-18  2026-10-18
  # 22.x    LTS        2024-10-16  2028-10-16  2029-10-16
  # 23.x    Standard   2025-10-15  2026-10-15  2027-10-15
  asterisk-lts = versions.asterisk_22;
  asterisk-stable = versions.asterisk_20;
  asterisk = versions.asterisk_22.overrideAttrs (o: {
    passthru = (o.passthru or { }) // {
      inherit updateScript;
    };
  });

}
// versions