Help with building JSON files that contain regex

A huge part of what won me over to NixOS is the integration with OpenSnitch. The idea of having stable reliable application level firewall rules that updated in lock step with packages updating sounded like I dream. Overall it has been fantastic, but I’ve consistently had issues writing regex rules.

NixOS lets you declare a JSON value for a rule
. It is then written out to disk as a JSON file. OpenSnitch you write regex to match various properties like the file path, destination host, etc. For the life of me I couldn’t get just \. to appear in a rule. \. becomes .. \\. because \\.. Eventually I found this issue: `builtins.toJSON` impossible to create single backslash followed by certain characters · Issue #10082 · NixOS/nix · GitHub

So my question is, is there any work around for this? I was thinking of making string templates for the rules to work around this restriction but that seems like a fair amount of work and wonky. Is an upstream fix possible in a future major release? Am I missing something very obvious to get around this? Maybe a regex trick to avoid ever needing to escape a dot?

What’s the issue with \\. in the json?
\\. in json ↔ \. in regex ↔ literal .
Seems okay to me…

EDIT: in fact, I don’t think \. would be a valid escape sequence in JSON anyway - see page 5 of https://www.ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf

2 Likes

Oft! You are spot on. I feel like a doofus, got mixed up on the escaping and was blaming a bug in my regex on it. Thank you