about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/interpreters/php/generic.nix
blob: 68ca8e4d3bbaa6c200b5e96b11cae3b009469fe5 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# We have tests for PCRE and PHP-FPM in nixos/tests/php/ or
# both in the same attribute named nixosTests.php

let
  generic =
    { callPackage
    , lib
    , stdenv
    , nixosTests
    , tests
    , fetchurl
    , makeBinaryWrapper
    , symlinkJoin
    , writeText
    , autoconf
    , automake
    , bison
    , flex
    , libtool
    , pkg-config
    , re2c
    , apacheHttpd
    , libargon2
    , libxml2
    , pcre2
    , systemd
    , system-sendmail
    , valgrind
    , xcbuild
    , writeShellScript
    , common-updater-scripts
    , curl
    , jq

    , version
    , phpSrc ? null
    , hash ? null
    , extraPatches ? [ ]
    , packageOverrides ? (final: prev: { })
    , phpAttrsOverrides ? (attrs: { })
    , pearInstallPhar ? (callPackage ./install-pear-nozlib-phar.nix { })

      # Sapi flags
    , cgiSupport ? true
    , cliSupport ? true
    , fpmSupport ? true
    , pearSupport ? true
    , pharSupport ? true
    , phpdbgSupport ? true

      # Misc flags
    , apxs2Support ? false
    , argon2Support ? true
    , cgotoSupport ? false
    , embedSupport ? false
    , staticSupport ? false
    , ipv6Support ? true
    , zendSignalsSupport ? true
    , zendMaxExecutionTimersSupport ? false
    , systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemd
    , valgrindSupport ? !stdenv.isDarwin && lib.meta.availableOn stdenv.hostPlatform valgrind
    , ztsSupport ? apxs2Support
    }@args:

    let
      # Compose two functions of the type expected by 'overrideAttrs'
      # into one where changes made in the first are available to the second.
      composeOverrides =
        f: g: attrs:
        let
          fApplied = f attrs;
          attrs' = attrs // fApplied;
        in
        fApplied // g attrs';

      # buildEnv wraps php to provide additional extensions and
      # configuration. Its usage is documented in
      # doc/languages-frameworks/php.section.md.
      #
      # Create a buildEnv with earlier overridden values and
      # extensions functions in its closure. This is necessary for
      # consecutive calls to buildEnv and overrides to work as
      # expected.
      mkBuildEnv = prevArgs: prevExtensionFunctions: lib.makeOverridable (
        { extensions ? ({ enabled, ... }: enabled), extraConfig ? "", ... }@innerArgs:
        let
          allArgs = args // prevArgs // innerArgs;
          filteredArgs = builtins.removeAttrs allArgs [ "extensions" "extraConfig" ];
          php = generic filteredArgs;

          php-packages = (callPackage ../../../top-level/php-packages.nix {
            phpPackage = phpWithExtensions;
          }).overrideScope packageOverrides;

          allExtensionFunctions = prevExtensionFunctions ++ [ extensions ];
          enabledExtensions =
            builtins.foldl'
              (enabled: f:
                f { inherit enabled; all = php-packages.extensions; })
              [ ]
              allExtensionFunctions;

          getExtName = ext: ext.extensionName;

          # Recursively get a list of all internal dependencies
          # for a list of extensions.
          getDepsRecursively = extensions:
            let
              deps = lib.concatMap
                (ext: (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]))
                extensions;
            in
            if ! (deps == [ ]) then
              deps ++ (getDepsRecursively deps)
            else
              deps;

          # Generate extension load configuration snippets from the
          # extension parameter. This is an attrset suitable for use
          # with textClosureList, which is used to put the strings in
          # the right order - if a plugin which is dependent on
          # another plugin is placed before its dependency, it will
          # fail to load.
          extensionTexts =
            lib.listToAttrs
              (map
                (ext:
                  let
                    extName = getExtName ext;
                    phpDeps = (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]);
                    type = "${lib.optionalString (ext.zendExtension or false) "zend_"}extension";
                  in
                  lib.nameValuePair extName {
                    text = "${type}=${ext}/lib/php/extensions/${extName}.so";
                    deps = map getExtName phpDeps;
                  })
                (enabledExtensions ++ (getDepsRecursively enabledExtensions)));

          extNames = map getExtName enabledExtensions;
          extraInit = writeText "php-extra-init-${version}.ini" ''
            ${lib.concatStringsSep "\n"
              (lib.textClosureList extensionTexts extNames)}
            ${extraConfig}
          '';

          phpWithExtensions = symlinkJoin {
            name = "php-with-extensions-${version}";
            inherit (php) version;
            nativeBuildInputs = [ makeBinaryWrapper ];
            passthru = php.passthru // {
              buildEnv = mkBuildEnv allArgs allExtensionFunctions;
              withExtensions = mkWithExtensions allArgs allExtensionFunctions;
              overrideAttrs =
                f:
                let
                  newPhpAttrsOverrides = composeOverrides (filteredArgs.phpAttrsOverrides or (attrs: { })) f;
                  php = generic (filteredArgs // { phpAttrsOverrides = newPhpAttrsOverrides; });
                in
                php.buildEnv { inherit extensions extraConfig; };
              phpIni = "${phpWithExtensions}/lib/php.ini";
              unwrapped = php;
              # Select the right php tests for the php version
              tests = {
                nixos = lib.recurseIntoAttrs nixosTests."php${lib.strings.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor php.version)}";
                package = tests.php;
              };
              inherit (php-packages) extensions buildPecl mkComposerRepository buildComposerProject composerHooks mkExtension;
              packages = php-packages.tools;
              meta = php.meta // {
                outputsToInstall = [ "out" ];
              };
            };
            paths = [ php ];
            postBuild = ''
              ln -s ${extraInit} $out/lib/php.ini

              if test -e $out/bin/php; then
                wrapProgram $out/bin/php --set-default PHP_INI_SCAN_DIR $out/lib
              fi

              if test -e $out/bin/php-fpm; then
                wrapProgram $out/bin/php-fpm --set-default PHP_INI_SCAN_DIR $out/lib
              fi

              if test -e $out/bin/phpdbg; then
                wrapProgram $out/bin/phpdbg --set-default PHP_INI_SCAN_DIR $out/lib
              fi

              if test -e $out/bin/php-cgi; then
                wrapProgram $out/bin/php-cgi --set-default PHP_INI_SCAN_DIR $out/lib
              fi
            '';
          };
        in
        phpWithExtensions
      );

      mkWithExtensions = prevArgs: prevExtensionFunctions: extensions:
        mkBuildEnv prevArgs prevExtensionFunctions { inherit extensions; };

      defaultPhpSrc = fetchurl {
        url = "https://www.php.net/distributions/php-${version}.tar.bz2";
        inherit hash;
      };
    in
    stdenv.mkDerivation (
      let
        attrs = {
          pname = "php";

          inherit version;

          enableParallelBuilding = true;

          nativeBuildInputs = [ autoconf automake bison flex libtool pkg-config re2c ]
            ++ lib.optional stdenv.isDarwin xcbuild;

          buildInputs =
            # PCRE extension
            [ pcre2 ]

            # Enable sapis
            ++ lib.optionals pearSupport [ libxml2.dev ]

            # Misc deps
            ++ lib.optional apxs2Support apacheHttpd
            ++ lib.optional argon2Support libargon2
            ++ lib.optional systemdSupport systemd
            ++ lib.optional valgrindSupport valgrind
          ;

          CXXFLAGS = lib.optionalString stdenv.cc.isClang "-std=c++11";
          SKIP_PERF_SENSITIVE = 1;

          configureFlags =
            # Disable all extensions
            [ "--disable-all" ]

            # PCRE
            ++ [ "--with-external-pcre=${pcre2.dev}" ]

            # Enable sapis
            ++ lib.optional (!cgiSupport) "--disable-cgi"
            ++ lib.optional (!cliSupport) "--disable-cli"
            ++ lib.optional fpmSupport "--enable-fpm"
            ++ lib.optionals pearSupport [ "--with-pear" "--enable-xml" "--with-libxml" ]
            ++ lib.optional pharSupport "--enable-phar"
            ++ lib.optional (!phpdbgSupport) "--disable-phpdbg"


            # Misc flags
            ++ lib.optional apxs2Support "--with-apxs2=${apacheHttpd.dev}/bin/apxs"
            ++ lib.optional argon2Support "--with-password-argon2=${libargon2}"
            ++ lib.optional cgotoSupport "--enable-re2c-cgoto"
            ++ lib.optional embedSupport "--enable-embed${lib.optionalString staticSupport "=static"}"
            ++ lib.optional (!ipv6Support) "--disable-ipv6"
            ++ lib.optional systemdSupport "--with-fpm-systemd"
            ++ lib.optional valgrindSupport "--with-valgrind=${valgrind.dev}"
            ++ lib.optional ztsSupport "--enable-zts"
            ++ lib.optional staticSupport "--enable-static"
            ++ lib.optional (!zendSignalsSupport) ["--disable-zend-signals"]
            ++ lib.optional zendMaxExecutionTimersSupport "--enable-zend-max-execution-timers"


            # Sendmail
            ++ [ "PROG_SENDMAIL=${system-sendmail}/bin/sendmail" ]
          ;

          hardeningDisable = [ "bindnow" ];

          preConfigure =
            # Don't record the configure flags since this causes unnecessary
            # runtime dependencies
            ''
              substituteInPlace main/build-defs.h.in \
                --replace-fail '@CONFIGURE_COMMAND@' '(omitted)'
              substituteInPlace scripts/php-config.in \
                --replace-fail '@CONFIGURE_OPTIONS@' "" \
                --replace-fail '@PHP_LDFLAGS@' ""

              export EXTENSION_DIR=$out/lib/php/extensions

              ./buildconf --copy --force

              if [ -f "scripts/dev/genfiles" ]; then
                ./scripts/dev/genfiles
              fi
            '' + lib.optionalString stdenv.isDarwin ''
              substituteInPlace configure --replace-fail "-lstdc++" "-lc++"
            '';

          # When compiling PHP sources from Github, this file is missing and we
          # need to install it ourselves.
          # On the other hand, a distribution includes this file by default.
          preInstall = ''
            if [[ ! -f ./pear/install-pear-nozlib.phar ]]; then
              cp ${pearInstallPhar} ./pear/install-pear-nozlib.phar
            fi
          '';

          postInstall = ''
            test -d $out/etc || mkdir $out/etc
            cp php.ini-production $out/etc/php.ini
          '';

          postFixup = ''
            mkdir -p $dev/bin $dev/share/man/man1
            mv $out/bin/phpize $out/bin/php-config $dev/bin/
            mv $out/share/man/man1/phpize.1.gz \
               $out/share/man/man1/php-config.1.gz \
               $dev/share/man/man1/
          '';

          src = if phpSrc == null then defaultPhpSrc else phpSrc;

          patches = [ ./fix-paths-php7.patch ] ++ extraPatches;

          separateDebugInfo = true;

          outputs = [ "out" "dev" ];

          passthru = {
            updateScript =
              let
                script = writeShellScript "php${lib.versions.major version}${lib.versions.minor version}-update-script" ''
                  set -o errexit
                  PATH=${lib.makeBinPath [ common-updater-scripts curl jq ]}
                  new_version=$(curl --silent "https://www.php.net/releases/active" | jq --raw-output '."${lib.versions.major version}"."${lib.versions.majorMinor version}".version')
                  update-source-version "$UPDATE_NIX_ATTR_PATH.unwrapped" "$new_version" "--file=$1"
                '';
              in [
                script
                # Passed as an argument so that update.nix can ensure it does not become a store path.
                (./. + "/${lib.versions.majorMinor version}.nix")
              ];
            buildEnv = mkBuildEnv { } [ ];
            withExtensions = mkWithExtensions { } [ ];
            overrideAttrs =
              f:
              let
                newPhpAttrsOverrides = composeOverrides phpAttrsOverrides f;
                php = generic (args // { phpAttrsOverrides = newPhpAttrsOverrides; });
              in
              php;
            inherit ztsSupport;
          };

          meta = with lib; {
            description = "An HTML-embedded scripting language";
            homepage = "https://www.php.net/";
            license = licenses.php301;
            mainProgram = "php";
            maintainers = teams.php.members;
            platforms = platforms.all;
            outputsToInstall = [ "out" "dev" ];
          };
        };
      in
      attrs // phpAttrsOverrides attrs
    );
in
generic