Flox isn't setting environment variables in project environments

Flox doesn’t set environment variables I declare in a project-level flox.nix. Here’s my customized flox.nix after running flox init --template project:

  # Set environment variables
  environmentVariables.LANG = "en_US.UTF-8";
  environmentVariables.PYTHONPATH = "$PYTHONPATH:$PWD/__pypackages__/3.11/lib";
  environmentVariables.PATH = "$PATH:$PWD/__pypackages__/3.11/lib";

  packages.nixpkgs-flox.python311 = { };
}

And outputs of commands that should give an idea of the problem.

❯ flox init --template project
Found git repo: /home/taohansen/Downloads/projects/mothers_emblems
wrote: /home/taohansen/Downloads/projects/mothers_emblems/flake.nix
wrote: /home/taohansen/Downloads/projects/mothers_emblems/flox.nix
Run 'flox activate' to enter the environment.

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.10.10 took 8s 
❯ flox install python311
Installed 'python311' package(s) into '.#default' environment.

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.10.10 took 4s 
❯ flox activate

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 
❯ exit
exit

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.10.10 took 15s 
❯ flox activate

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 
echo $LANG
en_DK.UTF-8

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 took 3s 
❯ exit
exit

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.10.10 took 1m22s 
❯ flox activate

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 
❯ echo $PATH
/home/taohansen/Downloads/projects/mothers_emblems/.flox/envs/x86_64-linux.default/bin:/home/taohansen/.local/share/flox/environments/worldofgeese/x86_64-linux.default/bin:/home/taohansen/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/home/taohansen/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/var/lib/flatpak/exports/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/home/taohansen/bin:/home/taohansen/.garden/bin

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 took 7s 
❯ echo $PYTHONPATH

It’s odd because I’m just coming back to Flox after a few weeks where I may have hallucinated this all working :person_shrugging:

I’m running v0.1.3.

Hey thanks for the report, I’ll see if I can help debug a bit.
You’re right that with the flox.nix file you’ve written LANG should be set to en_US.UTF-8 inside the environment.
I’ll start by trying to reproduce the issue, so getting a bit more info about your shell and “rc script” would help me investigate.

My immediate suspicion is that a profile script such as ~/.bashrc or ~/.zshrc is likely setting LANG after flox activate runs its profile setup. If you could share any relevant snippets from any ~/.profile or “rc scripts” related to LANG settings that would be a big help.

Thanks! It’s not just LANG, PYTHONPATH returns empty when it should be returning $PYTHONPATH:$PWD/__pypackages__/3.11/lib. Neither my .bashrc or .profile are setting any of these values.

My home.nix is at home.nix · GitHub

One thing I caught was that you were trying to use $PWD when declaring a shell variable.
One important behavior to take note of is that these variables are set “literally”, so no shell expansion is performed.
Here’s an example:

$ mkdir /tmp/example;
$ cd /tmp/example;
$ flox init -t project -i;
$ cat <<'EOF' > ./flox.nix;
{
  environmentVariables.LANG = "en_US.UTF-8";
  environmentVariables.GOOD = toString ./.;
  environmentVariables.BAD = "$PWD";
  shell.hook = ''
    echo "LANG: $LANG" >&2;
    echo "GOOD: $GOOD" >&2;
    echo "BAD:  $BAD"  >&2;
  '';
}
EOF

# re-render environment since we manually modified it:
$ EDITOR=cat flox edit -e '.#default';

# To prove that LANG gets set we'll use a junk value in another shell:
$ bash -c "export LANG=xxxx; . <( flox activate -e '.#default'; );";
LANG: en_US.UTF-8
GOOD: /tmp/example
BAD:  $PWD

Are you by any chance using direnv?
There might be additional hooks being executed there.

How should one be doing shell expansion with Flox?

I usually use direnv but for this example I’ve purged any .envrc

Shell expansion will work in the shell.hook.
So this works:

{
  environmentVariables.FOODIR = "foo";
  shell.hook = ''
    FOO="$PWD/$FOODIR";
  '';
}
1 Like

I tried this

{
  # Run shell hook when you enter flox environment
  shell.hook = ''
    PYTHONPATH="$PYTHONPATH:$PWD/__pypackages__/3.11/lib";
    PATH="$PATH:$PWD/__pypackages__/3.11/bin";
  '';
  packages.nixpkgs-flox.python311 = { };
}
```sh

then activated my Flox environment

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.10.10 
❯ flox activate

which returned an empty PYTHONPATH

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 
❯ echo $PYTHONPATH


Out of curiosity did you manually edit flox.nix or did you use flox edit?

There’s an unintuitive behavior ( we are working on it ) where you have to run flox edit to “refresh” a modified flox.nix file.

If you prefer to use a regular text editor you can run EDITOR=true flox edit <ARGS>; for the time being until we can roll out an improvement.

If you did use flox edit already, let me know and we can get into some more debugging.

I appreciate the report and your patience with this :slight_smile:

I appreciate your patience with me! You didn’t have to help but you did :hugs:

I did not use flox edit but it looks like that’s gone some way to helping! PATH and PYTHONPATH are still not performing expansions but they are being populated

mothers_emblems on  main [✘!+?] is 📦 v0.1.0 via 🐍 v3.11.2 
❯ echo $PYTHONPATH
:/home/taohansen/Downloads/projects/mothers_emblems/__pypackages__/3.11/lib

@aameen :wave: I saw you wrote a reply yesterday but it evaporated as quickly as it appeared. Was that a mistake or was there more research necessary? Happy to try any changes.

I had written a recommendation about escaping ${FOO} style variable expansion in your flox.nix file, but after re-reading your post I realized that wasn’t the issue.

I think I know why PYTHONPATH is not populating and need to cook up a nice explanation and example to teach you how to fix it. Just to help confirm my suspicion, in your source tree do you currently have a <PROJECT-ROOT>/__pypackages_/3.11/lib directory containing modules?

Also could you confirm that PATH was fixed by my previous recommendation?

I do!

✦ ❯ exa --tree --level=2 __pypackages__
__pypackages__
└── 3.11
   ├── bin
   ├── etc
   ├── include
   ├── lib
   ├── LICENSE.txt
   ├── README.rst
   ├── requirements.txt
   └── share

It appears so

❯ echo $PATH
/home/taohansen/Downloads/projects/mothers_emblems/.flox/envs/x86_64-linux.default/bin:/home/taohansen/.local/share/flox/environments/worldofgeese/x86_64-linux.default/bin:/home/taohansen/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/home/taohansen/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/var/lib/flatpak/exports/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/home/taohansen/bin:/home/taohansen/.garden/bin:/home/taohansen/Downloads/projects/mothers_emblems/__pypackages__/3.11/bin

One thing you can try changing in your setup hook is to only add “:” if PYTHONPATH is already set.
Right now you’re getting :<PROJECT-ROOT>/__pypackages__/3.11/lib which may cause issues.

You can use the following. Just to help debug I’ll add a few extra prints:

{
  environmentVariables.LANG = "en_US.UTF-8";
  packages.nixpkgs-flox.python311 = {};
  shell.hook = ''
    PYTHONPATH="$PWD/__pypackages__3.11/lib''${PYTHONPATH:+:$PYTHONPATH}";
    export PYTHONPATH;
    echo "PWD: $PWD" >&2;
    echo "LANG: $LANG" >&2;
    ls -R >&2;
  '';
}