summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2021-01-03 16:18:41 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2021-01-03 16:21:35 +0100
commit49fc7a188e1db386be4da0232df5cd11e0ea0a55 (patch)
treed0b64dbe3385971a602efa30b83043cbe252050f
parent27f0c7079add14a6990cbfeb4e348e433450be63 (diff)
downloadnixos-49fc7a188e1db386be4da0232df5cd11e0ea0a55.tar
nixos-49fc7a188e1db386be4da0232df5cd11e0ea0a55.tar.gz
nixos-49fc7a188e1db386be4da0232df5cd11e0ea0a55.tar.bz2
nixos-49fc7a188e1db386be4da0232df5cd11e0ea0a55.tar.xz
nixos-49fc7a188e1db386be4da0232df5cd11e0ea0a55.zip
gkleen@sif: additional zshrc
-rw-r--r--accounts/gkleen@sif/default.nix2
-rw-r--r--accounts/gkleen@sif/zshrc403
2 files changed, 405 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/default.nix b/accounts/gkleen@sif/default.nix
index be16693e..d6e91447 100644
--- a/accounts/gkleen@sif/default.nix
+++ b/accounts/gkleen@sif/default.nix
@@ -78,6 +78,8 @@ in {
78 }; 78 };
79 profiles = customUtils.recImport { dir = ./autorandr-profiles; }; 79 profiles = customUtils.recImport { dir = ./autorandr-profiles; };
80 }; 80 };
81
82 zsh.initExtra = "source ${./zshrc}";
81 }; 83 };
82 84
83 services = { 85 services = {
diff --git a/accounts/gkleen@sif/zshrc b/accounts/gkleen@sif/zshrc
new file mode 100644
index 00000000..34aa3772
--- /dev/null
+++ b/accounts/gkleen@sif/zshrc
@@ -0,0 +1,403 @@
1filebin() {
2 basePath=/srv/www/files
3 ssh ymir find "${basePath}" -type f -print0 \
4 | while IFS= read -r -d $'\0' p; do
5 printf "https://f.141.li/%s\n" "${p#${basePath}/}"
6 done
7}
8
9genmail() {
10 local baseName=""
11 local target=""
12 if [[ ${#@} -ge 1 ]]; then
13 target=${1}
14 shift
15 fi
16
17 if [[ ${#@} -ge 1 ]]; then
18 baseName=$(pwgen ${@})
19 else
20 baseName=$(pwgen -v -A -0 16 1)
21 fi
22 baseName=$(tr -cd $'[:alnum:]!#$%&*+-/=?^_{|}~.' <<<${baseName})
23 address=${baseName}@141.li
24 insertAddr() {
25 echo "${baseName} gkleen" | ssh ymir tee -a /srv/mail/spm 1>/dev/null \
26 }
27
28 printf "%s\n" ${address}
29 read -q 'cont?Continue [y/N]? ' || return
30
31 insertAddr
32}
33
34s() {
35 dir=$(pwd)
36 [[ ${#@} -ge 1 ]] && dir=$1
37
38 shellFile=$(findNix ${@})
39 [[ ${#@} -ge 1 ]] && shift
40
41 typeset -a cmd
42 if [[ -d ${dir}/.nix-gc-roots ]]; then
43 cmd=(persistent-nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@})
44 else
45 cmd=(nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@})
46 fi
47
48 if [[ -n "${S_SYSTEMD}" ]]; then
49 systemd-run --user --slice=development.slice --collect -E PATH=${PATH} -p WorkingDirectory=${dir} -- ${cmd}
50 else
51 (
52 cd ${dir}
53
54 exec ${cmd}
55 )
56 fi
57}
58
59sz() {
60 typeset -a S_EXTRA_ARGS
61 S_EXTRA_ARGS=(--run "env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
62}
63st() {
64 typeset -a S_EXTRA_ARGS
65 S_EXTRA_ARGS=(--run "tmux new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
66}
67stt() {
68 typeset -a S_EXTRA_ARGS
69 S_SYSTEMD=true S_EXTRA_ARGS=(--run "urxvt -e tmux -S .tmux.sock new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
70}
71se() {
72 typeset -a S_EXTRA_ARGS
73 S_SYSTEMD=true S_EXTRA_ARGS=(--run "emacs") s ${@}
74}
75
76findNix() {
77 if [[ $#@ -eq 0 ]]; then
78 findNix $(pwd)
79 elif [[ -f "$1" ]]; then
80 print ${1:a}
81 elif [[ -d "$1" && -f "$1"/shell.nix ]]; then
82 print ${1:a}/shell.nix
83 elif [[ -d "$1" && -f "$1"/default.nix ]]; then
84 print ${1:a}/default.nix
85 elif [[ -d "$1" && "$1" != "/" ]]; then
86 findNix ${1:h}
87 else
88 printf "Traversed directories to ‘/’ and found no shell specification\n" >&2
89 return 1
90 fi
91}
92
93dir() {
94 curlArchive=false
95 templateArchive=""
96 repoUrl=""
97 nixShell=""
98 findNix=false
99 dir=""
100 forceShell=false
101 wormhole=false
102 gitWorktree=""
103 notmuchMsg=""
104 quickserve=false
105
106 while getopts ':t:a:s:Sd:ir:wqg:n:' arg; do
107 case $arg in
108 "t") ;;
109 "a")
110 if [[ ${OPTARG} =~ "://" ]]; then
111 templateArchive=${OPTARG}
112 curlArchive=true
113 else
114 templateArchive=${OPTARG:a}
115 fi
116 ;;
117 "s") nixShell=${OPTARG:a} ;;
118 "S") findNix=true ;;
119 "d") dir=${OPTARG} ;;
120 "i") forceShell=true ;;
121 "r") repoUrl=${OPTARG} ;;
122 "w") wormhole=true ;;
123 "g") gitWorktree=${OPTARG} ;;
124 "n") notmuchMsg=${OPTARG} ;;
125 "q") quickserve=true ;;
126 *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
127 esac
128 done
129
130 shift $((OPTIND - 1))
131
132 if [[ -z ${dir} && ${#@} -ge 1 ]]; then
133 dir=${1}
134 shift
135 fi
136
137 [[ -n ${dir} ]] || return 2;
138
139 if [[ ! -e ${dir} ]]; then
140 if [[ -z "${gitWorktree}" ]]; then
141 mkdir -vp ${dir}
142 else
143 git -C ${gitWorktree} worktree add ${dir}
144 fi
145 else
146 gitWorktree=""
147 fi
148
149 (
150 cd ${dir}
151 export dir;
152
153 ${findNix} && { nixShell=$(findNix) || return $? }
154
155 [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} .
156
157 if [[ -n ${templateArchive} ]]; then
158 (
159 archiveFile=""
160 cleanup() {
161 [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile}
162 }
163 trap cleanup EXIT
164
165 if ${curlArchive}; then
166 archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}")
167
168 curl -L -o ${archiveFile} ${templateArchive}
169
170 templateArchive=${archiveFile}
171 fi
172
173 case $(file --brief --mime-type ${templateArchive}) in
174 application/zip) unzip ${templateArchive} ;;
175 *) tar -xvaf ${templateArchive} ;;
176 esac
177 )
178 fi
179
180
181 if [[ -n ${notmuchMsg} ]]; then
182 getMimeTypes() {
183 nix-shell -p mailcap --run "find \${buildInputs} -path '*/etc/mime.types' | head -n 1 | xargs -- cat"
184 }
185
186 typeset -a messages
187 messages=(${(z)$(notmuch search --output=messages ${notmuchMsg})})
188
189 for message (${messages}); do
190 typeset -A notmuchAtts
191 notmuchAtts=()
192
193 while IFS= read -r -d $'\n' line; do
194 [[ ${line} =~ '(attachment|part)\{ ID: ([0-9]+)' ]] || continue
195 attId=${match[2]}
196
197 [[ ${line} =~ 'Content-type: multipart/' ]] && continue
198
199 fName="part_${attId}"
200 [[ ${line} =~ 'Filename: (([^,]|,[^ ])+)' ]] && fName=${match[1]}
201
202 if [[ ${#messages} -gt 1 ]]; then
203 fName="${message}/${fName}"
204 fi
205
206 fExt="${fName:e}"
207 [[ -n "${fExt}" ]] && fName="${fName:r}"
208
209 if [[ -z "${fExt}" && ${line} =~ 'Content-type: (([^,]|,[^ ])+)$' ]]; then
210 fExt=$(getMimeTypes | grep ${match[1]}$'\t' | head -n 1 | awk '{ print $2; }')
211 fi
212
213 mkdir -p ${fName:h}
214 if [[ -n "${fExt}" ]]; then
215 fName=$(mktemp -p . "${fName}.XXXXXX.${fExt}")
216 else
217 fName=$(mktemp -p . "${fName}.XXXXXX")
218 fi
219
220 notmuchAtts[${attId}]=${fName}
221 done <<(notmuch show --decrypt=false -- ${message} | tr -d $'\f')
222
223 for attId fName in ${(kv)notmuchAtts}; do
224 [[ -d ${fName:h} ]] || mkdir -p ${fName:h}
225 printf "#%d → ‘%s’\n" "${attId}" "${fName}" >&2
226
227 notmuch show --decrypt=false --part=${attId} -- ${message} | pv -W -D 2 -i 0.1 >${fName}
228 done
229 done
230 fi
231
232
233 ${wormhole} && wormhole receive
234
235 if ${quickserve}; then
236 quickserve --root . --upload . --show-hidden --tar gz
237 fi
238
239
240 if [[ ${#@} -eq 0 ]] || ${forceShell}; then
241 if [[ ${#@} -gt 0 ]]; then
242 if [[ -z ${nixShell} ]]; then
243 ${@}
244 else
245 nix-shell ${nixShell} --run "${@}"
246 fi
247 fi
248
249 cd $(pwd) # Needed for mounting to work
250
251 isSingleDir() {
252 typeset -a contents
253 contents=(*(N) .*(N))
254
255 if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then
256 print ${contents[1]}
257 return 0
258 else
259 return 1
260 fi
261 }
262 while d=$(isSingleDir); do cd ${d}; done
263
264
265 if [[ -z ${nixShell} ]]; then
266 exec -- zsh
267 else
268 exec -- nix-shell ${nixShell} --run zsh
269 fi
270 else
271 if [[ -z ${nixShell} ]]; then
272 exec -- ${@}
273 else
274 exec -- nix-shell ${nixShell} --run "${@}"
275 fi
276 fi
277 )
278}
279
280tmpdir() {
281 cleanup()
282 {
283 cd /
284 unmount() {
285 printf "Unmounting %s\n" ${1} >&2
286 fusermount -u ${1} || umount ${1} || sudo umount ${1}
287 }
288
289 if mountpoint -q -- ${dir}; then
290 unmount ${dir} || return $?
291 else
292 while read -d $'\0' subDir; do
293 mountpoint -q -- ${subDir} || continue
294 unmount ${subDir} || return $?
295 done <<<$(find ${dir} -xdev -type d -print0 | sort -zr)
296 fi
297
298 rm -rfv --one-file-system -- ${dir}
299 }
300
301 local tmpdir=""
302
303 while getopts ':t:a:s:Sd:ir:wqg:n:' arg; do
304 case $arg in
305 "t") tmpdir="=${OPTARG}" ;;
306 "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
307 esac
308 done
309
310 (
311 trap cleanup EXIT
312
313
314 local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?)
315
316 dir -d ${dir} ${@}
317 )
318}
319
320inhibit-sleep() {
321 if systemctl --user is-active prevent-suspend.service 1>/dev/null; then
322 echo "Allowing suspend"
323 systemctl --user stop prevent-suspend.service
324 else
325 echo "Inhibiting suspend"
326 systemctl --user start prevent-suspend.service
327 fi
328}
329
330qr() {
331 qrencode -l M -o - -t ANSIUTF8 $@
332}
333
334clock() {
335 tty-clock -sSbc -C 7 -d 0 -a 100000000
336}
337
338public-ip() {
339 curl -s -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip'
340}
341
342nix-ghci() {
343 pkgExpr=""
344 if [[ ${#@} -gt 0 ]]; then
345 pkgExpr="${1}"
346 shift
347 fi
348
349 nix-shell -p "with (import <nixpkgs> {}); pkgs.haskellPackages.ghcWithPackages (p: with p; [${pkgExpr}])" --run "ghci ${@}"
350}
351
352swap() {
353 f1=${1}
354 f2=${2}
355
356 if [[ -z "${f1}" || ! -e "${f1}" ]]; then
357 printf "‘%s’ does not exist\n" "${f1}" >&2
358 return 2
359 fi
360 if [[ -z "${f2}" || ! -e "${f2}" ]]; then
361 printf "‘%s’ does not exist\n" "${f2}" >&2
362 return 2
363 fi
364
365 tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX)
366 mv -v ${f1} ${tmpfile}
367 mv -v ${f2} ${f1}
368 mv -v ${tmpfile} ${f2}
369}
370
371unalias l
372l() {
373 exa --binary --git --time-style=iso --long --all --header --group-directories-first --colour=always $@
374}
375
376unalias ll
377ll() {
378 l $@ | less --mouse -FR
379}
380
381alias '..'='cd ..'
382alias -g L='| less'
383alias -g S='&> /dev/null'
384alias -g G='| grep'
385alias -g B='&> /dev/null &'
386alias -g BB='&> /dev/null &!'
387
388fancy-ctrl-z () {
389 emulate -LR zsh
390 if [[ $#BUFFER -eq 0 ]]; then
391 [[ -n $(jobs -s) ]] && bg
392 zle redisplay
393 else
394 zle push-input
395 fi
396}
397zle -N fancy-ctrl-z
398bindkey '^Z' fancy-ctrl-z
399
400export DEFAULT_USER=gkleen
401export EDITOR=emacsclient
402
403bindkey -e \ No newline at end of file