git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/searx: declarative configuration
commitcce415c7434f0a295916eb699a2ac98f0c4da0fe authorrnhmjoj <rnhmjoj@inventati.org> dateTue 27 Oct 2020, 13:55 (6 years ago) parent8addce8
1 file changed+122−34
nixos/modules/services/networking/searx.nix+122−34
nixos/modules/services/networking/searx.nix+122−34
@@ -3,32 +3,130 @@
33with lib;
44
55let
6−
6+ dataDir = "/var/lib/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 ${dataDir}
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+ '';
1050
1151in
1252
1353{
1454
55+ imports = [
56+ (mkRenamedOptionModule
57+ [ "services" "searx" "configFile" ]
58+ [ "services" "searx" "settingsFile" ])
59+ ];
60+
1561 ###### interface
1662
1763 options = {
1864
1965 services.searx = {
2066
21− enable = mkEnableOption
22− "the searx server. See https://github.com/asciimoo/searx";
67+ enable = mkOption {
68+ type = types.bool;
69+ default = false;
70+ relatedPackages = [ "searx" ];
71+ description = "Whether to enable Searx, the meta search engine.";
72+ };
2373
24− configFile = mkOption {
74+ environmentFile = mkOption {
2575 type = types.nullOr types.path;
2676 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− ";
77+ description = ''
78+ Environment file (see <literal>systemd.exec(5)</literal>
79+ "EnvironmentFile=" section for the syntax) to define variables for
80+ Searx. This option can be used to safely include secret keys into the
81+ Searx configuration.
82+ '';
83+ };
84+
85+ settings = mkOption {
86+ type = types.attrs;
87+ default = { };
88+ example = literalExample ''
89+ { server.port = 8080;
90+ server.bind_address = "0.0.0.0";
91+ server.secret_key = "@SEARX_SECRET_KEY@";
92+
93+ engines.wolframalpha =
94+ { shortcut = "wa";
95+ api_key = "@WOLFRAM_API_KEY@";
96+ engine = "wolframalpha_api";
97+ };
98+ }
99+ '';
100+ description = ''
101+ Searx settings. These will be merged with (taking precedence over)
102+ the default configuration. It's also possible to refer to
103+ environment variables
104+ (defined in <xref linkend="opt-services.searx.environmentFile"/>)
105+ using the syntax <literal>@VARIABLE_NAME@</literal>.
106+ <note>
107+ <para>
108+ For available settings, see the Searx
109+ <link xlink:href="https://searx.github.io/searx/admin/settings.html">docs</link>.
110+ </para>
111+ </note>
112+ '';
113+ };
114+
115+ settingsFile = mkOption {
116+ type = types.path;
117+ default = "${dataDir}/settings.yml";
118+ description = ''
119+ The path of the Searx server settings.yml file. If no file is
120+ specified, a default file is used (default config file has debug mode
121+ enabled). Note: setting this options overrides
122+ <xref linkend="opt-services.searx.settings"/>.
123+ <warning>
124+ <para>
125+ This file, along with any secret key it contains, will be copied
126+ into the world-readable Nix store.
127+ </para>
128+ </warning>
129+ '';
32130 };
33131
34132 package = mkOption {
@@ -46,30 +144,20 @@ in
46144 ###### implementation
47145
48146 config = mkIf config.services.searx.enable {
49−
50− users.users.searx =
51− { uid = config.ids.uids.searx;
52− description = "Searx user";
53− createHome = true;
54− home = "/var/lib/searx";
55− };
56−
57− users.groups.searx =
58− { gid = config.ids.gids.searx;
59− };
60−
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− });
147+ systemd.services.searx = {
148+ description = "Searx server, the meta search engine.";
149+ after = [ "network.target" ];
150+ wantedBy = [ "multi-user.target" ];
151+ serviceConfig = {
152+ User = "searx";
153+ DynamicUser = true;
154+ ExecStart = "${cfg.package}/bin/searx-run";
155+ StateDirectory = "searx";
156+ } // optionalAttrs (cfg.environmentFile != null)
157+ { EnvironmentFile = builtins.toPath cfg.environmentFile; };
158+ environment.SEARX_SETTINGS_PATH = cfg.settingsFile;
159+ preStart = mergeConfig;
160+ };
73161
74162 environment.systemPackages = [ cfg.package ];
75163