nte/engine.nix

106 lines
3 KiB
Nix
Raw Normal View History

2024-05-25 15:50:10 +02:00
pkgs: src: {extraArgs, entries, templates}: let
2024-05-22 21:19:37 +02:00
inherit (pkgs) lib;
2024-05-25 15:50:10 +02:00
inherit (builtins) abort dirOf toString;
inherit (lib.attrsets) hasAttr;
inherit (lib.lists) forEach findFirst;
2024-05-25 15:50:10 +02:00
inherit (lib.path) removePrefix;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg hasSuffix isString removeSuffix;
2024-05-22 21:19:37 +02:00
inherit (lib.trivial) functionArgs;
2024-09-13 16:21:40 +02:00
templates' = templates ++ [
2024-09-13 17:08:49 +02:00
(pkgs.writeText "passthrough.nix" /*nix*/''
{ format, output, ... }: {
name = "passthrough";
inherit format output;
}
'')
2024-09-13 16:21:40 +02:00
];
2024-09-13 11:17:33 +02:00
args = {inherit pkgs getEntry applyTemplate;}
2024-05-22 21:19:37 +02:00
// (import ./stdlib.nix pkgs)
// extraArgs;
2024-05-25 15:50:10 +02:00
isBaseTemplate = template:
isString template.output;
2024-05-22 21:19:37 +02:00
findTemplateFn = entry: let
template = findFirst (templateFile: let
templateFn = import templateFile;
template' = templateFn (functionArgs templateFn);
in
template'.name == entry.template)
null
2024-09-13 16:21:40 +02:00
templates';
2024-05-25 15:50:10 +02:00
in
if template == null then
2024-05-25 15:50:10 +02:00
abort "unknown template `${entry.template}`"
else
(import template);
2024-05-22 21:19:37 +02:00
applyTemplate = templateFn: entry: let
2024-05-22 21:19:37 +02:00
template = templateFn (args // entry);
in
if isBaseTemplate template then
template.output
2024-05-22 21:19:37 +02:00
else let
newEntry = template.output;
foundTemplateFn = findTemplateFn newEntry;
2024-05-22 21:19:37 +02:00
in
applyTemplate foundTemplateFn newEntry;
2024-05-22 21:19:37 +02:00
applyTemplateFile = templateFn: entry: {
inherit (entry) file;
output = applyTemplate templateFn entry;
};
2024-05-25 15:50:10 +02:00
replaceSuffix = from: to: string:
if !(hasSuffix from string) then
abort "invalid suffix `${from}` for string `${string}`"
2024-05-22 21:19:37 +02:00
else
2024-05-25 15:50:10 +02:00
concatStrings [ (removeSuffix from string) to ];
getTemplateFormat = entry: templateFn: let
2024-05-25 15:50:10 +02:00
# getEntry needs to go down to the base template for the format
# but any template through the way can ask for a file
# so we just give it a placeholder - an empty string here
# since the output of this thing doesn't matter
template = templateFn (args // entry // { file = ""; });
in
if isBaseTemplate template then
template.format
else let
newEntry = template.output;
foundTemplateFn = findTemplateFn newEntry;
2024-05-25 15:50:10 +02:00
in
getTemplateFormat newEntry foundTemplateFn;
2024-05-25 15:50:10 +02:00
getEntry = entryFile: let
sourceFile = toString (removePrefix src entryFile);
entry = (import entryFile) args;
foundTemplateFn = findTemplateFn entry;
entryFormat = getTemplateFormat entry foundTemplateFn;
2024-05-25 15:50:10 +02:00
in
if !(hasAttr "file" entry) then
entry // {
file = replaceSuffix ".nix" ".${entryFormat}" sourceFile;
}
else
entry;
processEntryFile = entryFile: let
foundTemplateFn = findTemplateFn entry;
2024-09-13 11:02:03 +02:00
entry = getEntry entryFile;
in
applyTemplateFile foundTemplateFn entry;
in /*sh*/''
${concatMapStrings
(result: /*sh*/''
mkdir -p $out/${dirOf result.file}
echo ${escapeShellArg result.output} > $out/${result.file}
'')
(forEach entries processEntryFile)
}
''