By default Nix doesn't have dependencies on your existing environment without them being explicitly specified. If you want to do this, I would suggest adding a parameter to the .nix file and then passing them in when you build using --arg
$ nix-build
these derivations will be built:
/nix/store/mcwn62zwczay4a06mk12zrs77424zgrj-example.drv
building '/nix/store/mcwn62zwczay4a06mk12zrs77424zgrj-example.drv'...
/nix/store/pa9p2zzmdgp8bgxw6qz42kdbrxdm9vr1-example
$ nix-build --argstr environmentVariable "hello!"
these derivations will be built:
/nix/store/2d5yzznb8wg721lblrcv5pv8wrxy2871-example.drv
building '/nix/store/2d5yzznb8wg721lblrcv5pv8wrxy2871-example.drv'...
hello!
/nix/store/11gn4sppncwzqp9dnrbk9snvisvy3xlf-example
Yes, that's how I can get an envvar into a runCommand, which takes envvars as an argument. I need the envvar to end up in a haskellPackage.developPackage which doesn't take envvars as an argument :thinking:
{-# LANGUAGE TemplateHaskell #-}moduleMainwhereimportLanguage.Haskell.THimportSystem.Environment(lookupEnv)importData.Maybe(fromMaybe)value::Stringvalue=$(runIO$dovar<-lookupEnv"environmentVariable"pure$LitE$StringL$fromMaybe""var)main::IO()main=putStrLnvalue
I also needed a .cabal file but I haven't included it here. This is how you would use it:
When building on TravisCI we pull in the values of a few of Travis envvars (https://docs.travis-ci.com/user/environment-variables/#default-environment-variables). It seems they aren't pushed into the
nix-build
environment. Any pointers on how to do that would be most appreciated.By default Nix doesn't have dependencies on your existing environment without them being explicitly specified. If you want to do this, I would suggest adding a parameter to the
.nix
file and then passing them in when you build using--arg
How can I explicitly specify that an envvar should be passed through?
I don't succeed in getting anything I pass in via
--arg
to actually end up as an envvar in the build environment.Here's an example:
default.nix
and you can use it as follows:
Yes, that's how I can get an envvar into a
runCommand
, which takes envvars as an argument. I need the envvar to end up in ahaskellPackage.developPackage
which doesn't take envvars as an argument :thinking:What is using these environment variables?
They are used during the build itself. Template Haskell picking them up and embedding them in the binary.
You can use
preBuild
to set those environment variables:default.nix
Main.hs
I also needed a
.cabal
file but I haven't included it here. This is how you would use it:$ nix-build --argstr environmentVariable "hello!" <...> $ result/bin/environment-example hello!
I can only have one
preBuild
right?No, you can have multiple ones as long as you prepend previous overrides by doing something like
Apparently the following works too:
Cool!
Thanks for your help. You put me on track to that
overrideAttrs
solution, which I rather like (easier to compose).