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

     1  #!/usr/bin/env bash
     2  # This script is a wrapper around nix-build with some niceties.
     3  set -e
     4  
     5  GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
     6  resultPath="${GIT_ROOT}/result/"
     7  source "${GIT_ROOT}/scripts/colors.sh"
     8  source "${GIT_ROOT}/nix/scripts/source.sh"
     9  
    10  # cleanup for artifacts created during builds
    11  cleanup() {
    12    # clear trapped signals
    13    trap - EXIT ERR INT QUIT
    14    # do the actual cleanup, ignore failure
    15    if ${GIT_ROOT}/nix/scripts/clean.sh "${nixResultPath}"; then
    16      echo -e "${GRN}Successful cleanup!${RST}"
    17    elif [[ -n "${JENKINS_URL}" ]]; then
    18      # in CI removing some paths can fail due to parallel builds
    19      echo -e "${YLW}Ignoring cleanup failure in CI.${RST}"
    20    else
    21      echo -e "${RED}Failed cleanup!${RST}"
    22      exit 1
    23    fi
    24  }
    25  
    26  # If you want to clean after every build set _NIX_CLEAN=true
    27  if [[ -n "${_NIX_CLEAN}" ]]; then
    28    trap cleanup EXIT ERR INT QUIT
    29  fi
    30  
    31  # build output will end up under /nix, we have to extract it
    32  extractResults() {
    33    local nixResultPath="$1"
    34    mkdir -p "${resultPath}"
    35    cp -vfr ${nixResultPath}/* "${resultPath}" | sed 's#'${PWD}'#.#'
    36    chmod -R u+w "${resultPath}"
    37  }
    38  
    39  TARGET="${1}"
    40  shift
    41  
    42  if [[ -z "${TARGET}" ]]; then
    43    echo -e "${RED}First argument is mandatory and has to specify the Nix attribute!${RST}"
    44    exit 1
    45  fi
    46  
    47  # Hack fix for missing Android SDK for aarch64 on Darwin. See systemOverride in `nix/pkgs.nix`.
    48  if [[ "${TARGET}" =~ ^(targets.status-go.mobile.android|targets.mobile.android.release)$ ]]; then
    49    os=$(uname -s | tr '[:upper:]' '[:lower:]')
    50    export NIXPKGS_SYSTEM_OVERRIDE="x86_64-${os}"
    51  fi
    52  
    53  # Some defaults flags, --pure could be optional in the future.
    54  # NOTE: The --keep-failed flag can be used for debugging issues.
    55  nixOpts=(
    56    "--pure"
    57    "--fallback"
    58    "--no-out-link"
    59    "--show-trace"
    60    "--attr" "${TARGET}"
    61  )
    62  
    63  # Save derivation from being garbage collected
    64  "${GIT_ROOT}/nix/scripts/gcroots.sh" "${TARGET}" "${@}"
    65  
    66  # Run the actual build
    67  echo -e "${GRN}Running:${RST} ${BLD}nix-build "${nixOpts[@]}" ${@}${RST}"
    68  nixResultPath=$(nix-build "${nixOpts[@]}" "${@}" shell.nix)
    69  
    70  echo -e "\n${YLW}Extracting result${RST}: ${BLD}${nixResultPath}${RST}"
    71  
    72  extractResults "${nixResultPath}"
    73  
    74  echo -e "\n${GRN}SUCCESS${RST}"