1{ config, lib, pkgs, ... }:
2with lib;
3let
4 cfg = config.services.selfoss;
5
6 poolName = "selfoss_pool";
7
8 dataDir = "/var/lib/selfoss";
9
10 selfoss-config =
11 let
12 db_type = cfg.database.type;
13 default_port = if (db_type == "mysql") then 3306 else 5342;
14 in
15 pkgs.writeText "selfoss-config.ini" ''
16 [globals]
17 ${lib.optionalString (db_type != "sqlite") ''
18 db_type=${db_type}
19 db_host=${cfg.database.host}
20 db_database=${cfg.database.name}
21 db_username=${cfg.database.user}
22 db_password=${cfg.database.password}
23 db_port=${toString (if (cfg.database.port != null) then cfg.database.port
24 else default_port)}
25 ''
26 }
27 ${cfg.extraConfig}
28 '';
29in
30 {
31 options = {
32 services.selfoss = {
33 enable = mkEnableOption "selfoss";
34
35 user = mkOption {
36 type = types.str;
37 default = "nginx";
38 example = "nginx";
39 description = ''
40 User account under which both the service and the web-application run.
41 '';
42 };
43
44 pool = mkOption {
45 type = types.str;
46 default = "${poolName}";
47 description = ''
48 Name of existing phpfpm pool that is used to run web-application.
49 If not specified a pool will be created automatically with
50 default values.
51 '';
52 };
53
54 database = {
55 type = mkOption {
56 type = types.enum ["pgsql" "mysql" "sqlite"];
57 default = "sqlite";
58 description = ''
59 Database to store feeds. Supported are sqlite, pgsql and mysql.
60 '';
61 };
62
63 host = mkOption {
64 type = types.str;
65 default = "localhost";
66 description = ''
67 Host of the database (has no effect if type is "sqlite").
68 '';
69 };
70
71 name = mkOption {
72 type = types.str;
73 default = "tt_rss";
74 description = ''
75 Name of the existing database (has no effect if type is "sqlite").
76 '';
77 };
78
79 user = mkOption {
80 type = types.str;
81 default = "tt_rss";
82 description = ''
83 The database user. The user must exist and has access to
84 the specified database (has no effect if type is "sqlite").
85 '';
86 };
87
88 password = mkOption {
89 type = types.nullOr types.str;
90 default = null;
91 description = ''
92 The database user's password (has no effect if type is "sqlite").
93 '';
94 };
95
96 port = mkOption {
97 type = types.nullOr types.int;
98 default = null;
99 description = ''
100 The database's port. If not set, the default ports will be
101 provided (5432 and 3306 for pgsql and mysql respectively)
102 (has no effect if type is "sqlite").
103 '';
104 };
105 };
106 extraConfig = mkOption {
107 type = types.lines;
108 default = "";
109 description = ''
110 Extra configuration added to config.ini
111 '';
112 };
113 };
114 };
115
116 config = mkIf cfg.enable {
117 services.phpfpm.pools = mkIf (cfg.pool == "${poolName}") {
118 ${poolName} = {
119 user = "nginx";
120 settings = mapAttrs (name: mkDefault) {
121 "listen.owner" = "nginx";
122 "listen.group" = "nginx";
123 "listen.mode" = "0600";
124 "pm" = "dynamic";
125 "pm.max_children" = 75;
126 "pm.start_servers" = 10;
127 "pm.min_spare_servers" = 5;
128 "pm.max_spare_servers" = 20;
129 "pm.max_requests" = 500;
130 "catch_workers_output" = 1;
131 };
132 };
133 };
134
135 systemd.services.selfoss-config = {
136 serviceConfig.Type = "oneshot";
137 script = ''
138 mkdir -m 755 -p ${dataDir}
139 cd ${dataDir}
140
141 # Delete all but the "data" folder
142 ls | grep -v data | while read line; do rm -rf $line; done || true
143
144 # Create the files
145 cp -r "${pkgs.selfoss}/"* "${dataDir}"
146 ln -sf "${selfoss-config}" "${dataDir}/config.ini"
147 chown -R "${cfg.user}" "${dataDir}"
148 chmod -R 755 "${dataDir}"
149 '';
150 wantedBy = [ "multi-user.target" ];
151 };
152
153 systemd.services.selfoss-update = {
154 serviceConfig = {
155 ExecStart = "${pkgs.php}/bin/php ${dataDir}/cliupdate.php";
156 User = "${cfg.user}";
157 };
158 startAt = "hourly";
159 after = [ "selfoss-config.service" ];
160 wantedBy = [ "multi-user.target" ];
161
162 };
163
164 };
165}