Last updated 2022-02-17, see Changelog
Building plugin-supporting applications in Go is currently a sub-par experience.
Go does have a
-buildmode=plugin that lets you create
.so files, which can then be loaded as plugin in a Go application. But:
- It doesn’t work on Windows.
- It doesn’t work well with vendoring.
- Since shared libraries are deprecated, every plugin is huge because they can’t share even the standard library with the main application.
In this article, I will show how to use Nix Flakes as a build system for Go supporting plugin management. This article has three parts, excluding the abstract you are just reading:
I will show how to set up your application as Nix Flake that can be extended with plugins, and I will show how to write a first plugin as proof-of-concept. This part is the one most focused on Nix Flakes.
I will show how to give your application a plugin API and how to consume it from within a plugin. I will also show how to depend on external C libraries.
Independently from plugins, this part may also be of interest to people who simply want to cross-compile Go applications with C dependencies.
I will assume you are familiar with Nix Flakes and Go. In particular, I will show some complex Nix expressions and assume you can read them. If you don’t know much about Nix and are in a hurry, I recommend this article for a quick overview of the language and Flakes. A proper way to learn about Nix is the Nix Pills series, and this series about Nix Flakes.
This article may also be interesting for people who simply are curious how to use Nix with Go.
To follow the instructions in this article, you need the
nix utility installed and Flake support enabled.
Executing the commands shown in this article will potentially trigger downloads of multiple GB of data.
If you want to follow the article’s instructions, create an empty directory that will be the root for all files and subdirectories we’ll create
We need all files processed by Nix to be checked in to version control, so initialize a git repository in this directory with
All code discussed in this article is available on GitHub, so if you don’t want to copy code from the article, you can also just clone that repository.
- +Working RPi 4 build and .deb package creation.