[Nix-dev] Nix language: converting an attribute set to a list

Sergey Mironov grrwlf at gmail.com
Mon Apr 14 10:37:08 CEST 2014


Hi. Nix language has a built-in function builtins.listToAttrs which
converts list to the Attrs. I'm searching a way to make an opposite
thing - convert attrs to a list. Say, I'd like to convert

      users = {
        root = { name = "root"; group = groups.root; };
        ssh = { name = "ssh" ; group = groups.nogroup ; severity = 33; };
      };

to a list like this:

      [
        { name = "root"; group = groups.root; }
        { name = "ssh" ; group = groups.nogroup ; severity = 33; }
      ];

to use it later both in shell scripting, as list

lib.map  (toList users) (u: ''
  adduser ${u.name} ${u.group}
'');

.. and in derivations, by reference

stdenv.mkDerivation "ssh" {
  user = users.ssh;
};

In order to workaround the problem I have to add additional field
`list' (like below) but this way I have to duplicate field names which
is not good (easy to forget). So Is it possible to do the
transformation in a more
safe way?

Regards,
Sergey

# A workaround

      users = rec {
        root = { name = "root"; group = groups.root; };
        ssh = { name = "ssh" ; group = groups.nogroup ; severity = 33; };
        list = [ root ssh ];
      };


More information about the nix-dev mailing list