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

+4
View File
@@ -61,6 +61,10 @@ moduleSuiteChecks
proxy-suite-hosts = builtins.seq (builtins.deepSeq hostChecks.assertions true) (
pkgs.writeText "proxy-suite-hosts-check" "ok"
);
proxy-helpers = import ./checks/proxy-helpers.nix {
inherit pkgs;
inherit (checkLib) evalProxySuite;
};
}
// pkgs.lib.optionalAttrs (system == "x86_64-linux") {
amneziawg-runtime = amneziaWgRuntime;
+3
View File
@@ -33,6 +33,7 @@ let
hostStubs = {
homeManager.options = {
lib = stub (types.attrsOf types.attrs) { };
home.packages = packages;
xdg.stateHome = stub types.str "/home/u/.local/state";
xdg.cacheHome = stub types.str "/home/u/.cache";
@@ -185,6 +186,8 @@ in
assert hmCfg.services.proxy-suite.host.serviceManager == "systemd-user";
assert lib.hasPrefix "/home/u/.local/state/" hmCfg.services.proxy-suite.host.stateDir;
assert hmCfg.systemd.user.services ? proxy-suite-socks;
# The helpers are published where home-manager keeps its own.
assert hmCfg.lib.proxy-suite.urls.http == "http://127.0.0.1:1080";
assert hmCfg.systemd.user.services ? proxy-suite-inbounds;
assert hmCfg.systemd.user.services ? proxy-suite-tor;
# Rootless, there is no userControl group to hand the control socket to.
+3 -1
View File
@@ -9,10 +9,12 @@
let
generated = import ./read-generated.nix;
# Every fixture shares this one nixpkgs instead of importing its own: about 200 MB and
# 4 s less per evaluation. No fixture sets nixpkgs.* options, which this would ignore.
evalProxySuite =
modules:
import "${nixpkgs}/nixos/lib/eval-config.nix" {
inherit system;
inherit system pkgs;
modules = [ proxySuiteModule ] ++ modules;
};
forceEval = value: builtins.tryEval (builtins.deepSeq value true);
+98
View File
@@ -0,0 +1,98 @@
# config.lib.proxy-suite: the wrappers are built for real and run, and each
# unavailable helper refuses with its reason instead of building something broken.
{ pkgs, evalProxySuite }:
let
inherit (pkgs) lib;
helpersFor =
settings:
(evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
outbounds = [
{
tag = "a";
url = "socks://127.0.0.1:9";
}
];
};
}
// settings;
}
]).config.lib.proxy-suite;
helpers = helpersFor {
perAppRouting = {
enable = true;
createDefaultProfiles = true;
proxychains.enable = true;
tun.enable = true;
};
};
bare = helpersFor { };
refuses = value: !(builtins.tryEval (builtins.deepSeq value value)).success;
# Prints its environment; its .desktop file starts it by store path. Labelled unfree
# (built as free, since nixpkgs here refuses unfree), which the wrappers must not inherit.
fake =
(pkgs.runCommand "fake-app" { } ''
mkdir -p $out/bin $out/share/applications
printf '#!${pkgs.runtimeShell}\nexec ${pkgs.coreutils}/bin/env\n' > $out/bin/fake
printf '#!${pkgs.runtimeShell}\necho other\n' > $out/bin/other
chmod +x $out/bin/fake $out/bin/other
printf '[Desktop Entry]\nExec=%s/bin/fake %%U\n' "$out" > $out/share/applications/fake.desktop
'')
// {
meta.license = lib.licenses.unfree;
};
viaHttp = helpers.wrapEnv { } fake;
viaSocks = helpers.wrapEnv {
protocol = "socks";
programs = [ "fake" ];
} fake;
viaChains = helpers.wrapProxychains { programs = [ "fake" ]; } fake;
viaTun = helpers.wrapPerApp { profile = "tun"; } fake;
in
assert helpers.urls.http == "http://127.0.0.1:1080";
assert helpers.env.ALL_PROXY == "socks5h://127.0.0.1:1080";
assert refuses (bare.wrapProxychains { } fake);
assert refuses (bare.wrapPerApp { profile = "tun"; } fake);
assert refuses (helpers.wrapPerApp { profile = "nope"; } fake);
assert refuses (
(helpersFor {
proxy.listener.auth = {
username = "u";
passwordFile = "/run/secrets/p";
};
}).env
);
pkgs.runCommand "proxy-suite-proxy-helpers-check" { } ''
set -eu
export ALL_PROXY=socks5h://elsewhere:1 HTTPS_PROXY=http://elsewhere:1
# http: its variables set, the shell's socks one dropped; every program wrapped.
${viaHttp}/bin/fake > http.env
grep -qx 'HTTPS_PROXY=http://127.0.0.1:1080' http.env
grep -qx 'no_proxy=localhost,127.0.0.0/8,::1' http.env
! grep -q '^ALL_PROXY=' http.env || exit 1
grep -q 'HTTPS_PROXY' ${viaHttp}/bin/other
# The .desktop file starts the wrapper, not the original.
grep -qx "Exec=${viaHttp}/bin/fake %U" ${viaHttp}/share/applications/fake.desktop
# socks: the reverse, and only the named program.
${viaSocks}/bin/fake > socks.env
grep -qx 'ALL_PROXY=socks5h://127.0.0.1:1080' socks.env
! grep -q '^HTTPS_PROXY=' socks.env || exit 1
[ "$(readlink -f ${viaSocks}/bin/other)" = ${fake}/bin/other ]
# proxychains preloads itself into the program.
${viaChains}/bin/fake | grep -q '^LD_PRELOAD=.*libproxychains4'
# Per-app routing goes through proxy-ctl.
grep -q 'proxy-ctl.* apps run tun -- ${fake}/bin/fake' ${viaTun}/bin/fake
touch "$out"
''
+27 -13
View File
@@ -59,14 +59,21 @@ in
'';
# The usage guides are hand-written; what can go stale in them is checked here. Every
# ```nix block that sets services.proxy-suite (in the guides and the README) must
# evaluate, with no proxy-suite assertion or warning; a block fenced ```nix no-check is
# skipped. Every relative link must reach a file, and its #anchor an <a id> there.
# ```nix block that sets services.proxy-suite (in the guides, their examples/ and the
# README) must evaluate, with no proxy-suite assertion or warning; a block fenced
# ```nix no-check is skipped. A block is either module attributes or, when it starts
# with "{", a whole module. Every relative link must reach a file, and its #anchor an
# <a id> there.
usage-docs =
let
inherit (pkgs) lib;
usageDir = ../../docs/usage;
guides = builtins.filter (lib.hasSuffix ".md") (builtins.attrNames (builtins.readDir usageDir));
markdownIn =
dir:
map (name: lib.removePrefix "./" "${dir}/${name}") (
builtins.filter (lib.hasSuffix ".md") (builtins.attrNames (builtins.readDir (usageDir + "/${dir}")))
);
guides = markdownIn "." ++ markdownIn "examples";
readmeText = builtins.readFile ../../README.md;
guideText = name: builtins.readFile (usageDir + "/${name}");
@@ -98,15 +105,22 @@ in
where: snippet:
let
module = import (
builtins.toFile "snippet.nix" ''
{ config, lib, pkgs, ... }:
{
system.stateVersion = "26.05";
${snippet}
}
''
builtins.toFile "snippet.nix" (
if lib.hasPrefix "{" snippet then
snippet
else
''
{ config, lib, pkgs, ... }:
{
${snippet}
}
''
)
);
eval = evalProxySuite [ module ];
eval = evalProxySuite [
module
{ system.stateVersion = "26.05"; }
];
inherit (eval) config;
failed = map (a: a.message) (builtins.filter (a: !a.assertion) config.assertions);
own = builtins.filter (lib.hasPrefix "proxy-suite:") (failed ++ config.warnings);
@@ -135,7 +149,7 @@ in
let
target = builtins.head link;
anchor = builtins.elemAt link 1;
path = if target == "" then usageDir + "/${name}" else usageDir + "/${target}";
path = if target == "" then usageDir + "/${name}" else usageDir + "/${dirOf name}/${target}";
where = "docs/usage/${name}: ${target}${if anchor == null then "" else anchor}";
in
if !builtins.pathExists path then