Multiple Declaration Error - Custom Merging Options

Carrying on from here.

I can declare some options multiple times, and the result is merged (as described).

However with multiple systemd.tmpfiles.rules declarations I get this error:

error: attribute 'systemd.tmpfiles.rules' at .../configuration.nix:241:3 already defined at .../configuration.nix:203:3

any nice way have multiple declarations merged? (I see the rules have this type: types.listOf types.str).

I guess theoretically, I could declare my own custom ‘option’, ensure everything merges, and then set systemd.tmpfiles.rules equal to this merged result…

This only works in specific situations, namely when mkMerge is used, or lib.attrsets.recursiveUpdate in some situations. NixOS’ module system also automatically merges files that are included using imports.

You can do the same in your configuration, of course.

1 Like

Your error is much more local than what you think, here’s another example of your error:

nix-repl> { x.y = 10; x.y = 20; }
error: attribute 'x.y' already defined at (string):1:3

In your case it will look something like this:

  systemd.tmpfiles.rules = [ "someRule" ];

  # ...

  systemd.tmpfiles.rules = [ "anotherRule" ];


The solution here is to combine the two lists into a single one:

  systemd.tmpfiles.rules = [ "someRule" "anotherRule" ];

Thanks, my goal was to actually have one file with multiple declarations. In order to have independent parts of a file for different concerns…

But maybe I should try importing multiple declarations. If that works it might be nice…

Imports just work!

I’d have preferred just using one file, but perhaps the most idiomatic solution.