# Flox isn't setting environment variables in project environments

**URL:** https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720
**Category:** Help
**Tags:** closed, abandoned
**Created:** [April 10, 2023, 7:58pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720 "2023-04-10T19:58:34Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 10, 2023, 7:58pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/1 "2023-04-10T19:58:34Z")

</div>

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`:

```plaintext
  # 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.

```sh
❯ 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 🤷

I’m running v0.1.3.

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 11, 2023, 2:26pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/2 "2023-04-11T14:26:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 12, 2023, 6:54pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/3 "2023-04-12T18:54:40Z")

</div>

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](https://gist.github.com/worldofgeese/6460b8332a834538259e3f861c940578)

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 12, 2023, 7:47pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/4 "2023-04-12T19:47:22Z")

</div>

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:

```auto
$ 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.

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 13, 2023, 5:59pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/5 "2023-04-13T17:59:20Z")

</div>

How should one be doing shell expansion with Flox?

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

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 13, 2023, 6:00pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/6 "2023-04-13T18:00:30Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 13, 2023, 6:21pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/8 "2023-04-13T18:21:57Z")

</div>

I tried this

````plaintext
{
  # 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`

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

```

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 13, 2023, 6:44pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/9 "2023-04-13T18:44:06Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 13, 2023, 6:48pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/10 "2023-04-13T18:48:51Z")

</div>

I appreciate _your_ patience with _me_! You didn’t have to help but you did 🤗

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

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

```

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 14, 2023, 5:35pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/12 "2023-04-14T17:35:31Z")

</div>

@aameen 👋 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.

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 14, 2023, 5:59pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/13 "2023-04-14T17:59:07Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![superbird](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@superbird](https://discourse.flox.dev/u/superbird)
#### Post date: [April 14, 2023, 7:16pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/14 "2023-04-14T19:16:35Z")

</div>

> [@aameen](#):
>
> in your source tree do you currently have a `<PROJECT-ROOT>/__pypackages_/3.11/lib` directory containing modules?

I do!

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

```

> [@aameen](#):
>
> Also could you confirm that `PATH` was fixed by my previous recommendation?

It appears so

```sh
❯ 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

```

---

<div class="post-metadata">

### Author: ![aameen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/aameen/32/564_2.png) [@aameen](https://discourse.flox.dev/u/aameen)
#### Post date: [April 18, 2023, 4:07pm UTC](https://discourse.flox.dev/t/flox-isnt-setting-environment-variables-in-project-environments/720/15 "2023-04-18T16:07:00Z")

</div>

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:

```plaintext
{
  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;
  '';
}

```
