vibed improved gc
This commit is contained in:
parent
a79ca6a399
commit
9cb871275a
5 changed files with 208 additions and 4 deletions
101
util/dev_flake_gc.sh
Normal file
101
util/dev_flake_gc.sh
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
set -o pipefail
|
||||
|
||||
retention_days="30"
|
||||
scan_roots=(
|
||||
/
|
||||
/home
|
||||
/root
|
||||
/srv
|
||||
/opt
|
||||
/tmp
|
||||
/var/tmp
|
||||
/var/lib
|
||||
)
|
||||
|
||||
root_seen() {
|
||||
local candidate="$1"
|
||||
local seen
|
||||
|
||||
for seen in "${seen_roots[@]}"; do
|
||||
[ "$candidate" = "$seen" ] && return 0
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
has_recent_activity() {
|
||||
local path="$1"
|
||||
|
||||
find "$path" \
|
||||
-xdev \
|
||||
\( -name .git -o -name .hg -o -name .svn -o -name node_modules -o -name target \) -prune \
|
||||
-o -mindepth 1 -mtime "-$retention_days" -print -quit 2>/dev/null \
|
||||
| grep -q .
|
||||
}
|
||||
|
||||
is_nix_store_symlink() {
|
||||
local link="$1"
|
||||
local target
|
||||
|
||||
target="$(readlink "$link" 2>/dev/null || true)"
|
||||
case "$target" in
|
||||
/nix/store/*) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
cleanup_direnv() {
|
||||
local direnv_dir="$1"
|
||||
local project_dir
|
||||
|
||||
project_dir="$(dirname "$direnv_dir")"
|
||||
|
||||
if [ ! -e "$project_dir/flake.nix" ] \
|
||||
&& [ ! -e "$direnv_dir/flake-profile" ] \
|
||||
&& [ ! -e "$direnv_dir/gcroots" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if has_recent_activity "$project_dir"; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo "Removing stale nix-direnv cache: $direnv_dir"
|
||||
rm -rf --one-file-system "$direnv_dir"
|
||||
}
|
||||
|
||||
cleanup_result_link() {
|
||||
local link="$1"
|
||||
|
||||
if ! is_nix_store_symlink "$link"; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo "Removing stale Nix build result symlink: $link"
|
||||
rm -f "$link"
|
||||
}
|
||||
|
||||
seen_roots=()
|
||||
|
||||
for root in "${scan_roots[@]}"; do
|
||||
[ -d "$root" ] || continue
|
||||
root="$(readlink -f "$root")"
|
||||
root_seen "$root" && continue
|
||||
seen_roots+=("$root")
|
||||
|
||||
find "$root" \
|
||||
-xdev \
|
||||
\( -path /nix -o -path /proc -o -path /sys -o -path /dev -o -path /run -o -path /boot \) -prune \
|
||||
-o -type d -name .direnv -mtime "+$retention_days" -print0 2>/dev/null \
|
||||
| while IFS= read -r -d "" direnv_dir; do
|
||||
cleanup_direnv "$direnv_dir"
|
||||
done
|
||||
|
||||
find "$root" \
|
||||
-xdev \
|
||||
\( -path /nix -o -path /proc -o -path /sys -o -path /dev -o -path /run -o -path /boot \) -prune \
|
||||
-o -type l \( -name result -o -name "result-*" \) -mtime "+$retention_days" -print0 2>/dev/null \
|
||||
| while IFS= read -r -d "" link; do
|
||||
cleanup_result_link "$link"
|
||||
done
|
||||
done
|
||||
23
util/system_notify.sh
Normal file
23
util/system_notify.sh
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
set -o pipefail
|
||||
|
||||
target_user="${SYSTEM_NOTIFY_USER:-alisceon}"
|
||||
urgency="${1:-normal}"
|
||||
title="${2:-System task}"
|
||||
body="${3:-}"
|
||||
|
||||
uid="$(id -u "$target_user" 2>/dev/null || true)"
|
||||
[ -n "$uid" ] || exit 0
|
||||
|
||||
runtime_dir="/run/user/$uid"
|
||||
bus="$runtime_dir/bus"
|
||||
[ -S "$bus" ] || exit 0
|
||||
|
||||
runuser -u "$target_user" -- env \
|
||||
XDG_RUNTIME_DIR="$runtime_dir" \
|
||||
DBUS_SESSION_BUS_ADDRESS="unix:path=$bus" \
|
||||
notify-send \
|
||||
--app-name="NixOS maintenance" \
|
||||
--urgency="$urgency" \
|
||||
"$title" \
|
||||
"$body" \
|
||||
|| exit 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue