WIP: Adds kanatctl to runs/after/system, adds ability for kanatactl to install service as user or system service.

This commit is contained in:
2025-11-10 11:36:21 -05:00
parent 74011a46bc
commit 3c98a008c8
5 changed files with 157 additions and 77 deletions

View File

@@ -15,7 +15,7 @@ DEV_ENV=${DEV_ENV:-""}
KBD=${KBD:-""} # Keyboard config to use, either "voyager" or "macbook" KBD=${KBD:-""} # Keyboard config to use, either "voyager" or "macbook"
usage() { usage() {
cat <<EOF cat <<EOF
Manages kanata qmk keyboard program, which needs to be built locally. Currently the linux builds Manages kanata qmk keyboard program, which needs to be built locally. Currently the linux builds
are only for x86. Also manages the kanata systemd service. are only for x86. Also manages the kanata systemd service.
@@ -41,17 +41,17 @@ EOF
# Logging utility function, use in place of echo. # Logging utility function, use in place of echo.
log() { log() {
logging log --source "$THIS_FILE" "$@" logging log --source "$THIS_FILE" "$@"
} }
# Bootstrap a new machine, by building and installing the kanata executable, # Bootstrap a new machine, by building and installing the kanata executable,
# installing the systemd service files, and enable / start the service. # installing the systemd service files, and enable / start the service.
bootstrap() { bootstrap() {
log "Bootstrapping new system..." log "Bootstrapping new system..."
$THIS_FILE pkg install $THIS_FILE pkg install
$THIS_FILE service install $THIS_FILE service install
$THIS_FILE service enable $THIS_FILE service enable
$THIS_FILE service start $THIS_FILE service start
} }
################################################################################ ################################################################################
@@ -63,29 +63,29 @@ source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL" setup-logging "$LOG_FILE" "$LOG_LABEL"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0 usage && exit 0
elif [[ $1 == "bootstrap" ]]; then elif [[ $1 == "bootstrap" ]]; then
bootstrap && exit 0 bootstrap && exit 0
elif [[ $1 == "config" ]]; then elif [[ $1 == "config" ]]; then
shift shift
THIS="$THIS config" $SCRIPTS/utils/kanatactl/config "$@" THIS="$THIS config" $SCRIPTS/utils/kanatactl/config "$@"
exit $? exit $?
elif [[ $1 == "service" ]]; then elif [[ $1 == "service" ]]; then
shift shift
THIS="$THIS service" $SCRIPTS/utils/kanatactl/service "$@" THIS="$THIS service" $SCRIPTS/utils/kanatactl/service "$@"
exit $? exit $?
elif [[ $1 == "pkg" ]]; then elif [[ $1 == "pkg" ]]; then
shift shift
THIS="$THIS pkg" $SCRIPTS/utils/kanatactl/pkg "$@" THIS="$THIS pkg" $SCRIPTS/utils/kanatactl/pkg "$@"
exit $? exit $?
elif [[ $1 == "logs" ]]; then elif [[ $1 == "logs" ]]; then
bat ${LOG_DIR:-/tmp/logs}/$LOG_FILE && exit 0 bat ${LOG_DIR:-/tmp/logs}/$LOG_FILE && exit 0
elif [[ $1 == "update" ]]; then elif [[ $1 == "update" ]]; then
install_or_update "Updating" && exit 0 install_or_update "Updating" && exit 0
else else
break break
fi fi
done done
# If we've made it here, then we didn't handle the command. # If we've made it here, then we didn't handle the command.

View File

@@ -11,8 +11,15 @@ THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
DEV_ENV=${DEV_ENV:-""} DEV_ENV=${DEV_ENV:-""}
declare user_flag system_flag prompt_flag mode
declare -a args
user_flag="1"
system_flag="0"
prompt_flag="0"
mode=""
usage() { usage() {
cat <<EOF cat <<EOF
Manages the kanata systemd service. Manages the kanata systemd service.
USAGE: USAGE:
@@ -21,6 +28,9 @@ USAGE:
FLAGS: FLAGS:
-h | --help: Show this help page. -h | --help: Show this help page.
--user: Setup systemd service as a user service (default).
--system: Setup systemd service as a system service.
--prompt: Prompt the user to install service as system or user service.
COMMANDS: COMMANDS:
enable: Enable the kanata service. enable: Enable the kanata service.
@@ -40,52 +50,90 @@ EOF
# Logging utility function, use in place of echo. # Logging utility function, use in place of echo.
log() { log() {
logging log --source "$THIS_FILE" "$@" logging log --source "$THIS_FILE" "$@"
} }
enable_service() { enable_service() {
log "Enabling kanata service..." log "Enabling kanata service..."
sudo systemctl enable kanata.service if [[ $user_flag == "1" ]]; then
systemctl --user enable --now kanata.service
else
sudo systemctl enable --now kanata.service
fi
} }
get_status() { get_status() {
log "Getting kanata service status..." log "Getting kanata service status..."
systemctl status kanata.service systemctl status kanata.service
}
prompt_user() {
log "Prompting user for how they would like to setup kanata systemd service..."
local choice
choice=$(echo -e "user\nsystem" | fzf --header "How would you like to setup the kanata systemd service?")
if [[ $choice == "system" ]]; then
user_flag="0"
system_flag="1"
elif [[ $choice == "user" ]]; then
user_flag="1"
system_flag="0"
else
exit 1
fi
} }
install_service() { install_service() {
if [[ -z $DEV_ENV ]]; then local service_dir="user"
log --error "DEV_ENV is not set properly." && exit 1
fi
# Ensure the configuration is copied / setup otherwise the keyboard may not work. if [[ -z $DEV_ENV ]]; then
"$SCRIPTS/kanatactl" config install "$@" log --error "DEV_ENV is not set properly." && exit 1
fi
log "Starting install service..."
log "Installing kanata service..." # Ensure the configuration is copied / setup otherwise the keyboard may not work.
[[ -f /etc/systemd/system/kanata.service ]] && sudo rm -rf /etc/systemd/system/kanata.service "$SCRIPTS/kanatactl" config install "$@"
sudo cp "$DEV_ENV/env/etc/systemd/system/kanata.service" /etc/systemd/system
sudo systemctl daemon-reload # Prompt the user for how they would like to install the systemd service, if the
# prompt flag is set.
[[ $prompt_flag == "1" ]] && prompt_user || exit 1
[[ $system_flag == "1" ]] && service_dir="system"
log "Installing kanata service as: '$service_dir' service..."
if [[ $service_dir == "system" ]]; then
[[ -f /etc/systemd/system/kanata.service ]] && sudo rm -rf /etc/systemd/system/kanata.service
sudo cp "$DEV_ENV/env/etc/systemd/system/kanata.service" /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable --now kanata.service
else
[[ -f ~/.config/systemd/user/kanata.service ]] && rm -rf ~/.config/systemd/user/kanata.service
mkdir -p ~/.config/systemd/user &>/dev/null
cp "$DEV_ENV/env/etc/systemd/user/kanata.service" ~/.config/systemd/user
systemctl --user daemon-reload
systemctl --user enable --now kanata.service
fi
} }
start_service() { start_service() {
log "Starting kanata service..." log "Starting kanata service..."
systemctl start kanata.service systemctl start kanata.service
} }
stop_service() { stop_service() {
log "Stopping kanata service..." log "Stopping kanata service..."
sudo systemctl stop kanata.service sudo systemctl stop kanata.service
} }
disable_service() { disable_service() {
log "Disabling kanata service..." log "Disabling kanata service..."
stop_service stop_service
sudo systemctl disable kanata.service sudo systemctl disable kanata.service
} }
restart_service() { restart_service() {
log "Restarting kanata service..." log "Restarting kanata service..."
sudo systemctl restart kanata.service sudo systemctl restart kanata.service
} }
################################################################################ ################################################################################
@@ -96,26 +144,42 @@ restart_service() {
source "$SCRIPTS/hypr/logging" source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL" setup-logging "$LOG_FILE" "$LOG_LABEL"
# Parse flags / arguments.
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0 usage && exit 0
elif [[ $1 == "disable" ]]; then elif [[ $1 == "--user" ]]; then
disable_service && exit $? user_flag="1"
elif [[ $1 == "enable" ]]; then elif [[ $1 == "--system" ]]; then
enable_service && exit $? system_flag="1"
elif [[ $1 == "install" ]]; then elif [[ $1 == "--prompt" ]]; then
shift prompt_flag="1"
install_service "$@" && exit $? # Set first non-flag to the mode / subcommand.
elif [[ $1 == "start" ]]; then elif [[ -z $mode ]]; then
start_service && exit $? mode="$1"
elif [[ $1 == "status" ]]; then # Add any other arguments to the 'args' array.
get_status && exit $? else
elif [[ $1 == "stop" ]]; then args+=("$1")
stop_service && exit $? fi
elif [[ $1 == "restart" ]]; then shift
restart_service && exit $?
fi
done done
# If we made it here, then none of the subcommands handled the args. # Handle mode.
usage && exit 1 if [[ $mode == "disable" ]]; then
disable_service && exit $?
elif [[ $mode == "enable" ]]; then
enable_service && exit $?
elif [[ $mode == "install" ]]; then
install_service "${args[@]}" && exit $?
elif [[ $mode == "start" ]]; then
start_service && exit $?
elif [[ $mode == "status" ]]; then
get_status && exit $?
elif [[ $mode == "stop" ]]; then
stop_service && exit $?
elif [[ $mode == "restart" ]]; then
restart_service && exit $?
else
# If we made it here, then none of the subcommands handled the args.
usage && exit 1
fi

12
env/etc/systemd/user/kanata.service vendored Normal file
View File

@@ -0,0 +1,12 @@
[Unit]
Description=Kanata Service
Documentation=https://github.com/jtroo/kanata
[Service]
Environment=PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin
Type=simple
ExecStart=/bin/sh -c 'exec /usr/bin/kanata --cfg /home/michael/.config/kanata/config.kbd'
Restart=no
[Install]
WantedBy=default.target

View File

@@ -8,6 +8,7 @@ install() {
log " Setting up espanso." log " Setting up espanso."
sudo setcap "cap_dac_override+p" "$(which espanso)" sudo setcap "cap_dac_override+p" "$(which espanso)"
espanso service register espanso service register
systemctl --user daemon-reload
espanso service start espanso service start
} }

View File

@@ -4,16 +4,19 @@ set -e
set -o nounset set -o nounset
set -o pipefail set -o pipefail
SCRIPTS="${DEV_ENV}/env/.local/scripts"
install() { install() {
log " Setting user shell to 'zsh'." log " Setting user shell to 'zsh'."
sudo chsh --shell "$(which zsh)" sudo chsh --shell "$(which zsh)"
log " Enabling up systemd services." log " Enabling up systemd services."
systemctl --user daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now pcscd.service
sudo systemctl enable --now firewalld.service
systemctl --user enable --now logout-task.service systemctl --user enable --now logout-task.service
systemctl --user enable --now battery-monitor.timer systemctl --user enable --now battery-monitor.timer
systemctl --user enable --now tmux-kill-sessions.timer systemctl --user enable --now tmux-kill-sessions.timer
sudo systemctl enable --now pcscd.service SCRIPTS="$SCRIPTS" "$SCRIPTS/kanatactl" service install --prompt
sudo systemctl enable --now firewalld.service
} }
arg=${1:-""} arg=${1:-""}