aboutsummaryrefslogtreecommitdiff
path: root/systems/eldiron/websites/kanboard/farm.nix
blob: a70d0d6f60570b3bd061e38825ebae2f76a7e99b (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ lib, pkgs, config, ... }:
let
  cfg = config.myServices.tools.kanboard.farm;
  apacheUser = config.services.websites.env.tools.user;
  apacheGroup = config.services.websites.env.tools.group;
  toVardir = name: "/var/lib/kanboard_farm/${name}";
  varDirs = lib.mapAttrsToList (name: v: toVardir name) cfg.instances;
  toPhpBaseDir = name: [ rootDir (toVardir name) ];
  phpBaseDir = builtins.concatStringsSep ":" (lib.unique (lib.flatten (lib.mapAttrsToList (name: v: toPhpBaseDir name) cfg.instances)));
  rootDir = pkgs.kanboard;

  toVhost = name: ''
    Alias /${name} "${rootDir}"
    <Location /${name}>
      SetEnv DATA_DIR "${toVardir name}"
      SetEnv MAIL_FROM "kanboard@tools.immae.eu"
    </Location>
    '';
  toCustomVhost = name: lib.optionalAttrs (cfg.instances."${name}".customHost != null) {
    "kanboard_farm_${name}" = {
      certName = "eldiron";
      hosts = [cfg.instances."${name}".customHost];
      root = null;
      extraConfig = [
        ''
        Alias / "${rootDir}"
        <Location />
          SetEnv DATA_DIR "${toVardir name}"
          SetEnv MAIL_FROM "kanboard@tools.immae.eu"
        </Location>
        <Directory "${rootDir}">
          DirectoryIndex index.php
          AllowOverride All
          Options FollowSymlinks
          Require all granted

          <FilesMatch "\.php$">
            SetHandler "proxy:unix:${config.services.phpfpm.pools.kanboard_farm.socket}|fcgi://localhost"
          </FilesMatch>
        </Directory>
        <DirectoryMatch "${rootDir}/data">
          Require all denied
        </DirectoryMatch>
          ''
      ];
    };
  };
  customHosts = lib.filter (n: n != null) (map (n: cfg.instances."${n}".customHost) (builtins.attrNames cfg.instances));
  customVhosts = lib.foldl (o: n: o // n) {} (map toCustomVhost (builtins.attrNames cfg.instances));
  phpPackage = pkgs.php74.withExtensions({ enabled, all }: enabled ++ [all.redis]);
in
{
  options.myServices.tools.kanboard.farm = {
    instances = lib.mkOption {
      description = "Instances names for the kanboard Farm";
      default = {};
      type = lib.types.attrsOf (lib.types.submodule {
        options = {
          customHost = lib.mkOption {
            description = "Custom host to use for the kanboard instance";
            default = null;
            type = lib.types.nullOr lib.types.str;
          };
        };
      });
    };
    vhosts = lib.mkOption {
      description = "Instance vhosts configs";
      readOnly = true;
      type = lib.types.attrsOf lib.types.str;
      default = lib.mapAttrs (name: v: toVhost name) cfg.instances;
    };
  };

  config = lib.mkIf (builtins.length (builtins.attrNames cfg.instances) > 0) {
    myServices.dns.zones."immae.eu".subdomains.kanboard =
      with config.myServices.dns.helpers; ips servers.eldiron.ips.main;

    myServices.chatonsProperties.hostings.kanboard = {
      file.datetime = "2022-08-21T19:40:00";
      hosting = {
        name = "Kanboard";
        description = "Kanban project management software";
        website = "https://tools.immae.eu/kanboard";
        logo = "https://tools.immae.eu/kanboard/assets/img/favicon.png";
        type = "INSTANCE";
        status.level = "OK";
        status.description = "OK";
        registration.load = "OPEN";
        install.type = "PACKAGE";
      };
    };
    myServices.chatonsProperties.services.kanboard = {
      file.datetime = "2022-08-21T19:40:00";
      service = {
        name = "Kanboard";
        description = "Kanban project management software";
        website = "https://tools.immae.eu/kanboard";
        logo = "https://tools.immae.eu/kanboard/assets/img/favicon.png";
        status.level = "OK";
        status.description = "OK";
        registration."" = ["MEMBER" "CLIENT"];
        registration.load = "OPEN";
        install.type = "PACKAGE";
      };
      software = {
        name = "Kanboard";
        website = "https://kanboard.org/";
        license.url = "https://github.com/kanboard/kanboard/blob/main/LICENSE";
        license.name = "MIT License";
        version = pkgs.kanboard.version;
        source.url = "https://github.com/kanboard/kanboard";
      };
    };
    system.activationScripts.kanboard_farm_vardirs = {
      deps = [ "httpd" ];
      text = ''
        install -m 0755 -o ${apacheUser} -g ${apacheGroup} -d ${builtins.concatStringsSep " " varDirs}
        '';
    };
    services.phpfpm.pools.kanboard_farm = {
      user = apacheUser;
      group = apacheGroup;
      settings = let
        instanceNb = builtins.length (builtins.attrNames cfg.instances);
      in {
        "listen.owner" = apacheUser;
        "listen.group" = apacheGroup;
        "pm" = "dynamic";
        "pm.max_children" = builtins.toString (60 * instanceNb);
        "pm.start_servers" = builtins.toString (2 * instanceNb);
        "pm.min_spare_servers" = builtins.toString (2 * instanceNb);
        "pm.max_spare_servers" = builtins.toString (3 * instanceNb);
        "pm.process_idle_timeout" = "60";

        "php_admin_value[output_buffering]" = "0";
        "php_admin_value[max_execution_time]" = "1800";
        "php_admin_value[zend_extension]" = "opcache";
        "php_value[apcu.enable_cli]" = "1";
        "php_value[apcu.enabled]" = "1";
        #already enabled by default?
        #"php_value[opcache.enable]" = "1";
        "php_value[opcache.enable_cli]" = "1";
        "php_value[opcache.interned_strings_buffer]" = "8";
        "php_value[opcache.max_accelerated_files]" = "10000";
        "php_value[opcache.memory_consumption]" = "128";
        "php_value[opcache.save_comments]" = "1";
        "php_value[opcache.revalidate_freq]" = "1";
        "php_admin_value[memory_limit]" = "512M";

        "php_admin_value[open_basedir]" = "/run/wrappers/bin/sendmail:${phpBaseDir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp";
        "php_admin_value[session.save_handler]" = "redis";
        "php_admin_value[session.save_path]" = "'unix:///run/redis-php-sessions/redis.sock?persistent=1&prefix=Tools:KanboardFarm:'";
      };
      inherit phpPackage;
    };
    security.acme.certs.eldiron.extraDomainNames = customHosts ++ [ "kanboard.immae.eu" ];
    services.websites.env.tools.vhostConfs = {
      kanboard = {
        certName = "eldiron";
        hosts = ["kanboard.immae.eu"];
        root = null;
        extraConfig = [
          ''
          <Directory "${rootDir}">
            DirectoryIndex index.php
            AllowOverride All
            Options FollowSymlinks
            Require all granted

            <FilesMatch "\.php$">
              SetHandler "proxy:unix:${config.services.phpfpm.pools.kanboard_farm.socket}|fcgi://localhost"
            </FilesMatch>
          </Directory>
          <DirectoryMatch "${rootDir}/data">
            Require all denied
          </DirectoryMatch>
            ''
        ] ++ builtins.attrValues cfg.vhosts;
      };
    } // customVhosts;
  };
}