Templating
n5i renders app files with the Tera template engine, whose syntax is similar to Jinja2. Use templates to generate passwords, react to user settings, or include data from other apps and plugins.
Template files
| File | Rendered | Context |
|---|---|---|
app.yml.jinja | whenever the app is loaded, installed or updated | full context |
custom.yml.jinja | like app.yml.jinja | full context |
metadata.yml.jinja | whenever the app's metadata is loaded | basic context only |
data/**/*.n5i_jinja | on installation and updates, see preloading files | full context |
If both app.yml and app.yml.jinja exist, the template is used.
# app.yml.jinja
version: 1
containers:
main:
image: linkstackorg/linkstack:latest
ingress_port: 80
environment:
SERVER_ADMIN: "{{ settings.admin_email | default(value='admin@example.com') }}"
APP_KEY: "base64:{{ derive_entropy(identifier='app-key') | hex_to_base64 }}"
{% if "other-app" in installed_apps %}
OTHER_APP_URL: http://api.other-app.{{ user }}.n5i:3000
{% endif %}Template context
The basic context is available in all templates:
| Variable | Description |
|---|---|
app_id | ID of the app. |
user | ID of the user the app is installed for. |
user_display_name | Display name of that user. |
installed_apps | IDs of the apps the user has installed. |
server_ip | IP address of the node. |
server_is_public | true if server_ip is a public IP address. |
jwt_pubkey_base64 | Public key to verify n5i's JWTs. |
init_domain | Deprecated: the app's primary domain, or a placeholder. Use primary_domain. |
The full context also contains:
| Variable | Description |
|---|---|
settings | The app's settings, with defaults for settings the user didn't change. |
primary_domain | The app's primary domain, if it has one. |
plugins | Data from context plugins, as plugins.<plugin app>.<plugin id>. |
Built-in functions
| Function | Description |
|---|---|
derive_entropy(identifier="...") | A secret hex string derived from the server's seed, the user, the app and identifier. It is stable across reinstalls and updates, so use it for passwords and keys. |
n5i_has_feature(feature="...") | Whether this n5i version supports a feature. Currently known: volume-types, disable-network-policy. |
Tera's get_env function is disabled.
Built-in filters
In addition to Tera's filters:
| Filter | Description |
|---|---|
sha256 | SHA-256 hash, hex-encoded. |
bcrypt(cost=12) | bcrypt hash. cost must be between 4 and 15. |
base64_encode, base64_decode | Base64 encoding of a string. |
hex_encode, hex_decode | Hex encoding of a string. |
hex_to_base64, base64_to_hex | Convert between hex and base64. |
You can add your own functions in JavaScript, see extending the preprocessor.
Preloading files into volumes
Files in data/<volume-id>/ are copied into the volume <volume-id> when the app is installed. Files ending in .n5i_jinja are rendered first and saved without that extension, for example data/config/config.yml.n5i_jinja becomes config.yml in volume config. Files ending in .n5i_jinja_hex must render to hex, which is decoded into a binary file.