How to convert Yaml -> Nix object?

I’m trying to configure program.alacritty in home-manager where program.alacritty.settings takes in an object.

I looked around and there is only builtins.fromJSON. Is there any support for YAML?

I also tried creating a config file “repository” derivation where I put alacritty.yaml through yq to produce alacritty.json. However, it seems pure builds doesn’t allow builtins.fromJSON "${my-derivation}/alacritty/alacritty.json" because it targets absolute path /nix/store/...

Right now, I see some different directions:

  • Write a library function for fromYAML in Nixlang
  • Make an escape hatch to use yq to turn YAML → JSON
  • Correctly create a data-only derivation and read the data contents upon execution
  • Hack it off and just use something like
    xdg.configFile."alacritty/alacritty.yaml" = builtins.readFile "${proj_root}/alacritty/alacritty.yaml"
1 Like

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:

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.