old
  1{
  2  den,
  3  lib,
  4  inputs,
  5  ...
  6}: {
  7  den.aspects.services.proxy.mihomo = {
  8    settings = {
  9      autoStart = lib.mkEnableOption "Auto start mihomo service";
 10      tailscaleWebControl = lib.mkEnableOption "Allow connect external controller from tailscale subnet";
 11      interfaces = {
 12        wan = lib.mkOption {
 13          description = "The WAN interface to bind.";
 14          type = lib.types.str;
 15          default = null;
 16        };
 17        lan = lib.mkOption {
 18          description = "The LAN interface to bind.";
 19          type = lib.types.listOf lib.types.str;
 20          default = [];
 21        };
 22      };
 23      ports = {
 24        controller = lib.mkOption {
 25          type = lib.types.port;
 26          default = 7900;
 27        };
 28        dns = lib.mkOption {
 29          type = lib.types.port;
 30          default = 1053;
 31        };
 32        mixed = lib.mkOption {
 33          type = lib.types.port;
 34          default = 7890;
 35        };
 36        tproxy = lib.mkOption {
 37          type = lib.types.port;
 38          default = 7894;
 39        };
 40      };
 41    };
 42
 43    includes = with den.aspects.services.proxy.mihomo; [
 44      dns
 45      proxies
 46      proxy-groups
 47      rules
 48      sniffer
 49    ];
 50
 51    cache = {
 52      directories = [
 53        {
 54          directory = "/var/lib/mihomo";
 55          user = "mihomo";
 56          group = "mihomo";
 57        }
 58      ];
 59    };
 60
 61    nixos = {
 62      host,
 63      pkgs,
 64      ...
 65    }: let
 66      cfg = host.settings.services.proxy.mihomo;
 67    in {
 68      imports = [inputs.nur-hpcesia.nixosModules.mihomo];
 69
 70      services.mihomo = {
 71        enable = true;
 72        webui = pkgs.metacubexd;
 73        processesInfo = lib.mkDefault true;
 74
 75        config = {
 76          mixed-port = cfg.ports.mixed;
 77          mode = "rule";
 78          ipv6 = false;
 79          find-process-mode = lib.mkDefault "strict";
 80          allow-lan = lib.mkDefault true;
 81          bind-address = lib.mkDefault "*";
 82          log-level = "warning";
 83          interface-name = cfg.interfaces.wan;
 84          unified-delay = true;
 85          tcp-concurrent = true;
 86          geodata-mode = true; # `.dat`
 87          geox-url = {
 88            geoip = "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat";
 89            geosite = "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat";
 90          };
 91          external-controller = "${
 92            if cfg.tailscaleWebControl
 93            then host.address.ipv4.tailscale
 94            else "127.0.0.1"
 95          }:${toString cfg.ports.controller}";
 96          external-controller-cors = {
 97            allow-origins = ["*"];
 98            allow-private-network = true;
 99          };
100        };
101      };
102
103      systemd.services."mihomo" = {
104        after = lib.optional (cfg.tailscaleWebControl) "tailscaled.service";
105        wantedBy = lib.mkIf (!cfg.autoStart) (lib.mkForce []);
106      };
107    };
108  };
109}