Search code examples
haskellenvironment-variablescabal

Haskell: Why are my environment variables not available when running my project under cabal?


Does anyone know how to define environment variables to be accessed by a program run with cabal?

I want to define system environment variables that are available to my program, e.g. via System.Environment's 'getEnv' function. The variables are available to 'getEnv' in ghci, but not to a program run with cabal ('cabal run').

I define my environment variables as:

$ export myvar=MYVALUE

Accessing them works in ghci:

Prelude System.Environment> getEnv "myvar"
"MYVALUE"

However, similarly using 'getEnv' in my program and running with 'cabal run' gives the error:

getEnv: does not exist (no environment variable)

Somehow my program is not finding them when run via cabal. I tried placing the 'export' statements in my .bash_profile and restarting my terminal. But this gave the same issue of working in ghci and giving that error when run with cabal.


Solution

  • Thanks for the comments, I realized that this was just due to a stupid mistake by me.

    My program used a couple different environment variables, some of which were being successfully read -- the error was due to a missing one which I hadn't tested in ghci.

    So the issue was due to not testing all the variables, a silly mistake, but thanks for the comments anyway as they were helpful in diagnosing the issue.