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