I’m trying to define some containers, while automating away some boilerplate. So I created a module that would take some container configs, and create the corresponding containers all in the same way. The following is actually a simplified version of it that only creates the first one:
{ lib, options, config, ... }: {
options.local.containers = lib.mkOption {
type = lib.types.listOf (lib.types.submodule {
options = {
name = lib.mkOption {
type = lib.types.str;
};
config = lib.mkOption {
type = lib.types.anything;
description = "Container config";
};
options = lib.mkOption {
type = lib.types.attrs;
description = "Container options (see containers.<name>)";
default = { };
};
};
});
};
config = let
id = 0;
attrs = builtins.elemAt config.local.containers id; # Take the first defined container
name = attrs.name;
options = attrs.options;
ip_n3 = toString (100 + id);
in {
networking.hosts = {
"192.168.${ip_n3}.1" = [ name ];
"fe00::${ip_n3}:1" = [ name ];
};
containers.${name} = {
config = {
imports = [
{ # Add some additional things to the container config
services.kresd.enable = lib.mkDefault true;
}
attrs.config
];
};
privateNetwork = true;
localAddress = "192.168.${ip_n3}.1";
hostAddress = "192.168.${ip_n3}.2";
localAddress6 = "fc00::${ip_n3}:1";
hostAddress6 = "fc00::${ip_n3}:2";
} // options;
};
}
I then tried to use it to create a container:
let
container = { pkgs, ... }: {
environment.systemPackages = [ pkgs.neovim ];
};
in {
local.containers = [{
name = "test";
config = container;
}];
}
But when I try to build, I get an error:
nix-repl> nixosConfigurations.test-system.config.containers.test
{ additionalCapabilities = [ ... ]; allowedDevices = [ ... ]; autoStart = false; bindMounts = { ... }; config = error:
… while evaluating the attribute 'value'
at /nix/store/qkzr5clkyq7j6n43qs5ff73in6c3wacv-source/lib/modules.nix:809:9:
808| in warnDeprecation opt //
809| { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
| ^
810| inherit (res.defsFinal') highestPrio;
… while calling the 'addErrorContext' builtin
at /nix/store/qkzr5clkyq7j6n43qs5ff73in6c3wacv-source/lib/modules.nix:809:17:
808| in warnDeprecation opt //
809| { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
| ^
810| inherit (res.defsFinal') highestPrio;
(stack trace truncated; use '--show-trace' to show the full trace)
error: function 'container' called without required argument 'pkgs'
at /nix/store/dn9czq0mv21h45nzkc3qghfhy9hvjvjj-source/systems/modules/test-container.nix:2:15:
1| let
2| container = { pkgs, ... }: {
| ^
3| environment.systemPackages = [ pkgs.neovim ];
I have no idea what is going wring here. attrs.config
is a lambda function that’s a module, and I’d think that, when added to the container imports, would be treated as a module. But that doesn’t seem to happen for some reason, and I have no idea how to debug further.