Package up dependencies with composer2nix

I had to patch composer2nix with:

require_once(dirname(__FILE__)./../../../../vendor/autoload.php);
This commit is contained in:
Graham Christensen 2018-08-08 13:28:14 -04:00
parent a7000b99bf
commit 648fcf0405
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
6 changed files with 416 additions and 20 deletions

View file

@ -68,22 +68,5 @@ EOF
fi fi
''; '';
ofborg.php = pkgs.runCommand ofborg.php = import ./php { inherit pkgs; };
"ofborg"
{
src = builtins.filterSource
(path: type: !(
(type == "symlink" && baseNameOf path == "result")
|| (type == "directory" && baseNameOf path == ".git")
))
./php;
}
''
cp -r $src ./ofborg
chmod -R u+w ./ofborg
cd ofborg
ls -la
cd ..
mv ofborg $out
'';
} }

270
php/composer-env.nix Normal file
View file

@ -0,0 +1,270 @@
# This file originates from composer2nix
{ stdenv, writeTextFile, fetchurl, php, unzip }:
let
composer = stdenv.mkDerivation {
name = "composer-1.6.5";
src = fetchurl {
url = https://github.com/composer/composer/releases/download/1.6.5/composer.phar;
sha256 = "07xkpg9y1dd4s33y3cbf7r5fphpgc39mpm066a8m9y4ffsf539f0";
};
buildInputs = [ php ];
# We must wrap the composer.phar because of the impure shebang.
# We cannot use patchShebangs because the executable verifies its own integrity and will detect that somebody has tampered with it.
buildCommand = ''
# Copy phar file
mkdir -p $out/share/php
cp $src $out/share/php/composer.phar
chmod 755 $out/share/php/composer.phar
# Create wrapper executable
mkdir -p $out/bin
cat > $out/bin/composer <<EOF
#! ${stdenv.shell} -e
exec ${php}/bin/php $out/share/php/composer.phar "\$@"
EOF
chmod +x $out/bin/composer
'';
meta = {
description = "Dependency Manager for PHP";
#license = stdenv.licenses.mit;
maintainers = [ stdenv.lib.maintainers.sander ];
platforms = stdenv.lib.platforms.unix;
};
};
buildZipPackage = { name, src }:
stdenv.mkDerivation {
inherit name src;
buildInputs = [ unzip ];
buildCommand = ''
unzip $src
baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
cd $baseDir
mkdir -p $out
mv * $out
'';
};
buildPackage =
{ name
, src
, packages ? {}
, devPackages ? {}
, buildInputs ? []
, symlinkDependencies ? false
, executable ? false
, removeComposerArtifacts ? false
, postInstall ? ""
, noDev ? false
, unpackPhase ? "true"
, buildPhase ? "true"
, ...}@args:
let
reconstructInstalled = writeTextFile {
name = "reconstructinstalled.php";
executable = true;
text = ''
#! ${php}/bin/php
<?php
if(file_exists($argv[1]))
{
$composerLockStr = file_get_contents($argv[1]);
if($composerLockStr === false)
{
fwrite(STDERR, "Cannot open composer.lock contents\n");
exit(1);
}
else
{
$config = json_decode($composerLockStr, true);
if(array_key_exists("packages", $config))
$allPackages = $config["packages"];
else
$allPackages = array();
${stdenv.lib.optionalString (!noDev) ''
if(array_key_exists("packages-dev", $config))
$allPackages = array_merge($allPackages, $config["packages-dev"]);
''}
$packagesStr = json_encode($allPackages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
print($packagesStr);
}
}
else
print("[]");
?>
'';
};
constructBin = writeTextFile {
name = "constructbin.php";
executable = true;
text = ''
#! ${php}/bin/php
<?php
$composerJSONStr = file_get_contents($argv[1]);
if($composerJSONStr === false)
{
fwrite(STDERR, "Cannot open composer.json contents\n");
exit(1);
}
else
{
$config = json_decode($composerJSONStr, true);
if(array_key_exists("bin-dir", $config))
$binDir = $config["bin-dir"];
else
$binDir = "bin";
if(array_key_exists("bin", $config))
{
if(!file_exists("vendor/".$binDir))
mkdir("vendor/".$binDir);
foreach($config["bin"] as $bin)
symlink("../../".$bin, "vendor/".$binDir."/".basename($bin));
}
}
?>
'';
};
bundleDependencies = dependencies:
stdenv.lib.concatMapStrings (dependencyName:
let
dependency = dependencies.${dependencyName};
in
''
${if dependency.targetDir == "" then ''
vendorDir="$(dirname ${dependencyName})"
mkdir -p "$vendorDir"
${if symlinkDependencies then
''ln -s "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
else
''cp -av "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
}
'' else ''
namespaceDir="${dependencyName}/$(dirname "${dependency.targetDir}")"
mkdir -p "$namespaceDir"
${if symlinkDependencies then
''ln -s "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
else
''cp -av "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
}
''}
'') (builtins.attrNames dependencies);
extraArgs = removeAttrs args [ "name" "packages" "devPackages" "buildInputs" ];
in
stdenv.mkDerivation ({
name = "composer-${name}";
buildInputs = [ php composer ] ++ buildInputs;
inherit unpackPhase buildPhase;
installPhase = ''
${if executable then ''
mkdir -p $out/share/php
cp -av $src $out/share/php/$name
chmod -R u+w $out/share/php/$name
cd $out/share/php/$name
'' else ''
cp -av $src $out
chmod -R u+w $out
cd $out
''}
# Remove unwanted files
rm -f *.nix
export HOME=$TMPDIR
# Remove the provided vendor folder if it exists
rm -Rf vendor
# If there is no composer.lock file, compose a dummy file.
# Otherwise, composer attempts to download the package.json file from
# the registry which we do not want.
if [ ! -f composer.lock ]
then
cat > composer.lock <<EOF
{
"packages": []
}
EOF
fi
# Reconstruct the installed.json file from the lock file
mkdir -p vendor/composer
${reconstructInstalled} composer.lock > vendor/composer/installed.json
# Copy or symlink the provided dependencies
cd vendor
${bundleDependencies packages}
${stdenv.lib.optionalString (!noDev) (bundleDependencies devPackages)}
cd ..
# Reconstruct autoload scripts
# We use the optimize feature because Nix packages cannot change after they have been built
# Using the dynamic loader for a Nix package is useless since there is nothing to dynamically reload.
composer dump-autoload --optimize ${stdenv.lib.optionalString noDev "--no-dev"}
# Run the install step as a validation to confirm that everything works out as expected
composer install --optimize-autoloader ${stdenv.lib.optionalString noDev "--no-dev"}
${stdenv.lib.optionalString executable ''
# Reconstruct the bin/ folder if we deploy an executable project
${constructBin} composer.json
ln -s $(pwd)/vendor/bin $out/bin
''}
${stdenv.lib.optionalString (!symlinkDependencies) ''
# Patch the shebangs if possible
if [ -d $(pwd)/vendor/bin ]
then
# Look for all executables in bin/
for i in $(pwd)/vendor/bin/*
do
# Look for their location
realFile=$(readlink -f "$i")
# Restore write permissions
chmod u+wx "$(dirname "$realFile")"
chmod u+w "$realFile"
# Patch shebang
sed -e "s|#!/usr/bin/php|#!${php}/bin/php|" \
-e "s|#!/usr/bin/env php|#!${php}/bin/php|" \
"$realFile" > tmp
mv tmp "$realFile"
chmod u+x "$realFile"
done
fi
''}
if [ "$removeComposerArtifacts" = "1" ]
then
# Remove composer stuff
rm -f composer.json composer.lock
fi
# Execute post install hook
runHook postInstall
'';
} // extraArgs);
in
{
composer = stdenv.lib.makeOverridable composer;
buildZipPackage = stdenv.lib.makeOverridable buildZipPackage;
buildPackage = stdenv.lib.makeOverridable buildPackage;
}

View file

@ -1,5 +1,7 @@
{ {
"name": "ofborg-webhook",
"require": { "require": {
"php-amqplib/php-amqplib": ">=2.6.1" "php-amqplib/php-amqplib": ">=2.6.1",
"svanderburg/composer2nix": ">=0.0.3"
} }
} }

85
php/composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "f0b42ac9169509834501cb7aa271b580", "content-hash": "81858eba648031903f09c76e5d8e23cd",
"packages": [ "packages": [
{ {
"name": "php-amqplib/php-amqplib", "name": "php-amqplib/php-amqplib",
@ -76,6 +76,89 @@
"rabbitmq" "rabbitmq"
], ],
"time": "2018-02-11T19:28:00+00:00" "time": "2018-02-11T19:28:00+00:00"
},
{
"name": "svanderburg/composer2nix",
"version": "v0.0.3",
"source": {
"type": "git",
"url": "https://github.com/svanderburg/composer2nix.git",
"reference": "2fb157acaf0ecbe34436195c694637396f7258a6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/svanderburg/composer2nix/zipball/2fb157acaf0ecbe34436195c694637396f7258a6",
"reference": "2fb157acaf0ecbe34436195c694637396f7258a6",
"shasum": ""
},
"require": {
"svanderburg/pndp": "0.0.2"
},
"require-dev": {
"phpdocumentor/phpdocumentor": "2.9.x"
},
"bin": [
"bin/composer2nix"
],
"type": "library",
"autoload": {
"psr-4": {
"Composer2Nix\\": "src/Composer2Nix"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Sander van der Burg",
"email": "svanderburg@gmail.com",
"homepage": "http://sandervanderburg.nl"
}
],
"description": "Generate Nix expressions to build PHP composer packages",
"time": "2018-06-29T20:58:30+00:00"
},
{
"name": "svanderburg/pndp",
"version": "v0.0.2",
"source": {
"type": "git",
"url": "https://github.com/svanderburg/pndp.git",
"reference": "4bfe9c4120c23354ab8dc295957dc3009a39bff0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/svanderburg/pndp/zipball/4bfe9c4120c23354ab8dc295957dc3009a39bff0",
"reference": "4bfe9c4120c23354ab8dc295957dc3009a39bff0",
"shasum": ""
},
"require-dev": {
"phpdocumentor/phpdocumentor": "2.9.x"
},
"bin": [
"bin/pndp-build"
],
"type": "library",
"autoload": {
"psr-4": {
"PNDP\\": "src/PNDP"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Sander van der Burg",
"email": "svanderburg@gmail.com",
"homepage": "http://sandervanderburg.nl"
}
],
"description": "PNDP: An internal DSL for Nix in PHP",
"time": "2017-10-22T12:43:22+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [],

13
php/default.nix Normal file
View file

@ -0,0 +1,13 @@
{pkgs ? import <nixpkgs> {
inherit system;
}, system ? builtins.currentSystem, noDev ? false}:
let
composerEnv = import ./composer-env.nix {
inherit (pkgs) stdenv writeTextFile fetchurl php unzip;
};
in
import ./php-packages.nix {
inherit composerEnv noDev;
inherit (pkgs) fetchurl fetchgit fetchhg fetchsvn;
}

45
php/php-packages.nix Normal file
View file

@ -0,0 +1,45 @@
{composerEnv, fetchurl, fetchgit ? null, fetchhg ? null, fetchsvn ? null, noDev ? false}:
let
packages = {
"php-amqplib/php-amqplib" = {
targetDir = "";
src = composerEnv.buildZipPackage {
name = "php-amqplib-php-amqplib-dfd3694a86f1a7394d3693485259d4074a6ec79b";
src = fetchurl {
url = https://api.github.com/repos/php-amqplib/php-amqplib/zipball/dfd3694a86f1a7394d3693485259d4074a6ec79b;
sha256 = "1dlxgdnhy8xyx8xbp1glc7igksvsqyc3yaq76irhy09djij013ip";
};
};
};
"svanderburg/composer2nix" = {
targetDir = "";
src = composerEnv.buildZipPackage {
name = "svanderburg-composer2nix-2fb157acaf0ecbe34436195c694637396f7258a6";
src = fetchurl {
url = https://api.github.com/repos/svanderburg/composer2nix/zipball/2fb157acaf0ecbe34436195c694637396f7258a6;
sha256 = "01i3kxgx7pcmxafclp8ib08nib1xh6nvr5sbl6y38rw19xhnwa0m";
};
};
};
"svanderburg/pndp" = {
targetDir = "";
src = composerEnv.buildZipPackage {
name = "svanderburg-pndp-4bfe9c4120c23354ab8dc295957dc3009a39bff0";
src = fetchurl {
url = https://api.github.com/repos/svanderburg/pndp/zipball/4bfe9c4120c23354ab8dc295957dc3009a39bff0;
sha256 = "0n2vwpwshv16bhb7a6j95m664zh4lpfa7dqmcyhmn89nxpgvg91y";
};
};
};
};
devPackages = {};
in
composerEnv.buildPackage {
inherit packages devPackages noDev;
name = "ofborg-webhook";
src = ./.;
executable = false;
symlinkDependencies = false;
meta = {};
}