summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/selfoss.nix
blob: 899976ac696c8539c3b56b622525f20da180bcc7 (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
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.selfoss;

  poolName = "selfoss_pool";

  dataDir = "/var/lib/selfoss";

  selfoss-config =
  let
    db_type = cfg.database.type;
    default_port = if (db_type == "mysql") then 3306 else 5342;
  in
  pkgs.writeText "selfoss-config.ini" ''
    [globals]
    ${lib.optionalString (db_type != "sqlite") ''
      db_type=${db_type}
      db_host=${cfg.database.host}
      db_database=${cfg.database.name}
      db_username=${cfg.database.user}
      db_password=${cfg.database.password}
      db_port=${toString (if (cfg.database.port != null) then cfg.database.port
                    else default_port)}
    ''
    }
    ${cfg.extraConfig}
  '';
in
  {
    options = {
      services.selfoss = {
        enable = mkEnableOption "selfoss";

        user = mkOption {
          type = types.str;
          default = "nginx";
          description = ''
            User account under which both the service and the web-application run.
          '';
        };

        pool = mkOption {
          type = types.str;
          default = "${poolName}";
          description = ''
            Name of existing phpfpm pool that is used to run web-application.
            If not specified a pool will be created automatically with
            default values.
          '';
        };

      database = {
        type = mkOption {
          type = types.enum ["pgsql" "mysql" "sqlite"];
          default = "sqlite";
          description = ''
            Database to store feeds. Supported are sqlite, pgsql and mysql.
          '';
        };

        host = mkOption {
          type = types.str;
          default = "localhost";
          description = ''
            Host of the database (has no effect if type is "sqlite").
          '';
        };

        name = mkOption {
          type = types.str;
          default = "tt_rss";
          description = ''
            Name of the existing database (has no effect if type is "sqlite").
          '';
        };

        user = mkOption {
          type = types.str;
          default = "tt_rss";
          description = ''
            The database user. The user must exist and has access to
            the specified database (has no effect if type is "sqlite").
          '';
        };

        password = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            The database user's password (has no effect if type is "sqlite").
          '';
        };

        port = mkOption {
          type = types.nullOr types.int;
          default = null;
          description = ''
            The database's port. If not set, the default ports will be
            provided (5432 and 3306 for pgsql and mysql respectively)
            (has no effect if type is "sqlite").
          '';
        };
      };
      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Extra configuration added to config.ini
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    services.phpfpm.pools = mkIf (cfg.pool == "${poolName}") {
      ${poolName} = {
        user = "nginx";
        settings = mapAttrs (name: mkDefault) {
          "listen.owner" = "nginx";
          "listen.group" = "nginx";
          "listen.mode" = "0600";
          "pm" = "dynamic";
          "pm.max_children" = 75;
          "pm.start_servers" = 10;
          "pm.min_spare_servers" = 5;
          "pm.max_spare_servers" = 20;
          "pm.max_requests" = 500;
          "catch_workers_output" = 1;
        };
      };
    };

    systemd.services.selfoss-config = {
      serviceConfig.Type = "oneshot";
      script = ''
        mkdir -m 755 -p ${dataDir}
        cd ${dataDir}

        # Delete all but the "data" folder
        ls | grep -v data | while read line; do rm -rf $line; done || true

        # Create the files
        cp -r "${pkgs.selfoss}/"* "${dataDir}"
        ln -sf "${selfoss-config}" "${dataDir}/config.ini"
        chown -R "${cfg.user}" "${dataDir}"
        chmod -R 755 "${dataDir}"
      '';
      wantedBy = [ "multi-user.target" ];
    };

    systemd.services.selfoss-update = {
      serviceConfig = {
        ExecStart = "${pkgs.php}/bin/php ${dataDir}/cliupdate.php";
        User = "${cfg.user}";
      };
      startAt = "hourly";
      after = [ "selfoss-config.service" ];
      wantedBy = [ "multi-user.target" ];

    };

  };
}