Environment variables and Nix

I’m trying out Nix for my Haskell development and I’ve run into an issue with environment variables.

I’m using haskellPackages.developPackage but I can’t seem to find a way to instruct it to allow environment variables through.

Environment variables are an example of external influences on building a package, one of the things nix tries to avoid. If you want to set environment variables while building a package you can always doing something like:

preBuild = ''
  export SOME_VAR="some value"

It’s typically even easier just to define the variable as a derivation attribute, unless it conflicts with some semantic variable.

I’m not 100% sure I understand what you mean, but I did find a solution I rather like:

addEnvVars = drv: drv.overrideAttrs (old: old // {
  TRAVIS_BUILD_NUMBER = (p.lib.maybeEnv "TRAVIS_BUILD_NUMBER" "not a CI build");
  TRAVIS_BRANCH = (p.lib.maybeEnv "TRAVIS_BRANCH" "not a CI build");
  TRAVIS_COMMIT = (p.lib.maybeEnv "TRAVIS_COMMIT" "not a CI build");

and then I adorn my package with

myPkg = haskellPackages.developPackage {
  root = ./.;

  modifier = addEnvVars;
1 Like