Skip to content

Instantly share code, notes, and snippets.

@mibmo
Created April 10, 2025 22:13
Show Gist options
  • Save mibmo/459df1d3f25bbc6194162878723fbb3f to your computer and use it in GitHub Desktop.
Save mibmo/459df1d3f25bbc6194162878723fbb3f to your computer and use it in GitHub Desktop.
Convert Nix types into Python data (probably)
let
inherit (builtins)
attrValues
concatStringsSep
foldl'
head
length
mapAttrs
tail
typeOf
;
toPython =
value:
{
bool = if value then "True" else "False";
float = toString value;
int = toString value;
lambda = ""; # @TODO: panic
list =
if length value > 0 then
''[${(foldl' (acc: elem: acc + "," + (toPython elem)) (toPython (head value)) (tail value))}]''
else
"[]";
null = "None";
path = toString value;
set =
"{"
+ (concatStringsSep "" (
attrValues (mapAttrs (name: value: ''"${name}":${toPython value},'') value)
))
+ "}";
string = ''"${value}"'';
}
.${typeOf value};
in
toPython
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment