ctucx.git: tinyDAV

[php] Cal-/ CardDAV server with a simple web-GUI based on SabreDAV

commit 431e98cb42d5918190cdd16b3162c13d29aba1ce
parent ae4f03c1bdebdf94099ae01a4b81a2c275f539c4
Author: ctucx <c@ctu.cx>
Date: Thu, 23 Apr 2020 21:26:34 +0200

added nixfiles
4 files changed, 348 insertions(+), 0 deletions(-)
A
composer-env.nix
|
238
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
default.nix
|
14
++++++++++++++
A
php-packages.nix
|
95
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M
public/composer.json
|
1
+
diff --git a/composer-env.nix 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
+          <?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;
+}
diff --git a/default.nix b/default.nix
@@ -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 phpPackages;
+  };
+in
+import ./php-packages.nix {
+  inherit composerEnv noDev;
+  inherit (pkgs) fetchurl fetchgit fetchhg fetchsvn;
+}+
\ No newline at end of file
diff --git a/php-packages.nix b/php-packages.nix
@@ -0,0 +1,95 @@
+{composerEnv, fetchurl, fetchgit ? null, fetchhg ? null, fetchsvn ? null, noDev ? false}:
+
+let
+  packages = {
+    "psr/log" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "psr-log-446d54b4cb6bf489fc9d75f55843658e6f25d801";
+        src = fetchurl {
+          url = https://api.github.com/repos/php-fig/log/zipball/446d54b4cb6bf489fc9d75f55843658e6f25d801;
+          sha256 = "04baykaig5nmxsrwmzmcwbs60ixilcx1n0r9wdcnvxnnj64cf2kr";
+        };
+      };
+    };
+    "ralouphie/mimey" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "ralouphie-mimey-8f74e6da73f9df7bd965e4e123f3d8fb9acb89ba";
+        src = fetchurl {
+          url = https://api.github.com/repos/ralouphie/mimey/zipball/8f74e6da73f9df7bd965e4e123f3d8fb9acb89ba;
+          sha256 = "1jhbi5f9s0vm20b3law7r5iidszc94vy4brwnq823xh1ps14f2ib";
+        };
+      };
+    };
+    "sabre/dav" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-dav-fd0234d46c045fc9b35ec06bd2e7b490240e6ade";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/dav/zipball/fd0234d46c045fc9b35ec06bd2e7b490240e6ade;
+          sha256 = "1rrww0hx005bvzyn3jf4hcbcnczb4ichyvy5w373d390vrb3dvs4";
+        };
+      };
+    };
+    "sabre/event" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-event-f5cf802d240df1257866d8813282b98aee3bc548";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/event/zipball/f5cf802d240df1257866d8813282b98aee3bc548;
+          sha256 = "1003imr8dl8cdpybkg0r959hl0gipfrnidhp7r60bqfyriwczc9a";
+        };
+      };
+    };
+    "sabre/http" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-http-f91c7d4437dcbc6f89c8b64e855e1544f4b60250";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/http/zipball/f91c7d4437dcbc6f89c8b64e855e1544f4b60250;
+          sha256 = "1x883wairlj03ja8xn8jvgkhdh2q6fgbdym6f4r94amm9gvkf51h";
+        };
+      };
+    };
+    "sabre/uri" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-uri-18f454324f371cbcabdad3d0d3755b4b0182095d";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/uri/zipball/18f454324f371cbcabdad3d0d3755b4b0182095d;
+          sha256 = "163lb35kycq9q4d6djp6avvjphbjys7h0s9sr4wij10qfnh6is5l";
+        };
+      };
+    };
+    "sabre/vobject" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-vobject-6d7476fbd227ae285029c19ad518cd451336038c";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/vobject/zipball/6d7476fbd227ae285029c19ad518cd451336038c;
+          sha256 = "1nzlbmwaijmr151g8qk7sxyhbzndnmglhsl9gnmk8klh6pzv1n0z";
+        };
+      };
+    };
+    "sabre/xml" = {
+      targetDir = "";
+      src = composerEnv.buildZipPackage {
+        name = "sabre-xml-f08a58f57e2b0d7df769a432756aa371417ab9eb";
+        src = fetchurl {
+          url = https://api.github.com/repos/sabre-io/xml/zipball/f08a58f57e2b0d7df769a432756aa371417ab9eb;
+          sha256 = "10vsva7z39knkv44b2nqzndkc2r654f8gs0x7bxldirmgda3b830";
+        };
+      };
+    };
+  };
+  devPackages = {};
+in
+composerEnv.buildPackage {
+  inherit packages devPackages noDev;
+  name = "ctucx-tinyDAV";
+  src = ./public/.;
+  executable = false;
+  symlinkDependencies = false;
+  meta = {};
+}
diff --git a/public/composer.json b/public/composer.json
@@ -1,4 +1,5 @@
 {
+    "name": "ctucx/tinyDAV",
     "require": {
         "php"             : ">=7.0",
         "sabre/dav"       : "~4.0",