about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/search/typesense.nix
blob: 856c3cad22df0f538f27489158ceffe64eea56a8 (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
{ config, lib, pkgs, ... }: let
  inherit
    (lib)
    concatMapStringsSep
    generators
    mdDoc
    mkEnableOption
    mkIf
    mkOption
    mkPackageOption
    optionalString
    types
    ;

  cfg = config.services.typesense;
  settingsFormatIni = pkgs.formats.ini {
    listToValue = concatMapStringsSep " " (generators.mkValueStringDefault { });
    mkKeyValue = generators.mkKeyValueDefault
      {
        mkValueString = v:
          if v == null then ""
          else generators.mkValueStringDefault { } v;
      }
      "=";
  };
  configFile = settingsFormatIni.generate "typesense.ini" cfg.settings;
in {
  options.services.typesense = {
    enable = mkEnableOption "typesense";
    package = mkPackageOption pkgs "typesense" {};

    apiKeyFile = mkOption {
      type = types.path;
      description = ''
        Sets the admin api key for typesense. Always use this option
        instead of {option}`settings.server.api-key` to prevent the key
        from being written to the world-readable nix store.
      '';
    };

    settings = mkOption {
      description = mdDoc "Typesense configuration. Refer to [the documentation](https://typesense.org/docs/0.24.1/api/server-configuration.html) for supported values.";
      default = {};
      type = types.submodule {
        freeformType = settingsFormatIni.type;
        options.server = {
          data-dir = mkOption {
            type = types.str;
            default = "/var/lib/typesense";
            description = mdDoc "Path to the directory where data will be stored on disk.";
          };

          api-address = mkOption {
            type = types.str;
            description = mdDoc "Address to which Typesense API service binds.";
          };

          api-port = mkOption {
            type = types.port;
            default = 8108;
            description = mdDoc "Port on which the Typesense API service listens.";
          };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.typesense = {
      description = "Typesense search engine";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      script = ''
        export TYPESENSE_API_KEY=$(cat ${cfg.apiKeyFile})
        exec ${cfg.package}/bin/typesense-server --config ${configFile}
      '';

      serviceConfig = {
        Restart = "on-failure";
        DynamicUser = true;
        User = "typesense";
        Group = "typesense";

        StateDirectory = "typesense";
        StateDirectoryMode = "0700";

        # Hardening
        CapabilityBoundingSet = "";
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateUsers = true;
        PrivateTmp = true;
        PrivateDevices = true;
        PrivateMounts = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProcSubset = "pid";
        ProtectSystem = "strict";
        RemoveIPC = true;
        RestrictAddressFamilies = [
          "AF_INET"
          "AF_INET6"
          "AF_UNIX"
        ];
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
        ];
        UMask = "0077";
      };
    };
  };
}