4.7 KiB
Wrap programs with the proxy
Build the proxy into a package, so a program uses it wherever and however it is started,
with no setup in the program. On NixOS and home-manager, the module provides the pieces as
config.lib.proxy-suite, built from your listener and per-app routing settings.
| Helper | How the program is routed | Covers |
|---|---|---|
wrapEnv |
Proxy variables (https_proxy, all_proxy, …) |
Programs that read them: most CLIs, Go, Python, curl, git |
wrapProxychains |
proxychains, for programs that ignore the variables | TCP; not static or Go programs |
wrapPerApp |
A per-app routing profile, via proxy-ctl apps run |
Everything, TCP and UDP (tun profile) |
env, envFor |
Just the variables, for services and shells | Whatever reads them |
On NixOS
{ config, pkgs, ... }:
let
proxySuite = config.lib.proxy-suite;
in
{
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ];
};
perAppRouting = {
enable = true;
createDefaultProfiles = true;
proxychains.enable = true;
tun.enable = true;
};
};
environment.systemPackages = [
(proxySuite.wrapEnv { } pkgs.yt-dlp)
(proxySuite.wrapProxychains { } pkgs.curl)
(proxySuite.wrapPerApp { profile = "tun"; } pkgs.firefox)
];
# A service: here nix-daemon, so downloads go through the proxy.
systemd.services.nix-daemon.environment = proxySuite.envFor "http";
}
The wrapped programs keep their names, and the package's .desktop files start them.
From home-manager
A home-manager config inside NixOS reads the system's helpers through osConfig:
{ osConfig, pkgs, ... }:
let
proxySuite = osConfig.lib.proxy-suite;
in
{
home.packages = [
(proxySuite.wrapEnv { protocol = "all"; } pkgs.codex)
(proxySuite.wrapEnv { } pkgs.claude-code)
];
}
When proxy-suite itself runs under home-manager, use config.lib.proxy-suite.
Other hosts, or another host's proxy
system-manager and Nix-on-Droid have no config.lib. There, or for a proxy that this config
does not run, the flake builds the URLs, the variables and wrapEnv from an address:
proxySuite = inputs.proxy-suite.lib.proxyHelpers {
inherit pkgs;
address = "192.168.1.10"; # default "127.0.0.1"
port = 1080; # default 1080
# username and password, if the proxy asks for them
};
Reference
urls.http,urls.socks: the proxy's URLs. A listener on0.0.0.0or::is reached on loopback.env: every proxy variable (http_proxy,https_proxy,all_proxy, their upper-case forms, andno_proxy).envFor "http"orenvFor "socks"gives one kind only.wrapEnv { protocol ? "http"; programs ? null; } package: the package, with its programs run with the variables ofprotocol.wrapProxychains { programs ? null; } package: its programs run under proxychains. NeedsperAppRouting.proxychains.enable.wrapPerApp { profile; programs ? null; } package: its programs run withproxy-ctl apps run <profile>. Needs the profile inperAppRouting.
programs = [ "…" ] wraps only the named programs of the package; the rest stay as they
are. By default, every program in its bin/ is wrapped.
Which variables
wrapEnv sets one kind and unsets the other, so proxy variables in your shell cannot
send the program somewhere else:
protocol = "http"(the default):http_proxyandhttps_proxy, which nearly everything reads.protocol = "socks":all_proxywithsocks5h://, so the proxy resolves names. For programs that read only that one, or handle SOCKS better.protocol = "all": both, when you are not sure which one a program reads.
Local addresses (localhost, 127.0.0.0/8, ::1) always stay direct, through no_proxy.
Good to know
- The variables need
proxy.enable. They cannot hold a password fromlistener.auth.passwordFile. UsewrapProxychainsthen, since it reads the password at run time. wrapPerAppwith atun,tproxyorzapretprofile has the same needs asproxy-ctl apps run: an admin password, or theperAppscope inuserControl.- A helper that cannot work stops the build and says why, for example when a profile does not exist.
- A wrapper does not inherit the package's license, so wrapping an unfree package needs nothing beyond what the package itself needs.
- For every program on the machine, see Route the whole machine instead.
See also
- Route a single app: the profiles
wrapPerAppuses perAppRouting.profiles