cleverca22
10/24/2016 - 6:50 PM

default.nix

{ stdenv, aedb, writeTextFile, cfg }:

let
  config = writeTextFile {
    name = "config.php";
    text = ''
      <?php
      $master_server = "${cfg.master}";
      $this_server = "${cfg.hostname}";
      $base = 'nixos';
    '';
  };
in
stdenv.mkDerivation {
  name = "public_html";
  
  phases = [ "installPhase" ];

  installPhase = ''
    mkdir -pv $out/dsis/
    cp -vi ${aedb}/root/index.php $out/
    ln -sv ${aedb}/web/css $out/dsis/
    ln -sv ${aedb}/web/java $out/dsis/
    ln -sv ${aedb}/web/js $out/dsis/
    ln -sv ${aedb}/web/sounds $out/dsis/
    ln -sv ${aedb}/web/help $out/dsis/
    ln -sv ${aedb}/web/images $out/dsis/
    substituteInPlace $out/index.php --replace @AEDB@ ${aedb} --replace @CONFIGFILE@ ${config};
  '';
}
{ lib, config, pkgs, ... }:

with lib;

let
  aedb = pkgs.callPackage ./aedb.nix {};
  public_html = pkgs.callPackage ./public_html.nix { aedb = aedb; cfg = config.services.AEDB; };
in
{
  config = {
    services = {
      lighttpd = {
        enable = true;
        document-root = public_html;
        enableModules = [ "mod_fastcgi" "mod_redirect" "mod_rewrite" ];
        extraConfig =
''
#debug.log-request-handling = "enable"
$HTTP["host"] =~ "^nixos.example.com$" {
  server.document-root = "${public_html}"
  url.rewrite-once += (
    "/.*\?(.*)$" => "/index.php?$1",
    "/.*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
    "/" => "/index.php"
  )
  fastcgi.server = ( ".php" =>
    ( "localhost" =>
      (
        "socket" => "/run/phpfpm/AEDB"
      )
    )
  )
}
'';
      };
    };
  };
}