From a77e762efbdf8c331c356c1963e458323390fded Mon Sep 17 00:00:00 2001 From: Sizhe Zhao Date: Sun, 1 Mar 2026 21:02:29 +0800 Subject: [PATCH] sing-box: support naive outbound (cherry picked from commit e99be1b21f7e78caf6d3853f94f650f52752e1bd) --- pkgs/by-name/si/sing-box/cronet-go.patch | 27 ++++++++++++ pkgs/by-name/si/sing-box/package.nix | 53 ++++++++++++++++++++---- 2 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 pkgs/by-name/si/sing-box/cronet-go.patch diff --git a/pkgs/by-name/si/sing-box/cronet-go.patch b/pkgs/by-name/si/sing-box/cronet-go.patch new file mode 100644 index 000000000000..b50ab0e12c2b --- /dev/null +++ b/pkgs/by-name/si/sing-box/cronet-go.patch @@ -0,0 +1,27 @@ +--- a/internal/cronet/loader_unix.go ++++ b/internal/cronet/loader_unix.go +@@ -68,10 +68,11 @@ + } + + var searchPaths []string +- executablePath, err := os.Executable() +- if err == nil { +- searchPaths = append(searchPaths, filepath.Dir(executablePath)) +- } ++ // executablePath, err := os.Executable() ++ // if err == nil { ++ // searchPaths = append(searchPaths, filepath.Dir(executablePath)) ++ // } ++ searchPaths = append(searchPaths, "@out@/lib") + + if ldPath := os.Getenv("LD_LIBRARY_PATH"); ldPath != "" { + paths := filepath.SplitList(ldPath) +@@ -85,7 +86,7 @@ + } + } + +- searchPaths = append(searchPaths, "/usr/local/lib", "/usr/lib") ++ // searchPaths = append(searchPaths, "/usr/local/lib", "/usr/lib") + + for _, searchPath := range searchPaths { + fullPath := filepath.Join(searchPath, libName) diff --git a/pkgs/by-name/si/sing-box/package.nix b/pkgs/by-name/si/sing-box/package.nix index 467f0ae8be37..42960d0db491 100644 --- a/pkgs/by-name/si/sing-box/package.nix +++ b/pkgs/by-name/si/sing-box/package.nix @@ -1,17 +1,28 @@ { lib, buildGoModule, - fetchFromGitHub, - installShellFiles, + buildPackages, coreutils, - nix-update-script, + cronet-go, + fetchFromGitHub, + go, + installShellFiles, nixosTests, -}: + stdenvNoCC, + withStaticCronet ? true, + withNaiveOutbound ? true, +}: +assert lib.assertMsg ( + withNaiveOutbound -> !withStaticCronet -> stdenvNoCC.hostPlatform.isLinux +) "Dynamic linking to cronet-go is only available on Linux."; buildGoModule (finalAttrs: { pname = "sing-box"; version = "1.13.15"; + __structuredAttrs = true; + + # nixpkgs-update: no auto update src = fetchFromGitHub { owner = "SagerNet"; repo = "sing-box"; @@ -34,15 +45,28 @@ buildGoModule (finalAttrs: { "with_ocm" "badlinkname" "tfogo_checklinkname0" - ]; + ] + ++ lib.optional withNaiveOutbound "with_naive_outbound" + ++ lib.optional (withNaiveOutbound && !withStaticCronet) "with_purego"; subPackages = [ "cmd/sing-box" ]; - env.CGO_ENABLED = 0; + env = { + CGO_ENABLED = 0; + } + // lib.optionalAttrs (withNaiveOutbound && withStaticCronet) { + CGO_ENABLED = 1; + CGO_LDFLAGS = "-fuse-ld=lld"; + }; - nativeBuildInputs = [ installShellFiles ]; + nativeBuildInputs = [ + installShellFiles + ] + ++ lib.optional (withNaiveOutbound && withStaticCronet) buildPackages.rustc.llvmPackages.bintools; + + buildInputs = lib.optional (withNaiveOutbound && withStaticCronet) cronet-go; ldflags = [ "-X=github.com/sagernet/sing-box/constant.Version=${finalAttrs.version}" @@ -50,6 +74,17 @@ buildGoModule (finalAttrs: { "-checklinkname=0" ]; + postConfigure = lib.optionalString withNaiveOutbound '' + pushd vendor/github.com/sagernet/cronet-go + chmod -R u+w . + cp -r ${cronet-go}/ . + # for !withStaticCronet + patch -p1 < ${./cronet-go.patch} + substituteInPlace internal/cronet/loader_unix.go \ + --subst-var out + popd + ''; + postInstall = '' installShellCompletion release/completions/sing-box.{bash,fish,zsh} @@ -60,10 +95,12 @@ buildGoModule (finalAttrs: { install -Dm444 release/config/sing-box.rules $out/share/polkit-1/rules.d/sing-box.rules install -Dm444 release/config/sing-box-split-dns.xml $out/share/dbus-1/system.d/sing-box-split-dns.conf + '' + + lib.optionalString (withNaiveOutbound && !withStaticCronet) '' + ln -s "${cronet-go}/lib/${go.GOOS}_${go.GOARCH}/libcronet.so" "$out/lib/" ''; passthru = { - updateScript = nix-update-script { }; tests = { inherit (nixosTests) sing-box; }; };