about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/nix-env/help.txt4
-rw-r--r--src/nix-env/main.cc59
-rw-r--r--src/nix-env/profiles.hh8
3 files changed, 71 insertions, 0 deletions
diff --git a/src/nix-env/help.txt b/src/nix-env/help.txt
index 3832f1655b4e..9a772fc8330a 100644
--- a/src/nix-env/help.txt
+++ b/src/nix-env/help.txt
@@ -13,6 +13,10 @@ The previous operations take a list of derivation names.  The special
 name `*' may be used to indicate all derivations.
 
   --switch-profile / -S [FILE]: switch to specified profile
+  --switch-generation / -G NUMBER: switch to specified generation of profile
+  --rollback: switch to the previous generation
+  --list-generations: list available generations of a profile
+  
   --import / -I FILE: set default Nix expression
 
   --version: output version information
diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc
index 5ef61bb306a0..69b3fdd9c5cf 100644
--- a/src/nix-env/main.cc
+++ b/src/nix-env/main.cc
@@ -472,6 +472,61 @@ static void opSwitchProfile(Globals & globals,
 }
 
 
+static const int prevGen = -2;
+
+
+static void switchGeneration(Globals & globals, int dstGen)
+{
+    int curGen;
+    Generations gens = findGenerations(globals.profile, curGen);
+
+    Generation dst;
+    for (Generations::iterator i = gens.begin(); i != gens.end(); ++i)
+        if ((dstGen == prevGen && i->number < curGen) ||
+            (dstGen >= 0 && i->number == dstGen))
+            dst = *i;
+
+    if (!dst)
+        if (dstGen == prevGen)
+            throw Error(format("no generation older than the current (%1%) exists")
+                % curGen);
+        else
+            throw Error(format("generation %1% does not exist") % dstGen);
+
+    switchLink(globals.profile, dst.path);
+}
+
+
+static void opSwitchGeneration(Globals & globals,
+    Strings opFlags, Strings opArgs)
+{
+    if (opFlags.size() > 0)
+        throw UsageError(format("unknown flag `%1%'") % opFlags.front());
+    if (opArgs.size() != 1)
+        throw UsageError(format("exactly one argument expected"));
+
+    istringstream str(opArgs.front());
+    int dstGen;
+    str >> dstGen;
+    if (!str || !str.eof())
+        throw UsageError(format("expected a generation number"));
+
+    switchGeneration(globals, dstGen);
+}
+
+
+static void opRollback(Globals & globals,
+    Strings opFlags, Strings opArgs)
+{
+    if (opFlags.size() > 0)
+        throw UsageError(format("unknown flag `%1%'") % opFlags.front());
+    if (opArgs.size() != 0)
+        throw UsageError(format("no arguments expected"));
+
+    switchGeneration(globals, prevGen);
+}
+
+
 static void opListGenerations(Globals & globals,
     Strings opFlags, Strings opArgs)
 {
@@ -550,6 +605,10 @@ void run(Strings args)
         }
         else if (arg == "--switch-profile" || arg == "-S")
             op = opSwitchProfile;
+        else if (arg == "--switch-generation" || arg == "-G")
+            op = opSwitchGeneration;
+        else if (arg == "--rollback")
+            op = opRollback;
         else if (arg == "--list-generations")
             op = opListGenerations;
         else if (arg[0] == '-')
diff --git a/src/nix-env/profiles.hh b/src/nix-env/profiles.hh
index 2422c9f7425a..2ce468dfa837 100644
--- a/src/nix-env/profiles.hh
+++ b/src/nix-env/profiles.hh
@@ -11,6 +11,14 @@ struct Generation
     int number;
     Path path;
     time_t creationTime;
+    Generation()
+    {
+        number = -1;
+    }
+    operator bool() const
+    {
+        return number != -1;
+    }
 };
 
 typedef list<Generation> Generations;