5

I just noticed that Haskell programs run via stack do not receive environment variables from the calling environment. Here's a sample program:

-- testenv.hs
import System.Environment
main :: IO ()
main = print =<< getEnv "FOOBAR"

If I run it without stack, like this, it works:

% FOOBAR=123 runhaskell testenv.hs
"123"

But using stack:

% FOOBAR=123 stack runhaskell testenv.hs
testenv.hs: FOOBAR: getEnv: does not exist (no environment variable)

Same goes for when it is compiled: FOOBAR=123 stack exec testenv fails while FOOBAR=123 .stack-work/install/BLAHBLAH/testenv works.

Is there a way to force stack to pass-through certain environment variables?

The real problem I'm having is with yesod devel, there are some settings I want to override with environment variables, but yesod devel uses stack to run the program so they don't go through.

This is stack 1.6.5 on NixOS 18.03.132262.0a73111bc29.

chrisleague
  • 558
  • 2
  • 15
  • 2
    For stack 1.6.5 I cannot reproduce it with your example. Could it be some other, special environment variable? – max630 May 24 '18 at 05:49
  • Yeah I can't reproduce your issue either but the way to do it is compile it and run it with `FOOBAR=123 stack exec mybin`. `stack exec` seems to be what you want. – urbanslug May 24 '18 at 11:00
  • Actually I meant `stack exec` when I said `stack run` – I'll edit the question – but it's the same behavior whether it's `stack runhaskell` / `stack runghc` / `stack exec`. This is stack-1.6.5 but on NixOS, so maybe a bug with packaging? – chrisleague May 24 '18 at 13:19
  • 2
    This does have something to do with NixOS. I had `nix: enable: true` in my global `config.yaml`. After I turned this off, environment variables pass through as they should. I thought nix support in stack only meant that it would install non-Haskell dependencies using nix; but it seems like it might be running stack in a nix-shell? – chrisleague May 24 '18 at 15:07

1 Answers1

7

It seems like this is the relevant section of the stack manual, which I missed:

“By default, stack will run the build in a pure Nix build environment (or shell), which means two important things: (1) basically no environment variable will be forwarded from your user session to the nix-shell [...]”

So this advice worked:

“To override this behaviour, add pure: false to your stack.yaml or pass the --no-nix-pure option to the command line.”

% FOOBAR=123 stack --no-nix-pure runhaskell testenv.hs
"123"
chrisleague
  • 558
  • 2
  • 15