@@ -3,7 +3,7 @@
33with lib;
44
55let
6− dataDir = "/var/lib/searx";
6+ runDir = "/run/searx";
77 cfg = config.services.searx;
88
99 hasEngines =
@@ -13,7 +13,7 @@ let
1313 # Script to merge NixOS settings with
1414 # the default settings.yml bundled in searx.
1515 mergeConfig = ''
16− cd ${dataDir}
16+ cd ${runDir}
1717 # find the default settings.yml
1818 default=$(find '${cfg.package}/' -name settings.yml)
1919
@@ -46,6 +46,9 @@ let
4646 env -0 | while IFS='=' read -r -d ''' n v; do
4747 sed "s#@$n@#$v#g" -i settings.yml
4848 done
49+
50+ # set strict permissions
51+ chmod 400 settings.yml
4952 '';
5053
5154in
@@ -114,7 +117,7 @@ in
114117
115118 settingsFile = mkOption {
116119 type = types.path;
117− default = "${dataDir}/settings.yml";
120+ default = "${runDir}/settings.yml";
118121 description = ''
119122 The path of the Searx server settings.yml file. If no file is
120123 specified, a default file is used (default config file has debug mode
@@ -136,6 +139,38 @@ in
136139 description = "searx package to use.";
137140 };
138141
142+ runInUwsgi = mkOption {
143+ type = types.bool;
144+ default = false;
145+ description = ''
146+ Whether to run searx in uWSGI as a "vassal", instead of using its
147+ built-in HTTP server. This is the recommended mode for public or
148+ large instances, but is unecessary for LAN or local-only use.
149+ <warning>
150+ <para>
151+ The built-in HTTP server logs all queries by default.
152+ </para>
153+ </warning>
154+ '';
155+ };
156+
157+ uwsgiConfig = mkOption {
158+ type = types.attrs;
159+ default = { http = ":8080"; };
160+ example = lib.literalExample ''
161+ {
162+ disable-logging = true;
163+ http = ":8080"; # serve via HTTP...
164+ socket = "/run/searx/searx.sock"; # ...or UNIX socket
165+ }
166+ '';
167+ description = ''
168+ Additional configuration of the uWSGI vassal running searx. It
169+ should notably specify on which interfaces and ports the vassal
170+ should listen.
171+ '';
172+ };
173+
139174 };
140175
141176 };
@@ -143,23 +178,66 @@ in
143178
144179 ###### implementation
145180
146− config = mkIf config.services.searx.enable {
147− systemd.services.searx = {
148− description = "Searx server, the meta search engine.";
149− after = [ "network.target" ];
150− wantedBy = [ "multi-user.target" ];
181+ config = mkIf cfg.enable {
182+ environment.systemPackages = [ cfg.package ];
183+
184+ users.users.searx =
185+ { description = "Searx daemon user";
186+ group = "searx";
187+ isSystemUser = true;
188+ };
189+
190+ users.groups.searx = { };
191+
192+ systemd.services.searx-init = {
193+ description = "Initialise Searx settings";
151194 serviceConfig = {
195+ Type = "oneshot";
196+ RemainAfterExit = true;
152197 User = "searx";
153− DynamicUser = true;
198+ RuntimeDirectory = "searx";
199+ RuntimeDirectoryMode = "750";
200+ } // optionalAttrs (cfg.environmentFile != null)
201+ { EnvironmentFile = builtins.toPath cfg.environmentFile; };
202+ script = mergeConfig;
203+ };
204+
205+ systemd.services.searx = mkIf (!cfg.runInUwsgi) {
206+ description = "Searx server, the meta search engine.";
207+ wantedBy = [ "network.target" "multi-user.target" ];
208+ requires = [ "searx-init.service" ];
209+ after = [ "searx-init.service" ];
210+ serviceConfig = {
211+ User = "searx";
212+ Group = "searx";
154213 ExecStart = "${cfg.package}/bin/searx-run";
155− StateDirectory = "searx";
156214 } // optionalAttrs (cfg.environmentFile != null)
157215 { EnvironmentFile = builtins.toPath cfg.environmentFile; };
158216 environment.SEARX_SETTINGS_PATH = cfg.settingsFile;
159− preStart = mergeConfig;
160217 };
161218
162− environment.systemPackages = [ cfg.package ];
219+ systemd.services.uwsgi = mkIf (cfg.runInUwsgi)
220+ { requires = [ "searx-init.service" ];
221+ after = [ "searx-init.service" ];
222+ };
223+
224+ services.uwsgi = mkIf (cfg.runInUwsgi) {
225+ enable = true;
226+ plugins = [ "python3" ];
227+
228+ instance.type = "emperor";
229+ instance.vassals.searx = {
230+ type = "normal";
231+ strict = true;
232+ immediate-uid = "searx";
233+ immediate-gid = "searx";
234+ lazy-apps = true;
235+ enable-threads = true;
236+ module = "searx.webapp";
237+ env = [ "SEARX_SETTINGS_PATH=${cfg.settingsFile}" ];
238+ pythonPackages = self: [ cfg.package ];
239+ } // cfg.uwsgiConfig;
240+ };
163241
164242 };
165243