Skip to content

Instantly share code, notes, and snippets.

@Lillecarl
Last active July 7, 2025 09:26
Show Gist options
  • Save Lillecarl/ac39eaf7755320619d9a5df6f0ef71b2 to your computer and use it in GitHub Desktop.
Save Lillecarl/ac39eaf7755320619d9a5df6f0ef71b2 to your computer and use it in GitHub Desktop.
# Apply function to all strings in an attrset recursively through lists as well.
mapStringsDeep =
function: value:
if builtins.isString value then
function value
else if builtins.isAttrs value then
lib.mapAttrs (name: mapStringsDeep function) value
else if builtins.isList value then
map (mapStringsDeep function) value
else
value;
# Escapes strings so variables aren't interpolated with ${} and %{}
tfEscapeString =
value:
lib.replaceStrings
[
# Nix and tf shares this escape pattern
"$$\{"
# tf also interpolates %{}
"%{"
]
[
"\${"
"%%{"
]
value;
# Escape
escapeStringsDeep = attrs: mapStringsDeep tfEscapeString attrs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment