commit e66f5a3707fc3ca9a975528ce50ca4f82aa9b31a Author: jacekpoz Date: Wed May 22 21:19:37 2024 +0200 init diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3b4562c --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f121a37 --- /dev/null +++ b/README.md @@ -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 diff --git a/engine.nix b/engine.nix new file mode 100644 index 0000000..660728f --- /dev/null +++ b/engine.nix @@ -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) + } +'' diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ed44e8b --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..86a46c7 --- /dev/null +++ b/flake.nix @@ -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; + } + ); + }; +} diff --git a/stdlib.nix b/stdlib.nix new file mode 100644 index 0000000..4528947 --- /dev/null +++ b/stdlib.nix @@ -0,0 +1,8 @@ +pkgs: let + inherit (pkgs) lib runCommand; + inherit (lib.strings) readFile; +in { + run = script: readFile (runCommand "run" {} '' + echo $(${script}) > $out + ''); +}