Variables
Each task is executed in a sandboxed environment. This means the environment is almost entirely cleared before the task is started. A few variables, in particular HOME, USER and DISPLAY, are retained.
Let's have a look at how you can manage your build environment.
Variables via Bobfile
You can define variables in your Bobfile using the variables
node:
variables:
VAR_ONE: hello
build:
build:
cmd: echo $VAR_ONE
rebuild: always
When running bob build
VAR_ONE
is made available to the task, see the hello
output:
$ bob build
Running task build with 0 dependencies
build running task...
build hello
build ...done
Variables via CLI
You can also set and forward variables via CLI using the --env
flag.
Having a bob.yaml
with following contents:
build:
hello:
cmd: echo $VAR_ONE $VAR_TWO
rebuild: always
you can run bob build hello --env VAR_ONE=Hello --env VAR_TWO=World!
to set the values of VAR_ONE
and VAR_TWO
:
$ bob build hello --env VAR_ONE=Hello --env VAR_TWO=World!
Running task hello with 0 dependencies
hello running task...
hello Hello World!
hello ...done
The priority of environment variables is CLI > Bobfile
which means that passing a variable via CLI will
overwrite other variable with the same name in your Bobfile.
The --env
flag is available on both bob build
and bob run
commands.
To forward variables from your environment just do bob build --env NAME_OF_VAR
(without =value).
Examples
Forwarding variables
You can forward variables from the local environment with the same --env
flag.
For example, if we want to use the LANG
variable from our system, we can just use --env LANG
.
Let's try to create a new variables and also keep one from the local system:
bob build env --env NEW_VAR="Hello" --env LANG
$ bob build env --env NEW_VAR="Hello" --env LANG
Building nix dependencies...
Succeeded building nix dependencies
Running task env with 0 dependencies
env running task...
env ....
env HOME=/home/andrei
env LANG=en_US.UTF-8
env NEW_VAR=Hello
env ....
env PATH=/nix/store/j9rc786ylq8cid4zgcn6idknilkbd4ax-bash-5.1-p16/bin:/nix/store/l2xyarvzahpz3fysr9hqbvcsgv5gnrnk-coreutils-9.1/bin:/nix/store/l62i4fn54qry9xamnj209wfizpw2bng5-gnused-4.8/bin:/nix/store/ip1zdc23sqyq15957lbjrxj31m5xh72c-findutils-4.9.0/bin
env ...done
If you browse through the list of environment variables you will notice that the LANG
variable was added from your
local system and also NEW_VAR
was created.