Nix-shell: how to specify a custom environment variable?

I am learning nixos and nix expressions. In the project folder I have created shell.nix and when I run nix-shell

I want it to set an environment variable for me. For example, to set PGDATA env var.

I know there are several ways to write nix expression files (I'm not used to most of them yet). Here's my example:

shell.nix

let 
  pkgs = import <nixpkgs> {};
  name = "test";
in pkgs.myEnvFun {
  buildInputs = [
    pkgs.python
    pkgs.libxml2
  ];
  inherit name;
  extraCmds = ''
    export TEST="ABC"
  '';
 }

      

+3


source to share


1 answer


Use the buildPythonPackage function (which uses mkDerivation). By passing anything, the env variables will be set in the bash shell:



with import <nixpkgs> {};

buildPythonPackage {
  name = "test";

  buildInputs = [ pkgs.python pkgs.libxml2 ];

  src = null;

  PGDATA = "...";
}

      

+3


source







All Articles