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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With