Skip to content

Commit

Permalink
Add 'nix profile wipe-history' command
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Sep 14, 2021
1 parent f359b99 commit 4b738fc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libstore/profiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/nix/profile-wipe-history.md
Original file line number Diff line number Diff line change
@@ -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.

)""
39 changes: 39 additions & 0 deletions src/nix/profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,44 @@ struct CmdProfileRollback : virtual StoreCommand, MixDefaultProfile, MixDryRun
}
};

struct CmdProfileWipeHistory : virtual StoreCommand, MixDefaultProfile, MixDryRun
{
std::optional<std::string> 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> store) override
{
if (minAge)
deleteGenerationsOlderThan(*profile, *minAge, dryRun);
else
deleteOldGenerations(*profile, dryRun);
}
};

struct CmdProfile : NixMultiCommand
{
CmdProfile()
Expand All @@ -586,6 +624,7 @@ struct CmdProfile : NixMultiCommand
{"diff-closures", []() { return make_ref<CmdProfileDiffClosures>(); }},
{"history", []() { return make_ref<CmdProfileHistory>(); }},
{"rollback", []() { return make_ref<CmdProfileRollback>(); }},
{"wipe-history", []() { return make_ref<CmdProfileWipeHistory>(); }},
})
{ }

Expand Down

0 comments on commit 4b738fc

Please sign in to comment.