Example config for NixOS as host for lxd/lxc containers?

I want to run some other linuxes on my host NixOS system, using lxd/lxc containers. I don’t see much documentation or examples on this, besides one blog post linked from wiki, which doesn’t answer all my questions.

For example, when I first install and run lxd and lxc, they advise to run lxd init if this is the first time running them on this system, which it is.

Is that the proper way to setup lxd in NixOS, or should that be done in configuration.nix instead? Seems too imperative, and like there should be a declarative config option.

There doesn’t appear to be a declarative lxd config property for custom config, but there are several lxc properties for this.

Does anyone have an example config file for setting up these properties?

Also, lxd on ZFS wants to use a ZFS pool for its backend storage, but can it be configured to use a ZFS dataset within an existing pool instead?

AFAIK, currently in NixOS there is no proper (declarative) way to set up LXD. However, after some trial and error I have found a solution that seems to work reliably:

The trick is: lxd init can take a --preseed argument. Nix generates the preseed config and feeds this into lxd init as soon as the lxd.socket is available. Doing the lxd init again on an already initialized system seems to have no effect, just like I want it.

3 Likes