aboutsummaryrefslogtreecommitdiff
path: root/modules/private/websites/bakeer/cloud.nix
blob: 001ca0355f241cb4d106751e096fb2dbce0645b2 (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
{ lib, pkgs, config,  ... }:
let
  cfg = config.myServices.websites.bakeer.cloud;
  varDir = "/var/lib/nextcloud_farm/bakeer";
  apacheUser = config.services.httpd.Prod.user;
  apacheGroup = config.services.httpd.Prod.group;
  nextcloud = (pkgs.webapps.nextcloud.override { varDir = null; }).withApps (a: [
    a.apporder a.audioplayer a.bookmarks a.calendar a.carnet a.contacts
    a.cookbook a.deck a.extract a.files_markdown a.files_readmemd
    a.flowupload a.gpxedit a.gpxpod a.impersonate a.keeweb a.maps
    a.metadata a.music a.notes a.ocsms a.passman a.polls a.spreed
    a.tasks
  ]);
  phpBaseDir = builtins.concatStringsSep ":" ([ nextcloud varDir ] ++ nextcloud.apps);
in {
  options.myServices.websites.bakeer.cloud.enable = lib.mkEnableOption "enable Bakeer’s cloud";

  config = lib.mkIf cfg.enable {
    system.activationScripts.bakeer_cloud = {
      deps = [ "httpd" ];
      text = ''
        install -m 0755 -o ${apacheUser} -g ${apacheGroup} -d ${varDir}
        install -m 0755 -o ${apacheUser} -g ${apacheGroup} -d /var/lib/nextcloud_farm/phpSessions
        '';
    };
    systemd.services.phpfpm-nextcloud_farm.after = lib.mkAfter [ "postgresql.service" ];
    systemd.services.phpfpm-nextcloud_farm.wants = [ "postgresql.service" ];
    services.phpfpm.pools.nextcloud_farm = {
      user = apacheUser;
      group = apacheGroup;
      settings = {
        "listen.owner" = apacheUser;
        "listen.group" = apacheGroup;
        "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:${phpBaseDir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp";
        "php_admin_value[session.save_path]" = "/var/lib/nextcloud_farm/phpSessions";
      };
      phpPackage = pkgs.php74.withExtensions({ enabled, all }: enabled ++ [ all.redis all.apcu all.opcache ]);
    };
    users.users.root.packages = let
      occ = pkgs.writeScriptBin "nextcloud-occ-bakeer" ''
        #! ${pkgs.stdenv.shell}
        cd ${nextcloud}
        NEXTCLOUD_CONFIG_DIR="${varDir}" \
          exec \
          sudo -E -u wwwrun ${pkgs.php74}/bin/php \
          -c ${pkgs.php74}/etc/php.ini \
          occ $*
        '';
    in [ occ ];
    services.websites.env.production.modules = [ "proxy_fcgi" ];
    services.websites.env.production.vhostConfs.bakeer = {
      certName     = "bakeer";
      certMainHost = "bakeer.immae.eu";
      addToCerts   = true;
      hosts        = ["bakeer.immae.eu" "baxsolution.immae.eu"];
      root         = nextcloud;
      extraConfig  = [
        ''
          SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
          SetEnv NEXTCLOUD_CONFIG_DIR "${varDir}"
          <Directory ${nextcloud}>
            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:${config.services.phpfpm.pools.nextcloud_farm.socket}|fcgi://localhost"
            </FilesMatch>

          </Directory>
          ''
      ];
    };
    services.cron = {
      enable = true;
      systemCronJobs = let
        script = pkgs.writeScriptBin "nextcloud-cron" ''
          #! ${pkgs.stdenv.shell}
          export LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
          export PATH=/run/wrappers/bin:$PATH
          export NEXTCLOUD_CONFIG_DIR="${varDir}"
          ${pkgs.php74}/bin/php -d memory_limit=512M -f ${nextcloud}/cron.php
          '';
      in [
        ''
          */15 * * * * wwwrun ${script}/bin/nextcloud-cron
        ''
      ];
    };
  };
}