Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nix: Map list of keys to attribute set members

I'm trying to map a list of attribute names to an attribute set in Nix in a way where the list holds certain keys that are used to create new attribute set members. My simplified high-level objective is that I want to apply the configuration line users.users.myuser.packages = [pkgs.curl]; to a list of users without code repetition.

I tried something like this:

> userList = ["foo" "bar"]
> users = { users = {}; } 
> builtins.map (u: users.users."${u}" = {}) userList

This fails as there is an unexpected =. I have a feeling that using map is the wrong operation, but I don't know what the name of the functional operation is that I want to execute here.

like image 308
phip1611 Avatar asked Feb 02 '26 06:02

phip1611


1 Answers

You have to create a list of attribute sets, which you can then convert to a real attribute set:

    let
        users = ["foo" "bar"];
        wantedPackages = [ "curl" ];
    in
        builtins.listToAttrs (builtins.map(u: { name = u; value = { packages = wantedPackages;}; }) users);
like image 150
kbmz Avatar answered Feb 04 '26 01:02

kbmz