about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Theune <ct@flyingcircus.io>2015-05-18T06·38+0200
committerChristian Theune <ct@flyingcircus.io>2015-05-18T06·38+0200
commit3d8318870289d7b6b08677fcd2da6ceb0b082f8c (patch)
tree5c7ed0116d998bfd23891458db6dbd18a3205936
parentbe1ff233526cc90d36eb13fdee690cf508baeb7a (diff)
Enable lazy/sparse allocation of generation symlinks: avoid creating
new generations if a generation already exists.

Alternatively or additionally I propose a mode where only the *last* generation will be sparse.
-rw-r--r--src/nix-env/profiles.cc17
-rw-r--r--tests/user-envs.sh9
2 files changed, 25 insertions, 1 deletions
diff --git a/src/nix-env/profiles.cc b/src/nix-env/profiles.cc
index d8eb0ef5269c..1691dc099f4c 100644
--- a/src/nix-env/profiles.cc
+++ b/src/nix-env/profiles.cc
@@ -80,7 +80,22 @@ Path createGeneration(Path profile, Path outPath)
        previous ones. */
     int dummy;
     Generations gens = findGenerations(profile, dummy);
-    unsigned int num = gens.size() > 0 ? gens.back().number : 0;
+
+    unsigned int num;
+    if (gens.size() > 0) {
+        /* Check existing generations whether they represent an
+           environment we already materialized before.  In that case:
+           avoid cluttering the system with additional symlinks. */
+        for (auto & gen : gens) {
+            if (readLink(gen.path) == outPath) {
+                return gen.path;
+            }
+        }
+
+        num = gens.back().number;
+    } else {
+        num = 0;
+    }
 
     /* Create the new generation.  Note that addPermRoot() blocks if
        the garbage collector is running to prevent the stuff we've
diff --git a/tests/user-envs.sh b/tests/user-envs.sh
index c9eed5a6633d..ad15ddc3e441 100644
--- a/tests/user-envs.sh
+++ b/tests/user-envs.sh
@@ -99,6 +99,15 @@ if nix-env -q '*' | grep -q bar; then false; fi
 nix-env --list-generations
 test "$(nix-env --list-generations | wc -l)" -eq 7
 
+# Doing the same operation twice should result in the same generation, not an
+# additional one. At this point we just brought back foo. Installing it again
+# should not create a new generation.
+nix-env -i foo
+
+# Count generations.
+nix-env --list-generations
+test "$(nix-env --list-generations | wc -l)" -eq 7
+
 # Switch to a specified generation.
 nix-env --switch-generation 7
 [ "$(nix-store -q --resolve $profiles/test)" = "$oldGen" ]