feat: allow using suite on non-nixos systems

This commit is contained in:
FUFSoB committed 2026-09-17 13:27:08 +05:00
1 parent e852bef400
commit 3b568bb54e
63 files changed
+3572 -523

No files matched your search

@@ -12,10 +12,10 @@
mkSubscriptionFetchBlock,
runtimeSubscriptionsFetchBlock,
jq,
systemctl,
}:
let
systemctl = "${pkgs.systemd}/bin/systemctl";
curl = "${pkgs.curl}/bin/curl";
restartActiveBlock = lib.concatMapStrings (svc: ''
+3
View File
@@ -63,6 +63,9 @@
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
autoProxyEnabled = if proxyCfg.autoProxy.enable then "1" else "0";
autoProxyStateDir = constants.autoProxyStateDir;
inherit (constants) stateDir runtimeDir serviceManager;
privileged = if constants.privileged then "1" else "0";
supervisorCtl = if constants.serviceManager == "supervisor" then constants.systemctl else "";
singBox = "${singBoxCfg.package}/bin/sing-box";
localProxyUrl = "http://${
if proxyCfg.listener.address == "0.0.0.0" then "127.0.0.1" else proxyCfg.listener.address
+92 -98
View File
@@ -1,7 +1,6 @@
# Assembles proxy-suite systemd services from the sub-modules.
# Assembles proxy-suite services from the sub-modules, as host-neutral declarations
# under services.proxy-suite.internal (see ../hosts).
{
config,
options,
lib,
pkgs,
packages,
@@ -130,60 +129,51 @@ let
import ../autoproxy.nix {
inherit lib pkgs cfg;
inherit (control) proxyCtl;
inherit (constants) autoProxyStateDir;
inherit (constants) autoProxyStateDir runtimeDir journalctl;
inherit userControlAllows;
}
);
in
lib.mkMerge [
autoProxyUnits
# The GUI's pkexec needs its setuid wrapper, off by default since nixpkgs 26.11 (and
# always there before, where the option does not exist).
(lib.optionalAttrs (options.security.polkit ? enablePkexecWrapper) {
security.polkit.enablePkexecWrapper = lib.mkIf (cfg.enable && cfg.gui.enable) true;
})
{
# See constants.serviceUser.
users.users.${constants.serviceUser} = {
isSystemUser = true;
group = constants.serviceUser;
description = "proxy-suite daemons";
};
users.groups.${constants.serviceUser} = { };
}
{
environment.systemPackages = [
control.proxyCtl
]
++ lib.optional cfg.tui.enable control.proxyCtl.tui
++ lib.optional cfg.gui.enable control.proxyCtl.gui;
services.proxy-suite.internal = {
# The GUI's pkexec needs its setuid wrapper.
polkit.pkexecWrapper = lib.mkIf (cfg.enable && cfg.gui.enable) true;
# See constants.serviceUser.
systemUsers = [ constants.serviceUser ];
# nftables must be on for transparent routing backends. Global TUN uses
# SingBox auto_redirect programs an `inet sing-box` nftables table.
networking.nftables.enable = lib.mkIf (
globalTun.enable
|| globalTproxy.enable
|| perAppRoutingTun.enable
|| perAppRoutingTproxy.enable
|| perAppZapretEnabled
) (lib.mkDefault true);
packages = [
control.proxyCtl
]
++ lib.optional cfg.tui.enable control.proxyCtl.tui
++ lib.optional cfg.gui.enable control.proxyCtl.gui;
# Inbound listeners are reached from outside, so their ports have to be open.
networking.firewall = lib.mkIf (proxyInboundsEnabled && cfg.inbounds.openFirewall) {
allowedTCPPorts = proxyInboundFirewallPorts;
allowedUDPPorts = proxyInboundFirewallUdpPorts;
};
# nftables must be on for transparent routing backends. Global TUN uses
# SingBox auto_redirect programs an `inet sing-box` nftables table.
nftables = lib.mkIf (
globalTun.enable
|| globalTproxy.enable
|| perAppRoutingTun.enable
|| perAppRoutingTproxy.enable
|| perAppZapretEnabled
) true;
users.groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) {
"${userControlCfg.group}" = { };
};
# Inbound listeners are reached from outside, so their ports have to be open.
firewall = lib.mkIf (proxyInboundsEnabled && cfg.inbounds.openFirewall) {
allowedTCPPorts = proxyInboundFirewallPorts;
allowedUDPPorts = proxyInboundFirewallUdpPorts;
};
security.polkit.enable = lib.mkIf (cfg.enable && (userControlEnabled || cfg.gui.enable)) true;
# The GUI's "Retry as Root" and root toggle run proxy-ctl through pkexec: one
# admin password then covers the next few minutes, as sudo's does in the TUI.
security.polkit.extraConfig = lib.mkMerge [
(lib.mkIf (cfg.enable && cfg.gui.enable) (
lib.mkAfter ''
groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) [
userControlCfg.group
];
polkit.enable = lib.mkIf (cfg.enable && (userControlEnabled || cfg.gui.enable)) true;
# The GUI's "Retry as Root" and root toggle run proxy-ctl through pkexec: one
# admin password then covers the next few minutes, as sudo's does in the TUI.
polkit.rules = lib.mkMerge [
(lib.mkIf (cfg.enable && cfg.gui.enable) ''
polkit.addRule(function(action, subject) {
if (action.id === "org.freedesktop.policykit.exec" &&
action.lookup("program") === "${control.proxyCtl}/bin/proxy-ctl") {
@@ -191,10 +181,8 @@ lib.mkMerge [
}
return null;
});
''
))
(lib.mkIf (cfg.enable && userControlEnabled) (
lib.mkAfter ''
'')
(lib.mkIf (cfg.enable && userControlEnabled) ''
polkit.addRule(function(action, subject) {
if (!subject.isInGroup("${userControlCfg.group}")) {
return null;
@@ -209,50 +197,60 @@ lib.mkMerge [
return null;
});
''
))
];
'')
];
# Spool dirs for outbounds and subscriptions added at runtime. Setgid so the
# files proxy-ctl drops here inherit the group; without the outbounds scope
# only root writes them. tmpfiles rather than the start scripts, so the group can add an
# outbound before the proxy has ever run.
systemd.tmpfiles.rules = lib.mkIf (cfg.enable && proxyEnabled) (
map
(
dir:
"d ${dir} ${
if userControlAllows "outbounds" then "2770 root ${userControlCfg.group}" else "0700 root root"
} -"
)
[
constants.runtimeOutboundsDir
constants.runtimeSubscriptionsDir
]
);
# Spool dirs for outbounds and subscriptions added at runtime. Setgid so the
# files proxy-ctl drops here inherit the group; without the outbounds scope
# only root writes them. tmpfiles rather than the start scripts, so the group can add an
# outbound before the proxy has ever run.
tmpfiles = lib.mkIf (cfg.enable && proxyEnabled) (
map
(
dir:
"d ${dir} ${
if userControlAllows "outbounds" then
"2770 root ${userControlCfg.group}"
else if constants.privileged then
"0700 root root"
else
"0700 - -"
} -"
)
[
constants.runtimeOutboundsDir
constants.runtimeSubscriptionsDir
]
);
systemd.user.services = lib.mkMerge [
(mkNamedUnits userServiceEntries)
(lib.mkIf (cfg.gui.enable && cfg.gui.autostart) {
# Starts hidden in the tray with every graphical session.
proxy-suite-gui = {
description = "Proxy Suite GUI (tray icon)";
wantedBy = [ "graphical-session.target" ];
partOf = [ "graphical-session.target" ];
after = [ "graphical-session.target" ];
unitConfig.ConditionEnvironment = [
"|WAYLAND_DISPLAY"
"|DISPLAY"
];
serviceConfig = {
ExecStart = "${control.proxyCtl.gui}/bin/proxy-suite-gui --hidden";
Restart = "on-failure";
RestartSec = 3;
userServices = lib.mkMerge [
(mkNamedUnits userServiceEntries)
(lib.mkIf (cfg.gui.enable && cfg.gui.autostart) {
# Starts hidden in the tray with every graphical session.
proxy-suite-gui = {
description = "Proxy Suite GUI (tray icon)";
wantedBy = [ "graphical-session.target" ];
partOf = [ "graphical-session.target" ];
after = [ "graphical-session.target" ];
unitConfig.ConditionEnvironment = [
"|WAYLAND_DISPLAY"
"|DISPLAY"
];
serviceConfig = {
ExecStart = "${control.proxyCtl.gui}/bin/proxy-suite-gui --hidden";
Restart = "on-failure";
RestartSec = 3;
};
};
};
})
];
})
];
services = mkNamedUnits systemServiceEntries;
timers = mkNamedUnits timerEntries;
};
}
{
assertions = import ../service-assertions.nix {
inherit lib cfg derived;
tgWsProxyCfg = cfg.tgWsProxy;
@@ -276,10 +274,6 @@ lib.mkMerge [
"proxy-suite: no outbounds or subscriptions are declared; the proxy will not start"
+ " until one is added with `proxy-ctl proxy outbounds add`"
);
systemd.services = mkNamedUnits systemServiceEntries;
systemd.timers = mkNamedUnits timerEntries;
}
# A certificate or key XRay cannot read is copied in at start (proxy-inbounds-scripts.nix),
@@ -298,20 +292,20 @@ lib.mkMerge [
);
in
lib.mkIf (cfg.enable && proxyInboundsEnabled && certFiles != [ ]) {
systemd.paths.proxy-suite-inbounds-certs = {
services.proxy-suite.internal.paths.proxy-suite-inbounds-certs = {
description = "proxy-suite - watch the inbounds' TLS certificates for renewals";
wantedBy = [ "paths.target" ];
pathConfig.PathChanged = certFiles;
};
systemd.services.proxy-suite-inbounds-certs = {
services.proxy-suite.internal.services.proxy-suite-inbounds-certs = {
description = "proxy-suite - restart the inbounds onto a renewed certificate";
serviceConfig.Type = "oneshot";
script = ''
# XRay reloads the files it reads itself; only copies go stale.
[ -d /run/proxy-suite-inbounds/tls ] || exit 0
[ -d ${constants.runtimeDir}/proxy-suite-inbounds/tls ] || exit 0
# A renewal writes the chain and the key one after the other.
sleep 10
${pkgs.systemd}/bin/systemctl try-restart proxy-suite-inbounds.service
${constants.systemctl} try-restart proxy-suite-inbounds.service
'';
};
}
@@ -32,6 +32,7 @@ let
perAppZapretSliceName = "proxy-suite-per-app-zapret.slice";
profiles = import ./per-app-routing/profiles.nix {
inherit (constants) runtimeDir;
inherit
lib
pkgs
@@ -7,6 +7,7 @@
perAppRoutingTun,
perAppRoutingTproxy,
perAppZapretCfg,
runtimeDir,
}:
let
@@ -55,7 +56,7 @@ let
proxychainsConfigFile =
if localProxyAuthEnabled then
"/run/proxy-suite-socks/proxychains.conf"
"${runtimeDir}/proxy-suite-socks/proxychains.conf"
else
pkgs.writeText "proxy-suite-per-app" ''
strict_chain
@@ -24,7 +24,7 @@
}:
let
runtimeDir = "/run/proxy-suite-inbounds";
runtimeDir = "${constants.runtimeDir}/proxy-suite-inbounds";
linksFile = "${runtimeDir}/links.json";
subscriptionsFile = "${runtimeDir}/subscriptions.json";
subsCfg = proxyInboundsCfg.subscriptions;
@@ -108,8 +108,9 @@ let
printf '%s' "$body" > "$SUB_DIR.new/$token"
done
chmod -R u=rwX,g=rX,o= "$SUB_DIR.new"
${pkgs.coreutils}/bin/chgrp -R ${lib.escapeShellArg subsCfg.group} "$SUB_DIR.new" ||
echo "proxy-suite: group ${subsCfg.group} cannot be given the subscriptions; they stay root-only" >&2
${constants.ifPrivileged ''
${pkgs.coreutils}/bin/chgrp -R ${lib.escapeShellArg subsCfg.group} "$SUB_DIR.new" ||
echo "proxy-suite: group ${subsCfg.group} cannot be given the subscriptions; they stay root-only" >&2''}
rm -rf "$SUB_DIR"
mv "$SUB_DIR.new" "$SUB_DIR"
${jq} -c '[.subscriptions[] | {user, token}]' <<< "$RENDERED" > "${subscriptionsFile}"
@@ -163,10 +164,10 @@ let
while IFS= read -r source; do
[ -n "$source" ] || continue
if ! ${runXray} ${pkgs.coreutils}/bin/test -r "$source"; then
[ -d "$CERT_DIR" ] || install -d -m 0750 -g ${constants.serviceUser} "$CERT_DIR"
[ -d "$CERT_DIR" ] || install -d -m 0750 ${constants.ifPrivileged "-g ${constants.serviceUser} "}"$CERT_DIR"
index=$((index + 1))
copy="$CERT_DIR/$index-$(basename "$source")"
install -m 0640 -g ${constants.serviceUser} "$source" "$copy"
install -m 0640 ${constants.ifPrivileged "-g ${constants.serviceUser} "}"$source" "$copy"
echo "proxy-suite: ${constants.serviceUser} cannot read $source; XRay uses a copy until the next restart" >&2
${jq} --arg from "$source" --arg to "$copy" '
(.inbounds[].streamSettings.tlsSettings.certificates[]? | (.certificateFile, .keyFile)
@@ -187,7 +188,7 @@ let
''
else
''
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"''}
chmod 640 "$RUNTIME_DIR/config.json.tmp"
''
}
@@ -25,13 +25,13 @@ lib.optionalString hybridEnabled ''
local batch
batch=$(${jq} -c \
--argjson base "$XRAY_SIDECAR_NEXT_PORT" \
--argjson mark ${toString xraySidecarRoutingMark} '
--argjson mark ${if xraySidecarRoutingMark == null then "null" else toString xraySidecarRoutingMark} '
to_entries | map(
($base + .key) as $port
| ("proxy-suite-xray-" + ($port | tostring)) as $auth
| .value.tag as $tag
| {outbound: (.value
| .streamSettings.sockopt.mark = $mark
${lib.optionalString (xraySidecarRoutingMark != null) "| .streamSettings.sockopt.mark = $mark"}
| .streamSettings.sockopt.domainStrategy = (.streamSettings.sockopt.domainStrategy // "UseIP")),
inbound: {tag: ($tag + "-inbound"), listen: "127.0.0.1", port: $port, protocol: "socks",
settings: {auth: "password", udp: true, accounts: [{user: $auth, pass: $auth}]}},
@@ -160,7 +160,9 @@ let
}
_proxy_suite_parse_xray_url() {
${parse "--backend xray" " --routing-mark ${toString xraySidecarRoutingMark}"}
${parse "--backend xray" (
lib.optionalString (xraySidecarRoutingMark != null) " --routing-mark ${toString xraySidecarRoutingMark}"
)}
}
''
else
@@ -331,7 +333,7 @@ let
# sing-box opens the key as ${constants.serviceUser}, which cannot read a key in a
# home directory: it gets a copy only root and its group can read.
SSH_IDENTITY="$RUNTIME_DIR/ssh-identity"
install -m 0640 -g ${constants.serviceUser} ${lib.escapeShellArg sshProxyCfg.identityFile} "$SSH_IDENTITY"
install -m 0640 ${constants.ifPrivileged "-g ${constants.serviceUser} "}${lib.escapeShellArg sshProxyCfg.identityFile} "$SSH_IDENTITY"
OB_JSON=$(${jq} --arg key "$SSH_IDENTITY" '.private_key_path = $key' <<< "$OB_JSON")
''}
${hostKeyFileBlock}
@@ -357,7 +359,7 @@ let
_proxy_suite_record_tag_source ${lib.escapeShellArg outbound.tag} ${source}
'';
# WARP and "singBox" AmneziaWG profiles run in a tunnel unit, which restarts them when the
# WARP and "singBox" or "userspace" AmneziaWG profiles run in a tunnel unit, which restarts them when the
# handshake stops being answered. Every backend reaches it as a loopback SOCKS hop.
mkTunnelOutboundBlock =
unit: source: tag: port:
@@ -10,6 +10,7 @@
hybridEnabled,
mainBackend,
backend,
stateDir,
runtimeSubscriptionsDir,
jq,
python3,
@@ -21,7 +22,7 @@
let
subscriptionBackend = if hybridEnabled then "hybrid" else mainBackend;
subscriptionBackendArg = "--backend ${subscriptionBackend}";
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions/${backend}";
subscriptionCacheDir = "${stateDir}/subscriptions/${backend}";
mkSubscriptionUrlSource =
sub:
+6 -4
View File
@@ -56,7 +56,7 @@ let
pkgs.writeText "proxy-suite-core" localProxyAuth.password
else
null;
routeModeStateFile = "/run/proxy-suite/route-mode";
routeModeStateFile = "${constants.runtimeDir}/proxy-suite/route-mode";
inherit (constants)
pinnedOutboundFile
runtimeOutboundsDir
@@ -64,9 +64,9 @@ let
outboundInventoryFile
;
clashApi = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
xrayLoglevelFile = "/run/proxy-suite/xray-loglevel";
runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf";
xraySidecarRoutingMark = globalTproxy.proxyMark;
xrayLoglevelFile = "${constants.runtimeDir}/proxy-suite/xray-loglevel";
runtimeProxychainsConfig = "${constants.runtimeDir}/proxy-suite-socks/proxychains.conf";
xraySidecarRoutingMark = if constants.privileged then globalTproxy.proxyMark else null;
routingMarkJq =
routingMark:
@@ -78,6 +78,7 @@ let
" | .routing_mark = ${toString routingMark}";
subscriptionScripts = import ./script-blocks/subscriptions.nix {
inherit (constants) stateDir;
inherit
lib
pkgs
@@ -220,6 +221,7 @@ let
proxyInboundsSubscriptionsFile = proxyInboundsScripts.subscriptionsFile;
controlScripts = import ./control-scripts.nix {
inherit (constants) systemctl;
inherit
lib
pkgs
+10 -9
View File
@@ -42,7 +42,7 @@ let
autoProxyRender = import ../autoproxy-render.nix {
inherit pkgs;
inherit (constants) serviceUser;
inherit (constants) serviceUser ifPrivileged;
};
runBackend = constants.runAsServiceUser pkgs constants.backendCaps;
userControlGroup = lib.escapeShellArg userControlCfg.group;
@@ -136,7 +136,7 @@ let
${lib.escapeShellArg localProxyAuth.username} \
"$LOCAL_PROXY_PASSWORD"
} > "${runtimeProxychainsConfig}"
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${runtimeProxychainsConfig}"
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${runtimeProxychainsConfig}"''}
chmod 640 "${runtimeProxychainsConfig}"
'';
@@ -258,7 +258,7 @@ let
${
if userControlCfg.enable || localProxyAuthEnabled then
''
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$ENDPOINTS_TMP"
${constants.ifPrivileged ''${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$ENDPOINTS_TMP"''}
chmod 640 "$ENDPOINTS_TMP"
''
else
@@ -358,14 +358,14 @@ let
''
else
''
${chgrp} ${constants.serviceUser} "$backend_config"
${constants.ifPrivileged ''${chgrp} ${constants.serviceUser} "$backend_config"''}
chmod 640 "$backend_config"
''
}
done
FAKE_IP_CACHE=$(${jq} -r '.experimental.cache_file.path? // empty' "$RUNTIME_DIR/config.json")
if [ -n "$FAKE_IP_CACHE" ]; then
install -d -m 0700 -o ${constants.serviceUser} -g ${constants.serviceUser} "$(dirname "$FAKE_IP_CACHE")"
install -d -m 0700 ${constants.ifPrivileged "-o ${constants.serviceUser} -g ${constants.serviceUser} "}"$(dirname "$FAKE_IP_CACHE")"
fi
${
@@ -409,9 +409,10 @@ let
'';
startSocks = mkStartScript {
runtimeDir = "/run/proxy-suite-socks";
runtimeDir = "${constants.runtimeDir}/proxy-suite-socks";
configFile = tproxyFile;
routingMark = globalTproxy.proxyMark;
# Rootless hosts cannot set SO_MARK, and have no TProxy rules to escape anyway.
routingMark = if constants.privileged then globalTproxy.proxyMark else null;
enableLocalProxyAuth = localProxyAuthEnabled;
# TProxy takes the system resolver's own upstream queries too, so a proxy server's name
# has to resolve inside XRay, as under the TUN.
@@ -425,7 +426,7 @@ let
};
startTun = mkStartScript {
runtimeDir = "/run/proxy-suite-tun";
runtimeDir = "${constants.runtimeDir}/proxy-suite-tun";
configFile = tunFile;
routingMark = if pureXrayEnabled then globalTproxy.proxyMark else null;
xrayTunDnsRuntime = pureXrayEnabled;
@@ -436,7 +437,7 @@ let
};
startPerAppTun = mkStartScript {
runtimeDir = "/run/proxy-suite-per-app-tun";
runtimeDir = "${constants.runtimeDir}/proxy-suite-per-app-tun";
configFile = perAppTunFile;
routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null;
xrayTunDnsRuntime = pureXrayEnabled;