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

Use buildPythonPackage function (that uses mkDerivation). Passing anything to it will set env variables in bash shell:

with import <nixpkgs> {};

buildPythonPackage {
  name = "test";

  buildInputs = [ pkgs.python pkgs.libxml2 ];

  src = null;

  PGDATA = "...";
}

To set environment variable for a nix-shell without creating a new package, shellHook option can be used. As shown in the example from the manual:

shellHook =
  ''
    echo "Hello shell"
    export SOME_API_TOKEN="$(cat ~/.config/some-app/api-token)"
  '';

A full shell.nix example based on my use-case - with go of version 1.18 from unstable channel:

let
  pkgs = import <nixpkgs> {};

  unstable = import <nixos-unstable> { config = { allowUnfree = true; }; };

in pkgs.mkShell rec {
  name = "go-1.18";

  buildInputs = with pkgs; [
    unstable.go_1_18
  ];

  shellHook = ''
    export PATH="$HOME/go/bin:$PATH"
  '';
}

The script also sets name option, which is then shown in the shell prompt (works nicely with Starship shell prompt).


You may also use pkgs.stdenv.mkDerivation.shellHook.

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

Tags:

Nixos