Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php: improvements & fixes #296

Merged
merged 9 commits into from
Sep 16, 2022
Merged
50 changes: 39 additions & 11 deletions src/subsystems/php/builders/simple/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
stdenv,
# dream2nix inputs
externals,
callPackageDream,
...
}: {
### FUNCTIONS
Expand Down Expand Up @@ -36,6 +37,29 @@
} @ args: let
l = lib // builtins;

inherit (callPackageDream ../../semver.nix {}) satisfies;

# php with required extensions
php =
if satisfies pkgs.php81.version subsystemAttrs.phpSemver
then
pkgs.php81.withExtensions (
{
all,
enabled,
}:
l.unique (enabled
++ (l.attrValues (l.filterAttrs (e: _: l.elem e subsystemAttrs.phpExtensions) all)))
)
else
l.abort ''
Error: incompatible php versions.
Package "${defaultPackageName}" defines required php version:
"php": "${subsystemAttrs.phpSemver}"
Using php version "${pkgs.php81.version}" from attribute "pkgs.php81".
'';
composer = php.packages.composer;

# packages to export
packages =
{default = packages.${defaultPackageName};}
Expand Down Expand Up @@ -91,11 +115,11 @@

nativeBuildInputs = with pkgs; [
jq
php81Packages.composer
composer
];
buildInputs = with pkgs; [
php81
php81Packages.composer
php
composer
];

dontConfigure = true;
Expand Down Expand Up @@ -130,23 +154,27 @@
popd
'';
installPhase = ''
if [ -d $PKG_OUT/bin ]
then
pushd $PKG_OUT

BINS=$(jq -rcM "(.bin // [])[]" composer.json)
for bin in $BINS
do
mkdir -p $out/bin
for bin in $(ls $PKG_OUT/bin)
do
ln -s $PKG_OUT/bin/$bin $out/bin/$bin
done
fi
pushd $out/bin
ln -s $PKG_OUT/$bin
popd
done

popd
'';

passthru.devShell = import ./devShell.nix {
inherit
name
pkg
php
;
inherit (pkgs) mkShell;
php = pkgs.php81;
};
};
in
Expand Down
2 changes: 1 addition & 1 deletion src/subsystems/php/builders/simple/devShell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mkShell {
];
shellHook = let
vendorDir =
pkg.overrideAttrs (old: {
pkg.overrideAttrs (_: {
dontInstall = true;
})
+ "/lib/vendor/${name}/vendor";
Expand Down
79 changes: 73 additions & 6 deletions src/subsystems/php/semver.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{lib}: let
{lib, ...}: let
l = lib // builtins;

# Replace a list entry at defined index with set value
Expand All @@ -20,8 +20,7 @@
mkCaretComparison = version: v: let
ver = builtins.splitVersion v;
major = l.toInt (l.head ver);
minor = builtins.toString (l.toInt (l.head ver) + 1);
upper = builtins.concatStringsSep "." (ireplace 0 minor ver);
upper = builtins.toString (l.toInt (l.head ver) + 1);
in
if major == 0
then mkTildeComparison version v
Expand Down Expand Up @@ -111,12 +110,80 @@
else throw ''Constraint "${constraintStr}" could not be parsed''
);

satisfies = version: constraint: let
satisfiesSingleInternal = version: constraint: let
inherit (parseConstraint constraint) ops v;
in
if ops.t == "-"
then (operators."${ops.l}" version v.vl && operators."${ops.u}" version v.vu)
else operators."${ops.t}" version v;
in {
inherit satisfies;

# remove v from version strings: ^v1.2.3 -> ^1.2.3
# remove branch suffix: ^1.2.x-dev -> ^1.2
satisfiesSingle = version: constraint: let
removeSuffix = c: let
m = l.match "^(.*)[-][[:alpha:]]+$" c;
in
if m != null && l.length m >= 0
then l.head m
else c;
wildcard = c: let
m = l.match "^([[:d:]]+.*)[.][*x]$" c;
in
if m != null && l.length m >= 0
then "^${l.head m}"
else c;
removeV = c: let
m = l.match "^(.)*v([[:d:]]+[.].*)$" c;
in
if m != null && l.length m > 0
then l.concatStrings m
else c;
isVersionLike = c: let
m = l.match "^([0-9><=!-^~*]*)$" c;
in
m != null && l.length m > 0;
cleanConstraint = removeV (wildcard (removeSuffix (l.removePrefix "dev-" constraint)));
cleanVersion = l.removePrefix "v" (wildcard (removeSuffix version));
in
(l.elem constraint ["" "*" "@dev" "@master" "@dev-master"])
|| (version == constraint)
|| ((isVersionLike cleanConstraint) && (satisfiesSingleInternal cleanVersion cleanConstraint));

trim = s: l.head (l.match "^[[:space:]]*(.*[^[:space:]])[[:space:]]*$" s);
splitAlternatives = v: let
# handle version alternatives: ^1.2 || ^2.0
clean = l.replaceStrings ["||"] ["|"] v;
in
map trim (l.splitString "|" clean);
splitConjunctives = v: let
clean =
l.replaceStrings
["," " - " " -" "- " " as "]
[" " "-" "-" "-" "##"]
v;
cleanInlineAlias = v: let
m = l.match "^(.*)[#][#](.*)$" v;
in
if m != null && l.length m > 0
then l.head m
else v;
in
map (x: trim (cleanInlineAlias x)) (l.splitString " " clean);
in rec {
# matching a version with semver
# 1.0.2 (~1.0.1 || >=2.1 <2.4)
satisfies = version: constraint:
l.any
(c:
l.all
(satisfiesSingle version)
(splitConjunctives c))
(splitAlternatives constraint);

# matching multiversion like the one in `provide` with semver
# (1.0|2.0) (^2.0 || 3.2 - 3.6)
multiSatisfies = multiversion: constraint:
l.any
(version: satisfies version constraint)
(splitAlternatives multiversion);
}
5 changes: 1 addition & 4 deletions src/subsystems/php/translators/composer-json/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
dlib,
lib,
...
}: let
l = lib // builtins;
in {
}: {
type = "impure";

# A derivation which outputs a single executable at `$out`.
Expand All @@ -27,7 +25,6 @@ in {
coreutils,
jq,
phpPackages,
writeScriptBin,
...
}:
utils.writePureShellScript
Expand Down
Loading