programs/bash.nix (view raw)
1{ config
2, pkgs
3, ...
4}:
5let
6 awk = "${pkgs.gawk}/bin/awk";
7 fzy = "${pkgs.fzy}/bin/fzy";
8in
9{
10 programs.bash = {
11 enable = true;
12 historyControl = [ "erasedups" "ignoredups" "ignorespace" ];
13 historyFile = "\$HOME/.bash_history";
14 historyFileSize = 40000;
15 historyIgnore = [ "ls" "exit" "kill" ];
16 historySize = 40000;
17
18 shellAliases = {
19 o = "xdg-open";
20 gc = "git commit -v -S";
21 gst = "git status --short";
22 ga = "git add";
23 gd = "git diff --minimal";
24 gl = "git log --oneline --decorate --graph";
25 k = "kubectl";
26 n = "z";
27 };
28
29 shellOptions = [
30 "histappend"
31 "autocd"
32 "globstar"
33 "checkwinsize"
34 "cdspell"
35 "dirspell"
36 "expand_aliases"
37 "dotglob"
38 "gnu_errfmt"
39 "histreedit"
40 "nocasematch"
41 ];
42
43 sessionVariables = {
44
45 TERM = "xterm-256color-italic";
46 EDITOR = "nvim";
47 MANPAGER = "nvim +Man!";
48 PATH = "/etc/profiles/per-user/icy/bin:$PATH:$HOME/go/bin:$HOME/bin";
49 CLICOLOR = "1";
50 LANG = "en_US.UTF-8";
51
52 };
53
54 # TODO: nixify this
55 bashrcExtra = ''
56 refresh_tmux() {
57 tmux refresh-client -S
58 }
59
60 PROMPT_COMMAND=refresh_tmux
61 PS1="\n\001\002▲\001\002 ";
62 PS2="> "
63
64 ggp() {
65 if [[ "$1" == "-f" ]]; then
66 git push "$(git remote show)" -f "$(git branch --show-current)"
67 else
68 git push "$(git remote show)" "$(git branch --show-current)"
69 fi
70 }
71
72 gpl() {
73 if [[ "$1" != "" ]]; then
74 branch="$1"
75 else
76 branch="$(git branch --show-current)"
77 fi
78 git pull -r "$(git remote show)" "$branch"
79 }
80
81 gco() {
82 [[ "$1" == "" ]] && return 1
83
84 git rev-parse --verify "$1" &> /dev/null
85 if [ $? -eq 0 ]; then
86 git checkout "$1"
87 else
88 git checkout -b "$1"
89 fi
90 }
91
92 gaf() {
93 git status --short | grep "^ M\|^ D\|^\?\?" | fzy | awk '{ print $2 }' | xargs git add
94 }
95 '';
96
97 initExtra = ''
98 [[ $(HISTTIMEFORMAT="" builtin history 1) =~ [[:digit:]]+ ]]
99 __fzy_history__() {
100 ch="$(fc -rl 1 | ${awk} -F'\t' '{print $2}' | ${awk} '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' | ${fzy})"
101 ch="$(echo "$ch" | ${awk} '{$1=$1;print}')"
102 READLINE_LINE=''${ch#*$'\t'}
103 if [[ -z "$READLINE_POINT" ]]; then
104 echo "$READLINE_LINE"
105 else
106 READLINE_POINT=0x7fffffff
107 fi
108 }
109
110 bind -m emacs-standard -x '"\C-r": __fzy_history__'
111
112 complete -cf doas
113
114 if command -v kubectl &> /dev/null; then
115 source <(kubectl completion bash)
116 complete -F __start_kubectl k
117 fi
118 '';
119
120 };
121}