Rework autostart manager
This commit is contained in:
		
							parent
							
								
									ccd4c85027
								
							
						
					
					
						commit
						97e4aa0070
					
				
					 3 changed files with 68 additions and 35 deletions
				
			
		
							
								
								
									
										1
									
								
								.bin/.e
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								.bin/.e
									
										
									
									
									
								
							| 
						 | 
					@ -4,6 +4,7 @@ declare -A mapper=(
 | 
				
			||||||
	[".e"]="$HOME/.bin/.e"
 | 
						[".e"]="$HOME/.bin/.e"
 | 
				
			||||||
	["alacritty"]="$HOME/.config/alacritty/alacritty.yml"
 | 
						["alacritty"]="$HOME/.config/alacritty/alacritty.yml"
 | 
				
			||||||
	["astronvim"]="$HOME/.config/astronvim/lua/user/init.lua"
 | 
						["astronvim"]="$HOME/.config/astronvim/lua/user/init.lua"
 | 
				
			||||||
 | 
						["autostart"]="$HOME/.bin/autostart-manage"
 | 
				
			||||||
	["bash"]="$HOME/.bashrc"
 | 
						["bash"]="$HOME/.bashrc"
 | 
				
			||||||
	["bspwm"]="$HOME/.config/bspwm/bspwmrc"
 | 
						["bspwm"]="$HOME/.config/bspwm/bspwmrc"
 | 
				
			||||||
	["environment"]="$HOME/.config/environment"
 | 
						["environment"]="$HOME/.config/environment"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,44 +1,38 @@
 | 
				
			||||||
#!/usr/bin/env bash
 | 
					#!/usr/bin/env nix-shell
 | 
				
			||||||
 | 
					#!nix-shell -i bash -p dasel hostname
 | 
				
			||||||
base_dir="$HOME/.config/systemd/user/"
 | 
					# shellcheck shell=bash
 | 
				
			||||||
 | 
					 | 
				
			||||||
_filename () {
 | 
					 | 
				
			||||||
	echo "autostart-$1.service"
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
_systemctl () {
 | 
					_systemctl () {
 | 
				
			||||||
	systemctl --user "${@:2}" "$(_filename "$1")"
 | 
						systemctl --user "${@:2}" "autostart@$1.service"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_dasel () {
 | 
				
			||||||
 | 
						dasel -f "$HOME/.config/autostart.toml" -r toml "${@}"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_list () {
 | 
					_list () {
 | 
				
			||||||
	systemctl --user list-unit-files \
 | 
						part_root='all().filter(not(equal(type(),object))).key()'
 | 
				
			||||||
		| grep "autostart-" \
 | 
						part_host='all().filter(equal(type(),object)).all().key()'
 | 
				
			||||||
		| awk '{print $1}' \
 | 
						arg="merge($part_host,$part_root).all()"
 | 
				
			||||||
		| sed -e 's/^autostart-//' -e 's/.service$//'
 | 
						_dasel "$arg" \
 | 
				
			||||||
 | 
							| sort \
 | 
				
			||||||
 | 
							| uniq
 | 
				
			||||||
 | 
						#systemctl --user list-unit-files \
 | 
				
			||||||
 | 
						#	| grep "autostart-" \
 | 
				
			||||||
 | 
						#	| awk '{print $1}' \
 | 
				
			||||||
 | 
						#	| sed -e 's/^autostart-//' -e 's/.service$//'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_echo_table () {
 | 
					_echo_table () {
 | 
				
			||||||
	printf "Unit\tFilename\tEnabled\tActive\n"
 | 
					 | 
				
			||||||
	for unit in "$@"
 | 
						for unit in "$@"
 | 
				
			||||||
	do
 | 
						do
 | 
				
			||||||
		printf "%s\t%s\t%s\t%s\n" \
 | 
							printf "%s\t%s\t%s\n" \
 | 
				
			||||||
			"$unit" \
 | 
								"$unit" \
 | 
				
			||||||
			"$(_filename "$unit")" \
 | 
					 | 
				
			||||||
			"$(_systemctl "$unit" is-enabled)" \
 | 
								"$(_systemctl "$unit" is-enabled)" \
 | 
				
			||||||
			"$(_systemctl "$unit" is-active)"
 | 
								"$(_systemctl "$unit" is-active)"
 | 
				
			||||||
	done
 | 
						done
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_autostart_manage_add () {
 | 
					 | 
				
			||||||
	cat <<EOF >"$base_dir$(_filename "$1")"
 | 
					 | 
				
			||||||
[Unit]
 | 
					 | 
				
			||||||
Description=$1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
[Service]
 | 
					 | 
				
			||||||
ExecStart=/bin/sh -c '. \$HOME/.profile && $2'
 | 
					 | 
				
			||||||
EOF
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
_autostart_manage_list () {
 | 
					_autostart_manage_list () {
 | 
				
			||||||
	_list
 | 
						_list
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -50,7 +44,7 @@ _autostart_manage_info () {
 | 
				
			||||||
		autostart_units+=("$line")
 | 
							autostart_units+=("$line")
 | 
				
			||||||
	done < <(_list)
 | 
						done < <(_list)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_echo_table "${autostart_units[@]}" | column -t -s$'\t'
 | 
						_echo_table "${autostart_units[@]}" | column -t -s$'\t' --table-columns 'Unit,Enabled?,Active?'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_autostart_manage_enable_all () {
 | 
					_autostart_manage_enable_all () {
 | 
				
			||||||
| 
						 | 
					@ -66,8 +60,26 @@ _autostart_manage_enable_all () {
 | 
				
			||||||
	done
 | 
						done
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_autostart_manage_rm () {
 | 
					_get_autostart_cmd () {
 | 
				
			||||||
	rm "$base_dir$(_filename "$1")"
 | 
						selector_base="all()"
 | 
				
			||||||
 | 
						selector_host="all().filter(equal(type(),object)).filter(equal(key(),$(hostname))).all()"
 | 
				
			||||||
 | 
						selector_entry="filter(not(equal(type(),object))).filter(equal(key(),$1))"
 | 
				
			||||||
 | 
						cmd=$(_dasel "$selector_base.$selector_entry")
 | 
				
			||||||
 | 
						cmd_local=$(_dasel "$selector_host.$selector_entry")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if [ -n "$cmd_local" ]; then
 | 
				
			||||||
 | 
							if [ "$cmd_local" = "false" ]; then
 | 
				
			||||||
 | 
								cmd=""
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								cmd="$cmd_local"
 | 
				
			||||||
 | 
							fi
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
						echo "$cmd"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_autostart_manage_exec () {
 | 
				
			||||||
 | 
						cmd=$(_get_autostart_cmd "$1")
 | 
				
			||||||
 | 
						bash -c "$cmd"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_autostart_run_xorg () {
 | 
					_autostart_run_xorg () {
 | 
				
			||||||
| 
						 | 
					@ -78,11 +90,11 @@ _autostart_run_xorg () {
 | 
				
			||||||
	"$HOME/.config/bspwm/focus-voip.py" &
 | 
						"$HOME/.config/bspwm/focus-voip.py" &
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	numlockx on
 | 
						numlockx on
 | 
				
			||||||
 | 
						xsetroot -cursor_name left_ptr
 | 
				
			||||||
	xrdb "$HOME/.Xresources"
 | 
						xrdb "$HOME/.Xresources"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pgrep -x sxhkd > /dev/null || sxhkd &
 | 
						#pkill sxhkd; sxhkd &
 | 
				
			||||||
	pgrep -x xcompmgr > /dev/null || xcompmgr -cnfFn -D3 &
 | 
						#pgrep -x xcompmgr > /dev/null || xcompmgr -cnfFn -D3 &
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	start-audio pipewire
 | 
						start-audio pipewire
 | 
				
			||||||
| 
						 | 
					@ -101,11 +113,7 @@ _autostart_run_xorg () {
 | 
				
			||||||
case $1 in
 | 
					case $1 in
 | 
				
			||||||
	list)		_autostart_manage_list ;;
 | 
						list)		_autostart_manage_list ;;
 | 
				
			||||||
	info)		_autostart_manage_info ;;
 | 
						info)		_autostart_manage_info ;;
 | 
				
			||||||
	add)		_autostart_manage_add "$2" "$3" ;;
 | 
					 | 
				
			||||||
	rm)			_autostart_manage_rm "$2" ;;
 | 
					 | 
				
			||||||
	enable-all)	_autostart_manage_enable_all "$2" ;;
 | 
						enable-all)	_autostart_manage_enable_all "$2" ;;
 | 
				
			||||||
	show)		_systemctl "$2" cat ;;
 | 
					 | 
				
			||||||
	edit)		_systemctl "$2" edit --full ;;
 | 
					 | 
				
			||||||
	enable)		_systemctl "$2" add-wants autostart.target ;;
 | 
						enable)		_systemctl "$2" add-wants autostart.target ;;
 | 
				
			||||||
	disable)	_systemctl "$2" disable ;;
 | 
						disable)	_systemctl "$2" disable ;;
 | 
				
			||||||
	restart)	_systemctl "${2:-*}" restart;;
 | 
						restart)	_systemctl "${2:-*}" restart;;
 | 
				
			||||||
| 
						 | 
					@ -114,5 +122,6 @@ case $1 in
 | 
				
			||||||
	stop)		_systemctl "${2:-*}" stop ;;
 | 
						stop)		_systemctl "${2:-*}" stop ;;
 | 
				
			||||||
	run)		systemctl --user start autostart.target ;;
 | 
						run)		systemctl --user start autostart.target ;;
 | 
				
			||||||
	run-xorg)	_autostart_run_xorg ;;
 | 
						run-xorg)	_autostart_run_xorg ;;
 | 
				
			||||||
 | 
						exec)		_autostart_manage_exec "$2" ;;
 | 
				
			||||||
	*)			echo "'$1' is not valid" ;;
 | 
						*)			echo "'$1' is not valid" ;;
 | 
				
			||||||
esac
 | 
					esac
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								.config/autostart.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								.config/autostart.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					conky-clock = 'conky --config="$HOME/.config/conky/clock.conf"'
 | 
				
			||||||
 | 
					conky-system = 'conky --config="$HOME/.config/conky/system.conf"'
 | 
				
			||||||
 | 
					discord = 'flatpak run com.discordapp.Discord'
 | 
				
			||||||
 | 
					dunst = 'dunst'
 | 
				
			||||||
 | 
					element = 'flatpak run im.riot.Riot'
 | 
				
			||||||
 | 
					kdeconnect = '$HOME/.nix-profile/libexec/kdeconnectd'
 | 
				
			||||||
 | 
					parcellite = 'parcellite'
 | 
				
			||||||
 | 
					qutebrowser = 'qutebrowser'
 | 
				
			||||||
 | 
					spotify = 'spotifyd --no-daemon'
 | 
				
			||||||
 | 
					sxhkd = 'sxhkd'
 | 
				
			||||||
 | 
					syncthing = 'syncthing -no-browser'
 | 
				
			||||||
 | 
					teamspeak = 'flatpak run com.teamspeak.TeamSpeak -nosingleinstance'
 | 
				
			||||||
 | 
					xcompmgr = 'xcompmgr -cnfFn -D3'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[portalo]
 | 
				
			||||||
 | 
					ckb-next = 'ckb-next --background'
 | 
				
			||||||
 | 
					conky-archlinux-updates = 'conky --config="$HOME/.config/conky/archlinux-updates.conf"'
 | 
				
			||||||
 | 
					polkit = '/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1'
 | 
				
			||||||
 | 
					qbittorrent = 'flatpak run org.qbittorrent.qBittorrent'
 | 
				
			||||||
 | 
					signal = 'flatpak run org.signal.Signal'
 | 
				
			||||||
 | 
					solaar = 'solaar --window hide'
 | 
				
			||||||
 | 
					steam = 'flatpak run com.valvesoftware.Steam -silent'
 | 
				
			||||||
 | 
					thunderbird = 'thunderbird'
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue