summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/jirafeau.nix
blob: 328c61c8e6462bf068e84ba1b358da326db320c3 (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
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.jirafeau;

  group = config.services.nginx.group;
  user = config.services.nginx.user;

  withTrailingSlash = str: if hasSuffix "/" str then str else "${str}/";

  localConfig = pkgs.writeText "config.local.php" ''
    <?php
      $cfg['admin_password'] = '${cfg.adminPasswordSha256}';
      $cfg['web_root'] = 'http://${withTrailingSlash cfg.hostName}';
      $cfg['var_root'] = '${withTrailingSlash cfg.dataDir}';
      $cfg['maximal_upload_size'] = ${builtins.toString cfg.maxUploadSizeMegabytes};
      $cfg['installation_done'] = true;

      ${cfg.extraConfig}
  '';
in
{
  options.services.jirafeau = {
    adminPasswordSha256 = mkOption {
      type = types.str;
      default = "";
      description = ''
        SHA-256 of the desired administration password. Leave blank/unset for no password.
      '';
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/jirafeau/data/";
      description = "Location of Jirafeau storage directory.";
    };

    enable = mkEnableOption "Jirafeau file upload application.";

    extraConfig = mkOption {
      type = types.lines;
      default = "";
      example = ''
        $cfg['style'] = 'courgette';
        $cfg['organisation'] = 'ACME';
      '';
      description = let
        documentationLink =
          "https://gitlab.com/mojo42/Jirafeau/-/blob/${cfg.package.version}/lib/config.original.php";
      in
        ''
          Jirefeau configuration. Refer to <link xlink:href="${documentationLink}"/> for supported
          values.
        '';
    };

    hostName = mkOption {
      type = types.str;
      default = "localhost";
      description = "URL of instance. Must have trailing slash.";
    };

    maxUploadSizeMegabytes = mkOption {
      type = types.int;
      default = 0;
      description = "Maximum upload size of accepted files.";
    };

    maxUploadTimeout = mkOption {
      type = types.str;
      default = "30m";
      description = let
        nginxCoreDocumentation = "http://nginx.org/en/docs/http/ngx_http_core_module.html";
      in
        ''
          Timeout for reading client request bodies and headers. Refer to
          <link xlink:href="${nginxCoreDocumentation}#client_body_timeout"/> and
          <link xlink:href="${nginxCoreDocumentation}#client_header_timeout"/> for accepted values.
        '';
    };

    nginxConfig = mkOption {
      type = types.submodule
        (import ../web-servers/nginx/vhost-options.nix { inherit config lib; });
      default = {};
      example = literalExpression ''
        {
          serverAliases = [ "wiki.''${config.networking.domain}" ];
        }
      '';
      description = "Extra configuration for the nginx virtual host of Jirafeau.";
    };

    package = mkOption {
      type = types.package;
      default = pkgs.jirafeau;
      defaultText = literalExpression "pkgs.jirafeau";
      description = "Jirafeau package to use";
    };

    poolConfig = mkOption {
      type = with types; attrsOf (oneOf [ str int bool ]);
      default = {
        "pm" = "dynamic";
        "pm.max_children" = 32;
        "pm.start_servers" = 2;
        "pm.min_spare_servers" = 2;
        "pm.max_spare_servers" = 4;
        "pm.max_requests" = 500;
      };
      description = ''
        Options for Jirafeau PHP pool. See documentation on <literal>php-fpm.conf</literal> for
        details on configuration directives.
      '';
    };
  };


  config = mkIf cfg.enable {
    services = {
      nginx = {
        enable = true;
        virtualHosts."${cfg.hostName}" = mkMerge [
          cfg.nginxConfig
          {
            extraConfig = let
              clientMaxBodySize =
                if cfg.maxUploadSizeMegabytes == 0 then "0" else "${cfg.maxUploadSizeMegabytes}m";
            in
              ''
                index index.php;
                client_max_body_size ${clientMaxBodySize};
                client_body_timeout ${cfg.maxUploadTimeout};
                client_header_timeout ${cfg.maxUploadTimeout};
              '';
            locations = {
              "~ \\.php$".extraConfig = ''
                include ${config.services.nginx.package}/conf/fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index index.php;
                fastcgi_pass unix:${config.services.phpfpm.pools.jirafeau.socket};
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              '';
            };
            root = mkForce "${cfg.package}";
          }
        ];
      };

      phpfpm.pools.jirafeau = {
        inherit group user;
        phpEnv."JIRAFEAU_CONFIG" = "${localConfig}";
        settings = {
          "listen.mode" = "0660";
          "listen.owner" = user;
          "listen.group" = group;
        } // cfg.poolConfig;
      };
    };

    systemd.tmpfiles.rules = [
      "d ${cfg.dataDir} 0750 ${user} ${group} - -"
      "d ${cfg.dataDir}/files/ 0750 ${user} ${group} - -"
      "d ${cfg.dataDir}/links/ 0750 ${user} ${group} - -"
      "d ${cfg.dataDir}/async/ 0750 ${user} ${group} - -"
    ];
  };

  # uses attributes of the linked package
  meta.buildDocsInSandbox = false;
}