summaryrefslogtreecommitdiff
path: root/accounts/gkleen@skadhi/zsh
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2026-09-23 21:08:35 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2026-09-23 21:08:35 +0200
commitae9a80c1f012686c1f65fce7ce76fdd1b77b0b71 (patch)
tree141f738276cf84c6b7e5633c2bd5f2b44e9b0b75 /accounts/gkleen@skadhi/zsh
parent6a763272038e637a8a03dd9059d43a1ff44921dd (diff)
downloadnixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar
nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.gz
nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.bz2
nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.xz
nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.zip
...
Diffstat (limited to 'accounts/gkleen@skadhi/zsh')
-rw-r--r--accounts/gkleen@skadhi/zsh/default.nix106
-rw-r--r--accounts/gkleen@skadhi/zsh/zshrc298
2 files changed, 404 insertions, 0 deletions
diff --git a/accounts/gkleen@skadhi/zsh/default.nix b/accounts/gkleen@skadhi/zsh/default.nix
new file mode 100644
index 00000000..ddc69f0d
--- /dev/null
+++ b/accounts/gkleen@skadhi/zsh/default.nix
@@ -0,0 +1,106 @@
1{ pkgs, lib, config, sysConfig, flakeInputs, ... }:
2let
3 zshrc = pkgs.resholve.mkDerivation {
4 pname = "zshrc";
5 version = "0.0.0";
6
7 src = ./zshrc;
8
9 dontUnpack = true;
10 dontConfigure = true;
11 dontBuild = true;
12
13 installPhase = ''
14 mkdir -p $out/share
15 install "$src" $out/share/zshrc
16 '';
17
18 solutions = {
19 default = {
20 scripts = [ "share/zshrc" ];
21 interpreter = "none";
22 inputs = with pkgs; [
23 coreutils
24 rpm
25 binutils
26 squashfsTools
27 unzip
28 config.programs.git.package
29 magickWrapped
30 curl
31 file
32 gnutar
33 cpio
34 magic-wormhole
35 config.programs.zsh.package
36 util-linux
37 findutils
38 qrencode
39 tty-clock
40 config.programs.jq.package
41 eza
42 less
43 sysConfig.systemd.package
44 sysConfig.programs.ssh.package
45 gnused
46 miniserve
47 p7zip
48 ];
49 execer = with pkgs; [
50 "cannot:${lib.getExe' rpm "rpm2cpio"}"
51 "cannot:${lib.getExe' squashfsTools "unsquashfs"}"
52 "cannot:${lib.getExe' unzip "unzip"}"
53 "cannot:${lib.getExe config.programs.git.package}"
54 "cannot:${lib.getExe cpio}"
55 "cannot:${lib.getExe' magic-wormhole "wormhole"}"
56 "cannot:${lib.getExe' fuse "fusermount"}"
57 "cannot:${lib.getExe less}"
58 "cannot:${lib.getExe' sysConfig.systemd.package "systemctl"}"
59 "cannot:${lib.getExe sysConfig.programs.ssh.package}"
60 "cannot:${lib.getExe' p7zip "7z"}"
61 ];
62 wrapper = with pkgs; [
63 "${lib.getExe' magickWrapped "magick"}:${lib.getExe' imagemagick "magick"}"
64 ];
65 fake = {
66 builtin = ["print"];
67 external = lib.mapAttrsToList (_: cfg: cfg.program) sysConfig.security.wrappers;
68 };
69 };
70 };
71 };
72 magickWrapped = pkgs.symlinkJoin {
73 inherit (pkgs.imagemagick) name;
74 paths = [ pkgs.imagemagick ];
75
76 buildInputs = with pkgs; [ makeWrapper ];
77 postBuild = ''
78 wrapProgram $out/bin/magick \
79 --prefix PATH : ${lib.makeBinPath (with pkgs; [ ghostscript ])}
80 '';
81 };
82in {
83 config = {
84 programs.zsh.initContent = ''
85 source ${zshrc}/share/zshrc
86 '';
87 programs.zsh.dirHashes = let
88 flakeHashes = lib.mapAttrs' (n: v: lib.nameValuePair (inputNames.${n} or n) (toString v)) flakeInputs;
89 inputNames = {
90 self = "nixos";
91 };
92 in flakeHashes // {
93 docs = "$HOME/documents";
94 dl = "$HOME/Downloads";
95 scrot = "$HOME/screenshots";
96 media = "$HOME/media";
97 };
98 programs.starship.settings = {
99 directory.substitutions = lib.mapAttrs' (name: value: let
100 value' = builtins.unsafeDiscardStringContext value;
101 value'' = if lib.hasPrefix "$HOME/" value' then "~" + lib.removePrefix "$HOME" value' else value';
102 in lib.nameValuePair value'' "~${name}"
103 ) config.programs.zsh.dirHashes;
104 };
105 };
106}
diff --git a/accounts/gkleen@skadhi/zsh/zshrc b/accounts/gkleen@skadhi/zsh/zshrc
new file mode 100644
index 00000000..c532b308
--- /dev/null
+++ b/accounts/gkleen@skadhi/zsh/zshrc
@@ -0,0 +1,298 @@
1dir() {
2 curlArchive=false
3 templateArchive=""
4 repoUrl=""
5 dir=""
6 forceShell=false
7 wormhole=false
8 gitWorktree=""
9 modifyPDF=""
10 miniserve=false
11
12 while getopts ':t:a:d:ir:wg:p:m' arg; do
13 case $arg in
14 "t") ;;
15 "a")
16 if [[ ${OPTARG} =~ "://" ]]; then
17 templateArchive=${OPTARG}
18 curlArchive=true
19 else
20 templateArchive=${OPTARG:a}
21 fi
22 ;;
23 "d") dir=${OPTARG} ;;
24 "i") forceShell=true ;;
25 "r") repoUrl=${OPTARG} ;;
26 "w") wormhole=true ;;
27 "g") gitWorktree=${OPTARG} ;;
28 "p") modifyPDF=${OPTARG:a} ;;
29 "m") miniserve=true ;;
30 *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
31 esac
32 done
33
34 shift $((OPTIND - 1))
35
36 if [[ -z ${dir} && ${#@} -ge 1 ]]; then
37 dir=${1}
38 shift
39 fi
40
41 [[ -n ${dir} ]] || return 2;
42
43 if [[ ! -e ${dir} ]]; then
44 if [[ -z "${gitWorktree}" ]]; then
45 mkdir -vp ${dir}
46 else
47 git -C ${gitWorktree} worktree add ${dir}
48 fi
49 else
50 gitWorktree=""
51 fi
52
53 miniservePIDFile=""
54 if [[ ${miniserve} = "true" ]]; then
55 miniservePIDFile=$(mktemp --tmpdir --suffix=.pid)
56 fi
57
58 cleanup()
59 {
60 if [[ -n ${modifyPDF} ]]; then
61 cd ${modifyPDF:h}
62 typeset -a pages
63 eval 'pages=(${dir}/${modifyPDF:t:r}_*.png(on))'
64 magick -verbose "${pages[@]}" $(mktemp --tmpdir=${modifyPDF:h} ${modifyPDF:t:r}_XXXXXXXXXX.${modifyPDF:e})
65 modifyPDF=""
66 fi
67 if [[ -n ${miniservePIDFile} ]]; then
68 command kill --verbose -- $(cat ${miniservePIDFile}) && wait $(cat ${miniservePIDFile})
69 miniservePIDFile=""
70 fi
71 }
72
73 (
74 set -o localoptions -o localtraps
75 trap 'return 1' INT TERM
76 trap cleanup EXIT
77
78 cd ${dir}
79 export dir;
80
81 [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} .
82
83 [[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF}
84
85 if [[ -n ${templateArchive} ]]; then
86 (
87 archiveFile=""
88 cleanup() {
89 [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile}
90 }
91 trap cleanup EXIT
92
93 if [[ $curlArchive = "true" ]]; then
94 archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}")
95
96 curl -SfL -o ${archiveFile} ${templateArchive}
97
98 templateArchive=${archiveFile}
99 fi
100
101 unpack=true
102 while [[ $unpack = "true" ]]; do
103 case $(file --brief --mime-type --dereference ${templateArchive}) in
104 application/zip)
105 unzip ${templateArchive}
106 unpack=false
107 ;;
108 application/vnd.debian.binary-package)
109 ar x ${templateArchive}
110 mkdir control data
111 tar -C control -xvaf control.*
112 tar -C data -xvaf data.*
113 unpack=false
114 ;;
115 application/x-rpm)
116 cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio")
117 rpm2cpio ${templateArchive} > ${cpioArchive}
118 templateArchive=${cpioArchive}
119 unpack=true
120 ;;
121 application/x-cpio)
122 cpio --extract --make-directories --no-absolute-filenames -F ${templateArchive}
123 unpack=false
124 ;;
125 application/pdf)
126 magick -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png
127 unpack=false
128 ;;
129 application/octet-stream)
130 if [[ $(file --brief --dereference ${templateArchive}) =~ Squashfs ]]; then
131 unsquashfs -d . ${templateArchive}
132 unpack=false
133 fi
134 ;;
135 application/x-iso9660-image)
136 7z x ${templateArchive}
137 unpack=false
138 ;;
139 *)
140 tar -xvaf ${templateArchive}
141 unpack=false
142 ;;
143 esac
144 done
145 )
146 fi
147
148
149 [[ $wormhole = "true" ]] && wormhole receive --accept-file
150
151
152 if [[ ${#@} -gt 0 ]]; then
153 ${@}
154 fi
155
156 cd $(pwd) # Needed for mounting to work
157
158 if [[ ${miniserve} = "true" ]]; then
159 miniserve --random-route --hidden --enable-tar-gz --enable-zip . &
160 echo $! > "${miniservePIDFile}"
161 fi
162
163 if [[ ${#@} -eq 0 ]] && [[ ${miniserve} != "true" ]] || [[ $forceShell = "true" ]]; then
164 isSingleDir() {
165 typeset -a contents
166 contents=(*(N) .*(N))
167
168 if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then
169 print ${contents[1]}
170 return 0
171 else
172 return 1
173 fi
174 }
175 while d=$(isSingleDir); do cd ${d}; done
176
177 zsh
178 elif [[ ${miniserve} == "true" ]]; then
179 wait $(cat "${miniservePIDFile}")
180 fi
181 )
182}
183
184tmpdir() {
185 cleanup()
186 {
187 cd /
188 unmount() {
189 printf "Unmounting %s\n" ${1} >&2
190 fusermount3 -u ${1} || umount ${1} || sudo umount ${1}
191 }
192
193 if [[ -n ${dir} ]]; then
194 if mountpoint -q -- ${dir}; then
195 unmount ${dir} || return $?
196 else
197 while read -d $'\0' subDir; do
198 mountpoint -q -- ${subDir} || continue
199 unmount ${subDir} || return $?
200 done <<<$(find ${dir} -xdev -type d -print0 | sort -zr)
201 fi
202
203 rm -rfv --one-file-system -- ${dir}
204 dir=""
205 fi
206 }
207
208 local tmpdir=""
209
210 while getopts ':t:a:d:ir:wg:p:m' arg; do
211 case $arg in
212 "t") tmpdir="=${OPTARG}" ;;
213 "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
214 esac
215 done
216
217 (
218 set -o localoptions -o localtraps
219 trap 'return 1' INT TERM
220 trap cleanup EXIT
221
222
223 local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?)
224
225 ( dir -d ${dir} ${@} )
226 )
227}
228
229qr() {
230 qrencode -l M -o - -t ANSIUTF8 $@
231}
232
233clock() {
234 tty-clock -sSbc -C 7 -d 0 -a 100000000
235}
236
237public-ip() {
238 curl -sSf -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip'
239}
240
241swap() {
242 f1=${1}
243 f2=${2}
244
245 if [[ -z "${f1}" || ! -e "${f1}" ]]; then
246 printf "ā€˜%s’ does not exist\n" "${f1}" >&2
247 return 2
248 fi
249 if [[ -z "${f2}" || ! -e "${f2}" ]]; then
250 printf "ā€˜%s’ does not exist\n" "${f2}" >&2
251 return 2
252 fi
253
254 tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX)
255 mv -v ${f1} ${tmpfile}
256 mv -v ${f2} ${f1}
257 mv -v ${tmpfile} ${f2}
258}
259
260ls() {
261 eza --all --group-directories-first --colour=always --sort=modified --smart-group $@ | less --mouse -FR
262}
263
264l() {
265 ls --long --binary --git --time-style=iso --header $@
266}
267
268ssh-installer() {
269 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@
270}
271
272dichotomic_search() {
273 min=$1; shift
274 max=$1; shift
275
276 while [[ $min -lt $max ]]; do
277 # Compute the mean between min and max, rounded up to the superior unit
278 current=$(( (min + max + 1 ) / 2 ))
279 if $@ $current; then
280 min=$current
281 else
282 max=$((current - 1))
283 fi
284 done
285
286 echo $min
287}
288
289while read -r -d $'\0' d _ s; do
290 hash -d ${s}=${d}
291done < <(find ~/projects ~/uni -regextype posix-extended -maxdepth 2 -type d -regex '\S+/[0-9]{2}[ws]/\S+' -print0 2>/dev/null | \
292 sed -zr 's|(.*/([0-9]{2}[ws])/(.+))|\1 \2 \3|' | \
293 sort -z -r -k2 | sort -z -s -k3 | uniq -z -f 2)
294
295alias rzadm=$'tmpdir -i sh -c \'mkdir adm; sshfs gkleen@mgmt01:/adm adm\''
296alias mathcloud=$'tmpdir -i rclone mount --daemon mathcloud:// .'
297
298export DEFAULT_USER=gkleen