about summary refs log tree commit diff stats
path: root/nix/modules/default.nix
blob: 62cc5c064f45efaadb4eee463b2c36a5378916a2 (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
flake:
{ config
, lib
, pkgs
, ...
}:

let
  cfg = config.services.searchix;

  package = flake.packages.${pkgs.system}.default;

  settingsFormat = pkgs.formats.toml { };

  defaultServiceConfig = {
    User = cfg.user;
    Group = cfg.group;
    ReadWritePaths = [ cfg.homeDir ];
    StateDirectory = mkIf (cfg.homeDir == "/var/lib/searchix") [ "searchix" ];
    Restart = "on-failure";

    CacheDirectory = "searchix";
    CapabilityBoundingSet = "";
    DeviceAllow = "";
    LockPersonality = true;
    MemoryDenyWriteExecute = true;
    NoNewPrivileges = true;
    PrivateDevices = true;
    PrivateMounts = true;
    PrivateTmp = true;
    PrivateUsers = true;
    ProtectClock = true;
    ProtectHome = true;
    ProtectHostname = true;
    ProtectSystem = "strict";
    ProtectControlGroups = true;
    ProtectKernelLogs = true;
    ProtectKernelModules = true;
    ProtectKernelTunables = true;
    RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
    RestrictNamespaces = true;
    RestrictRealtime = true;
    RestrictSUIDSGID = true;
    SystemCallArchitectures = "native";
    SystemCallFilter = [ "@system-service" "~@privileged @setuid @keyring" ];
    UMask = "0066";
  };

  inherit (lib) mkEnableOption mkOption mkIf optionalAttrs types;
in
{
  options.services.searchix = {
    enable = mkEnableOption "Searchix options search";

    user = mkOption {
      type = types.str;
      default = "searchix";
      description = "User account under which searchix runs.";
    };

    group = mkOption {
      type = types.str;
      default = "searchix";
      description = "Group under which searchix runs.";
    };

    homeDir = mkOption {
      type = types.path;
      default = "/var/lib/searchix";
      description = "Home directory for searchix user";
    };

    dates = mkOption {
      type = types.singleLineStr;
      default = "04:00";
      example = "weekly";
    };

    settings = mkOption {
      type = types.submodule {
        freeformType = settingsFormat.type;
        options = {
          dataPath = mkOption {
            type = types.str;
            description = "Where to store search index and other data.";
            default = "${cfg.homeDir}/data";
          };

          logLevel = mkOption {
            type = with types; enum [ "error" "warn" "info" "debug" ];
            description = "Only log messages with the given severity or above.";
            default = "info";
          };

          web = mkOption {
            type = types.submodule {
              freeformType = settingsFormat.type;
              options = {
                port = mkOption {
                  type = types.port;
                  description = "Port for searchix to listen on";
                  default = 51313;
                };

                listenAddress = mkOption {
                  type = types.str;
                  description = "Listen on a specific IP address.";
                  default = "localhost";
                };

                baseURL = mkOption {
                  type = types.str;
                  description = "The base URL that searchix will be served on.";
                  default = "http://localhost:3000";
                };

                sentryDSN = mkOption {
                  type = with types; nullOr str;
                  description = "Optionally enable sentry to track errors.";
                  default = null;
                };
              };
            };
          };

          importer = mkOption {
            type = types.submodule {
              freeformType = settingsFormat.type;

              importTimeout = mkOption {
                type = types.str;
                default = "30m";
                description = ''
                  Maximum time to wait for all import jobs.
                  May need to be increased based on the number of sources.
                '';
              };

              sources = mkOption {
                type = with types;
                  attrsOf (submodule (import ./source-options.nix {
                    inherit cfg settingsFormat;
                  }));
                default = {
                  nixos.enable = true;
                  darwin.enable = false;
                  home-manager.enable = false;
                };
                description = "Declarative specification of options sources for searchix.";
              };
            };
          };
        };
      };
      default = { };
      description = "Configuration for searchix (a web search interface for options in the nix ecosystem).";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.searchix-importer = {
      description = "Searchix option importer";
      conflicts = [ "searchix-web.service" ];
      path = with pkgs; [ nix ];
      serviceConfig = defaultServiceConfig // {
        ExecStart = "${package}/bin/import --config ${(settingsFormat.generate "searchix-config.toml" cfg.settings)}";
        Type = "oneshot";

        RestartSec = 10;
        RestartSteps = 5;
        RestartMaxDelaySec = "5 min";
      };

      startAt = cfg.dates;
    };

    systemd.timers.searchix-importer = {
      timerConfig = {
        Persistent = true;
        RandomizedDelaySec = 1800;
      };
    };

    systemd.services.searchix-web = {
      description = "Searchix Nix option search";
      after = [ "searchix-importer.service" ];
      wants = [ "searchix-importer.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = defaultServiceConfig // {
        ExecStart = "${package}/bin/serve --config ${(settingsFormat.generate "searchix-config.toml" cfg.settings)}";
      } // lib.optionalAttrs (cfg.port < 1024) {
        AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
        CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
      };
    };

    users.users = optionalAttrs (cfg.user == "searchix") {
      searchix = {
        group = cfg.group;
        home = cfg.homeDir;
        isSystemUser = true;
      };
    };

    users.groups = optionalAttrs (cfg.group == "searchix") {
      searchix = { };
    };
  };
}