lib/data: init

This commit is contained in:
Gabriel Arazas 2024-06-09 23:03:26 +08:00
parent e3d03a206d
commit 9e3a95ba24
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
8 changed files with 104 additions and 1 deletions

47
lib/data.nix Normal file
View File

@ -0,0 +1,47 @@
# Anything that has to do with interacting with data especially with external
# ones (that is, anything that is not represented as a Nix object).
# Unfortunately, most of these functions are surely going to use packages found
# in nixpkgs so expect a lot of them are used as an IFD.
{ pkgs, lib, self }:
{
/* Import the YAML file and return it as a Nix object. Unfortunately, this is
implemented as an import-from-derivation (IFD) so it will not be pretty.
Type: importYAML :: Path -> Attrs
Example:
importYAML ./your-mom.yaml
=> { name = "Yor Mum"; age = 56; world = "Herown"; }
*/
importYAML = path:
let
dataDrv = pkgs.runCommand "convert-yaml-to-json" { } ''
${lib.getExe' pkgs.yaml2json "yaml2json"} < "${path}" > "$out"
'';
in lib.importJSON dataDrv;
/* Render a Tera template given a parameter set powered by `tera-cli`. Also
typically used as an IFD.
Type: renderTeraTemplate :: Attrs -> Path
Example:
renderTeraTemplate { path = ./template.tera; context = { hello = 34; }; }
=> /nix/store/HASH-tera-render-template
*/
renderTeraTemplate = { template, context, extraArgs ? { } }:
let
extraArgs' = lib.cli.toGNUCommandLineShell { } extraArgs;
# Take note we're generating the context file in this way since tera-cli
# can only detect them through its file extension and doesn't have a way
# of explicitly reading files as JSON, YAML, etc.
settingsFormat = pkgs.formats.json { };
contextFile = settingsFormat.generate "tera-context.json" context;
in pkgs.runCommand "tera-render-template" {
nativeBuildInputs = with pkgs; [ tera-cli ];
} ''
tera --out "$out" ${extraArgs'} --template "${template}" "${contextFile}"
'';
}

View File

@ -1,4 +1,7 @@
# The entrypoint for our custom library set. # The entrypoint for our custom library set. Take note, this is modularly
# included as part of the environment so we cannot have any functions or
# references that could make the evaluation go in an infinite recursion such as
# a function that generates a valid nixpkgs module.
{ pkgs }: { pkgs }:
pkgs.lib.makeExtensible pkgs.lib.makeExtensible
@ -8,6 +11,8 @@ pkgs.lib.makeExtensible
callLib = file: import file { inherit pkgs lib self; }; callLib = file: import file { inherit pkgs lib self; };
in { in {
trivial = callLib ./trivial.nix; trivial = callLib ./trivial.nix;
data = callLib ./data.nix;
inherit (self.trivial) countAttrs getConfig getUser; inherit (self.trivial) countAttrs getConfig getUser;
inherit (self.data) importYAML renderTeraTemplate;
}) })

View File

@ -0,0 +1,9 @@
{
"hello": "world",
"whoa": 4566,
"list-of-names": [
"Cheesy",
"Angry",
"Ash"
]
}

View File

@ -0,0 +1,6 @@
hello: world
whoa: 4566
list-of-names:
- Cheesy
- Angry
- Ash

View File

@ -0,0 +1,33 @@
{ pkgs, lib, self }:
lib.runTests {
testImportYAML = {
expr = self.data.importYAML ./data/sample.yaml;
expected = {
hello = "world";
whoa = 4566;
list-of-names = [
"Cheesy" "Angry" "Ash"
];
};
};
testImportYAMLAsJSON = {
expr = self.data.importYAML ./data/sample.json;
expected = {
hello = "world";
whoa = 4566;
list-of-names = [
"Cheesy" "Angry" "Ash"
];
};
};
testRenderTeraTemplate = {
expr = builtins.readFile (self.data.renderTeraTemplate {
template = ./templates/sample.tera;
context = lib.importJSON ./data/sample.json;
});
expected = builtins.readFile ./fixtures/sample.tera;
};
}

View File

@ -0,0 +1 @@
world

View File

@ -0,0 +1 @@
{{ hello }}

View File

@ -18,6 +18,7 @@ let
in in
{ {
trivial = callLib ./trivial.nix; trivial = callLib ./trivial.nix;
data = callLib ./data;
# Environment-specific subset. # Environment-specific subset.
home-manager = callLib ./home-manager.nix; home-manager = callLib ./home-manager.nix;