removed mkbackup script, refactored all script files to use pkgs.writeShellApplication rather than pkgs.writeShellScriptBin

This commit is contained in:
pagedmov
2024-11-06 18:17:29 -05:00
parent ad22e5a0b1
commit a53bae9870
18 changed files with 497 additions and 416 deletions

View File

@@ -1,6 +1,23 @@
{ pkgs }:
pkgs.writeShellScriptBin "icanhazip" ''
echo "Public IP: $(curl -s icanhazip.com -4)"
ip route | awk '/default/ {print "Default Gateway: " $3} /src/ {print "Local IP: " $9}' | head -n 2
''
pkgs.writeShellApplication {
name = "icanhazip";
runtimeInputs = with pkgs; [
iproute2
curl
gawk
coreutils
];
text = ''
if [ $# -eq 0 ]; then
echo "Public IP: $(curl -s icanhazip.com -4)"
ip route | awk '/default/ {print "Default Gateway: " $3} /src/ {print "Local IP: " $9}' | head -n 2
else case $1 in
"-p" ) echo "Public IP: $(curl -s icanhazip.com -4)";;
"-d" ) ip route | awk '/default/ {print $3}';;
"-l" ) ip route | awk '/src/ {print $9}';;
* ) echo "Options: -p, -d or -l for public ip, default gateway, and local ip respectively"; echo "i.e. icanhazip -p"
esac
fi
'';
}

View File

@@ -1,9 +1,11 @@
{
self,
pkgs,
pkgs
}:
pkgs.writeShellScriptBin "invoke" ''
cmd="$1"
shift
nix run nixpkgs#"$cmd" -- "$@"
''
pkgs.writeShellApplication {
name = "invoke";
text = ''
cmd="$1"
shift
nix run nixpkgs#"$cmd" -- "$@"
'';
}

View File

@@ -1,30 +0,0 @@
{ pkgs }:
pkgs.writeShellScriptBin "mkbackup" ''
if ! findmnt | grep share; then
echo "Mounting shared filesystem..."
if ! sshfs pagedmov@192.168.1.200:/home/pagedmov/share $HOME/share; then
echo "failed to mount shared storage to $HOME/share" >&2
exit 1
fi
echo "Done"
fi
echo "Copying files..."
if printf '%s\0' "$@" | xargs -0 -I {} cp {} -v "$HOME/share"; then
echo "Done"
else
echo "Failed to copy files to shared directory."
exit 1
fi
echo "Moving files to backup folder on serverside..."
if ssh pagedmov@192.168.1.200 "IFS=' ' read -rA files <<< \"$@\"; for file in \''${files[@]}; do echo -n \"\$(whoami)@\$(hostname): \"; mv -v ~/share/\"\$file\" ~/backup; done"; then
echo "Backup completed."
umount $HOME/share
else
echo "Failed to move files on serverside. Unmounting shared filesystem."
umount $HOME/share
exit 1
fi
''

View File

@@ -1,22 +1,29 @@
{
self,
pkgs,
}:
pkgs.writeShellScriptBin "runbg" ''
#!/usr/bin/env bash
pkgs.writeShellApplication {
name = "runbg";
runtimeInputs = with pkgs; [
coreutils # Provides `basename`, `which`, etc.
bash # Provides the Bash shell
util-linux # Provides `tty`
];
text = ''
#!/usr/bin/env bash
[ $# -eq 0 ] && { # $# is number of args
echo "$(basename $0): missing command" >&2
exit 1
}
prog="$(which "$1")" # see below
[ -z "$prog" ] && {
echo "$(basename $0): unknown command: $1" >&2
exit 1
}
shift # remove $1, now $prog, from args
tty -s && exec </dev/null # if stdin is a terminal, redirect from null
tty -s <&1 && exec >/dev/null # if stdout is a terminal, redirect to null
tty -s <&2 && exec 2>&1 # stderr to stdout (which might not be null)
"$prog" "$@" & # $@ is all args
''
[ $# -eq 0 ] && { # $# is number of args
echo "$(basename "$0"): missing command" >&2
exit 1
}
prog="$(which "$1")" # see below
[ -z "$prog" ] && {
echo "$(basename "$0"): unknown command: $1" >&2
exit 1
}
shift # remove $1, now $prog, from args
tty -s && exec </dev/null # if stdin is a terminal, redirect from null
tty -s <&1 && exec >/dev/null # if stdout is a terminal, redirect to null
tty -s <&2 && exec 2>&1 # stderr to stdout (which might not be null)
"$prog" "$@" & # $@ is all args
'';
}

View File

@@ -1,13 +1,18 @@
{
self,
pkgs,
pkgs
}:
pkgs.writeShellScriptBin "splash" ''
#!/bin/bash
echo "NixOS kernel ver. $(uname -a | awk '{print $3}') x86_64 GNU/Linux"
date +"%A %B %-d %Y"
echo
echo " NixOS" | toilet -f 3d | lolcat -S 25
echo
''
pkgs.writeShellApplication {
name = "splash";
runtimeInputs = with pkgs; [
lolcat
toilet
coreutils
];
text = ''
echo "NixOS kernel ver. $(uname -a | awk '{print $3}') x86_64 GNU/Linux"
date +"%A %B %-d %Y"
echo
echo " NixOS" | toilet -f 3d | lolcat -S 25
echo
'';
}

View File

@@ -1,81 +1,95 @@
{pkgs, ...}:
pkgs.writeShellScriptBin "toolbelt" ''
cliphistory() {
selection=$(cliphist list | fzf --preview="
index=\$(echo {} | awk '{print \$1}'); \
mime=\$(cliphist decode \$index | file -i -); \
if echo \"\$mime\" | grep -q 'image'; then \
echo \$(cliphist list | rg \"^\$index\" | cut -d ' ' -f 2- | fmt -w 30); \
else \
cliphist decode \"\$index\" | fmt -w 30; \
fi" --prompt="> " | awk '{print $1}')
[ -z "$selection" ] && return 1
cliphist decode "$selection" | wl-copy
}
btop_cmd() {
local hostname=$(cat /etc/hostname)
if [ "$hostname" = "oganesson" ]; then
hyprctl dispatch resizeactive 20% 155% &&
moveonscreen --center &&
btop &&
hyprctl dispatch resizeactive exact 40% 25% &&
moveonscreen
else
hyprctl dispatch resizeactive exact 60% 68% &&
moveonscreen --center &&
btop &&
hyprctl dispatch resizeactive exact 40% 25% &&
moveonscreen
fi
}
{pkgs}:
pkgs.writeShellApplication {
name = "toolbelt";
runtimeInputs = with pkgs; [
cliphist
fzf
ripgrep
gawk
wl-clipboard
hyprland
alsa-utils
btop
coreutils
];
text = ''
cliphistory() {
selection=$(cliphist list | fzf --preview="
index=\$(echo {} | awk '{print \$1}'); \
mime=\$(cliphist decode \$index | file -i -); \
if echo \"\$mime\" | grep -q 'image'; then \
echo \$(cliphist list | rg \"^\$index\" | cut -d ' ' -f 2- | fmt -w 30); \
else \
cliphist decode \"\$index\" | fmt -w 30; \
fi" --prompt="> " | awk '{print $1}')
[ -z "$selection" ] && return 1
cliphist decode "$selection" | wl-copy
}
btop_cmd() {
running=true
if [ "$(cat /etc/hostname)" = "oganesson" ]; then
hyprctl dispatch resizeactive 20% 155% &&
moveonscreen --center &&
btop &&
hyprctl dispatch resizeactive exact 40% 25% &&
moveonscreen
else
hyprctl dispatch resizeactive exact 60% 68% &&
moveonscreen --center &&
btop &&
hyprctl dispatch resizeactive exact 40% 25% &&
moveonscreen
fi
}
declare -A commands=(
["Change Wallpaper"]="moveonscreen --center && if chpaper; then running=false; else moveonscreen; fi"
["Change System Color Scheme"]="hyprctl dispatch resizeactive 10% 80% && moveonscreen --center && if chscheme; then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
["Open System Monitor"]="btop_cmd"
["Open Volume Controls"]="hyprctl dispatch resizeactive 10% 80% && moveonscreen --center && alsamixer && hyprctl dispatch resizeactive exact 40% 25% && moveonscreen"
["Open Keyring"]="hyprctl dispatch resizeactive -300 0 && moveonscreen && if keyring; then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
["View Clipboard History"]="hyprctl dispatch resizeactive 45% 120% && moveonscreen --center && if cliphistory;then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
)
running=true
ordered_commands=(
"Open Keyring"
"Open System Monitor"
"Open Volume Controls"
"Change Wallpaper"
"Change System Color Scheme"
"View Clipboard History"
)
declare -A commands=(
["Change Wallpaper"]="moveonscreen --center && if chpaper; then running=false; else moveonscreen; fi"
["Change System Color Scheme"]="hyprctl dispatch resizeactive 10% 80% && moveonscreen --center && if chscheme; then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
["Open System Monitor"]="btop_cmd"
["Open Volume Controls"]="hyprctl dispatch resizeactive 10% 80% && moveonscreen --center && alsamixer && hyprctl dispatch resizeactive exact 40% 25% && moveonscreen"
["Open Keyring"]="hyprctl dispatch resizeactive -300 0 && moveonscreen && if keyring; then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
["View Clipboard History"]="hyprctl dispatch resizeactive 45% 120% && moveonscreen --center && if cliphistory;then running=false; else hyprctl dispatch resizeactive exact 40% 25% && moveonscreen; fi"
)
# Use fzf to select a command with preview
while $running; do
selected_command=$(printf "%s\n" "''${ordered_commands[@]}" | fzf --preview="
cleaned_key=\$(echo {} | tr -d \"'\"); \
echo \"Cleaned key: \$cleaned_key\"; \
declare -A descriptions=(
[\"Change Wallpaper\"]=\"Choose a wallpaper to switch to from the assets/wallpapers folder in the system flake directory. Requires rebuilding the system and restarting hyprpaper.\"
[\"Change System Color Scheme\"]=\"Changes the base16 color scheme used by stylix to color system applications.\"
[\"Open System Monitor\"]=\"Opens a btop window.\"
[\"Open Volume Controls\"]=\"Opens alsamixer.\"
[\"Open Keyring\"]=\"Opens a fuzzy finder with all of the paths held in ~/.password-store. Selecting one uses pass to copy that password to the clipboard. Password is cleared from clipboard after 45 seconds, and isn't saved to clipboard history.\"
[\"View Clipboard History\"]=\"Opens clipboard history. Selecting an item copies it to the clipboard.\"
); \
if [[ -v descriptions[\$cleaned_key] ]]; then \
clear; \
echo \''${descriptions[\$cleaned_key]} | fmt -w 28; \
else \
clear; \
echo \"No description available\"; \
fi" --prompt="> ")
ordered_commands=(
"Open Keyring"
"Open System Monitor"
"Open Volume Controls"
"Change Wallpaper"
"Change System Color Scheme"
"View Clipboard History"
)
# Use fzf to select a command with preview
while $running; do
selected_command=$(printf "%s\n" "''${ordered_commands[@]}" | fzf --preview="
cleaned_key=\$(echo {} | tr -d \"'\"); \
echo \"Cleaned key: \$cleaned_key\"; \
declare -A descriptions=(
[\"Change Wallpaper\"]=\"Choose a wallpaper to switch to from the assets/wallpapers folder in the system flake directory. Requires rebuilding the system and restarting hyprpaper.\"
[\"Change System Color Scheme\"]=\"Changes the base16 color scheme used by stylix to color system applications.\"
[\"Open System Monitor\"]=\"Opens a btop window.\"
[\"Open Volume Controls\"]=\"Opens alsamixer.\"
[\"Open Keyring\"]=\"Opens a fuzzy finder with all of the paths held in ~/.password-store. Selecting one uses pass to copy that password to the clipboard. Password is cleared from clipboard after 45 seconds, and isn't saved to clipboard history.\"
[\"View Clipboard History\"]=\"Opens clipboard history. Selecting an item copies it to the clipboard.\"
); \
if [[ -v descriptions[\$cleaned_key] ]]; then \
clear; \
echo \''${descriptions[\$cleaned_key]} | fmt -w 28; \
else \
clear; \
echo \"No description available\"; \
fi" --prompt="> ")
#Execute the selected command if selection is not empty
if [[ -n $selected_command ]]; then
eval "''${commands[$selected_command]}"
else
running=false
fi
done
''
#Execute the selected command if selection is not empty
if [[ -n $selected_command ]]; then
eval "''${commands[$selected_command]}"
else
running=false
fi
done
'';
}

View File

@@ -1,22 +1,31 @@
{pkgs}:
pkgs.writeShellScriptBin "viconf" ''
#!/usr/bin/env bash
pkgs.writeShellApplication {
name = "viconf";
runtimeInputs = with pkgs; [
coreutils
fd
ripgrep
fzf
];
text = ''
#!/usr/bin/env bash
[ ! $# -eq 1 ] && echo "Usage: viconf <*.nix>" && exit 1
[ ! $# -eq 1 ] && echo "Usage: viconf <*.nix>" && exit 1
results=$(find "$FLAKEPATH" -wholename "*$1*" -exec find {} \; | sort | uniq | rg '\.nix$')
numresults=$(echo "$results" | wc -l)
results=$(find "$FLAKEPATH" -wholename "*$1*" -exec find {} \; | sort | uniq | rg '\.nix$')
numresults=$(echo "$results" | wc -l)
[ "$numresults" -eq 0 ] && echo "$1 not found in \$FLAKEPATH" && exit 1
[ "$numresults" -eq 0 ] && echo "$1 not found in \$FLAKEPATH" && exit 1
if [ "$numresults" -gt 1 ]; then
# cut up the paths to give shorter path names to fuzzy finder
results_prefix=$(echo "$results" | tail -n 1 | cut -d'/' -f-3)
results=$(echo "$results" | cut -d'/' -f4-)
results=$(echo "$results" | grep "$1")
if [ "$numresults" -gt 1 ]; then
# cut up the paths to give shorter path names to fuzzy finder
results_prefix=$(echo "$results" | tail -n 1 | cut -d'/' -f-3)
results=$(echo "$results" | cut -d'/' -f4-)
results=$(echo "$results" | grep "$1")
echo "$results" | tr ' ' '\n' | fzf | xargs -I {} nvim "$results_prefix"/{}
else
nvim "$results"
fi
''
echo "$results" | tr ' ' '\n' | fzf | xargs -I {} nvim "$results_prefix"/{}
else
nvim "$results"
fi
'';
}