mirror of
https://github.com/tomasriveral/nixos.git
synced 2026-08-12 02:28:37 +02:00
Formated with alejandra
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
# creates and maintains cache file for the notification history
|
||||
{
|
||||
writeShellApplication,
|
||||
...
|
||||
}:
|
||||
{writeShellApplication, ...}:
|
||||
writeShellApplication {
|
||||
name = "QS-notifycache";
|
||||
runtimeInputs = [
|
||||
];
|
||||
text = ''
|
||||
CACHE="$HOME/.cache/notify_history"
|
||||
# Create or erase existing cache
|
||||
mkdir -p "$(dirname "$CACHE")"
|
||||
: > "$CACHE" # truncate file
|
||||
echo "Notification history initialized at $(date)" >> "$CACHE"
|
||||
CACHE="$HOME/.cache/notify_history"
|
||||
# Create or erase existing cache
|
||||
mkdir -p "$(dirname "$CACHE")"
|
||||
: > "$CACHE" # truncate file
|
||||
echo "Notification history initialized at $(date)" >> "$CACHE"
|
||||
'';
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
{
|
||||
writeShellApplication,
|
||||
...
|
||||
}:
|
||||
{writeShellApplication, ...}:
|
||||
writeShellApplication {
|
||||
name = "QS-notifyhistory";
|
||||
runtimeInputs = [
|
||||
|
||||
@@ -1,80 +1,80 @@
|
||||
{
|
||||
writeShellApplication,
|
||||
ripgrep,
|
||||
gawkInteractive,
|
||||
iproute2,
|
||||
coreutils,
|
||||
procps,
|
||||
upower,
|
||||
...
|
||||
writeShellApplication,
|
||||
ripgrep,
|
||||
gawkInteractive,
|
||||
iproute2,
|
||||
coreutils,
|
||||
procps,
|
||||
upower,
|
||||
...
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "QS-sysinfo";
|
||||
runtimeInputs = [
|
||||
ripgrep
|
||||
gawkInteractive
|
||||
iproute2
|
||||
coreutils
|
||||
procps
|
||||
upower
|
||||
];
|
||||
text = ''
|
||||
# Pad CPU, MEM, DISK to 2 chars (right-aligned)
|
||||
|
||||
cpu=$(awk '/^cpu /{printf("%.0f", ($2+$4)*100/($2+$4+$5))}' /proc/stat)
|
||||
cpu=$(printf "%2s" "$cpu")
|
||||
name = "QS-sysinfo";
|
||||
runtimeInputs = [
|
||||
ripgrep
|
||||
gawkInteractive
|
||||
iproute2
|
||||
coreutils
|
||||
procps
|
||||
upower
|
||||
];
|
||||
text = ''
|
||||
# Pad CPU, MEM, DISK to 2 chars (right-aligned)
|
||||
|
||||
sum=0
|
||||
count=0
|
||||
cpu=$(awk '/^cpu /{printf("%.0f", ($2+$4)*100/($2+$4+$5))}' /proc/stat)
|
||||
cpu=$(printf "%2s" "$cpu")
|
||||
|
||||
for f in /sys/class/hwmon/hwmon*/temp*_input; do
|
||||
if [ -r "$f" ]; then
|
||||
val=$(cat "$f" 2>/dev/null) || continue
|
||||
sum=$((sum + val))
|
||||
count=$((count + 1))
|
||||
fi
|
||||
done
|
||||
sum=0
|
||||
count=0
|
||||
|
||||
temp=$(( sum / count / 1000 ))
|
||||
for f in /sys/class/hwmon/hwmon*/temp*_input; do
|
||||
if [ -r "$f" ]; then
|
||||
val=$(cat "$f" 2>/dev/null) || continue
|
||||
sum=$((sum + val))
|
||||
count=$((count + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
sum=0
|
||||
count=0
|
||||
temp=$(( sum / count / 1000 ))
|
||||
|
||||
for f in /sys/class/hwmon/hwmon*/fan*_input; do
|
||||
if [ -r "$f" ]; then
|
||||
val=$(cat "$f" 2>/dev/null) || continue
|
||||
sum=$((sum + val))
|
||||
count=$((count + 1))
|
||||
fi
|
||||
done
|
||||
fan=$(( sum / count ))
|
||||
fan=$(printf "%4s" "$fan")
|
||||
sum=0
|
||||
count=0
|
||||
|
||||
mem=$(free | awk '/Mem:/ {printf("%.0f",$3/$2*100)}')
|
||||
mem=$(printf "%2s" "$mem")
|
||||
for f in /sys/class/hwmon/hwmon*/fan*_input; do
|
||||
if [ -r "$f" ]; then
|
||||
val=$(cat "$f" 2>/dev/null) || continue
|
||||
sum=$((sum + val))
|
||||
count=$((count + 1))
|
||||
fi
|
||||
done
|
||||
fan=$(( sum / count ))
|
||||
fan=$(printf "%4s" "$fan")
|
||||
|
||||
disk=$(df / | awk 'NR==2 {gsub("%",""); print $5}')
|
||||
disk=$(printf "%2s" "$disk")
|
||||
mem=$(free | awk '/Mem:/ {printf("%.0f",$3/$2*100)}')
|
||||
mem=$(printf "%2s" "$mem")
|
||||
|
||||
# Pad POWER to 5 chars (XX.XX)
|
||||
power=$(upower -b | awk -F: '/energy-rate/ {gsub(/^[ \t]+/, "", $2); split($2,a," "); printf("%.2f", a[1])}')
|
||||
power=$(printf "%5s" "$power")
|
||||
disk=$(df / | awk 'NR==2 {gsub("%",""); print $5}')
|
||||
disk=$(printf "%2s" "$disk")
|
||||
|
||||
# network calculations
|
||||
iface=$(ip route get 1.1.1.1 | awk '{print $5; exit}')
|
||||
# Pad POWER to 5 chars (XX.XX)
|
||||
power=$(upower -b | awk -F: '/energy-rate/ {gsub(/^[ \t]+/, "", $2); split($2,a," "); printf("%.2f", a[1])}')
|
||||
power=$(printf "%5s" "$power")
|
||||
|
||||
rx1=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
|
||||
tx1=$(cat "/sys/class/net/$iface/statistics/tx_bytes")
|
||||
# network calculations
|
||||
iface=$(ip route get 1.1.1.1 | awk '{print $5; exit}')
|
||||
|
||||
sleep 1
|
||||
rx1=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
|
||||
tx1=$(cat "/sys/class/net/$iface/statistics/tx_bytes")
|
||||
|
||||
rx2=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
|
||||
tx2=$(cat "/sys/class/net/$iface/statistics/tx_bytes")
|
||||
sleep 1
|
||||
|
||||
down_speed=$(( (rx2 - rx1) / 1024 ))
|
||||
up_speed=$(( (tx2 - tx1) / 1024 ))
|
||||
rx2=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
|
||||
tx2=$(cat "/sys/class/net/$iface/statistics/tx_bytes")
|
||||
|
||||
# Output JSON
|
||||
echo "{\"cpu\":\"$cpu\",\"temp\":\"$temp\",\"fan\":\"$fan\",\"mem\":\"$mem\",\"disk\":\"$disk\",\"power\":\"$power\",\"down\":\"$down_speed\",\"up\":\"$up_speed\"}"
|
||||
'';
|
||||
down_speed=$(( (rx2 - rx1) / 1024 ))
|
||||
up_speed=$(( (tx2 - tx1) / 1024 ))
|
||||
|
||||
# Output JSON
|
||||
echo "{\"cpu\":\"$cpu\",\"temp\":\"$temp\",\"fan\":\"$fan\",\"mem\":\"$mem\",\"disk\":\"$disk\",\"power\":\"$power\",\"down\":\"$down_speed\",\"up\":\"$up_speed\"}"
|
||||
'';
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
# taken and adapted https://github.com/miniMinn24/Battery_Notify with MIT License
|
||||
{
|
||||
writeShellApplication,
|
||||
mplayer,
|
||||
brightnessctl,
|
||||
...
|
||||
writeShellApplication,
|
||||
mplayer,
|
||||
brightnessctl,
|
||||
...
|
||||
}: let
|
||||
NotifySound = ../../assets/battery_notify.mp3;
|
||||
NotifySound = ../../assets/battery_notify.mp3;
|
||||
in
|
||||
writeShellApplication {
|
||||
name = "custom-batterywarning";
|
||||
runtimeInputs = [
|
||||
mplayer
|
||||
brightnessctl
|
||||
];
|
||||
text = ''
|
||||
writeShellApplication {
|
||||
name = "custom-batterywarning";
|
||||
runtimeInputs = [
|
||||
mplayer
|
||||
brightnessctl
|
||||
];
|
||||
text = ''
|
||||
|
||||
# Delay for startup
|
||||
sleep 5
|
||||
# Delay for startup
|
||||
sleep 5
|
||||
|
||||
|
||||
playsound() {
|
||||
mplayer ${NotifySound}
|
||||
}
|
||||
playsound() {
|
||||
mplayer ${NotifySound}
|
||||
}
|
||||
|
||||
|
||||
|
||||
while true; do
|
||||
Battery_Status="$(cat /sys/class/power_supply/BAT*/status)"
|
||||
Battery_Capacity="$(cat /sys/class/power_supply/BAT*/capacity)"
|
||||
while true; do
|
||||
Battery_Status="$(cat /sys/class/power_supply/BAT*/status)"
|
||||
Battery_Capacity="$(cat /sys/class/power_supply/BAT*/capacity)"
|
||||
|
||||
if [ "$Battery_Status" == "Discharging" ]; then
|
||||
if [ "$Battery_Capacity" -le 20 ] && [ "$Battery_Capacity" -ge 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "Power running out: <b>''${Battery_Capacity}%</b>"
|
||||
playsound
|
||||
sleep 180
|
||||
if [ "$Battery_Status" == "Discharging" ]; then
|
||||
if [ "$Battery_Capacity" -le 20 ] && [ "$Battery_Capacity" -ge 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "Power running out: <b>''${Battery_Capacity}%</b>"
|
||||
playsound
|
||||
sleep 180
|
||||
|
||||
elif [ "$Battery_Capacity" -lt 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "<b>Save your works</b> before immediate shutdown."
|
||||
playsound
|
||||
sleep 180
|
||||
fi
|
||||
else
|
||||
sleep 120
|
||||
fi
|
||||
done
|
||||
'';
|
||||
}
|
||||
elif [ "$Battery_Capacity" -lt 10 ]; then
|
||||
notify-send -e -u critical "Battery Low!" "<b>Save your works</b> before immediate shutdown."
|
||||
playsound
|
||||
sleep 180
|
||||
fi
|
||||
else
|
||||
sleep 120
|
||||
fi
|
||||
done
|
||||
'';
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
rsync,
|
||||
...
|
||||
}:
|
||||
|
||||
writeShellApplication {
|
||||
name = "custom-obsidianbackup";
|
||||
runtimeInputs = [
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
ripgrep,
|
||||
...
|
||||
}:
|
||||
|
||||
writeShellApplication {
|
||||
name = "custom-obsidianvaults";
|
||||
runtimeInputs = [
|
||||
@@ -42,5 +41,5 @@ writeShellApplication {
|
||||
# Exit the kitty terminal after selection
|
||||
pkill -f "kitty.*Select Obsidian vault"
|
||||
exit
|
||||
'';
|
||||
'';
|
||||
}
|
||||
|
||||
+230
-230
@@ -10,257 +10,257 @@ writeShellApplication {
|
||||
nix
|
||||
];
|
||||
text = ''
|
||||
set -euo pipefail
|
||||
set -euo pipefail
|
||||
|
||||
## Defaults
|
||||
keepGensDef=30; keepDaysDef=7
|
||||
keepGens=$keepGensDef; keepDays=$keepDaysDef
|
||||
## Defaults
|
||||
keepGensDef=30; keepDaysDef=7
|
||||
keepGens=$keepGensDef; keepDays=$keepDaysDef
|
||||
|
||||
## Usage
|
||||
usage () {
|
||||
printf "Usage:\n\t ./trim-generations.sh <keep-generations> <keep-days> <profile> \n\n
|
||||
(defaults are: Keep-Gens=%d Keep-Days=%d Profile=user)\n\n" "$keepGensDef" "$keepDaysDef"
|
||||
printf "If you enter any parameters, you must enter all three, or none to use defaults.\n"
|
||||
printf "Example:\n\t trim-generations.sh 15 10 home-manager\n"
|
||||
printf " this will work on the home-manager profile and keep all generations from the\n"
|
||||
printf "last 10 days, and keep at least 15 generations no matter how old.\n"
|
||||
printf "\nProfiles available are:\tuser, home-manager, channels, system (root)\n"
|
||||
printf "\n-h or --help prints this help text."
|
||||
}
|
||||
## Usage
|
||||
usage () {
|
||||
printf "Usage:\n\t ./trim-generations.sh <keep-generations> <keep-days> <profile> \n\n
|
||||
(defaults are: Keep-Gens=%d Keep-Days=%d Profile=user)\n\n" "$keepGensDef" "$keepDaysDef"
|
||||
printf "If you enter any parameters, you must enter all three, or none to use defaults.\n"
|
||||
printf "Example:\n\t trim-generations.sh 15 10 home-manager\n"
|
||||
printf " this will work on the home-manager profile and keep all generations from the\n"
|
||||
printf "last 10 days, and keep at least 15 generations no matter how old.\n"
|
||||
printf "\nProfiles available are:\tuser, home-manager, channels, system (root)\n"
|
||||
printf "\n-h or --help prints this help text."
|
||||
}
|
||||
|
||||
if [ $# -eq 1 ]; then # if help requested
|
||||
if [ "$1" = "-h" ]; then
|
||||
usage
|
||||
exit 1;
|
||||
fi
|
||||
if [ "$1" = "--help" ]; then
|
||||
usage
|
||||
exit 2;
|
||||
fi
|
||||
printf "Dont recognise your option exiting..\n\n"
|
||||
usage
|
||||
exit 3;
|
||||
|
||||
elif [ $# -eq 0 ]; then # print the defaults
|
||||
printf "The current defaults are:\n Keep-Gens=%d Keep-Days=%d \n\n" "$keepGensDef" "$keepDaysDef"
|
||||
read -r -p "Keep these defaults? (y/n):" answer
|
||||
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "Using defaults..\n"
|
||||
;;
|
||||
[nN0] ) printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* ) printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
## Handle parameters (and change if root)
|
||||
if [[ $EUID -ne 0 ]]; then # if not root
|
||||
profile=$(readlink /home/"$USER"/.nix-profile)
|
||||
else
|
||||
if [ -d /nix/var/nix/profiles/system ]; then # maybe this or the other
|
||||
profile="/nix/var/nix/profiles/system"
|
||||
elif [ -d /nix/var/nix/profiles/default ]; then
|
||||
profile="/nix/var/nix/profiles/default"
|
||||
else
|
||||
echo "Cant find profile for root. Exiting"
|
||||
exit 8
|
||||
fi
|
||||
fi
|
||||
if (( $# < 1 )); then
|
||||
printf "Keeping default: %d generations OR %d days, whichever is more\n" "$keepGensDef" "$keepDaysDef"
|
||||
elif [[ $# -le 2 ]]; then
|
||||
printf "\nError: Not enough arguments.\n\n" >&2
|
||||
usage
|
||||
exit 1
|
||||
elif (( $# > 4)); then
|
||||
printf "\nError: Too many arguments.\n\n" >&2
|
||||
usage
|
||||
exit 2
|
||||
else
|
||||
if [ "$1" -lt 1 ]; then
|
||||
printf "using Gen numbers less than 1 not recommended. Setting to min=1\n"
|
||||
read -r -p "is that ok? (y/n): " answer
|
||||
printf "%s" "$answer"
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "ok, continuing..\n"
|
||||
;;
|
||||
[nN0] )
|
||||
printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* )
|
||||
printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
if [ "$2" -lt 0 ]; then
|
||||
printf "using negative days number not recommended. Setting to min=0\n"
|
||||
read -r -p "is that ok? (y/n): " answer
|
||||
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "ok, continuing..\n"
|
||||
;;
|
||||
[nN0] )
|
||||
printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* )
|
||||
printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
keepGens=$1; keepDays=$2;
|
||||
(( keepGens < 1 )) && keepGens=1
|
||||
(( keepDays < 0 )) && keepDays=0
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
if [[ $3 == "user" ]] || [[ $3 == "default" ]]; then
|
||||
profile=$(readlink /home/"$USER"/.nix-profile)
|
||||
elif [[ $3 == "home-manager" ]]; then
|
||||
# home-manager defaults to $XDG_STATE_HOME; otherwise, use
|
||||
# `home-manager generations` and `nix-store --query --roots
|
||||
# /nix/store/...` to figure out what reference is keeping the old
|
||||
# generations alive.
|
||||
profile="''${XDG_STATE_HOME:-$HOME/.local/state}/nix/profiles/home-manager"
|
||||
elif [[ $3 == "channels" ]]; then
|
||||
profile="/nix/var/nix/profiles/per-user/$USER/channels"
|
||||
else
|
||||
printf "\nError: Do not understand your third argument. Should be one of: (user / home-manager/ channels)\n\n"
|
||||
usage
|
||||
exit 3
|
||||
if [ $# -eq 1 ]; then # if help requested
|
||||
if [ "$1" = "-h" ]; then
|
||||
usage
|
||||
exit 1;
|
||||
fi
|
||||
if [ "$1" = "--help" ]; then
|
||||
usage
|
||||
exit 2;
|
||||
fi
|
||||
printf "Dont recognise your option exiting..\n\n"
|
||||
usage
|
||||
exit 3;
|
||||
|
||||
elif [ $# -eq 0 ]; then # print the defaults
|
||||
printf "The current defaults are:\n Keep-Gens=%d Keep-Days=%d \n\n" "$keepGensDef" "$keepDaysDef"
|
||||
read -r -p "Keep these defaults? (y/n):" answer
|
||||
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "Using defaults..\n"
|
||||
;;
|
||||
[nN0] ) printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* ) printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
## Handle parameters (and change if root)
|
||||
if [[ $EUID -ne 0 ]]; then # if not root
|
||||
profile=$(readlink /home/"$USER"/.nix-profile)
|
||||
else
|
||||
if [[ $3 == "system" ]]; then
|
||||
if [ -d /nix/var/nix/profiles/system ]; then # maybe this or the other
|
||||
profile="/nix/var/nix/profiles/system"
|
||||
elif [[ $3 == "user" ]] || [[ $3 == "default" ]]; then
|
||||
elif [ -d /nix/var/nix/profiles/default ]; then
|
||||
profile="/nix/var/nix/profiles/default"
|
||||
else
|
||||
printf "\nError: Do not understand your third argument. Should be one of: (user / system)\n\n"
|
||||
usage
|
||||
exit 3
|
||||
echo "Cant find profile for root. Exiting"
|
||||
exit 8
|
||||
fi
|
||||
fi
|
||||
printf "OK! \t Keep Gens = %s \t Keep Days = %s \n\n" "$keepGens" "$keepDays"
|
||||
if (( $# < 1 )); then
|
||||
printf "Keeping default: %d generations OR %d days, whichever is more\n" "$keepGensDef" "$keepDaysDef"
|
||||
elif [[ $# -le 2 ]]; then
|
||||
printf "\nError: Not enough arguments.\n\n" >&2
|
||||
usage
|
||||
exit 1
|
||||
elif (( $# > 4)); then
|
||||
printf "\nError: Too many arguments.\n\n" >&2
|
||||
usage
|
||||
exit 2
|
||||
else
|
||||
if [ "$1" -lt 1 ]; then
|
||||
printf "using Gen numbers less than 1 not recommended. Setting to min=1\n"
|
||||
read -r -p "is that ok? (y/n): " answer
|
||||
printf "%s" "$answer"
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "ok, continuing..\n"
|
||||
;;
|
||||
[nN0] )
|
||||
printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* )
|
||||
printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
if [ "$2" -lt 0 ]; then
|
||||
printf "using negative days number not recommended. Setting to min=0\n"
|
||||
read -r -p "is that ok? (y/n): " answer
|
||||
|
||||
case "$answer" in
|
||||
[yY1] )
|
||||
printf "ok, continuing..\n"
|
||||
;;
|
||||
[nN0] )
|
||||
printf "ok, doing nothing, exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* )
|
||||
printf "%b" "Doing nothing, exiting.."
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
keepGens=$1; keepDays=$2;
|
||||
(( keepGens < 1 )) && keepGens=1
|
||||
(( keepDays < 0 )) && keepDays=0
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
if [[ $3 == "user" ]] || [[ $3 == "default" ]]; then
|
||||
profile=$(readlink /home/"$USER"/.nix-profile)
|
||||
elif [[ $3 == "home-manager" ]]; then
|
||||
# home-manager defaults to $XDG_STATE_HOME; otherwise, use
|
||||
# `home-manager generations` and `nix-store --query --roots
|
||||
# /nix/store/...` to figure out what reference is keeping the old
|
||||
# generations alive.
|
||||
profile="''${XDG_STATE_HOME:-$HOME/.local/state}/nix/profiles/home-manager"
|
||||
elif [[ $3 == "channels" ]]; then
|
||||
profile="/nix/var/nix/profiles/per-user/$USER/channels"
|
||||
else
|
||||
printf "\nError: Do not understand your third argument. Should be one of: (user / home-manager/ channels)\n\n"
|
||||
usage
|
||||
exit 3
|
||||
fi
|
||||
else
|
||||
if [[ $3 == "system" ]]; then
|
||||
profile="/nix/var/nix/profiles/system"
|
||||
elif [[ $3 == "user" ]] || [[ $3 == "default" ]]; then
|
||||
profile="/nix/var/nix/profiles/default"
|
||||
else
|
||||
printf "\nError: Do not understand your third argument. Should be one of: (user / system)\n\n"
|
||||
usage
|
||||
exit 3
|
||||
fi
|
||||
fi
|
||||
printf "OK! \t Keep Gens = %s \t Keep Days = %s \n\n" "$keepGens" "$keepDays"
|
||||
|
||||
|
||||
fi
|
||||
|
||||
printf "Operating on profile: %s \t \n\n" "$profile"
|
||||
|
||||
## Runs at the end, to decide whether to delete profiles that match chosen parameters.
|
||||
choose () {
|
||||
local default="$1"
|
||||
local prompt="$2"
|
||||
local answer
|
||||
|
||||
read -r -p "$prompt" answer
|
||||
[ -z "$answer" ] && answer="$default"
|
||||
|
||||
case "$answer" in
|
||||
[yY1] ) #printf "answered yes!\n"
|
||||
nix-env --delete-generations -p "$profile" "''${!gens[@]}"
|
||||
exit 0
|
||||
;;
|
||||
[nN0] ) printf "Ok doing nothing exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* ) printf "%b" "Unexpected answer '$answer'!" >&2
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
} # end of function choose
|
||||
|
||||
# printf "profile = %s \n\n" "$profile"
|
||||
## Query nix-env for generations list
|
||||
#IFS=$'\n' nixGens=( $(nix-env --list-generations -p "$profile" | sed 's:^\s*::; s:\s*$::' | tr '\t' ' ' | tr -s ' ') )
|
||||
# gave errors replaced by:
|
||||
mapfile -t nixGens < <(
|
||||
nix-env --list-generations -p "$profile" |
|
||||
sed 's:^\s*::; s:\s*$::' |
|
||||
tr '\t' ' ' |
|
||||
tr -s ' '
|
||||
)
|
||||
timeNow=$(date +%s)
|
||||
|
||||
## Get info on oldest generation
|
||||
IFS=' ' read -r -a oldestGenArr <<< "''${nixGens[0]}"
|
||||
oldestGen=''${oldestGenArr[0]}
|
||||
oldestDate=''${oldestGenArr[1]}
|
||||
printf "%-30s %s\n" "oldest generation:" "$oldestGen"
|
||||
#oldestDate=''${nixGens[0]:3:19}
|
||||
printf "%-30s %s\n" "oldest generation created:" "$oldestDate"
|
||||
oldestTime=$(date -d "$oldestDate" +%s)
|
||||
oldestElapsedSecs=$((timeNow-oldestTime))
|
||||
oldestElapsedMins=$((oldestElapsedSecs/60))
|
||||
oldestElapsedHours=$((oldestElapsedMins/60))
|
||||
oldestElapsedDays=$((oldestElapsedHours/24))
|
||||
printf "%-30s %s\n" "minutes before now:" "$oldestElapsedMins"
|
||||
printf "%-30s %s\n" "hours before now:" "$oldestElapsedHours"
|
||||
printf "%-30s %s\n\n" "days before now:" "$oldestElapsedDays"
|
||||
|
||||
## Get info on current generation
|
||||
for i in "''${nixGens[@]}"; do
|
||||
IFS=' ' read -r -a iGenArr <<< "$i"
|
||||
genNumber=''${iGenArr[0]}
|
||||
genDate=''${iGenArr[1]}
|
||||
if [[ "$i" =~ current ]]; then
|
||||
currentGen=$genNumber
|
||||
printf "%-30s %s\n" "current generation:" "$currentGen"
|
||||
currentDate=$genDate
|
||||
printf "%-30s %s\n" "current generation created:" "$currentDate"
|
||||
currentTime=$(date -d "$currentDate" +%s)
|
||||
currentElapsedSecs=$((timeNow-currentTime))
|
||||
currentElapsedMins=$((currentElapsedSecs/60))
|
||||
currentElapsedHours=$((currentElapsedMins/60))
|
||||
currentElapsedDays=$((currentElapsedHours/24))
|
||||
printf "%-30s %s\n" "minutes before now:" "$currentElapsedMins"
|
||||
printf "%-30s %s\n" "hours before now:" "$currentElapsedHours"
|
||||
printf "%-30s %s\n\n" "days before now:" "$currentElapsedDays"
|
||||
fi
|
||||
done
|
||||
|
||||
## Compare oldest and current generations
|
||||
timeBetweenOldestAndCurrent=$((currentTime-oldestTime))
|
||||
elapsedDays=$((timeBetweenOldestAndCurrent/60/60/24))
|
||||
generationsDiff=$((currentGen-oldestGen))
|
||||
printf "Operating on profile: %s \t \n\n" "$profile"
|
||||
|
||||
## Figure out what we should do, based on generations and options
|
||||
if [[ elapsedDays -le keepDays ]]; then
|
||||
printf "All generations are no more than %s days older than current generation. \nOldest gen days difference from current gen: %s \n\n\tNothing to do!\n" "$keepDays" "$elapsedDays"
|
||||
exit 4;
|
||||
elif [[ generationsDiff -lt keepGens ]]; then
|
||||
printf "Oldest generation (%d) is only %d generations behind current (%d). \n\n\t Nothing to do!\n" "$oldestGen" "$generationsDiff" "$currentGen"
|
||||
exit 5;
|
||||
else
|
||||
printf "\tSomething to do...\n"
|
||||
declare -a gens
|
||||
## Runs at the end, to decide whether to delete profiles that match chosen parameters.
|
||||
choose () {
|
||||
local default="$1"
|
||||
local prompt="$2"
|
||||
local answer
|
||||
|
||||
read -r -p "$prompt" answer
|
||||
[ -z "$answer" ] && answer="$default"
|
||||
|
||||
case "$answer" in
|
||||
[yY1] ) #printf "answered yes!\n"
|
||||
nix-env --delete-generations -p "$profile" "''${!gens[@]}"
|
||||
exit 0
|
||||
;;
|
||||
[nN0] ) printf "Ok doing nothing exiting..\n"
|
||||
exit 6;
|
||||
;;
|
||||
* ) printf "%b" "Unexpected answer '$answer'!" >&2
|
||||
exit 7;
|
||||
;;
|
||||
esac
|
||||
} # end of function choose
|
||||
|
||||
# printf "profile = %s \n\n" "$profile"
|
||||
## Query nix-env for generations list
|
||||
#IFS=$'\n' nixGens=( $(nix-env --list-generations -p "$profile" | sed 's:^\s*::; s:\s*$::' | tr '\t' ' ' | tr -s ' ') )
|
||||
# gave errors replaced by:
|
||||
mapfile -t nixGens < <(
|
||||
nix-env --list-generations -p "$profile" |
|
||||
sed 's:^\s*::; s:\s*$::' |
|
||||
tr '\t' ' ' |
|
||||
tr -s ' '
|
||||
)
|
||||
timeNow=$(date +%s)
|
||||
|
||||
## Get info on oldest generation
|
||||
IFS=' ' read -r -a oldestGenArr <<< "''${nixGens[0]}"
|
||||
oldestGen=''${oldestGenArr[0]}
|
||||
oldestDate=''${oldestGenArr[1]}
|
||||
printf "%-30s %s\n" "oldest generation:" "$oldestGen"
|
||||
#oldestDate=''${nixGens[0]:3:19}
|
||||
printf "%-30s %s\n" "oldest generation created:" "$oldestDate"
|
||||
oldestTime=$(date -d "$oldestDate" +%s)
|
||||
oldestElapsedSecs=$((timeNow-oldestTime))
|
||||
oldestElapsedMins=$((oldestElapsedSecs/60))
|
||||
oldestElapsedHours=$((oldestElapsedMins/60))
|
||||
oldestElapsedDays=$((oldestElapsedHours/24))
|
||||
printf "%-30s %s\n" "minutes before now:" "$oldestElapsedMins"
|
||||
printf "%-30s %s\n" "hours before now:" "$oldestElapsedHours"
|
||||
printf "%-30s %s\n\n" "days before now:" "$oldestElapsedDays"
|
||||
|
||||
## Get info on current generation
|
||||
for i in "''${nixGens[@]}"; do
|
||||
IFS=' ' read -r -a iGenArr <<< "$i"
|
||||
genNumber=''${iGenArr[0]}
|
||||
genDiff=$((currentGen-genNumber))
|
||||
genDate=''${iGenArr[1]}
|
||||
genTime=$(date -d "$genDate" +%s)
|
||||
elapsedSecs=$((timeNow-genTime))
|
||||
genDaysOld=$((elapsedSecs/60/60/24))
|
||||
if [[ genDaysOld -gt keepDays ]] && [[ genDiff -ge keepGens ]]; then
|
||||
gens["$genNumber"]="$genDate, $genDaysOld day(s) old"
|
||||
if [[ "$i" =~ current ]]; then
|
||||
currentGen=$genNumber
|
||||
printf "%-30s %s\n" "current generation:" "$currentGen"
|
||||
currentDate=$genDate
|
||||
printf "%-30s %s\n" "current generation created:" "$currentDate"
|
||||
currentTime=$(date -d "$currentDate" +%s)
|
||||
currentElapsedSecs=$((timeNow-currentTime))
|
||||
currentElapsedMins=$((currentElapsedSecs/60))
|
||||
currentElapsedHours=$((currentElapsedMins/60))
|
||||
currentElapsedDays=$((currentElapsedHours/24))
|
||||
printf "%-30s %s\n" "minutes before now:" "$currentElapsedMins"
|
||||
printf "%-30s %s\n" "hours before now:" "$currentElapsedHours"
|
||||
printf "%-30s %s\n\n" "days before now:" "$currentElapsedDays"
|
||||
fi
|
||||
done
|
||||
printf "\nFound the following generation(s) to delete:\n"
|
||||
for K in "''${!gens[@]}"; do
|
||||
printf "generation %d \t ''${gens[$K]}\n" "$K"
|
||||
done
|
||||
printf "\n"
|
||||
choose "y" "Do you want to delete these? [Y/n]: "
|
||||
fi
|
||||
|
||||
## Compare oldest and current generations
|
||||
timeBetweenOldestAndCurrent=$((currentTime-oldestTime))
|
||||
elapsedDays=$((timeBetweenOldestAndCurrent/60/60/24))
|
||||
generationsDiff=$((currentGen-oldestGen))
|
||||
|
||||
## Figure out what we should do, based on generations and options
|
||||
if [[ elapsedDays -le keepDays ]]; then
|
||||
printf "All generations are no more than %s days older than current generation. \nOldest gen days difference from current gen: %s \n\n\tNothing to do!\n" "$keepDays" "$elapsedDays"
|
||||
exit 4;
|
||||
elif [[ generationsDiff -lt keepGens ]]; then
|
||||
printf "Oldest generation (%d) is only %d generations behind current (%d). \n\n\t Nothing to do!\n" "$oldestGen" "$generationsDiff" "$currentGen"
|
||||
exit 5;
|
||||
else
|
||||
printf "\tSomething to do...\n"
|
||||
declare -a gens
|
||||
for i in "''${nixGens[@]}"; do
|
||||
IFS=' ' read -r -a iGenArr <<< "$i"
|
||||
genNumber=''${iGenArr[0]}
|
||||
genDiff=$((currentGen-genNumber))
|
||||
genDate=''${iGenArr[1]}
|
||||
genTime=$(date -d "$genDate" +%s)
|
||||
elapsedSecs=$((timeNow-genTime))
|
||||
genDaysOld=$((elapsedSecs/60/60/24))
|
||||
if [[ genDaysOld -gt keepDays ]] && [[ genDiff -ge keepGens ]]; then
|
||||
gens["$genNumber"]="$genDate, $genDaysOld day(s) old"
|
||||
fi
|
||||
done
|
||||
printf "\nFound the following generation(s) to delete:\n"
|
||||
for K in "''${!gens[@]}"; do
|
||||
printf "generation %d \t ''${gens[$K]}\n" "$K"
|
||||
done
|
||||
printf "\n"
|
||||
choose "y" "Do you want to delete these? [Y/n]: "
|
||||
fi
|
||||
|
||||
'';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user