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

Cameron Turner cam.turn at gmail.com
Mon Apr 14 11:13:10 CEST 2014


The implementation of this would be:

users = {
        root = { name = "root"; group = groups.root; };
        ssh = { name = "ssh" ; group = groups.nogroup ; severity = 33; };
      };
userList = (map (key: getAttr key users) (attrNames users));

Roughly based on some code in Disnix's lib.nix. I'm sure you could split this out into a library function (maybe that's already been done), but there's no need for it to be a built-in.

Shell

Sergey Mironov <grrwlf at gmail.com> wrote:
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 ];
      };
_______________________________________________
nix-dev mailing list
nix-dev at lists.science.uu.nl
http://lists.science.uu.nl/mailman/listinfo/nix-dev


More information about the nix-dev mailing list