How to access user-login shell environment variables from Jupyter notebook

When our users login via ssh, they have a bunch of credentials (set as environment variables) in their environment. These credentials expire after 24 hours.

When a user starts (‘spawns’? is this the terminology?) a Jupyter notebook server, these environment variables are no longer present in the environment (e.g. running a cell that contains env in a notebook will not list these variables).

At the admin level, can we configure TLJH so that whenever a user spawns a notebook server, it will inherit their current environment variables? What is the appropriate config setting to do this?

Thanks!

Do any of these cover what you seek?

Document how to set environment variables for user notebooks
TLJH-How to set env vars for users after but not for the spawner

Thank you for sharing! I was aware of some of those answers already - the part that I’m struggling with is how can I set environment variables on a per-user basis.

The workflow would be something like:

  • user login via ssh, environment variables set (each user has a different value, they change daily)
  • user spawns a new notebook server
  • notebook server inherits environment variables from user environment and variables visible in notebook

Is this sort of things possible with TLJH?

(Thanks for your patience, maybe I just need to be a little more creative!)

TLJH uses SystemdSpawner. Since it’s Systemd the default environment variables won’t be the same as the variables you get when you login with SSH. I don’t know if simply wrapping the startup command with a login shell (bash -l -c "jupyter......") would work, or whether being inside systemd prevents that. Might be worth a try? Spawners — JupyterHub 2.0.0 documentation

Otherwise you could probably override/subclass the spawner to setup any cusotm environment you require?

Though since a user can configure arbitrary configuration when they login (e.g. they could highly customise their ~/.bashrc, or use some other shell), this may be tricky to get right. If the extra credentials that you’ve mentioned are specially setup for SSH only then that’s obviously completely separate so you’ll need to figure out how that works.

1 Like