nix-search

This commit is contained in:
Peder Bergebakken Sundt 2025-03-21 20:40:44 +01:00
parent 3368c8149f
commit d3b2cb7fe3
3 changed files with 80 additions and 23 deletions
profiles
users/pbsds/home
default.nix
profiles/bashrc.d

@ -82,7 +82,6 @@
# nix utils
nix-output-monitor
nix-top
nix-search-cli # nix-search
nix-diff
comma
];

@ -106,7 +106,6 @@
nurl
manix
#comma # collides with nix-index-database
nix-search-cli # nix-search
devenv
nix-fast-build
nix-eval-jobs

@ -1,6 +1,6 @@
nix-search() (
set -euo pipefail
flake="${1:-nixpkgs}"
local flake="${1:-nixpkgs}"; shift ||:
trace(){
if [[ -t 2 ]]; then
@ -14,21 +14,25 @@ nix-search() (
command -v nix >/dev/null || die "ERROR: 'nix' not in PATH"
command -v jq >/dev/null || die "ERROR: 'jq' not in PATH"
command -v fzf >/dev/null || die "ERROR: 'fzf' not in PATH"
command -v bat >/dev/null || die "ERROR: 'fzf' not in PATH"
command -v bat >/dev/null || die "ERROR: 'bat' not in PATH"
command -v tr >/dev/null || die "ERROR: 'tr' not in PATH"
command -v sed >/dev/null || die "ERROR: 'sed' not in PATH"
command -v grep >/dev/null || die "ERROR: 'grep' not in PATH"
command -v sort >/dev/null || die "ERROR: 'sort' not in PATH"
if [[ -t 1 ]] && ! command -v gum >/dev/null; then
trace "WARNING: 'gum' not in PATH, no action menu will be provided"
fi
nixpkgs=$(nix eval "$flake"#path --json | jq . -r) # --raw hangs?
cache="${XDG_CACHE_HOME:-"$HOME/.cache"}/nix-search-sh"
# local nixpkgs=$(nix eval "$flake"#path --json | jq . -r) # --raw hangs?
local nixpkgs=$(nix flake metadata "$flake" --json | jq .path -r)
local cachedir="${XDG_CACHE_HOME:-"$HOME/.cache"}/nix-search-sh"
export storepath="$cache"/"${flake//"/"/--}"/path
export packages_json="$cache"/"${flake//"/"/--}"/packages.json
export packages_tsv="$cache"/"${flake//"/"/--}"/packages.tsv
export storepath="$cachedir"/"${flake//"/"/--}"/path
export packages_json="$cachedir"/"${flake//"/"/--}"/packages.json
export packages_tsv="$cachedir"/"${flake//"/"/--}"/packages.tsv
if [[ ! -f "$storepath" || "$nixpkgs" != "$(cat "$storepath")" ]]; then
mkdir -p "$cache"/"$flake"
mkdir -p "$cachedir"/"$flake"
# TODO: somehow inherit if user has configured allowUnfree and such in any file
trace "Indexing..."
@ -59,7 +63,7 @@ nix-search() (
show-package() {
local attrpath="$(head -n1 <<<"$1")"
printf "%s\n" "(toggle preview with 'alt-enter')"
printf "%s\n" "(toggle preview with 'alt-enter', scroll with '{shift,alt}-{up,down}')"
printf "\n"
grep -F -- $'--\t'"$attrpath"$'\t' "$packages_tsv" | cut -f4 | jq '. | [
@ -80,18 +84,73 @@ nix-search() (
' -r | bat --style=plain --language yaml --color=always
}
# attrpaths=$(
cut <"$packages_tsv" -f2-3 | tr '\n' '\0' | tr '\t' '\n' \
| fzf --read0 --print0 --gap 1 --reverse --bind 'ctrl-a:toggle-all' --bind 'alt-enter:toggle-preview' --multi --tiebreak=begin --preview "$(declare -f show-package); show-package {}" \
| tr '\n\0' '\t\n' | cut -f1
# )
local fzf_config=(
--reverse
--bind 'ctrl-a:toggle-all'
--bind 'alt-enter:toggle-preview'
# --bind 'shift-page-up:preview-page-up'
# --bind 'shift-page-down:preview-page-down'
# --bind 'ctrl-page-up:preview-page-up'
# --bind 'ctrl-page-down:preview-page-down'
--bind 'alt-up:preview-up'
--bind 'alt-down:preview-down'
--bind 'shift-up:preview-page-up'
--bind 'shift-down:preview-page-down'
)
# if [[ "$(wc -l <<<"$attrpaths")" -eq 1 ]]; then
# trace " nix profile install $flake#$attrpaths"
# trace " nix run $flake#$attrpaths"
# trace " nix shell $flake#$attrpaths^*"
# trace " nix-shell '<$flake>' -A $flake#$attrpaths"
# fi
local -a attributes
readarray -d $'\n' -t attributes < <(
cut <"$packages_tsv" -f2-3 | tr '\n' '\0' | tr '\t' '\n' \
| fzf --read0 --print0 --gap 1 --multi --tiebreak=begin "${fzf_config[@]}" --preview "$(declare -f show-package); show-package {}" \
| tr '\n\0' '\t\n' | cut -f1
)
# cat <<<"$attrpaths"
if [[ "${#attributes[@]}" -gt 0 ]]; then
if [[ ! -t 1 ]] || ! command -v gum >/dev/null; then
printf "%s\n" "${attributes[@]}"
else
local -a menu=(
"print : 'printf' the packages"
"print2 : 'printf' the packages with flake prefixed"
"build : 'nix-build' the packages"
"install : 'nix profile install' the packages"
# "enter : 'nix-shell' the packages and modify this environment"
"shell : 'nix shell ...' the packages"
"shell2 : 'nix shell ...^*' the packages"
"shell3 : 'nix-shell ...' the packages"
)
case "$(gum choose "${menu[@]}" | cut -d' ' -f1)" in
print)
printf "%s\n" "${attributes[@]}"
;;
print2)
printf "%s\n" "${attributes[@]/#/"$flake#"}"
;;
build)
if command -v nom-build >/dev/null; then
eval "nom-build '$nixpkgs' ${attributes[*]/#/-A }"
else
eval "nix-build '$nixpkgs' ${attributes[*]/#/-A }"
fi
;;
install)
nix profile install "${attributes[@]/#/"$flake#"}"
;;
shell)
nix shell "${attributes[@]/#/"$flake#"}"
;;
shell2)
local -a attributes_all=("${attributes[@]/%/'^*'}")
nix shell "${attributes_all[@]/#/"$flake#"}"
;;
shell3)
eval "nix-shell '$nixpkgs' ${attributes[*]/#/-A }"
;;
*)
die "unknown option"
exit 1
;;
esac
fi
fi
)