Variables
Each task is executed in a sandboxed environment. This means environment variables from your system are not forwarded to the internal shell. Let's have a look at the options you have to alter that behaviour.
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
Sandboxed Shell
To test this, let's create an env task which print the environment variables using the printenv command:
build:
env:
cmd: printenv
rebuild: always
Run bob build env to get:
$ bob build env
Building nix dependencies...
Succeeded building nix dependencies
Running task env with 0 dependencies
env running task...
env HOME=/home/andrei
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
The whole environment was cleared except HOME and PATH.
HOME and XDG_CACHE_HOME are whitelisted for convenience because many programs use them.
PATH was added because of default packages which were installed by nix. By default, Nix will install bash
, coreutils gnused and findutils packages.
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 HOME=/home/andrei
env LANG=en_US.UTF-8
env NEW_VAR=Hello
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
As you can see, the LANG variable was added from the local system and also NEW_VAR was created.