blob: 4a8bfb72afaf28555e5515aab23b5eaa65d539fb [file] [log] [blame]
Skyler Grey4e230892024-02-13 22:58:46 +00001#!/usr/bin/env bash
2if [ "$1" = "" ]
3then
4 read -ep "What command are you looking for?: " CMD
5else
6 CMD=$1
7fi
8
9LOCATION=$(command -V "$CMD" 2>&1 | sed -rn "s/.* (.*)$/\1/p")
10
11if [ "$LOCATION" = "found" ] # Not found
12then
13 echo "The command $CMD wasn't found"
14 exit 1
15fi
16
17if [ "$LOCATION" = "builtin" ] # Shell builtin
18then
19 echo "The command $CMD is a shell builtin"
20 exit 0
21fi
22
23RESOLVED_LOCATION=$(readlink -f "$LOCATION")
24
25if [[ ! "$RESOLVED_LOCATION" =~ ^\/nix\/store\/.*-.*-.*$ ]];
26then
27 echo "The command $CMD is at $RESOLVED_LOCATION"
28 exit 0
29fi
30
31PACKAGE=$(echo "$RESOLVED_LOCATION" | sed -rn "s/\/nix\/store\/[^-]*-(.*)-[^-]*/\1/p")
32
33SEARCHED=$(nix search "nixpkgs#$PACKAGE" 2>&1)
34
35if [ "$SEARCHED" = "error: no results for the given search term(s)!" ]
36then
37 echo "The command $CMD is at $RESOLVED_LOCATION"
38 exit 0
39fi
40
41echo "The command $CMD is from:"
42echo "$SEARCHED"
43echo "Which is available at $RESOLVED_LOCATION"