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

with lib;
let
  cfg = config.services.meguca;
  postgres = config.services.postgresql;
in
{
  options.services.meguca = {
    enable = mkEnableOption "meguca";

    baseDir = mkOption {
      type = types.path;
      default = "/run/meguca";
      description = "Location where meguca stores it's database and links.";
    };

    password = mkOption {
      type = types.str;
      default = "meguca";
      description = "Password for the meguca database.";
    };

    passwordFile = mkOption {
      type = types.path;
      default = "/run/keys/meguca-password-file";
      description = "Password file for the meguca database.";
    };

    reverseProxy = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "Reverse proxy IP.";
    };

    sslCertificate = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "Path to the SSL certificate.";
    };

    listenAddress = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "Listen on a specific IP address and port.";
    };

    cacheSize = mkOption {
      type = types.nullOr types.int;
      default = null;
      description = "Cache size in MB.";
    };

    postgresArgs = mkOption {
      type = types.str;
      default = "user=meguca password=" + cfg.password + " dbname=meguca sslmode=disable";
      description = "Postgresql connection arguments.";
    };

    postgresArgsFile = mkOption {
      type = types.path;
      default = "/run/keys/meguca-postgres-args";
      description = "Postgresql connection arguments file.";
    };

    compressTraffic = mkOption {
      type = types.bool;
      default = false;
      description = "Compress all traffic with gzip.";
    };

    assumeReverseProxy = mkOption {
      type = types.bool;
      default = false;
      description = "Assume the server is behind a reverse proxy, when resolving client IPs.";
    };

    httpsOnly = mkOption {
      type = types.bool;
      default = false;
      description = "Serve and listen only through HTTPS.";
    };
  };

  config = mkIf cfg.enable {
    security.sudo.enable = cfg.enable == true;
    services.postgresql.enable = cfg.enable == true;

    services.meguca.passwordFile = mkDefault (toString (pkgs.writeTextFile {
      name = "meguca-password-file";
      text = cfg.password;
    }));

    services.meguca.postgresArgsFile = mkDefault (toString (pkgs.writeTextFile {
      name = "meguca-postgres-args";
      text = cfg.postgresArgs;
    }));

    systemd.services.meguca = {
      description = "meguca";
      after = [ "network.target" "postgresql.service" ];
      wantedBy = [ "multi-user.target" ];

      preStart = ''
        # Ensure folder exists and links are correct or create them
        mkdir -p ${cfg.baseDir}
        ln -sf ${pkgs.meguca}/share/meguca/www ${cfg.baseDir}

        # Ensure the database is correct or create it
        ${pkgs.sudo}/bin/sudo -u ${postgres.superUser} ${postgres.package}/bin/createuser \
          -SDR meguca || true
        ${pkgs.sudo}/bin/sudo -u ${postgres.superUser} ${postgres.package}/bin/psql \
          -c "ALTER ROLE meguca WITH PASSWORD '$(cat ${cfg.passwordFile})';" || true
        ${pkgs.sudo}/bin/sudo -u ${postgres.superUser} ${postgres.package}/bin/createdb \
          -T template0 -E UTF8 -O meguca meguca || true
      '';

    script = ''
      cd ${cfg.baseDir}

      ${pkgs.meguca}/bin/meguca -d "$(cat ${cfg.postgresArgsFile})"\
        ${optionalString (cfg.reverseProxy != null) " -R ${cfg.reverseProxy}"}\
        ${optionalString (cfg.sslCertificate != null) " -S ${cfg.sslCertificate}"}\
        ${optionalString (cfg.listenAddress != null) " -a ${cfg.listenAddress}"}\
        ${optionalString (cfg.cacheSize != null) " -c ${toString cfg.cacheSize}"}\
        ${optionalString (cfg.compressTraffic) " -g"}\
        ${optionalString (cfg.assumeReverseProxy) " -r"}\
        ${optionalString (cfg.httpsOnly) " -s"} start
    '';

      serviceConfig = {
        PermissionsStartOnly = true;
        Type = "forking";
        User = "meguca";
        Group = "meguca";
        RuntimeDirectory = "meguca";
        ExecStop = "${pkgs.meguca}/bin/meguca stop";
      };
    };

    users = {
      users.meguca = {
        description = "meguca server service user";
        home = cfg.baseDir;
        createHome = true;
        group = "meguca";
        uid = config.ids.uids.meguca;
      };

      groups.meguca = {
        gid = config.ids.gids.meguca;
        members = [ "meguca" ];
      };
    };
  };

  meta.maintainers = with maintainers; [ chiiruno ];
}