stdenv: fix documentation for stripAllFlags and stripDebugFlags (#352127)
[NixPkgs.git] / pkgs / servers / web-apps / bookstack / composer-env.nix
blobb050628e8d1b4d1b57846d192817a5e6ffc011e3
1 # This file originates from composer2nix
4   stdenv,
5   lib,
6   writeTextFile,
7   fetchurl,
8   php,
9   unzip,
10   phpPackages,
13 let
14   inherit (phpPackages) composer;
16   filterSrc =
17     src:
18     builtins.filterSource (
19       path: type:
20       type != "directory"
21       || (baseNameOf path != ".git" && baseNameOf path != ".git" && baseNameOf path != ".svn")
22     ) src;
24   buildZipPackage =
25     { name, src }:
26     stdenv.mkDerivation {
27       inherit name src;
28       nativeBuildInputs = [ unzip ];
29       buildCommand = ''
30         shopt -s dotglob
31         unzip $src
32         baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
33         cd $baseDir
34         mkdir -p $out
35         mv * $out
36       '';
37     };
39   buildPackage =
40     {
41       name,
42       src,
43       packages ? { },
44       devPackages ? { },
45       buildInputs ? [ ],
46       symlinkDependencies ? false,
47       executable ? false,
48       removeComposerArtifacts ? false,
49       postInstall ? "",
50       noDev ? false,
51       composerExtraArgs ? "",
52       unpackPhase ? "true",
53       buildPhase ? "true",
54       ...
55     }@args:
57     let
58       reconstructInstalled = writeTextFile {
59         name = "reconstructinstalled.php";
60         executable = true;
61         text = ''
62           #! ${php}/bin/php
63           <?php
64           if(file_exists($argv[1]))
65           {
66               $composerLockStr = file_get_contents($argv[1]);
68               if($composerLockStr === false)
69               {
70                   fwrite(STDERR, "Cannot open composer.lock contents\n");
71                   exit(1);
72               }
73               else
74               {
75                   $config = json_decode($composerLockStr, true);
77                   if(array_key_exists("packages", $config))
78                       $allPackages = $config["packages"];
79                   else
80                       $allPackages = array();
82                   ${lib.optionalString (!noDev) ''
83                     if(array_key_exists("packages-dev", $config))
84                         $allPackages = array_merge($allPackages, $config["packages-dev"]);
85                   ''}
87                   $packagesStr = json_encode($allPackages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
88                   print($packagesStr);
89               }
90           }
91           else
92               print("[]");
93           ?>
94         '';
95       };
97       constructBin = writeTextFile {
98         name = "constructbin.php";
99         executable = true;
100         text = ''
101           #! ${php}/bin/php
102           <?php
103           $composerJSONStr = file_get_contents($argv[1]);
105           if($composerJSONStr === false)
106           {
107               fwrite(STDERR, "Cannot open composer.json contents\n");
108               exit(1);
109           }
110           else
111           {
112               $config = json_decode($composerJSONStr, true);
114               if(array_key_exists("bin-dir", $config))
115                   $binDir = $config["bin-dir"];
116               else
117                   $binDir = "bin";
119               if(array_key_exists("bin", $config))
120               {
121                   if(!file_exists("vendor/".$binDir))
122                       mkdir("vendor/".$binDir);
124                   foreach($config["bin"] as $bin)
125                       symlink("../../".$bin, "vendor/".$binDir."/".basename($bin));
126               }
127           }
128           ?>
129         '';
130       };
132       bundleDependencies =
133         dependencies:
134         lib.concatMapStrings (
135           dependencyName:
136           let
137             dependency = dependencies.${dependencyName};
138           in
139           ''
140             ${
141               if dependency.targetDir == "" then
142                 ''
143                   vendorDir="$(dirname ${dependencyName})"
144                   mkdir -p "$vendorDir"
145                   ${
146                     if symlinkDependencies then
147                       ''ln -s "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
148                     else
149                       ''cp -av "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
150                   }
151                 ''
152               else
153                 ''
154                   namespaceDir="${dependencyName}/$(dirname "${dependency.targetDir}")"
155                   mkdir -p "$namespaceDir"
156                   ${
157                     if symlinkDependencies then
158                       ''ln -s "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
159                     else
160                       ''cp -av "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
161                   }
162                 ''
163             }
164           ''
165         ) (builtins.attrNames dependencies);
167       extraArgs = removeAttrs args [
168         "packages"
169         "devPackages"
170         "buildInputs"
171       ];
172     in
173     stdenv.mkDerivation (
174       {
175         buildInputs = [
176           php
177           composer
178         ] ++ buildInputs;
180         inherit unpackPhase buildPhase;
182         installPhase = ''
183           ${
184             if executable then
185               ''
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
190               ''
191             else
192               ''
193                 cp -av $src $out
194                 chmod -R u+w $out
195                 cd $out
196               ''
197           }
199           # Remove unwanted files
200           rm -f *.nix
202           export HOME=$TMPDIR
204           # Remove the provided vendor folder if it exists
205           rm -Rf vendor
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 ]
211           then
212               cat > composer.lock <<EOF
213           {
214               "packages": []
215           }
216           EOF
217           fi
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
224           cd vendor
225           ${bundleDependencies packages}
226           ${lib.optionalString (!noDev) (bundleDependencies devPackages)}
227           cd ..
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
241           ''}
243           ${lib.optionalString (!symlinkDependencies) ''
244             # Patch the shebangs if possible
245             if [ -d $(pwd)/vendor/bin ]
246             then
247                 # Look for all executables in bin/
248                 for i in $(pwd)/vendor/bin/*
249                 do
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"
257                     # Patch shebang
258                     sed -e "s|#!/usr/bin/php|#!${php}/bin/php|" \
259                         -e "s|#!/usr/bin/env php|#!${php}/bin/php|" \
260                         "$realFile" > tmp
261                     mv tmp "$realFile"
262                     chmod u+x "$realFile"
263                 done
264             fi
265           ''}
267           if [ "$removeComposerArtifacts" = "1" ]
268           then
269               # Remove composer stuff
270               rm -f composer.json composer.lock
271           fi
273           # Execute post install hook
274           runHook postInstall
275         '';
276       }
277       // extraArgs
278     );
281   inherit filterSrc;
282   composer = lib.makeOverridable composer;
283   buildZipPackage = lib.makeOverridable buildZipPackage;
284   buildPackage = lib.makeOverridable buildPackage;