1 # We have tests for PCRE and PHP-FPM in nixos/tests/php/ or
2 # both in the same attribute named nixosTests.php
31 , common-updater-scripts
39 , packageOverrides ? (final: prev: { })
40 , phpAttrsOverrides ? (attrs: { })
41 , pearInstallPhar ? (callPackage ./install-pear-nozlib-phar.nix { })
49 , phpdbgSupport ? true
52 , apxs2Support ? false
53 , argon2Support ? true
54 , cgotoSupport ? false
55 , embedSupport ? false
56 , staticSupport ? false
58 , zendSignalsSupport ? true
59 , zendMaxExecutionTimersSupport ? false
60 , systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemd
61 , valgrindSupport ? !stdenv.hostPlatform.isDarwin && lib.meta.availableOn stdenv.hostPlatform valgrind
62 , ztsSupport ? apxs2Support
66 # Compose two functions of the type expected by 'overrideAttrs'
67 # into one where changes made in the first are available to the second.
72 attrs' = attrs // fApplied;
76 # buildEnv wraps php to provide additional extensions and
77 # configuration. Its usage is documented in
78 # doc/languages-frameworks/php.section.md.
80 # Create a buildEnv with earlier overridden values and
81 # extensions functions in its closure. This is necessary for
82 # consecutive calls to buildEnv and overrides to work as
84 mkBuildEnv = prevArgs: prevExtensionFunctions: lib.makeOverridable (
85 { extensions ? ({ enabled, ... }: enabled), extraConfig ? "", ... }@innerArgs:
87 allArgs = args // prevArgs // innerArgs;
88 filteredArgs = builtins.removeAttrs allArgs [ "extensions" "extraConfig" ];
89 php = generic filteredArgs;
91 php-packages = (callPackage ../../../top-level/php-packages.nix {
92 phpPackage = phpWithExtensions;
93 }).overrideScope packageOverrides;
95 allExtensionFunctions = prevExtensionFunctions ++ [ extensions ];
99 f { inherit enabled; all = php-packages.extensions; })
101 allExtensionFunctions;
103 getExtName = ext: ext.extensionName;
105 # Recursively get a list of all internal dependencies
106 # for a list of extensions.
107 getDepsRecursively = extensions:
110 (ext: (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]))
113 if ! (deps == [ ]) then
114 deps ++ (getDepsRecursively deps)
118 # Generate extension load configuration snippets from the
119 # extension parameter. This is an attrset suitable for use
120 # with textClosureList, which is used to put the strings in
121 # the right order - if a plugin which is dependent on
122 # another plugin is placed before its dependency, it will
129 extName = getExtName ext;
130 phpDeps = (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]);
131 type = "${lib.optionalString (ext.zendExtension or false) "zend_"}extension";
133 lib.nameValuePair extName {
134 text = "${type}=${ext}/lib/php/extensions/${extName}.so";
135 deps = map getExtName phpDeps;
137 (enabledExtensions ++ (getDepsRecursively enabledExtensions)));
139 extNames = map getExtName enabledExtensions;
140 extraInit = writeText "php-extra-init-${version}.ini" ''
141 ${lib.concatStringsSep "\n"
142 (lib.textClosureList extensionTexts extNames)}
146 phpWithExtensions = symlinkJoin {
147 name = "php-with-extensions-${version}";
148 inherit (php) version;
149 nativeBuildInputs = [ makeBinaryWrapper ];
150 passthru = php.passthru // {
151 buildEnv = mkBuildEnv allArgs allExtensionFunctions;
152 withExtensions = mkWithExtensions allArgs allExtensionFunctions;
156 newPhpAttrsOverrides = composeOverrides (filteredArgs.phpAttrsOverrides or (attrs: { })) f;
157 php = generic (filteredArgs // { phpAttrsOverrides = newPhpAttrsOverrides; });
159 php.buildEnv { inherit extensions extraConfig; };
160 phpIni = "${phpWithExtensions}/lib/php.ini";
162 # Select the right php tests for the php version
164 nixos = lib.recurseIntoAttrs nixosTests."php${lib.strings.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor php.version)}";
167 inherit (php-packages) extensions buildPecl mkComposerRepository mkComposerVendor buildComposerProject buildComposerProject2 buildComposerWithPlugin composerHooks composerHooks2 mkExtension;
168 packages = php-packages.tools;
170 outputsToInstall = [ "out" ];
175 ln -s ${extraInit} $out/lib/php.ini
177 if test -e $out/bin/php; then
178 wrapProgram $out/bin/php --set-default PHP_INI_SCAN_DIR $out/lib
181 if test -e $out/bin/php-fpm; then
182 wrapProgram $out/bin/php-fpm --set-default PHP_INI_SCAN_DIR $out/lib
185 if test -e $out/bin/phpdbg; then
186 wrapProgram $out/bin/phpdbg --set-default PHP_INI_SCAN_DIR $out/lib
189 if test -e $out/bin/php-cgi; then
190 wrapProgram $out/bin/php-cgi --set-default PHP_INI_SCAN_DIR $out/lib
198 mkWithExtensions = prevArgs: prevExtensionFunctions: extensions:
199 mkBuildEnv prevArgs prevExtensionFunctions { inherit extensions; };
201 defaultPhpSrc = fetchurl {
202 url = "https://www.php.net/distributions/php-${version}.tar.bz2";
206 stdenv.mkDerivation (
213 enableParallelBuilding = true;
215 nativeBuildInputs = [ autoconf automake bison flex libtool pkg-config re2c ]
216 ++ lib.optional stdenv.hostPlatform.isDarwin xcbuild;
223 ++ lib.optionals pearSupport [ libxml2.dev ]
226 ++ lib.optional apxs2Support apacheHttpd
227 ++ lib.optional argon2Support libargon2
228 ++ lib.optional systemdSupport systemd
229 ++ lib.optional valgrindSupport valgrind
232 CXXFLAGS = lib.optionalString stdenv.cc.isClang "-std=c++11";
233 SKIP_PERF_SENSITIVE = 1;
236 # Disable all extensions
240 ++ [ "--with-external-pcre=${pcre2.dev}" ]
243 ++ lib.optional (!cgiSupport) "--disable-cgi"
244 ++ lib.optional (!cliSupport) "--disable-cli"
245 ++ lib.optional fpmSupport "--enable-fpm"
246 ++ lib.optionals pearSupport [ "--with-pear" "--enable-xml" "--with-libxml" ]
247 ++ lib.optional pharSupport "--enable-phar"
248 ++ lib.optional (!phpdbgSupport) "--disable-phpdbg"
252 ++ lib.optional apxs2Support "--with-apxs2=${apacheHttpd.dev}/bin/apxs"
253 ++ lib.optional argon2Support "--with-password-argon2=${libargon2}"
254 ++ lib.optional cgotoSupport "--enable-re2c-cgoto"
255 ++ lib.optional embedSupport "--enable-embed${lib.optionalString staticSupport "=static"}"
256 ++ lib.optional (!ipv6Support) "--disable-ipv6"
257 ++ lib.optional systemdSupport "--with-fpm-systemd"
258 ++ lib.optional valgrindSupport "--with-valgrind=${valgrind.dev}"
259 ++ lib.optional ztsSupport "--enable-zts"
260 ++ lib.optional staticSupport "--enable-static"
261 ++ lib.optional (!zendSignalsSupport) ["--disable-zend-signals"]
262 ++ lib.optional zendMaxExecutionTimersSupport "--enable-zend-max-execution-timers"
266 ++ [ "PROG_SENDMAIL=${system-sendmail}/bin/sendmail" ]
269 hardeningDisable = [ "bindnow" ];
272 # Don't record the configure flags since this causes unnecessary
273 # runtime dependencies
275 substituteInPlace main/build-defs.h.in \
276 --replace-fail '@CONFIGURE_COMMAND@' '(omitted)'
277 substituteInPlace scripts/php-config.in \
278 --replace-fail '@CONFIGURE_OPTIONS@' "" \
279 --replace-fail '@PHP_LDFLAGS@' ""
281 export EXTENSION_DIR=$out/lib/php/extensions
283 ./buildconf --copy --force
285 if [ -f "scripts/dev/genfiles" ]; then
286 ./scripts/dev/genfiles
288 '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
289 substituteInPlace configure --replace-fail "-lstdc++" "-lc++"
292 # When compiling PHP sources from Github, this file is missing and we
293 # need to install it ourselves.
294 # On the other hand, a distribution includes this file by default.
296 if [[ ! -f ./pear/install-pear-nozlib.phar ]]; then
297 cp ${pearInstallPhar} ./pear/install-pear-nozlib.phar
302 test -d $out/etc || mkdir $out/etc
303 cp php.ini-production $out/etc/php.ini
307 mkdir -p $dev/bin $dev/share/man/man1
308 mv $out/bin/phpize $out/bin/php-config $dev/bin/
309 mv $out/share/man/man1/phpize.1.gz \
310 $out/share/man/man1/php-config.1.gz \
314 src = if phpSrc == null then defaultPhpSrc else phpSrc;
316 patches = lib.optionals (lib.versionOlder version "8.4") [
317 ./fix-paths-php7.patch
318 ] ++ lib.optionals (lib.versionAtLeast version "8.4") [
319 ./fix-paths-php84.patch
322 separateDebugInfo = true;
324 outputs = [ "out" "dev" ];
329 script = writeShellScript "php${lib.versions.major version}${lib.versions.minor version}-update-script" ''
331 PATH=${lib.makeBinPath [ common-updater-scripts curl jq ]}
332 new_version=$(curl --silent "https://www.php.net/releases/active" | jq --raw-output '."${lib.versions.major version}"."${lib.versions.majorMinor version}".version')
333 update-source-version "$UPDATE_NIX_ATTR_PATH.unwrapped" "$new_version" "--file=$1"
337 # Passed as an argument so that update.nix can ensure it does not become a store path.
338 (./. + "/${lib.versions.majorMinor version}.nix")
340 buildEnv = mkBuildEnv { } [ ];
341 withExtensions = mkWithExtensions { } [ ];
345 newPhpAttrsOverrides = composeOverrides phpAttrsOverrides f;
346 php = generic (args // { phpAttrsOverrides = newPhpAttrsOverrides; });
353 description = "HTML-embedded scripting language";
354 homepage = "https://www.php.net/";
355 license = licenses.php301;
357 maintainers = teams.php.members;
358 platforms = platforms.all;
359 outputsToInstall = [ "out" "dev" ];
363 attrs // phpAttrsOverrides attrs