There’s various substitution methods in that section of the manual that may or may not be easier to use for your particular circumstance.
EDIT: Unrelated, but it’s hilarious that this happens so often that @TLATER and I respond with very similar explanations at virtually exactly the same time lol. Thank you for your contributions!
You could instead use home.sessionVariables to set an environment variable and use that in the script instead and just write a normal script accessing that with e.g. $A_NAME.
Writing a derivation that uses substitute is probably preferable, though if you wanted the script to work in non-NixOS contexts doing the above can make a lot of sense.
While I’m here, I will mention: I really like how this makes my dotfiles quite declarative and “integrated” with NixOS; on the other hand, I don’t like that my path to deploying a new version of a dotfile that I’m tinkering with now includes doing a full rebuild of my system. Not sure if there’s any advice out there about this tradeoff.