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