#Is there an example/article that explains how to use environment variables with Tauri?

33 messages · Page 1 of 1 (latest)

wet skiff
#

I need to provide an environment variable trough different ways;

  • from local environment while I run the app via command line (tauri dev, tauri build)
  • from GitHub Actions while building the app

So far, I have found this answer https://github.com/tauri-apps/tauri/discussions/2873#discussioncomment-4333701
It uses a command to fetch variable with std::env::var(name) and pass it to FE with a Tauri command. But I'm only troubling to understand the mechanism for the two use cases mentioned above.

cyan phoenix
#

in this case you want the env!() macro instead of the std::env::var() function (not confusing at all huh).
https://doc.rust-lang.org/std/macro.env.html the macro reads the env var at build time, and the function reads it when the app actually runs

#

note that many frontend bundlers have a build-in mechanism for the env!() macro if you don't wanna do the rust roundtrip

wet skiff
#

I'm still trying to understand the concept. I have two variables, one is a secret hash and other one is a environment related variable.
I have found that there's a plugin called tauri-plugin-stronghold and I thought I could use it to store my secret hash value. But the thing is, I'm not sure how to use it, supply the secret key to Tauri builder or the main.rs while it's building on GitHub actions

cyan phoenix
#

i don't think you can give stronghold any values to store at build time, only at runtime, similar to localStorage

#

how secret is the key? like, is it alright if one can read it if you open the executable in a hex editor for example?

#

(if not then i guess there's no way to embed it in the app at build time anyway)

wet skiff
#

It's not that secret luckily. Embedding is my preferred way, but how 😄 I just tried using the dotenv file trough creating a .env file from the GitHub Actions and seems like it has it's own security that prevents you from writing the has value and end up having a ***

cyan phoenix
#

typically just in the logs though

#

like, the value itself is there, but it's hidden in the logs

#

but at the same time, no idea about .env files in github action. i only ever use "real" env vars

wet skiff
#

I could use it too but "how" is still an issue

#

:/

#

${{ secrects.KEY }} is the variable that holds the value. Can I pass this to Tauri's CLI command somehow?

cyan phoenix
wet skiff
#

What about accessing that value with the macro.env

cyan phoenix
#

matching whatever name you gave in the workflow file of course

wet skiff
#

😮

#

Will try it in a bit

#

What about setting the variable from local environment? Do I have to add the variable at OS level or is it possible to provide it projects scope?

cyan phoenix
#

depends if you want it temporary or permanent. temporary you can use ENV_VAR="whatever" yarn tauri build on linux/macos and $env:ENV_VAR="whatever" ; yarn tauri build in powershell on windows

#

permanently probably needs some package.json scripts via npm package helpers

wet skiff
#

Something like cross-env?

cyan phoenix
#

yea

#

there is also something similar that can read .env files

wet skiff
#

dotenv?

#

I got the point I think. Will that be enough to pass the variable to main.rs?

cyan phoenix
# wet skiff `dotenv`?

i think that one only works inside actual node scripts, not in package.json like cross-env does.
env-cmd may work easier idk🤔

wet skiff
#

Endless errors 😄

#

Trough the npm command, I could successfully pass variable at my local env. This is only a analyzer issue but it is an annoying one