about summary refs log tree commit diff
path: root/wikiwiki.nix
blob: a5cb5b56088a9a46c7c169717e16ac2e7e35544c (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
# MediaWiki server for Wikipedia research
# Copyright (C) 2023  Nguyễn Gia Phong
#
# This file is part of loang configuration.
#
# Loang configuration is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Loang configuration is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with loang configuration.  If not, see <https://www.gnu.org/licenses/>.

{ config, lib, pkgs, ... }:
let hostname = "nhanh.${config.networking.domain}";
in {
  services = {
    mediawiki = {
      database.type = "postgres";
      enable = true;
      extensions = {
        # null means enabled here.
        Cite = null;
        Interwiki = null;
      };
      extraConfig = ''
        $wgEnableEmail = false;
        $wgGroupPermissions['*']['read'] = false;
        $wgGroupPermissions['*']['edit'] = false;
        $wgGroupPermissions['*']['createaccount'] = false;
        $wgLogos = [
          'svg' => "https://upload.wikimedia.org/wikipedia/commons/e/e1/Wiki_research_logo.svg",
        ];
        $wgFileExtensions = array_merge($wgFileExtensions, [ 'pdf' ]);
      '';
      name = "Research on Wikipedia governance";
      nginx.hostName = hostname;
      passwordFile = "/dev/null"; # this is so dumb
      webserver = "nginx";
    };

    nginx.virtualHosts.${hostname} = {
      enableACME = true;
      forceSSL = true;
    };

    phpfpm.pools.mediawiki.phpOptions = ''
      post_max_size = 100M
      upload_max_filesize = 100M
    '';

    postgresqlBackup.databases = [ "mediawiki" ];
  };

  systemd = let
    service = user: script: {
      inherit script;
      serviceConfig  = {
        Type = "oneshot";
        User = user;
      };
    };
    timer = unit: {
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnBootSec = "1d";
        OnUnitActiveSec = "1d";
        Unit = "${unit}.service";
      };
    };
  in {
    services = {
      "wiki-db-backup" = service "postgres" ''
        today=$(${pkgs.coreutils}/bin/date --iso-8601)
        backup=/mnt/nas/backup/nhanh.loang.net/$today.sql.zstd
        ${pkgs.coreutils}/bin/install -g mediawiki -m 640\
          /mnt/nas/backup/postgresql/mediawiki.sql.zstd $backup
      '';
      "wiki-uploads-backup" = service "mediawiki" ''
        backup=/mnt/nas/backup/nhanh.loang.net
        ${pkgs.rsync}/bin/rsync -a --delete\
          ${config.services.mediawiki.uploadsDir}/ $backup/latest/
        ${pkgs.coreutils}/bin/chown -R mediawiki:mediawiki $backup/latest/
        today=$(${pkgs.coreutils}/bin/date --iso-8601)
        ${pkgs.coreutils}/bin/cp -al $backup/{latest,$today}
      '';
    };
    timers = {
      "wiki-db-backup" = timer "wiki-db-backup";
      "wiki-uploads-backup" = timer "wiki-uploads-backup";
    };
  };

  users.groups.mediawiki.members = [
    "mediawiki" "nginx" "postgres"
    "ooze" "cnx"
  ];
}