-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomma.bash
More file actions
43 lines (36 loc) · 1.31 KB
/
comma.bash
File metadata and controls
43 lines (36 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!@bash@/bin/bash
# Special case: if the program is `sudo` we actually run the utility with
# `sudo` instead of looking for a package with `/bin/sudo`.
specials=()
while [ "$1" = "sudo" -o "$1" = "rlwrap" ]; do
specials+=("$1")
shift
done
if [ $# -lt 1 ]; then
echo >&2 "Usage: $0 [sudo] program [args...]"
exit 1
fi
program="$1"
# Find all packages that contain the binary we're trying to run, discarding:
# - packages that just wrap other programs (currently just cope)
# - "Unwrapped" packages (which are more useful in their wrapped version)
mapfile -t derivations < <(@nix-index@/bin/nix-locate --minimal --whole-name /bin/"$program" | @toybox@/bin/egrep -v '^cope\.out|.*-unwrapped\.out$')
case ${#derivations[@]} in
0)
echo >&2 "Executable '$program' not found in database"
exit 1
;;
1)
derivation="${derivations[0]}"
;;
*)
derivation="$(printf '%s\n' "${derivations[@]}" | @fzy@/bin/fzy)"
;;
esac
# If user presses CTRL-D then `give` will give empty string.
if [ -z "$derivation" ]; then
echo "No derivation selected. Aborting." >&2
exit 1
fi
# TODO: The official comma uses -f <nixpkgs> if it is defined in path. We should do that too, depending on the behavior of nix-index.
@nix@/bin/nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#"$derivation" --command "${specials[@]}" "$@"