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

31 lines
1.1 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.*
{ pkgs
, lib
} @ inputs:
2023-01-13 06:42:21 +00:00
let
2022-12-08 21:00:53 +00:00
yamlToJsonDrv = yamlContent: outputPath: pkgs.callPackage
({ runCommand }:
# runCommand source: https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix#L33
runCommand outputPath { inherit yamlContent; nativeBuildInputs = [ pkgs.yq ]; }
# 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-01-13 06:42:21 +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"));
fromYamlPath = yamlPath: builtins.fromJSON (
builtins.readFile (
2023-01-13 06:42:21 +00:00
yamlToJsonDrv
(
builtins.readFile yamlPath)
"any-output.json"));
2022-12-08 21:00:53 +00:00
# TODO: fromToml?
}