Add AST-extracted metadata from .nix module files to a srctree filesystem tree using nix-meta.
Embed metadata (descriptions, author info, version numbers) in your Nix modules without affecting the evaluated configuration. _meta is stripped before the module reaches evaluation.
- Parse —
nix-metaparses.nixfiles at the AST level and extracts the top-level_metaattribute set. - Strip —
_metais removed from each AST, then the modified AST is rendered to temporary files. - Load —
srctreeevaluates the stripped files. - Enrich — The evaluated content and extracted metadata are merged into the final metatree.
Note
Because _meta is stripped before evaluation, the evaluated modules never see the _meta attribute. This prevents type errors or warnings in strict modules.
Add metatree-nix to your flake.nix inputs:
{
inputs.metatree.url = "github:z1-0/metatree-nix";
outputs = { metatree, ... }: {
lib = metatree.lib;
};
}Add a top-level _meta attribute set to your module files:
# src/services/web.nix
{
_meta = {
description = "Web server service module";
author = "team-infra";
version = 1;
};
port = 8080;
host = "0.0.0.0";
workers = 4;
}let
pkgs = import nixpkgs { system = "x86_64-linux"; };
metatree = metatree.lib.load pkgs ./src;
in
metatreeload— Returns enriched tree structure directly.loadHaumea— Returns{ tree, attrs }for haumea-style access.
{
name = "web";
path = "/abs/path/to/src/services/web.nix";
type = "file";
content = {
port = 8080;
host = "0.0.0.0";
workers = 4;
};
meta = {
description = "Web server service module";
author = "team-infra";
version = 1;
};
}Tip
Use loadHaumea when you need both tree and attrs. Use load for direct tree access.
load :: pkgs -> src -> tree
Load a directory with srctree, extract _meta from Nix files, strip it, evaluate, and return the enriched tree.
pkgs— Package set for running parser/renderer utilities.src— Source directory to load.
loadHaumea :: pkgs -> args -> { tree, attrs }
Load with haumea-style return, providing both tree and clean attrs.
pkgs— Package set for running parser/renderer utilities.args— Arguments forwarded tosrctree.lib.loadHaumea.
withMeta :: pkgs -> tree -> tree
Enrich an already loaded srctree tree with AST-extracted _meta attributes.
pkgs— Package set for running parser/renderer utilities.tree— A srctree tree structure to enrich.