Commit b0c8a95e2108

Vincent Demeester <vincent@sbr.pm>
2026-07-06 23:16:28
feat(zsh): add $ prefix, isolate sandbox, clean utility
Added dollar-sign function for paste-friendly commands, bwrap isolate wrapper for sandboxed shells, and interactive clean function for nix, podman, docker, go, journal, and old files. Removed empty clean stub from auto-expanding-aliases.
1 parent c9da38b
Changed files (2)
dots
config
home
dots/config/zsh/core/56-utilities.zsh
@@ -0,0 +1,93 @@
+# Miscellaneous utility functions
+
+# Allow pasting commands prefixed with $
+# See https://vincent.bernat.ch/en/blog/2025-zsh-autoexpand-aliases
+function \$() { "$@" }
+
+# Isolate commands in a bwrap sandbox
+# Usage:
+#   isolate                    — shell with only PWD writable
+#   isolate --share-net        — same but with network
+#   isolate --share-net -- cmd — run cmd in sandbox
+#   isolate --no-cwd -- cmd   — no PWD access
+(( $+commands[bwrap] )) && isolate() {
+    local -a options moreoptions nocwd
+    options=(
+        --ro-bind /{,}
+        --dev /dev
+        --proc /proc
+        --tmpfs /run
+        --tmpfs /tmp
+        --tmpfs /var/tmp
+        --tmpfs $HOME
+        --unshare-all
+        --die-with-parent
+    )
+    [[ $(sysctl -en dev.tty.legacy_tiocsti) == 0 ]] || options=($options --new-session)
+    [[ -n $XDG_RUNTIME_DIR ]] && options=($options --tmpfs $XDG_RUNTIME_DIR)
+    [[ -L /etc/resolv.conf ]] && options=($options --ro-bind ${${:-/etc/resolv.conf}:A}{,})
+    case $1 in
+        (--*)
+            while [[ $# -gt 0 ]] && [[ $1 != "--" ]]; do
+                case $1 in
+                    (--no-cwd) nocwd=1 ;;
+                    (*) moreoptions=($moreoptions $1) ;;
+                esac
+                shift
+            done
+            [[ $1 == "--" ]] && shift
+            ;;
+    esac
+    [[ -z $nocwd ]] && [[ $PWD != $HOME ]] && options=($options --bind $PWD{,})
+    options=($options $moreoptions)
+
+    if [[ $# -eq 0 ]]; then
+        options=(
+            $options
+            --setenv VDE_SHELL_ISOLATED true
+            --
+            zsh -i
+        )
+    else
+        options=($options -- "$@")
+    fi
+    bwrap $options
+}
+
+# Interactive system cleanup
+clean() {
+    local prompt() {
+        local what=$1
+        local prompt="${(%):-%B}Clean $what?${(%):-%b}"
+        read -sq "?$prompt "
+        case $? in
+            0)
+                print -P "%F{green}yes%F{default}"
+                return 0
+                ;;
+        esac
+        print -P "%F{red}no%F{default}"
+        return 1
+    }
+
+    (( $+commands[podman] )) && prompt "Podman unused data" && \
+        podman system prune -f
+    (( $+commands[docker] )) && prompt "Docker unused data" && \
+        sudo docker system prune -f
+    [[ -d /nix ]] && prompt "Nix store (older than 7d)" && \
+        nix-collect-garbage --delete-older-than 7d
+    [[ -d /var/log/journal ]] && prompt "journal logs (older than 2 months)" && \
+        sudo journalctl --vacuum-time='2 months'
+    (( $+commands[go] )) && {
+        [[ -d $(go env GOMODCACHE) ]] && prompt "Go module cache" && go clean -modcache
+        [[ -d $(go env GOCACHE) ]] && prompt "Go build cache" && go clean -cache
+    }
+    (( $+commands[flatpak] )) && prompt "Flatpak unused runtimes" && \
+        flatpak uninstall --unused
+    local d
+    for d in tmp src download; do
+        [[ -d ~/$d ]] && prompt "~/$d entries older than 60 days" && \
+            find ~/$d -maxdepth 1 -mindepth 1 -type d -mtime +60 -print0 | xargs -0r rm -rf && \
+            find ~/$d -maxdepth 1 -mindepth 1 -type f -mtime +60 -delete
+    done
+}
home/common/shell/zsh/auto-expanding-aliases.zsh
@@ -125,7 +125,6 @@ v() {
     fi
 }
 
-function clean() {}
 
 if [[ -d ${HOME}/src/github.com/chmouel/jayrah ]]; then
     alias jayrah="uv --directory=${HOME}/src/github.com/chmouel/jayrah run jayrah"