Skip to content

Instantly share code, notes, and snippets.

@drupol
Last active September 8, 2023 21:08
Show Gist options
  • Save drupol/85a5e59101b804938065e790a3018c94 to your computer and use it in GitHub Desktop.
Save drupol/85a5e59101b804938065e790a3018c94 to your computer and use it in GitHub Desktop.
Build PHP >= 8 from https://github.com/php/php-src with Nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
pear = {
url = "https://pear.php.net/install-pear-nozlib.phar";
flake = false;
};
};
outputs = inputs@{ self, flake-parts, systems, ... }: flake-parts.lib.mkFlake { inherit inputs; } {
systems = import systems;
perSystem = { config, self', inputs', pkgs, system, lib, ... }: {
apps = lib.mapAttrs
(name: value: {
type = "app";
program = lib.getExe value;
})
self'.packages;
checks = self'.packages;
packages =
let
patchName = patch: patch.name or (builtins.baseNameOf patch);
generic = "${inputs.nixpkgs}/pkgs/development/interpreters/php/generic.nix";
base-snapshot = pkgs.callPackage generic {
version =
let
configureFile = ./configure.ac;
extractVersionFromConfigureAc = configureText:
let
match = builtins.match ".*AC_INIT\\(\\[PHP],\\[([^]-]+)(-dev)?].*" configureText;
in
if match != null then builtins.head match else null;
version =
let
configureText = builtins.readFile configureFile;
version = extractVersionFromConfigureAc configureText;
in
if version != null then version else "0.0.0+unknown";
in
"${version}.snapshot.${self.lastModifiedDate}";
hash = null;
phpAttrsOverrides = attrs: {
src = ./.;
preInstall = attrs.preInstall or "" + ''
cp -f ${inputs.pear} ./pear/install-pear-nozlib.phar
'';
};
packageOverrides = final: prev: {
extensions = prev.extensions // {
dom = prev.extensions.dom.overrideAttrs (attrs: {
postPatch =
lib.concatStringsSep "\n" [
(attrs.postPatch or "")
(lib.optionalString (lib.versionOlder prev.php.version "8.1") ''
# Removing tests failing with libxml2 (2.11.4) > 2.10.4
rm ext/dom/tests/DOMDocument_loadXML_error2.phpt
rm ext/dom/tests/DOMDocument_load_error2.phpt
'')
];
});
openssl = prev.extensions.openssl.overrideAttrs (attrs: {
buildInputs =
let
replaceOpenssl = pkg:
if pkg.pname == "openssl" && lib.versionOlder prev.php.version "8.1" then
pkgs.openssl_1_1.overrideAttrs
(old: {
meta = builtins.removeAttrs old.meta [ "knownVulnerabilities" ];
})
else
pkg;
in
builtins.map replaceOpenssl attrs.buildInputs;
});
pdo = prev.extensions.pdo.overrideAttrs (attrs: {
# Remove this attribute when https://github.com/NixOS/nixpkgs/pull/254001 lands
buildInputs = attrs.buildInputs or [ ] ++ [
pkgs.bison
];
# Remove this attribute when https://github.com/NixOS/nixpkgs/pull/254001 lands
preConfigure = attrs.preConfigure or "" + ''
../../scripts/dev/genfiles
'';
});
tokenizer = prev.extensions.tokenizer.overrideAttrs (attrs: {
patches = builtins.filter
(patch:
# The patch do not apply to PHP < 8.1
patchName patch == "fix-tokenizer-php81.patch" -> lib.versionAtLeast prev.php.version "8.1"
)
(attrs.patches or [ ]);
# Remove this attribute when https://github.com/NixOS/nixpkgs/pull/254001 lands
buildInputs = attrs.buildInputs or [ ] ++ [
pkgs.bison
];
# Remove this attribute when https://github.com/NixOS/nixpkgs/pull/254001 lands
preConfigure = attrs.preConfigure or "" + ''
../../scripts/dev/genfiles
'';
});
};
};
};
in
{
snapshot = base-snapshot.withExtensions ({ all, ... }: with all; [
bcmath
calendar
curl
ctype
dom
exif
fileinfo
filter
ftp
gd
gettext
gmp
iconv
intl
ldap
mbstring
mysqli
mysqlnd
opcache
openssl
pcntl
pdo
pdo_mysql
pdo_odbc
pdo_pgsql
pdo_sqlite
pgsql
posix
readline
session
simplexml
sockets
soap
sodium
sqlite3
tokenizer
xmlreader
xmlwriter
zip
zlib
] ++ lib.optionals (!pkgs.stdenv.isDarwin) [
imap
]);
};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment