Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions bin/omarchy-cmd-desktop-exec-repair
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/python3
# omarchy:summary=Preserve desktop customizations while repairing a known executable token
# omarchy:args=<desktop> <vendor-desktop> <wrapper> <known-executable>...
# omarchy:hidden=true

import os
from pathlib import Path
import re
import shutil
import sys
import tempfile


def repair(target, vendor, wrapper, known):
if target.is_symlink() or (target.exists() and not target.is_file()):
print(f"Preserving custom desktop route: {target}", file=sys.stderr)
return
existing = target.exists()
source = target if existing else vendor
if not source.is_file():
return
data = source.read_bytes()
text = data.decode("utf-8")
# Desktop Exec is not a shell command. Only replace a literal leading token;
# env, shell commands, escaped/complex routes and all following bytes survive.
pattern = re.compile(r'^(Exec)=([ \t]*)("[^"\\\n]+"|[^\s"\\]+)(?=[ \t\r\n]|$)', re.MULTILINE)
def replace(match):
token = match[3]
value = token[1:-1] if token.startswith('"') else token
if value not in known:
return match[0]
new = wrapper
if token.startswith('"') or any(c.isspace() for c in new):
new = '"' + new.replace('\\', '\\\\').replace('"', '\\"') + '"'
return match[1] + '=' + match[2] + new
# TryExec is a desktop string containing the whole executable path, not an
# Exec command line. Quotes are literal bytes to its availability resolver.
escapes = {'s': ' ', 'n': '\n', 't': '\t', 'r': '\r', '\\': '\\'}
def replace_try_exec(match):
value = re.sub(r'\\([sntr\\])', lambda part: escapes[part[1]], match[1])
# Repair the quoted form emitted by older versions of this helper too.
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
if value not in known and value != wrapper:
return match[0]
new = wrapper.replace('\\', '\\\\').replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r')
return 'TryExec=' + new + match[2]
updated = re.sub(r'^TryExec=([^\r\n]*)(\r?)$', replace_try_exec,
pattern.sub(replace, text), flags=re.MULTILINE).encode("utf-8")
if updated == data:
return
target.parent.mkdir(parents=True, exist_ok=True)
staged = None
try:
with tempfile.NamedTemporaryFile(dir=target.parent, prefix='.' + target.name + '.', delete=False) as stream:
staged = stream.name
stream.write(updated)
os.chmod(staged, source.stat().st_mode & 0o777)
if existing:
with tempfile.NamedTemporaryFile(dir=target.parent, prefix=target.name + '.bak.', delete=False) as backup:
backup.write(data)
shutil.copystat(target, backup.name)
os.replace(staged, target)
staged = None
finally:
if staged:
os.unlink(staged)


if __name__ == '__main__':
if len(sys.argv) < 5:
sys.exit('Usage: omarchy-cmd-desktop-exec-repair <desktop> <vendor-desktop> <wrapper> <known-executable>...')
try:
repair(Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3], set(sys.argv[4:]))
except (OSError, UnicodeError) as error:
sys.exit(f'Desktop repair failed: {error}')
8 changes: 8 additions & 0 deletions bin/omarchy-cmd-electron-gl-args
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# omarchy:summary=Print Chromium/Electron flags for software GL when no render GPU is present
# omarchy:hidden=true

if ! omarchy-hw-render-gpu; then
printf '%s\n' --ozone-platform=wayland --disable-gpu
fi
117 changes: 117 additions & 0 deletions bin/omarchy-cmd-electron-gl-wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/bash

# omarchy:summary=Install a PATH wrapper that adds software GL flags when no render GPU is present
# omarchy:args=[--check] <command-name> <real-binary>
# omarchy:examples=omarchy-cmd-electron-gl-wrap 1password /opt/1Password/1password | omarchy-cmd-electron-gl-wrap chromium /usr/bin/chromium
# omarchy:hidden=true
# omarchy:requires-sudo=true

set -euo pipefail

# Exit 3 means an administrator owns the launcher; callers may skip that case.
# Exit 4 is reserved for --check: the managed wrapper is not ready yet.
check=false
if [[ ${1:-} == "--check" ]]; then
check=true
shift
fi

if (($# != 2)); then
echo "Usage: omarchy-cmd-electron-gl-wrap [--check] <command-name> <real-binary>" >&2
exit 2
fi

name=$1
real=$2
if [[ -z $name || $name == */* || $name == "." || $name == ".." ]]; then
echo "omarchy-cmd-electron-gl-wrap: command name must be a basename: $name" >&2
exit 2
fi

bind_dir=${OMARCHY_ELECTRON_GL_BIND_DIR:-/usr/local/bin}
dest=$bind_dir/$name

if [[ ! -x $real ]]; then
echo "omarchy-cmd-electron-gl-wrap: not executable: $real" >&2
exit 1
fi

# A symlink is never an Omarchy-owned launcher, even when it aliases the real
# binary. Keep literal and hardlinked self-wraps fatal before inspecting markers.
if [[ $dest == "$real" ]] || [[ ! -L $dest && $dest -ef $real ]]; then
echo "omarchy-cmd-electron-gl-wrap: refusing to wrap a binary over itself: $dest" >&2
exit 1
fi

if [[ -e $dest || -L $dest ]]; then
owned=false
if [[ ! -L $dest && -f $dest ]]; then
if grep -q '^# omarchy-electron-gl-wrapper$' "$dest"; then
owned=true
else
status=$?
if ((status != 1)); then
exit "$status"
fi
fi
fi
if ! $owned; then
echo "omarchy-cmd-electron-gl-wrap: refusing to replace an unmanaged launcher: $dest" >&2
exit 3
fi
fi

as_root() {
if ((EUID == 0)) || [[ -d $bind_dir && -w $bind_dir ]]; then
"$@"
else
sudo "$@"
fi
}

quoted_real=$(printf '%q' "$real")
wrapper=$(cat <<EOF
#!/bin/bash
# omarchy-electron-gl-wrapper
real=$quoted_real
extra=()
if omarchy-cmd-present omarchy-cmd-electron-gl-args; then
while IFS= read -r flag; do
[[ -n \$flag ]] && extra+=("\$flag")
done < <(omarchy-cmd-electron-gl-args)
fi
exec "\$real" "\${extra[@]}" "\$@"
EOF
)

staged=""
cleanup() {
if [[ -n $staged ]]; then
as_root rm -f -- "$staged"
fi
}

if [[ ! -L $dest && -f $dest && $(<"$dest") == "$wrapper" ]]; then
# System setup may already have installed this wrapper before user finalization.
# Leave correct permissions alone so first login needs no root access.
if [[ $(stat -c %a "$dest") != "755" ]]; then
if $check; then
exit 4
else
as_root chmod 755 "$dest"
fi
fi
elif $check; then
exit 4
else
trap cleanup EXIT

# Stage beside the launcher so rename replaces it atomically and a failed
# write or chmod leaves the old launcher (and real binary) untouched.
as_root mkdir -p -- "$bind_dir"
staged=$(as_root mktemp -- "$bind_dir/.omarchy-electron-gl.XXXXXX")
as_root tee -- "$staged" >/dev/null <<<"$wrapper"
as_root chmod 755 -- "$staged"
as_root mv -fT -- "$staged" "$dest"
staged=""
fi
15 changes: 15 additions & 0 deletions bin/omarchy-hw-render-gpu
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# omarchy:summary=Detect whether a DRM render GPU is present
# omarchy:hidden=true

dri=${OMARCHY_DRI_PATH:-/dev/dri}

shopt -s nullglob
for node in "$dri"/renderD*; do
if [[ -e $node ]]; then
exit 0
fi
done

exit 1
25 changes: 25 additions & 0 deletions bin/omarchy-install-service-1password
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,33 @@ install_chromium_extension() {
sudo chmod 644 "$EXTENSION_FILE"
}

# Without a DRM render node (a VM, a headless GPU, a GPU with no driver yet)
# Electron draws nothing unless it is told to use software GL. Launch 1Password
# through a wrapper that adds those flags while there is no render node, and
# point the user's desktop entry at it.
wrap_1password_for_software_gl() {
local real=${OMARCHY_1PASSWORD_BIN:-/opt/1Password/1password}
local vendor=${OMARCHY_1PASSWORD_DESKTOP:-/usr/share/applications/1password.desktop}
local status=0

if omarchy-hw-render-gpu || [[ ! -x $real ]]; then
return 0
fi

omarchy-cmd-electron-gl-wrap 1password "$real" || status=$?
if (( status == 0 )); then
omarchy-cmd-desktop-exec-repair "$HOME/.local/share/applications/1password.desktop" \
"$vendor" "${OMARCHY_ELECTRON_GL_BIND_DIR:-/usr/local/bin}/1password" "$real" 1password
elif (( status == 3 )); then
echo "Preserving administrator-owned 1Password launcher" >&2
else
return "$status"
fi
}

echo "Installing 1Password..."
omarchy-pkg-add 1password 1password-cli
wrap_1password_for_software_gl

echo "Installing 1Password extension for Chromium..."
install_chromium_extension
Expand Down
Loading