2019-07-15 16:34:33 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2020-02-19 13:15:28 +00:00
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
|
|
|
source "${GIT_ROOT}/nix/scripts/source.sh"
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
log() { echo "$@" 1>&2; }
|
2019-07-15 16:34:33 +00:00
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
# helpers for getting related paths in Nix store
|
2022-01-05 13:32:02 +00:00
|
|
|
getSources() { nix-store --query --binding src "${1}"; }
|
|
|
|
getOutputs() { nix-store --query --outputs "${1}"; }
|
|
|
|
getDrvFiles() { nix-store --query --deriver "${1}"; }
|
|
|
|
getReferrers() { nix-store --query --referrers "${1}"; }
|
|
|
|
getRoots() { nix-store --query --roots "${1}"; }
|
2019-07-15 16:34:33 +00:00
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
findRelated() {
|
2019-08-20 19:45:41 +00:00
|
|
|
path="${1}"
|
|
|
|
found+=("${path}")
|
2019-07-15 16:34:33 +00:00
|
|
|
if [[ "${path}" =~ .*.chroot ]]; then
|
2019-11-19 11:17:39 +00:00
|
|
|
log " ! Chroot: ${path}"
|
|
|
|
return
|
|
|
|
elif [[ "${path}" =~ .*.lock ]]; then
|
|
|
|
log " ! Lock: ${path}"
|
|
|
|
return
|
2022-07-17 12:37:46 +00:00
|
|
|
elif [[ "${path}" =~ .*status-mobile-shell.drv ]]; then
|
2019-11-19 11:17:39 +00:00
|
|
|
echo -n "${path}"
|
|
|
|
return
|
2019-07-15 16:34:33 +00:00
|
|
|
fi
|
2019-08-20 19:45:41 +00:00
|
|
|
log " ? Checking: ${path}"
|
2019-07-15 16:34:33 +00:00
|
|
|
drv=$(getDrvFiles "${path}")
|
|
|
|
# if drv is unknown-deriver then path is a source
|
|
|
|
if [[ "${drv}" == "unknown-deriver" ]]; then
|
2019-11-19 11:17:39 +00:00
|
|
|
drv=$(getReferrers "${path}" | head -n1)
|
2019-07-15 16:34:33 +00:00
|
|
|
src="${path}"
|
|
|
|
elif [[ -f "${drv}" ]]; then
|
|
|
|
src=$(getSources "${drv}")
|
|
|
|
fi
|
2019-11-19 11:17:39 +00:00
|
|
|
# empty paths means this is a source
|
|
|
|
if [[ -z "${drv}" ]]; then
|
|
|
|
echo -n "${src}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [[ $(getRoots "${drv}" | wc -l) -eq 0 ]]; then
|
2019-09-12 16:36:20 +00:00
|
|
|
log " - Derivation: ${drv}"
|
|
|
|
log " - Source: ${src}"
|
|
|
|
found+=("${drv}" "${src}")
|
|
|
|
fi
|
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
printf '%s\n' "${found[@]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# used to find things to delete based on a regex
|
2022-01-05 13:32:02 +00:00
|
|
|
findByRegex() {
|
2019-08-20 19:45:41 +00:00
|
|
|
regex="${1}"
|
|
|
|
|
|
|
|
log "Searching by regex: '${regex}'"
|
|
|
|
# search for matching entries in the store
|
|
|
|
drvPaths=$(
|
2019-09-12 16:36:20 +00:00
|
|
|
nix-store --gc --print-dead 2> /dev/null | grep -E "${regex}"
|
2019-08-20 19:45:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# list of store entries to delete
|
|
|
|
declare -a found
|
2024-06-04 12:32:09 +00:00
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
# 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
|
2022-01-05 13:32:02 +00:00
|
|
|
findByResult() {
|
2019-08-20 19:45:41 +00:00
|
|
|
mainPath="${1}"
|
|
|
|
log "Searching by result: '${mainPath}'"
|
|
|
|
|
|
|
|
# list of store entries to delete
|
|
|
|
declare -a found
|
|
|
|
|
|
|
|
findRelated "${mainPath}"
|
|
|
|
}
|
|
|
|
|
|
|
|
log "Cleanup of /nix/store..."
|
2019-07-15 16:34:33 +00:00
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
# 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}")
|
2024-06-04 12:32:09 +00:00
|
|
|
else
|
2022-07-17 12:37:46 +00:00
|
|
|
# use regular expression that should match all status-mobile build artifacts
|
2024-01-04 12:52:47 +00:00
|
|
|
toDelete=$(findByRegex '.*-status-(mobile|go)-(shell|source|build|patched-npm-gradle-modules).*')
|
2019-08-20 19:45:41 +00:00
|
|
|
fi
|
2019-07-15 16:34:33 +00:00
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
# remove duplicates and return
|
|
|
|
toDelete=$(printf '%s\n' "${toDelete[@]}" | sort | uniq)
|
2019-07-15 16:34:33 +00:00
|
|
|
|
2019-08-20 19:45:41 +00:00
|
|
|
log "Deleting..."
|
2020-02-19 13:15:28 +00:00
|
|
|
nix-store --delete ${toDelete[@]}
|