diff options
Diffstat (limited to 'pkgs/build-support/php/builders/v1/hooks')
5 files changed, 458 insertions, 0 deletions
diff --git a/pkgs/build-support/php/builders/v1/hooks/composer-install-hook.sh b/pkgs/build-support/php/builders/v1/hooks/composer-install-hook.sh new file mode 100644 index 0000000000000..44e87d06d3a53 --- /dev/null +++ b/pkgs/build-support/php/builders/v1/hooks/composer-install-hook.sh @@ -0,0 +1,126 @@ +declare composerRepository +declare version +declare composerNoDev +declare composerNoPlugins +declare composerNoScripts + +preConfigureHooks+=(composerInstallConfigureHook) +preBuildHooks+=(composerInstallBuildHook) +preCheckHooks+=(composerInstallCheckHook) +preInstallHooks+=(composerInstallInstallHook) + +source @phpScriptUtils@ + +composerInstallConfigureHook() { + echo "Executing composerInstallConfigureHook" + + if [[ ! -e "${composerRepository}" ]]; then + echo "No local composer repository found." + exit 1 + fi + + if [[ -e "$composerLock" ]]; then + cp "$composerLock" composer.lock + fi + + if [[ ! -f "composer.lock" ]]; then + setComposeRootVersion + + composer \ + --no-install \ + --no-interaction \ + --no-progress \ + ${composerNoDev:+--no-dev} \ + ${composerNoPlugins:+--no-plugins} \ + ${composerNoScripts:+--no-scripts} \ + update + + mkdir -p $out + cp composer.lock $out/ + + echo + echo -e "\e[31mERROR: No composer.lock found\e[0m" + echo + echo -e '\e[31mNo composer.lock file found, consider adding one to your repository to ensure reproducible builds.\e[0m' + echo -e "\e[31mIn the meantime, a composer.lock file has been generated for you in $out/composer.lock\e[0m" + echo + echo -e '\e[31mTo fix the issue:\e[0m' + echo -e "\e[31m1. Copy the composer.lock file from $out/composer.lock to the project's source:\e[0m" + echo -e "\e[31m cp $out/composer.lock <path>\e[0m" + echo -e '\e[31m2. Add the composerLock attribute, pointing to the copied composer.lock file:\e[0m' + echo -e '\e[31m composerLock = ./composer.lock;\e[0m' + echo + + exit 1 + fi + + echo "Validating consistency between composer.lock and ${composerRepository}/composer.lock" + if ! @cmp@ -s "composer.lock" "${composerRepository}/composer.lock"; then + echo + echo -e "\e[31mERROR: vendorHash is out of date\e[0m" + echo + echo -e "\e[31mcomposer.lock is not the same in $composerRepository\e[0m" + echo + echo -e "\e[31mTo fix the issue:\e[0m" + echo -e '\e[31m1. Set vendorHash to an empty string: `vendorHash = "";`\e[0m' + echo -e '\e[31m2. Build the derivation and wait for it to fail with a hash mismatch\e[0m' + echo -e '\e[31m3. Copy the "got: sha256-..." value back into the vendorHash field\e[0m' + echo -e '\e[31m You should have: vendorHash = "sha256-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=";\e[0m' + echo + + exit 1 + fi + + chmod +w composer.json composer.lock + + echo "Finished composerInstallConfigureHook" +} + +composerInstallBuildHook() { + echo "Executing composerInstallBuildHook" + + setComposeRootVersion + + # Since this file cannot be generated in the composer-repository-hook.sh + # because the file contains hardcoded nix store paths, we generate it here. + composer build-local-repo-lock -m "${composerRepository}" . + + echo "Finished composerInstallBuildHook" +} + +composerInstallCheckHook() { + echo "Executing composerInstallCheckHook" + + checkComposerValidate + + echo "Finished composerInstallCheckHook" +} + +composerInstallInstallHook() { + echo "Executing composerInstallInstallHook" + + setComposeRootVersion + + # Finally, run `composer install` to install the dependencies and generate + # the autoloader. + composer \ + --no-interaction \ + --no-progress \ + ${composerNoDev:+--no-dev} \ + ${composerNoPlugins:+--no-plugins} \ + ${composerNoScripts:+--no-scripts} \ + install + + # Copy the relevant files only in the store. + mkdir -p "$out"/share/php/"${pname}" + cp -r . "$out"/share/php/"${pname}"/ + + # Create symlinks for the binaries. + jq -r -c 'try (.bin[] | select(test(".bat$")? | not) )' composer.json | while read -r bin; do + echo -e "\e[32mCreating symlink ${bin}...\e[0m" + mkdir -p "$out"/bin + ln -s "$out"/share/php/"${pname}"/"$bin" "$out"/bin/"$(basename "$bin")" + done + + echo "Finished composerInstallInstallHook" +} diff --git a/pkgs/build-support/php/builders/v1/hooks/composer-repository-hook.sh b/pkgs/build-support/php/builders/v1/hooks/composer-repository-hook.sh new file mode 100644 index 0000000000000..ec9777541fc0f --- /dev/null +++ b/pkgs/build-support/php/builders/v1/hooks/composer-repository-hook.sh @@ -0,0 +1,91 @@ +declare composerLock +declare version +declare composerNoDev +declare composerNoPlugins +declare composerNoScripts +declare composerStrictValidation + +preConfigureHooks+=(composerRepositoryConfigureHook) +preBuildHooks+=(composerRepositoryBuildHook) +preCheckHooks+=(composerRepositoryCheckHook) +preInstallHooks+=(composerRepositoryInstallHook) + +source @phpScriptUtils@ + +composerRepositoryConfigureHook() { + echo "Executing composerRepositoryConfigureHook" + + if [[ -e "$composerLock" ]]; then + cp $composerLock composer.lock + fi + + if [[ ! -f "composer.lock" ]]; then + setComposeRootVersion + + composer \ + --no-install \ + --no-interaction \ + --no-progress \ + ${composerNoDev:+--no-dev} \ + ${composerNoPlugins:+--no-plugins} \ + ${composerNoScripts:+--no-scripts} \ + update + + mkdir -p $out + cp composer.lock $out/ + + echo + echo -e "\e[31mERROR: No composer.lock found\e[0m" + echo + echo -e '\e[31mNo composer.lock file found, consider adding one to your repository to ensure reproducible builds.\e[0m' + echo -e "\e[31mIn the meantime, a composer.lock file has been generated for you in $out/composer.lock\e[0m" + echo + echo -e '\e[31mTo fix the issue:\e[0m' + echo -e "\e[31m1. Copy the composer.lock file from $out/composer.lock to the project's source:\e[0m" + echo -e "\e[31m cp $out/composer.lock <path>\e[0m" + echo -e '\e[31m2. Add the composerLock attribute, pointing to the copied composer.lock file:\e[0m' + echo -e '\e[31m composerLock = ./composer.lock;\e[0m' + echo + + exit 1 + fi + + echo "Finished composerRepositoryConfigureHook" +} + +composerRepositoryBuildHook() { + echo "Executing composerRepositoryBuildHook" + + mkdir -p repository + + setComposeRootVersion + + # Build the local composer repository + # The command 'build-local-repo' is provided by the Composer plugin + # nix-community/composer-local-repo-plugin. + composer build-local-repo-lock ${composerNoDev:+--no-dev} -r repository + + echo "Finished composerRepositoryBuildHook" +} + +composerRepositoryCheckHook() { + echo "Executing composerRepositoryCheckHook" + + checkComposerValidate + + echo "Finished composerRepositoryCheckHook" +} + +composerRepositoryInstallHook() { + echo "Executing composerRepositoryInstallHook" + + mkdir -p $out + + cp -ar repository/. $out/ + + # Copy the composer.lock files to the output directory, to be able to validate consistency with + # the src composer.lock file where this fixed-output derivation is used + cp composer.lock $out/ + + echo "Finished composerRepositoryInstallHook" +} diff --git a/pkgs/build-support/php/builders/v1/hooks/composer-with-plugin-vendor-hook.sh b/pkgs/build-support/php/builders/v1/hooks/composer-with-plugin-vendor-hook.sh new file mode 100644 index 0000000000000..0d88d14094ad4 --- /dev/null +++ b/pkgs/build-support/php/builders/v1/hooks/composer-with-plugin-vendor-hook.sh @@ -0,0 +1,93 @@ +declare composerLock +declare version +declare composerNoDev +declare composerNoPlugins +declare composerNoScripts +declare composerStrictValidation + +preConfigureHooks+=(composerWithPluginConfigureHook) +preBuildHooks+=(composerWithPluginBuildHook) +preCheckHooks+=(composerWithPluginCheckHook) +preInstallHooks+=(composerWithPluginInstallHook) +preInstallCheckHooks+=(composerWithPluginInstallCheckHook) + +source @phpScriptUtils@ + +composerWithPluginConfigureHook() { + echo "Executing composerWithPluginConfigureHook" + + mkdir -p $out + + export COMPOSER_HOME=$out + + if [[ -e "$composerLock" ]]; then + cp $composerLock $out/composer.lock + fi + + cp $composerJson $out/composer.json + cp -ar $src $out/src + + if [[ ! -f "$out/composer.lock" ]]; then + setComposeRootVersion + + composer \ + global \ + --no-install \ + --no-interaction \ + --no-progress \ + ${composerNoDev:+--no-dev} \ + ${composerNoPlugins:+--no-plugins} \ + ${composerNoScripts:+--no-scripts} \ + update + + echo + echo -e "\e[31mERROR: No composer.lock found\e[0m" + echo + echo -e '\e[31mNo composer.lock file found, consider adding one to your repository to ensure reproducible builds.\e[0m' + echo -e "\e[31mIn the meantime, a composer.lock file has been generated for you in $out/composer.lock\e[0m" + echo + echo -e '\e[31mTo fix the issue:\e[0m' + echo -e "\e[31m1. Copy the composer.lock file from $out/composer.lock to the project's source:\e[0m" + echo -e "\e[31m cp $out/composer.lock <path>\e[0m" + echo -e '\e[31m2. Add the composerLock attribute, pointing to the copied composer.lock file:\e[0m' + echo -e '\e[31m composerLock = ./composer.lock;\e[0m' + echo + + exit 1 + fi + + echo "Finished composerWithPluginConfigureHook" +} + +composerWithPluginBuildHook() { + echo "Executing composerWithPluginBuildHook" + + echo "Finished composerWithPluginBuildHook" +} + +composerWithPluginCheckHook() { + echo "Executing composerWithPluginCheckHook" + + checkComposerValidate + + echo "Finished composerWithPluginCheckHook" +} + +composerWithPluginInstallHook() { + echo "Executing composerWithPluginInstallHook" + + composer \ + global \ + --no-interaction \ + --no-progress \ + ${composerNoDev:+--no-dev} \ + ${composerNoPlugins:+--no-plugins} \ + ${composerNoScripts:+--no-scripts} \ + install + + echo "Finished composerWithPluginInstallHook" +} + +composerWithPluginInstallCheckHook() { + composer global show $pluginName +} diff --git a/pkgs/build-support/php/builders/v1/hooks/default.nix b/pkgs/build-support/php/builders/v1/hooks/default.nix new file mode 100644 index 0000000000000..d10ff78067278 --- /dev/null +++ b/pkgs/build-support/php/builders/v1/hooks/default.nix @@ -0,0 +1,60 @@ +{ + lib, + makeSetupHook, + jq, + writeShellApplication, + moreutils, + cacert, + buildPackages, +}: + +let + php-script-utils = writeShellApplication { + name = "php-script-utils"; + runtimeInputs = [ jq ]; + text = builtins.readFile ./php-script-utils.bash; + }; +in +{ + composerRepositoryHook = makeSetupHook { + name = "composer-repository-hook.sh"; + propagatedBuildInputs = [ + jq + moreutils + cacert + ]; + substitutions = { + phpScriptUtils = lib.getExe php-script-utils; + }; + } ./composer-repository-hook.sh; + + composerInstallHook = makeSetupHook { + name = "composer-install-hook.sh"; + propagatedBuildInputs = [ + jq + moreutils + cacert + ]; + substitutions = { + # Specify the stdenv's `diff` by abspath to ensure that the user's build + # inputs do not cause us to find the wrong `diff`. + cmp = "${lib.getBin buildPackages.diffutils}/bin/cmp"; + phpScriptUtils = lib.getExe php-script-utils; + }; + } ./composer-install-hook.sh; + + composerWithPluginVendorHook = makeSetupHook { + name = "composer-with-plugin-vendor-hook.sh"; + propagatedBuildInputs = [ + jq + moreutils + cacert + ]; + substitutions = { + # Specify the stdenv's `diff` by abspath to ensure that the user's build + # inputs do not cause us to find the wrong `diff`. + cmp = "${lib.getBin buildPackages.diffutils}/bin/cmp"; + phpScriptUtils = lib.getExe php-script-utils; + }; + } ./composer-with-plugin-vendor-hook.sh; +} diff --git a/pkgs/build-support/php/builders/v1/hooks/php-script-utils.bash b/pkgs/build-support/php/builders/v1/hooks/php-script-utils.bash new file mode 100644 index 0000000000000..65c0a3b410f69 --- /dev/null +++ b/pkgs/build-support/php/builders/v1/hooks/php-script-utils.bash @@ -0,0 +1,88 @@ +declare version +declare composerStrictValidation +declare composerGlobal + +setComposeRootVersion() { + set +e # Disable exit on error + + if [[ -v version ]]; then + echo -e "\e[32mSetting COMPOSER_ROOT_VERSION to $version\e[0m" + export COMPOSER_ROOT_VERSION=$version + fi + + set -e +} + +checkComposerValidate() { + setComposeRootVersion + + if [ "1" == "${composerGlobal-}" ]; then + global="global"; + else + global=""; + fi + + command="composer ${global} validate --strict --quiet --no-interaction --no-check-all --no-check-lock" + if ! $command; then + if [ "1" == "${composerStrictValidation-}" ]; then + echo + echo -e "\e[31mERROR: composer files validation failed\e[0m" + echo + echo -e '\e[31mThe validation of the composer.json failed.\e[0m' + echo -e '\e[31mMake sure that the file composer.json is valid.\e[0m' + echo + echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' + echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information, and apply any available remote patches as a temporary solution '\('with fetchpatch'\)'.\e[0m' + echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' + echo + exit 1 + else + echo + echo -e "\e[33mWARNING: composer files validation failed\e[0m" + echo + echo -e '\e[33mThe validation of the composer.json failed.\e[0m' + echo -e '\e[33mMake sure that the file composer.json is valid.\e[0m' + echo + echo -e '\e[33mTo address the issue efficiently, follow one of these steps:\e[0m' + echo -e '\e[33m 1. File an issue in the project'\''s issue tracker with detailed information, and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' + echo -e '\e[33m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' + echo + echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' + echo + fi + fi + + command="composer ${global} validate --strict --no-ansi --no-interaction --quiet --no-check-all --check-lock" + if ! $command; then + if [ "1" == "${composerStrictValidation-}" ]; then + echo + echo -e "\e[31mERROR: composer files validation failed\e[0m" + echo + echo -e '\e[31mThe validation of the composer.json and composer.lock failed.\e[0m' + echo -e '\e[31mMake sure that the file composer.lock is consistent with composer.json.\e[0m' + echo + echo -e '\e[31mThis often indicates an issue with the upstream project, which can typically be resolved by reporting the issue to the relevant project maintainers.\e[0m' + echo + echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' + echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information '\('run '\''composer update --lock --no-install'\'' to fix the issue'\)', and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' + echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' + echo + exit 1 + else + echo + echo -e "\e[33mWARNING: composer files validation failed\e[0m" + echo + echo -e '\e[33mThe validation of the composer.json and composer.lock failed.\e[0m' + echo -e '\e[33mMake sure that the file composer.lock is consistent with composer.json.\e[0m' + echo + echo -e '\e[33mThis often indicates an issue with the upstream project, which can typically be resolved by reporting the issue to the relevant project maintainers.\e[0m' + echo + echo -e '\e[33mTo address the issue efficiently, follow one of these steps:\e[0m' + echo -e '\e[33m 1. File an issue in the project'\''s issue tracker with detailed information '\('run '\''composer update --lock --no-install'\'' to fix the issue'\)', and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' + echo -e '\e[33m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' + echo + echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' + echo + fi + fi +} |