tup

How do you use environment variables in Tup run statements?


I'm trying to run a file given an environment variable to generate some environment-specific rules.

However, Tup doesn't allow for environment variables to be used in Tupfiles directly (you can send them through to child processes using the export keyword but you can't use them in run statements).

How can I use an environment variable in a run statement?


Solution

  • You can achieve this by using the export keyword to pass the environment variable through to a child process (bash, in this case), and then using the child process' ability to access environment variables to then run whatever script you want.

    To do this, create a helper script:

    #!/bin/bash
    # bootstrap-script.sh
    "${!1}/$2"
    

    which takes the first argument passed to it, resolves it as a variable name, and then performs a replacement (where as the second argument is a direct replacement), and then run it from your Tupfile by using:

    # Tupfile
    export SCRIPT_DIR
    run ./bootstrap-script.sh SCRIPT_DIR my_script.py
    

    The above passes through SCRIPT_DIR to the previously mentioned bootstrap script, along with a target script or program to run (in order to generate rules using Tup's run keyword).