Latex provides several commands relating to creating new commands such as \newcommand. If I want to define new commands from a Lua script one way is use tex.print to write this directly into the document.
It this really the best way though? The whole point of LuaTeX is to avoid programming with Tex macros, yet this approaches involves doing exactly that. How can I directly access, edit and create commands directly from Lua?
script.lua
tex.print("\\newcommand{\\hello}{Hello world}")
document.tex
\documentclass{article}
\begin{document}
\directlua{ require("script") }
\hello
\end{document}

luacodeenvironment instead see luatex - Use lualatex to create macros - TeX - LaTeX Stack Exchange – user202729 Jan 06 '22 at 02:28