From d6985ded7e86c5c74503f1ab3757d90270aef8a6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 8 Jan 2024 23:37:31 +0100 Subject: [PATCH] Init --- .envrc | 3 + .gitignore | 4 + Makefile | 20 +++++ autostart-manage/bashly.yml | 99 +++++++++++++++++++++++++ autostart-manage/completions_command.sh | 3 + autostart-manage/exec_command.sh | 7 ++ autostart-manage/info_command.sh | 9 +++ autostart-manage/lib/colors.sh | 42 +++++++++++ autostart-manage/lib/common.sh | 57 ++++++++++++++ autostart-manage/list_command.sh | 3 + autostart-manage/log_command.sh | 3 + autostart-manage/run_command.sh | 3 + autostart-manage/run_wayland_command.sh | 5 ++ autostart-manage/run_xorg_command.sh | 12 +++ autostart-manage/sync_command.sh | 14 ++++ autostart-manage/systemctl.sh | 9 +++ settings.yml | 49 ++++++++++++ shell.nix | 6 ++ 18 files changed, 348 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 autostart-manage/bashly.yml create mode 100644 autostart-manage/completions_command.sh create mode 100644 autostart-manage/exec_command.sh create mode 100644 autostart-manage/info_command.sh create mode 100644 autostart-manage/lib/colors.sh create mode 100644 autostart-manage/lib/common.sh create mode 100644 autostart-manage/list_command.sh create mode 100644 autostart-manage/log_command.sh create mode 100644 autostart-manage/run_command.sh create mode 100644 autostart-manage/run_wayland_command.sh create mode 100644 autostart-manage/run_xorg_command.sh create mode 100644 autostart-manage/sync_command.sh create mode 100644 autostart-manage/systemctl.sh create mode 100644 settings.yml create mode 100644 shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..f45b145 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +use nix + +printf '\nPlease set and export $BASHLY_SOURCE_DIR\n\n' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff7242 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.target +output/ + +autostart-manage/lib/send_completions.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..51360b5 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +target: + @echo "Enter target directory:" + @read target; \ + echo $$target > .target + +get-target: + $(eval TARGET := $(shell cat .target)) + +output-dir: + mkdir -p ./output + +generate: get-target output-dir + BASHLY_SOURCE_DIR=$(TARGET) bashly generate + +completions: generate + BASHLY_SOURCE_DIR=$(TARGET) bashly add --force completions_script ./output/_$(TARGET) + +deploy: completions + cp -f ./output/$(TARGET) ~/.local/bin/ + cp -f ./output/_$(TARGET) ~/.config/completionsrc.d/ diff --git a/autostart-manage/bashly.yml b/autostart-manage/bashly.yml new file mode 100644 index 0000000..e65abd6 --- /dev/null +++ b/autostart-manage/bashly.yml @@ -0,0 +1,99 @@ +name: autostart-manage +help: Manage autostart +version: 0.1.0 + +dependencies: + yq: + command: [tomlq] + help: please install yq (https://github.com/kislyuk/yq) + +environment_variables: +- name: HOSTNAME + help: The hostname to use. We will try to get the value automatically. + +commands: + - name: completions + help: Generate bash completions + + - name: list + help: List available programs + - name: info + help: Get the current status of all programs + - name: sync + help: Remove all programs from autostart and the re-enable all + - name: enable + help: Add a single program to autostart + args: + - name: program + required: true + help: Program to add to autostart + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: disable + help: Remove a single program from autostart + args: + - name: program + required: true + help: Program to add to autostart + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: restart + help: Restart programs from autostart + args: + - name: program + help: Program to add to autostart + default: "*" + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: start + help: Start programs from autostart + args: + - name: program + help: Program to start to autostart + default: "*" + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: status + help: Check status of programs from autostart + args: + - name: program + help: Program to check status on + default: "*" + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: stop + help: Stop programs from autostart + args: + - name: program + help: Program to stop + default: "*" + completions: + - $(autostart-manage list) + filename: systemctl.sh + - name: log + help: Show the log for a single program from autostart + args: + - name: program + required: true + help: Program to log + completions: + - $(autostart-manage list) + - name: exec + help: Execute a single program from autostart + args: + - name: program + required: true + help: Program to execute + completions: + - $(autostart-manage list) + - name: run + help: Run all programs + - name: run-wayland + help: Run all the wayland specific tasks and all programs + - name: run-xorg + help: Run all the xorg specific tasks and all programs diff --git a/autostart-manage/completions_command.sh b/autostart-manage/completions_command.sh new file mode 100644 index 0000000..0d10993 --- /dev/null +++ b/autostart-manage/completions_command.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +send_completions diff --git a/autostart-manage/exec_command.sh b/autostart-manage/exec_command.sh new file mode 100644 index 0000000..2842069 --- /dev/null +++ b/autostart-manage/exec_command.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +program=${args[program]:?} + +cmd=$(_get_autostart_cmd "$program") +sleep "$(_get_autostart_delay "$program")" +bash -c "$cmd" diff --git a/autostart-manage/info_command.sh b/autostart-manage/info_command.sh new file mode 100644 index 0000000..1f895e3 --- /dev/null +++ b/autostart-manage/info_command.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +autostart_units=() +while IFS='' read -r line +do + autostart_units+=("$line") +done < <(_list) + +_echo_table "${autostart_units[@]}" | column -t -s$'\t' --table-columns 'Unit,Enabled?,Active?,Command' diff --git a/autostart-manage/lib/colors.sh b/autostart-manage/lib/colors.sh new file mode 100644 index 0000000..cbdc015 --- /dev/null +++ b/autostart-manage/lib/colors.sh @@ -0,0 +1,42 @@ +## Color functions [@bashly-upgrade colors] +## This file is a part of Bashly standard library +## +## Usage: +## Use any of the functions below to color or format a portion of a string. +## +## echo "before $(red this is red) after" +## echo "before $(green_bold this is green_bold) after" +## +## Color output will be disabled if `NO_COLOR` environment variable is set +## in compliance with https://no-color.org/ +## +print_in_color() { + local color="$1" + shift + if [[ -z ${NO_COLOR+x} ]]; then + printf "$color%b\e[0m\n" "$*" + else + printf "%b\n" "$*" + fi +} + +red() { print_in_color "\e[31m" "$*"; } +green() { print_in_color "\e[32m" "$*"; } +yellow() { print_in_color "\e[33m" "$*"; } +blue() { print_in_color "\e[34m" "$*"; } +magenta() { print_in_color "\e[35m" "$*"; } +cyan() { print_in_color "\e[36m" "$*"; } +bold() { print_in_color "\e[1m" "$*"; } +underlined() { print_in_color "\e[4m" "$*"; } +red_bold() { print_in_color "\e[1;31m" "$*"; } +green_bold() { print_in_color "\e[1;32m" "$*"; } +yellow_bold() { print_in_color "\e[1;33m" "$*"; } +blue_bold() { print_in_color "\e[1;34m" "$*"; } +magenta_bold() { print_in_color "\e[1;35m" "$*"; } +cyan_bold() { print_in_color "\e[1;36m" "$*"; } +red_underlined() { print_in_color "\e[4;31m" "$*"; } +green_underlined() { print_in_color "\e[4;32m" "$*"; } +yellow_underlined() { print_in_color "\e[4;33m" "$*"; } +blue_underlined() { print_in_color "\e[4;34m" "$*"; } +magenta_underlined() { print_in_color "\e[4;35m" "$*"; } +cyan_underlined() { print_in_color "\e[4;36m" "$*"; } diff --git a/autostart-manage/lib/common.sh b/autostart-manage/lib/common.sh new file mode 100644 index 0000000..758403e --- /dev/null +++ b/autostart-manage/lib/common.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +_systemctl () { + systemctl --user "${@:2}" "autostart@$1.service" +} + +_query_autostart_toml() { + tomlq -r --arg host "$HOSTNAME" \ + '.hosts[$host].groups as $groups | .apps | to_entries[] | select( + (.value.hosts | contains([$host])) or + ([.value.group] | inside($groups)) + ) | '"$1" \ + "$XDG_CONFIG_HOME/autostart.toml" +} + +_list () { + _query_autostart_toml '.value.alias // .key' +} + +_autostart_run_graphical () { + pass x # Try to unlock yubikey asap + + start-audio pipewire + wait-for-service "network-online.target" + + # Execute only if not already running + # Don't match keepassxc-proxy + if ! (pgrep -l keepassxc | grep -v prox) >/dev/null + then + if pass x + then + (pass keepass | head -n 1 | keepassxc --pw-stdin ~/sync/passwords.kdbx) & + fi + fi + + autoinstall graphical + autostart-manage run +} + +_echo_table () { + for unit in "$@" + do + printf "%s\t%s\t%s\t%s\n" \ + "$unit" \ + "$(_systemctl "$unit" is-enabled)" \ + "$(_systemctl "$unit" is-active)" \ + "$(_get_autostart_cmd "$unit")" + done +} + +_get_autostart_cmd () { + _query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.command' +} + +_get_autostart_delay () { + _query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.delay // 0' +} diff --git a/autostart-manage/list_command.sh b/autostart-manage/list_command.sh new file mode 100644 index 0000000..0986740 --- /dev/null +++ b/autostart-manage/list_command.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +_list diff --git a/autostart-manage/log_command.sh b/autostart-manage/log_command.sh new file mode 100644 index 0000000..7d9657c --- /dev/null +++ b/autostart-manage/log_command.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +journalctl --user -fu "autostart@${args[program]:?}.service" diff --git a/autostart-manage/run_command.sh b/autostart-manage/run_command.sh new file mode 100644 index 0000000..46fcb48 --- /dev/null +++ b/autostart-manage/run_command.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +systemctl --user start autostart.target diff --git a/autostart-manage/run_wayland_command.sh b/autostart-manage/run_wayland_command.sh new file mode 100644 index 0000000..6e2f6cb --- /dev/null +++ b/autostart-manage/run_wayland_command.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +_autostart_run_graphical + +killall -SIGUSR2 waybar diff --git a/autostart-manage/run_xorg_command.sh b/autostart-manage/run_xorg_command.sh new file mode 100644 index 0000000..b9ee3ae --- /dev/null +++ b/autostart-manage/run_xorg_command.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +feh --bg-fill "$XDG_PICTURES_DIR/wallpaper/active_wallpaper" + +"$HOME/.config/polybar/launch.sh" & +"$HOME/.config/bspwm/focus-voip.py" & + +numlockx on +xsetroot -cursor_name left_ptr +xrdb "$HOME/.Xresources" + +_autostart_run_graphical diff --git a/autostart-manage/sync_command.sh b/autostart-manage/sync_command.sh new file mode 100644 index 0000000..d08ac71 --- /dev/null +++ b/autostart-manage/sync_command.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +rm "$HOME/.config/systemd/user/autostart.target.wants/"* + +autostart_units=() +while IFS='' read -r line +do + autostart_units+=("$line") +done < <(_list) + +for unit in "${autostart_units[@]}" +do + _systemctl "$unit" add-wants autostart.target +done diff --git a/autostart-manage/systemctl.sh b/autostart-manage/systemctl.sh new file mode 100644 index 0000000..7ae9a38 --- /dev/null +++ b/autostart-manage/systemctl.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +systemctl_args=("$action") +if [[ $action == "enable" ]]; then + systemctl_args=("add-wants" "autostart.target") +fi + +#shellcheck disable=SC2068 +_systemctl "${args[program]:?}" "${systemctl_args[@]}" diff --git a/settings.yml b/settings.yml new file mode 100644 index 0000000..63e7150 --- /dev/null +++ b/settings.yml @@ -0,0 +1,49 @@ +# The path to bashly.yml +config_path: "%{source_dir}/bashly.yml" + +# The path to use for creating the bash script +target_dir: output + +# The path to use for common library files, relative to source_dir +lib_dir: lib + +# The path to use for command files, relative to source_dir +# When set to nil (~), command files will be placed directly under source_dir +# When set to any other string, command files will be placed under this +# directory, and each command will get its own subdirectory +commands_dir: ~ + +# Configure the bash options that will be added to the initialize function: +# strict: true Bash strict mode (set -euo pipefail) +# strict: false Only exit on errors (set -e) +# strict: '' Do not add any 'set' directive +# strict: Add any other custom 'set' directive +strict: true + +# When true, the generated script will use tab indentation instead of spaces +# (every 2 leading spaces will be converted to a tab character) +tab_indent: true + +# When true, the generated script will consider any argument in the form of +# `-abc` as if it is `-a -b -c`. +compact_short_flags: true + +# Set to 'production' or 'development': +# env: production Generate a smaller script, without file markers +# env: development Generate with file markers +env: development + +# The extension to use when reading/writing partial script snippets +partials_extension: sh + +# Display various usage elements in color by providing the name of the color +# function. The value for each property is a name of a function that is +# available in your script, for example: `green` or `bold`. +# You can run `bashly add colors` to add a standard colors library. +# This option cannot be set via environment variables. +usage_colors: + caption: blue + command: green + arg: red + flag: yellow + environment_variable: cyan diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..121edf5 --- /dev/null +++ b/shell.nix @@ -0,0 +1,6 @@ +with import {}; +mkShell { + nativeBuildInputs = [ + bashly + ]; +}