Environment variables in dev vs production

This question does involve a little bit more help with operating systems in general, apologies if it feels off-topic.

I am creating an environment variable editor with a backend in Rust (using Tauri). When running in dev, it shows all of my environment variables:

However, when running in production (building the app and installing it onto my Mac), it shows a much smaller list:

  • /usr/bin/
  • /bin/
  • /usr/bin/
  • /sbin

Is there any particular reason why the production version of my app can't show all variables, and is there any way to remedy that? My assumption is that it has something to do with the app running as root or something.

Additionally, each time I add a variable in dev mode, each time I add a variable, the window doesn't update unless I restart the shell--i.e. close the program, open a new terminal, and run the app again. Is there a way to restart the shell from within a Rust app?

Thanks!

Environment variables are meant to be ~immutable per execution. The exception being to set them when you're 100% sure no threads have been spawned.

So you can't modify the actual environment of the shell that spawned you. And typically you can't modify the variable values (which may shadow the actual environment) of the shell that spawned you, either.

Here's a related recent thread.


As for the difference in PATH when running in different environments... yeah the different environments are just setting up different environment variables through whatever means (.profile or .bashrc or ...). How did you imagine they would be synchronized?


Adding where? Adding to something the shell reads upon initialization? I think probably you have some misconception about the relationship between processes and where the environment is coming from.

You could use exec to launch a new instance of your shell which then launches your program again I suppose. But I'm uncertain if saying that is a clue leading you towards what you want, or bait leading you further astray.

Probably you need to learn more about how shells work.

This is not “not showing all variables”, it's that the content of the PATH variable is different. This because the environment is different depending on how the program was run. The big PATH you see in the first example was probably mostly created by commands in your shell init files (.bashrc, .profile, .bash_profile, .zshrc — the relevant file depends on your shell).

Environment is not a global thing — it is a characteristic each process has. (And there's no such thing as a hidden environment variable.)

each time I add a variable in dev mode, each time I add a variable, the window doesn't update unless I restart the shell

What exactly is your program doing to “add a variable”? Based on the results you describe, it sounds like you're editing a file (perhaps one of the shell init files I mentioned). If so, then the goals of your editor will be better served by reading that file rather than by consulting its own environment. This will not only update appropriately, but it will also be more correct; it will not be replacing the contents of the file with values from its own environment (which might be different).

2 Likes

Ahh, that makes tons of sense. That probably explains the discrepancy between the two--when I'm running it in dev mode, it's run from my terminal so running env::vars() managed to get everything from my shell file.

Yup, editing ./zshrc. I think in that case, reading through the shell files seems like the best bet. Thank you so much!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.