2023-09-03 00:29:02 +08:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
2023-08-07 14:32:32 +08:00
|
|
|
let
|
|
|
|
cfg = config.services.dae;
|
2023-09-03 00:29:02 +08:00
|
|
|
assets = cfg.assets;
|
|
|
|
genAssetsDrv =
|
|
|
|
paths:
|
|
|
|
pkgs.symlinkJoin {
|
|
|
|
name = "dae-assets";
|
|
|
|
inherit paths;
|
|
|
|
};
|
2023-08-07 14:32:32 +08:00
|
|
|
in
|
|
|
|
{
|
2023-09-03 00:29:02 +08:00
|
|
|
meta.maintainers = with lib.maintainers; [
|
|
|
|
pokon548
|
|
|
|
oluceps
|
|
|
|
];
|
2023-08-07 14:32:32 +08:00
|
|
|
|
|
|
|
options = {
|
2023-09-03 00:29:02 +08:00
|
|
|
services.dae = with lib; {
|
2023-10-18 22:59:26 +02:00
|
|
|
enable = mkEnableOption "dae, a Linux high-performance transparent proxy solution based on eBPF";
|
2023-09-03 00:29:02 +08:00
|
|
|
|
2023-11-30 19:03:14 +01:00
|
|
|
package = mkPackageOption pkgs "dae" { };
|
2023-09-03 00:29:02 +08:00
|
|
|
|
|
|
|
assets = mkOption {
|
|
|
|
type = with types; (listOf path);
|
|
|
|
default = with pkgs; [
|
|
|
|
v2ray-geoip
|
|
|
|
v2ray-domain-list-community
|
|
|
|
];
|
|
|
|
defaultText = literalExpression "with pkgs; [ v2ray-geoip v2ray-domain-list-community ]";
|
|
|
|
description = ''
|
|
|
|
Assets required to run dae.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
assetsPath = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${genAssetsDrv assets}/share/v2ray";
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
(symlinkJoin {
|
|
|
|
name = "dae-assets";
|
|
|
|
paths = assets;
|
|
|
|
})/share/v2ray
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
The path which contains geolocation database.
|
|
|
|
This option will override `assets`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type =
|
|
|
|
with types;
|
|
|
|
submodule {
|
|
|
|
options = {
|
2023-10-18 22:59:26 +02:00
|
|
|
enable = mkEnableOption "opening {option}`port` in the firewall";
|
2023-09-03 00:29:02 +08:00
|
|
|
port = mkOption {
|
2023-09-12 11:38:33 +08:00
|
|
|
type = types.port;
|
2023-09-03 00:29:02 +08:00
|
|
|
description = ''
|
|
|
|
Port to be opened. Consist with field `tproxy_port` in config file.
|
|
|
|
'';
|
2024-12-10 20:26:33 +01:00
|
|
|
};
|
2023-09-03 00:29:02 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
default = {
|
|
|
|
enable = true;
|
|
|
|
port = 12345;
|
|
|
|
};
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
port = 12345;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Open the firewall port.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-09-03 00:46:35 +08:00
|
|
|
configFile = mkOption {
|
2023-09-12 00:14:12 +08:00
|
|
|
type = with types; (nullOr path);
|
|
|
|
default = null;
|
2023-09-03 00:46:35 +08:00
|
|
|
example = "/path/to/your/config.dae";
|
|
|
|
description = ''
|
|
|
|
The path of dae config file, end with `.dae`.
|
|
|
|
'';
|
|
|
|
};
|
2023-09-03 00:29:02 +08:00
|
|
|
|
|
|
|
config = mkOption {
|
2023-09-12 00:14:12 +08:00
|
|
|
type = with types; (nullOr str);
|
|
|
|
default = null;
|
2023-09-03 00:29:02 +08:00
|
|
|
description = ''
|
2023-09-12 00:14:12 +08:00
|
|
|
WARNING: This option will expose store your config unencrypted world-readable in the nix store.
|
2023-09-03 00:29:02 +08:00
|
|
|
Config text for dae.
|
2023-09-03 03:51:42 +00:00
|
|
|
|
|
|
|
See <https://github.com/daeuniverse/dae/blob/main/example.dae>.
|
2023-09-03 00:29:02 +08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
disableTxChecksumIpGeneric = mkEnableOption "" // {
|
2023-10-18 22:59:26 +02:00
|
|
|
description = "See <https://github.com/daeuniverse/dae/issues/43>";
|
|
|
|
};
|
2023-09-03 00:29:02 +08:00
|
|
|
|
2023-08-07 14:32:32 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
config =
|
|
|
|
lib.mkIf cfg.enable
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
{
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
systemd.packages = [ cfg.package ];
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
networking = lib.mkIf cfg.openFirewall.enable {
|
|
|
|
firewall =
|
2024-12-10 20:26:33 +01:00
|
|
|
let
|
2023-09-03 00:29:02 +08:00
|
|
|
portToOpen = cfg.openFirewall.port;
|
2024-12-10 20:26:33 +01:00
|
|
|
in
|
|
|
|
{
|
2023-09-03 00:29:02 +08:00
|
|
|
allowedTCPPorts = [ portToOpen ];
|
|
|
|
allowedUDPPorts = [ portToOpen ];
|
2024-12-10 20:26:33 +01:00
|
|
|
};
|
|
|
|
};
|
2023-09-03 00:29:02 +08:00
|
|
|
|
|
|
|
systemd.services.dae =
|
|
|
|
let
|
|
|
|
daeBin = lib.getExe cfg.package;
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
configPath =
|
|
|
|
if cfg.configFile != null then cfg.configFile else pkgs.writeText "config.dae" cfg.config;
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
TxChecksumIpGenericWorkaround =
|
|
|
|
with lib;
|
2023-09-12 00:14:12 +08:00
|
|
|
(getExe pkgs.writeShellApplication {
|
|
|
|
name = "disable-tx-checksum-ip-generic";
|
|
|
|
text = with pkgs; ''
|
|
|
|
iface=$(${iproute2}/bin/ip route | ${lib.getExe gawk} '/default/ {print $5}')
|
2023-09-03 00:29:02 +08:00
|
|
|
${lib.getExe ethtool} -K "$iface" tx-checksum-ip-generic off
|
2024-12-10 20:26:33 +01:00
|
|
|
'';
|
|
|
|
});
|
2023-09-03 00:29:02 +08:00
|
|
|
in
|
|
|
|
{
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
LoadCredential = [ "config.dae:${configPath}" ];
|
|
|
|
ExecStartPre = [
|
2024-12-10 20:26:33 +01:00
|
|
|
""
|
2023-09-03 00:29:02 +08:00
|
|
|
"${daeBin} validate -c \${CREDENTIALS_DIRECTORY}/config.dae"
|
|
|
|
] ++ (with lib; optional cfg.disableTxChecksumIpGeneric TxChecksumIpGenericWorkaround);
|
|
|
|
ExecStart = [
|
2024-12-10 20:26:33 +01:00
|
|
|
""
|
2023-09-03 00:29:02 +08:00
|
|
|
"${daeBin} run --disable-timestamp -c \${CREDENTIALS_DIRECTORY}/config.dae"
|
|
|
|
];
|
|
|
|
Environment = "DAE_LOCATION_ASSET=${cfg.assetsPath}";
|
2024-12-10 20:26:33 +01:00
|
|
|
};
|
2023-09-03 00:29:02 +08:00
|
|
|
};
|
2023-08-07 14:32:32 +08:00
|
|
|
|
2023-09-03 00:29:02 +08:00
|
|
|
assertions = [
|
2024-12-10 20:26:33 +01:00
|
|
|
{
|
2023-09-03 00:29:02 +08:00
|
|
|
assertion = lib.pathExists (toString (genAssetsDrv cfg.assets) + "/share/v2ray");
|
2023-09-12 00:14:12 +08:00
|
|
|
message = ''
|
|
|
|
Packages in `assets` has no preset paths included.
|
2023-09-03 00:29:02 +08:00
|
|
|
Please set `assetsPath` instead.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-09-12 00:14:12 +08:00
|
|
|
assertion = !((config.services.dae.config != null) && (config.services.dae.configFile != null));
|
2023-09-03 00:29:02 +08:00
|
|
|
message = ''
|
|
|
|
Option `config` and `configFile` could not be set
|
|
|
|
at the same time.
|
|
|
|
'';
|
|
|
|
}
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2023-09-12 00:14:12 +08:00
|
|
|
{
|
|
|
|
assertion = !((config.services.dae.config == null) && (config.services.dae.configFile == null));
|
|
|
|
message = ''
|
|
|
|
Either `config` or `configFile` should be set.
|
|
|
|
'';
|
|
|
|
}
|
2023-09-03 00:29:02 +08:00
|
|
|
];
|
2023-08-07 14:32:32 +08:00
|
|
|
};
|
|
|
|
}
|