How to convert Yaml -> Nix object?

I recently had to write a fromYAML in one of my projects, so let me see if I can give you some pointers.

There is no fromYAML builtin in Nix, although there is an open PR adding one:

https://github.com/NixOS/nix/pull/7340

If you search on GitHub for fromYAML, you’ll find a bunch of examples of people trying to write this function (although be careful, since some are obviously incorrect).

In my own project, I actually needed a readYAML function (a combination of builtins.readFile and fromYAML), and here is what I came up with:

This internally uses remarshal, but something like yq should definitely work as well.

As long as you control the alacritty.yaml file, one more option for you would be to just write the file in JSON. YAML is a super-set of JSON, so you could just write your alacritty.yaml file in JSON. Then, both alacritty and home-manager could directly read it correctly.

6 Likes