<commit_before>#!/usr/bin/env bash

set -e

function log() { echo "$@" 1>&2; }

# helpers for getting related paths in Nix store
function getSources()   { nix-store --query --binding src "${1}"; }
function getOutputs()   { nix-store --query --outputs "${1}"; }
function getDrvFiles()  { nix-store --query --deriver "${1}"; }
function getReferrers() { nix-store --query --referrers "${1}"; }

function findRelated() {
    path="${1}"
    found+=("${path}")
    if [[ "${path}" =~ .*.chroot ]]; then
        log " ! Chroot:    ${path}"
        continue
    fi
    log " ? Checking:   ${path}"
    drv=$(getDrvFiles "${path}")
    # if drv is unknown-deriver then path is a source
    if [[ "${drv}" == "unknown-deriver" ]]; then
        drv=$(getReferrers "${path}")
        src="${path}"
    elif [[ -f "${drv}" ]]; then
        src=$(getSources "${drv}")
    fi
    log " - Derivation: ${drv}"
    log " - Source:     ${src}"
    
    found+=("${drv}" "${src}")
    printf '%s\n' "${found[@]}"
}

# used to find things to delete based on a regex
function findByRegex() {
    regex="${1}"

    log "Searching by regex: '${regex}'"
    # search for matching entries in the store
    drvPaths=$(
      find /nix/store -maxdepth 1 -type d -regextype egrep -regex "${regex}"
    )

    # list of store entries to delete
    declare -a found
    
    # for each entry find the source and derivation
    for mainPath in ${drvPaths}; do
        findRelated "${mainPath}"
    done
}

# used to find things to delete based on a given path
function findByResult() {
    mainPath="${1}"
    log "Searching by result: '${mainPath}'"

    # list of store entries to delete
    declare -a found

    findRelated "${mainPath}"
}

log "Cleanup of /nix/store..."

# This is an optional CLI argument
nixResultPath="${1}"
if [[ -n "${nixResultPath}" ]]; then
    # if provided we can narrow down what to clean based on result path
    toDelete=$(findByResult "${nixResultPath}")
else 
    # use regular expression that should match all status-react build artifacts
    toDelete=$(findByRegex '.*-status-react-(shell|source|build).*')
fi

# remove duplicates and return
toDelete=$(printf '%s\n' "${toDelete[@]}" | sort | uniq)

log "Deleting..."
nix-store --ignore-liveness --delete ${toDelete[@]}<commit_msg>Make nix-clean also delete `android-gradle-and-npm-modules` derivations<commit_after>#!/usr/bin/env bash

set -e

function log() { echo "$@" 1>&2; }

# helpers for getting related paths in Nix store
function getSources()   { nix-store --query --binding src "${1}"; }
function getOutputs()   { nix-store --query --outputs "${1}"; }
function getDrvFiles()  { nix-store --query --deriver "${1}"; }
function getReferrers() { nix-store --query --referrers "${1}"; }

function findRelated() {
    path="${1}"
    found+=("${path}")
    if [[ "${path}" =~ .*.chroot ]]; then
        log " ! Chroot:    ${path}"
        continue
    fi
    log " ? Checking:   ${path}"
    drv=$(getDrvFiles "${path}")
    # if drv is unknown-deriver then path is a source
    if [[ "${drv}" == "unknown-deriver" ]]; then
        drv=$(getReferrers "${path}")
        src="${path}"
    elif [[ -f "${drv}" ]]; then
        src=$(getSources "${drv}")
    fi
    log " - Derivation: ${drv}"
    log " - Source:     ${src}"
    
    found+=("${drv}" "${src}")
    printf '%s\n' "${found[@]}"
}

# used to find things to delete based on a regex
function findByRegex() {
    regex="${1}"

    log "Searching by regex: '${regex}'"
    # search for matching entries in the store
    drvPaths=$(
      find /nix/store -maxdepth 1 -type d -regextype egrep -regex "${regex}"
    )

    # list of store entries to delete
    declare -a found
    
    # for each entry find the source and derivation
    for mainPath in ${drvPaths}; do
        findRelated "${mainPath}"
    done
}

# used to find things to delete based on a given path
function findByResult() {
    mainPath="${1}"
    log "Searching by result: '${mainPath}'"

    # list of store entries to delete
    declare -a found

    findRelated "${mainPath}"
}

log "Cleanup of /nix/store..."

# This is an optional CLI argument
nixResultPath="${1}"
if [[ -n "${nixResultPath}" ]]; then
    # if provided we can narrow down what to clean based on result path
    toDelete=$(findByResult "${nixResultPath}")
else 
    # use regular expression that should match all status-react build artifacts
    toDelete=$(findByRegex '.*-status-react-(shell|source|build).*')
fi

# remove duplicates and return
toDelete=$(printf '%s\n' "${toDelete[@]}" | sort | uniq)

log "Deleting..."
nix-store --ignore-liveness --delete ${toDelete[@]}

log "Deleting dead Android Gradle and NPM derivations..."
nix-store --gc --print-dead | grep -E 'android-gradle-and-npm-modules(\.drv)?$' | xargs nix-store --delete
{'fraction_non_alphanumeric': 0.17125, 'fraction_numerical': 0.004583333333333333, 'mean_word_length': 2.9743377483443707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5671923', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1495, 'n_words': 507}