github.com/status-im/status-go@v1.1.0/nix/scripts/shell.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # This script is used by the Makefile to have an implicit nix-shell.
     4  # The following environment variables modify the script behavior:
     5  # - TARGET: This attribute is passed via --attr to Nix, defining the scope.
     6  # - _NIX_PURE: This variable allows for making the shell pure with the use of --pure.
     7  #     Take note that this makes Nix tools like `nix-build` unavailable in the shell.
     8  # - _NIX_KEEP: This variable allows specifying which env vars to keep for Nix pure shell.
     9  
    10  GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
    11  source "${GIT_ROOT}/scripts/colors.sh"
    12  source "${GIT_ROOT}/nix/scripts/source.sh"
    13  
    14  export TERM=xterm # fix for colors
    15  shift # we remove the first -c from arguments
    16  
    17  if [[ -z "${TARGET}" ]]; then
    18      export TARGET="default"
    19      echo -e "${YLW}Missing TARGET, assuming default target.${RST} See nix/README.md for more details." 1>&2
    20  fi
    21  
    22  # Minimal shell with just Nix sourced, useful for `make nix-gc`.
    23  if [[ "${TARGET}" == "nix" ]]; then
    24      eval $@
    25      exit 0
    26  fi
    27  if [[ -n "${IN_NIX_SHELL}" ]] && [[ -n "${NIX_SHELL_TARGET}" ]]; then
    28      if [[ "${NIX_SHELL_TARGET}" == "${TARGET}" ]]; then
    29          echo -e "${YLW}Nix shell for TARGET=${TARGET} is already active.${RST}" >&2
    30          exit 0
    31      else
    32          # Nesting nix shells does not work due to how we detect already present shell.
    33          echo -e "${RED}Cannot nest Nix shells with different targets!${RST}" >&2
    34          exit 1
    35      fi
    36  fi
    37  
    38  entryPoint="default.nix"
    39  nixArgs=(
    40      "--show-trace"
    41      "--attr shells.${TARGET}"
    42  )
    43  
    44  # This variable allows specifying which env vars to keep for Nix pure shell
    45  # The separator is a colon
    46  if [[ -n "${_NIX_KEEP}" ]]; then
    47      nixArgs+=("--keep ${_NIX_KEEP//,/ --keep }")
    48  fi
    49  
    50  # Not all builds are ready to be run in a pure environment
    51  if [[ -n "${_NIX_PURE}" ]]; then
    52      nixArgs+=("--pure")
    53      pureDesc='pure '
    54  fi
    55  
    56  # Hack fix for missing Android SDK for aarch64 on Darwin. See systemOverride in `nix/pkgs.nix`.
    57  if [[ "${TARGET}" =~ ^(android-sdk|android|gradle|keytool|status-go)$ ]]; then
    58      os=$(uname -s | tr '[:upper:]' '[:lower:]')
    59      export NIXPKGS_SYSTEM_OVERRIDE="x86_64-${os}"
    60  fi
    61  
    62  echo -e "${GRN}Configuring ${pureDesc}Nix shell for target '${TARGET}'...${RST}" 1>&2
    63  
    64  # Save derivation from being garbage collected
    65  "${GIT_ROOT}/nix/scripts/gcroots.sh" "shells.${TARGET}"
    66  
    67  # ENTER_NIX_SHELL is the fake command used when `make shell` is run.
    68  # It is just a special string, not a variable, and a marker to not use `--run`.
    69  if [[ "${@}" == "ENTER_NIX_SHELL" ]]; then
    70      export NIX_SHELL_TARGET="${TARGET}"
    71      exec nix-shell ${nixArgs[@]} --keep NIX_SHELL_TARGET ${entryPoint}
    72  else
    73      exec nix-shell ${nixArgs[@]} --run "$@" ${entryPoint}
    74  fi