Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variables into Haskell programs run via stack?

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.

like image 365
chrisleague Avatar asked May 24 '18 04:05

chrisleague


1 Answers

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"
like image 126
chrisleague Avatar answered Oct 31 '22 19:10

chrisleague