1 # This file originates from composer2nix
14 inherit (phpPackages) composer;
18 builtins.filterSource (
21 || (baseNameOf path != ".git" && baseNameOf path != ".git" && baseNameOf path != ".svn")
28 nativeBuildInputs = [ unzip ];
32 baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
46 symlinkDependencies ? false,
48 removeComposerArtifacts ? false,
51 composerExtraArgs ? "",
58 reconstructInstalled = writeTextFile {
59 name = "reconstructinstalled.php";
64 if(file_exists($argv[1]))
66 $composerLockStr = file_get_contents($argv[1]);
68 if($composerLockStr === false)
70 fwrite(STDERR, "Cannot open composer.lock contents\n");
75 $config = json_decode($composerLockStr, true);
77 if(array_key_exists("packages", $config))
78 $allPackages = $config["packages"];
80 $allPackages = array();
82 ${lib.optionalString (!noDev) ''
83 if(array_key_exists("packages-dev", $config))
84 $allPackages = array_merge($allPackages, $config["packages-dev"]);
87 $packagesStr = json_encode($allPackages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
97 constructBin = writeTextFile {
98 name = "constructbin.php";
103 $composerJSONStr = file_get_contents($argv[1]);
105 if($composerJSONStr === false)
107 fwrite(STDERR, "Cannot open composer.json contents\n");
112 $config = json_decode($composerJSONStr, true);
114 if(array_key_exists("bin-dir", $config))
115 $binDir = $config["bin-dir"];
119 if(array_key_exists("bin", $config))
121 if(!file_exists("vendor/".$binDir))
122 mkdir("vendor/".$binDir);
124 foreach($config["bin"] as $bin)
125 symlink("../../".$bin, "vendor/".$binDir."/".basename($bin));
134 lib.concatMapStrings (
137 dependency = dependencies.${dependencyName};
141 if dependency.targetDir == "" then
143 vendorDir="$(dirname ${dependencyName})"
144 mkdir -p "$vendorDir"
146 if symlinkDependencies then
147 ''ln -s "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
149 ''cp -av "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
154 namespaceDir="${dependencyName}/$(dirname "${dependency.targetDir}")"
155 mkdir -p "$namespaceDir"
157 if symlinkDependencies then
158 ''ln -s "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
160 ''cp -av "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
165 ) (builtins.attrNames dependencies);
167 extraArgs = removeAttrs args [
173 stdenv.mkDerivation (
180 inherit unpackPhase buildPhase;
186 mkdir -p $out/share/php
187 cp -av $src $out/share/php/$name
188 chmod -R u+w $out/share/php/$name
189 cd $out/share/php/$name
199 # Remove unwanted files
204 # Remove the provided vendor folder if it exists
207 # If there is no composer.lock file, compose a dummy file.
208 # Otherwise, composer attempts to download the package.json file from
209 # the registry which we do not want.
210 if [ ! -f composer.lock ]
212 cat > composer.lock <<EOF
219 # Reconstruct the installed.json file from the lock file
220 mkdir -p vendor/composer
221 ${php}/bin/php ${reconstructInstalled} composer.lock > vendor/composer/installed.json
223 # Copy or symlink the provided dependencies
225 ${bundleDependencies packages}
226 ${lib.optionalString (!noDev) (bundleDependencies devPackages)}
229 # Reconstruct autoload scripts
230 # We use the optimize feature because Nix packages cannot change after they have been built
231 # Using the dynamic loader for a Nix package is useless since there is nothing to dynamically reload.
232 composer dump-autoload --optimize ${lib.optionalString noDev "--no-dev"} ${composerExtraArgs}
234 # Run the install step as a validation to confirm that everything works out as expected
235 composer install --optimize-autoloader ${lib.optionalString noDev "--no-dev"} ${composerExtraArgs}
237 ${lib.optionalString executable ''
238 # Reconstruct the bin/ folder if we deploy an executable project
239 ${php}/bin/php ${constructBin} composer.json
240 ln -s $(pwd)/vendor/bin $out/bin
243 ${lib.optionalString (!symlinkDependencies) ''
244 # Patch the shebangs if possible
245 if [ -d $(pwd)/vendor/bin ]
247 # Look for all executables in bin/
248 for i in $(pwd)/vendor/bin/*
250 # Look for their location
251 realFile=$(readlink -f "$i")
253 # Restore write permissions
254 chmod u+wx "$(dirname "$realFile")"
255 chmod u+w "$realFile"
258 sed -e "s|#!/usr/bin/php|#!${php}/bin/php|" \
259 -e "s|#!/usr/bin/env php|#!${php}/bin/php|" \
262 chmod u+x "$realFile"
267 if [ "$removeComposerArtifacts" = "1" ]
269 # Remove composer stuff
270 rm -f composer.json composer.lock
273 # Execute post install hook
282 composer = lib.makeOverridable composer;
283 buildZipPackage = lib.makeOverridable buildZipPackage;
284 buildPackage = lib.makeOverridable buildPackage;