git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/searx: add support for running in uWSGI
commit623664e84f3d3db8c32d65df4cab1a174edff71e authorrnhmjoj <rnhmjoj@inventati.org> dateFri 6 Nov 2020, 10:12 (6 years ago) parent92d55d5
2 files changed+151−26
nixos/modules/services/networking/searx.nix+90−12 nixos/tests/searx.nix+61−14
nixos/modules/services/networking/searx.nix+90−12
@@ -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
nixos/tests/searx.nix+61−14
@@ -6,7 +6,8 @@ import ./make-test-python.nix ({ pkgs, ...} :
66 maintainers = [ rnhmjoj ];
77 };
88
9− machine = { ... }: {
9+ # basic setup: searx running the built-in webserver
10+ nodes.base = { ... }: {
1011 imports = [ ../modules/profiles/minimal.nix ];
1112
1213 services.searx = {
@@ -17,11 +18,10 @@ import ./make-test-python.nix ({ pkgs, ...} :
1718 '';
1819
1920 settings.server =
20− { port = 8080;
21+ { port = "8080";
2122 bind_address = "0.0.0.0";
2223 secret_key = "@SEARX_SECRET_KEY@";
2324 };
24−
2525 settings.engines = {
2626 wolframalpha =
2727 { api_key = "@WOLFRAM_API_KEY@";
@@ -29,34 +29,81 @@ import ./make-test-python.nix ({ pkgs, ...} :
2929 };
3030 startpage.shortcut = "start";
3131 };
32+ };
33+
34+ };
35+
36+ # fancy setup: run in uWSGI and use nginx as proxy
37+ nodes.fancy = { ... }: {
38+ imports = [ ../modules/profiles/minimal.nix ];
39+
40+ services.searx = {
41+ enable = true;
42+ runInUwsgi = true;
43+ uwsgiConfig = {
44+ # serve using the uwsgi protocol
45+ socket = "/run/searx/uwsgi.sock";
46+ chmod-socket = "660";
47+
48+ # use /searx as url "mountpoint"
49+ mount = "/searx=searx.webapp:application";
50+ module = "";
51+ manage-script-name = true;
52+ };
53+ };
3254
55+ # use nginx as reverse proxy
56+ services.nginx.enable = true;
57+ services.nginx.virtualHosts.localhost = {
58+ locations."/searx".extraConfig =
59+ ''
60+ include ${pkgs.nginx}/conf/uwsgi_params;
61+ uwsgi_pass unix:/run/searx/uwsgi.sock;
62+ '';
63+ locations."/searx/static/".alias = "${pkgs.searx}/share/static/";
3364 };
65+
66+ # allow nginx access to the searx socket
67+ users.users.nginx.extraGroups = [ "searx" ];
68+
3469 };
3570
3671 testScript =
3772 ''
38− start_all()
73+ base.start()
3974
4075 with subtest("Settings have been merged"):
41− machine.wait_for_unit("searx")
42− output = machine.succeed(
43− "${pkgs.yq-go}/bin/yq r /var/lib/searx/settings.yml"
76+ base.wait_for_unit("searx-init")
77+ base.wait_for_file("/run/searx/settings.yml")
78+ output = base.succeed(
79+ "${pkgs.yq-go}/bin/yq r /run/searx/settings.yml"
4480 " 'engines.(name==startpage).shortcut'"
4581 ).strip()
4682 assert output == "start", "Settings not merged"
4783
4884 with subtest("Environment variables have been substituted"):
49− machine.succeed("grep -q somesecret /var/lib/searx/settings.yml")
50− machine.succeed("grep -q sometoken /var/lib/searx/settings.yml")
85+ base.succeed("grep -q somesecret /run/searx/settings.yml")
86+ base.succeed("grep -q sometoken /run/searx/settings.yml")
87+ base.copy_from_vm("/run/searx/settings.yml")
5188
52− with subtest("Searx service is running"):
53− machine.wait_for_open_port(8080)
54− machine.succeed(
89+ with subtest("Basic setup is working"):
90+ base.wait_for_open_port(8080)
91+ base.wait_for_unit("searx")
92+ base.succeed(
5593 "${pkgs.curl}/bin/curl --fail http://localhost:8080"
5694 )
95+ base.shutdown()
5796
58− machine.copy_from_vm("/var/lib/searx/settings.yml")
59− machine.shutdown()
97+ with subtest("Nginx+uWSGI setup is working"):
98+ fancy.start()
99+ fancy.wait_for_open_port(80)
100+ fancy.wait_for_unit("uwsgi")
101+ fancy.succeed(
102+ "${pkgs.curl}/bin/curl --fail http://localhost/searx >&2"
103+ )
104+ fancy.succeed(
105+ "${pkgs.curl}/bin/curl --fail http://localhost/searx/static/js/bootstrap.min.js >&2"
106+ )
60107 '';
61108})
62109