Parameterized NixOS Module

Hi. I’m new. This is my first post here.

I have a question (gist linked below for the long version)

Short version: I want to define a function which returns a NixOS module as output, but I get an infinite recursion in the config section when I try to refer to the values of the arguments contained therein.

My questions:

  1. Is this the sort of thing for which I should be sent to the Hague?
  2. If not, how can I fix it?
  3. If so, what should I do instead?

Generally what you should do for something like this is make some options for these parameters and configure them. NixOS 22.11 manual

If you need to do it multiple times in the same config, you can use attrsOf (see the submodule section of the above link).

1 Like

Right, I can do that much, my issue comes when trying to extract the pattern where the child elements (in the list of submodules) need to all undergo the same process, for example, if you want each child element to represent a set of systemd services, and the parent to represent a service upon which all the child services depend; or you have a signing key and a bunch of files that need to be signed by that key, etc.

My issue comes when trying to write config that refers to the options defined by parameter. Since config.${familyName} is defined by the outer closure, I think that this is confusing whatever does the module rollup, but I’m brand new to this (using it for a couple months), so I wasn’t sure if it’s impossible or I just needed to get good. It sounds like (from here and elsewhere on the internet where I have asked) to not be possible, and this individual summed it up well in the reddit post I made here, “The module system can be fairly temperamental, so I’m not sure if a pattern like this will work.”

If some passerby sees this thread and knows how to do something like this, I’d be interested, but this seems like I wandered adrift from where the module system wants you to go. I return to simpler waters.

1 Like

Short version – the problem is actually quite obvious, you can’t set config = functionOf config.anything because it’s a really clear infinite recursion. It should be possible to set any key of config, and I hold some hope that there might be One Weird Trick. If I find it, I’ll update the gist. I also go into a bit more detail in a comment on the gist.