summaryrefslogtreecommitdiff
path: root/modules/crs-tools.nix
blob: 3ca9ce7f39be12cb94917b905bc27d8e265c950f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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-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-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;
          '';
        };
      };
    };
  };
}