Skip to content
Closed
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
110 changes: 99 additions & 11 deletions bin/omarchy-channel-set
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
#!/bin/bash
#!/bin/bash -p

# omarchy:summary=Set the Omarchy package channel.
# omarchy:args=<stable|rc|edge|dev>
# omarchy:requires-sudo=true

if [[ $- != *p* ]]; then
echo "Refusing an unsafe Bash startup for channel switching." >&2
exit 126
fi

require_privileged_bash_startup() {
[[ $- == *p* ]] || return 1
/usr/bin/env -i /usr/bin/bash -p -c '
[[ $1 =~ ^[1-9][0-9]*$ ]] || exit 1
mapfile -d "" -t argv <"/proc/$1/cmdline" || exit 1
executable=$(/usr/bin/readlink -e -- "/proc/$1/exe") || exit 1
[[ $executable == /usr/bin/bash ]]
[[ ${argv[0]:-} == /bin/bash || ${argv[0]:-} == /usr/bin/bash ]]
[[ ${argv[1]:-} == -p ]]
' omarchy-bash-startup "$$"
}
if ! require_privileged_bash_startup; then
echo "Refusing an unsafe Bash startup for channel switching." >&2
exit 126
fi
unset -f require_privileged_bash_startup

set -euo pipefail

sanitize_bash_startup_environment() {
local environment_entry environment_name
local needs_reexec=0
local -a environment_unsets=(-u BASH_ENV -u ENV)

[[ -z ${BASH_ENV+x} && -z ${ENV+x} ]] || needs_reexec=1
while IFS= read -r -d '' environment_entry; do
environment_name="${environment_entry%%=*}"
if [[ $environment_name == BASH_FUNC_*%% ]]; then
environment_unsets+=(-u "$environment_name")
needs_reexec=1
fi
done < <(/usr/bin/env -0)

if (( needs_reexec )); then
exec /usr/bin/env "${environment_unsets[@]}" /usr/bin/bash -p "$0" "$@"
fi
}
sanitize_bash_startup_environment "$@"
unset -f sanitize_bash_startup_environment

cleanup_sudo_credentials() {
/usr/bin/sudo -k || true
}

usage() { echo "Usage: omarchy-channel-set [stable|rc|edge|dev]"; }
fail() { echo "Error: $*" >&2; exit 1; }

Expand All @@ -17,7 +64,7 @@ It's exclusively intended for developers working on Omarchy itself.

WARNING

gum confirm --default=false "Switch to dev channel?"
/usr/bin/gum confirm --default=false "Switch to dev channel?"
}

validate_dev_checkout() {
Expand All @@ -34,12 +81,43 @@ validate_dev_checkout() {

link_dev_checkout() {
local checkout="$1"
[[ -d $checkout/.git ]] || git clone https://github.com/basecamp/omarchy.git "$checkout"
[[ -d $checkout/.git ]] || /usr/bin/git clone https://github.com/basecamp/omarchy.git "$checkout"

omarchy-dev-link "$checkout" --no-reboot
/usr/bin/omarchy-dev-link "$checkout" --no-reboot
}

prepare_non_reusable_sudo() {
local wrapper=/usr/share/omarchy/default/omarchy/sudo-no-update/sudo
local current="$wrapper" canonical="" owner="" mode=""

LC_ALL=C /usr/bin/sudo -h 2>&1 | /usr/bin/grep -Eq '^usage: sudo .*\[[^]]*N[^]]*\]' ||
fail "This sudo does not support --no-update; refusing a mixed-trust channel switch."
[[ -f $wrapper && -x $wrapper && ! -L $wrapper ]] ||
fail "Trusted no-update sudo wrapper is missing."
canonical=$(/usr/bin/realpath -e -- "$wrapper") || fail "Cannot resolve the trusted no-update sudo wrapper."
[[ $canonical == "$wrapper" ]] || fail "Trusted no-update sudo wrapper is redirected."
while :; do
[[ ! -L $current ]] || fail "Trusted no-update sudo path contains a symlink."
read -r owner mode < <(/usr/bin/stat -Lc '%u %a' -- "$current") || fail "Cannot inspect the trusted no-update sudo path."
[[ $owner == "0" ]] && (( (8#$mode & 0022) == 0 )) ||
fail "Trusted no-update sudo path is writable by an untrusted account."
[[ $current == "/usr/share/omarchy" ]] && break
current=${current%/*}
done

PATH="${wrapper%/sudo}:/usr/bin:/usr/sbin:/bin:/sbin"
export PATH
}

(( $# > 0 )) || { usage; exit 1; }
user_path="${PATH:-/usr/bin:/bin}"

# Channel switching is a composite privileged workflow. Start cold, pin every
# helper to the installed system paths, and make helper sudo calls non-caching
# before any command that can execute user-controlled Git/tool configuration.
prepare_non_reusable_sudo
trap cleanup_sudo_credentials EXIT
/usr/bin/sudo -k || exit 1

dev_checkout=""
channel="$1"
Expand Down Expand Up @@ -79,21 +157,31 @@ fi
if [[ -n $dev_checkout ]]; then
link_dev_checkout "$dev_checkout"
export OMARCHY_PATH="$dev_checkout"
export PATH="$OMARCHY_PATH/bin:$PATH"
omarchy-state set reboot-required
/usr/bin/omarchy-state set reboot-required
fi

omarchy-refresh-pacman "$pacman_channel"
# The refresh hook is executable user code. Defer it across this composite
# workflow so a detached hook child cannot wait for the package switch or the
# update pipeline to publish a sudo timestamp. The installed fixed path cannot
# be replaced through the caller's PATH.
OMARCHY_SUDO_NO_UPDATE=1 /usr/bin/omarchy-refresh-pacman "$pacman_channel" --defer-hook
# --ask 4 accepts omarchy <-> omarchy-dev replacement prompts without file overwrites.
sudo env OMARCHY_UPDATE_PACMAN=1 pacman -S --needed --noconfirm --ask 4 "${packages[@]}"
/usr/bin/sudo -N -- /usr/bin/env PATH=/usr/bin:/usr/sbin:/bin:/sbin \
OMARCHY_UPDATE_PACMAN=1 /usr/bin/pacman -S --needed --noconfirm --ask 4 "${packages[@]}"

if [[ -z $dev_checkout ]]; then
omarchy-dev-unlink --no-reboot
/usr/bin/omarchy-dev-unlink --no-reboot
export OMARCHY_PATH=/usr/share/omarchy

if (( leaving_dev )); then
omarchy-state set reboot-required
/usr/bin/omarchy-state set reboot-required
fi
fi

omarchy-update -y
PATH="$user_path" OMARCHY_SUDO_NO_UPDATE=1 /usr/bin/omarchy-update -y

# This is the final action. The trusted refresh command re-resolves the source
# root from root-owned /etc/omarchy.conf, invalidates first, and invokes the
# hook exactly once. Its own EXIT cleanup also leaves any hook-authenticated
# credential cold. If an earlier step fails, the deferred hook is skipped.
PATH="$user_path" /usr/bin/omarchy-refresh-pacman --run-deferred-hook
153 changes: 138 additions & 15 deletions bin/omarchy-dev-pkg-test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

set -euo pipefail

PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH
unset BASH_ENV ENV CDPATH GLOBIGNORE
umask 077

if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
cat <<USAGE
Usage: omarchy dev pkg-test [package-name] [path-to-checkout]
Expand Down Expand Up @@ -37,7 +42,7 @@ remove_pkgver_function() {
local pkgbuild="$1"
local tmp="$pkgbuild.tmp"

awk '
/usr/bin/awk '
/^pkgver\(\)[[:space:]]*\{/ {
in_pkgver = 1
depth = 0
Expand All @@ -55,7 +60,7 @@ remove_pkgver_function() {
}
{ print }
' "$pkgbuild" >"$tmp"
mv "$tmp" "$pkgbuild"
/usr/bin/mv "$tmp" "$pkgbuild"
}

dev_package_name() {
Expand All @@ -82,6 +87,15 @@ else
fi
PKGBUILDS_ROOT="${OMARCHY_PKGBUILDS_DIR:-$HOME/Work/omarchy/omarchy-pkgs/pkgbuilds}"

for argument in "${MAKEPKG_ARGS[@]}"; do
case $argument in
-- | --config | --config=* | PACMAN=* | PACMAN+=* | PACMAN_AUTH=* | PACMAN_AUTH+=* | MAKEPKG_CONF=* | BASH_ENV=* | ENV=*)
echo "Error: makepkg argument '$argument' would bypass the fixed privilege boundary." >&2
exit 2
;;
esac
done

if [[ ! -d "$CHECKOUT" ]]; then
echo "Error: checkout not found at $CHECKOUT" >&2
exit 1
Expand All @@ -95,43 +109,152 @@ for PKG in "${PKGS[@]}"; do
fi
done

build_dir=$(mktemp -d -t omarchy-dev-pkg-test.XXXXXX)
trap 'rm -rf "$build_dir"' EXIT
trusted_system_command() {
local command=$1 require_setuid=${2:-false}
local canonical owner mode links

[[ $command == /usr/bin/* && -f $command && -x $command && ! -L $command ]] || return 1
canonical=$(/usr/bin/realpath -e -- "$command") || return 1
[[ $canonical == "$command" ]] || return 1
read -r owner mode links < <(/usr/bin/stat -Lc '%u %a %h' -- "$command") || return 1
[[ $owner == 0 && $links == 1 ]] || return 1
(( (8#$mode & 0022) == 0 )) || return 1
[[ $require_setuid != true ]] || (( (8#$mode & 04000) != 0 ))
}

for command in /usr/bin/realpath /usr/bin/stat /usr/bin/sudo /usr/bin/pacman /usr/bin/makepkg /usr/bin/setpriv; do
require_setuid=false
[[ $command != /usr/bin/sudo ]] || require_setuid=true
if ! trusted_system_command "$command" "$require_setuid"; then
echo "Refusing the package build because $command is not a trusted system command." >&2
exit 1
fi
done

/usr/bin/sudo -k >/dev/null || {
echo "Unable to start the package build with a cold sudo credential state." >&2
exit 1
}
if ! /usr/bin/sudo -N -V >/dev/null 2>&1; then
echo "This sudo does not support --no-update; refusing to run package build code." >&2
exit 1
fi

build_dir=$(/usr/bin/mktemp -d -t omarchy-dev-pkg-test.XXXXXX)

cleanup() {
local status=$?
trap - EXIT HUP INT TERM
/usr/bin/rm -rf -- "$build_dir"
if ! /usr/bin/sudo -k >/dev/null 2>&1; then
echo "Failed to invalidate sudo credentials after the package build." >&2
(( status != 0 )) || status=1
fi
exit "$status"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM

# pkgver=dev.<sha>[.dirty] so pacman -Q makes the source obvious.
short_sha=$(git -C "$CHECKOUT" rev-parse --short HEAD 2>/dev/null || echo "local")
short_sha=$(/usr/bin/git -C "$CHECKOUT" rev-parse --short HEAD 2>/dev/null || echo "local")
dirty=""
if [[ -d "$CHECKOUT/.git" ]] && [[ -n "$(git -C "$CHECKOUT" status --porcelain)" ]]; then
if [[ -d "$CHECKOUT/.git" ]] && [[ -n "$(/usr/bin/git -C "$CHECKOUT" status --porcelain)" ]]; then
dirty=".dirty"
fi
new_pkgver="dev.${short_sha}${dirty}"

dependency_name() {
local dependency=$1 name=${1%%[<>=]*}
[[ $name =~ ^[a-z0-9][a-z0-9@._+:-]*$ ]] || return 1
printf '%s\n' "$name"
}

install_build_dependencies() {
local srcinfo=$1 architecture dependency status missing_output name
local dependencies=() missing=()
local -A seen=()
architecture=$(/usr/bin/uname -m)

while IFS= read -r line; do
if [[ $line =~ ^$'\t'(depends|makedepends|checkdepends)(_([a-zA-Z0-9_]+))?\ =\ (.+)$ ]]; then
[[ -z ${BASH_REMATCH[3]} || ${BASH_REMATCH[3]} == "$architecture" ]] || continue
dependency=${BASH_REMATCH[4]}
dependency_name "$dependency" >/dev/null || {
echo "Error: unsafe dependency in generated .SRCINFO: $dependency" >&2
return 1
}
dependencies+=("$dependency")
fi
done <"$srcinfo"

(( ${#dependencies[@]} )) || return 0
if missing_output=$(/usr/bin/pacman -T -- "${dependencies[@]}"); then
return 0
else
status=$?
fi
(( status == 127 )) || return "$status"

while IFS= read -r dependency; do
[[ -n $dependency ]] || continue
name=$(dependency_name "$dependency") || {
echo "Error: pacman returned an unsafe dependency name: $dependency" >&2
return 1
}
if [[ -z ${seen[$name]+x} ]]; then
seen[$name]=1
missing+=("$name")
fi
done <<<"$missing_output"
(( ${#missing[@]} )) || return 1
/usr/bin/sudo -N -- /usr/bin/pacman -S --asdeps --needed --noconfirm -- "${missing[@]}"
}

built_pkgs=()
for PKG in "${PKGS[@]}"; do
PKGBUILD_DIR="$PKGBUILDS_ROOT/$PKG"
package_build_dir="$build_dir/$PKG"
mkdir -p "$package_build_dir"
cp -a "$PKGBUILD_DIR/." "$package_build_dir/"
/usr/bin/mkdir -p "$package_build_dir"
/usr/bin/cp -a "$PKGBUILD_DIR/." "$package_build_dir/"

remove_pkgver_function "$package_build_dir/PKGBUILD"
sed -i "s/^pkgver=.*/pkgver=${new_pkgver}/" "$package_build_dir/PKGBUILD"
/usr/bin/sed -i "s/^pkgver=.*/pkgver=${new_pkgver}/" "$package_build_dir/PKGBUILD"

echo "Building $PKG ${new_pkgver} from $CHECKOUT"
echo " build dir: $package_build_dir"
echo " PKGBUILD : $PKGBUILD_DIR/PKGBUILD"
echo

srcinfo="$package_build_dir/.SRCINFO.omarchy"
(
cd "$package_build_dir"
OMARCHY_SRC="$CHECKOUT" makepkg -s --skipchecksums --noconfirm "${MAKEPKG_ARGS[@]}"
OMARCHY_SRC="$CHECKOUT" /usr/bin/setpriv --no-new-privs /usr/bin/makepkg --printsrcinfo
) >"$srcinfo"
install_build_dependencies "$srcinfo"

(
cd "$package_build_dir"
# Dependency installation is deliberately separate. Even if a PKGBUILD
# mutates makepkg's internal NODEPS/PACMAN_AUTH variables, no_new_privs in
# the fixed launcher prevents any setuid program from granting it root.
OMARCHY_SRC="$CHECKOUT" /usr/bin/setpriv --no-new-privs /usr/bin/makepkg \
--nodeps --skipchecksums --noconfirm "${MAKEPKG_ARGS[@]}"
)

# Install separately so we can pass --overwrite='*' (makepkg -i can't).
# Dev builds frequently conflict with files left behind by previous
# script-installed Omarchy versions; the build is the authoritative state.
built_pkg=$(ls -t "$package_build_dir"/*.pkg.tar.* 2>/dev/null | grep -v '\.sig$' | head -1)
built_pkg=""
for candidate in "$package_build_dir"/*.pkg.tar.*; do
[[ -f $candidate && $candidate != *.sig ]] || continue
[[ -z $built_pkg || $candidate -nt $built_pkg ]] && built_pkg=$candidate
done
if [[ -z $built_pkg ]]; then
echo "Error: no built package found in $package_build_dir" >&2
exit 1
fi
sudo pacman -U --noconfirm --overwrite='*' "$built_pkg"
built_pkgs+=("$built_pkg")
done

# Install the built package set in one transaction so its
# provides/conflicts/dependencies resolve as they do from the repository.
/usr/bin/sudo -N -- /usr/bin/pacman -U --noconfirm -- "${built_pkgs[@]}"
Loading