{ config, lib, pkgs, ... }: let inherit (lib) types mkEnableOption mkIf mkOption ; cfg = config.crans.anubis; in { options.crans.anubis = { enable = mkEnableOption "Configuration d’Anubis du Crans."; useRedis = mkEnableOption "Utilisation de Redis pour sauvegarder les challenges"; instances = mkOption { type = types.attrsOf ( types.submodule { options = { hostname = mkOption { type = types.str; example = "wiki.crans.org"; description = '' Hostname to redirect to. ''; }; bind = mkOption { type = types.str; example = "/run/anubis/anubis-wiki/socket.sock"; description = '' Where Anubis should listen ''; }; bots = mkOption { type = types.listOf types.attrs; description = '' Anubis bot policy. ''; }; }; } ); }; }; config = mkIf cfg.enable { age.secrets.anubis-secret = mkIf cfg.enable { file = ../../secrets/reverseproxy/anubis-secret.age; owner = "anubis"; }; services.anubis = { defaultOptions.group = "nginx"; instances = lib.mapAttrs (name: conf: { enable = true; policy = { settings = { statusCodes = { CHALLENGE = 200; DENY = 403; }; logging = { sink = "stdio"; level = "WARN"; }; store = let redCfg = config.services.redis.servers.anubis; in mkIf cfg.useRedis { backend = "valkey"; parameters.url = "redis://${redCfg.bind}:${toString redCfg.port}/0"; }; }; extraBots = [ { name = "whitelist-crans"; action = "ALLOW"; remote_addresses = [ "185.230.79.0/22" "2a0c:700::/32" "46.105.102.188/32" "2001:41d0:2:d5bc::/128" ]; } { # https://github.com/TecharoHQ/anubis/blob/main/data/botPolicies.yaml import = "(data)/meta/default-config.yaml"; } { # On refuse si userAgent = "" # https://github.com/TecharoHQ/anubis/blob/main/data/common/rfc-violations.yaml import = "(data)/common/rfc-violations.yaml"; } ] ++ conf.bots; # Inclus via l’import de default-config.yaml. Et on préfére mettre le # whitelist Crans avant. useDefaultBotRules = false; }; settings = { BIND = conf.bind; BIND_NETWORK = "unix"; METRIC_BIND = "/run/anubis/anubis-${name}/metrics.sock"; TARGET = " "; # Subrequest authentification. COOKIE_DOMAIN = conf.hostname; # XXX: Impossible de mettre en commun les challenges inter-sites ? COOKIE_PREFIX = "anubis-${conf.hostname}"; REDIRECT_DOMAINS = conf.hostname; SOCKET_MODE = "0660"; # Secret partagé entre les instances ED25519_PRIVATE_KEY_HEX_FILE = config.age.secrets.anubis-secret.path; }; }) cfg.instances; }; services.redis = { # Meilleure licence que Redis package = pkgs.valkey; servers.anubis = { enable = cfg.useRedis; bind = "127.0.0.1"; port = 6379; databases = 1; }; }; }; }