Created
April 10, 2025 22:13
-
-
Save mibmo/459df1d3f25bbc6194162878723fbb3f to your computer and use it in GitHub Desktop.
Convert Nix types into Python data (probably)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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