NixOS Generations Trimmer: Difference between revisions

imported>Nix
move to gist to deal with formatting issues
imported>Mike1
m New confirm option added
 
(7 intermediate revisions by 2 users not shown)
Line 13: Line 13:
* If you are a normal user, you can run on either the <code>home-manager</code> or <code>channels</code> profiles too.
* If you are a normal user, you can run on either the <code>home-manager</code> or <code>channels</code> profiles too.


=== Example script output ===
== Script (trim-generations.sh) ==
 
The script is available here: [https://gist.github.com/MaxwellDupre/3077cd229490cf93ecab08ef2a79c852 Github Gist - NixOS: trim-generations.sh]
 
== Example script output ==


==== Without parameters (defaults) ====
=== Without parameters (defaults) ===


<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
❯ ./trim-generations.sh     
❯ ./trim-generations.sh     
Keeping default: 10 generations OR 7 days, whichever is more
Operating on profile: /nix/var/nix/profiles/per-user/user/profile


oldest generation:            27
The current defaults are:
oldest generation created:    2021-09-24
Keep-Gens=30 Keep-Days=30
minutes before now:            3918
 
hours before now:              65
Keep these defaults? (y/n):y
days before now:              2
Using defaults..
Keeping default: 30 generations OR 30 days, whichever is more
Operating on profile:    /nix/var/nix/profiles/per-user/dougal/profile
 
oldest generation:            75
oldest generation created:    2023-08-05
minutes before now:            46740
hours before now:              779
days before now:              32
 
current generation:            217
current generation created:    2023-09-06
minutes before now:            660
hours before now:              11
days before now:              0
 
        Something to do...


current generation:           33
Found the following generation(s) to delete:
current generation created:   2021-09-25
generation 75    2023-08-05, 32 day(s) old
generation 76    2023-08-05, 32 day(s) old
generation 77    2023-08-05, 32 day(s) old
generation 78    2023-08-05, 32 day(s) old
generation 79    2023-08-05, 32 day(s) old
generation 80    2023-08-05, 32 day(s) old
generation 81    2023-08-05, 32 day(s) old
generation 82    2023-08-05, 32 day(s) old
generation 83    2023-08-05, 32 day(s) old
generation 84    2023-08-05, 32 day(s) old
generation 85    2023-08-05, 32 day(s) old
generation 86   2023-08-05, 32 day(s) old


All generations are no more than 7 days older than current generation.
Do you want to delete these? [Y/n]:  
Oldest gen difference from current gen: 1


Nothing to do!
</syntaxHighlight>
</syntaxHighlight>


==== With parameters ====
=== With parameters ===


<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
Line 66: Line 93:
</syntaxHighlight>
</syntaxHighlight>


==== With parameters (not enough though) ====
=== With parameters (not enough though) ===


Example if you accidentally don't specify all parameters / arguments.
Example if you accidentally don't specify all parameters / arguments.
Line 76: Line 103:


Usage:
Usage:
trim-generations.sh (defaults are: Keep-Gens=10 Keep-Days=7 Profile=user)
        ./trim-generations.sh <keep-gernerations> <keep-days> <profile>


If you enter any parameters, you must enter all three.


(defaults are: Keep-Gens=30 Keep-Days=30 Profile=user)
If you enter any parameters, you must enter all three, or none to use defaults.
Example:
Example:
trim-generations.sh 15 10 home-manager
        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.
  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.
 
Profiles available are: user, home-manager, channels, system (root)


Profile choices available: user, home-manager, channels, system (root only)
-h or --help prints this help text.
</syntaxHighlight>
</syntaxHighlight>


==== Run as root (default) ====
=== Run as root (default) ===


<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
❯ sudo ./trim-generations.sh                 
❯ sudo ./trim-generations.sh                 
Keeping default: 10 generations OR 7 days, whichever is more
The current defaults are:
Operating on profile: /nix/var/nix/profiles/default
Keep-Gens=30 Keep-Days=30
 
Keep these defaults? (y/n):y
Using defaults..
Keeping default: 30 generations OR 30 days, whichever is more
Operating on profile:   /nix/var/nix/profiles/system


oldest generation:            8
oldest generation:            65
oldest generation created:    2021-09-04
oldest generation created:    2023-08-05
minutes before now:            32725
minutes before now:            46727
hours before now:              545
hours before now:              778
days before now:              22
days before now:              32


current generation:            8
current generation:            102
current generation created:    2021-09-04
current generation created:    2023-09-04
minutes before now:            3527
hours before now:              58
days before now:              2


All generations are no more than 7 days older than current generation.  
All generations are no more than 30 days older than current generation.  
Oldest gen days difference from current gen: 0
Oldest gen days difference from current gen: 30


Nothing to do!
        Nothing to do!
</syntaxHighlight>
</syntaxHighlight>


==== Run as root (on system profile) ====
=== Run as root (on system profile) ===


<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
Line 129: Line 169:
</syntaxHighlight>
</syntaxHighlight>


==== Run as root (wrong profile) ====
=== Run as root (wrong profile) ===


Example if you accidentally give wrong profile (no home-manager on root):
Example if you accidentally give wrong profile (no home-manager on root):
Line 139: Line 179:


Usage:
Usage:
trim-generations.sh (defaults are: Keep-Gens=10 Keep-Days=7 Profile=user)
        ./trim-generations.sh <keep-gernerations> <keep-days> <profile>
 


If you enter any parameters, you must enter all three.
(defaults are: Keep-Gens=30 Keep-Days=30 Profile=user)


If you enter any parameters, you must enter all three, or none to use defaults.
Example:
Example:
trim-generations.sh 15 10 home-manager
        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.
  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.
 
Profiles available are: user, home-manager, channels, system (root)


Profile choices available: user, home-manager, channels, system (root only)
-h or --help prints this help text.
</syntaxHighlight>
</syntaxHighlight>




 
[[Category:Tutorial]]
 
 
== Script (trim-generations.sh) ==
 
The script is available here: [https://gist.github.com/Bondrake/27555c9d02c2882fd5e32f8ab3ed620b Github Gist - NixOS: trim-generations.sh]