Automating your Rust workflows with cargo-make - Part 3 of 5 - Environment Variables, Conditions, Sub Tasks and Mixing

Image for post
Image for post

In the last article we talked about platform overrides and aliases based on OS (linux, windows and mac). But in many cases we will need something more complex and cargo-make built for rust gives that extra power.

In this article you will see how cargo-make allows you to setup conditions and help your scripting with environment variables with specific Rust info (but not just).

The previous articles in this series were:

Environment Variables

Environment variables are used everywhere and cargo make helps you define and use them.

First of all, lets look at how we define environment variables to be used in our execution flow.

Global Configuration

In your Makefile.toml, there is an env section where you can define any environment variables you want. When cargo-make starts up, it will load all of them before running any task.

For example:

Except the environment variables you define here, there are more defined in the default internal Makefile found here.

Inside Tasks

Tasks can also trigger environment variables to be defined, however unlike the global env section, these environment variables are only defined if the task is going to be invoked.

For example:

Command Line

You can also ask cargo-make to define environment variables when you run it as part of the command line using the --env or -e cli arguments, for example:

In addition to manually setting up all those environment variables, cargo-make will also automatically define a large set of them based on the current runtime.

The below list lists all of them:

  • CARGO_MAKE - Set to "true" to help sub processes identify they are running from cargo make.
  • CARGO_MAKE_TASK - Holds the name of the main task being executed.
  • CARGO_MAKE_WORKING_DIRECTORY - The current working directory (can be defined by setting the --cwd cli option)
  • CARGO_MAKE_RUST_VERSION - The rust version (for example 1.20.0)
  • CARGO_MAKE_RUST_CHANNEL - Rust channel (stable, beta, nightly)
  • CARGO_MAKE_RUST_TARGET_ARCH - x86, x86_64, arm, etc ... (see rust cfg feature)
  • CARGO_MAKE_RUST_TARGET_ENV - gnu, msvc, etc ... (see rust cfg feature)
  • CARGO_MAKE_RUST_TARGET_OS - windows, macos, ios, linux, android, etc ... (see rust cfg feature)
  • CARGO_MAKE_RUST_TARGET_VENDOR - apple, pc, unknown
  • CARGO_MAKE_CRATE_IS_WORKSPACE - Holds TRUE/FALSE based if this is a workspace crate or not (defined even if no Cargo.toml is found)
  • CARGO_MAKE_CRATE_WORKSPACE_MEMBERS - Holds list of member paths (defined as empty value if no Cargo.toml is found)

The following environment variables will be set by cargo-make if Cargo.toml file exists and the relevant value is defined:

  • CARGO_MAKE_CRATE_FS_NAME - Same as CARGO_MAKE_CRATE_NAME however some characters are replaced (for example '-' to '_').

The following environment variables will be set by cargo-make if the project is part of a git repo:

  • CARGO_MAKE_GIT_BRANCH - The current branch name.
  • CARGO_MAKE_GIT_USER_NAME - The user name pulled from the git config key.
  • CARGO_MAKE_GIT_USER_EMAIL - The user email pulled from the git config key.


Platform overrides and platform aliases give you some sort of condition handling for your scripts, but cargo-make has a much more powerful condition mechanism.

There are two types of built in conditions:

  • Criteria
  • Scripts

A task can define any or both types of conditions and only if all conditions are satisfied will the task be invoked.

Important to note that conditions only affect the task action and will not prevent its dependencies from being invoked. For dependencies there is another solution which will be covered in the next sections.


Criteria conditions are defined by the condition attribute which can define multiple different conditions to validate.

Below is an example of a condition script that checks that we are running on windows or linux (but not mac) and that we are running on beta or nightly (but not stable):

The follow criteria types exist:

  • platforms — List of platform names (windows, linux, mac)
  • channels — List of rust channels (stable, beta, nightly)
  • env_set — List of environment variables that must be defined
  • env_not_set — List of environment variables that must not be defined
  • env — Map of environment variables that must be defined and equal to the provided values

Let’s look at another example with all types:

As you can see setting up conditions using the rust channel shows yet another advantage of having a Rust aware task runner as you might want to run certain tasks for nightly channel only and so on…


You can also run custom scripts to validate more complex conditions using the condition_script attribute.

The condition is only met in case the script exit value is 0, otherwise the task will not be invoked.

Here is an example of a condition that always fails and prevents the task from running the build command:

Sub Tasks

In the first article I talked about task being able to do three things, run commands, script or other tasks. The article goes on explaining the first two but not how to run other tasks.

A task can run another task using the run_task attribute. Unlike dependencies which are invoked before the current task, the task defined in the run_task is invoked after the current task.

For example:

The run_task is also another way to invoke a specific task more than once (in previous article I talked about aliases being the first way).

Mixing Conditions and Sub Tasks

We previously showed how conditions work and explained that conditions prevent the task from being invoked, but will not stop its dependencies. We also talked about in previous articles that dependencies is the cargo-make way to define flows. So how do we have a condition prevent an entire flow?

If you mix conditions and sub tasks (run_task), you now have a way to define a conditional sub flow.

For example, if you have a flow that should only be invoked in a travis build, not on windows, and only if the rust channel is nightly. The flow is defined as follows:

But we need to setup conditions to prevent tasks1, 2, 3 from being invoked. We can set it up as follows:

We now invoke my-flow which in turn, if all conditions are met, will invoke the my-flow-no-condition task. And only now, the dependencies are invoked.

Mixing conditions and sub tasks is an important feature which is used a lot in the internal cargo-make Makefile.toml

In the next article I’ll talk about Rust workspaces and how cargo-make supports them, in addition to init/end tasks and external makefiles.

Written by

Software Engineer, today working mostly in web development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store