From 817562e6946fa44c253beb76a36d85b55fd9c14d Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 14 Sep 2021 19:05:28 +0200 Subject: [PATCH 1/5] Add "nix profile rollback" command --- src/libstore/profiles.cc | 31 +++++++++++++++++++++++++++++++ src/libstore/profiles.hh | 9 ++++++++- src/libutil/args.hh | 8 ++++++++ src/nix-env/nix-env.cc | 35 ++--------------------------------- src/nix/profile-rollback.md | 26 ++++++++++++++++++++++++++ src/nix/profile.cc | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 src/nix/profile-rollback.md diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index 84a21c0ba..9959da535 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -236,6 +236,37 @@ void switchLink(Path link, Path target) } +void switchGeneration( + const Path & profile, + std::optional dstGen, + bool dryRun) +{ + PathLocks lock; + lockProfile(lock, profile); + + auto [gens, curGen] = findGenerations(profile); + + std::optional dst; + for (auto & i : gens) + if ((!dstGen && i.number < curGen) || + (dstGen && i.number == *dstGen)) + dst = i; + + if (!dst) { + if (dstGen) + throw Error("generation %1% does not exist", *dstGen); + else + throw Error("no generation older than the current (%1%) exists", curGen.value_or(0)); + } + + notice("switching from generation %d to %d", curGen.value_or(0), dst->number); + + if (dryRun) return; + + switchLink(profile, dst->path); +} + + void lockProfile(PathLocks & lock, const Path & profile) { lock.lockPaths({profile}, (format("waiting for lock on profile '%1%'") % profile).str()); diff --git a/src/libstore/profiles.hh b/src/libstore/profiles.hh index be55a65d4..d100c970c 100644 --- a/src/libstore/profiles.hh +++ b/src/libstore/profiles.hh @@ -11,7 +11,7 @@ namespace nix { class StorePath; -typedef unsigned int GenerationNumber; +typedef uint64_t GenerationNumber; struct Generation { @@ -46,6 +46,13 @@ void deleteGenerationsOlderThan(const Path & profile, const string & timeSpec, b void switchLink(Path link, Path target); +/* Roll back a profile to the specified generation, or to the most + recent one older than the current. */ +void switchGeneration( + const Path & profile, + std::optional dstGen, + bool dryRun); + /* Ensure exclusive access to a profile. Any command that modifies the profile first acquires this lock. */ void lockProfile(PathLocks & lock, const Path & profile); diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 22c94b501..7521b3065 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -91,6 +91,14 @@ protected: }) , arity(1) { } + + template + Handler(std::optional * dest) + : fun([=](std::vector ss) { + *dest = string2IntWithUnitPrefix(ss[0]); + }) + , arity(1) + { } }; /* Options. */ diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index e04954d45..20bbc2064 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1204,37 +1204,6 @@ static void opSwitchProfile(Globals & globals, Strings opFlags, Strings opArgs) } -static constexpr GenerationNumber prevGen = std::numeric_limits::max(); - - -static void switchGeneration(Globals & globals, GenerationNumber dstGen) -{ - PathLocks lock; - lockProfile(lock, globals.profile); - - auto [gens, curGen] = findGenerations(globals.profile); - - std::optional dst; - for (auto & i : gens) - if ((dstGen == prevGen && i.number < curGen) || - (dstGen >= 0 && i.number == dstGen)) - dst = i; - - if (!dst) { - if (dstGen == prevGen) - throw Error("no generation older than the current (%1%) exists", curGen.value_or(0)); - else - throw Error("generation %1% does not exist", dstGen); - } - - printInfo("switching from generation %1% to %2%", curGen.value_or(0), dst->number); - - if (globals.dryRun) return; - - switchLink(globals.profile, dst->path); -} - - static void opSwitchGeneration(Globals & globals, Strings opFlags, Strings opArgs) { if (opFlags.size() > 0) @@ -1243,7 +1212,7 @@ static void opSwitchGeneration(Globals & globals, Strings opFlags, Strings opArg throw UsageError("exactly one argument expected"); if (auto dstGen = string2Int(opArgs.front())) - switchGeneration(globals, *dstGen); + switchGeneration(globals.profile, *dstGen, globals.dryRun); else throw UsageError("expected a generation number"); } @@ -1256,7 +1225,7 @@ static void opRollback(Globals & globals, Strings opFlags, Strings opArgs) if (opArgs.size() != 0) throw UsageError("no arguments expected"); - switchGeneration(globals, prevGen); + switchGeneration(globals.profile, {}, globals.dryRun); } diff --git a/src/nix/profile-rollback.md b/src/nix/profile-rollback.md new file mode 100644 index 000000000..fccdbb10e --- /dev/null +++ b/src/nix/profile-rollback.md @@ -0,0 +1,26 @@ +R""( + +# Examples + +* Roll back your default profile to the previous version: + + ```console + # nix profile rollback + switching from generation 519 to 518 + ``` + +* Switch your default profile to version 510: + + ```console + # nix profile rollback --to 510 + switching from generation 518 to 510 + ``` + +# Description + +This command switches a profile to the most recent version older +than the currently active version, or if `--to` *N* is given, to +version *N* of the profile. To see the available versions of a +profile, use `nix profile history`. + +)"" diff --git a/src/nix/profile.cc b/src/nix/profile.cc index 8cef6d0b6..4c4a9bff4 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -543,6 +543,38 @@ struct CmdProfileHistory : virtual StoreCommand, EvalCommand, MixDefaultProfile } }; +struct CmdProfileRollback : virtual StoreCommand, MixDefaultProfile, MixDryRun +{ + std::optional version; + + CmdProfileRollback() + { + addFlag({ + .longName = "to", + .description = "The profile version to roll back to.", + .labels = {"version"}, + .handler = {&version}, + }); + } + + std::string description() override + { + return "roll back to the previous version or a specified version of this profile"; + } + + std::string doc() override + { + return + #include "profile-rollback.md" + ; + } + + void run(ref store) override + { + switchGeneration(*profile, version, dryRun); + } +}; + struct CmdProfile : NixMultiCommand { CmdProfile() @@ -553,6 +585,7 @@ struct CmdProfile : NixMultiCommand {"list", []() { return make_ref(); }}, {"diff-closures", []() { return make_ref(); }}, {"history", []() { return make_ref(); }}, + {"rollback", []() { return make_ref(); }}, }) { } From 229ad612b8d678deee6c5077ff5a5f0b0085bd9e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 14 Sep 2021 19:48:16 +0200 Subject: [PATCH 2/5] Fix quotes --- src/nix-env/nix-env.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 20bbc2064..a86f55f84 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1265,12 +1265,12 @@ static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opAr } else if (opArgs.size() == 1 && opArgs.front().find('d') != string::npos) { deleteGenerationsOlderThan(globals.profile, opArgs.front(), globals.dryRun); } else if (opArgs.size() == 1 && opArgs.front().find('+') != string::npos) { - if(opArgs.front().size() < 2) - throw Error("invalid number of generations ‘%1%’", opArgs.front()); + if (opArgs.front().size() < 2) + throw Error("invalid number of generations '%1%'", opArgs.front()); string str_max = string(opArgs.front(), 1, opArgs.front().size()); auto max = string2Int(str_max); if (!max || *max == 0) - throw Error("invalid number of generations to keep ‘%1%’", opArgs.front()); + throw Error("invalid number of generations to keep '%1%'", opArgs.front()); deleteGenerationsGreaterThan(globals.profile, *max, globals.dryRun); } else { std::set gens; From f359b9981ba3d81e33981018c4bcb83ab45830a3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 14 Sep 2021 19:57:45 +0200 Subject: [PATCH 3/5] Generations -> profile versions --- src/libstore/profiles.cc | 12 ++++++------ src/nix/profile-rollback.md | 4 ++-- src/nix/profile.cc | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index 9959da535..aca0ea5ad 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -126,9 +126,9 @@ void deleteGeneration(const Path & profile, GenerationNumber gen) static void deleteGeneration2(const Path & profile, GenerationNumber gen, bool dryRun) { if (dryRun) - printInfo(format("would remove generation %1%") % gen); + printInfo(format("would remove profile version %1%") % gen); else { - printInfo(format("removing generation %1%") % gen); + printInfo(format("removing profile version %1%") % gen); deleteGeneration(profile, gen); } } @@ -142,7 +142,7 @@ void deleteGenerations(const Path & profile, const std::set & auto [gens, curGen] = findGenerations(profile); if (gensToDelete.count(*curGen)) - throw Error("cannot delete current generation of profile %1%'", profile); + throw Error("cannot delete current version of profile %1%'", profile); for (auto & i : gens) { if (!gensToDelete.count(i.number)) continue; @@ -254,12 +254,12 @@ void switchGeneration( if (!dst) { if (dstGen) - throw Error("generation %1% does not exist", *dstGen); + throw Error("profile version %1% does not exist", *dstGen); else - throw Error("no generation older than the current (%1%) exists", curGen.value_or(0)); + throw Error("no profile version older than the current (%1%) exists", curGen.value_or(0)); } - notice("switching from generation %d to %d", curGen.value_or(0), dst->number); + notice("switching profile from version %d to %d", curGen.value_or(0), dst->number); if (dryRun) return; diff --git a/src/nix/profile-rollback.md b/src/nix/profile-rollback.md index fccdbb10e..6bb75aa5e 100644 --- a/src/nix/profile-rollback.md +++ b/src/nix/profile-rollback.md @@ -6,14 +6,14 @@ R""( ```console # nix profile rollback - switching from generation 519 to 518 + switching profile from version 519 to 518 ``` * Switch your default profile to version 510: ```console # nix profile rollback --to 510 - switching from generation 518 to 510 + switching profile from version 518 to 510 ``` # Description diff --git a/src/nix/profile.cc b/src/nix/profile.cc index 4c4a9bff4..ab0e88fa1 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -559,7 +559,7 @@ struct CmdProfileRollback : virtual StoreCommand, MixDefaultProfile, MixDryRun std::string description() override { - return "roll back to the previous version or a specified version of this profile"; + return "roll back to the previous version or a specified version of a profile"; } std::string doc() override From 4b738fc7a922ff7b30135768ee4f469d784b5569 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 14 Sep 2021 20:35:12 +0200 Subject: [PATCH 4/5] Add 'nix profile wipe-history' command --- src/libstore/profiles.cc | 4 ++-- src/nix/profile-wipe-history.md | 20 +++++++++++++++++ src/nix/profile.cc | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/nix/profile-wipe-history.md diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index aca0ea5ad..73163424c 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -126,9 +126,9 @@ void deleteGeneration(const Path & profile, GenerationNumber gen) static void deleteGeneration2(const Path & profile, GenerationNumber gen, bool dryRun) { if (dryRun) - printInfo(format("would remove profile version %1%") % gen); + notice("would remove profile version %1%", gen); else { - printInfo(format("removing profile version %1%") % gen); + notice("removing profile version %1%", gen); deleteGeneration(profile, gen); } } diff --git a/src/nix/profile-wipe-history.md b/src/nix/profile-wipe-history.md new file mode 100644 index 000000000..b4b262864 --- /dev/null +++ b/src/nix/profile-wipe-history.md @@ -0,0 +1,20 @@ +R""( + +# Examples + +* Delete all versions of the default profile older than 100 days: + + ```console + # nix profile wipe-history --profile /tmp/profile --older-than 100d + removing profile version 515 + removing profile version 514 + ``` + +# Description + +This command deletes non-current versions of a profile, making it +impossible to roll back to these versions. By default, all non-current +versions are deleted. With `--older-than` *N*`d`, all non-current +versions older than *N* days are deleted. + +)"" diff --git a/src/nix/profile.cc b/src/nix/profile.cc index ab0e88fa1..6d5fa5cd0 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -575,6 +575,44 @@ struct CmdProfileRollback : virtual StoreCommand, MixDefaultProfile, MixDryRun } }; +struct CmdProfileWipeHistory : virtual StoreCommand, MixDefaultProfile, MixDryRun +{ + std::optional minAge; + + CmdProfileWipeHistory() + { + addFlag({ + .longName = "older-than", + .description = + "Delete versions older than the specified age. *age* " + "must be in the format *N*`d`, where *N* denotes a number " + "of days.", + .labels = {"age"}, + .handler = {&minAge}, + }); + } + + std::string description() override + { + return "delete non-current versions of a profile"; + } + + std::string doc() override + { + return + #include "profile-wipe-history.md" + ; + } + + void run(ref store) override + { + if (minAge) + deleteGenerationsOlderThan(*profile, *minAge, dryRun); + else + deleteOldGenerations(*profile, dryRun); + } +}; + struct CmdProfile : NixMultiCommand { CmdProfile() @@ -586,6 +624,7 @@ struct CmdProfile : NixMultiCommand {"diff-closures", []() { return make_ref(); }}, {"history", []() { return make_ref(); }}, {"rollback", []() { return make_ref(); }}, + {"wipe-history", []() { return make_ref(); }}, }) { } From b41968f15a6393a55b5c7ff5fe7eac4e33d94357 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 14 Sep 2021 20:47:33 +0200 Subject: [PATCH 5/5] nix profile history: Show profile date --- src/nix/profile-history.md | 4 ++-- src/nix/profile.cc | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/nix/profile-history.md b/src/nix/profile-history.md index d0fe40c82..f0bfe5037 100644 --- a/src/nix/profile-history.md +++ b/src/nix/profile-history.md @@ -6,10 +6,10 @@ R""( ```console # nix profile history - Version 508 -> 509: + Version 508 (2020-04-10): flake:nixpkgs#legacyPackages.x86_64-linux.awscli: ∅ -> 1.17.13 - Version 509 -> 510: + Version 509 (2020-05-16) <- 508: flake:nixpkgs#legacyPackages.x86_64-linux.awscli: 1.17.13 -> 1.18.211 ``` diff --git a/src/nix/profile.cc b/src/nix/profile.cc index 6d5fa5cd0..a1cb3fc76 100644 --- a/src/nix/profile.cc +++ b/src/nix/profile.cc @@ -12,6 +12,7 @@ #include #include +#include using namespace nix; @@ -528,10 +529,11 @@ struct CmdProfileHistory : virtual StoreCommand, EvalCommand, MixDefaultProfile if (!first) std::cout << "\n"; first = false; - if (prevGen) - std::cout << fmt("Version %d -> %d:\n", prevGen->first.number, gen.number); - else - std::cout << fmt("Version %d:\n", gen.number); + std::cout << fmt("Version %s%d" ANSI_NORMAL " (%s)%s:\n", + gen.number == curGen ? ANSI_GREEN : ANSI_BOLD, + gen.number, + std::put_time(std::gmtime(&gen.creationTime), "%Y-%m-%d"), + prevGen ? fmt(" <- %d", prevGen->first.number) : ""); ProfileManifest::printDiff( prevGen ? prevGen->second : ProfileManifest(),