How to make app plugins/extensions?

I don't know if this is possible. If I create an application, distributed as a closed-source native binary, can I also distribute plugins a separate binary files that extend the functionality of that app? Or is there not enough stable ABI for this?

The plugin would need to be able to use types in the application, and implement traits defined in the application.

// Define in app binary
pub trait Plugin {
    fn name(&self) -> String;
    fn startup(&self) -> anyhow::Result<()>;
    ...
}
pub struct Document {
    fn block(&self, node_id: NodeId) -> Option<&Block> { ... }
    fn span(&self, node_id: NodeId) -> Option<&Span> { ... }
}

// In plugin binary
struct ThisPlugin { ... }
impl Plugin for ThisPlugin { ... }

fn do_stuff(doc: &Document) {
    let b = doc.block(id) ...
} 

From what I know, you have 2 options:

  1. Use C ABI
  2. Create plugins as external apps and use some kind of IPC (either OS-provided like COM or D-Bus or custom one, based on sockets, shared memory or pipes).

The second option also gives you ability to write plugins in any language and even run them on another host (if you decided to use sockets), but it is not an option for hot paths.
In both cases, you would need to publish a "Plugin SDK" openly.

There are some hacks, though: abi_stable - Rust but I've never tried them.

In order to achieve this, no matter what strategy you use beyond that, you must put the relevant types and traits in a crate whose source is available to the plugin authors.[1] This crate might be a plugin API crate without any of the logic of your application, though.


  1. Technically you can distribute a compiled .rlib, but this requires the author to use a specific version of rustc, and doesn't necessarily hide much. ↩︎

The "I" there is probably more important than you realized.

If you build it all together at one time, and want to ship subsets, that's entirely feasible.

If you want to add extensions later, particular extensions by others, things get much more complicated.

Unless you want to have a plugin as a closure having access to all object of the main application you need to define:

  1. plugin interface, so your main app will use the interface to invoke the plugin functionality
  2. plugin discovery mechanism, so you main application ca figure out what plugins are available and for which functionality
  3. plugin authentication mechanism (optional) when your main application can decide if the plugin is legit or possible malware

After defining that, implementing a plugin should be fairly easy.

A third option is using WASM, which has the advantage of allowing plugins to work on all CPU architectures and OSs at once and being more secure.

it IS possible!

there are a couple of projects already offering such (WASM) plugins that i know of, one of them is mine.

have a look at xapi-rs (the server proper) which uses 2 additional crates: xapi-data and xapi-interfaces.

the other, xapi-interfaces, contains Rust Traits, plugins implement. that same crate also contains a single trait: Hashing w/ a single one-shot function. 2 plugins implementing Hashing, both loaded from a special location hard-wired in the server are included. the README file explains how to build the plugins. they both

If your plugins are simple, something similar to xapi-rs plugins should be ok, otherwise...

there's another project on github called: nur-cms. it has a more elaborate Plugin structure w/ a GUI front-end!

good luck.

it IS possible!

there are a couple of projects already offering such plugins that i know of, one of them is mine: xapi-rs.

have a look at xapi-rs which uses 2 additional crates: xapi-data and xapi-interfaces.

as previously mentioned in this thread, xapi-data contains the data types that can be used by any crate using the same data the xapi-rs server uses.

the other, xapi-interfaces contains Rust Traits, plugins implement. that same crate also contains a single trait: Hashing w/ a single function. 2 plugins implementing Hashing, both loaded from a special location hard-wired in the server are included. the README file explains how to build the plugins.

If your plugins are simple, something similar to xapi-rs plugins should be ok.

there's another project on github called: nur-cms. it has a more elaborate Plugin structure.

As you might guess from the wide variety of responses you are getting, advising you on this is greatly informed by knowing the actual intent of the plugin system. It is unlikely your code objects are very large unless you are embedding large binary objects like images, in which case you can separately ship and load such blobs to begin with, so you'd ideally start with an answer for why you didn't just include the functionality of the plugin inside the application itself.

A common answer is "because I want to enable someone else to write a plugin" which is where you would start encountering problems with ABI anyways.

I’m not the OP, but one noteworthy reason to do this is to have a less adversarial relationship with the users of your software, if the plugins are additional purchases:

  • With plugin files, the application does not need any kind of license-key check preventing the plugin code from running; instead, the plugin simply doesn’t exist to be loaded.
  • “This code is on your computer, but there is a mechanism preventing you from using it” can be distressing or tempting in a way “you don’t have the code on your computer, because you have not purchased nor pirated it” isn’t.

If the plugins are merely functionality that is of interest to fewer users, then I agree that incorporating them into the main executable makes sense, since it is so much simpler to implement and more flexible.

I'm looking at a few different situations, so all this advice is valuable. But the main one is that I'm working on an app that, by itself, is a kind of general purpose document editor (something like Obsidian, but not markdown), and then I'm imagining extensions that are almost like separate apps within that environment. For typical "in app purchase", like enabling the full app from a free version, I just ship the code and have the purchase enable it. But some of these extensions/plugins I'm imaging seem like they should be separate downloads.

If you wish to ship this on some app store, then you may wish to look at whether they help you deliver this kind of extension or whether you have to do all the grunt work yourself.

I mention this because the simplest approach is definitely what @scottmcm hinted at: just compile everything with the same compiler because the compiler does, as it turns out, have to agree with itself on its own ABI for a given function call/type/etc.

You can even check in a rust-toolchain.toml to make sure you know the version of rustc you were using for each version of the app so there's no chance of accidentally getting that wrong.

If you however have to have the extensions and the core app still work while being effectively separately versioned because of how distribution works, then the more complicated solutions become helpful.

Another reason for plugins is to allow adding more features wi

I thought rust was allowed to have a different ABI even for the exact same compiler version due to the rust ABI being completely unspecified?

I work on the ABI code.

No, that's not how it works.

The ABI is unspecified, yes, but that includes almost every program you have ever written. Yet unless you interact with a source of randomness, deliberately or non-deliberately (that means you don't sniff at the addresses of pointers), the program's results will be deterministic. This is required to reason about it at all.

So it does have rules. Unfortunately for you, the rules are what the source code does. Frankly, given C ABIs are also not part of the C language, but rather are separate specifications, and in practice have to be updated whenever two compilers disagree on how things should be implemented, one could also argue that C has no ABI and that their rules are also "what the source code does". Everyone relies on this stuff and prattles on about it yet has no idea about what is even in the sausage they are eating, nevermind how it's made.

But Rust has a deterministic ABI. It is so deterministic that we can have two different codegen backends generate a pair of Rust codegen units that are intended to be linked, and those can correctly interoperate across the ABI. Even when we have a compiler flag specifically for injecting some randomness, it still is designed to work in a way that will be deterministic and Rust will consistently reproduce the same ABI.

This is partly because layout is a core ABI concern, and the in-memory layout of your program simply doesn't work if your codegen units don't agree on how to use a given static!

What you may be surprised by, and is the only real difference, is that given a function FnN with arity N, and a function FnM with arity M where M < N, then assuming all types match through fn(T0, T1, T2 .. TM, TN), FnM is not required to be a subset of FnN's ABI. This is already untrue in the presence of C's variable arguments, as that is often a total special-case in many ABIs.

I don't know how to do this either. In C, you'd build a .so/.dylib/.dll and the do dlopen and get the function pointers. Is it the same in Rust? Ie, the plugin would be a shared lib with some "extern C" function definitions to look up from the main program binary? Or is there a more rusty way to do it?

have a look at this.