From e4458dcae8751a44b15e0782a0da2ba8bc446532 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Fri, 25 Sep 2026 23:23:54 +0500 Subject: [PATCH] docs: improve some parts --- README.md | 15 +- docs/options/amneziaWg.md | 10 +- docs/options/index.md | 18 +- docs/usage/.snippets/README-1.nix | 81 +++++++++ docs/usage/.snippets/amneziawg-1.nix | 15 ++ docs/usage/.snippets/amneziawg-2.nix | 14 ++ docs/usage/.snippets/chains-1.nix | 14 ++ docs/usage/.snippets/chains-2.nix | 35 ++++ docs/usage/.snippets/client-basics-1.nix | 12 ++ docs/usage/.snippets/control-1.nix | 16 ++ .../usage/.snippets/examples-multi-exit-1.nix | 100 +++++++++++ .../.snippets/examples-private-vpn-1.nix | 60 +++++++ .../.snippets/examples-relay-server-1.nix | 159 ++++++++++++++++++ .../.snippets/examples-workstation-1.nix | 57 +++++++ docs/usage/.snippets/per-app-1.nix | 16 ++ docs/usage/.snippets/per-app-2.nix | 15 ++ docs/usage/.snippets/routing-1.nix | 17 ++ docs/usage/.snippets/routing-2.nix | 18 ++ docs/usage/.snippets/routing-3.nix | 15 ++ docs/usage/.snippets/server-1.nix | 35 ++++ docs/usage/.snippets/server-2.nix | 9 + docs/usage/.snippets/system-wide-1.nix | 13 ++ docs/usage/.snippets/whitelist-bypass-1.nix | 13 ++ docs/usage/.snippets/whitelist-bypass-2.nix | 11 ++ docs/usage/.snippets/wrap-apps-1.nix | 28 +++ docs/usage/.snippets/zapret-1.nix | 13 ++ docs/usage/.snippets/zapret-2.nix | 11 ++ docs/usage/examples/multi-exit.md | 42 +---- flake.nix | 24 ++- modules/proxy-suite/options/amnezia-wg.nix | 5 + .../proxy-suite/options/types/amnezia-wg.nix | 1 + nix/checks/repo.nix | 85 ++++------ nix/options-doc.nix | 17 +- nix/usage-snippets.nix | 61 +++++++ 34 files changed, 932 insertions(+), 123 deletions(-) create mode 100644 docs/usage/.snippets/README-1.nix create mode 100644 docs/usage/.snippets/amneziawg-1.nix create mode 100644 docs/usage/.snippets/amneziawg-2.nix create mode 100644 docs/usage/.snippets/chains-1.nix create mode 100644 docs/usage/.snippets/chains-2.nix create mode 100644 docs/usage/.snippets/client-basics-1.nix create mode 100644 docs/usage/.snippets/control-1.nix create mode 100644 docs/usage/.snippets/examples-multi-exit-1.nix create mode 100644 docs/usage/.snippets/examples-private-vpn-1.nix create mode 100644 docs/usage/.snippets/examples-relay-server-1.nix create mode 100644 docs/usage/.snippets/examples-workstation-1.nix create mode 100644 docs/usage/.snippets/per-app-1.nix create mode 100644 docs/usage/.snippets/per-app-2.nix create mode 100644 docs/usage/.snippets/routing-1.nix create mode 100644 docs/usage/.snippets/routing-2.nix create mode 100644 docs/usage/.snippets/routing-3.nix create mode 100644 docs/usage/.snippets/server-1.nix create mode 100644 docs/usage/.snippets/server-2.nix create mode 100644 docs/usage/.snippets/system-wide-1.nix create mode 100644 docs/usage/.snippets/whitelist-bypass-1.nix create mode 100644 docs/usage/.snippets/whitelist-bypass-2.nix create mode 100644 docs/usage/.snippets/wrap-apps-1.nix create mode 100644 docs/usage/.snippets/zapret-1.nix create mode 100644 docs/usage/.snippets/zapret-2.nix create mode 100644 nix/usage-snippets.nix diff --git a/README.md b/README.md index 5f8d777..e2cec72 100644 --- a/README.md +++ b/README.md @@ -37,16 +37,6 @@ inputs.proxy-suite.url = "github:FUFSoB/proxy-suite-flake"; modules = [ inputs.proxy-suite.nixosModules.default ]; ``` -Other hosts take the same options through their own module: - -| Module | For | -|---|---| -| `nixosModules.default` | NixOS | -| `nixosModules.server` | a single-user VPS: VLESS REALITY, TLS and WS inbounds, ACME and SSH | -| `systemManagerModules.default` | other distributions, via [system-manager](https://github.com/numtide/system-manager); firewall and kernel modules are left to the host | -| `homeManagerModules.default` | home-manager, without root: the local proxy, inbounds on ports from 1024, and anything else that needs no root | -| `nixOnDroidModules.default` | Android, via Nix-on-Droid: what home-manager gets, without the GUI | - A starter config: ```nix @@ -99,7 +89,6 @@ services.proxy-suite = { }; }; - # Registers a WARP device on first start. warp = { enable = true; asOutbound = "singBox"; # outbound tagged "warp" @@ -123,8 +112,8 @@ services.proxy-suite = { # the joiner here is an outbound tagged "wl". whitelistBypass = { enable = true; - joiners.wl = { platform = "wbstream"; linkFile = "/run/secrets/wb-link"; }; - # creators.phone = { platform = "wbstream"; cookiesFile = "/run/secrets/wb-cookies.json"; }; + creators.phone = { platform = "wbstream"; cookiesFile = "/run/secrets/wb-cookies.json"; }; + # joiners.wl = { platform = "wbstream"; linkFile = "/run/secrets/wb-link"; }; }; gui.enable = true; diff --git a/docs/options/amneziaWg.md b/docs/options/amneziaWg.md index a0a1fd1..50709a4 100644 --- a/docs/options/amneziaWg.md +++ b/docs/options/amneziaWg.md @@ -83,7 +83,7 @@ Whether to enable AmneziaWG client profiles\. AWG 3\.1 kernel module package\. ` null `: userspace only\. **Type:** null or package\ -**Default:** `` +**Default:** ` amneziawg ` from ` boot.kernelPackages ` on NixOS, ` null ` elsewhere ## services\.proxy-suite\.amneziaWg\.profiles @@ -152,7 +152,7 @@ host:port (IPv6 in brackets) that replaces the first peer’s endpoint\. Interface name, at most 15 characters\. **Type:** string matching the pattern ^\[A-Za-z0-9_\.-]{1,15}$\ -**Default:** `"awg-‹name›"` +**Default:** `"awg-"` ## services\.proxy-suite\.amneziaWg\.profiles\.\\.settings @@ -542,7 +542,7 @@ File with a vpn:// export\. AWG 3\.1 package with ` awg ` and ` awg-quick `\. **Type:** package\ -**Default:** `` +**Default:** proxy-suite’s patched ` amneziawg-tools ` (` pkgs/amneziawg.nix `) ## services\.proxy-suite\.amneziaWg\.userspacePackage @@ -550,7 +550,7 @@ AWG 3\.1 package with ` awg ` and ` awg-quick `\. AWG 3\.1 userspace implementation, used when the kernel module is unavailable\. **Type:** package\ -**Default:** `` +**Default:** proxy-suite’s patched ` amneziawg-go ` (` pkgs/amneziawg.nix `) ## services\.proxy-suite\.amneziaWg\.wireproxyPackage @@ -558,4 +558,4 @@ AWG 3\.1 userspace implementation, used when the kernel module is unavailable\. wireproxy build with AWG 3\.1, used by profiles with ` asOutbound = "userspace" `\. **Type:** package\ -**Default:** `` +**Default:** proxy-suite’s ` wireproxy-awg ` (` pkgs/wireproxy-awg.nix `) diff --git a/docs/options/index.md b/docs/options/index.md index 13c6986..7f48eee 100644 --- a/docs/options/index.md +++ b/docs/options/index.md @@ -41,11 +41,11 @@ Whether to enable proxy-suite\. services.proxy-suite = { amneziaWg = { enable = false; - kernelModulePackage = pkgs.amneziawg; + kernelModulePackage = ‹amneziawg from boot.kernelPackages on NixOS, null elsewhere›; profiles = { }; - toolsPackage = pkgs.amneziawg-tools; - userspacePackage = pkgs.amneziawg-go; - wireproxyPackage = pkgs.wireproxy-awg; + toolsPackage = ‹proxy-suite's patched amneziawg-tools (pkgs/amneziawg.nix)›; + userspacePackage = ‹proxy-suite's patched amneziawg-go (pkgs/amneziawg.nix)›; + wireproxyPackage = ‹proxy-suite's wireproxy-awg (pkgs/wireproxy-awg.nix)›; }; enable = false; geodata = { @@ -66,7 +66,7 @@ services.proxy-suite = { enable = false; listeners = { }; openFirewall = true; - package = pkgs.xray; + package = ‹proxy-suite's xray (pkgs/xray.nix)›; routing = { blockPrivate = true; blockRu = true; @@ -160,7 +160,7 @@ services.proxy-suite = { strategy = null; }; enable = false; - ipv6 = true; + ipv6 = config.networking.enableIPv6; listener = { address = "127.0.0.1"; auth = { @@ -203,7 +203,7 @@ services.proxy-suite = { selectionExclude = [ ]; singBox = { clashApiPort = 9090; - package = pkgs.sing-box; + package = ‹sing-box from proxy-suite's own nixpkgs input›; }; subscriptionUpdateInterval = "1d"; subscriptions = [ ]; @@ -230,7 +230,7 @@ services.proxy-suite = { url = "https://www.gstatic.com/generate_204"; }; xray = { - package = pkgs.xray; + package = ‹proxy-suite's xray (pkgs/xray.nix)›; }; }; sshProxy = { @@ -322,7 +322,7 @@ services.proxy-suite = { creators = { }; enable = false; joiners = { }; - package = pkgs.whitelist-bypass; + package = ‹proxy-suite's whitelist-bypass (pkgs/whitelist-bypass.nix)›; }; zapret = { cidrExemption = { diff --git a/docs/usage/.snippets/README-1.nix b/docs/usage/.snippets/README-1.nix new file mode 100644 index 0000000..21aaaf1 --- /dev/null +++ b/docs/usage/.snippets/README-1.nix @@ -0,0 +1,81 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + + proxy = { + enable = true; + backend = "sing-box"; # or "xray", "hybrid" + + # `url` ends up in the Nix store; use `urlFile` for secrets. + outbounds = [ { tag = "nl-vps"; url = "hy2://password@example.com:443?sni=example.com"; } ]; + subscriptions = [ { tag = "main-sub"; urlFile = "/run/secrets/sub-url"; } ]; + selection = "urltest"; # pick the fastest; default "first" + + tproxy.enable = true; + tun.enable = true; + }; + + perAppRouting = { + enable = true; + createDefaultProfiles = true; + proxychains.enable = true; + tun.enable = true; + tproxy.enable = true; + zapret.enable = true; + }; + + zapret.enable = true; + + sshProxy = { + enable = true; + server = { + user = "root"; + host = "ssh.example.com"; + }; + identityFile = "/run/secrets/proxy-suite-ssh-key"; + hostKeyFile = "/run/secrets/proxy-suite-ssh-known-hosts"; + }; + + # Only one global tunnel (AWG, TUN or TProxy) runs at a time. + amneziaWg = { + enable = true; + profiles.home.configFile = "/run/secrets/home-amneziawg.conf"; + profiles.work.vpnFile = "/run/secrets/work-amnezia.vpn"; + # Or use a profile as an outbound, tagged "de": + profiles.de = { + asOutbound = "interface"; # or "userspace" (no root), "singBox" (plain WireGuard) + configFile = "/run/secrets/de-amneziawg.conf"; + }; + }; + + warp = { + enable = true; + asOutbound = "singBox"; # outbound tagged "warp" + # asAmneziaWg = true; # or a global profile: proxy-ctl awg on warp + }; + + # Outbound tagged "tor"; .onion names always go there. + tor = { + enable = true; + asOutbound = true; + # bridges.file = "/run/secrets/tor-bridges"; + # upstream = "proxy"; # reach Tor through the proxy + }; + + tgWsProxy = { + enable = true; + secretFile = "/run/secrets/tg-ws-proxy-secret"; + }; + + # Tunnel through a video call. A creator runs on a free server and logs the call link; + # the joiner here is an outbound tagged "wl". + whitelistBypass = { + enable = true; + creators.phone = { platform = "wbstream"; cookiesFile = "/run/secrets/wb-cookies.json"; }; + # joiners.wl = { platform = "wbstream"; linkFile = "/run/secrets/wb-link"; }; + }; + + gui.enable = true; +}; +} diff --git a/docs/usage/.snippets/amneziawg-1.nix b/docs/usage/.snippets/amneziawg-1.nix new file mode 100644 index 0000000..a45be83 --- /dev/null +++ b/docs/usage/.snippets/amneziawg-1.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + amneziaWg = { + enable = true; + profiles.home.configFile = "/run/secrets/home-awg.conf"; + profiles.work = { + vpnFile = "/run/secrets/work-amnezia.vpn"; + autostart = true; # at most one profile + }; + }; + killSwitch.enable = true; # optional: no leaks while the tunnel is down +}; +} diff --git a/docs/usage/.snippets/amneziawg-2.nix b/docs/usage/.snippets/amneziawg-2.nix new file mode 100644 index 0000000..16fbc59 --- /dev/null +++ b/docs/usage/.snippets/amneziawg-2.nix @@ -0,0 +1,14 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy.enable = true; + amneziaWg = { + enable = true; + profiles.de = { + configFile = "/run/secrets/de-awg.conf"; + asOutbound = "interface"; + }; + }; +}; +} diff --git a/docs/usage/.snippets/chains-1.nix b/docs/usage/.snippets/chains-1.nix new file mode 100644 index 0000000..cb019d4 --- /dev/null +++ b/docs/usage/.snippets/chains-1.nix @@ -0,0 +1,14 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ + { tag = "hop"; urlFile = "/run/secrets/hop-url"; } + { tag = "de-vps"; urlFile = "/run/secrets/de-vps-url"; detour = "hop"; } + ]; + selectionExclude = [ "hop" ]; # never picked as the exit on its own + }; +}; +} diff --git a/docs/usage/.snippets/chains-2.nix b/docs/usage/.snippets/chains-2.nix new file mode 100644 index 0000000..3447710 --- /dev/null +++ b/docs/usage/.snippets/chains-2.nix @@ -0,0 +1,35 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + routing.rules = [ + { domains = [ "chatgpt.com" "openai.com" ]; outbound = "warp"; } + { domains = [ "check.torproject.org" ]; outbound = "tor"; } + ]; + }; + + # Tagged "warp". Registers a free WARP device on first start. + warp = { + enable = true; + asOutbound = "singBox"; + }; + + # Tagged "tor". .onion names always go to it. + tor = { + enable = true; + asOutbound = true; + }; + + # Tagged "ssh-proxy". + sshProxy = { + enable = true; + asOutbound = true; + server = { user = "me"; host = "ssh.example.com"; }; + identityFile = "/run/secrets/ssh-key"; + hostKeyFile = "/run/secrets/ssh-known-hosts"; # from ssh-keyscan + }; +}; +} diff --git a/docs/usage/.snippets/client-basics-1.nix b/docs/usage/.snippets/client-basics-1.nix new file mode 100644 index 0000000..79c3aa9 --- /dev/null +++ b/docs/usage/.snippets/client-basics-1.nix @@ -0,0 +1,12 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + subscriptions = [ { tag = "provider"; urlFile = "/run/secrets/provider-sub-url"; } ]; + selection = "urltest"; + }; +}; +} diff --git a/docs/usage/.snippets/control-1.nix b/docs/usage/.snippets/control-1.nix new file mode 100644 index 0000000..8303944 --- /dev/null +++ b/docs/usage/.snippets/control-1.nix @@ -0,0 +1,16 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + }; + gui.enable = true; + userControl = { + enable = true; + scopes = [ "services" "routing" "outbounds" "perApp" ]; + }; +}; +users.users.alice.extraGroups = [ "proxy-suite" ]; +} diff --git a/docs/usage/.snippets/examples-multi-exit-1.nix b/docs/usage/.snippets/examples-multi-exit-1.nix new file mode 100644 index 0000000..483da70 --- /dev/null +++ b/docs/usage/.snippets/examples-multi-exit-1.nix @@ -0,0 +1,100 @@ +{ ... }: +let + # Telegram apps dial their data centres by IP, so names alone do not catch them + # (https://core.telegram.org/resources/cidr.txt). + telegramIps = [ + "91.105.192.0/23" + "91.108.4.0/22" + "91.108.8.0/22" + "91.108.12.0/22" + "91.108.16.0/22" + "91.108.20.0/22" + "91.108.56.0/22" + "149.154.160.0/20" + "185.76.151.0/24" + "2001:67c:4e8::/48" + "2001:b28:f23c::/48" + "2001:b28:f23d::/48" + "2001:b28:f23f::/48" + "2a0a:f280::/32" + ]; +in +{ + services.proxy-suite = { + enable = true; + + proxy = { + enable = true; + outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ]; + + routing = { + # Unlisted sites leave directly, where zapret2 unblocks what DPI blocks. + default = "direct"; + # Russian sites are routed by the last rule below instead. + directRu = false; + + # Checked in order; the first match wins. + rules = [ + { + outbound = "ssh-proxy"; + geosites = [ "pixiv" "twitter" ]; + } + { + outbound = "warp"; + geosites = [ "youtube" ]; + } + # To mask your IP from Russian services + { + outbound = "warp"; + geosites = [ "category-ru" ]; + geoips = [ "ru" ]; + } + ]; + + # Through the selected outbound ("primary"). + proxy = { + geosites = [ "openai" "anthropic" "telegram" "netflix" ]; + ips = telegramIps; + # WARP registers through the proxy: its API is blocked directly in Russia. + domains = [ "api.cloudflareclient.com" ]; + }; + }; + + # For blocked sites no list names yet. YouTube is pinned to WARP above, so it + # is kept out of the probes. + autoProxy = { + enable = true; + exclude = [ "googlevideo.com" "gvt1.com" ]; + }; + }; + + warp = { + enable = true; + asOutbound = "singBox"; + }; + + sshProxy = { + enable = true; + asOutbound = true; + server = { + user = "me"; + host = "203.0.113.20"; + port = 2222; + }; + identityFile = "/run/secrets/proxy-ssh-key"; + hostKeyFile = "/run/secrets/proxy-ssh-known-hosts"; + }; + + # Tor through the proxy, since this network blocks Tor. + tor = { + enable = true; + asOutbound = true; + upstream = "proxy"; + }; + + zapret = { + enable = true; + engine = "zapret2"; + }; + }; +} \ No newline at end of file diff --git a/docs/usage/.snippets/examples-private-vpn-1.nix b/docs/usage/.snippets/examples-private-vpn-1.nix new file mode 100644 index 0000000..33da723 --- /dev/null +++ b/docs/usage/.snippets/examples-private-vpn-1.nix @@ -0,0 +1,60 @@ +{ ... }: +let + interface = "awg0"; + serverAddress = "10.77.77.1"; + privateDomains = [ "mail.example.com" ]; +in +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ]; + }; + + inbounds = { + enable = true; + routing.via = "proxy"; + listeners.vpn = { + type = "amneziawg"; + port = 51820; + # Fixed addresses, so firewall rules and DNS can name each device. Keys are + # generated on first start. + users = [ + { name = "laptop"; address = "10.77.77.2"; } + { name = "phone"; address = "10.77.77.3"; } + ]; + amneziaWg = { + # Devices also reach this host, its LAN and each other, directly. + mode = "lan"; + interfaceName = interface; + subnet = "10.77.77.0/24"; + subnet6 = "fd77:77:77::/64"; + mtu = 1400; + dns = [ serverAddress ]; + # Optional: a fixed server key, so client configs survive a reinstall. + privateKeyFile = "/run/secrets/awg-server-key"; + }; + }; + }; + }; + + # Private names for VPN clients; everything else is forwarded to 1.1.1.1. + services.dnsmasq = { + enable = true; + resolveLocalQueries = false; + settings = { + bind-dynamic = true; + interface = interface; + listen-address = serverAddress; + no-resolv = true; + server = [ "1.1.1.1" ]; + address = map (domain: "/${domain}/${serverAddress}") privateDomains; + }; + }; + # The interface comes and goes with the listener. + systemd.services.dnsmasq = { + after = [ "proxy-suite-inbounds-awg.service" ]; + wants = [ "proxy-suite-inbounds-awg.service" ]; + }; +} \ No newline at end of file diff --git a/docs/usage/.snippets/examples-relay-server-1.nix b/docs/usage/.snippets/examples-relay-server-1.nix new file mode 100644 index 0000000..6a064ae --- /dev/null +++ b/docs/usage/.snippets/examples-relay-server-1.nix @@ -0,0 +1,159 @@ +{ config, lib, ... }: +let + domain = "vpn.example.com"; + certDir = config.security.acme.certs.${domain}.directory; + + users = map (name: { inherit name; uuidFile = "/run/secrets/proxy-uuid-${name}"; }) [ + "alice" + "bob" + "carol" + ]; + + # The listeners terminate TLS themselves, so share links honestly say security=tls. + tls = { + enable = true; + certificateFile = "${certDir}/fullchain.pem"; + keyFile = "${certDir}/key.pem"; + serverName = domain; + }; + + # nginx on 443 hands a path to a loopback listener. + fronted = port: path: extra: { + type = "vless"; + inherit port users; + address = "127.0.0.1"; + sharePort = 443; # share links name nginx's port, not the listener's + transport = extra // { + inherit path; + # nginx sets this header, so XRay sees each client's real address (for + # `proxy-ctl inbounds online`). A client cannot forge it: nginx overwrites it. + trustedXForwardedFor = [ "X-Real-IP" ]; + }; + inherit tls; + }; + location = port: extra: { + proxyPass = "https://127.0.0.1:${toString port}"; + extraConfig = '' + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header X-Real-IP $remote_addr; + proxy_ssl_server_name on; + proxy_ssl_name ${domain}; + # Tunnels idle along with their users; the 60s default would cut them. + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + '' + + extra; + }; +in +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ]; + }; + + inbounds = { + enable = true; + serverAddress = domain; + routing.via = "proxy"; + + listeners = { + # nginx speaks HTTP/1.1 to the listener, which only packet-up mode crosses. + xhttp = fronted 10003 "/media" { + type = "xhttp"; + mode = "packet-up"; + }; + ws = fronted 10002 "/media-ws" { type = "ws"; }; + # stream-one needs HTTP/2 all the way: nginx's grpc_pass provides it. + xhttp-stream = lib.recursiveUpdate (fronted 10004 "/media-stream" { + type = "xhttp"; + mode = "stream-one"; + }) { tls.alpn = [ "h2" ]; }; + + # HTTP/3 takes UDP 443 only, beside nginx on TCP 443. No trustedXForwardedFor: + # nothing is in front, so it already sees the real client. + xhttp-h3 = { + type = "vless"; + port = 443; + inherit users; + transport = { type = "xhttp"; path = "/media-h3"; }; + tls = tls // { alpn = [ "h3" ]; }; + }; + + reality = { + type = "vless"; + port = 2053; + inherit users; + flow = "xtls-rprx-vision"; + reality = { + enable = true; + # Probes without the key land on a real TLS 1.3 + HTTP/2 site. + dest = "www.samsung.com:443"; + serverNames = [ "www.samsung.com" ]; + privateKeyFile = "/run/secrets/proxy-reality-key"; + publicKey = "jNXH…"; + shortIds = [ "0123abcd" ]; + }; + }; + }; + + subscriptions = { + enable = true; + baseUrl = "https://${domain}/sub"; + }; + }; + + userControl.enable = true; # passwordless proxy-ctl for the admin, see below + }; + + users.users.admin.extraGroups = [ "proxy-suite" ]; + + security.acme = { + acceptTerms = true; + defaults.email = "admin@example.com"; + # A renewed certificate restarts the listeners. + certs.${domain}.reloadServices = [ "proxy-suite-inbounds.service" ]; + }; + systemd.services.proxy-suite-inbounds = { + wants = [ "acme-${domain}.service" ]; + after = [ "acme-${domain}.service" ]; + }; + + services.nginx = { + enable = true; + virtualHosts.${domain} = { + forceSSL = true; + enableACME = true; + locations = { + # A prefix match: XHTTP appends //. + "/media" = location 10003 '' + proxy_http_version 1.1; + proxy_request_buffering off; + proxy_buffering off; + client_max_body_size 0; + ''; + "= /media-ws" = location 10002 "" // { proxyWebsockets = true; }; + "/media-stream".extraConfig = '' + grpc_pass grpcs://127.0.0.1:10004; + grpc_set_header X-Forwarded-For $remote_addr; + grpc_set_header X-Real-IP $remote_addr; + grpc_ssl_server_name on; + grpc_ssl_name ${domain}; + grpc_read_timeout 3600s; + grpc_send_timeout 3600s; + client_max_body_size 0; + ''; + # Subscriptions. Some clients show profile-title and follow profile-update-interval. + "/sub/".extraConfig = '' + alias /run/proxy-suite-inbounds/subscriptions/; + default_type text/plain; + autoindex off; + add_header profile-title "My VPN" always; + add_header profile-update-interval "12" always; + ''; + }; + }; + }; +} \ No newline at end of file diff --git a/docs/usage/.snippets/examples-workstation-1.nix b/docs/usage/.snippets/examples-workstation-1.nix new file mode 100644 index 0000000..ecf166d --- /dev/null +++ b/docs/usage/.snippets/examples-workstation-1.nix @@ -0,0 +1,57 @@ +{ config, pkgs, ... }: +let + proxySuite = config.lib.proxy-suite; +in +{ + services.proxy-suite = { + enable = true; + + proxy = { + enable = true; + # The server's subscription: every listener, each a separate outbound. + subscriptions = [ { tag = "home"; urlFile = "/run/secrets/proxy-home-sub"; } ]; + # The fastest listener that gets through the current network. + selection = "urltest"; + urlTest.url = "https://telegram.org"; # blocked here, so a pass means it works + + routing.default = "direct"; + autoProxy.enable = true; + tun.enable = true; # when everything should go through: proxy-ctl proxy tun on + }; + + zapret = { + enable = true; + engine = "zapret2"; + }; + + # The private VPN: `proxy-ctl awg on home`. + amneziaWg = { + enable = true; + profiles.home.configFile = "/run/secrets/home-vpn.conf"; + }; + killSwitch.enable = true; + + perAppRouting = { + enable = true; + createDefaultProfiles = true; + proxychains.enable = true; + tun.enable = true; + }; + + gui.enable = true; + userControl.enable = true; + }; + + users.users.me.extraGroups = [ "proxy-suite" ]; + + environment.systemPackages = [ + # CLIs that only work through the proxy, whatever the routing says. + (proxySuite.wrapEnv { protocol = "all"; } pkgs.codex) + (proxySuite.wrapEnv { } pkgs.claude-code) + # A browser for blocked sites, with its UDP (QUIC) too. + (proxySuite.wrapPerApp { profile = "tun"; } pkgs.chromium) + ]; + + # Nix downloads through the proxy. + systemd.services.nix-daemon.environment = proxySuite.envFor "http"; +} \ No newline at end of file diff --git a/docs/usage/.snippets/per-app-1.nix b/docs/usage/.snippets/per-app-1.nix new file mode 100644 index 0000000..3ff5ab4 --- /dev/null +++ b/docs/usage/.snippets/per-app-1.nix @@ -0,0 +1,16 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + }; + perAppRouting = { + enable = true; + createDefaultProfiles = true; # a profile named after each method below + proxychains.enable = true; + tun.enable = true; + }; +}; +} diff --git a/docs/usage/.snippets/per-app-2.nix b/docs/usage/.snippets/per-app-2.nix new file mode 100644 index 0000000..cac1cd2 --- /dev/null +++ b/docs/usage/.snippets/per-app-2.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + }; + perAppRouting = { + enable = true; + tun.enable = true; + profiles = [ { name = "browser"; route = "tun"; } ]; + }; +}; +} diff --git a/docs/usage/.snippets/routing-1.nix b/docs/usage/.snippets/routing-1.nix new file mode 100644 index 0000000..735e988 --- /dev/null +++ b/docs/usage/.snippets/routing-1.nix @@ -0,0 +1,17 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + routing = { + default = "direct"; + proxy.domains = [ "youtube.com" "googlevideo.com" ]; + proxy.geosites = [ "instagram" ]; + }; + # Find blocked sites on its own, and route each one through an exit that reaches it. + autoProxy.enable = true; + }; +}; +} diff --git a/docs/usage/.snippets/routing-2.nix b/docs/usage/.snippets/routing-2.nix new file mode 100644 index 0000000..86bab9c --- /dev/null +++ b/docs/usage/.snippets/routing-2.nix @@ -0,0 +1,18 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ + { tag = "nl-vps"; urlFile = "/run/secrets/nl-vps-url"; } + { tag = "us-vps"; urlFile = "/run/secrets/us-vps-url"; } + ]; + routing = { + rules = [ { geosites = [ "netflix" ]; outbound = "us-vps"; } ]; + direct.domains = [ "bank.example" ]; + block.geosites = [ "category-ads-all" ]; + }; + }; +}; +} diff --git a/docs/usage/.snippets/routing-3.nix b/docs/usage/.snippets/routing-3.nix new file mode 100644 index 0000000..23ccf53 --- /dev/null +++ b/docs/usage/.snippets/routing-3.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + routing = { + default = "direct"; + ruleSets.blocked.url = "https://example.com/blocked.srs"; + proxy.ruleSets = [ "blocked" ]; + }; + }; +}; +} diff --git a/docs/usage/.snippets/server-1.nix b/docs/usage/.snippets/server-1.nix new file mode 100644 index 0000000..def3b67 --- /dev/null +++ b/docs/usage/.snippets/server-1.nix @@ -0,0 +1,35 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + inbounds = { + enable = true; + serverAddress = "vpn.example.com"; # null: this host's public IPv4 + routing.via = "direct"; # clients exit straight from this host + listeners = { + vless-reality = { + type = "vless"; + port = 443; + flow = "xtls-rprx-vision"; + users = [ + { name = "phone"; uuidFile = "/run/secrets/uuid-phone"; } + { name = "laptop"; uuidFile = "/run/secrets/uuid-laptop"; } + ]; + reality = { + enable = true; + serverNames = [ "www.microsoft.com" ]; + privateKeyFile = "/run/secrets/reality-private-key"; + publicKey = "jNXH…"; # from the same `xray x25519` run + shortIds = [ "0123abcd" ]; + }; + }; + # Keys for the server and each user are generated on first start. + awg = { + type = "amneziawg"; + port = 51820; + users = [ { name = "phone"; } ]; + }; + }; + }; +}; +} diff --git a/docs/usage/.snippets/server-2.nix b/docs/usage/.snippets/server-2.nix new file mode 100644 index 0000000..944648c --- /dev/null +++ b/docs/usage/.snippets/server-2.nix @@ -0,0 +1,9 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite.inbounds.subscriptions = { + enable = true; + baseUrl = "https://vpn.example.com/sub"; +}; +services.nginx.virtualHosts."vpn.example.com".locations."/sub/".alias = + "/run/proxy-suite-inbounds/subscriptions/"; +} diff --git a/docs/usage/.snippets/system-wide-1.nix b/docs/usage/.snippets/system-wide-1.nix new file mode 100644 index 0000000..41b9746 --- /dev/null +++ b/docs/usage/.snippets/system-wide-1.nix @@ -0,0 +1,13 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + outbounds = [ { tag = "my-vps"; urlFile = "/run/secrets/my-vps-url"; } ]; + tun.enable = true; + autostart = "tun"; # or null, to start it by hand + }; + killSwitch.enable = true; +}; +} diff --git a/docs/usage/.snippets/whitelist-bypass-1.nix b/docs/usage/.snippets/whitelist-bypass-1.nix new file mode 100644 index 0000000..61eb143 --- /dev/null +++ b/docs/usage/.snippets/whitelist-bypass-1.nix @@ -0,0 +1,13 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + whitelistBypass = { + enable = true; + creators.laptop = { + platform = "wbstream"; + cookiesFile = "/run/secrets/wbstream-cookies.json"; + }; + }; +}; +} diff --git a/docs/usage/.snippets/whitelist-bypass-2.nix b/docs/usage/.snippets/whitelist-bypass-2.nix new file mode 100644 index 0000000..0dfaf88 --- /dev/null +++ b/docs/usage/.snippets/whitelist-bypass-2.nix @@ -0,0 +1,11 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + proxy.enable = true; + whitelistBypass = { + enable = true; + joiners.wl.platform = "wbstream"; + }; +}; +} diff --git a/docs/usage/.snippets/wrap-apps-1.nix b/docs/usage/.snippets/wrap-apps-1.nix new file mode 100644 index 0000000..b611c35 --- /dev/null +++ b/docs/usage/.snippets/wrap-apps-1.nix @@ -0,0 +1,28 @@ +{ 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.telegram-desktop) + (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"; +} \ No newline at end of file diff --git a/docs/usage/.snippets/zapret-1.nix b/docs/usage/.snippets/zapret-1.nix new file mode 100644 index 0000000..49a52a0 --- /dev/null +++ b/docs/usage/.snippets/zapret-1.nix @@ -0,0 +1,13 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + zapret = { + enable = true; + zapret-discord-youtube = { + configName = "general(ALT)"; # the default; try others if it does not work + domains = [ "rutracker.org" ]; # more sites to unblock + }; + }; +}; +} diff --git a/docs/usage/.snippets/zapret-2.nix b/docs/usage/.snippets/zapret-2.nix new file mode 100644 index 0000000..d56c88b --- /dev/null +++ b/docs/usage/.snippets/zapret-2.nix @@ -0,0 +1,11 @@ +{ config, lib, pkgs, ... }: +{ + services.proxy-suite = { + enable = true; + zapret = { + enable = true; + engine = "zapret2"; + zapret2.domains = [ "rutracker.org" ]; # always treated as blocked + }; +}; +} diff --git a/docs/usage/examples/multi-exit.md b/docs/usage/examples/multi-exit.md index 5f685cf..40003ea 100644 --- a/docs/usage/examples/multi-exit.md +++ b/docs/usage/examples/multi-exit.md @@ -7,14 +7,6 @@ listed. Everything runs in global TUN mode, so every app on the host follows the ```nix { ... }: let - # Geosite "youtube" can miss the video and API hosts. - youtubeDomains = [ - "googlevideo.com" - "gvt1.com" - "ytimg.com" - "ggpht.com" - "youtubei.googleapis.com" - ]; # Telegram apps dial their data centres by IP, so names alone do not catch them # (https://core.telegram.org/resources/cidr.txt). telegramIps = [ @@ -41,8 +33,6 @@ in proxy = { enable = true; outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ]; - tun.enable = true; - autostart = "tun"; routing = { # Unlisted sites leave directly, where zapret2 unblocks what DPI blocks. @@ -52,24 +42,15 @@ in # Checked in order; the first match wins. rules = [ - # Sites that reject both this host's and the VPS's networks. { outbound = "ssh-proxy"; geosites = [ "pixiv" "twitter" ]; } - # Video throttled per connection by destination IP, which zapret cannot fix. - # One exit for the whole session: video URLs are bound to the IP that asked. { outbound = "warp"; - domains = youtubeDomains; geosites = [ "youtube" ]; } - # A CDN that stalls through WARP but runs at full speed directly. - { - outbound = "direct"; - domains = [ "video-cdn.example" ]; - } - # Russian services that refuse the VPS's foreign IP. + # To mask your IP from Russian services { outbound = "warp"; geosites = [ "category-ru" ]; @@ -81,13 +62,12 @@ in proxy = { geosites = [ "openai" "anthropic" "telegram" "netflix" ]; ips = telegramIps; - # WARP registers through the proxy: its API is blocked directly. + # WARP registers through the proxy: its API is blocked directly in Russia. domains = [ "api.cloudflareclient.com" ]; }; }; - # For blocked sites no list names yet. YouTube is pinned to WARP above, so it - # is kept out of the probes. + # For blocked sites no list names yet. YouTube is pinned to WARP above, so it is kept out of the probes. autoProxy = { enable = true; exclude = [ "googlevideo.com" "gvt1.com" ]; @@ -111,27 +91,11 @@ in hostKeyFile = "/run/secrets/proxy-ssh-known-hosts"; }; - # Tor through the proxy, since this network blocks Tor. - tor = { - enable = true; - asOutbound = true; - upstream = "proxy"; - }; - zapret = { enable = true; engine = "zapret2"; }; }; - - # autoProxy probes in bursts (seconds of CPU, a few hundred MB). On a busy host, keep - # it from competing with real work. - systemd.services.proxy-suite-autoproxy.serviceConfig = { - CPUWeight = 10; - IOWeight = 10; - Nice = 19; - MemoryHigh = "200M"; - }; } ``` diff --git a/flake.nix b/flake.nix index e912ad7..ff0f1ef 100644 --- a/flake.nix +++ b/flake.nix @@ -107,8 +107,14 @@ mkProxySuiteTray = throw "mkProxySuiteTray was replaced by Proxy Suite GUI: set services.proxy-suite.gui.enable"; }; - # `nix fmt`; the nix-format check holds the tree to it. - formatter = forAll (system: (pkgsFor system).nixfmt-tree); + # `nix fmt`; the nix-format check holds the tree to it. The usage snippets are + # generated, and the usage-docs check wants them as their Markdown has them. + formatter = forAll ( + system: + (pkgsFor system).nixfmt-tree.override { + settings.global.excludes = [ "docs/usage/.snippets/*" ]; + } + ); packages = forAll ( system: @@ -140,8 +146,14 @@ cp -r --no-preserve=mode,ownership "''${options_output_path}" "''${repo_root}/docs/options" install -Dm644 "''${readme_output_path}" "''${repo_root}/README.md" + # After the README, whose blocks are among them. + snippets_output_path="$(nix build --no-link --print-out-paths "''${repo_root}#usageSnippets")" + rm -rf "''${repo_root}/docs/usage/.snippets" + cp -r --no-preserve=mode,ownership "''${snippets_output_path}" "''${repo_root}/docs/usage/.snippets" + echo "updated ''${repo_root}/docs/options/" echo "updated ''${repo_root}/README.md" + echo "updated ''${repo_root}/docs/usage/.snippets/" ''; }; in @@ -167,6 +179,14 @@ ]; }).config.system.build.isoImage; optionsDoc = mkOptionsDoc system; + usageSnippets = pkgs.runCommand "proxy-suite-usage-snippets" { } ( + '' + mkdir "$out" + '' + + pkgs.lib.concatMapStrings (snippet: '' + cp ${pkgs.writeText snippet.file snippet.text} "$out"/${snippet.file} + '') (import ./nix/usage-snippets.nix { inherit (pkgs) lib; }).snippets + ); readmeDoc = mkReadmeDoc system; update-docs = updateDocs; update-options-doc = pkgs.writeShellApplication { diff --git a/modules/proxy-suite/options/amnezia-wg.nix b/modules/proxy-suite/options/amnezia-wg.nix index b530f7f..fff82f1 100644 --- a/modules/proxy-suite/options/amnezia-wg.nix +++ b/modules/proxy-suite/options/amnezia-wg.nix @@ -7,6 +7,7 @@ let inherit (lib) + literalMD mkEnableOption mkOption types @@ -21,18 +22,21 @@ in toolsPackage = mkOption { type = types.package; default = awgPackages.tools; + defaultText = literalMD "proxy-suite's patched `amneziawg-tools` (`pkgs/amneziawg.nix`)"; description = "AWG 3.1 package with `awg` and `awg-quick`."; }; userspacePackage = mkOption { type = types.package; default = awgPackages.userspace; + defaultText = literalMD "proxy-suite's patched `amneziawg-go` (`pkgs/amneziawg.nix`)"; description = "AWG 3.1 userspace implementation, used when the kernel module is unavailable."; }; wireproxyPackage = mkOption { type = types.package; default = awgPackages.wireproxy; + defaultText = literalMD "proxy-suite's `wireproxy-awg` (`pkgs/wireproxy-awg.nix`)"; description = "wireproxy build with AWG 3.1, used by profiles with `asOutbound = \"userspace\"`."; }; @@ -43,6 +47,7 @@ in kernelPackages = config.services.proxy-suite.host.kernelPackages; in if kernelPackages == null then null else awgPackages.kernelModule kernelPackages; + defaultText = literalMD "`amneziawg` from `boot.kernelPackages` on NixOS, `null` elsewhere"; description = "AWG 3.1 kernel module package. `null`: userspace only."; }; diff --git a/modules/proxy-suite/options/types/amnezia-wg.nix b/modules/proxy-suite/options/types/amnezia-wg.nix index 60b77d0..e7a96e2 100644 --- a/modules/proxy-suite/options/types/amnezia-wg.nix +++ b/modules/proxy-suite/options/types/amnezia-wg.nix @@ -170,6 +170,7 @@ let interfaceName = mkOption { type = types.strMatching "^[A-Za-z0-9_.-]{1,15}$"; default = "awg-${name}"; + defaultText = lib.literalExpression ''"awg-"''; description = "Interface name, at most 15 characters."; }; autostart = mkOption { diff --git a/nix/checks/repo.nix b/nix/checks/repo.nix index 36593c0..5f9bd15 100644 --- a/nix/checks/repo.nix +++ b/nix/checks/repo.nix @@ -48,6 +48,11 @@ in pkgs.runCommand "proxy-suite-options-doc-check" { nativeBuildInputs = [ pkgs.diffutils ]; } '' diff -ru ${../../docs/options} ${generatedOptionsDoc} + # A package default without defaultText renders as a store derivation. + if grep -rn '&2 + exit 1 + fi touch "$out" ''; @@ -60,30 +65,23 @@ 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, 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 - # there. + # README) must evaluate, with no proxy-suite assertion or warning, from its copy in + # docs/usage/.snippets (nix/usage-snippets.nix), which must match it. Every relative link + # must reach a file, and its #anchor an there. usage-docs = let inherit (pkgs) lib; usageDir = ../../docs/usage; - 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; + usageSnippets = import ../usage-snippets.nix { inherit lib; }; + inherit (usageSnippets) guides snippets; guideText = name: builtins.readFile (usageDir + "/${name}"); - snippets = - text: - builtins.filter (lib.hasInfix "services.proxy-suite") ( - map (chunk: builtins.head (lib.splitString "\n```" chunk)) ( - builtins.tail (lib.splitString "```nix\n" text) - ) - ); + snippetDir = ../.. + "/${usageSnippets.dir}"; + committed = lib.optionals (builtins.pathExists snippetDir) ( + builtins.attrNames (builtins.readDir snippetDir) + ); + outdated = "is out of date, run `nix run .#update-docs`"; + # Every visible option's value, so a wrong type fails too. Packages are skipped: # forcing one evaluates its whole build graph. Removed options throw when read. plain = @@ -102,39 +100,28 @@ in name: opt: if lib.isOption opt then plain cfg.${name} else visibleValues opt cfg.${name} ) (lib.filterAttrs (name: opt: name != "_module" && (opt.visible or true) != false) opts); snippetProblems = - where: snippet: + snippet: let - module = import ( - builtins.toFile "snippet.nix" ( - if lib.hasPrefix "{" snippet then - snippet - else - '' - { config, lib, pkgs, ... }: - { - ${snippet} - } - '' - ) - ); + path = snippetDir + "/${snippet.file}"; eval = evalProxySuite [ - module + (import path) { 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); in - builtins.addErrorContext "while evaluating ${where}" ( - builtins.deepSeq (visibleValues eval.options.services.proxy-suite config.services.proxy-suite) ( - map (message: "${where}: ${message}") own - ) - ); - textProblems = - file: text: - lib.concatLists ( - lib.imap1 (index: snippetProblems "${file}, nix block ${toString index}") (snippets text) - ); + if !builtins.elem snippet.file committed || builtins.readFile path != snippet.text then + [ "${snippet.where}: ${usageSnippets.dir}/${snippet.file} ${outdated}" ] + else + builtins.addErrorContext "while evaluating ${snippet.where}" ( + builtins.deepSeq (visibleValues eval.options.services.proxy-suite config.services.proxy-suite) ( + map (message: "${snippet.where}: ${message}") own + ) + ); + leftover = map (file: "${usageSnippets.dir}/${file}: no nix block for it, ${outdated}") ( + lib.subtractLists (map (snippet: snippet.file) snippets) committed + ); # [text](target) and [text](target#anchor), except absolute URLs. links = @@ -162,11 +149,7 @@ in [ ] ) (links (guideText name)); - problems = - textProblems "README.md" readmeText - ++ lib.concatMap ( - name: textProblems "docs/usage/${name}" (guideText name) ++ linkProblems name - ) guides; + problems = leftover ++ lib.concatMap snippetProblems snippets ++ lib.concatMap linkProblems guides; in pkgs.runCommand "proxy-suite-usage-docs-check" { } ( if problems == [ ] then @@ -178,14 +161,16 @@ in '' ); - # The tree as `nix fmt` leaves it. + # The tree as `nix fmt` leaves it; the generated usage snippets are left as written. nix-format = pkgs.runCommand "proxy-suite-nix-format-check" { nativeBuildInputs = [ pkgs.nixfmt ]; } '' cd ${ pkgs.lib.fileset.toSource { root = ../..; - fileset = pkgs.lib.fileset.fileFilter (file: file.hasExt "nix") ../..; + fileset = pkgs.lib.fileset.difference (pkgs.lib.fileset.fileFilter ( + file: file.hasExt "nix" + ) ../..) (pkgs.lib.fileset.maybeMissing ../../docs/usage/.snippets); } } find . -name '*.nix' -print0 | xargs -0 nixfmt --check diff --git a/nix/options-doc.nix b/nix/options-doc.nix index 3e37571..6da7dea 100644 --- a/nix/options-doc.nix +++ b/nix/options-doc.nix @@ -17,8 +17,14 @@ let ]; }; + pretty = text: { + __pretty = _: text; + val = null; + }; + # Visible options only (renamed aliases are hidden, and so are groups left - # with nothing but aliases), packages shown by name. + # with nothing but aliases). A defaultText is shown as written, Markdown ones + # as a ‹placeholder›; other packages by name. visibleConfig = opts: cfg: lib.filterAttrs (name: value: lib.isOption opts.${name} || value != { }) ( @@ -26,11 +32,12 @@ let name: opt: if !lib.isOption opt then visibleConfig opt cfg.${name} + else if (opt.defaultText._type or null) == "literalExpression" then + pretty opt.defaultText.text + else if (opt.defaultText._type or null) == "literalMD" then + pretty "‹${lib.replaceStrings [ "`" ] [ "" ] opt.defaultText.text}›" else if lib.isDerivation cfg.${name} then - { - __pretty = _: "pkgs.${lib.getName cfg.${name}}"; - val = null; - } + pretty "pkgs.${lib.getName cfg.${name}}" else cfg.${name} ) (lib.filterAttrs (name: opt: name != "_module" && (opt.visible or true) != false) opts) diff --git a/nix/usage-snippets.nix b/nix/usage-snippets.nix new file mode 100644 index 0000000..cfb9ab0 --- /dev/null +++ b/nix/usage-snippets.nix @@ -0,0 +1,61 @@ +# The ```nix blocks of the README and the usage guides that set services.proxy-suite, each +# as a whole module. update-docs writes them to docs/usage/.snippets, where the usage-docs +# check imports them: Nix only parses files, and `nix flake check --no-build` can't write +# one to the store. A block fenced ```nix no-check is skipped; one that starts with "{" is +# a module already, anything else is its attributes. +{ lib }: + +let + usageDir = ../docs/usage; + markdownIn = + dir: + map (name: lib.removePrefix "./" "${dir}/${name}") ( + builtins.filter (lib.hasSuffix ".md") (builtins.attrNames (builtins.readDir (usageDir + "/${dir}"))) + ); + guides = markdownIn "." ++ markdownIn "examples"; + + blocks = + text: + builtins.filter (lib.hasInfix "services.proxy-suite") ( + map (chunk: builtins.head (lib.splitString "\n```" chunk)) ( + builtins.tail (lib.splitString "```nix\n" text) + ) + ); + module = + block: + if lib.hasPrefix "{" block then + block + else + '' + { config, lib, pkgs, ... }: + { + ${block} + } + ''; + + docs = [ + { + where = "README.md"; + stem = "README"; + text = builtins.readFile ../README.md; + } + ] + ++ map (name: { + where = "docs/usage/${name}"; + stem = lib.replaceStrings [ "/" ] [ "-" ] (lib.removeSuffix ".md" name); + text = builtins.readFile (usageDir + "/${name}"); + }) guides; +in +{ + inherit guides; + dir = "docs/usage/.snippets"; + # [ { file, where, text } ], file relative to dir. + snippets = lib.concatMap ( + doc: + lib.imap1 (index: block: { + file = "${doc.stem}-${toString index}.nix"; + where = "${doc.where}, nix block ${toString index}"; + text = module block; + }) (blocks doc.text) + ) docs; +}