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
57 , systemdSupport ? lib.meta.availableOn stdenv.hostPlatform systemd
58 , valgrindSupport ? !stdenv.isDarwin && lib.meta.availableOn stdenv.hostPlatform valgrind
59 , ztsSupport ? apxs2Support
63 # Compose two functions of the type expected by 'overrideAttrs'
64 # into one where changes made in the first are available to the second.
69 attrs' = attrs // fApplied;
73 # buildEnv wraps php to provide additional extensions and
74 # configuration. Its usage is documented in
75 # doc/languages-frameworks/php.section.md.
77 # Create a buildEnv with earlier overridden values and
78 # extensions functions in its closure. This is necessary for
79 # consecutive calls to buildEnv and overrides to work as
81 mkBuildEnv = prevArgs: prevExtensionFunctions: lib.makeOverridable (
82 { extensions ? ({ enabled, ... }: enabled), extraConfig ? "", ... }@innerArgs:
84 allArgs = args // prevArgs // innerArgs;
85 filteredArgs = builtins.removeAttrs allArgs [ "extensions" "extraConfig" ];
86 php = generic filteredArgs;
88 php-packages = (callPackage ../../../top-level/php-packages.nix {
89 phpPackage = phpWithExtensions;
90 }).overrideScope packageOverrides;
92 allExtensionFunctions = prevExtensionFunctions ++ [ extensions ];
96 f { inherit enabled; all = php-packages.extensions; })
98 allExtensionFunctions;
100 getExtName = ext: ext.extensionName;
102 # Recursively get a list of all internal dependencies
103 # for a list of extensions.
104 getDepsRecursively = extensions:
107 (ext: (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]))
110 if ! (deps == [ ]) then
111 deps ++ (getDepsRecursively deps)
115 # Generate extension load configuration snippets from the
116 # extension parameter. This is an attrset suitable for use
117 # with textClosureList, which is used to put the strings in
118 # the right order - if a plugin which is dependent on
119 # another plugin is placed before its dependency, it will
126 extName = getExtName ext;
127 phpDeps = (ext.internalDeps or [ ]) ++ (ext.peclDeps or [ ]);
128 type = "${lib.optionalString (ext.zendExtension or false) "zend_"}extension";
130 lib.nameValuePair extName {
131 text = "${type}=${ext}/lib/php/extensions/${extName}.so";
132 deps = map getExtName phpDeps;
134 (enabledExtensions ++ (getDepsRecursively enabledExtensions)));
136 extNames = map getExtName enabledExtensions;
137 extraInit = writeText "php-extra-init-${version}.ini" ''
138 ${lib.concatStringsSep "\n"
139 (lib.textClosureList extensionTexts extNames)}
143 phpWithExtensions = symlinkJoin {
144 name = "php-with-extensions-${version}";
145 inherit (php) version;
146 nativeBuildInputs = [ makeBinaryWrapper ];
147 passthru = php.passthru // {
148 buildEnv = mkBuildEnv allArgs allExtensionFunctions;
149 withExtensions = mkWithExtensions allArgs allExtensionFunctions;
153 newPhpAttrsOverrides = composeOverrides (filteredArgs.phpAttrsOverrides or (attrs: { })) f;
154 php = generic (filteredArgs // { phpAttrsOverrides = newPhpAttrsOverrides; });
156 php.buildEnv { inherit extensions extraConfig; };
157 phpIni = "${phpWithExtensions}/lib/php.ini";
159 # Select the right php tests for the php version
161 nixos = lib.recurseIntoAttrs nixosTests."php${lib.strings.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor php.version)}";
164 inherit (php-packages) extensions buildPecl mkComposerRepository buildComposerProject composerHooks mkExtension;
165 packages = php-packages.tools;
167 outputsToInstall = [ "out" ];
172 ln -s ${extraInit} $out/lib/php.ini
174 if test -e $out/bin/php; then
175 wrapProgram $out/bin/php --set-default PHP_INI_SCAN_DIR $out/lib
178 if test -e $out/bin/php-fpm; then
179 wrapProgram $out/bin/php-fpm --set-default PHP_INI_SCAN_DIR $out/lib
182 if test -e $out/bin/phpdbg; then
183 wrapProgram $out/bin/phpdbg --set-default PHP_INI_SCAN_DIR $out/lib
186 if test -e $out/bin/php-cgi; then
187 wrapProgram $out/bin/php-cgi --set-default PHP_INI_SCAN_DIR $out/lib
195 mkWithExtensions = prevArgs: prevExtensionFunctions: extensions:
196 mkBuildEnv prevArgs prevExtensionFunctions { inherit extensions; };
198 defaultPhpSrc = fetchurl {
199 url = "https://www.php.net/distributions/php-${version}.tar.bz2";
203 stdenv.mkDerivation (
210 enableParallelBuilding = true;
212 nativeBuildInputs = [ autoconf automake bison flex libtool pkg-config re2c ]
213 ++ lib.optional stdenv.isDarwin xcbuild;
220 ++ lib.optionals pearSupport [ libxml2.dev ]
223 ++ lib.optional apxs2Support apacheHttpd
224 ++ lib.optional argon2Support libargon2
225 ++ lib.optional systemdSupport systemd
226 ++ lib.optional valgrindSupport valgrind
229 CXXFLAGS = lib.optionalString stdenv.cc.isClang "-std=c++11";
230 SKIP_PERF_SENSITIVE = 1;
233 # Disable all extensions
237 ++ [ "--with-external-pcre=${pcre2.dev}" ]
241 ++ lib.optional (!cgiSupport) "--disable-cgi"
242 ++ lib.optional (!cliSupport) "--disable-cli"
243 ++ lib.optional fpmSupport "--enable-fpm"
244 ++ lib.optionals pearSupport [ "--with-pear" "--enable-xml" "--with-libxml" ]
245 ++ lib.optional pharSupport "--enable-phar"
246 ++ lib.optional (!phpdbgSupport) "--disable-phpdbg"
250 ++ lib.optional apxs2Support "--with-apxs2=${apacheHttpd.dev}/bin/apxs"
251 ++ lib.optional argon2Support "--with-password-argon2=${libargon2}"
252 ++ lib.optional cgotoSupport "--enable-re2c-cgoto"
253 ++ lib.optional embedSupport "--enable-embed"
254 ++ lib.optional (!ipv6Support) "--disable-ipv6"
255 ++ lib.optional systemdSupport "--with-fpm-systemd"
256 ++ lib.optional valgrindSupport "--with-valgrind=${valgrind.dev}"
257 ++ lib.optional ztsSupport "--enable-zts"
261 ++ [ "PROG_SENDMAIL=${system-sendmail}/bin/sendmail" ]
264 hardeningDisable = [ "bindnow" ];
267 # Don't record the configure flags since this causes unnecessary
268 # runtime dependencies
270 for i in main/build-defs.h.in scripts/php-config.in; do
271 substituteInPlace $i \
272 --replace '@CONFIGURE_COMMAND@' '(omitted)' \
273 --replace '@CONFIGURE_OPTIONS@' "" \
274 --replace '@PHP_LDFLAGS@' ""
277 export EXTENSION_DIR=$out/lib/php/extensions
279 ./buildconf --copy --force
281 if [ -f "scripts/dev/genfiles" ]; then
282 ./scripts/dev/genfiles
284 '' + lib.optionalString stdenv.isDarwin ''
285 substituteInPlace configure --replace "-lstdc++" "-lc++"
288 # When compiling PHP sources from Github, this file is missing and we
289 # need to install it ourselves.
290 # On the other hand, a distribution includes this file by default.
292 if [[ ! -f ./pear/install-pear-nozlib.phar ]]; then
293 cp ${pearInstallPhar} ./pear/install-pear-nozlib.phar
298 test -d $out/etc || mkdir $out/etc
299 cp php.ini-production $out/etc/php.ini
303 mkdir -p $dev/bin $dev/share/man/man1
304 mv $out/bin/phpize $out/bin/php-config $dev/bin/
305 mv $out/share/man/man1/phpize.1.gz \
306 $out/share/man/man1/php-config.1.gz \
310 src = if phpSrc == null then defaultPhpSrc else phpSrc;
312 patches = [ ./fix-paths-php7.patch ] ++ extraPatches;
314 separateDebugInfo = true;
316 outputs = [ "out" "dev" ];
321 script = writeShellScript "php${lib.versions.major version}${lib.versions.minor version}-update-script" ''
323 PATH=${lib.makeBinPath [ common-updater-scripts curl jq ]}
324 new_version=$(curl --silent "https://www.php.net/releases/active" | jq --raw-output '."${lib.versions.major version}"."${lib.versions.majorMinor version}".version')
325 update-source-version "$UPDATE_NIX_ATTR_PATH.unwrapped" "$new_version" "--file=$1"
329 # Passed as an argument so that update.nix can ensure it does not become a store path.
330 (./. + "/${lib.versions.majorMinor version}.nix")
332 buildEnv = mkBuildEnv { } [ ];
333 withExtensions = mkWithExtensions { } [ ];
337 newPhpAttrsOverrides = composeOverrides phpAttrsOverrides f;
338 php = generic (args // { phpAttrsOverrides = newPhpAttrsOverrides; });
345 description = "An HTML-embedded scripting language";
346 homepage = "https://www.php.net/";
347 license = licenses.php301;
349 maintainers = teams.php.members;
350 platforms = platforms.all;
351 outputsToInstall = [ "out" "dev" ];
355 attrs // phpAttrsOverrides attrs