Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Created December 1, 2021 14:28
Show Gist options
  • Save offlinehacker/b1f0973f3442358ca39532c58485e2fb to your computer and use it in GitHub Desktop.
Save offlinehacker/b1f0973f3442358ca39532c58485e2fb to your computer and use it in GitHub Desktop.
Devcontainer env probe
#!/bin/bash
[[ $ENVRC_RUN != yes ]] && source /etc/envrc
# whether running via vscode env probe
if shopt -q login_shell && [[ "$BASH_EXECUTION_STRING" =~ $HOME/.vscode-server/bin/.*/node ]]; then
old_bash_path="$(which bash)"
if [ ! -z "${ENV_PRINT_COMMAND}" ]; then
cd "${WORKSPACE_DIR}"
eval "$(${ENV_PRINT_COMMAND})"
cd -
elif [ -f "${WORKSPACE_DIR}/.envrc" ]; then
eval "$(direnv exec ${WORKSPACE_DIR} direnv dump bash)"
elif [ -f "${WORKSPACE_DIR}/flake.nix" ]; then
cd "${WORKSPACE_DIR}"
eval "$(nix print-dev-env)"
cd -
elif [ -f "${WORKSPACE_DIR}/shell.nix" ]; then
eval "$(nix print-dev-env -f ${WORKSPACE_DIR}/shell.nix)"
fi
# remove bash from path if nix shell has overriden it,
# we don't want dev environment to pullute our shell
if [ "$(which bash)" != "${old_bash_path}" ]; then
PATH=:$PATH:
PATH=${PATH//:$(dirname $(which bash)):/:}
PATH=${PATH#:}; PATH=${PATH%:}
export PATH
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment