I have a nix flakes project that uses flake.parts that fails nix build because the app expects certain environment variables to be set. I added these to the nix shell using shellHook, so building the app works after doing nix develop.
What’s the standard way of setting these env vars? Is there a better way of setting env vars within the shellHook?
Functions like mkDerivation usually set all their arguments as environment variables for the build. This is what you’re supposed to do; you’re supposed to have the derivation include the environment variable, not set it in shellHook. It’s impossible to say what specifically you should do without seeing your nix expressions.
I’m not entirely sure which function from flake-parts calls mkDerivation. Unfortunately, it seems working with nix libraries makes things harder to follow. For example, I stopped using https://devenv.sh/ because I couldn’t debug an error message that came up during nix flake check even though devenv was convenient for provisioning services.
I’d expect adding
flake = {
MY_ARG = "foo";
};
within the call to flake-parts.lib.mkFlake would work. However, the build still fails for the same reason as before. The app can’t read the environment variable during nix build.
haskell-flake has an overrides option that allows you to override any Haskell package (dependency or local package). Specifically you want overrideAttrs. You might also want to put the env var in the shellHook so the language server knows about it as well.