about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--doc/manual/nix-env.xml10
-rw-r--r--src/nix-env/nix-env.cc19
2 files changed, 24 insertions, 5 deletions
diff --git a/doc/manual/nix-env.xml b/doc/manual/nix-env.xml
index 92becccba706..3bfcb15dbac2 100644
--- a/doc/manual/nix-env.xml
+++ b/doc/manual/nix-env.xml
@@ -1167,10 +1167,12 @@ $ nix-env --list-generations
 <refsection><title>Description</title>
 
 <para>This operation deletes the specified generations of the current
-profile.  The generations can be a list of generation numbers, or the
+profile.  The generations can be a list of generation numbers, the
 special value <literal>old</literal> to delete all non-current
-generations.  Periodically deleting old generations is important to
-make garbage collection effective.</para>
+generations, or a value such as <literal>30d</literal> to delete all
+non-current generations older than the specified number of days.
+Periodically deleting old generations is important to make garbage
+collection effective.</para>
 
 </refsection>
 
@@ -1179,6 +1181,8 @@ make garbage collection effective.</para>
 <screen>
 $ nix-env --delete-generations 3 4 8
 
+$ nix-env --delete-generations 30d
+
 $ nix-env -p other_profile --delete-generations old</screen>
 
 </refsection>
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index 333a7fe8c0ca..8a805263477a 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -1304,9 +1304,24 @@ static void opDeleteGenerations(Globals & globals,
             for (Generations::iterator j = gens.begin(); j != gens.end(); ++j)
                 if (j->number != curGen)
                     deleteGeneration2(globals, j->number);
-        }
+        } else if (i->size() >= 2 && tolower(*i->rbegin()) == 'd') {
+            time_t curTime = time(NULL);
+            time_t oldTime;
+            string strDays = string(*i, 0, i->size() - 1);
+            int days;
 
-        else {
+            if (!string2Int(strDays, days) || days < 1)
+                throw UsageError(format("invalid number of days specifier `%1%'") % *i);
+
+            oldTime = curTime - days * 24 * 3600;
+
+            for (Generations::iterator j = gens.begin(); j != gens.end(); ++j) {
+                if (j->number == curGen) continue;
+
+                if (j->creationTime < oldTime)
+                    deleteGeneration2(globals, j->number);
+            }
+        } else {
             int n;
             if (!string2Int(*i, n) || n < 0)
                 throw UsageError(format("invalid generation specifier `%1%'")  % *i);