aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/services/web-servers/meguca.nix
blob: 5a00070dc941644e1c2d227db400bec1b94d9092 (plain)
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
{ config, lib, pkgs, ... }:

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

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/meguca";
      example = "/home/okina/meguca";
      description = "Location where meguca stores it's database and links.";
    };

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

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

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

    sslCertificate = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "/home/okina/meguca/ssl.cert";
      description = "Path to the SSL certificate.";
    };

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

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

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

    postgresArgsFile = mkOption {
      type = types.path;
      default = "/run/keys/meguca-postgres-args";
      example = "/home/okina/meguca/keys/postgres";
      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.";
    };

    videoPaths = mkOption {
      type = types.listOf types.path;
      default = [];
      example = [ "/home/okina/Videos/tehe_pero.webm" ];
      description = "Videos that will be symlinked into www/videos.";
    };
  };

  config = mkIf cfg.enable {
    security.sudo.enable = cfg.enable;
    services.postgresql.enable = cfg.enable;
    services.postgresql.package = pkgs.postgresql_11;
    services.meguca.passwordFile = mkDefault (pkgs.writeText "meguca-password-file" cfg.password);
    services.meguca.postgresArgsFile = mkDefault (pkgs.writeText "meguca-postgres-args" cfg.postgresArgs);
    services.meguca.postgresArgs = mkDefault "user=meguca password=${cfg.password} dbname=meguca sslmode=disable";

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

      preStart = ''
        # Ensure folder exists or create it and links and permissions are correct
        mkdir -p ${escapeShellArg cfg.dataDir}/www
        rm -rf ${escapeShellArg cfg.dataDir}/www/videos
        ln -sf ${pkgs.meguca}/share/meguca/www/* ${escapeShellArg cfg.dataDir}/www
        unlink ${escapeShellArg cfg.dataDir}/www/videos
        mkdir -p ${escapeShellArg cfg.dataDir}/www/videos

        for vid in ${escapeShellArg cfg.videoPaths}; do
          ln -sf $vid ${escapeShellArg cfg.dataDir}/www/videos
        done

        chmod 750 ${escapeShellArg cfg.dataDir}
        chown -R meguca:meguca ${escapeShellArg cfg.dataDir}

        # 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/createdb \
          -T template0 -E UTF8 -O meguca meguca || true
        ${pkgs.sudo}/bin/sudo -u meguca ${postgres.package}/bin/psql \
          -c "ALTER ROLE meguca WITH PASSWORD '$(cat ${escapeShellArg cfg.passwordFile})';" || true
      '';

    script = ''
      cd ${escapeShellArg cfg.dataDir}

      ${pkgs.meguca}/bin/meguca -d "$(cat ${escapeShellArg 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";
        ExecStop = "${pkgs.meguca}/bin/meguca stop";
      };
    };

    users = {
      groups.meguca.gid = config.ids.gids.meguca;

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

  imports = [
    (mkRenamedOptionModule [ "services" "meguca" "baseDir" ] [ "services" "meguca" "dataDir" ])
  ];

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