]> Git — Sourcephile - julm/julm-nix.git/blob - home-manager/profiles/bash.nix
bash: add ZFS utilities
[julm/julm-nix.git] / home-manager / profiles / bash.nix
1 { pkgs, lib, ... }:
2 with lib;
3 {
4 programs.bash = {
5 enable = mkDefault true;
6 shellAliases = {
7 afk = "xset s activate dpms force off";
8 black-on-white = "echo -e '\\033]11;black\\007\\033]10;white\\007'";
9 c = "bat";
10 cl = "clear";
11 eic = "edit-in-commit";
12 eigg = "edit-in-git-grep";
13 emacs = "emacsclient --create-frame";
14 g = "git";
15 gg = "git grep";
16 grep = "grep --color";
17 j = "sudo journalctl -u";
18 jb = "sudo journalctl -b";
19 ju = "sudo journalctl --user -u";
20 l = "ls -alh";
21 ll = "ls -al";
22 ls = "ls --color=tty";
23 md-toc = "grep '^#\\+' --color";
24 mem = "ps -e -orss=,user=,args= | sort -b -k1,1n";
25 mem-top = "smem --sort rss --autosize";
26 mpl = "mplayer";
27 nf = "sudo nft list ruleset | less";
28 nix-du-svg = "nix-du | dot -Tsvg >nix-du.svg";
29 nixos-clean = "sudo nix-collect-garbage -d";
30 nixos-history = "sudo nix-env --list-generations --profile /nix/var/nix/profiles/system";
31 nixos-rollback = "sudo nixos-rebuild switch --rollback";
32 pass-gen = "tr -d -C A-Za-z0-9_- </dev/urandom | head -c";
33 r = "reset";
34 rot13 = "tr A-Za-z N-ZA-Mn-za-m";
35 rot135 = "tr A-Za-z0-9 N-ZA-Mn-za-m5-90-4";
36 rsync = "rsync --no-inc-recursive --info=progress2 --inplace --partial";
37 s = "sudo systemctl";
38 st = "sudo systemctl status";
39 t = "tmux";
40 t0 = "tmux new -t 0";
41 t1 = "tmux new -t 1";
42 t2 = "tmux new -t 2";
43 theme-black-on-white = "echo -e '\\033]10;black\\007\\033]11;white\\007'";
44 theme-white-on-black = "echo -e '\\033]10;white\\007\\033]11;black\\007'";
45 u = "systemctl --user";
46 ut = "systemctl --user status";
47 w1 = "watch --color --differences --interval 1";
48 w5 = "watch --color --differences --interval 5";
49 w10 = "watch --color --differences --interval 10";
50 w = "watch --color --differences";
51 watch = "watch --color --differences";
52 ze = "sudo zpool export";
53 zfs-umount = "zfs-unmount";
54 zi = "sudo zpool import";
55 zl = "zfs list";
56 zlb = "zfs list -t bookmarks";
57 zls = "zfs list -t snap";
58 zm = "zfs-mount";
59 zu = "zfs-unmount";
60 };
61 sessionVariables = {
62 HISTCONTROL = "erasedups:ignorespace";
63 HISTSIZE = "42000";
64 PS1 = ''\[\033[1;32m\]\[\e]0;\u@\h: \w\a\]\W\[\033[0m\] \$(e=\$?; if [ \$e != 0 ]; then echo '\[\e[0;91m\]'\$e'\[\e[0m\]'; fi)\$ '';
65 };
66 initExtra = ''
67 # Alias completion
68 . ${pkgs.complete-alias}/bin/complete_alias
69 complete -F _complete_alias "''${!BASH_ALIASES[@]}"
70
71 shopt -s globstar
72 shopt -s histappend
73 shopt -s histreedit
74 shopt -s histverify
75 # Disable ctrl-s/ctrl-q flow control
76 stty -ixon
77
78 ffmpeg-audio () {
79 for i in "$@"; do
80 ffmpeg -i "$i" -vn -map 0:a -acodec copy "''${i%.*}".audio
81 done
82 }
83 ffmpeg-opus () {
84 for i in "$@"; do
85 ffmpeg -i "$i" -vn -map 0:a -c:a libopus -b:a 64k -application voip "''${i%.*}".opus
86 done
87 }
88 ibm-fan () {
89 if [ $# -gt 0 ]
90 then sudo tee /proc/acpi/ibm/fan <<<"level $1"
91 else grep '^\(level\|speed\):' /proc/acpi/ibm/fan
92 fi
93 acpi -t
94 }
95 mkcd () {
96 mkdir -p "$1" &&
97 cd "$1"
98 }
99 mkpass () {
100 tr -d -C 'A-Za-z0-9' </dev/urandom | head -c 25 | xclip
101 }
102 opusenc-voice () {
103 find "$@" -depth -type f -print0 | sort -n -z |
104 xargs -0 -P "$(lscpu --online -p | grep -v "#" | wc -l)" -I {} bash -c '
105 test -e "''${0%.*}".opus ||
106 nice -n 19 ffmpeg -y -i "$0" -map 0:a -b:a 32k -application voip "''${0%.*}".opus
107 ' {} \;
108 }
109 smartctl-tbw () {
110 device=''${1:-/dev/sda}
111 sudo smartctl -A $device |
112 { awk '
113 $0 ~ /Power_On_Hours/ { poh=$10; printf "%s / %d hours / %d days / %.2f years\n", $2, $10, $10 / 24, $10 / 24 / 365.25 }
114 $0 ~ /Total_LBAs_Written/ {
115 lbas = $10;
116 bytes = $10 * 512;
117 mb = bytes / 1024^2;
118 gb = bytes / 1024^3;
119 tb = bytes / 1024^4;
120 printf "%s / %s / %d mb / %.1f gb / %.3f tb\n", $2, $10, mb, gb, tb
121 printf "mean writes per hour: / %.2f", mb/poh
122 }
123 $0 ~ /Airflow_Temperature_Cel/ { print $2 " / " $10}
124 $0 ~ /Wear_Leveling_Count/ { printf "%s / %d (%% health)\n", $2, int($4) }
125 '; echo; } |
126 sed -e 's:/:@:' |
127 sed -e "s\$^\$$device @ \$" |
128 column -ts@
129 }
130 stress-mem() { fac="$1"; stress-ng --vm 1 --vm-keep --vm-bytes $(awk "/MemAvailable/{ printf \"%d\n\", \$2 * $fac; }" </proc/meminfo)k; }
131 sysenter() { srv="$1"; shift; nsenter -a -t "$(systemctl show --property MainPID --value "$srv")" "$@"; }
132 systrace() { srv="$1"; shift; strace -f -p "$(systemctl show --property MainPID --value "$srv")" "$@"; }
133 swaplist () {
134 lastpid=
135 swap=0
136 sudo grep -H '^Swap:' /proc/*/smaps 2>/dev/null |
137 while IFS=: read -r file x size x
138 do
139 pid=''${file#/proc/}
140 pid=''${pid%/smaps}
141 size=''${size% kB}
142 size=''${size##* }
143 if test "$pid" = "$lastpid"
144 then swap=$(( swap + size ))
145 else
146 if test "$swap" -gt 0
147 then printf "%u pid=%u cmd=%s\n" "$swap" "$lastpid" "$(tr '\000' ' ' </proc/"$lastpid"/cmdline)"
148 fi
149 if test "$pid" = self
150 then break
151 else
152 lastpid=$pid
153 swap=$size
154 fi
155 fi
156 done |
157 sort -nk1,1
158 }
159 edit-in-commit () { $EDITOR $(git diff-tree --no-commit-id --name-only -r "$@"); }
160 edit-in-git-grep () { $EDITOR $(git grep --name-only --recursive "$@"); }
161
162 # Recursively mount not-mounted dataset,
163 # loading their keys if needed.
164 zfs-mount () {
165 local d
166 local -
167 set -e
168 for d in $(zfs list -rH -o name "$@"); do
169 mountpoint /mnt/"$d" 2>/dev/null ||
170 sudo zfs mount -l "$d"
171 done
172 }
173 # Recursively unmount dataset,
174 # unloading their keys.
175 zfs-unmount () { sudo zfs unmount -u "$@"; }
176
177 # Create bookmarks for all the snapshots of the given datasets.
178 # Bookmarks are only useful on source datasets
179 # But syncoid --create-bookmark only creates
180 # a bookmark for the latest snapshot,
181 # possibly leaving the *_daily or *_monthly snapshots without a bookmark.
182 zfs-fix-bookmarks () {
183 local d
184 local -
185 set -x
186 for d in "$@"; do
187 for s in $(zfs list -Hrpt snapshot -o name "$d"); do
188 zfs bookmark "$s" "''${s//@/#}"
189 done
190 done
191 }
192
193 # Restore the inheritance of encryptionroot,
194 # usually broken by zfs send --raw.
195 # Note that it needs to decrypt the datasets.
196 zfs-fix-encryptionroot () {
197 local d
198 zfs load-key "$1"
199 for d in $(zfs list -rHo name "$1" | tail -n +2); do
200 echo >&2 "$d"
201 test "$(zfs get -Ho value encryptionroot $d)" = "$1" ||
202 zfs change-key -li "$d"
203 done
204 }
205
206 # Recursively remove all the snapshots of given datasets
207 zfs-destroy-snapshots () {
208 local d
209 for d in "$@"; do
210 zfs list -t snapshot -rHo name "$d" |
211 xargs --no-run-if-empty -L1 zfs destroy
212 done
213 }
214 '';
215 profileExtra = ''
216 '';
217 };
218 programs.direnv.enableBashIntegration = true;
219 #programs.broot.enableBashIntegration = true;
220 programs.readline = {
221 enable = mkDefault true;
222 includeSystemConfig = true;
223 bindings = {
224 # Up/Down
225 "\\e[A" = "history-search-backward";
226 "\\e[B" = "history-search-forward";
227 "\\eOA" = "history-search-backward";
228 "\\eOB" = "history-search-forward";
229
230 # Ctrl-Left/Ctrl-Right
231 "\\e[1;5C" = "forward-word";
232 "\\e[1;5D" = "backward-word";
233 "\\e[5C" = "forward-word";
234 "\\e[5D" = "backward-word";
235 "\\e\\e[C" = "forward-word";
236 "\\e\\e[D" = "backward-word";
237
238 # Home/End
239 "\\e[1~" = "beginning-of-line";
240 "\\e[4~" = "end-of-line";
241
242 # Delete/Insert
243 "\\e[3~" = "delete-char";
244 "\\e[2~" = "quoted-insert";
245
246 # For non RH/Debian xterm, can't hurt for RH/Debian xterm
247 "\\eOF" = "end-of-line";
248 "\\eOH" = "beginning-of-line";
249
250 # For freebsd console
251 "\\e[F" = "end-of-line";
252 "\\e[H" = "beginning-of-line";
253
254 # $if term=rxvt
255 "\\e[7~" = "beginning-of-line";
256 "\\e[8~" = "end-of-line";
257 "\\eOc" = "forward-word";
258 "\\eOd" = "backward-word";
259 # $endif
260 };
261 variables = {
262 # Be 8 bit clean.
263 input-meta = mkDefault true;
264 output-meta = mkDefault true;
265 colored-completion-prefix = mkDefault true;
266 colored-stats = mkDefault true; # Note that this may cause completion text blink in some terminals (e.g. xterm).
267 echo-control-characters = mkDefault true;
268 mark-symlinked-directories = mkDefault true;
269 menu-complete-display-prefix = mkDefault true;
270 show-all-if-ambiguous = mkDefault true;
271 show-all-if-unmodified = mkDefault true;
272 visible-stats = mkDefault false; # Append char to indicate type
273 enable-bracketed-paste = mkDefault true;
274 };
275 };
276 }