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

     1  #!/usr/bin/env bash
     2  
     3  set -e
     4  
     5  GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
     6  source "${GIT_ROOT}/nix/scripts/source.sh"
     7  
     8  log() { echo "$@" 1>&2; }
     9  
    10  # helpers for getting related paths in Nix store
    11  getSources()   { nix-store --query --binding src "${1}"; }
    12  getOutputs()   { nix-store --query --outputs "${1}"; }
    13  getDrvFiles()  { nix-store --query --deriver "${1}"; }
    14  getReferrers() { nix-store --query --referrers "${1}"; }
    15  getRoots()     { nix-store --query --roots "${1}"; }
    16  
    17  findRelated() {
    18      path="${1}"
    19      found+=("${path}")
    20      if [[ "${path}" =~ .*.chroot ]]; then
    21          log " ! Chroot:     ${path}"
    22          return
    23      elif [[ "${path}" =~ .*.lock ]]; then
    24          log " ! Lock:       ${path}"
    25          return
    26      elif [[ "${path}" =~ .*status-mobile-shell.drv ]]; then
    27          echo -n "${path}"
    28          return
    29      fi
    30      log " ? Checking:   ${path}"
    31      drv=$(getDrvFiles "${path}")
    32      # if drv is unknown-deriver then path is a source
    33      if [[ "${drv}" == "unknown-deriver" ]]; then
    34          drv=$(getReferrers "${path}" | head -n1)
    35          src="${path}"
    36      elif [[ -f "${drv}" ]]; then
    37          src=$(getSources "${drv}")
    38      fi
    39      # empty paths means this is a source
    40      if [[ -z "${drv}" ]]; then
    41          echo -n "${src}"
    42          return
    43      fi
    44      if [[ $(getRoots "${drv}" | wc -l) -eq 0 ]]; then
    45          log " - Derivation: ${drv}"
    46          log " - Source:     ${src}"
    47          found+=("${drv}" "${src}")
    48      fi
    49  
    50      printf '%s\n' "${found[@]}"
    51  }
    52  
    53  # used to find things to delete based on a regex
    54  findByRegex() {
    55      regex="${1}"
    56  
    57      log "Searching by regex: '${regex}'"
    58      # search for matching entries in the store
    59      drvPaths=$(
    60        nix-store --gc --print-dead 2> /dev/null | grep -E "${regex}"
    61      )
    62  
    63      # list of store entries to delete
    64      declare -a found
    65      
    66      # for each entry find the source and derivation
    67      for mainPath in ${drvPaths}; do
    68          findRelated "${mainPath}"
    69      done
    70  }
    71  
    72  # used to find things to delete based on a given path
    73  findByResult() {
    74      mainPath="${1}"
    75      log "Searching by result: '${mainPath}'"
    76  
    77      # list of store entries to delete
    78      declare -a found
    79  
    80      findRelated "${mainPath}"
    81  }
    82  
    83  log "Cleanup of /nix/store..."
    84  
    85  # This is an optional CLI argument
    86  nixResultPath="${1}"
    87  if [[ -n "${nixResultPath}" ]]; then
    88      # if provided we can narrow down what to clean based on result path
    89      toDelete=$(findByResult "${nixResultPath}")
    90  else 
    91      # use regular expression that should match all status-mobile build artifacts
    92      toDelete=$(findByRegex '.*-status-(react|go)-(shell|source|build|patched-npm-gradle-modules).*')
    93  fi
    94  
    95  # remove duplicates and return
    96  toDelete=$(printf '%s\n' "${toDelete[@]}" | sort | uniq)
    97  
    98  log "Deleting..."
    99  nix-store --delete ${toDelete[@]}