feat: helper functions; docs: more complex examples

This commit is contained in:
fufsob committed 2026-09-25 11:00:55 +00:00
1 parent 63d9dffcae
commit 878983147c
20 files changed
+1046 -19

No files matched your search

+35
View File
@@ -36,6 +36,41 @@ in
config = lib.mkMerge [
{
services.proxy-suite.internal.helpers =
let
inherit (cfg.proxy.listener) address port auth;
perAppRouting = assembly.context.perAppRouting;
perAppOn = cfg.enable && cfg.perAppRouting.enable;
in
import ./helpers.nix {
inherit
lib
pkgs
address
port
;
inherit (auth) username password;
envUnavailable =
if !(cfg.enable && cfg.proxy.enable) then
"the proxy variables need services.proxy-suite.proxy.enable"
else if auth.passwordFile != null then
"the proxy variables cannot carry proxy.listener.auth.passwordFile; use wrapProxychains"
else
null;
proxychains =
if perAppOn && cfg.perAppRouting.proxychains.enable then
{ args = "${perAppRouting.proxychainsQuietArg}-f ${toString perAppRouting.proxychainsConfigFile}"; }
else
"wrapProxychains needs perAppRouting.proxychains.enable";
perApp =
if perAppOn then
{
inherit (assembly.context.control) proxyCtl;
profiles = perAppRouting.effectivePerAppRoutingProfileNames;
}
else
"wrapPerApp needs perAppRouting.enable";
};
# xray and sing-box default to this flake's own nixpkgs (redirect it
# with inputs.nixpkgs.follows), not the system's: a stable release lags
# both by months, and current xray needs a newer Go than stable ships.
+153
View File
@@ -0,0 +1,153 @@
# Ways to point programs at the local proxy without touching their settings: proxy
# variables, a proxychains wrapper, or a per-app routing profile. The module exposes them
# as config.lib.proxy-suite on NixOS and home-manager; the flake's lib.proxyHelpers builds
# the variables alone, for a proxy this config does not run or a host without config.lib.
{
lib,
pkgs,
address ? "127.0.0.1",
port ? 1080,
username ? null,
password ? null,
# Set when the variables cannot name the proxy (it is off, or its password is a file).
envUnavailable ? null,
# From the module only: { args } for proxychains4, and { proxyCtl, profiles } for
# per-app routing. A string instead says why the feature is unavailable.
proxychains ? "wrapProxychains needs the proxy-suite module",
perApp ? "wrapPerApp needs the proxy-suite module",
}:
let
# A wildcard listener is reached on loopback.
host =
{
"0.0.0.0" = "127.0.0.1";
"::" = "::1";
}
.${address} or address;
hostPort = "${if lib.hasInfix ":" host then "[${host}]" else host}:${toString port}";
credentials = lib.optionalString (
username != null
) "${lib.escapeURL username}:${lib.escapeURL password}@";
checked =
value: if envUnavailable != null then throw "proxy-suite helpers: ${envUnavailable}" else value;
urls = checked {
http = "http://${credentials}${hostPort}";
# socks5h: names are resolved by the proxy, so they follow its routing.
socks = "socks5h://${credentials}${hostPort}";
};
httpVars = [
"http_proxy"
"https_proxy"
"HTTP_PROXY"
"HTTPS_PROXY"
];
socksVars = [
"all_proxy"
"ALL_PROXY"
];
noProxy = {
no_proxy = "localhost,127.0.0.0/8,::1";
NO_PROXY = "localhost,127.0.0.0/8,::1";
};
protocolVars = {
http = httpVars;
socks = socksVars;
all = httpVars ++ socksVars;
};
varsOf =
protocol:
protocolVars.${protocol}
or (throw "proxy-suite helpers: protocol must be \"http\", \"socks\" or \"all\", not \"${protocol}\"");
envFor =
protocol:
lib.genAttrs (varsOf protocol) (name: if builtins.elem name httpVars then urls.http else urls.socks)
// noProxy;
# A copy of `package` whose programs (all of bin/, or the named ones) are replaced by
# what `wrap` writes, given the original as "$target" and the wrapper as "$prog".
# .desktop files that start a program by its store path are pointed at the wrapper.
wrapPrograms =
suffix: programs: wrap: package:
pkgs.symlinkJoin {
name = "${lib.getName package}-${suffix}";
paths = [ package ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
cd "$out/bin"
for prog in ${if programs == null then "*" else lib.escapeShellArgs programs}; do
target=$(readlink -f "$prog")
rm "$prog"
${wrap}
done
for desktop in "$out"/share/applications/*.desktop; do
[ -e "$desktop" ] && grep -q ${package}/bin/ "$desktop" || continue
source=$(readlink -f "$desktop")
rm "$desktop"
sed "s|${package}/bin/|$out/bin/|g" "$source" > "$desktop"
done
'';
passthru.unwrapped = package;
# Not the whole meta: its license would make an unfree package's wrapper unfree
# too, and refused unless the host allows unfree, which the package may not need.
meta = lib.filterAttrs (name: _: name == "mainProgram" || name == "description") (
package.meta or { }
);
};
using =
feature: use:
if builtins.isString feature then throw "proxy-suite helpers: ${feature}" else use feature;
in
{
inherit urls envFor;
env = envFor "all";
wrapEnv =
{
protocol ? "http",
programs ? null,
}:
let
vars = envFor protocol;
others = lib.subtractLists (varsOf protocol) (httpVars ++ socksVars);
in
# The others are unset, so a shell's own proxy variables cannot steer the program elsewhere.
wrapPrograms "proxied" programs ''
makeWrapper "$target" "$prog" ${
lib.concatStringsSep " " (
lib.mapAttrsToList (name: value: "--set ${name} ${lib.escapeShellArg value}") vars
++ map (name: "--unset ${name}") others
)
}
'';
wrapProxychains =
{
programs ? null,
}:
using proxychains (
{ args }:
wrapPrograms "proxychains" programs ''
makeWrapper ${pkgs.proxychains-ng}/bin/proxychains4 "$prog" \
--add-flags ${lib.escapeShellArg "${args} "}"$target"
''
);
wrapPerApp =
{
profile,
programs ? null,
}:
using perApp (
{ proxyCtl, profiles }:
if !builtins.elem profile profiles then
throw "proxy-suite helpers: no per-app routing profile named \"${profile}\" (have: ${lib.concatStringsSep ", " profiles})"
else
wrapPrograms profile programs ''
makeWrapper ${proxyCtl}/bin/proxy-ctl "$prog" \
--add-flags ${lib.escapeShellArg "apps run ${profile} -- "}"$target"
''
);
}
@@ -35,6 +35,8 @@ in
# have it cleared on reboot.
runtimeDir = lib.mkDefault "${config.xdg.cacheHome}/proxy-suite/run";
};
# home-manager's place for helper functions, as on NixOS.
lib.proxy-suite = cfg.internal.helpers;
}
(lib.mkIf cfg.enable (
+3
View File
@@ -32,6 +32,9 @@ in
firewallPackage = config.networking.firewall.package;
resolvconfPackage = config.networking.resolvconf.package;
};
# NixOS's place for helper functions; set even while disabled, so a config can
# refer to it unconditionally.
lib.proxy-suite = cfg.internal.helpers;
}
(lib.mkIf cfg.enable (
+3
View File
@@ -64,6 +64,9 @@ in
rules = internalOption types.lines "";
};
nftables = internalOption types.bool false;
# ../helpers.nix, which the NixOS and home-manager adapters publish as
# config.lib.proxy-suite; the other hosts have no config.lib.
helpers = internalOption types.raw { };
firewall = {
allowedTCPPorts = internalOption (types.listOf types.port) [ ];
allowedUDPPorts = internalOption (types.listOf types.port) [ ];