I am not sure if I am doing something wrong, or if this is a bug, or if this is the way Flox works.
I expected that packages installed inside a Flox environment would somehow be self-contained inside this environment. This would include creating local environment variables during the installation. However, when I installed Go, I found it uses the global Go installation’s environment.
Steps to replicate the issue:
Have Go installed on the system (mine is installed via Homebrew)
Init a Flox environment
Install Go
Test the Go environment outside the flox env
Activate the flox env
Test the Go environment and find that it is the global one. (See the shell log below: both Go installations use the same GOENV that points to /Users/christoph/Library/Application Support/go/env.)
I would have expected that the Flox-installed Go sets up its own GOENV and doesn’t use my system-global GOENV.
This results in two rather serious problems:
The Flox environment is not really shareable, as the Go environment lives outside the Flox environment. This means I still have the “works on my machine” problem I want to escape by using Flox.
With every change to a GOENV variable, I mess with my system-global Go installation. I would need to fix GOENV manually every time I do a flox install go.
Did I miss a step?
Is this an expected behavior?
Is this a bug with the Go Nix package?
Or, asking more generally: How can I install a Flox package and be sure that all the package’s config files and environment variables are automatically contained in the Flox environment?
Shell log:
Flox $ cd test
test $ flox init
✨ Created environment test (aarch64-darwin)
Next:
$ flox search <package> <- Search for a package
$ flox install <package> <- Install a package into an environment
$ flox activate <- Enter the environment
test $ flox install go
✅ 'go' installed to environment test at /Users/christoph/Temp/Flox/test
test $ which go
/opt/homebrew/bin/go
test $ go env GOENV
/Users/christoph/Library/Application Support/go/env
test $ flox activate
✅ You are now using the environment test at /Users/christoph/Temp/Flox/test.
To stop using this environment, type 'exit'
flox [test] test $ which go
/Users/christoph/Temp/Flox/test/.flox/run/aarch64-darwin.test/bin/go
flox [test] test $ go env GOENV
/Users/christoph/Library/Application Support/go/env
flox [test] test $
Unfortunately, we are lacking some integration work for the Go language, to make this more seamless.
Thus, as for your questions:
Is this an expected behavior?
Currently, given that you only install the go package, and Flox does not handle custom variables for that, this behavior is the “expected” default behavior of Go.
However, we recognize the need to handle this case better.
Did I miss a step?
Today, to have an isolated GOENV, you should be able to just add a line in your hook.script field in the .flox/env/manifest.toml:
Thank you, @ysndr, for the explanation and the tip about setting GOENV manually. This works, and it is certainly a workaround, even through I’d have to do this for all environment variables that are included in Go’s GOENV environment and have a path set at install time.
E.g, the variables
GOPATH=‘/Users/christoph/go’
GOCACHE=‘/Users/christoph/Library/Caches/go-build’
etc.
point to the same default paths that the Go installer would set outside a Flox installation.
The obvious workaround is to add a few more lines to hook.script.
I think GOCACHE, is effectively just a readonly cache for downloads and build intermediates?
In that case unless you want those to be separate for each project I assume they can remain shared/the same for all projects, since the project specific packages are already locked project wide.
It would at least save you some disk space… however much that may be…
Personally i understand the desire for project specific data to be separate, but since Go already has lockfiles / sumfiles that make a project specific “environment” maybe a virtual env approach is not entirely necessary.
Please take this with the two grains of salt, that are 1) that these are my personal thoughts and 2) that I’m rather foreign to the Go ecosystem.
You’re right, caches would not need to be environment-specific. GOPATH, on the other hand, also sets the base dir for other settings like GOBIN. If I compile a Go binary inside a Flox environment, it would end up outside the environment, in my global $GOPATH/bin (or $GOBIN if set).