refactor: move out large code blocks out of nix

This commit is contained in:
fufsob authored and FUFSoB committed 2026-10-04 11:54:04 +05:00
1 parent e8ace6523d
commit af5b3ff27a
47 files changed
+2220 -2070

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
# A script kept in its own file beside the Nix that runs it, with its @name@ placeholders
# filled from `vars`: replaceVars, but at eval time, so the text still goes to
# writeShellScript, runCommand and the like. Each value is spliced in as "${value}" would
# be, so a path lands in the store. Every name given must be a placeholder in the file and
# every placeholder must be given, so neither side can drift from the other unnoticed.
file: vars:
let
text = builtins.readFile file;
names = builtins.attrNames vars;
placeholders = builtins.concatMap (part: if builtins.isList part then part else [ ]) (
builtins.split "@([A-Za-z_][A-Za-z0-9_]*)@" text
);
unused = builtins.filter (name: !builtins.elem name placeholders) names;
unfilled = builtins.filter (name: !(vars ? ${name})) placeholders;
in
if unused != [ ] then
throw "${toString file}: no @${builtins.head unused}@ placeholder to fill"
else if unfilled != [ ] then
throw "${toString file}: @${builtins.head unfilled}@ is not given a value"
else
builtins.replaceStrings (map (name: "@${name}@") names) (map (name: "${vars.${name}}") names) text