diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/crs-tools.nix | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/modules/crs-tools.nix b/modules/crs-tools.nix new file mode 100644 index 0000000..fb43136 --- /dev/null +++ b/modules/crs-tools.nix @@ -0,0 +1,132 @@ +{ config, lib, pkgs, modulesPath, ... }: + +with lib; + +let + cfg = config.services.crs-tracker; + configFile = pkgs.writeText "crs-tracker-config.php" '' + <?php + Log::setPath(ROOT . '../log/application.log'); + Log::setLevel(Log::INFO); + Log::colorize(true); + + Database_PostgreSQL::init( + '${cfg.dbHost}', + '${cfg.dbUsername}', + '${cfg.dbPassword}', + '${cfg.dbDatabase}' + ); + + // TODO: make timezone a user setting or get it from browser + Database::$Instance->query( + 'SET timezone = ' . Database::$Instance->quote(date_default_timezone_get()) + ); + + requires('Cache/Adapter/APC'); + Cache::setAdapter(new Cache_Adapter_APC()); + + session_set_cookie_params(0, '/', null, false, true); + + libxml_disable_entity_loader(true); + ?> + ''; + tracker = pkgs.stdenv.mkDerivation { + src = pkgs.crs-tools.tracker; + name = "tracker-with-config"; + buildPhase = '' + mkdir $out + cp -r $src/* $out + chmod +w $out/src/Config/ + ln -s ${configFile} $out/src/Config/Config.php + ''; + phases = [ "buildPhase" ]; + }; +in { + options.services.crs-tracker = { + enable = mkOption { + type = types.bool; + default = false; + description = "tracker fahren."; + }; + + dbHost = mkOption { + type = types.str; + default = "localhost"; + description = + "domain the database runs on. This must be a postgres < 12."; + }; + + dbUsername = mkOption { + type = types.str; + default = "crstracker"; + }; + + dbPassword = mkOption { + type = types.str; + default = ""; + }; + + dbDatabase = mkOption { + type = types.str; + default = "crstracker"; + }; + + nginxVirtualHostConfig = mkOption { + type = types.submodule (import "${modulesPath}/services/web-servers/nginx/vhost-options.nix" { + inherit config lib; + }); + default = {}; + }; + }; + + config = mkIf cfg.enable { + + services.phpfpm = { + pools.crs-tracker = { + phpPackage = pkgs.crs-tools.php; + user = "crs-tools"; + group = "crs-tools"; + settings = { + "listen.owner" = config.services.nginx.user; + "listen.group" = config.services.nginx.group; + "pm" = "dynamic"; + "pm.max_children" = "32"; + "pm.start_servers" = "2"; + "pm.min_spare_servers" = "2"; + "pm.max_spare_servers" = "4"; + "pm.max_requests" = "500"; + "access.log" = "/var/log/$pool.access.log"; + }; + }; + }; + users.users.crs-tools = { + group = "crs-tools"; + isSystemUser = true; + home = tracker.outPath; + }; + users.groups.crs-tools.members = [ "crs-tools" ]; + + services.nginx = { + enable = mkDefault true; + + virtualHosts."crs-tools" = cfg.nginxVirtualHostConfig // { + root = "${tracker}/src/Public"; + locations."~ ^/(downloads|css|images|thumbnails|javascript|fahrplan)/".priority = 900; + locations."/" = { + extraConfig = '' + # include ${config.services.nginx.package}/conf/fastcgi.conf; + include ${config.services.nginx.package}/conf/fastcgi_params; + fastcgi_split_path_info ^(.+?)(\\/.*)$; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param SCRIPT_FILENAME $document_root/index.php; + fastcgi_index index.php; + fastcgi_pass unix:${config.services.phpfpm.pools.crs-tracker.socket}; + # remove trailing slash + rewrite ^(.+)/$ $1 permanent; + rewrite ^(.*)$ /index.php$1 break; + ''; + }; + }; + }; + }; +} |