[Nix-dev] concatAttrs :: [attrSet] -> attrSet ?

Linus Heckemann acc at sphalerite.org
Sun May 28 14:24:28 CEST 2017


On 28/05/17 13:18, Sergiu Ivanov wrote:
> Hello,
> 
> I found myself in the need of a function which would take a list of
> Nix attribute sets and concatenate them to produce one big attribute
> set:
> 
> nix-repl> concatAttrs [ {x=1;} {y=2;} ]
> {x=1; y=2;}
> 
> I did quite some research online and in nixpkgs source tree and found
> a lot of cool functions, but nothing resembling to concatAttrs.
> 
> Does such a function exist?
> 
> 
> In the meantime, I ended up writing the following definition:
> 
> concatAttrs = attrList: pkgs.lib.fold (x: y: x // y) {} attrList;
> 
> Do you see any isssues with such an implementation?
> 

Looks like a sensible way to do it to me, though I'd be surprised if it
doesn't already exist in nixpkgs.

Do keep in mind that // is not commutative — the order matters when the
two sets contain the same key — but other than that I don't see any
issues. You might also want to look at the merging logic used for nixos
modules (lib/modules.nix), although that's significantly more complex.


More information about the nix-dev mailing list