dotfiles/nix-conf/lib/serde/default.nix

40 lines
1.2 KiB
Nix
Raw Normal View History

2022-12-08 21:00:53 +00:00
# Takes care of serializing and deserializing to some formats
# Blame: Pegasust<pegasucksgg@gmail.com>
# TODO: Add to* formats from pkgs.formats.*
2023-06-18 00:46:31 +00:00
{
pkgs,
lib,
} @ inputs: let
yamlToJsonDrv = yamlContent: outputPath:
pkgs.callPackage
({runCommand}:
2022-12-08 21:00:53 +00:00
# runCommand source: https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix#L33
runCommand outputPath
{
2023-06-18 00:46:31 +00:00
inherit yamlContent;
nativeBuildInputs = [pkgs.yq];
}
2022-12-08 21:00:53 +00:00
# run yq which outputs '.' (no filter) on file at yamlPath
# note that $out is passed onto the bash/sh script for execution
''
echo "$yamlContent" | yq >$out
'')
2023-06-18 00:46:31 +00:00
{};
in {
2022-12-08 21:00:53 +00:00
# Takes in a yaml string and produces a derivation with translated JSON at $outputPath
# similar to builtins.fromJSON, turns a YAML string to nix attrset
fromYaml = yamlContent: builtins.fromJSON (builtins.readFile (yamlToJsonDrv yamlContent "any_output.json"));
2023-06-18 00:46:31 +00:00
fromYamlPath = yamlPath:
builtins.fromJSON (
builtins.readFile (
yamlToJsonDrv
2023-01-13 06:42:21 +00:00
(
2023-06-18 00:46:31 +00:00
builtins.readFile yamlPath
)
"any-output.json"
)
);
fromTOML = builtins.fromTOML;
fromJSON = builtins.fromJSON;
2022-12-08 21:00:53 +00:00
}