-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
64 lines (56 loc) · 1.5 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
set -euo pipefail
error() {
echo "$*" >&2
exit 1
}
confirm() {
read -rn1 -p "$1" confirm
echo
[[ "$confirm" = y ]]
}
: "${libexec_dir:="$(dirname "$(readlink -f "$0")")/../libexec/cache-gc"}"
[[ -r "$libexec_dir/add-registration-times.jq" ]] || error "Couldn't find registration time adder, are we installed correctly?"
usage() {
error "Usage: $0 [--days 90] [--delete] <cache-dir>"
}
delete=
cache_dir=
gc_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--delete)
delete=1
shift
;;
--days)
gc_args+=("$2")
shift 2
;;
*)
if [[ -z "$cache_dir" ]]; then
cache_dir="$1"
shift
else
usage
fi
;;
esac
done
[[ -n "$cache_dir" ]] || usage
paths_to_delete=$(
nix path-info --all --json --store file://"$cache_dir" --option extra-experimental-features "nix-command" |
jq -f "$libexec_dir/add-registration-times.jq" --slurpfile dates <(cd "$cache_dir"; echo *.narinfo | xargs stat -c '%Y %n' -- | jq -R) |
gc "${gc_args[@]}"
)
if [[ -z "$paths_to_delete" ]]; then
echo >&2 "Nothing to delete."
exit 0
fi
if [[ -n "$delete" ]] || confirm "Perform deletion? "; then
echo "$paths_to_delete" | (cd "$cache_dir"; xargs -P4 rm)
else
output=$(mktemp --tmpdir hydra-gc.XXXXXXX)
echo "$paths_to_delete" > "$output"
echo >&2 "Wrote paths to delete to $output."
fi