mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-02-07 06:19:00 +00:00
lib/data: init
This commit is contained in:
parent
e3d03a206d
commit
9e3a95ba24
47
lib/data.nix
Normal file
47
lib/data.nix
Normal 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}"
|
||||
'';
|
||||
}
|
@ -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.lib.makeExtensible
|
||||
@ -8,6 +11,8 @@ pkgs.lib.makeExtensible
|
||||
callLib = file: import file { inherit pkgs lib self; };
|
||||
in {
|
||||
trivial = callLib ./trivial.nix;
|
||||
data = callLib ./data.nix;
|
||||
|
||||
inherit (self.trivial) countAttrs getConfig getUser;
|
||||
inherit (self.data) importYAML renderTeraTemplate;
|
||||
})
|
||||
|
9
tests/lib/data/data/sample.json
Normal file
9
tests/lib/data/data/sample.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"hello": "world",
|
||||
"whoa": 4566,
|
||||
"list-of-names": [
|
||||
"Cheesy",
|
||||
"Angry",
|
||||
"Ash"
|
||||
]
|
||||
}
|
6
tests/lib/data/data/sample.yaml
Normal file
6
tests/lib/data/data/sample.yaml
Normal file
@ -0,0 +1,6 @@
|
||||
hello: world
|
||||
whoa: 4566
|
||||
list-of-names:
|
||||
- Cheesy
|
||||
- Angry
|
||||
- Ash
|
33
tests/lib/data/default.nix
Normal file
33
tests/lib/data/default.nix
Normal 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;
|
||||
};
|
||||
}
|
1
tests/lib/data/fixtures/sample.tera
Normal file
1
tests/lib/data/fixtures/sample.tera
Normal file
@ -0,0 +1 @@
|
||||
world
|
1
tests/lib/data/templates/sample.tera
Normal file
1
tests/lib/data/templates/sample.tera
Normal file
@ -0,0 +1 @@
|
||||
{{ hello }}
|
@ -18,6 +18,7 @@ let
|
||||
in
|
||||
{
|
||||
trivial = callLib ./trivial.nix;
|
||||
data = callLib ./data;
|
||||
|
||||
# Environment-specific subset.
|
||||
home-manager = callLib ./home-manager.nix;
|
||||
|
Loading…
Reference in New Issue
Block a user