# User-defined environment variables in z2jh

**URL:** <https://discourse.jupyter.org/t/user-defined-environment-variables-in-z2jh/1812>\
**Category:** Zero to JupyterHub on Kubernetes\
**Created:** [August 1, 2019, 5:18pm UTC](https://discourse.jupyter.org/t/user-defined-environment-variables-in-z2jh/1812 "2019-08-01T17:18:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian-r-rose](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/ian-r-rose/32/1119_2.png) [@ian-r-rose](https://discourse.jupyter.org/u/ian-r-rose)\
**Post date:** [August 1, 2019, 5:18pm UTC](https://discourse.jupyter.org/t/user-defined-environment-variables-in-z2jh/1812/1 "2019-08-01T17:18:19Z")

</div>

Is there a good way to propagate user-defined environment variables to kernels in a z2jh-based deployment? Users can set their own variables (such as personal API keys) in a `.bashrc` file, which makes them available in terminals, but these are not inherited by kernels.

I could imagine a new entry point `Spawner.cmd` that tries to source a `.bashrc` after mounting the user volume, but before launching the notebook server, but this seems a little brittle to me.

---

<div class="post-metadata">

**Author:** ![colinwilson](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/colinwilson/32/7722_2.png) [@colinwilson](https://discourse.jupyter.org/u/colinwilson)\
**Post date:** [August 15, 2022, 6:04pm UTC](https://discourse.jupyter.org/t/user-defined-environment-variables-in-z2jh/1812/2 "2022-08-15T18:04:46Z")

</div>

In case anyone runs across this in the future, I had a similar usecase where I wanted our users to be able to set their own personal API keys for internal tools in a `.bashrc` file or the like. With inspiration from [this answer](https://discourse.jupyter.org/t/how-to-make-created-custom-conda-environments-persist-across-user-sessions-in-jupyter-hub-on-aws-eks-cluster/12111/4) I created a file at `/usr/local/bin/start-notebook.d/source_bashrc.sh` that contained:

```auto
#!/usr/bin/env bash

if [-f "/home/jovyan/.bashrc"]; then
  source /home/jovyan/.bashrc
fi

```

I added this to our custom docker image. I also had to change the command being run in the pod to start the Jupyter Notebook instance from the default of `/opt/conda/bin/jupyterhub-singleuser` to use `/usr/local/bin/start-singleuser.sh`. That will call `/usr/local/bin/start.sh` which will run things palced in `/usr/local/bin/start-notebook.d/`. I did that by making my custom images in `profileList` look like:

```auto
  profileList:
    - display_name: "Python 3.9"
      description: "Python 3.9 with Poetry installed"
      kubespawner_override:
        image: "customeImageName"
        cmd: "start-singleuser.sh"

```

---

<div class="post-metadata">

**Author:** ![agelosnm](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/agelosnm/32/10273_2.png) [@agelosnm](https://discourse.jupyter.org/u/agelosnm)\
**Post date:** [November 24, 2023, 8:06pm UTC](https://discourse.jupyter.org/t/user-defined-environment-variables-in-z2jh/1812/3 "2023-11-24T20:06:14Z")

</div>

In my case I did the same but instead of `.bashrc` I used `.profile`
