Nixos-containers configuration with flakes

Hello,

I’m trying to create some NixOS Containers with flakes, rather than the old configuration.nix way, following these blog posts

https://xeiaso.net/blog/nix-flakes-3-2022-04-07

I think I understand that the configuration in the flake is what I would have previously written in the containers.<name>.config option - that is, the configuration inside the container.

But how do I specify systemd-nspawn configuration options (such as, for example, containers.<name>.interfaces, or BindMounts) from within the flake? Is it even possible?

Thanks in advance for any help!

2 Likes

Hello! I’m not 100% sure, but I think there’s no way to configure containers from flakes.

Even if you could, that wouldn’t exactly be optimal, as you’d either have to manually activate the containers with their config (instead of having them in configuration.nix), they would be activated for all hosts (which means that you’d have to separate your server config into a different flake), or you’d need to choose which containers and which config to use in each host’s configuration.

While the latter seems like a good idea, I don’t see what advantages it would bring over having the config in your main configuration.nix. If you want to, however, you could make a separate .nix file and instead of containers.<name>.config = { config, pkgs, ... }: ..., you could do containers.<name>.config = (import ./path/to/container-config.nix.

I hope this answer helps!

Somewhat related to this topic: GitHub - erikarvstedt/extra-container: Run declarative NixOS containers without full system rebuilds.