2019-04-05 17:22:49 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2019-04-09 15:14:32 +00:00
|
|
|
#
|
|
|
|
# This script is used by the Makefile to have an implicit nix-shell.
|
2019-06-04 16:50:29 +00:00
|
|
|
# The following environment variables modify the script behavior:
|
2020-04-26 12:40:06 +00:00
|
|
|
# - TARGET: This attribute is passed via --attr to Nix, defining the scope.
|
2019-07-15 16:34:33 +00:00
|
|
|
# - _NIX_PURE: This variable allows for making the shell pure with the use of --pure.
|
|
|
|
# Take note that this makes Nix tools like `nix-build` unavailable in the shell.
|
2019-11-15 15:04:36 +00:00
|
|
|
# - _NIX_KEEP: This variable allows specifying which env vars to keep for Nix pure shell.
|
2019-04-09 15:14:32 +00:00
|
|
|
|
2020-02-19 13:15:28 +00:00
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
|
|
|
source "${GIT_ROOT}/scripts/colors.sh"
|
|
|
|
source "${GIT_ROOT}/nix/scripts/source.sh"
|
2019-04-05 17:22:49 +00:00
|
|
|
|
|
|
|
export TERM=xterm # fix for colors
|
|
|
|
shift # we remove the first -c from arguments
|
2019-04-09 15:14:32 +00:00
|
|
|
|
2020-04-26 12:40:06 +00:00
|
|
|
if [[ -z "${TARGET}" ]]; then
|
2021-11-05 12:59:33 +00:00
|
|
|
export TARGET="default"
|
2020-04-26 12:40:06 +00:00
|
|
|
echo -e "${YLW}Missing TARGET, assuming default target.${RST} See nix/README.md for more details." 1>&2
|
2019-07-16 20:34:04 +00:00
|
|
|
fi
|
2021-11-05 12:59:33 +00:00
|
|
|
|
2020-06-19 13:43:58 +00:00
|
|
|
# Minimal shell with just Nix sourced, useful for `make nix-gc`.
|
|
|
|
if [[ "${TARGET}" == "nix" ]]; then
|
|
|
|
eval $@
|
2021-11-05 12:59:33 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
if [[ -n "${IN_NIX_SHELL}" ]] && [[ -n "${NIX_SHELL_TARGET}" ]]; then
|
|
|
|
if [[ "${NIX_SHELL_TARGET}" == "${TARGET}" ]]; then
|
|
|
|
echo -e "${YLW}Nix shell for TARGET=${TARGET} is already active.${RST}" >&2
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
# Nesting nix shells does not work due to how we detect already present shell.
|
|
|
|
echo -e "${RED}Cannot nest Nix shells with different targets!${RST}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-06-19 13:43:58 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-26 12:40:06 +00:00
|
|
|
entryPoint="default.nix"
|
2021-11-05 12:59:33 +00:00
|
|
|
nixArgs=(
|
|
|
|
"--show-trace"
|
|
|
|
"--attr shells.${TARGET}"
|
|
|
|
)
|
2020-04-26 12:40:06 +00:00
|
|
|
|
2020-01-13 14:28:40 +00:00
|
|
|
config=''
|
2021-11-05 12:59:33 +00:00
|
|
|
if [[ -n "${STATUS_GO_SRC_OVERRIDE}" ]]; then
|
|
|
|
config+="status-im.status-go.src-override=\"${STATUS_GO_SRC_OVERRIDE}\";"
|
2020-01-21 07:19:49 +00:00
|
|
|
fi
|
2020-01-13 14:28:40 +00:00
|
|
|
config+="status-im.build-type=\"${BUILD_TYPE}\";"
|
2020-01-08 14:41:07 +00:00
|
|
|
|
2021-11-05 12:59:33 +00:00
|
|
|
if [[ -n "$config" ]]; then
|
|
|
|
nixArgs+=("--arg config {$config}")
|
2019-11-15 15:04:36 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-26 12:40:06 +00:00
|
|
|
# This variable allows specifying which env vars to keep for Nix pure shell
|
|
|
|
# The separator is a colon
|
|
|
|
if [[ -n "${_NIX_KEEP}" ]]; then
|
2021-11-05 12:59:33 +00:00
|
|
|
nixArgs+=("--keep ${_NIX_KEEP//,/ --keep }")
|
2019-07-16 20:34:04 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-26 12:40:06 +00:00
|
|
|
# Not all builds are ready to be run in a pure environment
|
|
|
|
if [[ -n "${_NIX_PURE}" ]]; then
|
2021-11-05 12:59:33 +00:00
|
|
|
nixArgs+=("--pure")
|
|
|
|
pureDesc='pure '
|
2020-04-26 12:40:06 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "${GRN}Configuring ${pureDesc}Nix shell for target '${TARGET}'...${RST}" 1>&2
|
|
|
|
|
2020-06-19 13:43:58 +00:00
|
|
|
# Save derivation from being garbage collected
|
|
|
|
${GIT_ROOT}/nix/scripts/gcroots.sh "shells.${TARGET}"
|
|
|
|
|
2019-07-15 16:34:33 +00:00
|
|
|
# ENTER_NIX_SHELL is the fake command used when `make shell` is run.
|
|
|
|
# It is just a special string, not a variable, and a marker to not use `--run`.
|
2020-04-26 12:40:06 +00:00
|
|
|
if [[ "${@}" == "ENTER_NIX_SHELL" ]]; then
|
2021-11-05 12:59:33 +00:00
|
|
|
export NIX_SHELL_TARGET="${TARGET}"
|
|
|
|
exec nix-shell ${nixArgs[@]} --keep NIX_SHELL_TARGET ${entryPoint}
|
2019-07-16 20:34:04 +00:00
|
|
|
else
|
2021-11-05 12:59:33 +00:00
|
|
|
exec nix-shell ${nixArgs[@]} --run "$@" ${entryPoint}
|
2019-04-05 17:22:49 +00:00
|
|
|
fi
|