aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/,
blob: 52c83b1013fe52ba4c8063c25d33b73acf509e3e (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh -eu
# Idea condensed from https://github.com/Shopify/comma

program=$(basename "$0")
requires="$(command -V nix-index nix-locate nix-shell fzf | awk '{ print $1, $3 }' | column -t)"

help() {
printf \
"
Usage: %s [FLAGS]... [ARGUMENT]...

The wrapper $program shall search all nix
packages for a file and set up a nix shell for the
program that contains that file. The shell prints
all binaries in the package upon invocation.

Create and update the file database by running
the nix-index command. Warning: This command uses
up a large amount of memory.

nix-index --nixpkgs https://github.com/NixOS/nixpkgs/archive/master.tar.gz

  Command List:

   $program [ARGUMENT]...        Search for packages using file name argument.
   $program --help               Show this help menu.
   $program --check              Check dependencies.
" "$program";
}


[ "${1:-}" = "--check" ] && printf '%s\n' "$requires" && exit;

{ [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ] || [ "$#" = 0 ]; } && help && exit;

databasePath=${XDG_DATA_HOME:-~/.cache}/nix-index
nixLocate='nix-locate --db '"$databasePath"' --top-level --minimal --whole-name -- '"${1:-}"
attributes=$($nixLocate | while read -r attribute; do
  attributeName=${attribute%.*};
  printf '%s\n' "$attributeName";
done)

attribute=$(printf '%s' "$attributes" | fzf)
nix-shell --packages "$attribute" fish toybox --command \
'
  home=$TMPDIR/nix-shell-tmp-home.r2np9PHrby
  program=$(printf "%s" "$buildInputs" | cut --delimiter=" " --fields=1)
  path=$(IFS=" " && for buildInput in $buildInputs; do printf "%s/bin " "$buildInput"; done | sed "s/ /:/g")
  printf "\n";
  { [ -d "$program/bin" ] && ls "$program/bin"; } || printf "%s\n" "$program";
  printf "\n";
  tree -L 1 "$program";
  printf "\n";

  /usr/bin/env --ignore-environment /bin/sh -c \
  "
    export PATH=$path
    export DISPLAY=$DISPLAY
    export XAUTHORITY=$XAUTHORITY
    export TERM=$TERM
    export TERMINFO=$TERMINFO
    export TERMINFO_DIRS=$TERMINFO_DIRS
    export HOME=$home
    export fish_greeting=
    mkdir --parents $home
    cd || exit 1
    fish --init-command=\"function fish_prompt; printf '"'nix-shell@$attribute $ '"'; end\"
  "
';