View Source Environment variables

Nerves uses environment variables to control the build process and pass options to cross-compilers.

Overridable variables

Set these variables if you would like to adjust how Nerves builds device-specific code.

NameDescription
XDG_DATA_HOMEIf set, uses $XDG_DATA_HOME/nerves as the data directory. Defaults to ~/.nerves if unset
NERVES_DL_DIRPath where compressed Nerves system and toolchain artifacts are downloaded during mix deps.get. Defaults to $XDG_DATA_HOME/nerves/dl
NERVES_ARTIFACTS_DIRPath where Nerves system and toolchain artifacts are decompressed (from $NERVES_DL_DIR/<artifact-name>) and cached for use with compilation. Defaults to $XDG_DATA_HOME/nerves/artifacts
NERVES_ENV_DISABLEDSet to 1 to disable the nerves_package compiler
NERVES_DEBUGSet to 1 to print out debug info during compilation
NERVES_LOG_DISABLE_PROGRESS_BARSet to 1 to disable progress bar output when fetching artifacts (typically for CI)
SOURCE_DATE_EPOCHUsed for reproducable builds. Can also be set via config :nerves, source_date_epoch: val

Nerves-provided environment variables

Nerves sets the environment variables in this section to control compilation. Most variables affect the compilation of C and C++ code so that they use the right crosscompiler, flags, and directories. These environment variables are available to mix, rebar3 and any code invoked from them. For example, these are frequently used in the Makefiles invoked by elixir_make.

NameMin nerves_system_br versionDescription
AR_FOR_BUILDv1.13.1The host's ar
AS_FOR_BUILDv1.13.1The host's as
CCAllThe path to gcc for crosscompiling to the target
CC_FOR_BUILDv1.13.1The host's cc
CFLAGSAllRecommended C compilation flags
CFLAGS_FOR_BUILDv1.13.1Recommended C compiler flags for the host
CMAKE_TOOLCHAIN_FILEv1.18.3To build CMake projects, configure CMake with -DCMAKE_TOOLCHAIN_FILE="$(CMAKE_TOOLCHAIN_FILE)"
CPPFLAGSv1.14.5Recommended C preprocessor flags
CPPFLAGS_FOR_BUILDv1.13.1Recommended C preprocessor flags for the host
CROSSCOMPILEAllThe path and prefix for the crosscompilers (e.g., "$CROSSCOMPILE-gcc" is the path to gcc)
CXXAllThe path to g++ for crosscompiling to the target
CXX_FOR_BUILDv1.13.1The host's g++
CXXFLAGSAllRecommended C++ compilation flags
CXXFLAGS_FOR_BUILDv1.13.1Recommended C++ compiler flags for the host
ERL_CFLAGSAllAdditional compilation flags for Erlang NIFs and ports
ERL_EI_INCLUDE_DIRAllRebar variable for finding erl interface include files
ERL_EI_LIBDIRAllRebar variable for finding erl interface libraries
ERL_LDFLAGSAllAdditional linker flags for Erlang NIFs and ports
ERTS_INCLUDE_DIRAllerlang.mk variable for finding erts include files
GCC_FOR_BUILDv1.13.1The host's gcc
LD_FOR_BUILDv1.13.1The host's ld
LDFLAGSAllRecommended linker flags
LDFLAGS_FOR_BUILDv1.13.1Recommended linker flags for the host
PKG_CONFIG_SYSROOT_DIRv1.8.5Sysroot for using pkg-config to find libraries in the Nerves system
PKG_CONFIG_LIBDIRv1.8.5Metadata for pkg-config on the target
QMAKESPECv1.4.0If Qt is available, this points to the spec file
REBAR_TARGET_ARCHAllSet to the binutils prefix (e.g., arm-linux-gnueabi) for rebar2
STRIPAllThe path to strip for target binaries (Nerves strips binaries by default)
NERVES_APPAllCurrent Nerves project root path
NERVES_SYSTEMAllPath to target Nerves system to use ($NERVES_ARTIFACTS_DIR/<system-name>)
NERVES_TOOLCHAINAllPath to target Nerves toolchain to use ($NERVES_ARTIFACTS_DIR/<toolchain-name>)
NERVES_SDK_IMAGESAllPath to Nerves system images directory ($NERVES_SYSTEM/images)
NERVES_SDK_SYSROOTAllPath to Nerves system sysroot directory ($NERVES_SYSTEM/staging)
TARGET_ABISee belowThe target ABI (e.g., gnueabihf, musl)
TARGET_ARCHSee belowThe target CPU architecture (e.g., arm, aarch64, mipsel, x86_64, riscv64)
TARGET_CPUSee belowThe target CPU (e.g., cortex_a7)
TARGET_GCC_FLAGSSee belowAdditional options to be passed to gcc. For example, enable CPU-specific features or force ASLR or stack smash protections
TARGET_OSSee belowThe target OS. Always linux for Nerves.

Also see the elixir_make documentation for additional environment variables that may be useful.

Target CPU, ARCH, OS, and ABI

The TARGET_* variables are optionally set by the Nerves system. All official Nerves systems set them, but it is not mandatory for forks. These variables are useful for guiding compilation of LLVM-based tools.

The current way of deriving their values is to use zig and to select the combination that makes most sense for the target. To view the options, install zig and run:

zig targets | less

These variables are defined as custom environment variables in the Nerves system's mix.exs. For example, the following is the definition for the Raspberry Pi Zero:

  defp nerves_package do
    [
      type: :system,
      ...
      env: [
        {"TARGET_ARCH", "arm"},
        {"TARGET_CPU", "arm1176jzf_s"},
        {"TARGET_OS", "linux"},
        {"TARGET_ABI", "gnueabihf"}
      ]
      ...
    ]
  end

While the TARGET_* environment variables are mostly geared for non-gcc compilers, it's useful to add custom flags to gcc invocations as well. The TARGET_GCC_FLAGS option supports this. The Nerves tooling will prepend the contents of TARGET_GCC_FLAGS to the CFLAGS and CXXFLAGS used when compiling NIFs and ports. This can be used to enable features like ARM NEON support that would otherwise be off when using crosscompiler toolchain defaults. Most users don't need to concern themselves with TARGET_GCC_FLAGS. If you are creating a custom system, not setting TARGET_GCC_FLAGS is almost always fine, but will result in NIFs and ports being built with generic compiler options.