Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions bin/omarchy
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GROUP_DESCRIPTIONS[toggle]="Toggle Omarchy features"
GROUP_DESCRIPTIONS[transcode]="Image and video transcoding"
GROUP_DESCRIPTIONS[tui]="Terminal UI launchers"
GROUP_DESCRIPTIONS[update]="Omarchy and system updates"
GROUP_DESCRIPTIONS[user]="Login user management"
GROUP_DESCRIPTIONS[version]="Version and channel information"
GROUP_DESCRIPTIONS[voxtype]="Voxtype dictation"
GROUP_DESCRIPTIONS[weather]="Weather status"
Expand Down
174 changes: 174 additions & 0 deletions bin/omarchy-user-add
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/bin/bash

# omarchy:summary=Create a login user with groups and password (sudo managed via 'omarchy user privileges')
# omarchy:args=[username] [--groups g1,g2] [--yes]
# omarchy:examples=omarchy user add | omarchy user add alice --groups wheel --yes
# omarchy:requires-sudo=true

set -euo pipefail

PROG="omarchy user add"
ASSUME_YES=0
SKIP_PASSWORD=0
USERNAME=""
GROUPS_ARG=""
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

fail() {
echo "$PROG: $*" >&2
exit 1
}

interactive() {
[[ -t 0 && -t 1 ]]
}

show_logo() {
if interactive && [[ -n ${OMARCHY_PATH:-} && -f $OMARCHY_PATH/logo.txt ]]; then
echo -e "\033[32m"
cat <"$OMARCHY_PATH/logo.txt"
echo -e "\033[0m"
echo
fi
}

confirm() {
local prompt="$1"
if ((ASSUME_YES)); then
return 0
fi
if interactive; then
gum confirm "$prompt"
else
fail "refusing to continue without confirmation; pass --yes"
fi
}

need_gum() {
omarchy-cmd-present gum || fail "gum is required for the interactive menu"
}

# Groups worth offering on an Omarchy desktop. wheel is the admin group;
# the rest grant hardware/device access. Filtered at runtime to groups
# that actually exist on this machine.
# Note: wheel is omitted here; sudo is managed exclusively via 'omarchy user privileges'.
CURATED_GROUPS="video audio optical storage input scanner network power uucp"

existing_groups() {
local g out=""
for g in $CURATED_GROUPS; do
if getent group "$g" >/dev/null; then
out+="$g"$'\n'
fi
done
printf '%s' "$out"
}

while (($# > 0)); do
case "$1" in
--groups)
if [[ -z ${2:-} || $2 == -* ]]; then
fail "--groups needs a comma-separated list"
fi
GROUPS_ARG="$2"
shift 2
;;
--skip-password)
SKIP_PASSWORD=1
shift
;;
--yes | -y)
ASSUME_YES=1
shift
;;
-h | --help)
echo "Usage: $PROG [username] [--groups g1,g2] [--skip-password] [--yes]"
echo
echo "Creates a login user, puts it in the given groups (default: none),"
echo "sets its password via passwd (or skip it with --skip-password). Sudo is"
echo "managed separately via 'omarchy user privileges'."
echo
echo "With no arguments it walks you through each step with gum."
exit 0
;;
-*)
fail "unknown option: $1 (try --help)"
;;
*)
if [[ -n $USERNAME ]]; then
fail "unexpected argument: $1"
fi
USERNAME="$1"
shift
;;
esac
done

show_logo

if [[ -z $USERNAME ]]; then
interactive || fail "a username is required"
need_gum
USERNAME=$(gum input --placeholder "alice" --prompt "Username: ") || fail "cancelled"
[[ -n $USERNAME ]] || fail "nothing entered"
fi
valid_username() {
[[ $1 =~ ^[a-z_][a-z0-9_-]{0,31}$ ]]
}
valid_username "$USERNAME" || fail "invalid username '$USERNAME' (lowercase letters, digits, _ or -)"
is_reserved() {
local reserved='^(root|bin|daemon|mail|ftp|http|nobody|dbus|systemd-coredump|systemd-network|systemd-oom|systemd-journal-remote|systemd-resolve|systemd-timesync|tss|uuidd|alpm|git|avahi|cups|cups-browsed|lp|_talkd|polkitd|rtkit|qemu|brltty|gluster|rpc|libvirt-qemu|pcscd|nvidia-persistenced|sddm)$'
[[ $1 =~ $reserved ]]
}
is_reserved "$USERNAME" && fail "username '$USERNAME' is reserved for the system"
id "$USERNAME" &>/dev/null && fail "user '$USERNAME' already exists (see omarchy user groups)"

if [[ -z $GROUPS_ARG ]]; then
if interactive; then
need_gum
GROUPS_ARG=$(existing_groups | gum choose --no-limit --header="Groups for $USERNAME? (space to toggle)") || fail "cancelled"
GROUPS_ARG=$(printf '%s' "$GROUPS_ARG" | tr '\n' ',' | sed 's/,$//')
else
GROUPS_ARG=""
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
fi
fi
GROUPS_CSV=""
if [[ -n $GROUPS_ARG ]]; then
IFS=',' read -ra GROUP_ARR <<<"$GROUPS_ARG"
for g in "${GROUP_ARR[@]}"; do
[[ -n $g ]] || fail "empty group name in '$GROUPS_ARG'"
getent group "$g" >/dev/null || fail "unknown group: $g"
done
GROUPS_CSV=$(IFS=,; printf '%s' "${GROUP_ARR[*]}")
fi

if ((SKIP_PASSWORD == 0)) && ! interactive; then
fail "setting a password needs a terminal; pass --skip-password or run interactively"
fi

if [[ -x /usr/bin/bash ]]; then
USER_SHELL=/usr/bin/bash
else
USER_SHELL=/bin/bash
fi

echo
echo "About to create:"
echo " user: $USERNAME (shell $USER_SHELL)"
echo " groups: ${GROUPS_CSV:-<none>}"
confirm "Create user $USERNAME?" || fail "aborted"

sudo useradd -m -s "$USER_SHELL" "$USERNAME"
if [[ -n $GROUPS_CSV ]]; then
sudo usermod -aG "$GROUPS_CSV" "$USERNAME"
fi

if ((SKIP_PASSWORD)); then
echo "Skipped password (--skip-password). The greeter needs one: run 'sudo passwd $USERNAME'."
else
echo "Set the password for $USERNAME:"
sudo passwd "$USERNAME"
fi

echo
echo "Created $USERNAME: $(id "$USERNAME")"
echo "First graphical login provisions the desktop (or run 'omarchy finalize user' as $USERNAME)."
45 changes: 45 additions & 0 deletions bin/omarchy-user-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# omarchy:hidden=true
# omarchy:summary=Provide internal helpers for managing login users and their sudo access

if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
echo "omarchy-user-functions is an internal function library." >&2
exit 64
fi

# Callers define fail(). Login users are real accounts (UID 1000..59999).

caller_uid() {
id -u "${SUDO_USER:-$(id -un)}"
}

is_login_uid() {
(($1 >= 1000 && $1 < 60000))
}

in_group() {
[[ " $(id -nG "$1") " == *" $2 "* ]]
}

# Refuses to drop wheel from the account running the command, from a system
# account, or from the last login user in wheel.
guard_wheel_removal() {
local username=$1 member uid count=0

uid=$(id -u "$username")
is_login_uid "$uid" || fail "refusing to change wheel for system account '$username' (UID $uid)"
if ((uid == $(caller_uid))); then
fail "refusing to remove wheel from your own account"
fi

for member in $(getent group wheel | cut -d: -f4 | tr ',' ' '); do
uid=$(id -u "$member" 2>/dev/null) || continue
if is_login_uid "$uid"; then
((++count))
fi
done
if ((count <= 1)); then
fail "refusing to remove wheel: would leave the wheel group with no login users"
fi
}
Loading