php.buildEnv: Provide a list of currently enabled extensions

Rework withExtensions / buildEnv to handle currently enabled
extensions better and make them compatible with override. They now
accept a function with the named arguments enabled and all, where
enabled is a list of currently enabled extensions and all is the set
of all extensions. This gives us several nice properties:

 - You always get the right version of the list of currently enabled
   extensions

 - Invocations chain

 - It works well with overridden PHP packages - you always get the
   correct versions of extensions

As a contrived example of what's possible, you can add ImageMagick,
then override the version and disable fpm, then disable cgi, and
lastly remove the zip extension like this:

{ pkgs ? (import <nixpkgs>) {} }:
with pkgs;

let
  phpWithImagick = php74.withExtensions ({ all, enabled }: enabled ++ [ all.imagick ]);

  phpWithImagickWithoutFpm743 = phpWithImagick.override {
    version = "7.4.3";
    sha256 = "wVF7pJV4+y3MZMc6Ptx21PxQfEp6xjmYFYTMfTtMbRQ=";
    fpmSupport = false;
  };

  phpWithImagickWithoutFpmZip743 = phpWithImagickWithoutFpm743.withExtensions (
    { enabled, all }:
      lib.filter (e: e != all.zip) enabled);

  phpWithImagickWithoutFpmZipCgi743 = phpWithImagickWithoutFpmZip743.override {
    cgiSupport = false;
  };
in
  phpWithImagickWithoutFpmZipCgi743
This commit is contained in:
talyz
2020-04-12 23:31:56 +02:00
parent abedfadd73
commit 2ba7926959
5 changed files with 68 additions and 47 deletions

View File

@@ -43,8 +43,16 @@ let
phpWithExtensions = self.withExtensions defaultPhpExtensions;
});
mkBuildEnv = prevArgs: lib.makeOverridable (
{ extensions ? (_: []), extraConfig ? "", ... }@innerArgs:
# 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 ? ({...}: []), extraConfig ? "", ... }@innerArgs:
let
allArgs = args // prevArgs // innerArgs;
filteredArgs = builtins.removeAttrs allArgs [ "extensions" "extraConfig" ];
@@ -54,8 +62,15 @@ let
inherit php phpWithExtensions;
});
allExtensionFunctions = prevExtensionFunctions ++ [ extensions ];
enabledExtensions =
builtins.foldl'
(state: f:
f { enabled = state; all = php-packages.extensions; })
[]
allExtensionFunctions;
getExtName = ext: lib.removePrefix "php-" (builtins.parseDrvName ext.name).name;
enabledExtensions = extensions php-packages.extensions;
# Generate extension load configuration snippets from the
# extension parameter. This is an attrset suitable for use
@@ -89,9 +104,8 @@ let
inherit (php) version;
nativeBuildInputs = [ makeWrapper ];
passthru = {
buildEnv = mkBuildEnv allArgs;
withExtensions = mkWithExtensions allArgs;
inherit enabledExtensions;
buildEnv = mkBuildEnv allArgs allExtensionFunctions;
withExtensions = mkWithExtensions allArgs allExtensionFunctions;
inherit (php-packages) packages extensions;
};
paths = [ php ];
@@ -108,8 +122,8 @@ let
in
phpWithExtensions);
mkWithExtensions = prevArgs: extensions:
mkBuildEnv prevArgs { inherit extensions; };
mkWithExtensions = prevArgs: prevExtensionFunctions: extensions:
mkBuildEnv prevArgs prevExtensionFunctions { inherit extensions; };
pcre' = if (lib.versionAtLeast version "7.3") then pcre2 else pcre;
in
@@ -218,9 +232,8 @@ let
outputs = [ "out" "dev" ];
passthru = {
enabledExtensions = [];
buildEnv = mkBuildEnv {};
withExtensions = mkWithExtensions {};
buildEnv = mkBuildEnv {} [];
withExtensions = mkWithExtensions {} [];
inherit (php-packages) packages extensions;
};
@@ -258,7 +271,7 @@ let
inherit defaultPhpExtensions;
});
defaultPhpExtensions = extensions: with extensions; ([
defaultPhpExtensions = { all, ... }: with all; ([
bcmath calendar curl ctype dom exif fileinfo filter ftp gd
gettext gmp iconv intl json ldap mbstring mysqli mysqlnd opcache
openssl pcntl pdo pdo_mysql pdo_odbc pdo_pgsql pdo_sqlite pgsql
@@ -266,8 +279,8 @@ let
tokenizer xmlreader xmlwriter zip zlib
] ++ lib.optionals (!stdenv.isDarwin) [ imap ]);
defaultPhpExtensionsWithHash = extensions:
(defaultPhpExtensions extensions) ++ [ extensions.hash ];
defaultPhpExtensionsWithHash = { all, ... }:
(defaultPhpExtensions { inherit all; }) ++ [ all.hash ];
php74 = php74base.withExtensions defaultPhpExtensions;
php73 = php73base.withExtensions defaultPhpExtensionsWithHash;