From ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 23 Sep 2026 21:08:35 +0200 Subject: ... --- accounts/gkleen@skadhi/zsh/default.nix | 106 ++++++++++++ accounts/gkleen@skadhi/zsh/zshrc | 298 +++++++++++++++++++++++++++++++++ 2 files changed, 404 insertions(+) create mode 100644 accounts/gkleen@skadhi/zsh/default.nix create mode 100644 accounts/gkleen@skadhi/zsh/zshrc (limited to 'accounts/gkleen@skadhi/zsh') 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 @@ +{ pkgs, lib, config, sysConfig, flakeInputs, ... }: +let + zshrc = pkgs.resholve.mkDerivation { + pname = "zshrc"; + version = "0.0.0"; + + src = ./zshrc; + + dontUnpack = true; + dontConfigure = true; + dontBuild = true; + + installPhase = '' + mkdir -p $out/share + install "$src" $out/share/zshrc + ''; + + solutions = { + default = { + scripts = [ "share/zshrc" ]; + interpreter = "none"; + inputs = with pkgs; [ + coreutils + rpm + binutils + squashfsTools + unzip + config.programs.git.package + magickWrapped + curl + file + gnutar + cpio + magic-wormhole + config.programs.zsh.package + util-linux + findutils + qrencode + tty-clock + config.programs.jq.package + eza + less + sysConfig.systemd.package + sysConfig.programs.ssh.package + gnused + miniserve + p7zip + ]; + execer = with pkgs; [ + "cannot:${lib.getExe' rpm "rpm2cpio"}" + "cannot:${lib.getExe' squashfsTools "unsquashfs"}" + "cannot:${lib.getExe' unzip "unzip"}" + "cannot:${lib.getExe config.programs.git.package}" + "cannot:${lib.getExe cpio}" + "cannot:${lib.getExe' magic-wormhole "wormhole"}" + "cannot:${lib.getExe' fuse "fusermount"}" + "cannot:${lib.getExe less}" + "cannot:${lib.getExe' sysConfig.systemd.package "systemctl"}" + "cannot:${lib.getExe sysConfig.programs.ssh.package}" + "cannot:${lib.getExe' p7zip "7z"}" + ]; + wrapper = with pkgs; [ + "${lib.getExe' magickWrapped "magick"}:${lib.getExe' imagemagick "magick"}" + ]; + fake = { + builtin = ["print"]; + external = lib.mapAttrsToList (_: cfg: cfg.program) sysConfig.security.wrappers; + }; + }; + }; + }; + magickWrapped = pkgs.symlinkJoin { + inherit (pkgs.imagemagick) name; + paths = [ pkgs.imagemagick ]; + + buildInputs = with pkgs; [ makeWrapper ]; + postBuild = '' + wrapProgram $out/bin/magick \ + --prefix PATH : ${lib.makeBinPath (with pkgs; [ ghostscript ])} + ''; + }; +in { + config = { + programs.zsh.initContent = '' + source ${zshrc}/share/zshrc + ''; + programs.zsh.dirHashes = let + flakeHashes = lib.mapAttrs' (n: v: lib.nameValuePair (inputNames.${n} or n) (toString v)) flakeInputs; + inputNames = { + self = "nixos"; + }; + in flakeHashes // { + docs = "$HOME/documents"; + dl = "$HOME/Downloads"; + scrot = "$HOME/screenshots"; + media = "$HOME/media"; + }; + programs.starship.settings = { + directory.substitutions = lib.mapAttrs' (name: value: let + value' = builtins.unsafeDiscardStringContext value; + value'' = if lib.hasPrefix "$HOME/" value' then "~" + lib.removePrefix "$HOME" value' else value'; + in lib.nameValuePair value'' "~${name}" + ) config.programs.zsh.dirHashes; + }; + }; +} 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 @@ +dir() { + curlArchive=false + templateArchive="" + repoUrl="" + dir="" + forceShell=false + wormhole=false + gitWorktree="" + modifyPDF="" + miniserve=false + + while getopts ':t:a:d:ir:wg:p:m' arg; do + case $arg in + "t") ;; + "a") + if [[ ${OPTARG} =~ "://" ]]; then + templateArchive=${OPTARG} + curlArchive=true + else + templateArchive=${OPTARG:a} + fi + ;; + "d") dir=${OPTARG} ;; + "i") forceShell=true ;; + "r") repoUrl=${OPTARG} ;; + "w") wormhole=true ;; + "g") gitWorktree=${OPTARG} ;; + "p") modifyPDF=${OPTARG:a} ;; + "m") miniserve=true ;; + *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;; + esac + done + + shift $((OPTIND - 1)) + + if [[ -z ${dir} && ${#@} -ge 1 ]]; then + dir=${1} + shift + fi + + [[ -n ${dir} ]] || return 2; + + if [[ ! -e ${dir} ]]; then + if [[ -z "${gitWorktree}" ]]; then + mkdir -vp ${dir} + else + git -C ${gitWorktree} worktree add ${dir} + fi + else + gitWorktree="" + fi + + miniservePIDFile="" + if [[ ${miniserve} = "true" ]]; then + miniservePIDFile=$(mktemp --tmpdir --suffix=.pid) + fi + + cleanup() + { + if [[ -n ${modifyPDF} ]]; then + cd ${modifyPDF:h} + typeset -a pages + eval 'pages=(${dir}/${modifyPDF:t:r}_*.png(on))' + magick -verbose "${pages[@]}" $(mktemp --tmpdir=${modifyPDF:h} ${modifyPDF:t:r}_XXXXXXXXXX.${modifyPDF:e}) + modifyPDF="" + fi + if [[ -n ${miniservePIDFile} ]]; then + command kill --verbose -- $(cat ${miniservePIDFile}) && wait $(cat ${miniservePIDFile}) + miniservePIDFile="" + fi + } + + ( + set -o localoptions -o localtraps + trap 'return 1' INT TERM + trap cleanup EXIT + + cd ${dir} + export dir; + + [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} . + + [[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF} + + if [[ -n ${templateArchive} ]]; then + ( + archiveFile="" + cleanup() { + [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile} + } + trap cleanup EXIT + + if [[ $curlArchive = "true" ]]; then + archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}") + + curl -SfL -o ${archiveFile} ${templateArchive} + + templateArchive=${archiveFile} + fi + + unpack=true + while [[ $unpack = "true" ]]; do + case $(file --brief --mime-type --dereference ${templateArchive}) in + application/zip) + unzip ${templateArchive} + unpack=false + ;; + application/vnd.debian.binary-package) + ar x ${templateArchive} + mkdir control data + tar -C control -xvaf control.* + tar -C data -xvaf data.* + unpack=false + ;; + application/x-rpm) + cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio") + rpm2cpio ${templateArchive} > ${cpioArchive} + templateArchive=${cpioArchive} + unpack=true + ;; + application/x-cpio) + cpio --extract --make-directories --no-absolute-filenames -F ${templateArchive} + unpack=false + ;; + application/pdf) + magick -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png + unpack=false + ;; + application/octet-stream) + if [[ $(file --brief --dereference ${templateArchive}) =~ Squashfs ]]; then + unsquashfs -d . ${templateArchive} + unpack=false + fi + ;; + application/x-iso9660-image) + 7z x ${templateArchive} + unpack=false + ;; + *) + tar -xvaf ${templateArchive} + unpack=false + ;; + esac + done + ) + fi + + + [[ $wormhole = "true" ]] && wormhole receive --accept-file + + + if [[ ${#@} -gt 0 ]]; then + ${@} + fi + + cd $(pwd) # Needed for mounting to work + + if [[ ${miniserve} = "true" ]]; then + miniserve --random-route --hidden --enable-tar-gz --enable-zip . & + echo $! > "${miniservePIDFile}" + fi + + if [[ ${#@} -eq 0 ]] && [[ ${miniserve} != "true" ]] || [[ $forceShell = "true" ]]; then + isSingleDir() { + typeset -a contents + contents=(*(N) .*(N)) + + if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then + print ${contents[1]} + return 0 + else + return 1 + fi + } + while d=$(isSingleDir); do cd ${d}; done + + zsh + elif [[ ${miniserve} == "true" ]]; then + wait $(cat "${miniservePIDFile}") + fi + ) +} + +tmpdir() { + cleanup() + { + cd / + unmount() { + printf "Unmounting %s\n" ${1} >&2 + fusermount3 -u ${1} || umount ${1} || sudo umount ${1} + } + + if [[ -n ${dir} ]]; then + if mountpoint -q -- ${dir}; then + unmount ${dir} || return $? + else + while read -d $'\0' subDir; do + mountpoint -q -- ${subDir} || continue + unmount ${subDir} || return $? + done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) + fi + + rm -rfv --one-file-system -- ${dir} + dir="" + fi + } + + local tmpdir="" + + while getopts ':t:a:d:ir:wg:p:m' arg; do + case $arg in + "t") tmpdir="=${OPTARG}" ;; + "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;; + esac + done + + ( + set -o localoptions -o localtraps + trap 'return 1' INT TERM + trap cleanup EXIT + + + local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?) + + ( dir -d ${dir} ${@} ) + ) +} + +qr() { + qrencode -l M -o - -t ANSIUTF8 $@ +} + +clock() { + tty-clock -sSbc -C 7 -d 0 -a 100000000 +} + +public-ip() { + curl -sSf -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip' +} + +swap() { + f1=${1} + f2=${2} + + if [[ -z "${f1}" || ! -e "${f1}" ]]; then + printf "ā€˜%s’ does not exist\n" "${f1}" >&2 + return 2 + fi + if [[ -z "${f2}" || ! -e "${f2}" ]]; then + printf "ā€˜%s’ does not exist\n" "${f2}" >&2 + return 2 + fi + + tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX) + mv -v ${f1} ${tmpfile} + mv -v ${f2} ${f1} + mv -v ${tmpfile} ${f2} +} + +ls() { + eza --all --group-directories-first --colour=always --sort=modified --smart-group $@ | less --mouse -FR +} + +l() { + ls --long --binary --git --time-style=iso --header $@ +} + +ssh-installer() { + ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@ +} + +dichotomic_search() { + min=$1; shift + max=$1; shift + + while [[ $min -lt $max ]]; do + # Compute the mean between min and max, rounded up to the superior unit + current=$(( (min + max + 1 ) / 2 )) + if $@ $current; then + min=$current + else + max=$((current - 1)) + fi + done + + echo $min +} + +while read -r -d $'\0' d _ s; do + hash -d ${s}=${d} +done < <(find ~/projects ~/uni -regextype posix-extended -maxdepth 2 -type d -regex '\S+/[0-9]{2}[ws]/\S+' -print0 2>/dev/null | \ + sed -zr 's|(.*/([0-9]{2}[ws])/(.+))|\1 \2 \3|' | \ + sort -z -r -k2 | sort -z -s -k3 | uniq -z -f 2) + +alias rzadm=$'tmpdir -i sh -c \'mkdir adm; sshfs gkleen@mgmt01:/adm adm\'' +alias mathcloud=$'tmpdir -i rclone mount --daemon mathcloud:// .' + +export DEFAULT_USER=gkleen -- cgit v1.2.3