Merge pull request #144564 from jtojnar/php-overridable

php: Implement overrideAttrs that composes with buildEnv/withExtensions
This commit is contained in:
Kim Lindberger 2021-11-15 18:12:44 +01:00 committed by GitHub
commit 0039b0b5a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 260 additions and 108 deletions

View file

@ -7,6 +7,7 @@ let
, lib
, stdenv
, nixosTests
, tests
, fetchurl
, makeWrapper
, symlinkJoin
@ -31,6 +32,7 @@ let
, sha256
, extraPatches ? [ ]
, packageOverrides ? (final: prev: { })
, phpAttrsOverrides ? (attrs: { })
# Sapi flags
, cgiSupport ? true
@ -52,6 +54,16 @@ let
}@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.
@ -129,10 +141,20 @@ let
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 = nixosTests."php${lib.strings.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor php.version)}";
tests = {
nixos = lib.recurseIntoAttrs nixosTests."php${lib.strings.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor php.version)}";
package = tests.php;
};
inherit (php-packages) extensions buildPecl mkExtension;
packages = php-packages.tools;
meta = php.meta // {
@ -163,7 +185,9 @@ let
mkWithExtensions = prevArgs: prevExtensionFunctions: extensions:
mkBuildEnv prevArgs prevExtensionFunctions { inherit extensions; };
in
stdenv.mkDerivation {
stdenv.mkDerivation (
let
attrs = {
pname = "php";
inherit version;
@ -285,6 +309,13 @@ let
passthru = {
buildEnv = mkBuildEnv { } [ ];
withExtensions = mkWithExtensions { } [ ];
overrideAttrs =
f:
let
newPhpAttrsOverrides = composeOverrides phpAttrsOverrides f;
php = generic (args // { phpAttrsOverrides = newPhpAttrsOverrides; });
in
php;
inherit ztsSupport;
};
@ -298,4 +329,7 @@ let
};
};
in
attrs // phpAttrsOverrides attrs
);
in
generic

View file

@ -37,6 +37,8 @@ with pkgs;
cross = callPackage ./cross {};
php = recurseIntoAttrs (callPackages ./php {});
rustCustomSysroot = callPackage ./rust-sysroot {};
buildRustCrate = callPackage ../build-support/rust/build-rust-crate/test { };
importCargoLock = callPackage ../build-support/rust/test/import-cargo-lock { };

116
pkgs/test/php/default.nix Normal file
View file

@ -0,0 +1,116 @@
{ lib
, php
, runCommand
}:
let
runTest = name: body: runCommand name { } ''
testFailed=
checking() {
echo -n "Checking $1... " > /dev/stderr
}
ok() {
echo ok > /dev/stderr
}
nok() {
echo fail > /dev/stderr
testFailed=1
}
${body}
if test -n "$testFailed"; then
exit 1
fi
touch $out
'';
check = cond: if cond then "ok" else "nok";
in
{
withExtensions-enables-previously-disabled-extensions = runTest "php-test-withExtensions-enables-previously-disabled-extensions" ''
php="${php}"
checking "that imagick is not present by default"
$php/bin/php -r 'exit(extension_loaded("imagick") ? 1 : 0);' && ok || nok
phpWithImagick="${php.withExtensions ({ all, ... }: [ all.imagick ])}"
checking "that imagick extension is present when enabled"
$phpWithImagick/bin/php -r 'exit(extension_loaded("imagick") ? 0 : 1);' && ok || nok
'';
overrideAttrs-preserves-enabled-extensions =
let
customPhp =
(php.withExtensions ({ all, ... }: [ all.imagick ])).overrideAttrs (attrs: {
postInstall = attrs.postInstall or "" + ''
touch "$out/oApee-was-here"
'';
});
in
runTest "php-test-overrideAttrs-preserves-enabled-extensions" ''
php="${customPhp}"
phpUnwrapped="${customPhp.unwrapped}"
checking "if overrides took hold"
test -f "$phpUnwrapped/oApee-was-here" && ok || nok
checking "if imagick extension is still present"
$php/bin/php -r 'exit(extension_loaded("imagick") ? 0 : 1);' && ok || nok
checking "if imagick extension is linked against the overridden PHP"
echo $php
$php/bin/php -r 'exit(extension_loaded("imagick") ? 0 : 1);' && ok || nok
'';
unwrapped-overrideAttrs-stacks =
let
customPhp =
lib.pipe php.unwrapped [
(pkg: pkg.overrideAttrs (attrs: {
postInstall = attrs.postInstall or "" + ''
touch "$out/oAs-first"
'';
}))
(pkg: pkg.overrideAttrs (attrs: {
postInstall = attrs.postInstall or "" + ''
touch "$out/oAs-second"
'';
}))
];
in
runTest "php-test-unwrapped-overrideAttrs-stacks" ''
checking "if first override remained"
${check (builtins.match ".*oAs-first.*" customPhp.postInstall != null)}
checking "if second override is there"
${check (builtins.match ".*oAs-second.*" customPhp.postInstall != null)}
'';
wrapped-overrideAttrs-stacks =
let
customPhp =
lib.pipe php [
(pkg: pkg.overrideAttrs (attrs: {
postInstall = attrs.postInstall or "" + ''
touch "$out/oAs-first"
'';
}))
(pkg: pkg.overrideAttrs (attrs: {
postInstall = attrs.postInstall or "" + ''
touch "$out/oAs-second"
'';
}))
];
in
runTest "php-test-wrapped-overrideAttrs-stacks" ''
checking "if first override remained"
${check (builtins.match ".*oAs-first.*" customPhp.unwrapped.postInstall != null)}
checking "if second override is there"
${check (builtins.match ".*oAs-second.*" customPhp.unwrapped.postInstall != null)}
'';
}