Skip to content

Instantly share code, notes, and snippets.

@filterfish
Last active January 15, 2021 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filterfish/d138fa6cafd515399a54747976da7cc5 to your computer and use it in GitHub Desktop.
Save filterfish/d138fa6cafd515399a54747976da7cc5 to your computer and use it in GitHub Desktop.
Script to clean up the cabal store
#!/bin/zsh
setopt extended_glob
# Find the basename for every input line
function filter {
while read i; do print $i:t:s/\.conf/; done
}
# Return a the path of packages over than the second argument in seconds
function old-packages {
find $1 -mindepth 1 -maxdepth 1 -mtime +$2 -a \( \( -name incoming -prune -o -name package.db -prune \) -o -print \)
}
function list-versions {
print "\nThe following versions are available:"
print -l $store_base/*(n) | while read i; do print " ${i:t:s/ghc-//}"; done
}
function broken-dependencies {
local str="listed above, or because"
ghc-pkg check --package-db=$packagedb 2>&1 | grep -v haddock | sed -n "/^$str/,\$p" | grep -v $str
}
function remove-broken-dependencies {
}
[[ $#* < 1 ]] && { print "usage: $0:t <ghc version> <duration>"; exit 1 }
version=$1
duration=${2:-60}
store_base=$HOME/.cabal/store/
store=$store_base/ghc-$version
packagedb=$store/package.db
[[ ! -d $store ]] && { print "There is no store directory corresponding to version: $version"; list-versions; exit 1 }
tmp=$(mktemp -d)
rm -f $packagedb/package.cache{,.lock}
# Remove old packages
old-packages $store $duration | xargs -r rm -r
broken-dependencies | while read i; do rm -r $i-[0-9a-f]*; done
pkgs=$tmp/cabal-pkg-files
pkgs_db=$tmp/cabal-pkg-db
print -l $store/*(/on) | filter > $pkgs
print -l $packagedb/*(.on) | filter > $pkgs_db
join -v2 $pkgs $pkgs_db | xargs -r -i{} rm $packagedb/{}.conf
ghc-pkg recache --package-db=$packagedb
rm -fr $tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment