There are several steps missing from the golang Templ documentation when installing the command line tools on Mac.
The following are the walk-through steps you'll want to take so you can run templ generate
from the mac command line
Templ uses the nix package manager instead of something like Homebrew to install and manage templ.
Go to the nix download page and select macOS
It should give you a bash script similar to:
sh <(curl -L https://nixos.org/nix/install)
It will go through a lenghtly process and ask you to use sudo
for full permissions
Nix by default doesn't have many of the experimental features enabled that templ requires to be installed. You might see these errors
error: experimental Nix feature 'nix-command' is disabled; add '--extra-experimental-features nix-command' to enable it
warning: unknown setting 'experimental-features'
warning: unknown setting 'nix.settings.experimental-features'
warning: unknown setting 'nix.extraOptions'
You need to enable nix experimental features to your nix conf.
Your nix conf should be installed at /etc/nix/nix.conf
on the Mac
Modify the nix.config
sudo vim /etc/nix/nix.conf
Add this line to the top of the file
experimental-features = nix-command flakes
From templ instructions run
nix run github:a-h/templ
Templ will be installed into the /bin
directory within /nix/store/
and sometimes doesn't auto symlink, so typing templ
might result in command not found
error.
To fix this, we can symlink it from the /nix/store/
directory but we need to know exactly where templ was installed, since the direcotries have their own unique hash values.
We can get the full directory by using the find
command
find /nix/store -type d | grep -E '/nix/store/[[:alnum:]]+-templ$'
Then we can symlink by running
ln -s /nix/store/r22hw3g6f0rkwy8d8bcgbjv3857g19lf-templ/bin/templ /usr/local/bin/templ
👀 Note: you'll have to use the directory we searched for earlier with your unique hash value. The above won't work if the hash values are different