nix-fd
This commit is contained in:
64
users/pbsds/home/profiles/bashrc.d/nix-fd.sh
Normal file
64
users/pbsds/home/profiles/bashrc.d/nix-fd.sh
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
_nix_fd() (
|
||||||
|
set -euo pipefail
|
||||||
|
declare nix="${1:-}"; shift
|
||||||
|
|
||||||
|
if [[ $# -lt 2 ]]; then
|
||||||
|
printf >&2 "%s\n" "ERROR: not enough args"
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
|
||||||
|
target_nix=0
|
||||||
|
in_fd_nargs=false
|
||||||
|
fd_pattern=()
|
||||||
|
fd_flags=()
|
||||||
|
nix_args=()
|
||||||
|
nix_flags=()
|
||||||
|
|
||||||
|
# set -x
|
||||||
|
for arg in "$@"; do
|
||||||
|
# separator
|
||||||
|
if [[ "$arg" = "--" ]]; then
|
||||||
|
if "$in_fd_nargs"; then
|
||||||
|
in_fd_nargs=false
|
||||||
|
fi
|
||||||
|
(( target_nix ^= 1 )) ||:
|
||||||
|
|
||||||
|
# flag
|
||||||
|
elif [[ "$arg" =~ ^- ]]; then
|
||||||
|
if [[ $target_nix -eq 0 ]] || $in_fd_nargs; then
|
||||||
|
fd_flags+=("$arg")
|
||||||
|
if [[ "$arg" = "-x" ]]; then
|
||||||
|
in_fd_nargs=true
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
nix_flags+=("$arg")
|
||||||
|
fi
|
||||||
|
|
||||||
|
# arg
|
||||||
|
elif [[ ! -v fd_pattern[0] ]]; then
|
||||||
|
fd_pattern+=("$arg")
|
||||||
|
elif $in_fd_nargs; then
|
||||||
|
fd_flags+=("$arg")
|
||||||
|
elif [[ $target_nix -ne 0 ]]; then
|
||||||
|
nix_flags+=("$arg")
|
||||||
|
elif [[ "${arg}" =~ ^.*[#:/].*$ ]]; then
|
||||||
|
nix_args+=("$arg")
|
||||||
|
else
|
||||||
|
nix_args+=("nixpkgs#$arg")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# echo + "$nix" build "${nix_args[@]}" "${nix_flags[@]}"
|
||||||
|
# echo + fd "${fd_pattern[@]}" ... "${fd_flags[@]}"
|
||||||
|
# return
|
||||||
|
|
||||||
|
readarray -d $'\n' -t storepaths < <(
|
||||||
|
"$nix" build "${nix_args[@]}" "${nix_flags[@]}" --print-out-paths --no-link
|
||||||
|
)
|
||||||
|
fd "${fd_pattern[@]}" "${storepaths[@]}" "${fd_flags[@]}"
|
||||||
|
)
|
||||||
|
|
||||||
|
nix-fd() { _nix_fd "nix" "$@"; }
|
||||||
|
if command -v nom >/dev/null; then
|
||||||
|
nom-fd() { _nix_fd "nom" "$@"; }
|
||||||
|
fi
|
Reference in New Issue
Block a user