But Nix uses a normal string when passing into TOML, so I do have to escape anyways
What do you mean by that? You are always able to just use the '' strings instead of the " strings, they are just different syntax for the same underlying type. Or are you just using lib.generators.toINI without any arguments? Maybe try something like this:
toTomlEscapeBackslashes = toINI {
mkKeyValue = mkKeyValueDefault { mkValueString = x: lib.escape [ ''\'' ] (toString x); } "=";
}
This will escape the values, like this:
nix-repl> :print toTomlEscapeBackslashes { my.regex = ''foo\nbar''; }
[my]
regex=foo\\nbar