aboutsummaryrefslogtreecommitdiff
path: root/module.nix
blob: 40415b354f9d9e651d1d6b2b09d27b2edf5e36c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
self:

{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.omnisearch;
  pkg = cfg.package;

  finalConfigFile =
    if cfg.configFile != null then
      cfg.configFile
    else
      pkgs.writeText "omnisearch.ini" ''
        [server]
        host = ${cfg.settings.server.host}
        port = ${toString cfg.settings.server.port}
        domain = ${cfg.settings.server.domain}

        [proxy]
        ${lib.optionalString (cfg.settings.proxy.proxy != null) "proxy = \"${cfg.settings.proxy.proxy}\""}
        ${lib.optionalString (
          cfg.settings.proxy.list_file != null
        ) "list_file = ${cfg.settings.proxy.list_file}"}
        max_retries = ${toString cfg.settings.proxy.max_retries}
        randomize_username = ${lib.boolToString cfg.settings.proxy.randomize_username}
        randomize_password = ${lib.boolToString cfg.settings.proxy.randomize_password}

        [cache]
        dir = ${cfg.settings.cache.dir}
        ttl_search = ${toString cfg.settings.cache.ttl_search}
        ttl_infobox = ${toString cfg.settings.cache.ttl_infobox}
      '';
in
{
  options.services.omnisearch = {
    enable = lib.mkEnableOption "OmniSearch metasearch engine";

    package = lib.mkOption {
      type = lib.types.package;
      default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
      description = "The omnisearch package to use.";
    };

    configFile = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      default = null;
      description = "Path to a custom config.ini. Overrides 'settings'.";
    };

    settings = {
      server = {
        host = lib.mkOption {
          type = lib.types.str;
          default = "0.0.0.0";
        };
        port = lib.mkOption {
          type = lib.types.port;
          default = 8087;
        };
        domain = lib.mkOption {
          type = lib.types.str;
          default = "http://localhost:8087";
        };
      };
      proxy = {
        proxy = lib.mkOption {
          type = lib.types.nullOr lib.types.str;
          default = null;
        };
        list_file = lib.mkOption {
          type = lib.types.nullOr lib.types.str;
          default = null;
        };
        max_retries = lib.mkOption {
          type = lib.types.int;
          default = 3;
        };
        randomize_username = lib.mkOption {
          type = lib.types.bool;
          default = true;
        };
        randomize_password = lib.mkOption {
          type = lib.types.bool;
          default = true;
        };
      };
      cache = {
        dir = lib.mkOption {
          type = lib.types.str;
          default = "/var/cache/omnisearch";
        };
        ttl_search = lib.mkOption {
          type = lib.types.int;
          default = 3600;
        };
        ttl_infobox = lib.mkOption {
          type = lib.types.int;
          default = 86400;
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.omnisearch = {
      description = "OmniSearch Service";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        ExecStart = "${pkg}/bin/omnisearch";

        WorkingDirectory = "/var/lib/omnisearch";
        StateDirectory = "omnisearch";
        CacheDirectory = "omnisearch";

        BindReadOnlyPaths = [
          "${pkg}/share/omnisearch/templates:/var/lib/omnisearch/templates"
          "${pkg}/share/omnisearch/static:/var/lib/omnisearch/static"
          "${finalConfigFile}:/var/lib/omnisearch/config.ini"
        ];

        DynamicUser = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        Restart = "always";
      };
    };
  };
}