Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #3

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions SCRIPTS/My Nixos commands script/About

This file was deleted.

23 changes: 23 additions & 0 deletions SCRIPTS/NixOs Related/My Nixos commands script/About.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Tolga Erok
10/6/2023


This script is a menu-driven tool that allows the user to run common Nix commands with sudo privileges. It provides a list of commands related to Nix package management and system configuration and executes the selected command using the sudo command. Here is a summary of its functionality:

The script defines several functions, including execute_command to run a command in sudo mode, display_menu to show the menu options, and delete_old_generations to delete old generations of the active profile.

The display_menu function displays a list of Nix commands along with their descriptions.

The script clears the screen and displays the initial menu.

It enters a loop that waits for the user to input a choice.

Depending on the selected choice, the script executes the corresponding command using the execute_command function with the appropriate arguments.

After executing the command, the script prompts the user to press any key to continue.

The screen is cleared, and the menu is displayed again for the user to choose another command or exit the script.

The loop continues until the user selects the "Exit" option.

Overall, this script provides a convenient way to run commonly used Nix commands with sudo privileges in a menu-driven manner.
91 changes: 91 additions & 0 deletions SCRIPTS/NixOs Related/My Nixos commands script/MyNixOS-commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/run/current-system/sw/bin/bash

# Tolga Erok 10/6/2023 Basic script that allows user to run common Nix commands at sudo level.

# Function to execute a command in sudo mode
execute_command() {
echo -e "\033[1;33mExecuting command: sudo $1\033[0m"
sudo $1
}

# Function to display the menu options
display_menu() {
echo -e "\033[1;34mNix Useful Commands Menu:\033[0m"
echo -e "\033[1;32m 1. nixos-rebuild switch\033[0m Rebuild and switch to the NixOS configuration"
echo -e "\033[1;32m 2. nix-store --optimise\033[0m Optimize the Nix store"
echo -e "\033[1;32m 3. nix-collect-garbage\033[0m Collect garbage from the Nix store"
echo -e "\033[1;32m 4. nix-store --gc\033[0m Perform garbage collection"
echo -e "\033[1;32m 5. nix-env --list-generations\033[0m List generations of the active profile"
echo -e "\033[1;32m 6. nix-env --profile --list-generations\033[0m List generations of the system profile"
echo -e "\033[1;32m 7. nix-channel --list\033[0m List available channels"
echo -e "\033[1;32m 8. List unstable channels\033[0m List available unstable channels"
echo -e "\033[1;32m 9. Add unstable channel\033[0m Add the unstable channel"
echo -e "\033[1;32m10. Delete unstable channel\033[0m Delete the unstable channel"
echo -e "\033[1;32m11. Refresh channels\033[0m Refresh all channels"
echo -e "\033[1;32m12. Upgrade packages\033[0m Upgrade all installed packages"
echo -e "\033[1;32m13. Upgrade channels\033[0m Upgrade all channels"
echo -e "\033[1;32m14. nix-store optimise\033[0m Optimize the Nix store (alternative command)"
echo -e "\033[1;32m15. nix-store --optimise --all\033[0m Optimize the Nix store (alternative command)"
echo -e "\033[1;32m16. nix-store --gc --print-dead\033[0m Print the paths to be deleted during garbage collection"
echo -e "\033[1;32m17. Delete old generations\033[0m Delete old generations of the active profile"
echo -e "\033[1;32m 0. Exit\033[0m Exit the script"
}

# Function to delete old generations
delete_old_generations() {
echo -e "\033[1;34mDelete Old Generations Menu:\033[0m"
echo -e "\033[1;32mGeneration Age\033[0m"

# Get the list of generations
generations=$(sudo nix-env --profile /nix/var/nix/profiles/system --list-generations | tail -n +3)

# Sort generations from oldest to newest
sorted_generations=$(echo "$generations" | sort -n)

# Calculate age for each generation and display
while read -r generation; do
age=$(sudo nix-env --query --requisites --installed --profile /nix/var/nix/profiles/system/$generation | awk -F ":" '{print $2}')
printf "%-13s %s\n" "$generation" "$age"
done <<< "$sorted_generations"

# Prompt for generation to delete
read -p $'\n\033[1;33mEnter the generation to delete (0 to cancel): \033[0m' choice
if [[ $choice -gt 0 ]]; then
execute_command "sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations $choice"
fi
}

# Clear the screen on the first run
clear

# Display the initial menu
display_menu

# Loop until the user selects "Exit"
while true; do
read -p "Enter your choice (0-17): " choice
case $choice in
1) execute_command "nixos-rebuild switch" ;;
2) execute_command "nix-store --optimise" ;;
3) execute_command "nix-collect-garbage" ;;
4) execute_command "nix-store --gc" ;;
5) execute_command "nix-env --list-generations" ;;
6) execute_command "nix-env --profile /nix/var/nix/profiles/system --list-generations" ;;
7) execute_command "nix-channel --list" ;;
8) execute_command "nix-channel --list --unstable" ;;
9) execute_command "nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable" ;;
10) execute_command "nix-channel --remove nixos-unstable" ;;
11) execute_command "nix-channel --update" ;;
12) execute_command "nix-env --upgrade" ;;
13) execute_command "nix-channel --update" ;;
14) execute_command "nix-store optimise" ;;
15) execute_command "nix-store --optimise --all" ;;
16) execute_command "nix-store --gc --print-dead" ;;
17) delete_old_generations ;;
0) exit ;;
*) echo -e "\033[1;31mInvalid choice. Please try again.\033[0m" ;;
esac
read -rsn1 -p "Press any key to continue..."
clear
display_menu
done
23 changes: 23 additions & 0 deletions SCRIPTS/NixOs Related/TrimGenerations/About.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Summary of the script:

1. The script sets the shell options (`set -euo pipefail`) to enforce strict error handling and behavior.

2. It defines default values for the number of generations to keep (`keepGensDef`) and the number of days to keep (`keepDaysDef`).

3. The script provides a usage function that explains how to use the script and its parameters.

4. It handles the command-line parameters, including the profile to operate on and the number of generations and days to keep.

5. The script retrieves information about the generations in the specified profile using `nix-env --list-generations` and calculates the oldest and current generation.

6. It compares the oldest and current generations to determine if any action needs to be taken based on the specified parameters.

7. If necessary, the script identifies the generations to delete and prompts the user for confirmation before deleting them using `nix-env --delete-generations`.

8. The script outputs relevant information throughout the process for debugging and reporting purposes.

9. The script uses functions to handle user input and make decisions based on the provided information.

10. The script exits with appropriate status codes to indicate the outcome of its operations.

Overall, the script is designed to trim generations in a Nix profile based on specified criteria and user confirmation. It provides flexibility to choose the profile, number of generations, and number of days to keep.
164 changes: 164 additions & 0 deletions SCRIPTS/NixOs Related/TrimGenerations/TrimmGenerations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/run/current-system/sw/bin/bash

set -euo pipefail

## Defaults
keepGensDef=10; keepDaysDef=7
keepGens=$keepGensDef; keepDays=$keepDaysDef

## Usage
usage () {
printf "Usage:\n\t trim-generations.sh (defaults are: Keep-Gens=$keepGensDef Keep-Days=$keepDaysDef Profile=user)\n\n"
printf "If you enter any parameters, you must enter all three.\n\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 last 10 days, and keep at least 15 generations no matter how old.\n"
printf "\nProfile choices available: \t user, home-manager, channels, system (root only)\n"
}

## Handle parameters (and change if root)
if [[ $EUID -ne 0 ]]; then
profile=$(readlink /home/$USER/.nix-profile)
else
profile="/nix/var/nix/profiles/default"
fi
if (( $# < 1 )); then
printf "Keeping default: 10 generations OR 7 days, whichever is more\n"
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
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
profile="/nix/var/nix/profiles/per-user/$USER/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 = $keepGens \t Keep Days = $keepDays\n\n"
fi

printf "Operating on profile: \t $profile\n\n"

## Runs at the end, to decide whether to delete profiles that match chosen parameters.
choose () {
local default="$1"
local prompt="$2"
local answer

read -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 "answered no! exiting\n"
exit 6;
;;
* ) printf "%b" "Unexpected answer '$answer'!" >&2
exit 7;
;;
esac
} # end of function choose


## Query nix-env for generations list
IFS=$'\n' 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))

## Figure out what we should do, based on generations and options
if [[ elapsedDays -le keepDays ]]; then
printf "All generations are no more than $keepDays days older than the current generation. \nOldest gen days difference from current gen: $elapsedDays \n\n\tNothing to do!\n"
exit 4;
elif [[ generationsDiff -lt keepGens ]]; then
printf "The oldest generation ($oldestGen) is only $generationsDiff generation(s) behind the current generation ($currentGen). \n\n\tNothing to do!\n"
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 $K \t ${gens[$K]}\n"
done
printf "\n"
choose "y" "Do you want to delete these? [Y/n]: "
fi
Empty file.