From 3610987241faf967ad0299d25e949dfecb1c2659 Mon Sep 17 00:00:00 2001 From: Alex David Date: Sun, 28 Sep 2025 15:44:36 -0700 Subject: [PATCH] Allow multiple calls to dbus combinator Before this change, additional calls to the dbus combinator would add multiple `xdg-dbus-proxy` instances, but only the latest call would be forwarded into the jail. With this change, all permissions are saved in state, then, in a deferred `add-runtime` we add a single `xdg-dbus-proxy` exposing the union of all previous dbus calls. This should allow writing combinators that expose dbus permissions like notifications, or reading light/dark mode changes which can be composed just like the other non-dbus combinators. --- lib/combinators.nix | 114 ++++++++++++++++++++---------- lib/jail.nix | 1 + tests/spec/DBusCombinatorsSpec.hs | 35 +++++++++ 3 files changed, 113 insertions(+), 37 deletions(-) diff --git a/lib/combinators.nix b/lib/combinators.nix index ea3a15c..23e04e5 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -980,6 +980,11 @@ rec { ]; } ``` + + Multiple calls to `dbus` will only run a single xdg-dbus-proxy with a + union of all passed own/talk/see/call/broadcast permissions. This makes + it possible to write wrapper combinators for specific dbus permissions + and use them in conjunction with each other. ''; __functor = _: @@ -990,44 +995,79 @@ rec { call ? [ ], broadcast ? [ ], }: - let - proxy = jail "xdg-dbus-proxy" pkgs.xdg-dbus-proxy [ - unsafe-dbus - (readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\"")) - ]; - - args = - [ "--filter" ] - ++ map (id: "--own=${lib.escapeShellArg id}") own - ++ map (id: "--talk=${lib.escapeShellArg id}") talk - ++ map (id: "--see=${lib.escapeShellArg id}") see - ++ map (id: "--call=${lib.escapeShellArg id}") call - ++ map (id: "--broadcast=${lib.escapeShellArg id}") broadcast; - in compose [ - (add-runtime '' - PROXIED_DBUS_SOCKET_DIR=$(mktemp -d) - export PROXIED_DBUS_SOCKET_DIR - PROXIED_DBUS_SOCKET="$PROXIED_DBUS_SOCKET_DIR/socket" - mkfifo "$PROXIED_DBUS_SOCKET_DIR/ready" - exec {XDG_DBUS_PROXY_READY_FD}<>"$PROXIED_DBUS_SOCKET_DIR/ready" - ${lib.getExe proxy} \ - "$DBUS_SESSION_BUS_ADDRESS" \ - "$PROXIED_DBUS_SOCKET" \ - --fd="$XDG_DBUS_PROXY_READY_FD" \ - ${lib.concatStringsSep " " args} \ - & - PROXY_PID=$! - IFS= read -rn1 -u "$XDG_DBUS_PROXY_READY_FD" - '') - (add-cleanup '' - kill "$PROXY_PID" - if [ -e "''${PROXIED_DBUS_SOCKET_DIR-}" ]; then - rm -rf "$PROXIED_DBUS_SOCKET_DIR" - fi - '') - (readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\"")) - (set-env "DBUS_SESSION_BUS_ADDRESS" (noescape "\"unix:path=$PROXIED_DBUS_SOCKET\"")) + # Just set the options on state so dbus can be called multiple times + ( + state: + state + // { + dbusPermissions = state.dbusPermissions ++ [ + { + inherit + own + talk + see + call + broadcast + ; + } + ]; + } + ) + # Then add deferred runtime logic to spin up xdg-dbus-proxy: + (include-once "dbus" ( + defer ( + state: + let + proxy = jail "xdg-dbus-proxy" pkgs.xdg-dbus-proxy [ + unsafe-dbus + (readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\"")) + ]; + + getFlags = + type: + lib.pipe state.dbusPermissions [ + (map (p: p.${type})) + lib.flatten + lib.unique + (map (id: "--${type}=${lib.escapeShellArg id}")) + ]; + + args = + [ "--filter" ] + ++ getFlags "own" + ++ getFlags "talk" + ++ getFlags "see" + ++ getFlags "call" + ++ getFlags "broadcast"; + in + compose [ + (add-runtime '' + PROXIED_DBUS_SOCKET_DIR=$(mktemp -d) + export PROXIED_DBUS_SOCKET_DIR + PROXIED_DBUS_SOCKET="$PROXIED_DBUS_SOCKET_DIR/socket" + mkfifo "$PROXIED_DBUS_SOCKET_DIR/ready" + exec {XDG_DBUS_PROXY_READY_FD}<>"$PROXIED_DBUS_SOCKET_DIR/ready" + ${lib.getExe proxy} \ + "$DBUS_SESSION_BUS_ADDRESS" \ + "$PROXIED_DBUS_SOCKET" \ + --fd="$XDG_DBUS_PROXY_READY_FD" \ + ${lib.concatStringsSep " " args} \ + & + PROXY_PID=$! + IFS= read -rn1 -u "$XDG_DBUS_PROXY_READY_FD" + '') + (add-cleanup '' + kill "$PROXY_PID" + if [ -e "''${PROXIED_DBUS_SOCKET_DIR-}" ]; then + rm -rf "$PROXIED_DBUS_SOCKET_DIR" + fi + '') + (readwrite (noescape "\"$PROXIED_DBUS_SOCKET_DIR\"")) + (set-env "DBUS_SESSION_BUS_ADDRESS" (noescape "\"unix:path=$PROXIED_DBUS_SOCKET\"")) + ] state + ) + )) ]; }; diff --git a/lib/jail.nix b/lib/jail.nix index aab4b7c..df8b120 100644 --- a/lib/jail.nix +++ b/lib/jail.nix @@ -43,6 +43,7 @@ let cleanup = [ ]; # See cleanup combinator deferredPermissions = [ ]; # See defer combinator additionalRuntimeClosures = [ ]; # See bind-nix-store-runtime-closure + dbusPermissions = [ ]; # See dbus combinator }; in lib.pipe initialState ( diff --git a/tests/spec/DBusCombinatorsSpec.hs b/tests/spec/DBusCombinatorsSpec.hs index 9ecadcc..d91aedc 100644 --- a/tests/spec/DBusCombinatorsSpec.hs +++ b/tests/spec/DBusCombinatorsSpec.hs @@ -145,6 +145,41 @@ spec = inTestM $ do sendDBusOutput `shouldBe` "Failed to call dbus method\n" methodCalls `shouldBe` [] + describe "multiple calls to the dbus combinator" $ do + it "exposes a union of all of the specified permissions" $ do + let mockMethod1 = + MockDBusMethod + { objPath = "/foo/object1", + ifaceName = "foo.iface1.Iface", + methodName = "method1" + } + let mockMethod2 = + MockDBusMethod + { objPath = "/foo/object2", + ifaceName = "foo.iface2.Iface", + methodName = "method2" + } + ((sendDBusOutput, method2Calls), method1Calls) <- withDBusClient $ \mockBusName client -> do + withMockDBusMethodHandler client mockMethod1 "return value 1" $ do + withMockDBusMethodHandler client mockMethod2 "return value 2" $ do + runNixDrv + [i| + jail + "send-multiple" + (sh '' + ${lib.getExe (#{callDBusMethodNixDrv mockBusName mockMethod1 "arg1"})} + ${lib.getExe (#{callDBusMethodNixDrv mockBusName mockMethod2 "arg2"})} + '') + (c: [ + (c.dbus { call = [ #{toNixString $ toProxyRule mockBusName mockMethod1} ]; }) + (c.dbus { call = [ #{toNixString $ toProxyRule mockBusName mockMethod2} ]; }) + ]) + |] + liftIO $ do + sendDBusOutput `shouldBe` " return value 1 return value 2" + method1Calls `shouldBe` ["arg1"] + method2Calls `shouldBe` ["arg2"] + -- * DBus test helpers withDBusClient :: (DBus.BusName -> DBus.Client -> TestM a) -> TestM a