Last active
July 4, 2022 15:22
-
-
Save olejorgenb/0c3bafa3c7b63d1a2f83ee13582de7b9 to your computer and use it in GitHub Desktop.
nix-package-search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/usr/env bash | |
# Originaly from: https://www.reddit.com/r/NixOS/comments/5yxt45/simple_nix_package_search/ | |
nq () { | |
local CACHE="$HOME/.cache/nq-cache" | |
if ! ( [ -e $CACHE ] && [ $(stat -c %Y $CACHE) -gt $(( $(date +%s) - 3600*24*7 )) ] ); then | |
# read gotcha.. can't pipe to read (as usual zsh beats bash) | |
read x MEM_AVAIL_KB UNIT < <(grep MemAvailable: /proc/meminfo) | |
if [[ $UNIT != "kB" || $MEM_AVAIL_KB -lt 1000000 ]]; then | |
# nix-env can easily use up to 1GB during -qa | |
echo "Refusing to update cache due to low memory ($((MEM_AVAIL_KB/1000))MB)" 1>&2 | |
echo "Searching in old cache" 1>&2 | |
else | |
echo "update cache" 1>&2 | |
nice nix-env -qa --file "<nixpkgs>" --json > "$CACHE" | |
fi | |
fi | |
jq -r 'to_entries | .[] | .key + "|" + .value.meta.description' < "$CACHE" | | |
{ | |
if [ $# -gt 0 ]; then | |
# double grep because coloring breaks column's char count | |
# $* so that we include spaces (could do .* instead?) | |
grep -i "$*" | column -t -s "|" | grep --color=always -i "$*" | |
else | |
column -t -s "|" | |
fi | |
} | |
} | |
nq "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I updated this a bit at https://github.com/larkery/zsh/blob/master/functions/nq if you're still using it, mainly so you can say
nq -A perlPackages
or whatever to look in one of the subsets that otherwise is a bit opaque.