Embedding External Binaries
You may need to embed external binaries to add additional functionality to your application or prevent users from installing additional dependencies (e.g., Node.js or Python). We call this binary a sidecar
.
Binaries are executables written in any programming language. Common use cases are Python CLI applications or API servers bundled using pyinstaller
.
To bundle the binaries of your choice, you can add the externalBin
property to the tauri > bundle
object in your tauri.conf.json
.
The externalBin
configuration expects a list of strings targeting binaries either with absolute or relative paths.
Here is a Tauri configuration snippet to illustrate a sidecar configuration:
To make the external binary work on each supported architecture, a binary with the same name and a -$TARGET_TRIPLE
suffix must exist on the specified path.
For instance, "externalBin": ["binaries/my-sidecar"]
requires a src-tauri/binaries/my-sidecar-x86_64-unknown-linux-gnu
executable on Linux or src-tauri/binaries/my-sidecar-aarch64-apple-darwin
on Mac OS with Apple Silicon.
You can find your current platform’s -$TARGET_TRIPLE
suffix by looking at the host:
property reported by the following command:
If the grep
and cut
commands are available, as they should on most Unix systems, you can extract the target triple directly with the following command:
On Windows you can use PowerShell instead:
Here’s a Node.js script to append the target triple to a binary:
Note that this script will not work if you compile for a different architecture than the one its running on, so only use it as a starting point for your own build scripts.
Running it from Rust
On the Rust side, import the tauri_plugin_shell::ShellExt
trait and call the shell().sidecar()
function on the AppHandle:
You can place this code inside a Tauri command to easily pass the AppHandle or you can store a reference to the AppHandle in the builder script to access it elsewhere in your application.
Running it from JavaScript
In the JavaScript code, import the Command
class from the @tauri-apps/plugin-shell
module and use the sidecar
static method.
Passing arguments
You can pass arguments to Sidecar commands just like you would for running normal Command.
Arguments can be either static (e.g. -o
or serve
) or dynamic (e.g. <file_path>
or localhost:<PORT>
). You define the arguments in the exact order in which you’d call them. Static arguments are defined as-is, while dynamic arguments can be defined using a regular expression.
First, define the arguments that need to be passed to the sidecar command in src-tauri/capabilities/default.json
:
Then, to call the sidecar command, simply pass in all the arguments as an array.
In Rust:
In JavaScript:
© 2024 Tauri Contributors. CC-BY / MIT