How to Escape Slash in YAML Key?

OpenTelemetry Collector’s configuration format requires forward slashes in dictionary keys, as can be seen in its docs. NixOS generates this config file using pkgs.formats.yaml, as the services.opentelemetry-collector.settings option. Unlike YAML however, in Nix the forward slash is a special character and so it cannot trivially be used in a key of an attribute set. Is there any way to work around this and generate a YAML file with forward slashes in its keys?

# fails with error: syntax error, unexpected PATH
nix-instantiate --eval -E '{ /tomato = "soup";  }./tomato'

#returns "soup"
nix-instantiate --eval -E '{ "/tomato" = "soup";  }."/tomato"'

putting key name under “” works for me in nix repl, did you try that already?

2 Likes

Yes this seems to work — I didn’t realise this was an option but it seems obvious now. Thanks.

2 Likes