I’ve been reading manual on how to define NixOS modules, trying to wrap my head around the inner workings of how the system all comes together. As far as I understand, any file I include with the imports
statement, as well as my top-level configuration.nix
is referred to as a module. The documentation states that modules can contain an options
property to declare options (as in: what types they have) and a config
section which then defines actual values for these options (which have been declared in other modules).
Here comes my question: how does this config
property differ from simply defining values at the top-level? For example, I’m not sure where the difference is between these two modules:
{ config, ... }:
{
environment.etc.firstfile.text = "hello";
environment.etc.secondfile.text = config.networking.hostName;
}
{ config, ... }:
{
config = {
environment.etc.firstfile.text = "hello";
environment.etc.secondfile.text = config.networking.hostName;
};
}
In both cases, I get “hello” in /etc/firstfile
and the system’s hostname in /etc/secondfile
. If any property that doesn’t have a special meaning gets merged into config
anyway, what’s the point of having a special config
property? And why do most modules use config
instead of directly defining options?