about summary refs log tree commit diff
path: root/nixos/modules/services/search/quickwit.nix
blob: c4cc0c2427dff209769a209522419fbb3d1fa555 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.quickwit;

  settingsFormat = pkgs.formats.yaml {};
  quickwitYml = settingsFormat.generate "quickwit.yml" cfg.settings;

  usingDefaultDataDir = cfg.dataDir == "/var/lib/quickwit";
  usingDefaultUserAndGroup = cfg.user == "quickwit" && cfg.group == "quickwit";
in
{

  options.services.quickwit = {
    enable = mkEnableOption "Quickwit";

    package = lib.mkPackageOption pkgs "Quickwit" {
      default = [ "quickwit" ];
    };

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = settingsFormat.type;

        options."rest" = lib.mkOption {
          default = {};
          description = ''
            Rest server configuration for Quickwit
          '';

          type = lib.types.submodule {
            freeformType = settingsFormat.type;

            options."listen_port" = lib.mkOption {
              type = lib.types.port;
              default = 7280;
              description = ''
                The port to listen on for HTTP REST traffic.
              '';
            };
          };
        };

        options."grpc_listen_port" = lib.mkOption {
          type = lib.types.port;
          default = 7281;
          description = ''
            The port to listen on for gRPC traffic.
          '';
        };

        options."listen_address" = lib.mkOption {
          type = lib.types.str;
          default = "127.0.0.1";
          description = ''
            Listen address of Quickwit.
          '';
        };

        options."version" = lib.mkOption {
          type = lib.types.float;
          default = 0.7;
          description = ''
            Configuration file version.
          '';
        };
      };

      default = {};

      description = ''
        Quickwit configuration.
      '';
    };

    dataDir = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/quickwit";
      apply = converge (removeSuffix "/");
      description = ''
        Data directory for Quickwit. If you change this, you need to
        manually create the directory. You also need to create the
        `quickwit` user and group, or change
        [](#opt-services.quickwit.user) and
        [](#opt-services.quickwit.group) to existing ones with
        access to the directory.
      '';
    };

    user = lib.mkOption {
      type = lib.types.str;
      default = "quickwit";
      description = ''
        The user Quickwit runs as. Should be left at default unless
        you have very specific needs.
      '';
    };

    group = lib.mkOption {
      type = lib.types.str;
      default = "quickwit";
      description = ''
        The group quickwit runs as. Should be left at default unless
        you have very specific needs.
      '';
    };

    extraFlags = lib.mkOption {
      description = "Extra command line options to pass to Quickwit.";
      default = [ ];
      type = lib.types.listOf lib.types.str;
    };

    restartIfChanged = lib.mkOption {
      type = lib.types.bool;
      description = ''
        Automatically restart the service on config change.
        This can be set to false to defer restarts on a server or cluster.
        Please consider the security implications of inadvertently running an older version,
        and the possibility of unexpected behavior caused by inconsistent versions across a cluster when disabling this option.
      '';
      default = true;
    };
  };

  config = mkIf cfg.enable {
    systemd.services.quickwit = {
      description = "Quickwit";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      inherit (cfg) restartIfChanged;
      environment = {
        QW_DATA_DIR = cfg.dataDir;
      };
      serviceConfig = {
        ExecStart = ''
          ${cfg.package}/bin/quickwit run --config ${quickwitYml} \
          ${escapeShellArgs cfg.extraFlags}
        '';
        User = cfg.user;
        Group = cfg.group;
        Restart = "on-failure";
        DynamicUser = usingDefaultUserAndGroup && usingDefaultDataDir;
        CapabilityBoundingSet = [ "" ];
        DevicePolicy = "closed";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectControlGroups = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        ReadWritePaths = [
          cfg.dataDir
        ];
        RestrictAddressFamilies = [
          "AF_NETLINK"
          "AF_INET"
          "AF_INET6"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          # 1. allow a reasonable set of syscalls
          "@system-service @resources"
          # 2. and deny unreasonable ones
          "~@privileged"
          # 3. then allow the required subset within denied groups
          "@chown"
        ];
      } // (optionalAttrs (usingDefaultDataDir) {
        StateDirectory = "quickwit";
        StateDirectoryMode = "0700";
      });
    };

    environment.systemPackages = [ cfg.package ];
  };
}