mirror of
https://github.com/nix-community/comma.git
synced 2024-10-26 05:58:05 +03:00
cd0f4852ea
it increases the closure size and limits the system we support in the future we can implement this better in the script
74 lines
1.6 KiB
Bash
Executable File
74 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# usage example:
|
|
# $ , yarn --help
|
|
# This finds a derivation providing a bin/yarn, and runs it with `nix run`.
|
|
# If there are multiple candidates, the user chooses one using `fzy`.
|
|
set -euo pipefail
|
|
|
|
picker=fzy
|
|
if [ -n "${COMMA_PICKER+1}" ]; then
|
|
picker="${COMMA_PICKER}"
|
|
fi
|
|
|
|
# Test that the picker exists
|
|
if ! [ -x "$(command -v "${picker}")" ]; then
|
|
>&2 echo "picker ${picker} is not found in PATH"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ $# -lt 1 ]]; then
|
|
>&2 echo "usage: , <program> [arguments]"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$1" == "--install" ]] || [[ "$1" == "-i" ]]; then
|
|
install=1
|
|
shift
|
|
else
|
|
install=""
|
|
fi
|
|
|
|
argv0=$1; shift
|
|
|
|
case "${argv0}" in
|
|
@OVERLAY_PACKAGES@)
|
|
attr="${argv0}"
|
|
;;
|
|
*)
|
|
attr="$(nix-locate --top-level --minimal --at-root --whole-name "/bin/${argv0}")"
|
|
if [[ "$(echo "${attr}" | wc -l)" -ne 1 ]]; then
|
|
attr="$(echo "${attr}" | "${picker}")"
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
if [[ -z $attr ]]; then
|
|
>&2 echo "no match"
|
|
exit 1
|
|
fi
|
|
|
|
# on flake based installations nixpkgs is specified via
|
|
# flake input and therefore NIX_PATH might be unset
|
|
if echo $NIX_PATH | grep -q "nixpkgs="; then
|
|
nixArgs=""
|
|
else
|
|
nixArgs="-I nixpkgs=${NIXPKGS}"
|
|
fi
|
|
|
|
if [[ -n $install ]]; then
|
|
nix-env $nixArgs -iA "nixpkgs.${attr%%.*}"
|
|
else
|
|
nix_version_greater_or_equal() {
|
|
local nix_version
|
|
nix_version=$(nix --version | cut -f3 -d ' ')
|
|
printf '%s\n%s' "$1" "$nix_version" | sort -C -V
|
|
}
|
|
|
|
if nix_version_greater_or_equal "2.4"; then
|
|
nix --extra-experimental-features 'nix-command flakes' shell "${NIXPKGS}#${attr}" -c "${argv0}" "$@"
|
|
else
|
|
nix run "nixpkgs.${attr}" -c "${argv0}" "$@"
|
|
fi
|
|
fi
|