{ config, lib, pkgs, ... }: let cfg = config.services.caveman; hunterDefaultSettings = { redis = "redis://127.0.0.1:${toString cfg.redis.port}/"; hosts = [ "mastodon.social" "fosstodon.org" "chaos.social" "dresden.network" ]; max_workers = 16; }; hunterSettings = lib.recursiveUpdate hunterDefaultSettings cfg.hunter.settings; hunterConfigFile = builtins.toFile "hunter.yaml" ( builtins.toJSON hunterSettings ); gathererDefaultSettings = { redis = "redis://127.0.0.1:${toString cfg.redis.port}/"; listen_port = 8000; }; gathererSettings = lib.recursiveUpdate gathererDefaultSettings cfg.gatherer.settings; gathererConfigFile = builtins.toFile "gatherer.yaml" ( builtins.toJSON gathererSettings ); limitNOFILE = 1000000; in { options.services.caveman = with lib; { redis.port = mkOption { type = types.int; default = 6379; }; redis.maxmemory = mkOption { type = types.int; default = 1024 * 1024 * 1024; }; redis.maxmemory-samples = mkOption { type = types.int; default = 8; }; hunter.enable = mkEnableOption "caveman hunter"; hunter.settings = mkOption { type = types.anything; default = hunterDefaultSettings; }; hunter.logLevel = mkOption { type = types.enum [ "ERROR" "WARN" "INFO" "DEBUG" "TRACE" ]; default = "DEBUG"; }; gatherer.enable = mkEnableOption "caveman gatherer"; gatherer.settings = mkOption { type = types.anything; default = gathererDefaultSettings; }; gatherer.logLevel = mkOption { type = types.enum [ "ERROR" "WARN" "INFO" "DEBUG" "TRACE" ]; default = "DEBUG"; }; }; config = { systemd.extraConfig = '' DefaultLimitNOFILE=${toString limitNOFILE} ''; services.redis.servers.caveman = lib.mkIf cfg.hunter.enable { enable = true; port = cfg.redis.port; settings = { inherit (cfg.redis) maxmemory maxmemory-samples; maxmemory-policy = "allkeys-lru"; }; }; systemd.services.caveman-hunter = lib.mkIf cfg.hunter.enable { wantedBy = [ "multi-user.target" ]; requires = [ "redis-caveman.service" ]; after = [ "redis-caveman.service" "network-online.target" ]; environment.RUST_LOG = "caveman=${cfg.hunter.logLevel}"; serviceConfig = { ExecStart = "${pkgs.caveman-hunter}/bin/caveman-hunter ${hunterConfigFile}"; Type = "notify"; WatchdogSec = 600; Restart = "always"; RestartSec = 30; DynamicUser = true; User = "caveman-hunter"; ProtectSystem = "strict"; ProtectHome = true; ProtectHostname = true; ProtectKernelLogs = true; ProtectKernelModules = true; ProtectKernelTunables = true; RestrictNamespaces = true; RestrictRealtime = true; LockPersonality = true; MemoryDenyWriteExecute = true; LimitNOFile = limitNOFILE; }; }; systemd.services.caveman-gatherer = lib.mkIf cfg.gatherer.enable { wantedBy = [ "multi-user.target" ]; requires = [ "redis-caveman.service" ]; after = [ "redis-caveman.service" "network-online.target" ]; environment.RUST_LOG = "caveman=${cfg.gatherer.logLevel}"; serviceConfig = { ExecStart = "${pkgs.caveman-gatherer}/bin/caveman-gatherer ${gathererConfigFile}"; Type = "notify"; WatchdogSec = 90; Restart = "always"; RestartSec = 1; DynamicUser = true; User = "caveman-gatherer"; ProtectSystem = "strict"; ProtectHome = true; ProtectHostname = true; ProtectKernelLogs = true; ProtectKernelModules = true; ProtectKernelTunables = true; RestrictNamespaces = true; RestrictRealtime = true; LockPersonality = true; MemoryDenyWriteExecute = true; LimitNOFile = limitNOFILE; WorkingDirectory = "${pkgs.caveman-gatherer}/share/caveman/gatherer"; }; }; systemd.timers.caveman-gatherer-probe = lib.mkIf cfg.gatherer.enable { wantedBy = [ "timers.target" ]; timerConfig.OnCalendar = "minutely"; }; systemd.services.caveman-gatherer-probe = lib.mkIf cfg.gatherer.enable { requires = [ "caveman-gatherer.service" ]; serviceConfig = { Type = "oneshot"; User = "caveman-gatherer-probe"; DynamicUser = true; ProtectSystem = "full"; }; path = with pkgs; [ wget ]; script = '' wget -O /dev/null --user-agent=caveman-gatherer-probe 127.0.0.1:${toString gathererSettings.listen_port}/ ''; }; }; }