From 0b9522026acbad22ab52a18d1fba734869af7a18 Mon Sep 17 00:00:00 2001 From: Lucian Pricop Date: Fri, 24 Apr 2020 15:25:01 +0300 Subject: [PATCH] added basic NIX scripts --- .gitignore | 5 + composer-env.nix | 238 +++++++++++++++++++++++++++++++++++++++++ composer.json | 66 ++++++++++++ sandd-php-packages.nix | 17 +++ sandd.nix | 29 +++++ 5 files changed, 355 insertions(+) create mode 100644 .gitignore create mode 100644 composer-env.nix create mode 100644 composer.json create mode 100644 sandd-php-packages.nix create mode 100644 sandd.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b38118a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# IntelliJ project files +.idea +*.iml +out +gen diff --git a/composer-env.nix b/composer-env.nix new file mode 100644 index 0000000..279b307 --- /dev/null +++ b/composer-env.nix @@ -0,0 +1,238 @@ +# This file originates from composer2nix + +{ stdenv, writeTextFile, fetchurl, php, unzip, phpPackages }: + +let + inherit (phpPackages) composer; + 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 + + ''; + }; + + constructBin = writeTextFile { + name = "constructbin.php"; + executable = true; + text = '' + #! ${php}/bin/php + + ''; + }; + + 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 < 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; +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..090fde5 --- /dev/null +++ b/composer.json @@ -0,0 +1,66 @@ +{ + "name": "laravel/laravel", + "description": "The Laravel Framework.", + "keywords": ["framework", "laravel"], + "license": "MIT", + "type": "project", + "require": { + "php": ">=7.1.3", + "doctrine/dbal": "^2.9", + "enqueue/amqp-bunny": "^0.10.0", + "enqueue/simple-client": "^0.10.0", + "fideloper/proxy": "~4.0", + "intervention/image": "^2.5", + "jasig/phpcas": "1.3.4", + "laravel/framework": "^5.7.7", + "laravel/tinker": "~1.0", + "maatwebsite/excel": "^3.1", + "subfission/cas": "2.0.9", + "symfony/process": "^4.4.1" + }, + "require-dev": { + "beyondcode/laravel-er-diagram-generator": "^1.4", + "filp/whoops": "~2.0", + "fzaninotto/faker": "~1.4", + "mockery/mockery": "~1.0", + "phpunit/phpunit": "~7.0" + }, + "autoload": { + "classmap": [ + "database/seeds", + "database/factories" + ], + "psr-4": { + "App\\": "app/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "dont-discover": [ + "TelescopeServiceProvider" + ] + } + }, + "scripts": { + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate" + ], + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover" + ] + }, + "config": { + "preferred-install": "dist", + "sort-packages": true, + "optimize-autoloader": true + } +} diff --git a/sandd-php-packages.nix b/sandd-php-packages.nix new file mode 100644 index 0000000..710e852 --- /dev/null +++ b/sandd-php-packages.nix @@ -0,0 +1,17 @@ +{composerEnv, fetchurl, fetchgit ? null, fetchhg ? null, fetchsvn ? null, noDev ? false, name ? "default"}: + +let + packages = { + # Add here any composer loaded dependency or use composer2nix to generate the list + + }; + devPackages = {}; +in +composerEnv.buildPackage { + inherit packages devPackages noDev; + inherit name; + src = ./.; + executable = false; + symlinkDependencies = false; + meta = {}; +} diff --git a/sandd.nix b/sandd.nix new file mode 100644 index 0000000..5119047 --- /dev/null +++ b/sandd.nix @@ -0,0 +1,29 @@ +{ pkgs ? import { + inherit system; + }, system ? builtins.currentSystem +}: +let + name = "search-and-diplace"; + inherit (pkgs) stdenv; + composerEnv = import ./composer-env.nix { + inherit (pkgs) stdenv writeTextFile fetchurl php unzip phpPackages; + }; + phpPackage = import ./sandd-php-packages.nix { + inherit composerEnv; + inherit (pkgs) fetchurl fetchgit fetchhg fetchsvn; + noDev = true; # Disable development dependencies + inherit name; + }; +in +with import (builtins.fetchurl https://github.com/searchanddisplace/searchanddisplace/raw/master/composer.json) {}; +phpPackage.override { + #buildCommand = '' + # curl -o composer.json https://github.com/searchanddisplace/searchanddisplace/raw/master/composer.json + #''; + removeComposerArtifacts = true; # Remove composer configuration files + buildInputs = [ pkgs.git ]; # Add here any environment dependency + # Add below all lines of post installation instructions e.g ''npm install'' ? + postInstall = '' + + ''; +} \ No newline at end of file