From 57fbdcb3e29f29b48f74dd89f2537d0ea53f3c88 Mon Sep 17 00:00:00 2001 From: Merrkry Date: Sun, 5 Apr 2026 20:53:56 +0800 Subject: [PATCH] sing-box: allow empty settings --- .../modules/services/networking/sing-box.nix | 15 ++++++---- nixos/tests/sing-box.nix | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/nixos/modules/services/networking/sing-box.nix b/nixos/modules/services/networking/sing-box.nix index c6ae9cb5c290..3076d94e83b9 100644 --- a/nixos/modules/services/networking/sing-box.nix +++ b/nixos/modules/services/networking/sing-box.nix @@ -50,6 +50,7 @@ in serviceConfig = { User = "sing-box"; Group = "sing-box"; + ConfigurationDirectory = "sing-box"; StateDirectory = "sing-box"; StateDirectoryMode = "0700"; RuntimeDirectory = "sing-box"; @@ -62,11 +63,15 @@ in chown --reference=/run/sing-box /run/sing-box/config.json ''; in - "+${script}"; - ExecStart = [ - "" - "${lib.getExe cfg.package} -D \${STATE_DIRECTORY} -C \${RUNTIME_DIRECTORY} run" - ]; + lib.mkIf (cfg.settings != { }) "+${script}"; + ExecStart = + let + configDir = if cfg.settings != { } then "RUNTIME_DIRECTORY" else "CONFIGURATION_DIRECTORY"; + in + [ + "" + "${lib.getExe cfg.package} -D \${STATE_DIRECTORY} -C \${${configDir}} run" + ]; }; # After= is specified by upstream requires = [ "network-online.target" ]; diff --git a/nixos/tests/sing-box.nix b/nixos/tests/sing-box.nix index cc890fa6fe69..820675d69a11 100644 --- a/nixos/tests/sing-box.nix +++ b/nixos/tests/sing-box.nix @@ -511,6 +511,31 @@ in }; }; }; + + empty_settings = + { ... }: + { + environment.etc."sing-box/config.json".text = builtins.toJSON { + inbounds = [ + { + type = "mixed"; + listen = "127.0.0.1"; + listen_port = 1088; + } + ]; + outbounds = [ + { + type = "direct"; + tag = "outbound:direct"; + } + ]; + }; + + services.sing-box = { + enable = true; + settings = { }; + }; + }; }; testScript = '' @@ -558,6 +583,9 @@ in fakeip.wait_for_unit("sing-box.service") fakeip.wait_until_succeeds("ip route get ${hosts."${target_host}"} | grep 'dev ${tunInbound.interface_name}'") fakeip.succeed("dig +short A ${target_host} @${target_host} | grep '^198.18.'") + + with subtest("empty settings"): + empty_settings.wait_for_unit("sing-box.service") ''; }