List to attribute set

Hi, I’m just starting with nix and I can’t seems to find how approach to converting a list of l = [{ foo = "x"; bar = "y"} .... ] to attribute set e.g. a = { x = { foo = "x"; bar = "y"; }; ... } ?

I’m trying to populate email accounts in home-manger and this is what I have tried so far

{ lib, pkgs, ... }:
let
  servers = {
    protonmail = {
      imap = {
        host = "127.0.0.1";
        port = 1143;
        tls.enable = true;
        tls.useStartTls = true;
      };
      smtp = {
        host = "127.0.0.1";
        port = 1025;
        tls.enable = true;
      };
    };
  };
  accs = [
    {
      username =  "a@b.c";
      password =  "pass";
      name = "name lastname";
    }
    {
      username =  "x@y.z";
      password =  "pass";
      name = "name lastname";
    }
  ];

in
{
  programs.himalaya = {
    enable = true;
    settings = { };
  };

  accounts.email.accounts = lists.forEach accs (x: servers.protonmail // {
      realName = x.name;
      himalaya.enable = true;
      address = x.username;
      userName = x.username;
      passwordCommand = x.password; #"cat ~/.config/.protonmail.password"; # Temporary password from ProtonMail Bridge
    });
}

But it gives me

error: A definition for option `home-manager.users.a.accounts.email.accounts' is not of type `attribute set of submodule'. Definition values:
       - In `/nix/store/54a6ai61n39vi8algxhd2p7j2bx8n0nv-source/mail':
           [
             {
               address = "a@b.c";
               himalaya = {
....

also I tried to use lib.genAttrs but no luck yet.

P.S. it is my first time here!

builtins.listToAttrs ( map ( { x, y } @ value: { name = x; inherit value; } ) [...] ) 
2 Likes

Thanks I’ll give a try later

I have a general “list to attrs from key”:

lak = field: list: builtins.listToAttrs ( map ( v: { name = v.${field}; value = v;  } ) list );

It’s nothing fancy but having that on hand with a short name is nice.

I actually decided it doesn’t worth it in this case and simply hardcoded all the details, but played around with your answer in repl and it will surely be useful in the future. so much to learn! thanks again