Environment variables set by Nix

I encountered a problem when using another tool (PM2 process manager) in nix-shell. The tool took the value of the shell variable ‘name’ set by Nix, which resulted in a failure.

I think the tool should not use the environment variables like this. I can fix this temporarily by unsetting ‘name’ in my nix-shell.

Is this allowed? Or does Nix require ‘name’ to be defined to work correctly?

More generally, what are the environment variables set by Nix which should not be changed inside nix-shell?


The related issue in PM2: https://github.com/Unitech/pm2/issues/1197#issuecomment-521375719

If you’re not relying on the functions defined by nix-shell, you can unset $name.
It’s just the name of the nix derivation used to create the shell. So you could create a derivation with the name you want to have that env variable.

As for other variables, you can use direnv to show the difference between environment variables (and also set them to your desire) when entering a directory that has a shell.nix.
E.g. I have:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  buildInputs = with pkgs; [ nodejs ];

and echo "use nix" > .envrc && direnv allow.
Direnv then says:

  +STRINGS +STRIP +TEMP +TEMPDIR +TMP +TMPDIR +buildInputs +builder +configureFlags
  +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget
  +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget
  +depsTargetTargetPropagated +doCheck +doInstallCheck +name +nativeBuildInputs
  +nobuildPhase +out +outputs +patches +phases +propagatedBuildInputs
  +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system

nix-shell creates all these variables and changes PATH.
For the shell to work you don’t really need any of the nix specific variables. Usually, since you just want a program available, PATH is enough. Most of the variables are actually functions, that nix would use to build a program.

Thank you for the information. I unset the ‘name’ variable and this seems to work fine.

I will keep in mind the actually the PATH setting is enough for Nix to work properly.