Is it possible to access arbitrary environmental v...
# nostupidquestions
s
Is it possible to access arbitrary environmental variables inside a nextflow config?
j
Copy code
env("VAR_NAME")
should do it, I think
s
Oh awesome, that makes my life much easier
t
You can also use the variables in strings, just like in `bash`:
Copy code
singularity {
    enabled    = true
    // /tmp is often too small -> redirect to scratch:
    runOptions = "-B /scratch/$USER:/tmp"
}

env {
    SINGULARITY_TMPDIR   = "/scratch/$USER/singularity/tmp"
    SINGULARITY_CACHEDIR = "/scratch/$USER/singularity/cache"
}
(Obviously that works also with other variables besides
$USER
, these are just my use cases.) Be sure to to use double quotes to ensure expansion.
s
Oh really? I had just assumed that wouldn't work since it's so rare to see
t
It works for me. One thing to be careful about is when those expressions are evaluated. E.g.
$USER
is not likely to change over the run of a pipeline, but
$HOSTNAME
is likely different between the point where you start the pipeline and where a particular process is run. I think you can use closures (
{ ... }
) to control that.
s
This is for some on-prem infra, we want to tag k8s executor jobs/pods with the user creating them as an annotation for easier tracking, this isn't for any specific workflow so I'm not concerned about flow etc
👍 1
a
Pretty sure the implicit access is not recommended now and that you should be using
system.getEnv
or
env
(docs - I think they are a bit confusing on the new best practice though personally)
party this 1