Pure nix implementation of mustache template engine.
# basic usage, library should be local
let
pkgs = import <nixpkgs> { config = {}; overlays = []; };
mustache = import ./mustache { inherit (pkgs) lib; };
in
mustache { template = "Hello, {{name}}!"; view = { name = "nix"; }; }
# (unreproducible) library loading from github & custom escape
let
pkgs = import <nixpkgs> { config = {}; overlays = []; };
repo = builtins.fetchGit {
url = "https://github.com/valodzka/nix-mustache.git";
ref = "master";
};
mustache = import (repo + "/mustache") { lib = pkgs.lib; };
escape = string: builtins.replaceStrings ["nix"] ["NIX"] string;
in
mustache { template = "Hello, {{name}}!"; view = { name = "nix"; }; config = { inherit escape; }; }
Given template Corefile.mustache
& nix file coredns-config.nix
:
# generated for {{pkgs.coredns.name}}
.:53 {
forward .{{#dnsServers}} {{.}}{{/dnsServers}}
log
{{#brokenSites}}
template IN AAAA {{.}} {
rcode NXDOMAIN
}
{{/brokenSites}}
}
{ pkgs ? import <nixpkgs> { config = {}; overlays = []; }, mustache ? import ./mustache { lib = pkgs.lib; } }:
let
config = mustache {
template = ./Corefile.mustache;
view = {
brokenSites = ["broken.com" "big-isp.com"];
dnsServers = ["8.8.8.8" "8.8.4.4"];
pkgs = pkgs;
};
};
corefile = pkgs.writeTextFile {
name = "Corefile";
destination = "/etc/coredns/Corefile";
text = config;
};
in corefile
Run nix-env --install --file coredns-config.nix
and it will generate:
# generated for coredns-1.11.1
.:53 {
forward . 8.8.8.8 8.8.4.4
log
template IN AAAA broken.com {
rcode NXDOMAIN
}
template IN AAAA big-isp.com {
rcode NXDOMAIN
}
}
- variables
{{escaped}}
,{{&unescaped}}
,{{{unescaped}}}
(default escape function does nothing) - sections
{{#section}}
- inverted sections
{{^inverted}}
- lambdas
{{#lambda}}
- comments
{{!comment}}
- variables dot notation
{{obj.prop}}
- partials
{{>partial}}
- set delimiter
{{=<% %>=}}
- tests, spec
Run mustache spec
Implements all core modules + lambdas from optional modules.
nix --extra-experimental-features nix-command eval --file spec.nix
nix-instantiate --strict --eval --json test.nix | jq .