1 # This file originates from composer2nix
3 { stdenv, lib, writeTextFile, fetchurl, php, unzip }:
6 composer = stdenv.mkDerivation {
7 name = "composer-1.8.0";
9 url = https://github.com/composer/composer/releases/download/1.8.0/composer.phar;
10 sha256 = "19pg9ip2mpyf5cyq34fld7qwl77mshqw3c4nif7sxmpnar6sh089";
12 buildInputs = [ php ];
14 # We must wrap the composer.phar because of the impure shebang.
15 # We cannot use patchShebangs because the executable verifies its own integrity and will detect that somebody has tampered with it.
19 mkdir -p $out/share/php
20 cp $src $out/share/php/composer.phar
21 chmod 755 $out/share/php/composer.phar
23 # Create wrapper executable
25 cat > $out/bin/composer <<EOF
27 exec ${php}/bin/php $out/share/php/composer.phar "\$@"
29 chmod +x $out/bin/composer
32 description = "Dependency Manager for PHP";
33 #license = lib.licenses.mit;
34 maintainers = [ lib.maintainers.sander ];
35 platforms = lib.platforms.unix;
39 buildZipPackage = { name, src }:
42 buildInputs = [ unzip ];
45 baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
58 , symlinkDependencies ? false
60 , removeComposerArtifacts ? false
64 , unpackPhase ? "true"
66 , doRemoveVendor ? true
70 reconstructInstalled = writeTextFile {
71 name = "reconstructinstalled.php";
76 if(file_exists($argv[1]))
78 $composerLockStr = file_get_contents($argv[1]);
80 if($composerLockStr === false)
82 fwrite(STDERR, "Cannot open composer.lock contents\n");
87 $config = json_decode($composerLockStr, true);
89 if(array_key_exists("packages", $config))
90 $allPackages = $config["packages"];
92 $allPackages = array();
94 ${lib.optionalString (!noDev) ''
95 if(array_key_exists("packages-dev", $config))
96 $allPackages = array_merge($allPackages, $config["packages-dev"]);
99 $packagesStr = json_encode($allPackages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
109 constructBin = writeTextFile {
110 name = "constructbin.php";
115 $composerJSONStr = file_get_contents($argv[1]);
117 if($composerJSONStr === false)
119 fwrite(STDERR, "Cannot open composer.json contents\n");
124 $config = json_decode($composerJSONStr, true);
126 if(array_key_exists("bin-dir", $config))
127 $binDir = $config["bin-dir"];
131 if(array_key_exists("bin", $config))
133 if(!file_exists("vendor/".$binDir))
134 mkdir("vendor/".$binDir);
136 foreach($config["bin"] as $bin)
137 symlink("../../".$bin, "vendor/".$binDir."/".basename($bin));
144 bundleDependencies = dependencies:
145 lib.concatMapStrings (dependencyName:
147 dependency = dependencies.${dependencyName};
150 ${if dependency.targetDir == "" then ''
151 vendorDir="$(dirname ${dependencyName})"
152 mkdir -p "$vendorDir"
153 ${if symlinkDependencies then
154 ''ln -s "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
156 ''cp -a "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
157 }${if dependency.needsModifyRights or false then "\n" + ''
158 chmod -R u+rwx "$vendorDir/$(basename "${dependencyName}")"
161 namespaceDir="${dependencyName}/$(dirname "${dependency.targetDir}")"
162 mkdir -p "$namespaceDir"
163 ${if symlinkDependencies then
164 ''ln -s "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
166 ''cp -a "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
167 }${if dependency.needsModifyRights or false then "\n" + ''
168 chmod -R u+rwx "$namespaceDir/$(basename "${dependency.targetDir}")"
171 '') (builtins.attrNames dependencies);
173 extraArgs = removeAttrs args [ "name" "packages" "devPackages" "buildInputs" ];
175 stdenv.mkDerivation ({
176 name = "composer-${name}";
177 buildInputs = [ php composer ] ++ buildInputs;
179 inherit unpackPhase buildPhase;
182 ${if executable then ''
183 mkdir -p $out/share/php
184 cp -a $src $out/share/php/$name
185 chmod -R u+w $out/share/php/$name
186 cd $out/share/php/$name
193 # Execute pre install hook
196 # Remove unwanted files
201 ${if doRemoveVendor then ''
202 # Remove the provided vendor folder if it exists
205 # If there is no composer.lock file, compose a dummy file.
206 # Otherwise, composer attempts to download the package.json file from
207 # the registry which we do not want.
208 if [ ! -f composer.lock ]
210 cat > composer.lock <<EOF
217 # Reconstruct the installed.json file from the lock file
218 mkdir -p vendor/composer
219 ${reconstructInstalled} composer.lock > vendor/composer/installed.json
221 # Copy or symlink the provided dependencies
223 ${bundleDependencies packages}
224 ${lib.optionalString (!noDev) (bundleDependencies devPackages)}
227 # Reconstruct autoload scripts
228 # We use the optimize feature because Nix packages cannot change after they have been built
229 # Using the dynamic loader for a Nix package is useless since there is nothing to dynamically reload.
230 composer dump-autoload --optimize ${lib.optionalString noDev "--no-dev"}
232 # Run the install step as a validation to confirm that everything works out as expected
233 composer install --optimize-autoloader ${lib.optionalString noDev "--no-dev"}
235 ${lib.optionalString executable ''
236 # Reconstruct the bin/ folder if we deploy an executable project
237 ${constructBin} composer.json
238 ln -s $(pwd)/vendor/bin $out/bin
241 ${lib.optionalString (!symlinkDependencies) ''
242 # Patch the shebangs if possible
243 if [ -d $(pwd)/vendor/bin ]
245 # Look for all executables in bin/
246 for i in $(pwd)/vendor/bin/*
248 # Look for their location
249 realFile=$(readlink -f "$i")
251 # Restore write permissions
252 chmod u+wx "$(dirname "$realFile")"
253 chmod u+w "$realFile"
256 sed -e "s|#!/usr/bin/php|#!${php}/bin/php|" \
257 -e "s|#!/usr/bin/env php|#!${php}/bin/php|" \
260 chmod u+x "$realFile"
265 if [ "$removeComposerArtifacts" = "1" ]
267 # Remove composer stuff
268 rm -f composer.json composer.lock
271 # Execute post install hook
276 { inherit composer buildZipPackage buildPackage; }