You can use a hook to achieve this. Use the one you need as described in auctex documentation. Here we check the file extension so that we don't do that for eg
.sty files. Also, we test the
buffer-read-only variable before modifying the buffer.
goto-char at the end ensures point is set at beginning of buffer.
(defun my/add-auctex-file-variables ()
(if (and (not buffer-read-only)
(string= (file-name-extension (buffer-file-name)) "tex"))
(add-file-local-variable 'coding 'utf-8-unix)
(add-file-local-variable 'TeX-engine 'luatex)
(add-hook 'LaTeX-mode-hook 'my/add-auctex-file-variables)
Also, as can be seen on
add-file-local-variable docstring (display it with
C-h f add-file-local-variable) any setting of the variable is first deleted, so there's no risk of accumulating such definitions.
A possible pitfall is that the file is marked as modified even if nothing has actually changed.