Skip to content

Adding an option to control the environment variables that are being passed into the child processes #239

@AnirudhKanaparthy

Description

@AnirudhKanaparthy
// Options for nob_cmd_run_opt() function.
typedef struct {
    // Run the command asynchronously appending its Nob_Proc to the provided Nob_Procs array
    Nob_Procs *async;
    
    char *const *env; // <-- New Field!

    // Maximum processes allowed in the .async list. Zero implies nob_nprocs().
    size_t max_procs;
    // Do not reset the command after execution.
    bool dont_reset;
    // Redirect stdin to file
    const char *stdin_path;
    // Redirect stdout to file
    const char *stdout_path;
    // Redirect stderr to file
    const char *stderr_path;
} Nob_Cmd_Opt;

This is useful for when you want to run executables which depend on certain environment variables to be set like in the case of some Qt applications.

char *const *custom_env = clone_env(environ);
env_set(custom_env, "QT_QPA_PLATFORM_PLUGIN_PATH", "/path/to/your/platform/plugins");

Cmd cmd = {0};
cmd_append(&cmd, "./qtapp");
cmd_run(&cmd, .env=custom_env);

It can be even used to modify the PATH variable.

const char* path = env_get(custom_env, "PATH");
const char new_loc[] = "/custom/path";

size_t new_path_len = strlen(path) + strlen(new_loc) + sizeof(":");
char* new_path = (char*)calloc(new_path_len, sizeof(char));
snprintf(new_path, new_path_len, "%s:%s", new_loc, path);

env_set(custom_env, "PATH", new_path);

Cmd cmd = {0};
cmd_append(&cmd, "./some_bs_cross_compile_script.sh");
cmd_run(&cmd, .env=custom_env);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions