dotfiles/lib/serde.nix

32 lines
1.1 KiB
Nix
Raw Permalink Normal View History

2022-12-27 04:22:07 +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
2023-01-13 06:42:21 +00:00
, ...
2022-12-27 04:22:07 +00:00
} @ inputs:
2023-01-13 06:42:21 +00:00
let
2022-12-27 04:22:07 +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-27 04:22:07 +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-27 04:22:07 +00:00
# TODO: fromToml?
}