Add cleanup combinator

This commit is contained in:
Alex David 2025-06-14 22:11:21 -07:00
commit b10ff57b1e
2 changed files with 45 additions and 1 deletions

View file

@ -20,6 +20,7 @@
env = {};
namespaces = {};
included-once = []; # See include-once combinator
cleanup = []; # See cleanup combinator
};
in lib.pipe initial-state (
# apply pre-user combinators
@ -75,8 +76,14 @@ in lib.pipe initial-state (
++ [
(state: ''
RUNTIME_ARGS=()
${if builtins.length state.cleanup > 0 then ''
function cleanup {
${lib.concatStringsSep "\n" state.cleanup}
}
trap cleanup EXIT
'' else ""}
${state.runtime}
exec ${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
${if builtins.length state.cleanup > 0 then "" else "exec "}${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
'')
(text: pkgs.writeShellApplication { inherit name text; })