2024-05-24 21:49:27 +02:00
< div align = "center" >
2024-05-25 18:21:40 +02:00
< img src = "branding/nte-colors.svg" width = "200" / >
2024-05-24 21:49:27 +02:00
< br / >
< h1 > nte< / h1 >
< / div >
2024-05-22 21:19:37 +02:00
nix template engine - takes some templates, entries and applies the templates to the entries
2024-05-25 18:38:59 +02:00
nte's main repository is on [my forgejo ](https://git.jacekpoz.pl/jacekpoz/nte ) instance
mirrors are available on [github ](https://github.com/jacekpoz/nte ) and [codeberg ](https://codeberg.org/jacekpoz/nte ), I accept contributions from anywhere
2024-05-25 18:21:40 +02:00
# sites written in nte
https://jacekpoz.pl
if your site (or anything else) is written in nte, let me know and I'll add you to this list
you can also use this button on your site and link to one of the repos
[<img src="branding/powered-by-nte.png"> ](https://git.jacekpoz.pl/jacekpoz/nte )
2024-05-22 21:55:06 +02:00
# examples
check `example/` for a static website written in nte
2024-05-22 21:19:37 +02:00
2024-05-25 17:19:50 +02:00
build and run it using
```sh
nix shell nixpkgs#darkhttpd --command sh -c "nix build -L .#examples.x86_64-linux.default & & darkhttpd ./result"
```
the site will be available at http://localhost:8080
2024-05-22 22:09:37 +02:00
2024-05-25 18:55:08 +02:00
the example is a cut down version of my own website
2024-05-22 21:55:06 +02:00
# usage
2024-05-22 21:19:37 +02:00
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";
};
```
2024-09-12 23:03:06 +02:00
then use the `mkNteDerivation` wrapper over `stdenv.mkDerivation` available under
2024-05-22 21:19:37 +02:00
```nix
2024-09-12 23:03:06 +02:00
inputs.nte.functions.${system}.mkNteDerivation
2024-05-22 21:19:37 +02:00
```
2024-09-12 23:03:06 +02:00
it accepts an attrset of:
- `name` , `version` , `src` - passthrough to `stdenv.mkDerivation`
- `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
- `extraFiles` - a list of attrsets of `source` and `destination` :
- `source` - a path, if relative `$PWD` is `$src` in the `installPhase`
- `destination` - a path, never absolute, appended to `$out` in the `installPhase`
example usage of the wrapper function:
2024-05-22 21:19:37 +02:00
```nix
2024-09-12 23:03:06 +02:00
mkNteDerivation {
name = "nte-example";
version = "0.1";
src = ./.;
2024-05-22 21:19:37 +02:00
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
];
2024-09-12 23:03:06 +02:00
extraFiles = [
{ source = ./image.png; destination = "/assets/"; }
{ source = ./image2.png; destination = "/assets/dupa.png"; }
{ source = "./data/*"; destination = "/assets/data/"; }
{ source = fetchurl { ... }; destination = "/"; }
];
}
2024-05-22 21:19:37 +02:00
```
nte will handle creating directories if your source file structure isn't flat
2024-09-12 23:03:06 +02:00
if the `mkNteDerivation` wrapper isn't enough for you, you can do things the old way - by putting the output of `inputs.nte.functions.${system}.engine` in a derivation's `buildPhase` :
```nix
mkDerivation {
# ...
buildPhase = ''
runHook preBuild
${engine {inherit extraArgs entries templates;}}
runHook postBuild
'';
}
```
in that case if you wish to replicate the functionality of `extraFiles` you can use the derivation's `installPhase` , manually `mkdir` the needed directories and `cp` your files into `$out`
2024-09-13 11:17:33 +02:00
nte offers a standard library that contains:
- `nixpkgs`
- `getEntry` - a function that gives you access to the entry's attributes
- `applyTemplate` - a function that allows you to manually apply a template to an entry
- utility functions found in [stdlib.nix ](./stdlib.nix )
2024-05-22 21:19:37 +02:00
## templates
2024-05-25 17:36:26 +02:00
a template can take an arbitrary number of arguments and returns `{ name, format, output }` :
- `name` - used as a template ID for the entries
- `format` - the extension of the output file (ignored if an entry defines `file` )
- `output` - string if in a base template, entry to another template otherwise
2024-05-22 21:19:37 +02:00
example template:
```nix
{
name,
location,
info,
...
}: {
name = "greeting";
2024-05-25 17:36:26 +02:00
format = "txt";
2024-05-22 21:19:37 +02:00
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,
...
2024-05-25 17:36:26 +02:00
}: {
2024-05-22 21:19:37 +02:00
name = "greeting-with-date";
output = {
template = "greeting";
inherit name location;
info = ''
You're visiting ${location} on ${date} at ${time}!
'';
};
}
```
2024-05-25 17:36:26 +02:00
a template that's inherited from a different template also inherits its format - no need to define it again
2024-05-22 21:19:37 +02:00
## entries
2024-05-25 17:36:26 +02:00
an entry can take an arbitrary number of arguments and returns `{ template, ... }` , the `...` being the desired template's arguments (sans `extraArgs` , those are passed either way)
2024-05-22 21:19:37 +02:00
example entries (using the previous example templates):
```nix
_: {
template = "greeting";
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";
2024-05-25 17:36:26 +02:00
2024-05-22 21:19:37 +02:00
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";
}
```
2024-05-25 17:36:26 +02:00
nte by default will follow your source file structure, if you want to specify the output location yourself use `file` :
```nix
_: {
# ...
file = "foo/bar.txt";
}
```
in this example the output of this entry will end up at `$out/foo/bar.txt` instead of the default location - a base template's `format` will also be ignored
2024-05-25 18:38:59 +02:00
# thanks
[raf ](https://notashelf.dev/ ) for helping me out with some of the nix and setting up mirrors
2024-05-22 21:19:37 +02:00
# license
MIT