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 }:
pkgs.writeShellScriptBin "icanhazip" '' pkgs.writeShellApplication {
name = "icanhazip";
runtimeInputs = with pkgs; [
iproute2
curl
gawk
coreutils
];
text = ''
if [ $# -eq 0 ]; then
echo "Public IP: $(curl -s icanhazip.com -4)" echo "Public IP: $(curl -s icanhazip.com -4)"
ip route | awk '/default/ {print "Default Gateway: " $3} /src/ {print "Local IP: " $9}' | head -n 2 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" '' pkgs.writeShellApplication {
name = "invoke";
text = ''
cmd="$1" cmd="$1"
shift shift
nix run nixpkgs#"$cmd" -- "$@" 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,17 +1,23 @@
{ {
self,
pkgs, pkgs,
}: }:
pkgs.writeShellScriptBin "runbg" '' 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 #!/usr/bin/env bash
[ $# -eq 0 ] && { # $# is number of args [ $# -eq 0 ] && { # $# is number of args
echo "$(basename $0): missing command" >&2 echo "$(basename "$0"): missing command" >&2
exit 1 exit 1
} }
prog="$(which "$1")" # see below prog="$(which "$1")" # see below
[ -z "$prog" ] && { [ -z "$prog" ] && {
echo "$(basename $0): unknown command: $1" >&2 echo "$(basename "$0"): unknown command: $1" >&2
exit 1 exit 1
} }
shift # remove $1, now $prog, from args shift # remove $1, now $prog, from args
@@ -19,4 +25,5 @@ pkgs.writeShellScriptBin "runbg" ''
tty -s <&1 && exec >/dev/null # if stdout is a terminal, redirect to 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) tty -s <&2 && exec 2>&1 # stderr to stdout (which might not be null)
"$prog" "$@" & # $@ is all args "$prog" "$@" & # $@ is all args
'' '';
}

View File

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

View File

@@ -1,5 +1,18 @@
{pkgs, ...}: {pkgs}:
pkgs.writeShellScriptBin "toolbelt" '' pkgs.writeShellApplication {
name = "toolbelt";
runtimeInputs = with pkgs; [
cliphist
fzf
ripgrep
gawk
wl-clipboard
hyprland
alsa-utils
btop
coreutils
];
text = ''
cliphistory() { cliphistory() {
selection=$(cliphist list | fzf --preview=" selection=$(cliphist list | fzf --preview="
index=\$(echo {} | awk '{print \$1}'); \ index=\$(echo {} | awk '{print \$1}'); \
@@ -13,8 +26,8 @@ pkgs.writeShellScriptBin "toolbelt" ''
cliphist decode "$selection" | wl-copy cliphist decode "$selection" | wl-copy
} }
btop_cmd() { btop_cmd() {
local hostname=$(cat /etc/hostname)
if [ "$hostname" = "oganesson" ]; then if [ "$(cat /etc/hostname)" = "oganesson" ]; then
hyprctl dispatch resizeactive 20% 155% && hyprctl dispatch resizeactive 20% 155% &&
moveonscreen --center && moveonscreen --center &&
btop && btop &&
@@ -78,4 +91,5 @@ pkgs.writeShellScriptBin "toolbelt" ''
running=false running=false
fi fi
done done
'' '';
}

View File

@@ -1,5 +1,13 @@
{pkgs}: {pkgs}:
pkgs.writeShellScriptBin "viconf" '' pkgs.writeShellApplication {
name = "viconf";
runtimeInputs = with pkgs; [
coreutils
fd
ripgrep
fzf
];
text = ''
#!/usr/bin/env bash #!/usr/bin/env bash
[ ! $# -eq 1 ] && echo "Usage: viconf <*.nix>" && exit 1 [ ! $# -eq 1 ] && echo "Usage: viconf <*.nix>" && exit 1
@@ -19,4 +27,5 @@ pkgs.writeShellScriptBin "viconf" ''
else else
nvim "$results" nvim "$results"
fi fi
'' '';
}

View File

@@ -6,16 +6,15 @@
pkgs, pkgs,
... ...
}: let }: let
keyring = import ./wm-controls/keyring.nix { inherit self pkgs; }; keyring = import ./wm-controls/keyring.nix { inherit pkgs; };
invoke = import ./commands/invoke.nix { inherit self pkgs; }; invoke = import ./commands/invoke.nix { inherit pkgs; };
splash = import ./commands/splash.nix { inherit self pkgs; }; splash = import ./commands/splash.nix { inherit pkgs; };
runbg = import ./commands/runbg.nix { inherit self pkgs; }; runbg = import ./commands/runbg.nix { inherit pkgs; };
mkbackup = import ./commands/mkbackup.nix { inherit pkgs; };
icanhazip = import ./commands/icanhazip.nix { inherit pkgs; }; icanhazip = import ./commands/icanhazip.nix { inherit pkgs; };
garbage-collect = import ./nix/garbage-collect.nix { inherit self pkgs; }; garbage-collect = import ./nix/garbage-collect.nix { inherit pkgs; };
nsp = import ./nix/nsp.nix { inherit self pkgs; }; nsp = import ./nix/nsp.nix { inherit pkgs; };
scheck = import ./wm-controls/s_check.nix { inherit self pkgs; }; scheck = import ./wm-controls/s_check.nix { inherit pkgs; };
switchmon = import ./wm-controls/switchmon.nix { inherit self pkgs; }; switchmon = import ./wm-controls/switchmon.nix { inherit pkgs; };
rebuild = import ./nix/rebuild.nix { inherit host self pkgs; }; rebuild = import ./nix/rebuild.nix { inherit host self pkgs; };
moveonscreen = import ./wm-controls/moveonscreen.nix { inherit pkgs; }; moveonscreen = import ./wm-controls/moveonscreen.nix { inherit pkgs; };
toolbelt = import ./commands/toolbelt.nix { inherit pkgs; }; toolbelt = import ./commands/toolbelt.nix { inherit pkgs; };
@@ -44,8 +43,6 @@ in {
lib.mkEnableOption "Enables all Nix shortcut scripts"; lib.mkEnableOption "Enables all Nix shortcut scripts";
# Individual options using scriptOverride or mkEnableOption directly # Individual options using scriptOverride or mkEnableOption directly
movScripts.commandScripts.mkbackup.enable =
scriptOverride "Enables the mkbackup command" "commandScripts" "mkbackup";
movScripts.commandScripts.icanhazip.enable = movScripts.commandScripts.icanhazip.enable =
scriptOverride "Enables the icanhazip command" "commandScripts" "icanhazip"; scriptOverride "Enables the icanhazip command" "commandScripts" "icanhazip";
movScripts.commandScripts.invoke.enable = movScripts.commandScripts.invoke.enable =
@@ -86,7 +83,6 @@ in {
config = lib.mkIf config.movScripts.enable { config = lib.mkIf config.movScripts.enable {
home.packages = lib.optionals config.movScripts.commandScripts.invoke.enable [ invoke ] home.packages = lib.optionals config.movScripts.commandScripts.invoke.enable [ invoke ]
++ lib.optionals config.movScripts.commandScripts.runbg.enable [ runbg ] ++ lib.optionals config.movScripts.commandScripts.runbg.enable [ runbg ]
++ lib.optionals config.movScripts.commandScripts.mkbackup.enable [ mkbackup ]
++ lib.optionals config.movScripts.commandScripts.icanhazip.enable [ icanhazip ] ++ lib.optionals config.movScripts.commandScripts.icanhazip.enable [ icanhazip ]
++ lib.optionals config.movScripts.commandScripts.splash.enable [ splash ] ++ lib.optionals config.movScripts.commandScripts.splash.enable [ splash ]
++ lib.optionals config.movScripts.commandScripts.toolbelt.enable [ toolbelt ] ++ lib.optionals config.movScripts.commandScripts.toolbelt.enable [ toolbelt ]

View File

@@ -1,8 +1,18 @@
{ {
self, pkgs
pkgs,
}: }:
pkgs.writeShellScriptBin "garbage-collect" '' pkgs.writeShellApplication {
name = "garbage-collect";
runtimeInputs = with pkgs; [
bash
coreutils
gnugrep
bc
alsa-utils
findutils
nix
];
text = ''
#!/run/current-system/sw/bin/bash #!/run/current-system/sw/bin/bash
echo "This will delete all unused paths in the nix store and delete any files in the gtrash folder." echo "This will delete all unused paths in the nix store and delete any files in the gtrash folder."
@@ -38,4 +48,5 @@ pkgs.writeShellScriptBin "garbage-collect" ''
echo -e "System cleaning complete, freed \033[1;4;38;2;166;227;161m$total_freed ''${units[$divisions]}\033[0m in total" echo -e "System cleaning complete, freed \033[1;4;38;2;166;227;161m$total_freed ''${units[$divisions]}\033[0m in total"
scheck && runbg aplay "$HOME/assets/sound/sys/rm.wav" scheck && runbg aplay "$HOME/assets/sound/sys/rm.wav"
'' '';
}

View File

@@ -1,9 +1,11 @@
{ {
self, pkgs
pkgs,
}: }:
pkgs.writeShellScriptBin "nsp" '' pkgs.writeShellApplication {
name = "nsp";
text = ''
#!/run/current-system/sw/bin/bash #!/run/current-system/sw/bin/bash
nix-shell -p "$@" --run zsh nix-shell -p "$@" --run zsh
'' '';
}

View File

@@ -3,16 +3,16 @@
self, self,
pkgs, pkgs,
}: }:
pkgs.writeShellScriptBin "rebuild" '' pkgs.writeShellApplication {
#!/run/current-system/sw/bin/bash name = "rebuild";
text = ''
scheck && runbg aplay ${self}/assets/sound/nixswitch-start.wav scheck && runbg aplay ${self}/assets/sound/nixswitch-start.wav
set -e set -e
nh os switch -H ${host} $HOME/.sysflake nh os switch -H ${host} "$HOME"/.sysflake
sudo nixos-rebuild switch --flake "$HOME/.sysflake#${host}" if sudo nixos-rebuild switch --flake "$HOME/.sysflake#${host}"; then
if [ $? -eq 0 ]; then
scheck && runbg aplay ${self}/assets/sound/update.wav scheck && runbg aplay ${self}/assets/sound/update.wav
else else
scheck && runbg aplay ${self}/assets/sound/error.wav scheck && runbg aplay ${self}/assets/sound/error.wav
fi fi
'' '';
}

View File

@@ -1,5 +1,14 @@
{pkgs}: {pkgs}:
pkgs.writeShellScriptBin "chpaper" '' pkgs.writeShellApplication {
name = "chpaper";
runtimeInputs = with pkgs; [
chafa
fzf
ripgrep
findutils
coreutils
];
text = ''
paper="$\{self}/assets/wallpapers/$(find "$FLAKEPATH"/assets/wallpapers -exec basename {} \; | rg "\.\w+$" | fzf --preview "chafa -s 30x40 $FLAKEPATH/assets/wallpapers/{}")" paper="$\{self}/assets/wallpapers/$(find "$FLAKEPATH"/assets/wallpapers -exec basename {} \; | rg "\.\w+$" | fzf --preview "chafa -s 30x40 $FLAKEPATH/assets/wallpapers/{}")"
[ "$paper" = "$\{self}/assets/wallpapers/" ] && echo "Cancelling wallpaper change" && exit 1 [ "$paper" = "$\{self}/assets/wallpapers/" ] && echo "Cancelling wallpaper change" && exit 1
echo "$paper" | xargs -I {} sed -i '/wallpaper =/s|"[^"]*"|"{}"|' "$FLAKEPATH"/modules/sys/environment/stylix.nix echo "$paper" | xargs -I {} sed -i '/wallpaper =/s|"[^"]*"|"{}"|' "$FLAKEPATH"/modules/sys/environment/stylix.nix
@@ -12,4 +21,5 @@ pkgs.writeShellScriptBin "chpaper" ''
echo "Exiting...";exit 0;; echo "Exiting...";exit 0;;
esac esac
done done
'' '';
}

View File

@@ -1,25 +1,30 @@
{pkgs}: { pkgs }:
pkgs.writeShellScriptBin "chscheme" '' pkgs.writeShellApplication {
name = "chscheme";
runtimeInputs = with pkgs; [
fzf
coreutils
];
text = ''
selected_scheme=$(/usr/bin/env ls "$(nix-build '<nixpkgs>' -A base16-schemes)"/share/themes | \ selected_scheme=$(/usr/bin/env ls "$(nix-build '<nixpkgs>' -A base16-schemes)"/share/themes | \
sed 's/\.yaml//g' | \ sed 's/\.yaml//g' | \
fzf --preview 'cat $(nix-build "<nixpkgs>" -A base16-schemes)/share/themes/{}.yaml | \ fzf --preview "cat \"$(nix-build '<nixpkgs>' -A base16-schemes)/share/themes/{}.yaml\" | \
while IFS=": " read -r key value; do \ while IFS=\": \" read -r key value; do \
if [[ $key =~ base0[0-9A-F] ]]; then \ if [[ \$key =~ base0[0-9A-F] ]]; then \
clean_value=$(echo $value | tr -d "\""); \ clean_value=\$(echo \"\$value\" | tr -d '\"'); \
r=$((16#''${clean_value:0:2})); \ r=\$((16#\''${clean_value:0:2})); \
g=$((16#''${clean_value:2:2})); \ g=\$((16#\''${clean_value:2:2})); \
b=$((16#''${clean_value:4:2})); \ b=\$((16#\''${clean_value:4:2})); \
printf "\033[48;2;%d;%d;%dm %-20s %s \033[0m\n" $r $g $b $key $clean_value; \ printf \"\\033[48;2;%d;%d;%dm %-20s %s \\033[0m\\n\" \$r \$g \$b \$key \$clean_value; \
fi; \ fi; \
done') done")
if [[ -z "$selected_scheme" ]]; then if [[ -z "$selected_scheme" ]]; then
echo "Aborting color scheme change." echo "Aborting color scheme change."
exit 1 exit 1
fi fi
echo "$selected_scheme" | xargs -I {} sed -i '/^[[:space:]]*scheme\s*=\s*"/s/"[^"]*"/"{}"/' "$FLAKEPATH"/modules/sys/environment/stylix.nix if ! echo "$selected_scheme" | xargs -I {} sed -i '/^[[:space:]]*scheme\s*=\s*"/s/"[^"]*"/"{}"/' "$FLAKEPATH"/modules/sys/environment/stylix.nix; then
if [ $? -ne 0 ]; then
echo "Failed to change color scheme." echo "Failed to change color scheme."
exit 1 exit 1
fi fi
@@ -37,4 +42,5 @@ pkgs.writeShellScriptBin "chscheme" ''
;; ;;
esac esac
done done
'' '';
}

View File

@@ -1,9 +1,17 @@
{ {
self, pkgs
pkgs,
...
}: }:
pkgs.writeShellScriptBin "keyring" '' pkgs.writeShellApplication {
name = "keyring";
runtimeInputs = with pkgs; [
pass
findutils
ripgrep
fzf
wl-clipboard
coreutils
];
text = ''
#!/run/current-system/sw/bin/bash #!/run/current-system/sw/bin/bash
# prevent multiple instances, conditional check happens in the hyprland bind # prevent multiple instances, conditional check happens in the hyprland bind
@@ -11,9 +19,7 @@ pkgs.writeShellScriptBin "keyring" ''
trap "[ -f /tmp/keyringfile ] && /run/current-system/sw/bin/rm /tmp/keyringfile" EXIT SIGHUP SIGINT trap "[ -f /tmp/keyringfile ] && /run/current-system/sw/bin/rm /tmp/keyringfile" EXIT SIGHUP SIGINT
# get passwords from password store, remove .password store/ prefix and .gpg suffix, exlude .gpg-id file, open results in fzf # get passwords from password store, remove .password store/ prefix and .gpg suffix, exlude .gpg-id file, open results in fzf
pass_string=$(find $HOME/.password-store -type f | sed 's|.*/.password-store/||; s|\.gpg$||' | sed 's|^\([^/]*\)|\x1b[32m\1\x1b[0m|' | rg -v "\.git|.gpg-id" | sort -r | fzf --border --border-label="$(whoami)'s keyring" --ansi --layout=reverse) pass_string=$(find "$HOME"/.password-store -type f | sed 's|.*/.password-store/||; s|\.gpg$||' | sed 's|^\([^/]*\)|\x1b[32m\1\x1b[0m|' | rg -v "\.git|.gpg-id" | sort -r | fzf --border --border-label="$(whoami)'s keyring" --ansi --layout=reverse)
[ $? = 0 ] || { [ -f /tmp/keyringfile ] && /run/current-system/sw/bin/rm /tmp/keyringfile; exit 1; }
# prevents cliphist from writing passwords to the clipboard history # prevents cliphist from writing passwords to the clipboard history
pkill -STOP wl-paste pkill -STOP wl-paste
@@ -32,4 +38,5 @@ pkgs.writeShellScriptBin "keyring" ''
/run/current-system/sw/bin/rm /tmp/keyringfile /run/current-system/sw/bin/rm /tmp/keyringfile
sleep 0.5 sleep 0.5
exit 0 exit 0
'' '';
}

View File

@@ -1,6 +1,17 @@
{ pkgs }: { pkgs }:
pkgs.writeShellScriptBin "mkscreenshots" '' pkgs.writeShellApplication {
name = "mkscreenshots";
runtimeInputs = with pkgs; [
jq
neofetch
kitty
coreutils
nemo
grimblast
git
];
text = ''
if [ -n "$(hyprctl clients -j | jq -r '.[] | select(.workspace.name == "4")')" ]; then if [ -n "$(hyprctl clients -j | jq -r '.[] | select(.workspace.name == "4")')" ]; then
echo "There are windows in workspace 4. This script uses workspace 4, so move those windows and run it again." echo "There are windows in workspace 4. This script uses workspace 4, so move those windows and run it again."
exit 1 exit 1
@@ -55,4 +66,5 @@ pkgs.writeShellScriptBin "mkscreenshots" ''
closewindows closewindows
hyprctl dispatch workspace "$prev_workspace" hyprctl dispatch workspace "$prev_workspace"
'' '';
}

View File

@@ -1,7 +1,15 @@
{pkgs}: {pkgs}:
pkgs.writeShellScriptBin "moveonscreen" '' pkgs.writeShellApplication {
name = "moveonscreen";
runtimeInputs = with pkgs; [
hyprland
jq
coreutils
gawk
];
text = ''
center_window=false center_window=false
if [[ $1 == "--center" ]]; then if [[ ! $# -eq 0 ]] && [[ $1 == "--center" ]]; then
center_window=true center_window=true
fi fi
@@ -73,4 +81,5 @@ pkgs.writeShellScriptBin "moveonscreen" ''
fi fi
hyprctl dispatch moveactive exact "$adjusted_x $adjusted_y" > /dev/null 2>&1 hyprctl dispatch moveactive exact "$adjusted_x $adjusted_y" > /dev/null 2>&1
'' '';
}

View File

@@ -1,9 +1,11 @@
{ {
self,
pkgs, pkgs,
}: }:
pkgs.writeShellScriptBin "scheck" '' pkgs.writeShellApplication {
name = "scheck";
text = ''
#!/run/current-system/sw/bin/bash #!/run/current-system/sw/bin/bash
[ "$SOUNDS_ENABLED" -eq 1 ] [ "$SOUNDS_ENABLED" -eq 1 ]
'' '';
}

View File

@@ -1,9 +1,11 @@
{ {
self,
pkgs, pkgs,
}: }:
pkgs.writeShellScriptBin "switchmon" '' pkgs.writeShellApplication {
name = "switchmon";
text = ''
#!/bin/zsh #!/bin/zsh
hyprctl dispatch focusmonitor $(echo "$(hyprctl -j monitors)" | jq -r '.[] | select(.focused == false) | .name') hyprctl dispatch focusmonitor "$(hyprctl -j monitors | jq -r '.[] | select(.focused == false) | .name')"
'' '';
}