Route onecli container egress through host clash proxy (flakes-i9ao)
OneCLI’s Rust gateway dials upstream services directly — it has no upstream
forward-proxy support (upstream issue https://github.com/onecli/onecli/issues/182,
still open, no maintainer response, no PR). Setting HTTPS_PROXY in
virtualisation.oci-containers.containers.onecli.environment
(nixos/containers/onecli/service.nix) therefore does not affect gateway
egress.
On CN-side hosts the only route to the internet is the clash proxy running as a
k3s NodePort, so the onecli container cannot reach upstream APIs at all. Fixed
below the application: an nftables REDIRECT captures the container’s outbound
TCP and hands it to a local mihomo instance that forwards to the host’s clash
proxy, rewriting each connection’s destination to the sniffed TLS SNI.
Chain: agent in yolo → onecli:10255 (injects credentials) → mihomo in the
onecli container → 10.100.0.1:31102 (host clash) → upstream.
Verified environment facts
- clash is a k3s NodePort in ns
services:31102→1102(http),31101→1101(socks5),31109→1109(control). - Both
pcandedgerrun k3s (via the autowiredmixins/nixos/ext4, which includesk3s.ext4.nix), so31102exists on both. incusbr0is10.100.0.1/24withipv4.nat = true, so from inside any incus container the host’s clash proxy is at10.100.0.1:31102.- firewalld backend is
nftablesandnetworking.nftables.enable = true(set bypacks/nixos/common/settings/firewalld.nix);networking.firewall.enable = false— firewalld replaces it. - The onecli container’s
system.stateVersionis26.05, sonetworking.nftables.flushRulesetdefaults to false — nftables.service only deletes the tables it declares and leaves firewalld’s ruleset intact. onecliis deployed onedger(nixos/hosts/edger/imports.nix);pcdoes not yet importmixins/nixos/services/onecliormixins/nixos/services/incus-ingress.nix.
Design decisions
- mihomo, not sing-box. The whole point is for the upstream clash to receive
CONNECT <domain>:443rather thanCONNECT <ip>:443, so that its domain rules still match and the container’s (poisoned) DNS answers become irrelevant. mihomo’ssniffer.override-destinationdoes exactly that. sing-box 1.13 cannot:sniff_override_destinationwas removed in 1.13.0, and the replacementaction: "sniff"only feeds route-rule matching. Verified empirically against sing-box 1.13.5 and mihomo 1.19.23 — see “Verification performed” below. - redsocks was rejected. It only ever issues
CONNECT <ip>:<port>, so clash’s domain rules would be bypassed and a separate clean-DNS story (local DoH resolver) would be mandatory. It is also a dead end in this repo: theservices.redsocksmodule applies its iptables rules vianetworking.firewall.extraCommands, which never runs because firewalld is used instead of the NixOS iptables firewall. redirect, nottun. A tun inbound would also catch UDP but needs/dev/net/tunadded to the container — an imperative step outside Nix.- No
meta skuidloop guard in the nftables rules: mihomo runs underDynamicUser, so its uid is unknowable at build time, and a name-basedskuidmatch fails the sandboxednetworking.nftables.checkRulesetbuild check. Excluding10.0.0.0/8covers mihomo’s own egress to the proxy. networking.enableIPv6 = falsein the mixin. The host proxy is IPv4 and only theipfamily is redirected, so leaving IPv6 on would let connections to dual-stack hosts silently bypass the proxy and hang. glibc prefers IPv4 over the incus ULA address by RFC 6724 precedence, but that is a preference, not a guarantee. Disabling IPv6 also makesgetaddrinfo’sAI_ADDRCONFIGdrop AAAA records so nothing even attempts IPv6.- No GEOIP/GEOSITE rules in the mihomo config: matching those makes mihomo fetch geodata, which it cannot do until the proxy it is configuring already works.
- Reusable mixin, so
searxng/ other containers can opt in later.
Verification performed
- Reproduced the whole data path on
pcbefore writing any Nix, using a throwaway nftables table (`ip daddr 198.51.100.7 tcp dport 443 redirect to - 12345
) plus a logging CONNECT proxy, andcurl –resolve example.com:443:198.51.100.7` so the destination IP was deliberately bogus:
- sing-box 1.13.5 with
action: "sniff"→ upstream receivedCONNECT 198.51.100.7:443— the IP. Destination override is gone. - sing-box 1.13.5 with legacy
sniff_override_destination: true→ refused to start: “legacy inbound fields are deprecated in sing-box 1.11.0 and removed in sing-box 1.13.0”. - mihomo 1.19.23 with
sniffer.override-destination: true→ upstream receivedCONNECT example.com:443. The bogus IP was discarded in favour of the sniffed SNI. - End-to-end against the real clash on
31102:http_code=200, and mihomo logged[TCP] ... --> example.com:443 match Match using upstream.
Build-time verification after implementation:
nixosConfigurations.onecli.config.system.build.toplevelbuilds, which includes the sandboxednetworking.nftables.checkRulesetvalidation of the generated ruleset.- The generated mihomo config passes
mihomo -t(“configuration file … test is successful”). pcandedgertoplevels still evaluate (nix build --dry-run), and both show31102/tcpinservices.firewalld.zones.incus.ports.
Summary of Changes
mixins/nixos/services/egress-proxy.nix (new)
Container-side mixin, parameterised via let bindings (proxyHost,
proxyPort, redirPort, directDests) in the style of
mixins/nixos/services/incus-ingress.nix:
services.mihomowith a generated config:redir-porton loopback,sniffer.override-destination: true(TLS:443, HTTP:80), a singlehttpproxy pointing at10.100.0.1:31102, andIP-CIDR ... DIRECT+MATCH,upstreamrules.networking.nftables.tables.egress-proxy— its owniptable with anat hook outputchain that returns for loopback / incus bridge / LAN / link-local / multicast and redirects all other TCP to the mihomo port.networking.enableIPv6 = false.
packs/nixos/host/incus.nix
Added { port = 31102; protocol = "tcp"; } to
services.firewalld.zones.incus.ports so containers can reach the host’s clash
NodePort. Applies to every host, which is the intent.
nixos/containers/onecli/imports.nix
Imports the new mixin.
nixos/containers/onecli/service.nix
podman-onecli now has after/wants on mihomo.service. The nftables
REDIRECT is installed before network-pre.target, so without this every
outbound connection during boot — including the ghcr.io/onecli/onecli:latest
image pull — is redirected to a closed port and refused.
Side benefit
The image pull and the curl calls in mixins/nixos/services/onecli/*.bash
now go through clash too; previously they went direct and would fail on
CN-side hosts.
Known limitation
Names that resolve to AAAA only will not work, since IPv6 is disabled and
the redirect is IPv4-only. None of the hosts OneCLI targets are in that
category (api.anthropic.com, github.com, context7.com all have A
records). If it ever matters, the fix is mihomo’s dns section in fake-ip
mode plus pointing the container’s resolv.conf at mihomo, which makes every
name resolve to a synthetic IPv4 that mihomo maps back to the domain.
Tasks
- Create
mixins/nixos/services/egress-proxy.nix - Open
31102/tcpinservices.firewalld.zones.incus(packs/nixos/host/incus.nix) - Import the mixin from
nixos/containers/onecli/imports.nix - Order
podman-onecliaftermihomo.service - Confirm the destination is rewritten to the sniffed domain, not the IP (mihomo 1.19.23 verified; sing-box 1.13.5 ruled out)
- Confirm no loop: mihomo’s own egress to
10.100.0.1:31102falls under the10.0.0.0/8exclusion - Build
nixosConfigurations.onecliincl. nftablescheckRuleset, andmihomo -tthe generated config - (done on
pc, 2026-10-15) Deploy toedgerand verify in situ:incus exec onecli -- curl -sS https://api.anthropic.comreaches upstream and clash logs the request by domain - Verify the gateway path end-to-end from
yolo(agent → onecli → mihomo → clash → upstream) - Check the boot-time ordering holds in practice (mihomo up before the image pull)
Deferred
pc host enablement of onecli needs mixins/nixos/services/onecli and
mixins/nixos/services/incus-ingress.nix added to nixos/hosts/pc/imports.nix,
which drags in the Caddy ingress, the Cloudflare secret and the 10.100.0.x
static-IP scheme. Worth its own bean.
Deploy notes (2026-10-15)
The onecli container was still running a pre-mihomo system, so after the k3s removal (471879b) its egress was dead and every agent container behind OneCLI lost HTTPS. Because onecli had no internet, deployed by building nixosConfigurations.onecli on edger, piping the missing closure in with nix-store --export | incus exec onecli -- nix-store --import, then nix-env -p /nix/var/nix/profiles/system --set + switch-to-configuration switch in the container. yolo was switched the same way.
Verified: curl from onecli and from yolo reaches github.com / api.anthropic.com; host mihomo logs show 10.100.0.2 and 10.100.0.100 connections by domain. mihomo logs Start Redir server error: operation not permitted - that is only the UDP redir listener (no permission in the container); the TCP listener on 127.0.0.1:12345 is up and is all the nftables rule uses.
podman-onecli was NOT restarted by the switch (only its unit ordering changed), so the boot-time ordering check is still open - needs a container restart.
Summary of Changes
Deployed and verified. Boot ordering after incus restart onecli: mihomo started at +676.59s, podman-onecli (and the image pull) at +676.78s; onecli and yolo egress both work after the reboot. The first restart attempt booted back into the old image system - fixed in flakes-dnjc.
Deployment on pc (2026-10-15)
- First real deploy exposed a bug: the nixpkgs mihomo module passes its config
via
LoadCredential, but inside the incus container the unit starts withoutCREDENTIALS_DIRECTORY, so mihomo looked for/config.yamland crash-looped. Fixed inegress-proxy.nixby forcingExecStartto read the store path and clearingLoadCredential. (edger’s in-place switch above did not hit this; it shows up when the container boots fresh from the image.) - Verified on a freshly launched onecli: mihomo + podman-onecli come up in order,
the image pull works, and
api.anthropic.comis reached via clash (the 403 region block is gone). From yolo: TLS through onecli OK, GitHub token injection OK. - onecli
:latestchanged the injection model: agents are “selective” and only get secrets they hold a grant for (PUT /api/agents/:id/grants/secrets/:sid).onecli-reseednow grants every secret to the default agent. The Auth.js/api/auth/sessionroute now 500s (MissingSecret) in local mode, so the scripts’ readiness probes use/api/user/api-keyinstead. - edger: if its onecli pulls a newer image, run
onecli-reseedthere too so the default agent gets its grants.