Search code examples
environment-variablescondazshhostname

Can't change HOST env variable in conda env


I can change HOST env variable in conda base env (as shown below)

However, I can't change it in other conda env, in my case, it is foo env

~ ❯ echo $HOST                                           base
localhost
~ ❯ HOST=bar                                             base
~ ❯ echo $HOST                                           base
bar
~ ❯ conda activate foo                                   base
~ ❯ echo $HOST                                           foo
x86_64-conda-linux-gnu
~ ❯ HOST=bar                                             foo
~ ❯ echo $HOST                                           foo
x86_64-conda-linux-gnu

I also tried export but no luck:

~ ❯ export HOST=bar                                      foo
~ ❯ echo $HOST                                           foo
x86_64-conda-linux-gnu

I want to change the HOST variable in order to follow this solution.

I'm using zsh, however when I changed to bash, everything works fine.

So how can I change the HOST variable (in conda env foo in zsh)?


Solution

  • Workaround

    A workaround is to chain the command:

    ~ ❯ HOST=localhost && echo $HOST
    localhost
    

    This doesn't solve the issue that you can't persist the value, but at least you can launch stuff with it defined as you wish.

    Source of Issue

    I'm not familiar with details of zsh, but this specific behavior by the Conda environment seems caused by the gcc_linux-64 package, which includes these lines in its activation script:

    # fix prompt for zsh
    if [[ -n "${ZSH_NAME:-}" ]]; then
        autoload -Uz add-zsh-hook
    
        _conda_clang_precmd() {
          HOST="${CONDA_BACKUP_HOST}"
        }
        add-zsh-hook -Uz precmd _conda_clang_precmd
    
        _conda_clang_preexec() {
          HOST="${CONDA_TOOLCHAIN_HOST}"
        }
        add-zsh-hook -Uz preexec _conda_clang_preexec
    fi
    

    Roughly, seems like this compiler toolchain requires HOST to be set in a particular way, and for some reason zsh makes it problematic to change it. The code here appears to use hooks to set HOST right before executing a command, and then restores it from the backup right before showing the prompt again.

    I encourage you to file an issue on the pertinent feedstock, asking why they are doing this and if it could be changed.