Merging of assignments possible?

Probably a beginner question but how do I combine these two assignments in the end?

let
  services = lib.listToAttrs (map (name: {
    inherit name;
    value = mkService name;
  }) serviceNames);

  avahiAliasesEnv = lib.concatMapStrings (name: "EXTRA_HOST_${name}=${name}.local") serviceNames;
in {
  systemd.services = services;
  systemd.services.avahi-aliases.serviceConfig.Environment = avahiAliasesEnv;
}

The problem is 2 assignments to systemd.services.
Any idea how to merge them perhaps?

The error: error: attribute 'systemd.services' already defined at [...]

I think you can use // operator to accomplish this:

{
  systemd.services = services // { avahi-aliases.serviceConfig.Environment = avahiAliasesEnv };
}

Tried that and it does not like to have an assignment inside. But then I got it solved:
This does it:

  systemd.services = lib.mkMerge [ services { avahi-aliases.serviceConfig.Environment = avahiAliasesEnv; } ];

But thx for the fast assist.

1 Like

Eh, I don’t tinker with my config much these days. Never heard of lib.mkMerge before your answer, I guess I learned something new!

// doesn’t merge, it simply overwrites previous values, so it should not be used in the NixOS module system. lib.mkMerge is the correct tool here as it handles deep merging and respects priority, ordering, etc. :slight_smile:

1 Like

More about preferring lib.mkMerge to its alternatives in this cheat sheet I drew up recently.

2 Likes