This commit is contained in:
jacekpoz 2024-05-22 21:19:37 +02:00
commit e66f5a3707
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
6 changed files with 300 additions and 0 deletions

7
LICENSE Normal file
View file

@ -0,0 +1,7 @@
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

173
README.md Normal file
View file

@ -0,0 +1,173 @@
# nte
nix template engine - takes some templates, entries and applies the templates to the entries
# usage
check `example/` for a static website generated using nte
first add nte as an input in your project's flake
```nix
nte = {
url = "git+https://git.jacekpoz.pl/jacekpoz/nte";
inputs.nixpkgs.follows = "nixpkgs";
};
```
then the engine function will be available under
```nix
inputs.nte.engines.${system}.default
```
it accepts 3 arguments:
- `extraArgs` - an attrset of additional arguments passed to all entries and templates
- `entries` - a list of all entry files to be processed
- `templates` - a list of all template files to be applied
and returns a string containing a shell script that applies the templates to the entries
here's an example usage inside of another derivation:
```nix
{
nte,
stdenv,
...
}: let
extraArgs = {
foo = 2137;
bar = "dupa";
baz = arg1: arg2: ''
here's arg1: ${arg1}
and here's arg2: ${arg2}
'';
};
entries = [
./entry1.nix
./foo/entry2.nix
./foo/entry3.nix
./bar/entry4.nix
./bar/entry5.nix
./bar/entry6.nix
];
templates = [
./template1.nix
./template2.nix
];
in
stdenv.mkDerivation {
name = "nte-example";
version = "0.1";
src = ./.;
buildPhase = ''
runHook preBuild
${nte {inherit extraArgs entries templates;}}
runHook postBuild
'';
}
```
nte will handle creating directories if your source file structure isn't flat
nte offers a standard library that contains `nixpkgs` and utility functions found in [stdlib.nix](./stdlib.nix)
## templates
a template can take an arbitrary number of arguments and returns `{ name, output }`
example template:
```nix
{
name,
location,
info,
...
}: {
name = "greeting";
output = ''
Hello ${name}! Welcome to ${location}.
Here's some more information:
${info}
'';
}
```
a template's output can also be an entry to another template:
```nix
{
name,
location,
date,
time,
...
} @ extraArgs : let
inherit (extraArgs) file;
in {
name = "greeting-with-date";
output = {
template = "greeting";
inherit file;
inherit name location;
info = ''
You're visiting ${location} on ${date} at ${time}!
'';
};
}
```
## entries
an entry can take an arbitrary number of arguments and returns `{ template, file, ... }`, the `...` being the desired template's arguments (sans `extraArgs`, those are passed either way)
example entries (using the previous example templates):
```nix
_: {
template = "greeting";
file = "wroclaw/welcome-jacek.txt";
name = "Jacek";
location = "Wrocław";
info = ''
As of 2023, the official population of Wrocław is 674132 making it the third largest city in Poland.
'';
}
```
an entry using the stdlib:
```nix
{
run,
...
}: {
template = "greeting-with-date";
file = "osieck/welcome-rafal.txt";
name = "Rafał";
location = "Osieck";
date = run "date +%F";
time = run "date +%T";
}
```
if a binary isn't in `$PATH`, remember that each entry gets `pkgs`:
```nix
{
pkgs,
run,
...
}: let
date = "${pkgs.coreutils-full}/bin/date";
in {
# ...
date = run "${date} +%F";
time = run "${date} +%T";
}
```
# license
MIT

64
engine.nix Normal file
View file

@ -0,0 +1,64 @@
pkgs: {extraArgs, entries, templates}: let
inherit (pkgs) lib;
inherit (builtins) abort;
inherit (lib.lists) elemAt forEach findFirst length remove;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg isString splitString;
inherit (lib.trivial) functionArgs;
args = {inherit pkgs;}
// (import ./stdlib.nix pkgs)
// extraArgs;
getDirectory = file: let
splitPath = splitString "/" file;
last = elemAt splitPath ((length splitPath) - 1);
in
concatStrings (remove last splitPath);
findTemplateFile = entry:
findFirst (templateFile: let
templateFn = import templateFile;
template = templateFn (functionArgs templateFn);
in
template.name == entry.template)
null
templates;
applyTemplate = entry: templateFile: let
templateFn = import templateFile;
template = templateFn (args // entry);
in
if (isString template.output)
then {
inherit (entry) file;
inherit (template) output;
}
else let
newEntry = template.output;
foundTemplateFile = findTemplateFile newEntry;
in
if foundTemplateFile == null then
abort "template ${newEntry.template} not found"
else
applyTemplate newEntry foundTemplateFile;
processEntryFile = entryFile: let
entryFn = import entryFile;
entry = entryFn args;
foundTemplateFile = findTemplateFile entry;
in
if foundTemplateFile == null then
abort "template ${entry.template} not found"
else
applyTemplate entry foundTemplateFile;
in /*sh*/''
${concatMapStrings
(result: /*sh*/''
mkdir -p $out/${getDirectory result.file}
echo ${escapeShellArg result.output} > $out/${result.file}
'')
(forEach entries processEntryFile)
}
''

27
flake.lock Normal file
View file

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1716218643,
"narHash": "sha256-i/E7gzQybvcGAYDRGDl39WL6yVk30Je/NXypBz6/nmM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a8695cbd09a7ecf3376bd62c798b9864d20f86ee",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

21
flake.nix Normal file
View file

@ -0,0 +1,21 @@
{
description = "Nix Template Engine";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
};
outputs = {nixpkgs, ...}: let
systems = ["x86_64-linux" "aarch64-linux"];
forEachSystem = nixpkgs.lib.genAttrs systems;
pkgsForEach = nixpkgs.legacyPackages;
in {
engines = forEachSystem (
system: let
pkgs = pkgsForEach.${system};
in {
default = import ./engine.nix pkgs;
}
);
};
}

8
stdlib.nix Normal file
View file

@ -0,0 +1,8 @@
pkgs: let
inherit (pkgs) lib runCommand;
inherit (lib.strings) readFile;
in {
run = script: readFile (runCommand "run" {} ''
echo $(${script}) > $out
'');
}