@@ -3,32 +3,133 @@
33with lib;
44
55let
6−
6+ runDir = "/run/searx";
77 cfg = config.services.searx;
88
9− configFile = cfg.configFile;
9+ hasEngines =
10+ builtins.hasAttr "engines" cfg.settings &&
11+ cfg.settings.engines != { };
12+
13+ # Script to merge NixOS settings with
14+ # the default settings.yml bundled in searx.
15+ mergeConfig = ''
16+ cd ${runDir}
17+ # find the default settings.yml
18+ default=$(find '${cfg.package}/' -name settings.yml)
19+
20+ # write NixOS settings as JSON
21+ cat <<'EOF' > settings.json
22+ ${builtins.toJSON cfg.settings}
23+ EOF
24+
25+ ${optionalString hasEngines ''
26+ # extract and convert the default engines array to an object
27+ ${pkgs.yq-go}/bin/yq r "$default" engines -j | \
28+ ${pkgs.jq}/bin/jq 'reduce .[] as $e ({}; .[$e.name] = $e)' \
29+ > engines.json
30+
31+ # merge and update the NixOS engines with the newly created object
32+ cp settings.json temp.json
33+ ${pkgs.jq}/bin/jq -s '. as [$s, $e] | $s | .engines |=
34+ ($e * . | to_entries | map (.value))' \
35+ temp.json engines.json > settings.json
36+
37+ # clean up temporary files
38+ rm {engines,temp}.json
39+ ''}
40+
41+ # merge the default and NixOS settings
42+ ${pkgs.yq-go}/bin/yq m -P settings.json "$default" > settings.yml
43+ rm settings.json
44+
45+ # substitute environment variables
46+ env -0 | while IFS='=' read -r -d ''' n v; do
47+ sed "s#@$n@#$v#g" -i settings.yml
48+ done
49+
50+ # set strict permissions
51+ chmod 400 settings.yml
52+ '';
1053
1154in
1255
1356{
1457
58+ imports = [
59+ (mkRenamedOptionModule
60+ [ "services" "searx" "configFile" ]
61+ [ "services" "searx" "settingsFile" ])
62+ ];
63+
1564 ###### interface
1665
1766 options = {
1867
1968 services.searx = {
2069
21− enable = mkEnableOption
22− "the searx server. See https://github.com/asciimoo/searx";
70+ enable = mkOption {
71+ type = types.bool;
72+ default = false;
73+ relatedPackages = [ "searx" ];
74+ description = "Whether to enable Searx, the meta search engine.";
75+ };
2376
24− configFile = mkOption {
77+ environmentFile = mkOption {
2578 type = types.nullOr types.path;
2679 default = null;
27− description = "
28− The path of the Searx server configuration file. If no file
29− is specified, a default file is used (default config file has
30− debug mode enabled).
31− ";
80+ description = ''
81+ Environment file (see <literal>systemd.exec(5)</literal>
82+ "EnvironmentFile=" section for the syntax) to define variables for
83+ Searx. This option can be used to safely include secret keys into the
84+ Searx configuration.
85+ '';
86+ };
87+
88+ settings = mkOption {
89+ type = types.attrs;
90+ default = { };
91+ example = literalExample ''
92+ { server.port = 8080;
93+ server.bind_address = "0.0.0.0";
94+ server.secret_key = "@SEARX_SECRET_KEY@";
95+
96+ engines.wolframalpha =
97+ { shortcut = "wa";
98+ api_key = "@WOLFRAM_API_KEY@";
99+ engine = "wolframalpha_api";
100+ };
101+ }
102+ '';
103+ description = ''
104+ Searx settings. These will be merged with (taking precedence over)
105+ the default configuration. It's also possible to refer to
106+ environment variables
107+ (defined in <xref linkend="opt-services.searx.environmentFile"/>)
108+ using the syntax <literal>@VARIABLE_NAME@</literal>.
109+ <note>
110+ <para>
111+ For available settings, see the Searx
112+ <link xlink:href="https://searx.github.io/searx/admin/settings.html">docs</link>.
113+ </para>
114+ </note>
115+ '';
116+ };
117+
118+ settingsFile = mkOption {
119+ type = types.path;
120+ default = "${runDir}/settings.yml";
121+ description = ''
122+ The path of the Searx server settings.yml file. If no file is
123+ specified, a default file is used (default config file has debug mode
124+ enabled). Note: setting this options overrides
125+ <xref linkend="opt-services.searx.settings"/>.
126+ <warning>
127+ <para>
128+ This file, along with any secret key it contains, will be copied
129+ into the world-readable Nix store.
130+ </para>
131+ </warning>
132+ '';
32133 };
33134
34135 package = mkOption {
@@ -38,6 +139,38 @@ in
38139 description = "searx package to use.";
39140 };
40141
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+
41174 };
42175
43176 };
@@ -45,33 +178,66 @@ in
45178
46179 ###### implementation
47180
48− config = mkIf config.services.searx.enable {
181+ config = mkIf cfg.enable {
182+ environment.systemPackages = [ cfg.package ];
49183
50184 users.users.searx =
51− { uid = config.ids.uids.searx;
52− description = "Searx user";
53− createHome = true;
54− home = "/var/lib/searx";
185+ { description = "Searx daemon user";
186+ group = "searx";
187+ isSystemUser = true;
55188 };
56189
57− users.groups.searx =
58− { gid = config.ids.gids.searx;
59− };
190+ users.groups.searx = { };
191+
192+ systemd.services.searx-init = {
193+ description = "Initialise Searx settings";
194+ serviceConfig = {
195+ Type = "oneshot";
196+ RemainAfterExit = true;
197+ User = "searx";
198+ RuntimeDirectory = "searx";
199+ RuntimeDirectoryMode = "750";
200+ } // optionalAttrs (cfg.environmentFile != null)
201+ { EnvironmentFile = builtins.toPath cfg.environmentFile; };
202+ script = mergeConfig;
203+ };
60204
61− systemd.services.searx =
62− {
63− description = "Searx server, the meta search engine.";
64− after = [ "network.target" ];
65− wantedBy = [ "multi-user.target" ];
66− serviceConfig = {
67− User = "searx";
68− ExecStart = "${cfg.package}/bin/searx-run";
69− };
70− } // (optionalAttrs (configFile != null) {
71− environment.SEARX_SETTINGS_PATH = configFile;
72− });
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";
213+ ExecStart = "${cfg.package}/bin/searx-run";
214+ } // optionalAttrs (cfg.environmentFile != null)
215+ { EnvironmentFile = builtins.toPath cfg.environmentFile; };
216+ environment.SEARX_SETTINGS_PATH = cfg.settingsFile;
217+ };
73218
74− 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+ };
75241
76242 };
77243