NixOS Generations Trimmer: Difference between revisions
imported>Mike1 |
imported>Mike1 m change script to less buggy one |
||
Line 15: | Line 15: | ||
== Script (trim-generations.sh) == | == Script (trim-generations.sh) == | ||
The script is available here: [https://gist.github.com/ | The script is available here: [https://gist.github.com/MaxwellDupre/3077cd229490cf93ecab08ef2a79c852 Github Gist - NixOS: trim-generations.sh] | ||
== Example script output == | == Example script output == |
Revision as of 09:45, 6 September 2023
Overview
The normal options available in NixOS to cleanup old generations might be too limiting. The script below is interactive and smart.
By default (run with no arguments), the script keeps the last 7 days of generations and will prompt you to remove older generations if they are more than 10 generations behind the current one. So it always keeps at least 10 generations and always preserves generations from the last week.
You can specify the number of days to always keep generations, and you can specify the number of generations to always keep.
By default, it works on the default profile for the user running it.
Other profile options:
- If you are root, you can run on the
system
profile - If you are a normal user, you can run on either the
home-manager
orchannels
profiles too.
Script (trim-generations.sh)
The script is available here: Github Gist - NixOS: trim-generations.sh
Example script output
Without parameters (defaults)
❯ ./trim-generations.sh
Keeping default: 30 generations OR 30 days, whichever is more
Operating on profile: /nix/var/nix/profiles/per-user/user/profile
oldest generation: 27
oldest generation created: 2021-09-24
minutes before now: 3918
hours before now: 65
days before now: 2
current generation: 33
current generation created: 2021-09-25
All generations are no more than 30 days older than current generation.
Oldest gen difference from current gen: 1
Nothing to do!
With parameters
❯ ./trim-generations.sh 3 0 home-manager
OK! Keep Gens = 3 Keep Days = 0
Operating on profile: /nix/var/nix/profiles/per-user/user/home-manager
oldest generation: 58
oldest generation created: 2021-09-24
minutes before now: 3922
hours before now: 65
days before now: 2
current generation: 65
current generation created: 2021-09-25
Something to do...
Found the following generation(s) to delete:
generation 58 2021-09-24, 2 day(s) old
generation 59 2021-09-25, 1 day(s) old
generation 60 2021-09-25, 1 day(s) old
generation 61 2021-09-25, 1 day(s) old
generation 62 2021-09-25, 1 day(s) old
Do you want to delete these? [Y/n]:
With parameters (not enough though)
Example if you accidentally don't specify all parameters / arguments.
❯ sudo ./trim-generations.sh 2 0
Error: Not enough arguments.
Usage:
trim-generations.sh (defaults are: Keep-Gens=10 Keep-Days=7 Profile=user)
If you enter any parameters, you must enter all three.
Example:
trim-generations.sh 15 10 home-manager
... 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.
Profile choices available: user, home-manager, channels, system (root only)
Run as root (default)
❯ sudo ./trim-generations.sh
Keeping default: 10 generations OR 7 days, whichever is more
Operating on profile: /nix/var/nix/profiles/default
oldest generation: 8
oldest generation created: 2021-09-04
minutes before now: 32725
hours before now: 545
days before now: 22
current generation: 8
current generation created: 2021-09-04
All generations are no more than 7 days older than current generation.
Oldest gen days difference from current gen: 0
Nothing to do!
Run as root (on system profile)
❯ sudo ./trim-generations.sh 2 0 system
OK! Keep Gens = 2 Keep Days = 0
Operating on profile: /nix/var/nix/profiles/system
oldest generation: 11
oldest generation created: 2021-09-22
minutes before now: 6807
hours before now: 113
days before now: 4
current generation: 12
current generation created: 2021-09-26
Oldest generation (11) is only 1 generations behind current (12). Nothing to do!
Run as root (wrong profile)
Example if you accidentally give wrong profile (no home-manager on root):
❯ sudo ./trim-generations.sh 3 0 home-manager
Error: Do not understand your third argument. Should be one of: (user / system)
Usage:
trim-generations.sh (defaults are: Keep-Gens=10 Keep-Days=7 Profile=user)
If you enter any parameters, you must enter all three.
Example:
trim-generations.sh 15 10 home-manager
... 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.
Profile choices available: user, home-manager, channels, system (root only)