1 { options, config, lib, pkgs, ... }:
8 cfg = config.services.searx;
10 settingsFile = pkgs.writeText "settings.yml"
11 (builtins.toJSON cfg.settings);
13 limiterSettingsFile = (pkgs.formats.toml { }).generate "limiter.toml" cfg.limiterSettings;
18 # write NixOS settings as JSON
21 cp --no-preserve=mode ${settingsFile} settings.yml
24 # substitute environment variables
25 env -0 | while IFS='=' read -r -d ''' n v; do
26 sed "s#@$n@#$v#g" -i settings.yml
30 settingType = with types; (oneOf
34 ]) // { description = "JSON value"; };
41 (mkRenamedOptionModule
42 [ "services" "searx" "configFile" ]
43 [ "services" "searx" "settingsFile" ])
51 relatedPackages = [ "searx" ];
52 description = "Whether to enable Searx, the meta search engine.";
55 environmentFile = mkOption {
56 type = types.nullOr types.path;
59 Environment file (see `systemd.exec(5)`
60 "EnvironmentFile=" section for the syntax) to define variables for
61 Searx. This option can be used to safely include secret keys into the
66 redisCreateLocally = mkOption {
70 Configure a local Redis server for SearXNG. This is required if you
71 want to enable the rate limiter and bot protection of SearXNG.
76 type = types.attrsOf settingType;
78 example = literalExpression ''
80 server.bind_address = "0.0.0.0";
81 server.secret_key = "@SEARX_SECRET_KEY@";
83 engines = lib.singleton
84 { name = "wolframalpha";
86 api_key = "@WOLFRAM_API_KEY@";
87 engine = "wolframalpha_api";
92 Searx settings. These will be merged with (taking precedence over)
93 the default configuration. It's also possible to refer to
95 (defined in [](#opt-services.searx.environmentFile))
96 using the syntax `@VARIABLE_NAME@`.
99 For available settings, see the Searx
100 [docs](https://searx.github.io/searx/admin/settings.html).
105 settingsFile = mkOption {
107 default = "${runDir}/settings.yml";
109 The path of the Searx server settings.yml file. If no file is
110 specified, a default file is used (default config file has debug mode
111 enabled). Note: setting this options overrides
112 [](#opt-services.searx.settings).
115 This file, along with any secret key it contains, will be copied
116 into the world-readable Nix store.
121 limiterSettings = mkOption {
122 type = types.attrsOf settingType;
124 example = literalExpression ''
131 botdetection.ip_lists.block_ip = [
132 # "93.184.216.34" # example.org
137 Limiter settings for SearXNG.
140 For available settings, see the SearXNG
141 [schema file](https://github.com/searxng/searxng/blob/master/searx/botdetection/limiter.toml).
146 package = mkPackageOption pkgs "searxng" { };
148 runInUwsgi = mkOption {
152 Whether to run searx in uWSGI as a "vassal", instead of using its
153 built-in HTTP server. This is the recommended mode for public or
154 large instances, but is unnecessary for LAN or local-only use.
157 The built-in HTTP server logs all queries by default.
162 uwsgiConfig = mkOption {
163 type = options.services.uwsgi.instance.type;
164 default = { http = ":8080"; };
165 example = literalExpression ''
167 disable-logging = true;
168 http = ":8080"; # serve via HTTP...
169 socket = "/run/searx/searx.sock"; # ...or UNIX socket
170 chmod-socket = "660"; # allow the searx group to read/write to the socket
174 Additional configuration of the uWSGI vassal running searx. It
175 should notably specify on which interfaces and ports the vassal
184 config = mkIf cfg.enable {
185 environment.systemPackages = [ cfg.package ];
188 { description = "Searx daemon user";
193 users.groups.searx = { };
195 systemd.services.searx-init = {
196 description = "Initialise Searx settings";
199 RemainAfterExit = true;
201 RuntimeDirectory = "searx";
202 RuntimeDirectoryMode = "750";
203 } // optionalAttrs (cfg.environmentFile != null)
204 { EnvironmentFile = builtins.toPath cfg.environmentFile; };
205 script = generateConfig;
208 systemd.services.searx = mkIf (!cfg.runInUwsgi) {
209 description = "Searx server, the meta search engine.";
210 wantedBy = [ "network.target" "multi-user.target" ];
211 requires = [ "searx-init.service" ];
212 after = [ "searx-init.service" ];
216 ExecStart = lib.getExe cfg.package;
217 } // optionalAttrs (cfg.environmentFile != null)
218 { EnvironmentFile = builtins.toPath cfg.environmentFile; };
220 SEARX_SETTINGS_PATH = cfg.settingsFile;
221 SEARXNG_SETTINGS_PATH = cfg.settingsFile;
225 systemd.services.uwsgi = mkIf cfg.runInUwsgi {
226 requires = [ "searx-init.service" ];
227 after = [ "searx-init.service" ];
230 services.searx.settings = {
231 # merge NixOS settings with defaults settings.yml
232 use_default_settings = mkDefault true;
233 redis.url = lib.mkIf cfg.redisCreateLocally "unix://${config.services.redis.servers.searx.unixSocket}";
236 services.uwsgi = mkIf cfg.runInUwsgi {
238 plugins = [ "python3" ];
240 instance.type = "emperor";
241 instance.vassals.searx = {
244 immediate-uid = "searx";
245 immediate-gid = "searx";
247 enable-threads = true;
248 module = "searx.webapp";
250 # TODO: drop this as it is only required for searx
251 "SEARX_SETTINGS_PATH=${cfg.settingsFile}"
252 # searxng compatibility https://github.com/searxng/searxng/issues/1519
253 "SEARXNG_SETTINGS_PATH=${cfg.settingsFile}"
256 pythonPackages = self: [ cfg.package ];
257 } // cfg.uwsgiConfig;
260 services.redis.servers.searx = lib.mkIf cfg.redisCreateLocally {
266 environment.etc."searxng/limiter.toml" = lib.mkIf (cfg.limiterSettings != { }) {
267 source = limiterSettingsFile;
271 meta.maintainers = with maintainers; [ rnhmjoj _999eagle ];