-
Notifications
You must be signed in to change notification settings - Fork 0
/
comma.bash
42 lines (35 loc) · 1.19 KB
/
comma.bash
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
#!@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`.
sudo=""
if [ "$1" = "sudo" ]; then
shift
sudo="sudo"
fi
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
# "meta" packages that just wrap other programs (currently just cope).
mapfile -t derivations < <(@nix-index@/bin/nix-locate --top-level --minimal --whole-name /bin/"$program" | @toybox@/bin/grep -v '^cope.out$')
case ${#derivations[@]} in
0)
echo >&2 "Executable '$program' not found in database"
exit 1
;;
1)
derivation="${derivations[1]}"
;;
*)
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 $sudo "$@"