Environment file customized for different OSes


I work on a mac under the macos (ventura) and I also have a windows VM that I run under parallels. I use VS code to develop the code of the project. Mainly, I work under macos but the final app will run on windows, so I was thinking of developing more often in the Windows VM. The code is shared between macos & the windows vm - it's in the same folder.

The question that I have is: the application reads some settings from a .env file, such as a database url. The database url is different on windows than the one on macos. What options do I have in order to be able to run the same code on windows and macos? I was thinking of some options:

  1. Use a different .env file on windows than on macos, but how to I tell the executable which .env file to use?
  2. Use the same .env file but annotate the settings with a prefix or suffix, something like: database_url.macos =postgres:.... , database_url.windows=postgres. Is this achievable at all?
  3. Distinguish at runtime the OS and use the appropriate setting from the .env file.

I like #2 best. Other options?


I ended up creating 2 env files, .env.windows & .env.macos and then using a build.rs file that gets invoked when I do the builds and which copies one of the 2 files to the .env file:

// build.rs

fn main() {
    // Determine the target OS at build time
    let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();

    // Define the path to the .env file based on the target OS
    let env_file_path = match target_os.as_str() {
        "macos" => ".env.macos",
        "windows" => ".env.windows",
        _ => ".env", // Use the default .env file for other OSes

    // Copy the selected .env file to the output directory
    std::fs::copy(env_file_path, ".env").expect("Failed to copy .env file");

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.