···11# nu_plugin_dbus
2233+[Nushell](https://nushell.sh/) plugin for interacting with [D-Bus](https://dbus.freedesktop.org/)
44+55+With the commands provided by this plugin, you can interact with many of the desktop-oriented
66+systems on UNIX-like systems that use D-Bus, including Linux and FreeBSD. You can control media
77+players, on-screen displays, power policies, and even administer services.
88+99+Nushell provides a particularly nice environment for interacting with D-Bus, as both support typed
1010+structured data, and interacting with this on a traditional UNIX command line with tools like
1111+`dbus-send` and `busctl` is cumbersome and tricky to automate.
1212+1313+This plugin automatically determines the correct input types through D-Bus introspection when
1414+available, unlike either of the aforementioned tools, making it easier to interact with objects on
1515+the bus without having to implement boilerplate from documentation.
1616+317## Install with Cargo
418519Run: `cargo install --locked nu_plugin_dbus`
···1832 Subcommands:
1933 dbus call - Call a method and get its response
2034 dbus get - Get a D-Bus property
2121- dbus get-all - Get all D-Bus property for the given objects
3535+ dbus get-all - Get all D-Bus properties for the given object
2236 dbus introspect - Introspect a D-Bus object
2337 dbus list - List all available connection names on the bus
2424- dbus set - Get all D-Bus property for the given objects
2525-2626- Flags:
2727- -h, --help - Display the help message for this command
2828-2929-## `dbus introspect`
3030-3131- Introspect a D-Bus object
3232-3333- Returns information about available nodes, interfaces, methods, signals, and properties on the given object path
3434-3535- Search terms: dbus
3636-3737- Usage:
3838- > dbus introspect {flags} <object>
3838+ dbus set - Set a D-Bus property
39394040 Flags:
4141 -h, --help - Display the help message for this command
4242- --session - Send to the session message bus (default)
4343- --system - Send to the system message bus
4444- --started - Send to the bus that started this process, if applicable
4545- --bus <String> - Send to the bus server at the given address
4646- --peer <String> - Send to a non-bus D-Bus server at the given address. Will not call the Hello method on initialization.
4747- --timeout <Duration> - How long to wait for a response
4848- --dest (required parameter) <String> - The name of the connection that owns the object
49425050- Parameters:
5151- object <string>: The path to the object to introspect
5252-5353- Examples:
5454- Look at the MPRIS2 interfaces exposed by Spotify
5555- > dbus introspect --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 | explore
5656-5757- Get methods exposed by KDE Plasma's on-screen display service
5858- > dbus introspect --dest=org.kde.plasmashell /org/kde/osdService | get interfaces | where name == org.kde.osdService | get 0.methods
5959-6060- List objects exposed by KWin
6161- > dbus introspect --dest=org.kde.KWin / | get children | select name
6262-6363-## `dbus call`
4343+# `dbus call`
64446545 Call a method and get its response
6646···9272 method <string>: The name of the method to send
9373 ...args <any>: Arguments to send with the method call
94747575+ Input/output types:
7676+ ╭───┬─────────┬────────╮
7777+ │ # │ input │ output │
7878+ ├───┼─────────┼────────┤
7979+ │ 0 │ nothing │ any │
8080+ ╰───┴─────────┴────────╯
8181+9582 Examples:
9683 Ping the D-Bus server itself
9784 > dbus call --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.Peer Ping
···9986 Show a notification on the desktop for 5 seconds
10087 > dbus call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications Notify "Floppy disks" 0 "media-floppy" "Rarely seen" "But sometimes still used" [] {} 5000
10188102102-## `dbus get`
8989+# `dbus get`
1039010491 Get a D-Bus property
10592···123110 interface <string>: The name of the interface the property belongs to
124111 property <string>: The name of the property to read
125112113113+ Input/output types:
114114+ ╭───┬─────────┬────────╮
115115+ │ # │ input │ output │
116116+ ├───┼─────────┼────────┤
117117+ │ 0 │ nothing │ any │
118118+ ╰───┴─────────┴────────╯
119119+126120 Examples:
127121 Get the currently playing song in Spotify
128122 > dbus get --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player Metadata
···133127 │ xesam:url │ https://open.spotify.com/track/51748BvzeeMs4PIdPuyZmv │
134128 ╰──────────────┴───────────────────────────────────────────────────────╯
135129136136-## `dbus get-all`
130130+# `dbus get-all`
137131138138- Get all D-Bus property for the given objects
132132+ Get all D-Bus properties for the given object
139133140134 Search terms: dbus
141135···156150 object <string>: The path to the object to read the property from
157151 interface <string>: The name of the interface the property belongs to
158152153153+ Input/output types:
154154+ ╭───┬─────────┬────────╮
155155+ │ # │ input │ output │
156156+ ├───┼─────────┼────────┤
157157+ │ 0 │ nothing │ record │
158158+ ╰───┴─────────┴────────╯
159159+159160 Examples:
160161 Get the current player state of Spotify
161162 > dbus get-all --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player
···165166 │ PlaybackStatus │ Paused │
166167 ╰────────────────┴────────╯
167168168168-## `dbus set`
169169+# `dbus introspect`
170170+171171+ Introspect a D-Bus object
169172170170- Get all D-Bus property for the given objects
173173+ Returns information about available nodes, interfaces, methods, signals, and properties on the given object path
171174172175 Search terms: dbus
173176174177 Usage:
175175- > dbus set {flags} <object> <interface> <property> <value>
178178+ > dbus introspect {flags} <object>
176179177180 Flags:
178181 -h, --help - Display the help message for this command
···182185 --bus <String> - Send to the bus server at the given address
183186 --peer <String> - Send to a non-bus D-Bus server at the given address. Will not call the Hello method on initialization.
184187 --timeout <Duration> - How long to wait for a response
185185- --signature <String> - Signature of the value to set, in D-Bus format.
186186- If not provided, it will be determined from introspection.
187187- If --no-introspect is specified and this is not provided, it will be guessed (poorly)
188188- --dest (required parameter) <String> - The name of the connection to write the property on
188188+ --dest (required parameter) <String> - The name of the connection that owns the object
189189190190 Parameters:
191191- object <string>: The path to the object to write the property on
192192- interface <string>: The name of the interface the property belongs to
193193- property <string>: The name of the property to write
194194- value <any>: The value to write to the property
191191+ object <string>: The path to the object to introspect
192192+193193+ Input/output types:
194194+ ╭───┬─────────┬────────╮
195195+ │ # │ input │ output │
196196+ ├───┼─────────┼────────┤
197197+ │ 0 │ nothing │ record │
198198+ ╰───┴─────────┴────────╯
195199196200 Examples:
197197- Set the volume of Spotify to 50%
198198- > dbus set --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player Volume 0.5
201201+ Look at the MPRIS2 interfaces exposed by Spotify
202202+ > dbus introspect --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 | explore
199203200200-## `dbus list`
204204+ Get methods exposed by KDE Plasma's on-screen display service
205205+ > dbus introspect --dest=org.kde.plasmashell /org/kde/osdService | get interfaces | where name == org.kde.osdService | get 0.methods
206206+207207+ List objects exposed by KWin
208208+ > dbus introspect --dest=org.kde.KWin / | get children | select name
209209+210210+# `dbus list`
201211202212 List all available connection names on the bus
203213···220230 Parameters:
221231 pattern <string>: An optional glob-like pattern to filter the result by (optional)
222232233233+ Input/output types:
234234+ ╭───┬─────────┬──────────────╮
235235+ │ # │ input │ output │
236236+ ├───┼─────────┼──────────────┤
237237+ │ 0 │ nothing │ list<string> │
238238+ ╰───┴─────────┴──────────────╯
239239+223240 Examples:
224241 List all names available on the bus
225242 > dbus list
···238255 │ 0 │ org.mpris.MediaPlayer2.spotify │
239256 │ 1 │ org.mpris.MediaPlayer2.kdeconnect.mpris_000001 │
240257 ╰───┴────────────────────────────────────────────────╯
258258+259259+# `dbus set`
260260+261261+ Set a D-Bus property
262262+263263+ Search terms: dbus
264264+265265+ Usage:
266266+ > dbus set {flags} <object> <interface> <property> <value>
267267+268268+ Flags:
269269+ -h, --help - Display the help message for this command
270270+ --session - Send to the session message bus (default)
271271+ --system - Send to the system message bus
272272+ --started - Send to the bus that started this process, if applicable
273273+ --bus <String> - Send to the bus server at the given address
274274+ --peer <String> - Send to a non-bus D-Bus server at the given address. Will not call the Hello method on initialization.
275275+ --timeout <Duration> - How long to wait for a response
276276+ --signature <String> - Signature of the value to set, in D-Bus format.
277277+ If not provided, it will be determined from introspection.
278278+ If --no-introspect is specified and this is not provided, it will be guessed (poorly)
279279+ --dest (required parameter) <String> - The name of the connection to write the property on
280280+281281+ Parameters:
282282+ object <string>: The path to the object to write the property on
283283+ interface <string>: The name of the interface the property belongs to
284284+ property <string>: The name of the property to write
285285+ value <any>: The value to write to the property
286286+287287+ Input/output types:
288288+ ╭───┬─────────┬─────────╮
289289+ │ # │ input │ output │
290290+ ├───┼─────────┼─────────┤
291291+ │ 0 │ nothing │ nothing │
292292+ ╰───┴─────────┴─────────╯
293293+294294+ Examples:
295295+ Set the volume of Spotify to 50%
296296+ > dbus set --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player Volume 0.5
297297+
+9-3
src/main.rs
···11use nu_plugin::{serve_plugin, MsgPackSerializer, Plugin, EvaluatedCall, LabeledError};
22-use nu_protocol::{PluginSignature, Value, SyntaxShape, PluginExample, Span};
22+use nu_protocol::{PluginSignature, Value, SyntaxShape, PluginExample, Span, Type};
3344mod config;
55mod client;
···3434 .accepts_dbus_client_options()
3535 .accepts_timeout()
3636 .usage("Introspect a D-Bus object")
3737+ .input_output_type(Type::Nothing, Type::Record(vec![]))
3738 .extra_usage("Returns information about available nodes, interfaces, methods, \
3839 signals, and properties on the given object path")
3940 .required_named("dest", SyntaxShape::String,
···6970 .accepts_timeout()
7071 .usage("Call a method and get its response")
7172 .extra_usage("Returns an array if the method call returns more than one value.")
7373+ .input_output_type(Type::Nothing, Type::Any)
7274 .named("signature", SyntaxShape::String,
7375 "Signature of the arguments to send, in D-Bus format.\n \
7476 If not provided, they will be determined from introspection.\n \
···110112 .accepts_dbus_client_options()
111113 .accepts_timeout()
112114 .usage("Get a D-Bus property")
115115+ .input_output_type(Type::Nothing, Type::Any)
113116 .required_named("dest", SyntaxShape::String,
114117 "The name of the connection to read the property from",
115118 None)
···139142 .is_dbus_command()
140143 .accepts_dbus_client_options()
141144 .accepts_timeout()
142142- .usage("Get all D-Bus property for the given objects")
145145+ .usage("Get all D-Bus properties for the given object")
146146+ .input_output_type(Type::Nothing, Type::Record(vec![]))
143147 .required_named("dest", SyntaxShape::String,
144148 "The name of the connection to read the property from",
145149 None)
···164168 .is_dbus_command()
165169 .accepts_dbus_client_options()
166170 .accepts_timeout()
167167- .usage("Get all D-Bus property for the given objects")
171171+ .usage("Set a D-Bus property")
172172+ .input_output_type(Type::Nothing, Type::Nothing)
168173 .named("signature", SyntaxShape::String,
169174 "Signature of the value to set, in D-Bus format.\n \
170175 If not provided, it will be determined from introspection.\n \
···196201 .accepts_timeout()
197202 .usage("List all available connection names on the bus")
198203 .extra_usage("These can be used as arguments for --dest on any of the other commands.")
204204+ .input_output_type(Type::Nothing, Type::List(Type::String.into()))
199205 .optional("pattern", SyntaxShape::String,
200206 "An optional glob-like pattern to filter the result by")
201207 .plugin_examples(vec![