2023-07-25 23:33:37 +08:00
|
|
|
inputs:
|
|
|
|
{
|
|
|
|
options.nixos.services = let inherit (inputs.lib) mkOption types; in
|
|
|
|
{
|
|
|
|
impermanence =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
persistence = mkOption { type = types.nonEmptyStr; default = "/nix/persistent"; };
|
2023-08-04 18:46:20 +08:00
|
|
|
root = mkOption { type = types.nonEmptyStr; default = "/nix/rootfs/current"; };
|
2023-07-25 23:33:37 +08:00
|
|
|
};
|
|
|
|
snapper =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
configs = mkOption { type = types.attrsOf types.nonEmptyStr; default = {}; };
|
|
|
|
};
|
2023-07-26 11:24:27 +08:00
|
|
|
kmscon.enable = mkOption { type = types.bool; default = false; };
|
2023-07-26 17:03:09 +08:00
|
|
|
fontconfig.enable = mkOption { type = types.bool; default = false; };
|
2023-07-26 17:08:32 +08:00
|
|
|
u2f.enable = mkOption { type = types.bool; default = false; };
|
2023-07-26 21:05:46 +08:00
|
|
|
sops =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
keyPathPrefix = mkOption { type = types.str; default = ""; };
|
|
|
|
};
|
|
|
|
samba =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
wsdd = mkOption { type = types.bool; default = false; };
|
|
|
|
private = mkOption { type = types.bool; default = false; };
|
|
|
|
hostsAllowed = mkOption { type = types.str; default = "127."; };
|
|
|
|
shares = mkOption
|
2023-07-25 23:33:37 +08:00
|
|
|
{
|
2023-07-26 21:05:46 +08:00
|
|
|
type = types.attrsOf (types.submodule { options =
|
2023-07-25 23:33:37 +08:00
|
|
|
{
|
2023-07-26 21:05:46 +08:00
|
|
|
comment = mkOption { type = types.nullOr types.nonEmptyStr; default = null; };
|
|
|
|
path = mkOption { type = types.nonEmptyStr; };
|
|
|
|
};});
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
2023-07-27 00:07:20 +08:00
|
|
|
sshd.enable = mkOption { type = types.bool; default = false; };
|
2023-07-27 19:01:58 +08:00
|
|
|
xrayClient =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
2023-08-10 20:14:41 +08:00
|
|
|
serverAddress = mkOption { type = types.nonEmptyStr; };
|
|
|
|
serverName = mkOption { type = types.nonEmptyStr; };
|
2023-07-27 21:50:51 +08:00
|
|
|
dns = mkOption { type = types.submodule { options =
|
|
|
|
{
|
|
|
|
hosts = mkOption { type = types.attrsOf types.nonEmptyStr; default = {}; };
|
|
|
|
extraInterfaces = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
|
|
|
|
}; }; };
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
2023-08-10 19:41:33 +08:00
|
|
|
xrayServer =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
serverName = mkOption { type = types.nonEmptyStr; };
|
|
|
|
};
|
2023-07-27 19:01:58 +08:00
|
|
|
firewall.trustedInterfaces = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
|
2023-08-04 20:07:57 +08:00
|
|
|
acme =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
certs = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
|
|
|
|
};
|
2023-08-07 21:16:49 +08:00
|
|
|
frpClient =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
serverName = mkOption { type = types.nonEmptyStr; };
|
|
|
|
user = mkOption { type = types.nonEmptyStr; };
|
|
|
|
tcp = mkOption
|
|
|
|
{
|
|
|
|
type = types.attrsOf (types.submodule
|
|
|
|
{
|
|
|
|
options =
|
|
|
|
{
|
|
|
|
localIp = mkOption { type = types.nonEmptyStr; };
|
|
|
|
localPort = mkOption { type = types.ints.unsigned; };
|
|
|
|
remotePort = mkOption { type = types.ints.unsigned; };
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
frpServer =
|
|
|
|
{
|
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
|
|
|
serverName = mkOption { type = types.nonEmptyStr; };
|
|
|
|
};
|
2023-08-07 21:44:46 +08:00
|
|
|
nix-serve.enable = mkOption { type = types.bool; default = false; };
|
2023-08-10 10:15:21 +08:00
|
|
|
smartd.enable = mkOption { type = types.bool; default = false; };
|
2023-08-10 17:50:08 +08:00
|
|
|
nginx =
|
|
|
|
{
|
2023-08-13 21:02:56 +08:00
|
|
|
enable = mkOption { type = types.bool; default = false; };
|
2023-08-10 17:50:08 +08:00
|
|
|
transparentProxy =
|
|
|
|
{
|
2023-08-10 19:41:33 +08:00
|
|
|
externalIp = mkOption { type = types.nonEmptyStr; };
|
2023-08-12 23:58:18 +08:00
|
|
|
map = mkOption { type = types.attrsOf types.ints.unsigned; };
|
2023-08-10 17:50:08 +08:00
|
|
|
};
|
2023-08-12 11:58:01 +08:00
|
|
|
httpProxy = mkOption
|
|
|
|
{
|
|
|
|
type = types.attrsOf (types.submodule { options =
|
|
|
|
{
|
|
|
|
upstream = mkOption { type = types.nonEmptyStr; };
|
|
|
|
rewriteHttps = mkOption { type = types.bool; default = false; };
|
|
|
|
# setHeaders = mkOption { type = types.attrsOf types.nonEmptyStr; default = {}; };
|
|
|
|
# addPin = mkOption { type = types.bool; default = false; };
|
|
|
|
# detectPin = mkOption { type = types.bool; default = false; };
|
|
|
|
}; });
|
|
|
|
default = {};
|
|
|
|
};
|
2023-08-10 17:50:08 +08:00
|
|
|
};
|
2023-08-13 20:53:42 +08:00
|
|
|
send.enable = mkOption { type = types.bool; default = false; };
|
2023-07-26 21:05:46 +08:00
|
|
|
};
|
|
|
|
config =
|
|
|
|
let
|
2023-08-04 20:07:57 +08:00
|
|
|
inherit (inputs.lib) mkMerge mkIf;
|
2023-07-26 21:05:46 +08:00
|
|
|
inherit (inputs.localLib) stripeTabs attrsToList;
|
2023-07-26 21:11:47 +08:00
|
|
|
inherit (inputs.config.nixos) services;
|
2023-08-12 23:58:18 +08:00
|
|
|
inherit (builtins) map listToAttrs concatStringsSep toString elemAt genList length attrNames attrValues;
|
2023-07-26 21:05:46 +08:00
|
|
|
in mkMerge
|
|
|
|
[
|
|
|
|
(
|
|
|
|
mkIf services.impermanence.enable
|
2023-07-26 11:24:27 +08:00
|
|
|
{
|
2023-08-04 18:46:20 +08:00
|
|
|
environment.persistence =
|
2023-07-26 21:05:46 +08:00
|
|
|
{
|
2023-08-04 18:46:20 +08:00
|
|
|
"${services.impermanence.persistence}" =
|
|
|
|
{
|
|
|
|
hideMounts = true;
|
|
|
|
directories =
|
|
|
|
[
|
|
|
|
"/etc/NetworkManager/system-connections"
|
|
|
|
"/home"
|
|
|
|
"/root"
|
|
|
|
"/var/db"
|
|
|
|
"/var/lib"
|
|
|
|
"/var/log"
|
|
|
|
"/var/spool"
|
|
|
|
];
|
|
|
|
files =
|
|
|
|
[
|
|
|
|
"/etc/machine-id"
|
|
|
|
"/etc/ssh/ssh_host_ed25519_key.pub"
|
|
|
|
"/etc/ssh/ssh_host_ed25519_key"
|
|
|
|
"/etc/ssh/ssh_host_rsa_key.pub"
|
|
|
|
"/etc/ssh/ssh_host_rsa_key"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
"${services.impermanence.root}" =
|
|
|
|
{
|
|
|
|
hideMounts = true;
|
|
|
|
directories = []
|
|
|
|
++ (if inputs.config.services.xserver.displayManager.sddm.enable then
|
|
|
|
[{ directory = "/var/lib/sddm"; user = "sddm"; group = "sddm"; mode = "0700"; }] else []);
|
|
|
|
};
|
2023-07-26 21:05:46 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf services.snapper.enable
|
|
|
|
{
|
|
|
|
services.snapper.configs =
|
|
|
|
let
|
|
|
|
f = (config:
|
|
|
|
{
|
|
|
|
inherit (config) name;
|
|
|
|
value =
|
|
|
|
{
|
|
|
|
SUBVOLUME = config.value;
|
|
|
|
TIMELINE_CREATE = true;
|
|
|
|
TIMELINE_CLEANUP = true;
|
|
|
|
TIMELINE_MIN_AGE = 1800;
|
|
|
|
TIMELINE_LIMIT_HOURLY = "10";
|
|
|
|
TIMELINE_LIMIT_DAILY = "7";
|
|
|
|
TIMELINE_LIMIT_WEEKLY = "1";
|
|
|
|
TIMELINE_LIMIT_MONTHLY = "0";
|
|
|
|
TIMELINE_LIMIT_YEARLY = "0";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
in
|
2023-08-04 20:07:57 +08:00
|
|
|
listToAttrs (map f (attrsToList services.snapper.configs));
|
2023-08-04 22:18:39 +08:00
|
|
|
nixpkgs.config.packageOverrides = pkgs:
|
|
|
|
{
|
|
|
|
snapper = pkgs.snapper.overrideAttrs (attrs:
|
|
|
|
{
|
|
|
|
patches = (if (attrs ? patches) then attrs.patches else []) ++ [ ./snapper.patch ];
|
|
|
|
});
|
|
|
|
};
|
2023-07-26 21:05:46 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf services.kmscon.enable
|
2023-07-26 17:03:09 +08:00
|
|
|
{
|
2023-07-26 21:05:46 +08:00
|
|
|
services.kmscon =
|
2023-07-26 17:03:09 +08:00
|
|
|
{
|
2023-07-26 21:05:46 +08:00
|
|
|
enable = true;
|
|
|
|
fonts = [{ name = "FiraCode Nerd Font Mono"; package = inputs.pkgs.nerdfonts; }];
|
2023-07-26 17:03:09 +08:00
|
|
|
};
|
2023-07-26 21:05:46 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf services.fontconfig.enable
|
|
|
|
{
|
|
|
|
fonts =
|
|
|
|
{
|
|
|
|
fontDir.enable = true;
|
2023-08-09 22:05:51 +08:00
|
|
|
packages = with inputs.pkgs;
|
2023-07-26 21:05:46 +08:00
|
|
|
[ noto-fonts source-han-sans source-han-serif source-code-pro hack-font jetbrains-mono nerdfonts ];
|
|
|
|
fontconfig.defaultFonts =
|
|
|
|
{
|
|
|
|
emoji = [ "Noto Color Emoji" ];
|
|
|
|
monospace = [ "Noto Sans Mono CJK SC" "Sarasa Mono SC" "DejaVu Sans Mono"];
|
|
|
|
sansSerif = [ "Noto Sans CJK SC" "Source Han Sans SC" "DejaVu Sans" ];
|
|
|
|
serif = [ "Noto Serif CJK SC" "Source Han Serif SC" "DejaVu Serif" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf services.sops.enable
|
|
|
|
{
|
|
|
|
sops =
|
|
|
|
{
|
|
|
|
defaultSopsFile = ../../secrets/${inputs.config.networking.hostName}.yaml;
|
|
|
|
# sops start before impermanence, so we need to use the absolute path
|
|
|
|
age.sshKeyPaths = [ "${services.sops.keyPathPrefix}/etc/ssh/ssh_host_ed25519_key" ];
|
2023-07-27 00:07:20 +08:00
|
|
|
gnupg.sshKeyPaths = [ "${services.sops.keyPathPrefix}/etc/ssh/ssh_host_rsa_key" ];
|
2023-07-26 21:05:46 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf services.samba.enable
|
2023-07-26 17:08:32 +08:00
|
|
|
{
|
2023-07-26 21:05:46 +08:00
|
|
|
# make shares visible for windows 10 clients
|
|
|
|
services =
|
|
|
|
{
|
|
|
|
samba-wsdd.enable = services.samba.wsdd;
|
|
|
|
samba =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
openFirewall = !services.samba.private;
|
|
|
|
securityType = "user";
|
|
|
|
extraConfig = stripeTabs
|
|
|
|
''
|
|
|
|
workgroup = WORKGROUP
|
|
|
|
server string = Samba Server
|
|
|
|
server role = standalone server
|
|
|
|
hosts allow = ${services.samba.hostsAllowed}
|
|
|
|
dns proxy = no
|
|
|
|
'';
|
|
|
|
# obey pam restrictions = yes
|
|
|
|
# encrypt passwords = no
|
2023-08-04 20:07:57 +08:00
|
|
|
shares = listToAttrs (map
|
2023-07-26 21:05:46 +08:00
|
|
|
(share:
|
|
|
|
{
|
|
|
|
name = share.name;
|
|
|
|
value =
|
|
|
|
{
|
|
|
|
comment = if share.value.comment != null then share.value.comment else share.name;
|
|
|
|
path = share.value.path;
|
|
|
|
browseable = true;
|
|
|
|
writeable = true;
|
|
|
|
"create mask" = "664";
|
|
|
|
"force create mode" = "644";
|
|
|
|
"directory mask" = "2755";
|
|
|
|
"force directory mode" = "2755";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(attrsToList services.samba.shares));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
2023-07-27 00:07:20 +08:00
|
|
|
(
|
2023-08-02 16:49:43 +08:00
|
|
|
mkIf services.sshd.enable
|
|
|
|
{
|
|
|
|
services.openssh =
|
|
|
|
{
|
|
|
|
enable = true;
|
2023-08-02 18:10:22 +08:00
|
|
|
settings =
|
|
|
|
{
|
2023-08-08 16:36:34 +08:00
|
|
|
X11Forwarding = true;
|
2023-08-02 18:10:22 +08:00
|
|
|
TrustedUserCAKeys = "${inputs.config.security.pam.ussh.caFile}";
|
|
|
|
ChallengeResponseAuthentication = false;
|
2023-08-03 17:03:54 +08:00
|
|
|
PasswordAuthentication = false;
|
|
|
|
KbdInteractiveAuthentication = false;
|
2023-08-02 18:10:22 +08:00
|
|
|
UsePAM = true;
|
|
|
|
};
|
2023-08-02 16:49:43 +08:00
|
|
|
};
|
|
|
|
}
|
2023-07-27 00:07:20 +08:00
|
|
|
)
|
2023-07-27 19:01:58 +08:00
|
|
|
(
|
|
|
|
mkIf services.xrayClient.enable
|
|
|
|
{
|
|
|
|
services =
|
|
|
|
{
|
|
|
|
dnsmasq =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
settings =
|
|
|
|
{
|
|
|
|
no-poll = true;
|
2023-08-10 20:47:59 +08:00
|
|
|
log-queries = true;
|
2023-07-27 19:01:58 +08:00
|
|
|
server = [ "127.0.0.1#10853" ];
|
2023-07-27 21:50:51 +08:00
|
|
|
interface = services.xrayClient.dns.extraInterfaces ++ [ "lo" ];
|
2023-07-27 23:41:04 +08:00
|
|
|
bind-dynamic = true;
|
2023-07-27 19:01:58 +08:00
|
|
|
ipset =
|
|
|
|
[
|
|
|
|
"/developer.download.nvidia.com/noproxy_net"
|
|
|
|
"/yuanshen.com/noproxy_net"
|
|
|
|
"/zoom.us/noproxy_net"
|
2023-07-27 21:50:51 +08:00
|
|
|
];
|
|
|
|
address = map (host: "/${host.name}/${host.value}") (attrsToList services.xrayClient.dns.hosts);
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
xray = { enable = true; settingsFile = inputs.config.sops.templates."xray-client.json".path; };
|
|
|
|
v2ray-forwarder = { enable = true; proxyPort = 10880; xmuPort = 10881; };
|
|
|
|
};
|
2023-07-27 19:32:51 +08:00
|
|
|
sops =
|
2023-07-27 19:01:58 +08:00
|
|
|
{
|
2023-07-27 19:32:51 +08:00
|
|
|
templates."xray-client.json" =
|
2023-07-27 19:01:58 +08:00
|
|
|
{
|
2023-08-11 15:30:47 +08:00
|
|
|
owner = inputs.config.users.users.v2ray.name;
|
|
|
|
group = inputs.config.users.users.v2ray.group;
|
2023-07-27 19:32:51 +08:00
|
|
|
content = builtins.toJSON
|
2023-07-27 19:01:58 +08:00
|
|
|
{
|
2023-08-10 20:43:10 +08:00
|
|
|
log.loglevel = "info";
|
2023-07-27 19:32:51 +08:00
|
|
|
dns =
|
|
|
|
{
|
|
|
|
servers =
|
|
|
|
[
|
2023-08-13 02:38:16 +08:00
|
|
|
{ address = "223.5.5.5"; domains = [ "geosite:geolocation-cn" ]; port = 53; }
|
|
|
|
{ address = "8.8.8.8"; domains = [ "geosite:geolocation-!cn" ]; port = 53; }
|
2023-07-27 19:32:51 +08:00
|
|
|
{ address = "223.5.5.5"; expectIPs = [ "geoip:cn" ]; }
|
|
|
|
{ address = "8.8.8.8"; }
|
|
|
|
];
|
|
|
|
disableCache = true;
|
|
|
|
queryStrategy = "UseIPv4";
|
2023-08-13 02:38:16 +08:00
|
|
|
disableFallback = true;
|
2023-07-27 19:32:51 +08:00
|
|
|
tag = "dns-internal";
|
|
|
|
};
|
|
|
|
inbounds =
|
2023-07-27 19:01:58 +08:00
|
|
|
[
|
2023-07-27 19:32:51 +08:00
|
|
|
{
|
|
|
|
port = 10853;
|
|
|
|
protocol = "dokodemo-door";
|
|
|
|
settings = { address = "8.8.8.8"; network = "tcp,udp"; port = 53; };
|
|
|
|
tag = "dns-in";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
port = 10880;
|
|
|
|
protocol = "dokodemo-door";
|
|
|
|
settings = { network = "tcp,udp"; followRedirect = true; };
|
|
|
|
streamSettings.sockopt.tproxy = "tproxy";
|
2023-08-10 20:43:10 +08:00
|
|
|
sniffing = { enabled = true; destOverride = [ "http" "tls" "quic" ]; routeOnly = true; };
|
2023-07-27 19:32:51 +08:00
|
|
|
tag = "common-in";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
port = 10881;
|
|
|
|
protocol = "dokodemo-door";
|
|
|
|
settings = { network = "tcp,udp"; followRedirect = true; };
|
|
|
|
streamSettings.sockopt.tproxy = "tproxy";
|
|
|
|
tag = "xmu-in";
|
|
|
|
}
|
|
|
|
{ port = 10882; protocol = "socks"; tag = "direct-in"; }
|
2023-07-27 19:01:58 +08:00
|
|
|
];
|
2023-07-27 19:32:51 +08:00
|
|
|
outbounds =
|
|
|
|
[
|
|
|
|
{
|
|
|
|
protocol = "vless";
|
|
|
|
settings.vnext =
|
2023-07-27 19:01:58 +08:00
|
|
|
[{
|
2023-08-10 20:14:41 +08:00
|
|
|
address = services.xrayClient.serverAddress;
|
2023-07-27 19:32:51 +08:00
|
|
|
port = 443;
|
|
|
|
users =
|
|
|
|
[{
|
|
|
|
id = inputs.config.sops.placeholder."xray-client/uuid";
|
|
|
|
encryption = "none";
|
|
|
|
flow = "xtls-rprx-vision-udp443";
|
|
|
|
}];
|
2023-07-27 19:01:58 +08:00
|
|
|
}];
|
2023-07-27 19:32:51 +08:00
|
|
|
streamSettings =
|
2023-07-27 19:01:58 +08:00
|
|
|
{
|
2023-07-27 19:32:51 +08:00
|
|
|
network = "tcp";
|
2023-08-12 23:58:18 +08:00
|
|
|
security = "reality";
|
|
|
|
realitySettings =
|
2023-07-27 19:32:51 +08:00
|
|
|
{
|
2023-08-10 20:14:41 +08:00
|
|
|
serverName = services.xrayClient.serverName;
|
2023-08-12 23:58:18 +08:00
|
|
|
publicKey = "Nl0eVZoDF9d71_3dVsZGJl3UWR9LCv3B14gu7G6vhjk";
|
2023-07-27 19:32:51 +08:00
|
|
|
fingerprint = "firefox";
|
|
|
|
};
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
2023-07-27 19:32:51 +08:00
|
|
|
tag = "proxy-vless";
|
|
|
|
}
|
|
|
|
{ protocol = "freedom"; tag = "direct"; }
|
|
|
|
{ protocol = "dns"; tag = "dns-out"; }
|
|
|
|
{
|
|
|
|
protocol = "socks";
|
|
|
|
settings.servers = [{ address = "127.0.0.1"; port = 10069; }];
|
|
|
|
tag = "xmu-out";
|
|
|
|
}
|
2023-07-27 19:01:58 +08:00
|
|
|
];
|
2023-07-27 19:32:51 +08:00
|
|
|
routing =
|
|
|
|
{
|
2023-08-10 20:43:10 +08:00
|
|
|
domainStrategy = "AsIs";
|
2023-07-27 19:32:51 +08:00
|
|
|
rules = builtins.map (rule: rule // { type = "field"; })
|
|
|
|
[
|
|
|
|
{ inboundTag = [ "dns-in" ]; outboundTag = "dns-out"; }
|
|
|
|
{ inboundTag = [ "xmu-in" ]; outboundTag = "xmu-out"; }
|
|
|
|
{ inboundTag = [ "direct-in" ]; outboundTag = "direct"; }
|
|
|
|
{ inboundTag = [ "common-in" ]; domain = [ "geosite:geolocation-cn" ]; outboundTag = "direct"; }
|
|
|
|
{
|
|
|
|
inboundTag = [ "common-in" ];
|
|
|
|
domain = [ "geosite:geolocation-!cn" ];
|
|
|
|
outboundTag = "proxy-vless";
|
|
|
|
}
|
|
|
|
{ inboundTag = [ "common-in" "dns-internal" ]; ip = [ "geoip:cn" ]; outboundTag = "direct"; }
|
|
|
|
{ inboundTag = [ "common-in" "dns-internal" ]; outboundTag = "proxy-vless"; }
|
|
|
|
];
|
|
|
|
};
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
|
|
|
};
|
2023-08-10 20:14:41 +08:00
|
|
|
secrets."xray-client/uuid" = {};
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
2023-07-27 23:40:39 +08:00
|
|
|
systemd.services.xray =
|
2023-07-27 19:01:58 +08:00
|
|
|
{
|
2023-07-27 23:40:39 +08:00
|
|
|
serviceConfig =
|
2023-07-27 20:12:20 +08:00
|
|
|
{
|
2023-07-27 23:40:39 +08:00
|
|
|
DynamicUser = inputs.lib.mkForce false;
|
|
|
|
User = "v2ray";
|
|
|
|
Group = "v2ray";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
LimitNPROC = 10000;
|
|
|
|
LimitNOFILE = 1000000;
|
2023-07-27 20:12:20 +08:00
|
|
|
};
|
2023-07-27 23:40:39 +08:00
|
|
|
restartTriggers = [ inputs.config.sops.templates."xray-client.json".file ];
|
2023-07-27 19:01:58 +08:00
|
|
|
};
|
|
|
|
users = { users.v2ray = { isSystemUser = true; group = "v2ray"; }; groups.v2ray = {}; };
|
|
|
|
environment.etc."resolv.conf".text = "nameserver 127.0.0.1";
|
|
|
|
}
|
|
|
|
)
|
2023-08-10 19:41:33 +08:00
|
|
|
(
|
2023-08-12 01:12:13 +08:00
|
|
|
mkIf services.xrayServer.enable (let userList = genList (n: n) 30; in
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
|
|
|
services =
|
|
|
|
{
|
|
|
|
xray = { enable = true; settingsFile = inputs.config.sops.templates."xray-server.json".path; };
|
2023-08-12 23:58:18 +08:00
|
|
|
nginx.virtualHosts.xray =
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
2023-08-12 23:58:18 +08:00
|
|
|
serverName = services.xrayServer.serverName;
|
|
|
|
default = true;
|
|
|
|
listen = [{ addr = "127.0.0.1"; port = 7233; ssl = true; }];
|
|
|
|
useACMEHost = services.xrayServer.serverName;
|
2023-08-13 00:17:06 +08:00
|
|
|
onlySSL = true;
|
2023-08-12 23:58:18 +08:00
|
|
|
locations."/".return = "400";
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
|
|
|
};
|
2023-08-11 13:57:25 +08:00
|
|
|
sops =
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
|
|
|
templates."xray-server.json" =
|
|
|
|
{
|
2023-08-11 15:30:47 +08:00
|
|
|
owner = inputs.config.users.users.v2ray.name;
|
|
|
|
group = inputs.config.users.users.v2ray.group;
|
2023-08-10 19:41:33 +08:00
|
|
|
content = builtins.toJSON
|
|
|
|
{
|
|
|
|
log.loglevel = "warning";
|
|
|
|
inbounds =
|
|
|
|
[
|
|
|
|
{
|
|
|
|
port = 4726;
|
|
|
|
listen = "127.0.0.1";
|
|
|
|
protocol = "vless";
|
|
|
|
settings =
|
|
|
|
{
|
|
|
|
clients = map
|
|
|
|
(n:
|
|
|
|
{
|
|
|
|
id = inputs.config.sops.placeholder."xray-server/clients/user${toString n}";
|
|
|
|
flow = "xtls-rprx-vision";
|
|
|
|
email = "${toString n}@xray.chn.moe";
|
|
|
|
})
|
|
|
|
userList;
|
|
|
|
decryption = "none";
|
|
|
|
fallbacks = [{ dest = "127.0.0.1:7233"; }];
|
|
|
|
};
|
|
|
|
streamSettings =
|
|
|
|
{
|
|
|
|
network = "tcp";
|
2023-08-12 23:58:18 +08:00
|
|
|
security = "reality";
|
|
|
|
realitySettings =
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
2023-08-12 23:58:18 +08:00
|
|
|
dest = "127.0.0.1:7233";
|
|
|
|
serverNames = [ services.xrayServer.serverName ];
|
|
|
|
privateKey = inputs.config.sops.placeholder."xray-server/private-key";
|
2023-08-13 01:35:00 +08:00
|
|
|
minClientVer = "1.8.0";
|
2023-08-12 23:58:18 +08:00
|
|
|
shortIds = [ "" ];
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
|
|
|
};
|
2023-08-11 00:47:02 +08:00
|
|
|
sniffing = { enabled = true; destOverride = [ "http" "tls" "quic" ]; routeOnly = true; };
|
2023-08-10 19:41:33 +08:00
|
|
|
tag = "in";
|
|
|
|
}
|
2023-08-11 00:47:02 +08:00
|
|
|
{
|
|
|
|
port = 4638;
|
|
|
|
listen = "127.0.0.1";
|
|
|
|
protocol = "vless";
|
|
|
|
settings =
|
|
|
|
{
|
2023-08-11 02:34:52 +08:00
|
|
|
clients = [{ id = "be01f0a0-9976-42f5-b9ab-866eba6ed393"; }];
|
2023-08-11 00:47:02 +08:00
|
|
|
decryption = "none";
|
|
|
|
};
|
|
|
|
streamSettings.network = "tcp";
|
|
|
|
sniffing = { enabled = true; destOverride = [ "http" "tls" "quic" ]; };
|
|
|
|
tag = "in-localdns";
|
|
|
|
}
|
2023-08-11 12:52:20 +08:00
|
|
|
{
|
|
|
|
listen = "127.0.0.1";
|
|
|
|
port = 6149;
|
|
|
|
protocol = "dokodemo-door";
|
|
|
|
settings.address = "127.0.0.1";
|
|
|
|
tag = "api";
|
|
|
|
}
|
2023-08-11 00:47:02 +08:00
|
|
|
];
|
|
|
|
outbounds =
|
|
|
|
[
|
|
|
|
{ protocol = "freedom"; tag = "freedom"; }
|
|
|
|
{
|
|
|
|
protocol = "vless";
|
|
|
|
settings.vnext =
|
|
|
|
[{
|
|
|
|
address = "127.0.0.1";
|
|
|
|
port = 4638;
|
2023-08-11 02:34:52 +08:00
|
|
|
users = [{ id = "be01f0a0-9976-42f5-b9ab-866eba6ed393"; encryption = "none"; }];
|
2023-08-11 00:47:02 +08:00
|
|
|
}];
|
|
|
|
streamSettings.network = "tcp";
|
|
|
|
tag = "loopback-localdns";
|
|
|
|
}
|
2023-08-10 19:41:33 +08:00
|
|
|
];
|
2023-08-11 00:47:02 +08:00
|
|
|
routing =
|
|
|
|
{
|
|
|
|
domainStrategy = "AsIs";
|
|
|
|
rules = builtins.map (rule: rule // { type = "field"; })
|
|
|
|
[
|
|
|
|
{ inboundTag = [ "in" ]; domain = [ "domain:openai.com" ]; outboundTag = "loopback-localdns"; }
|
|
|
|
{ inboundTag = [ "in" ]; outboundTag = "freedom"; }
|
|
|
|
{ inboundTag = [ "in-localdns" ]; outboundTag = "freedom"; }
|
2023-08-11 12:52:20 +08:00
|
|
|
{ inboundTag = [ "api" ]; outboundTag = "api"; }
|
2023-08-11 00:47:02 +08:00
|
|
|
];
|
|
|
|
};
|
2023-08-11 12:52:20 +08:00
|
|
|
stats = {};
|
|
|
|
api = { tag = "api"; services = [ "StatsService" ]; };
|
|
|
|
policy =
|
|
|
|
{
|
|
|
|
levels."0" = { statsUserUplink = true; statsUserDownlink = true; };
|
|
|
|
system =
|
|
|
|
{
|
|
|
|
statsInboundUplink = true;
|
|
|
|
statsInboundDownlink = true;
|
|
|
|
statsOutboundUplink = true;
|
|
|
|
statsOutboundDownlink = true;
|
|
|
|
};
|
|
|
|
};
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
|
|
|
};
|
2023-08-11 13:57:25 +08:00
|
|
|
secrets = listToAttrs (map (n: { name = "xray-server/clients/user${toString n}"; value = {}; }) userList)
|
2023-08-11 15:30:47 +08:00
|
|
|
// (listToAttrs (map
|
|
|
|
(name:
|
|
|
|
{
|
|
|
|
name = "xray-server/telegram/${name}";
|
|
|
|
value =
|
|
|
|
{
|
|
|
|
owner = inputs.config.users.users.v2ray.name;
|
|
|
|
group = inputs.config.users.users.v2ray.group;
|
|
|
|
};
|
|
|
|
})
|
2023-08-12 23:58:18 +08:00
|
|
|
[ "token" "chat" ]))
|
|
|
|
// { "xray-server/private-key" = {}; };
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
2023-08-11 13:57:25 +08:00
|
|
|
systemd =
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
2023-08-11 13:57:25 +08:00
|
|
|
services =
|
2023-08-10 19:41:33 +08:00
|
|
|
{
|
2023-08-11 13:57:25 +08:00
|
|
|
xray =
|
|
|
|
{
|
|
|
|
serviceConfig =
|
|
|
|
{
|
|
|
|
DynamicUser = inputs.lib.mkForce false;
|
|
|
|
User = "v2ray";
|
|
|
|
Group = "v2ray";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN CAP_NET_BIND_SERVICE";
|
|
|
|
LimitNPROC = 10000;
|
|
|
|
LimitNOFILE = 1000000;
|
|
|
|
};
|
|
|
|
restartTriggers = [ inputs.config.sops.templates."xray-server.json".file ];
|
|
|
|
};
|
|
|
|
xray-stat =
|
|
|
|
{
|
|
|
|
script =
|
|
|
|
let
|
|
|
|
xray = "${inputs.pkgs.xray}/bin/xray";
|
2023-08-11 15:02:42 +08:00
|
|
|
awk = "${inputs.pkgs.gawk}/bin/awk";
|
2023-08-11 13:57:25 +08:00
|
|
|
curl = "${inputs.pkgs.curl}/bin/curl";
|
2023-08-12 00:58:28 +08:00
|
|
|
jq = "${inputs.pkgs.jq}/bin/jq";
|
|
|
|
sed = "${inputs.pkgs.gnused}/bin/sed";
|
|
|
|
cat = "${inputs.pkgs.coreutils}/bin/cat";
|
2023-08-11 13:57:25 +08:00
|
|
|
token = inputs.config.sops.secrets."xray-server/telegram/token".path;
|
|
|
|
chat = inputs.config.sops.secrets."xray-server/telegram/chat".path;
|
|
|
|
in stripeTabs
|
|
|
|
''
|
|
|
|
message='xray:\n'
|
|
|
|
for i in {0..${toString ((length userList) - 1)}}
|
|
|
|
do
|
2023-08-11 15:16:23 +08:00
|
|
|
upload_bytes=$(${xray} api stats --server=127.0.0.1:6149 \
|
2023-08-12 00:58:28 +08:00
|
|
|
-name "user>>>''${i}@xray.chn.moe>>>traffic>>>uplink" | ${jq} '.stat.value' | ${sed} 's/"//g')
|
2023-08-11 15:16:23 +08:00
|
|
|
[ -z "$upload_bytes" ] && upload_bytes=0
|
|
|
|
download_bytes=$(${xray} api stats --server=127.0.0.1:6149 \
|
2023-08-12 00:58:28 +08:00
|
|
|
-name "user>>>''${i}@xray.chn.moe>>>traffic>>>downlink" | ${jq} '.stat.value' | ${sed} 's/"//g')
|
2023-08-11 15:16:23 +08:00
|
|
|
[ -z "$download_bytes" ] && download_bytes=0
|
|
|
|
traffic_gb=$(echo | ${awk} "{printf \"%.3f\",(''${upload_bytes}+''${download_bytes})/1073741824}")
|
|
|
|
message="$message$i"'\t'"''${traffic_gb}"'G\n'
|
2023-08-11 13:57:25 +08:00
|
|
|
done
|
|
|
|
${curl} -X POST -H 'Content-Type: application/json' \
|
2023-08-12 00:58:28 +08:00
|
|
|
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"$message\"}" \
|
|
|
|
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
2023-08-11 13:57:25 +08:00
|
|
|
'';
|
|
|
|
serviceConfig = { Type = "oneshot"; User = "v2ray"; Group = "v2ray"; };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
timers.xray-stat =
|
|
|
|
{
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = { OnCalendar = "*-*-* 0:00:00"; Unit = "xray-stat.service"; };
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
users = { users.v2ray = { isSystemUser = true; group = "v2ray"; }; groups.v2ray = {}; };
|
|
|
|
nixos.services =
|
|
|
|
{
|
|
|
|
acme = { enable = true; certs = [ services.xrayServer.serverName ]; };
|
|
|
|
nginx.transparentProxy =
|
|
|
|
{
|
|
|
|
enable = true;
|
2023-08-12 23:58:18 +08:00
|
|
|
map."${services.xrayServer.serverName}" = 4726;
|
2023-08-10 19:41:33 +08:00
|
|
|
};
|
|
|
|
};
|
2023-08-12 23:58:18 +08:00
|
|
|
security.acme.certs.${services.xrayServer.serverName}.group = inputs.config.users.users.nginx.group;
|
2023-08-10 19:41:33 +08:00
|
|
|
}
|
2023-08-11 13:57:25 +08:00
|
|
|
))
|
2023-07-27 19:01:58 +08:00
|
|
|
{ networking.firewall.trustedInterfaces = services.firewall.trustedInterfaces; }
|
2023-08-04 20:07:57 +08:00
|
|
|
(
|
|
|
|
mkIf services.acme.enable
|
|
|
|
{
|
|
|
|
security.acme =
|
|
|
|
{
|
|
|
|
acceptTerms = true;
|
|
|
|
defaults.email = "chn@chn.moe";
|
|
|
|
certs = listToAttrs (map
|
|
|
|
(name:
|
|
|
|
{
|
|
|
|
name = name; value =
|
|
|
|
{
|
|
|
|
dnsProvider = "cloudflare";
|
|
|
|
credentialsFile = inputs.config.sops.secrets."acme/cloudflare.ini".path;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
services.acme.certs);
|
|
|
|
};
|
|
|
|
sops.secrets."acme/cloudflare.ini" = {};
|
|
|
|
}
|
|
|
|
)
|
2023-08-07 21:16:49 +08:00
|
|
|
(
|
|
|
|
mkIf (services.frpClient.enable)
|
|
|
|
{
|
|
|
|
systemd.services.frpc =
|
|
|
|
let
|
|
|
|
frpc = "${inputs.pkgs.frp}/bin/frpc";
|
|
|
|
config = inputs.config.sops.templates."frpc.ini";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
description = "Frp Client Service";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig =
|
|
|
|
{
|
|
|
|
Type = "simple";
|
|
|
|
User = "frp";
|
2023-08-08 22:52:38 +08:00
|
|
|
Restart = "always";
|
2023-08-07 21:16:49 +08:00
|
|
|
RestartSec = "5s";
|
|
|
|
ExecStart = "${frpc} -c ${config.path}";
|
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
};
|
|
|
|
wantedBy= [ "multi-user.target" ];
|
|
|
|
restartTriggers = [ config.file ];
|
|
|
|
};
|
|
|
|
sops =
|
|
|
|
{
|
|
|
|
templates."frpc.ini" =
|
|
|
|
{
|
2023-08-11 15:30:47 +08:00
|
|
|
owner = inputs.config.users.users.frp.name;
|
|
|
|
group = inputs.config.users.users.frp.group;
|
2023-08-07 21:16:49 +08:00
|
|
|
content = inputs.lib.generators.toINI {}
|
|
|
|
(
|
|
|
|
{
|
|
|
|
common =
|
|
|
|
{
|
|
|
|
server_addr = services.frpClient.serverName;
|
|
|
|
server_port = 7000;
|
|
|
|
token = inputs.config.sops.placeholder."frp/token";
|
|
|
|
user = services.frpClient.user;
|
|
|
|
tls_enable = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// (listToAttrs (map
|
|
|
|
(tcp:
|
|
|
|
{
|
|
|
|
name = tcp.name;
|
|
|
|
value =
|
|
|
|
{
|
|
|
|
type = "tcp";
|
|
|
|
local_ip = tcp.value.localIp;
|
|
|
|
local_port = tcp.value.localPort;
|
|
|
|
remote_port = tcp.value.remotePort;
|
|
|
|
use_compression = true;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(attrsToList services.frpClient.tcp))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
secrets."frp/token" = {};
|
|
|
|
};
|
|
|
|
users = { users.frp = { isSystemUser = true; group = "frp"; }; groups.frp = {}; };
|
|
|
|
}
|
|
|
|
)
|
|
|
|
(
|
|
|
|
mkIf (services.frpServer.enable)
|
|
|
|
{
|
|
|
|
systemd.services.frps =
|
|
|
|
let
|
|
|
|
frps = "${inputs.pkgs.frp}/bin/frps";
|
|
|
|
config = inputs.config.sops.templates."frps.ini";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
description = "Frp Server Service";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig =
|
|
|
|
{
|
|
|
|
Type = "simple";
|
|
|
|
User = "frp";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "5s";
|
|
|
|
ExecStart = "${frps} -c ${config.path}";
|
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
};
|
|
|
|
wantedBy= [ "multi-user.target" ];
|
|
|
|
restartTriggers = [ config.file ];
|
|
|
|
};
|
|
|
|
sops =
|
|
|
|
{
|
|
|
|
templates."frps.ini" =
|
|
|
|
{
|
2023-08-11 15:30:47 +08:00
|
|
|
owner = inputs.config.users.users.frp.name;
|
|
|
|
group = inputs.config.users.users.frp.group;
|
2023-08-07 21:16:49 +08:00
|
|
|
content = inputs.lib.generators.toINI {}
|
|
|
|
{
|
|
|
|
common = let cert = inputs.config.security.acme.certs.${services.frpServer.serverName}.directory; in
|
|
|
|
{
|
|
|
|
bind_port = 7000;
|
|
|
|
bind_udp_port = 7000;
|
|
|
|
token = inputs.config.sops.placeholder."frp/token";
|
2023-08-07 21:30:05 +08:00
|
|
|
tls_cert_file = "${cert}/full.pem";
|
|
|
|
tls_key_file = "${cert}/key.pem";
|
2023-08-07 21:16:49 +08:00
|
|
|
tls_only = true;
|
|
|
|
user_conn_timeout = 30;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
secrets."frp/token" = {};
|
|
|
|
};
|
|
|
|
nixos.services.acme = { enable = true; certs = [ services.frpServer.serverName ]; };
|
|
|
|
security.acme.certs.${services.frpServer.serverName}.group = "frp";
|
|
|
|
users = { users.frp = { isSystemUser = true; group = "frp"; }; groups.frp = {}; };
|
2023-08-07 21:30:05 +08:00
|
|
|
networking.firewall.allowedTCPPorts = [ 7000 ];
|
2023-08-07 21:16:49 +08:00
|
|
|
}
|
|
|
|
)
|
2023-08-07 21:44:46 +08:00
|
|
|
(
|
|
|
|
mkIf services.nix-serve.enable
|
|
|
|
{
|
|
|
|
services.nix-serve =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
openFirewall = true;
|
|
|
|
secretKeyFile = inputs.config.sops.secrets."store/signingKey".path;
|
|
|
|
};
|
|
|
|
sops.secrets."store/signingKey" = {};
|
|
|
|
}
|
|
|
|
)
|
2023-08-10 10:15:21 +08:00
|
|
|
(mkIf services.smartd.enable { services.smartd.enable = true; })
|
2023-08-10 17:50:08 +08:00
|
|
|
(
|
|
|
|
mkIf services.nginx.transparentProxy.enable
|
|
|
|
{
|
2023-08-13 02:30:44 +08:00
|
|
|
services =
|
2023-08-10 17:50:08 +08:00
|
|
|
{
|
2023-08-13 02:30:44 +08:00
|
|
|
nginx =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
streamConfig = stripeTabs
|
|
|
|
''
|
2023-08-13 02:53:28 +08:00
|
|
|
geoip_country ${inputs.config.services.geoipupdate.settings.DatabaseDirectory}/GeoLite2-Country.mmdb;
|
2023-08-13 02:30:44 +08:00
|
|
|
log_format stream '[$time_local] $remote_addr-$geoip_country_code "$ssl_preread_server_name"->$backend $bytes_sent $bytes_received';
|
|
|
|
access_log syslog:server=unix:/dev/log stream;
|
|
|
|
map $ssl_preread_server_name $backend
|
|
|
|
{
|
|
|
|
${concatStringsSep "\n" (map
|
|
|
|
(x: '' "${x.name}" 127.0.0.1:${toString x.value};'')
|
|
|
|
(attrsToList services.nginx.transparentProxy.map))}
|
|
|
|
default 127.0.0.1:443;
|
|
|
|
}
|
|
|
|
server
|
|
|
|
{
|
|
|
|
listen ${services.nginx.transparentProxy.externalIp}:443;
|
|
|
|
ssl_preread on;
|
|
|
|
proxy_bind $remote_addr transparent;
|
|
|
|
proxy_pass $backend;
|
|
|
|
proxy_connect_timeout 1s;
|
|
|
|
proxy_socket_keepalive on;
|
|
|
|
proxy_buffer_size 128k;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
recommendedZstdSettings = true;
|
|
|
|
recommendedTlsSettings = true;
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
recommendedOptimisation = true;
|
|
|
|
recommendedGzipSettings = true;
|
|
|
|
recommendedBrotliSettings = true;
|
|
|
|
package = inputs.pkgs.nginxMainline;
|
|
|
|
};
|
|
|
|
geoipupdate =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
settings =
|
2023-08-10 17:53:48 +08:00
|
|
|
{
|
2023-08-13 02:30:44 +08:00
|
|
|
AccountID = 901296;
|
|
|
|
LicenseKey = inputs.config.sops.secrets."nginx/maxmind-license".path;
|
|
|
|
EditionIDs = [ "GeoLite2-ASN" "GeoLite2-City" "GeoLite2-Country" ];
|
|
|
|
};
|
|
|
|
};
|
2023-08-10 17:50:08 +08:00
|
|
|
};
|
2023-08-13 02:30:44 +08:00
|
|
|
sops.secrets."nginx/maxmind-license".owner = inputs.config.users.users.nginx.name;
|
2023-08-10 20:04:32 +08:00
|
|
|
systemd.services =
|
|
|
|
{
|
|
|
|
nginx-proxy =
|
|
|
|
let
|
|
|
|
ipset = "${inputs.pkgs.ipset}/bin/ipset";
|
|
|
|
iptables = "${inputs.pkgs.iptables}/bin/iptables";
|
|
|
|
ip = "${inputs.pkgs.iproute}/bin/ip";
|
|
|
|
start = inputs.pkgs.writeShellScript "nginx-proxy.start"
|
2023-08-10 17:50:08 +08:00
|
|
|
(
|
2023-08-10 20:04:32 +08:00
|
|
|
(
|
|
|
|
stripeTabs
|
|
|
|
''
|
|
|
|
${ipset} create nginx_proxy_port bitmap:port range 0-65535
|
|
|
|
${iptables} -t mangle -N nginx_proxy_mark
|
|
|
|
${iptables} -t mangle -A OUTPUT -j nginx_proxy_mark
|
|
|
|
${iptables} -t mangle -A nginx_proxy_mark -s 127.0.0.1 -p tcp \
|
|
|
|
-m set --match-set nginx_proxy_port src -j MARK --set-mark 2/2
|
|
|
|
${iptables} -t mangle -N nginx_proxy
|
|
|
|
${iptables} -t mangle -A PREROUTING -j nginx_proxy
|
|
|
|
${iptables} -t mangle -A nginx_proxy -s 127.0.0.1 -p tcp \
|
|
|
|
-m set --match-set nginx_proxy_port src -j MARK --set-mark 2/2
|
|
|
|
${ip} rule add fwmark 2/2 table 200
|
|
|
|
${ip} route add local 0.0.0.0/0 dev lo table 200
|
|
|
|
''
|
|
|
|
)
|
|
|
|
+ concatStringsSep "\n" (map
|
|
|
|
(port: ''${ipset} add nginx_proxy_port ${toString port}'')
|
2023-08-12 23:58:18 +08:00
|
|
|
((attrValues services.nginx.transparentProxy.map) ++ [ 443 ]) )
|
2023-08-10 20:04:32 +08:00
|
|
|
);
|
|
|
|
stop = inputs.pkgs.writeShellScript "nginx-proxy.stop" (stripeTabs
|
|
|
|
''
|
|
|
|
${iptables} -t mangle -F nginx_proxy_mark
|
|
|
|
${iptables} -t mangle -D OUTPUT -j nginx_proxy_mark
|
|
|
|
${iptables} -t mangle -X nginx_proxy_mark
|
|
|
|
${iptables} -t mangle -F nginx_proxy
|
|
|
|
${iptables} -t mangle -D PREROUTING -j nginx_proxy
|
|
|
|
${iptables} -t mangle -X nginx_proxy
|
|
|
|
${ip} rule del fwmark 2/2 table 200
|
|
|
|
${ip} route del local 0.0.0.0/0 dev lo table 200
|
|
|
|
${ipset} destroy nginx_proxy_port
|
|
|
|
'');
|
|
|
|
in
|
2023-08-10 17:50:08 +08:00
|
|
|
{
|
2023-08-10 20:04:32 +08:00
|
|
|
description = "nginx transparent proxy";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig =
|
|
|
|
{
|
|
|
|
Type = "simple";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStart = start;
|
|
|
|
ExecStop = stop;
|
|
|
|
};
|
|
|
|
wants = [ "network.target" ];
|
|
|
|
wantedBy= [ "multi-user.target" ];
|
2023-08-10 17:50:08 +08:00
|
|
|
};
|
2023-08-10 20:04:32 +08:00
|
|
|
nginx.serviceConfig =
|
|
|
|
{
|
|
|
|
CapabilityBoundingSet = [ "CAP_NET_ADMIN" ];
|
|
|
|
AmbientCapabilities = [ "CAP_NET_ADMIN" ];
|
2023-08-10 17:50:08 +08:00
|
|
|
};
|
2023-08-10 20:04:32 +08:00
|
|
|
};
|
2023-08-10 19:41:33 +08:00
|
|
|
networking.firewall.allowedTCPPorts = [ 443 ];
|
2023-08-10 17:50:08 +08:00
|
|
|
}
|
|
|
|
)
|
2023-08-12 11:58:01 +08:00
|
|
|
(
|
|
|
|
mkIf (services.nginx.httpProxy != {})
|
|
|
|
{
|
|
|
|
services.nginx.virtualHosts = listToAttrs (map
|
|
|
|
(site:
|
|
|
|
{
|
|
|
|
inherit (site) name;
|
|
|
|
value =
|
|
|
|
{
|
|
|
|
serverName = site.name;
|
2023-08-13 02:42:24 +08:00
|
|
|
listen = [{ addr = "127.0.0.1"; port = 443; ssl = true; } { addr = "0.0.0.0"; port = 80; } ];
|
2023-08-12 11:58:01 +08:00
|
|
|
useACMEHost = site.name;
|
|
|
|
locations."/".proxyPass = site.value.upstream;
|
|
|
|
forceSSL = site.value.rewriteHttps;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(attrsToList services.nginx.httpProxy));
|
|
|
|
nixos.services =
|
|
|
|
{
|
|
|
|
nginx.transparentProxy.enable = true;
|
|
|
|
acme =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
certs = attrNames services.nginx.httpProxy;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
security.acme.certs = listToAttrs (map
|
|
|
|
(cert: { name = cert; value.group = inputs.config.services.nginx.group; })
|
|
|
|
(attrNames services.nginx.httpProxy));
|
|
|
|
}
|
|
|
|
)
|
2023-08-13 20:53:42 +08:00
|
|
|
(
|
|
|
|
mkIf services.send.enable
|
|
|
|
{
|
|
|
|
virtualisation.oci-containers.containers.send =
|
|
|
|
{
|
|
|
|
image = "registry.gitlab.com/timvisee/send"
|
|
|
|
+ "@sha256:1ee495161f176946e6e4077e17be2b8f8634c2d502172cc530a8cd5affd7078f";
|
|
|
|
volumes = [ "/var/lib/send:/uploads" ];
|
|
|
|
ports = [ "127.0.0.1::1443" ];
|
|
|
|
extraOptions = [ "--add-host=host.docker.internal:host-gateway" ];
|
|
|
|
environmentFiles = [ inputs.config.sops.templates."send/env".path ];
|
|
|
|
};
|
|
|
|
sops =
|
|
|
|
{
|
|
|
|
templates."send/env".content = stripeTabs
|
|
|
|
''
|
|
|
|
BASE_URL = "https://send.chn.moe"
|
|
|
|
REDIS_HOST = "host.docker.internal"
|
|
|
|
REDIS_PORT = "7116"
|
|
|
|
REDIS_PASSWORD = "${inputs.config.sops.placeholder."send/redis-password"}"
|
|
|
|
'';
|
|
|
|
secrets."send/redis-password".owner = inputs.config.users.users.redis-send.name;
|
|
|
|
};
|
|
|
|
services.redis.servers.send =
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
unixSocket = null;
|
|
|
|
port = 7116;
|
|
|
|
requirePassFile = inputs.config.sops.secrets."send/redis-password".path;
|
|
|
|
};
|
2023-08-13 21:08:05 +08:00
|
|
|
nixos.services.nginx.httpProxy."send.chn.moe" =
|
2023-08-13 20:53:42 +08:00
|
|
|
{
|
2023-08-13 21:02:56 +08:00
|
|
|
upstream = "http://127.0.0.1:1443";
|
2023-08-13 20:53:42 +08:00
|
|
|
rewriteHttps = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
2023-07-26 21:05:46 +08:00
|
|
|
];
|
2023-07-25 23:33:37 +08:00
|
|
|
}
|