I realized that I had still set GOPATH to a folder in my home directory. Upon removing it my development workflow for go with nix breaks, for obvious reasons, since commands like go mod download can’t write to the nix store.
Does everyone just keep a permanent GOPATH in their shell config? Do you set a GOPATH on a per project basis? Do you handle this completely differently? Note that I’m explicitly not talking about how to build the package for production. It’s just about the development workflow for go.
I just make sure that GOPATH is unset during development… go will fallback to $HOME/go then.
Since the existence of pkgs.go alone in a shell.nix will trigger GOPATH to point into the nix-store, I usually put shellHook = "unset GOPATH" into it. (Thanks for @jonringer telling me about it last week, before that I unset GOPATH manually after entering the shell).
I’m a bit confused by this solution. If it falls back to $HOME/go, then multiple projects share the $HOME/go folder even though they may have conflicting go versions.
This is my workaround, but it feels a bit hacky:
# Avoid sharing GOPATH with other projects.
PROJECT_NAME="$(basename "$PWD")"
export GOPATH="$HOME/.local/share/go-workspaces/$PROJECT_NAME"