aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/websites/tools/cloud.nix
blob: a7fcd6149a086aea15b91e122bca1012f315719d (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
184
185
186
187
188
189
{ lib, pkgs, config, myconfig, mylibs, ... }:
let
  nextcloud = pkgs.webapps.nextcloud.withApps (builtins.attrValues pkgs.webapps.nextcloud-apps);
  env = myconfig.env.tools.nextcloud;
  varDir = "/var/lib/nextcloud";
  webappName = "tools_nextcloud";
  apacheRoot = "/run/current-system/webapps/${webappName}";
  cfg = config.services.myWebsites.tools.cloud;
  phpFpm = rec {
    basedir = builtins.concatStringsSep ":" (
      [ nextcloud varDir ]
      ++ builtins.attrValues pkgs.webapps.nextcloud-apps);
    socket = "/var/run/phpfpm/nextcloud.sock";
    phpConfig = ''
      extension=${pkgs.phpPackages.redis}/lib/php/extensions/redis.so
      extension=${pkgs.phpPackages.apcu}/lib/php/extensions/apcu.so
      zend_extension=${pkgs.php}/lib/php/extensions/opcache.so
      '';
    pool = ''
      listen = ${socket}
      user = wwwrun
      group = wwwrun
      listen.owner = wwwrun
      listen.group = wwwrun
      pm = ondemand
      pm.max_children = 60
      pm.process_idle_timeout = 60

      php_admin_value[output_buffering] = 0
      php_admin_value[max_execution_time] = 1800
      php_admin_value[zend_extension] = "opcache"
      ;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:${basedir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp"
      php_admin_value[session.save_path] = "${varDir}/phpSessions"
      '';
  };
in {
  options.services.myWebsites.tools.cloud = {
    enable = lib.mkEnableOption "enable cloud website";
  };

  config = lib.mkIf cfg.enable {
    security.acme.certs."eldiron".extraDomains."cloud.immae.eu" = null;

    services.myWebsites.tools.modules = [ "proxy_fcgi" ];

    services.myWebsites.tools.vhostConfs.cloud = {
      certName    = "eldiron";
      hosts       = ["cloud.immae.eu" ];
      root        = apacheRoot;
      extraConfig = [
        ''
          SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
          <Directory ${apacheRoot}>
            AcceptPathInfo On
            DirectoryIndex index.php
            Options FollowSymlinks
            Require all granted
            AllowOverride all

            <IfModule mod_headers.c>
              Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload"
            </IfModule>
            <FilesMatch "\.php$">
              CGIPassAuth on
              SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
            </FilesMatch>

          </Directory>
        ''
      ];
    };

    mySecrets.keys = [{
      dest = "webapps/tools-nextcloud";
      user = "wwwrun";
      group = "wwwrun";
      permissions = "0600";
      text = ''
        <?php
        $CONFIG = array (
          // FIXME: change this value when nextcloud starts getting slow
          'instanceid' => '${env.instance_id}1',
          'datadirectory' => '/var/lib/nextcloud/',
          'passwordsalt' => '${env.password_salt}',
          'debug' => false,
          'dbtype' => 'pgsql',
          'version' => '16.0.0.9',
          'dbname' => '${env.postgresql.database}',
          'dbhost' => '${env.postgresql.socket}',
          'dbtableprefix' => 'oc_',
          'dbuser' => '${env.postgresql.user}',
          'dbpassword' => '${env.postgresql.password}',
          'installed' => true,
          'maxZipInputSize' => 0,
          'allowZipDownload' => true,
          'forcessl' => true,
          'theme' => ${"''"},
          'maintenance' => false,
          'trusted_domains' => 
          array (
            0 => 'cloud.immae.eu',
          ),
          'secret' => '${env.secret}',
          'appstoreenabled' => false,
          'appstore.experimental.enabled' => true,
          'loglevel' => 2,
          'trashbin_retention_obligation' => 'auto',
          'htaccess.RewriteBase' => '/',
          'mail_smtpmode' => 'sendmail',
          'mail_smtphost' => '127.0.0.1',
          'mail_smtpname' => ''',
          'mail_smtppassword' => ''',
          'mail_from_address' => 'nextcloud',
          'mail_smtpauth' => false,
          'mail_domain' => 'tools.immae.eu',
          'memcache.local' => '\\OC\\Memcache\\APCu',
          'memcache.locking' => '\\OC\\Memcache\\Redis',
          'filelocking.enabled' => true,
          'redis' => 
          array (
            'host' => '${env.redis.socket}',
            'port' => 0,
            'dbindex' => ${env.redis.db_index},
          ),
          'overwrite.cli.url' => 'https://cloud.immae.eu',
          'ldapIgnoreNamingRules' => false,
          'ldapProviderFactory' => '\\OCA\\User_LDAP\\LDAPProviderFactory',
          'has_rebuilt_cache' => true,
        );
      '';
    }];
    users.users.root.packages = let
      occ = pkgs.writeScriptBin "nextcloud-occ" ''
        #! ${pkgs.stdenv.shell}
        cd ${nextcloud}
        NEXTCLOUD_CONFIG_DIR="${nextcloud}/config" \
          exec \
          sudo -u wwwrun ${pkgs.php}/bin/php \
          -c ${pkgs.php}/etc/php.ini \
          occ $*
        '';
    in [ occ ];

    system.activationScripts.nextcloud = {
      deps = [ "secrets" ];
      text = let
        confs = lib.attrsets.mapAttrs (n: v: pkgs.writeText "${n}.json" (builtins.toJSON v)) nextcloud.otherConfig;
      in
        ''
        install -m 0755 -o wwwrun -g wwwrun -d ${varDir}
        install -m 0750 -o wwwrun -g wwwrun -d ${varDir}/phpSessions
        ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v:
          "install -D -m 0644 -o wwwrun -g wwwrun -T ${v} ${varDir}/config/${n}.json"
          ) confs)}
        install -D -m 0600 -o wwwrun -g wwwrun -T /var/secrets/webapps/tools-nextcloud ${varDir}/config/config.php
      '';
    };
    # FIXME: add a warning when config.php changes
    system.extraSystemBuilderCmds = ''
      mkdir -p $out/webapps
      ln -s ${nextcloud} $out/webapps/${webappName}
      '';

    services.myPhpfpm = {
      poolPhpConfigs.nextcloud = phpFpm.phpConfig;
      poolConfigs.nextcloud = phpFpm.pool;
    };

    services.cron = {
      enable = true;
      systemCronJobs = [
        ''
          LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
          */15 * * * * wwwrun ${pkgs.php}/bin/php -f ${nextcloud}/cron.php
        ''
      ];
    };
  };
}