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