nix-super/src/nix/profile.cc

804 lines
26 KiB
C++
Raw Normal View History

2019-10-22 01:21:58 +03:00
#include "command.hh"
#include "common-args.hh"
#include "shared.hh"
#include "store-api.hh"
#include "derivations.hh"
#include "archive.hh"
#include "builtins/buildenv.hh"
#include "flake/flakeref.hh"
2020-03-30 15:29:29 +03:00
#include "../nix-env/user-env.hh"
#include "profiles.hh"
#include "names.hh"
2019-10-22 01:21:58 +03:00
#include <nlohmann/json.hpp>
2019-10-22 14:06:32 +03:00
#include <regex>
2021-09-14 21:47:33 +03:00
#include <iomanip>
2019-10-22 01:21:58 +03:00
using namespace nix;
struct ProfileElementSource
{
FlakeRef originalRef;
2019-10-22 01:28:16 +03:00
// FIXME: record original attrpath.
2019-10-22 01:21:58 +03:00
FlakeRef resolvedRef;
std::string attrPath;
ExtendedOutputsSpec outputs;
bool operator < (const ProfileElementSource & other) const
{
return
std::tuple(originalRef.to_string(), attrPath, outputs) <
std::tuple(other.originalRef.to_string(), other.attrPath, other.outputs);
}
2019-10-22 01:21:58 +03:00
};
const int defaultPriority = 5;
2019-10-22 01:21:58 +03:00
struct ProfileElement
{
StorePathSet storePaths;
2019-10-22 01:21:58 +03:00
std::optional<ProfileElementSource> source;
bool active = true;
int priority = defaultPriority;
std::string describe() const
{
if (source)
return fmt("%s#%s%s", source->originalRef, source->attrPath, source->outputs.to_string());
StringSet names;
for (auto & path : storePaths)
names.insert(DrvName(path.name()).name);
return concatStringsSep(", ", names);
}
std::string versions() const
{
StringSet versions;
for (auto & path : storePaths)
versions.insert(DrvName(path.name()).version);
return showVersions(versions);
}
bool operator < (const ProfileElement & other) const
{
return std::tuple(describe(), storePaths) < std::tuple(other.describe(), other.storePaths);
}
2022-03-02 21:37:46 +02:00
void updateStorePaths(
ref<Store> evalStore,
ref<Store> store,
const BuiltPaths & builtPaths)
2022-03-02 21:37:46 +02:00
{
storePaths.clear();
for (auto & buildable : builtPaths) {
2022-03-02 21:37:46 +02:00
std::visit(overloaded {
[&](const BuiltPath::Opaque & bo) {
storePaths.insert(bo.path);
},
[&](const BuiltPath::Built & bfd) {
for (auto & output : bfd.outputs)
2022-03-02 21:37:46 +02:00
storePaths.insert(output.second);
},
}, buildable.raw());
}
}
2019-10-22 01:21:58 +03:00
};
struct ProfileManifest
{
std::vector<ProfileElement> elements;
2019-10-22 14:06:32 +03:00
ProfileManifest() { }
2019-10-22 16:16:57 +03:00
ProfileManifest(EvalState & state, const Path & profile)
2019-10-22 01:21:58 +03:00
{
auto manifestPath = profile + "/manifest.json";
if (pathExists(manifestPath)) {
auto json = nlohmann::json::parse(readFile(manifestPath));
auto version = json.value("version", 0);
std::string sUrl;
std::string sOriginalUrl;
switch (version) {
case 1:
sUrl = "uri";
sOriginalUrl = "originalUri";
break;
case 2:
sUrl = "url";
sOriginalUrl = "originalUrl";
break;
default:
throw Error("profile manifest '%s' has unsupported version %d", manifestPath, version);
}
2019-10-22 01:21:58 +03:00
for (auto & e : json["elements"]) {
ProfileElement element;
for (auto & p : e["storePaths"])
element.storePaths.insert(state.store->parseStorePath((std::string) p));
2019-10-22 01:21:58 +03:00
element.active = e["active"];
2022-05-11 13:15:08 +03:00
if(e.contains("priority")) {
element.priority = e["priority"];
}
if (e.value(sUrl, "") != "") {
element.source = ProfileElementSource {
parseFlakeRef(e[sOriginalUrl]),
parseFlakeRef(e[sUrl]),
e["attrPath"],
e["outputs"].get<ExtendedOutputsSpec>()
2019-10-22 01:21:58 +03:00
};
}
elements.emplace_back(std::move(element));
}
}
2019-10-22 16:16:57 +03:00
else if (pathExists(profile + "/manifest.nix")) {
// FIXME: needed because of pure mode; ugly.
2021-10-07 13:11:00 +03:00
state.allowPath(state.store->followLinksToStore(profile));
state.allowPath(state.store->followLinksToStore(profile + "/manifest.nix"));
2019-10-22 16:16:57 +03:00
auto drvInfos = queryInstalled(state, state.store->followLinksToStore(profile));
for (auto & drvInfo : drvInfos) {
ProfileElement element;
element.storePaths = {drvInfo.queryOutPath()};
2019-10-22 16:16:57 +03:00
elements.emplace_back(std::move(element));
}
}
2019-10-22 01:21:58 +03:00
}
std::string toJSON(Store & store) const
2019-10-22 01:21:58 +03:00
{
auto array = nlohmann::json::array();
for (auto & element : elements) {
auto paths = nlohmann::json::array();
for (auto & path : element.storePaths)
paths.push_back(store.printStorePath(path));
2019-10-22 01:21:58 +03:00
nlohmann::json obj;
obj["storePaths"] = paths;
obj["active"] = element.active;
2022-05-11 13:15:08 +03:00
obj["priority"] = element.priority;
2019-10-22 01:21:58 +03:00
if (element.source) {
obj["originalUrl"] = element.source->originalRef.to_string();
obj["url"] = element.source->resolvedRef.to_string();
2019-10-22 01:21:58 +03:00
obj["attrPath"] = element.source->attrPath;
obj["outputs"] = element.source->outputs;
2019-10-22 01:21:58 +03:00
}
array.push_back(obj);
}
nlohmann::json json;
json["version"] = 2;
2019-10-22 01:21:58 +03:00
json["elements"] = array;
return json.dump();
}
StorePath build(ref<Store> store)
2019-10-22 01:21:58 +03:00
{
auto tempDir = createTempDir();
StorePathSet references;
2019-10-22 01:21:58 +03:00
Packages pkgs;
for (auto & element : elements) {
for (auto & path : element.storePaths) {
if (element.active)
2022-05-11 13:15:08 +03:00
pkgs.emplace_back(store->printStorePath(path), true, element.priority);
references.insert(path);
2019-10-22 01:21:58 +03:00
}
}
buildProfile(tempDir, std::move(pkgs));
writeFile(tempDir + "/manifest.json", toJSON(*store));
2019-10-22 01:21:58 +03:00
/* Add the symlink tree to the store. */
StringSink sink;
dumpPath(tempDir, sink);
auto narHash = hashString(htSHA256, sink.s);
2019-12-15 00:09:57 +02:00
2020-08-06 21:31:48 +03:00
ValidPathInfo info {
store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, "profile", references),
narHash,
};
info.references = std::move(references);
info.narSize = sink.s.size();
info.ca = FixedOutputHash { .method = FileIngestionMethod::Recursive, .hash = info.narHash };
2019-10-22 01:21:58 +03:00
StringSource source(sink.s);
store->addToStore(info, source);
2019-10-22 01:21:58 +03:00
return std::move(info.path);
2019-10-22 01:21:58 +03:00
}
static void printDiff(const ProfileManifest & prev, const ProfileManifest & cur, std::string_view indent)
{
auto prevElems = prev.elements;
std::sort(prevElems.begin(), prevElems.end());
auto curElems = cur.elements;
std::sort(curElems.begin(), curElems.end());
auto i = prevElems.begin();
auto j = curElems.begin();
bool changes = false;
while (i != prevElems.end() || j != curElems.end()) {
if (j != curElems.end() && (i == prevElems.end() || i->describe() > j->describe())) {
std::cout << fmt("%s%s: ∅ -> %s\n", indent, j->describe(), j->versions());
changes = true;
++j;
}
else if (i != prevElems.end() && (j == curElems.end() || i->describe() < j->describe())) {
std::cout << fmt("%s%s: %s -> ∅\n", indent, i->describe(), i->versions());
changes = true;
++i;
}
else {
auto v1 = i->versions();
auto v2 = j->versions();
if (v1 != v2) {
std::cout << fmt("%s%s: %s -> %s\n", indent, i->describe(), v1, v2);
changes = true;
}
++i;
++j;
}
}
if (!changes)
std::cout << fmt("%sNo changes.\n", indent);
}
2019-10-22 01:21:58 +03:00
};
2023-01-10 15:52:49 +02:00
static std::map<Installable *, std::pair<BuiltPaths, ExtraPathInfo>>
builtPathsPerInstallable(
const std::vector<std::pair<std::shared_ptr<Installable>, BuiltPathWithResult>> & builtPaths)
{
2023-01-10 15:52:49 +02:00
std::map<Installable *, std::pair<BuiltPaths, ExtraPathInfo>> res;
for (auto & [installable, builtPath] : builtPaths) {
auto & r = res[installable.get()];
/* Note that there could be conflicting info
(e.g. meta.priority fields) if the installable returned
2023-01-10 16:20:30 +02:00
multiple derivations. So pick one arbitrarily. FIXME:
print a warning? */
r.first.push_back(builtPath.path);
r.second = builtPath.info;
}
return res;
}
2019-10-22 01:21:58 +03:00
struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
{
2022-05-25 16:05:39 +03:00
std::optional<int64_t> priority;
2022-05-11 13:15:08 +03:00
CmdProfileInstall() {
addFlag({
.longName = "priority",
.description = "The priority of the package to install.",
.labels = {"priority"},
2022-05-13 23:02:28 +03:00
.handler = {&priority},
2022-05-11 13:15:08 +03:00
});
};
2019-10-22 01:21:58 +03:00
std::string description() override
{
return "install a package into a profile";
}
2020-12-18 15:25:36 +02:00
std::string doc() override
2019-10-22 01:21:58 +03:00
{
2020-12-18 15:25:36 +02:00
return
#include "profile-install.md"
;
2019-10-22 01:21:58 +03:00
}
void run(ref<Store> store) override
{
2019-10-22 16:16:57 +03:00
ProfileManifest manifest(*getEvalState(), *profile);
2019-10-22 01:21:58 +03:00
auto builtPaths = builtPathsPerInstallable(
Installable::build2(
getEvalStore(), store, Realise::Outputs, installables, bmNormal));
2019-10-22 01:21:58 +03:00
for (auto & installable : installables) {
2022-03-02 21:37:46 +02:00
ProfileElement element;
auto & [res, info] = builtPaths[installable.get()];
2022-05-13 23:02:28 +03:00
if (info.originalRef && info.resolvedRef && info.attrPath && info.extendedOutputsSpec) {
element.source = ProfileElementSource {
.originalRef = *info.originalRef,
.resolvedRef = *info.resolvedRef,
.attrPath = *info.attrPath,
.outputs = *info.extendedOutputsSpec,
2019-10-22 01:21:58 +03:00
};
2022-03-02 21:37:46 +02:00
}
2019-10-22 01:21:58 +03:00
// If --priority was specified we want to override the
// priority of the installable.
element.priority =
priority
? *priority
: info.priority.value_or(defaultPriority);
2022-05-13 23:02:28 +03:00
element.updateStorePaths(getEvalStore(), store, res);
2019-10-22 01:21:58 +03:00
2022-03-02 21:37:46 +02:00
manifest.elements.push_back(std::move(element));
2019-10-22 01:21:58 +03:00
}
try {
updateProfile(manifest.build(store));
} catch (BuildEnvFileConflictError & conflictError) {
auto findRefByFilePath = [&]<typename Iterator>(Iterator begin, Iterator end) {
for (auto it = begin; it != end; it++) {
auto profileElement = *it;
for (auto & storePath : profileElement.storePaths) {
if (conflictError.fileA.starts_with(store->printStorePath(storePath))) {
return std::pair(conflictError.fileA, profileElement.source->originalRef);
}
if (conflictError.fileB.starts_with(store->printStorePath(storePath))) {
return std::pair(conflictError.fileB, profileElement.source->originalRef);
}
}
}
throw conflictError;
};
// There are 2 conflicting files. We need to find out which one is from the already installed package and
// which one is the package that is the new package that is being installed.
// The first matching package is the one that was already installed (original).
auto [originalConflictingFilePath, originalConflictingRef] = findRefByFilePath(manifest.elements.begin(), manifest.elements.end());
// The last matching package is the one that was going to be installed (new).
auto [newConflictingFilePath, newConflictingRef] = findRefByFilePath(manifest.elements.rbegin(), manifest.elements.rend());
throw Error(
"An existing package already provides the following file:\n"
"\n"
" %1%\n"
"\n"
"This is the conflicting file from the new package:\n"
"\n"
" %2%\n"
"\n"
"To remove the existing package:\n"
"\n"
" nix profile remove %3%\n"
"\n"
"The new package can also be installed next to the existing one by assigning a different priority.\n"
"The conflicting packages have a priority of %5%.\n"
"To prioritise the new package:\n"
"\n"
" nix profile install %4% --priority %6%\n"
"\n"
"To prioritise the existing package:\n"
"\n"
" nix profile install %4% --priority %7%\n",
originalConflictingFilePath,
newConflictingFilePath,
originalConflictingRef.to_string(),
newConflictingRef.to_string(),
conflictError.priority,
conflictError.priority - 1,
conflictError.priority + 1
);
}
2019-10-22 01:21:58 +03:00
}
};
2019-10-22 14:06:32 +03:00
class MixProfileElementMatchers : virtual Args
{
std::vector<std::string> _matchers;
public:
MixProfileElementMatchers()
{
expectArgs("elements", &_matchers);
}
struct RegexPattern {
std::string pattern;
std::regex reg;
};
typedef std::variant<size_t, Path, RegexPattern> Matcher;
2019-10-22 14:06:32 +03:00
std::vector<Matcher> getMatchers(ref<Store> store)
{
std::vector<Matcher> res;
for (auto & s : _matchers) {
2021-01-08 13:22:21 +02:00
if (auto n = string2Int<size_t>(s))
res.push_back(*n);
2019-10-22 14:06:32 +03:00
else if (store->isStorePath(s))
res.push_back(s);
else
res.push_back(RegexPattern{s,std::regex(s, std::regex::extended | std::regex::icase)});
2019-10-22 14:06:32 +03:00
}
return res;
}
bool matches(const Store & store, const ProfileElement & element, size_t pos, const std::vector<Matcher> & matchers)
2019-10-22 14:06:32 +03:00
{
for (auto & matcher : matchers) {
if (auto n = std::get_if<size_t>(&matcher)) {
if (*n == pos) return true;
} else if (auto path = std::get_if<Path>(&matcher)) {
if (element.storePaths.count(store.parseStorePath(*path))) return true;
} else if (auto regex = std::get_if<RegexPattern>(&matcher)) {
2019-10-22 14:06:32 +03:00
if (element.source
&& std::regex_match(element.source->attrPath, regex->reg))
2019-10-22 14:06:32 +03:00
return true;
}
}
return false;
}
};
2019-10-22 16:16:57 +03:00
struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElementMatchers
2019-10-22 14:06:32 +03:00
{
std::string description() override
{
return "remove packages from a profile";
}
2020-12-18 15:25:36 +02:00
std::string doc() override
2019-10-22 14:06:32 +03:00
{
2020-12-18 15:25:36 +02:00
return
#include "profile-remove.md"
;
2019-10-22 14:06:32 +03:00
}
void run(ref<Store> store) override
{
2019-10-22 16:16:57 +03:00
ProfileManifest oldManifest(*getEvalState(), *profile);
2019-10-22 14:06:32 +03:00
auto matchers = getMatchers(store);
ProfileManifest newManifest;
for (size_t i = 0; i < oldManifest.elements.size(); ++i) {
auto & element(oldManifest.elements[i]);
if (!matches(*store, element, i, matchers)) {
newManifest.elements.push_back(std::move(element));
} else {
notice("removing '%s'", element.describe());
}
2019-10-22 14:06:32 +03:00
}
auto removedCount = oldManifest.elements.size() - newManifest.elements.size();
2019-10-22 14:06:32 +03:00
printInfo("removed %d packages, kept %d packages",
removedCount,
2019-10-22 14:06:32 +03:00
newManifest.elements.size());
if (removedCount == 0) {
for (auto matcher: matchers) {
2022-03-02 12:46:15 +02:00
if (const size_t * index = std::get_if<size_t>(&matcher)){
warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){
warn("'%s' does not match any packages", regex->pattern);
}
}
2022-03-02 12:46:15 +02:00
warn ("Use 'nix profile list' to see the current profile.");
}
2019-10-22 14:06:32 +03:00
updateProfile(newManifest.build(store));
}
};
2019-10-22 15:44:51 +03:00
struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProfileElementMatchers
{
std::string description() override
{
return "upgrade packages using their most recent flake";
}
2020-12-18 15:25:36 +02:00
std::string doc() override
2019-10-22 15:44:51 +03:00
{
2020-12-18 15:25:36 +02:00
return
#include "profile-upgrade.md"
;
2019-10-22 15:44:51 +03:00
}
void run(ref<Store> store) override
{
2019-10-22 16:16:57 +03:00
ProfileManifest manifest(*getEvalState(), *profile);
2019-10-22 15:44:51 +03:00
auto matchers = getMatchers(store);
2022-03-02 21:37:46 +02:00
std::vector<std::shared_ptr<Installable>> installables;
std::vector<size_t> indices;
2019-10-22 15:44:51 +03:00
auto upgradedCount = 0;
2019-10-22 15:44:51 +03:00
for (size_t i = 0; i < manifest.elements.size(); ++i) {
auto & element(manifest.elements[i]);
if (element.source
&& !element.source->originalRef.input.isLocked()
&& matches(*store, element, i, matchers))
2019-10-22 15:44:51 +03:00
{
upgradedCount++;
2019-10-22 15:44:51 +03:00
Activity act(*logger, lvlChatty, actUnknown,
fmt("checking '%s' for updates", element.source->attrPath));
2022-03-02 21:37:46 +02:00
auto installable = std::make_shared<InstallableFlake>(
this,
getEvalState(),
FlakeRef(element.source->originalRef),
"",
element.source->outputs,
2022-03-02 21:37:46 +02:00
Strings{element.source->attrPath},
Strings{},
lockFlags);
2019-10-22 15:44:51 +03:00
auto derivedPaths = installable->toDerivedPaths();
if (derivedPaths.empty()) continue;
auto & info = derivedPaths[0].info;
assert(info.resolvedRef && info.attrPath);
2019-10-22 15:44:51 +03:00
if (element.source->resolvedRef == info.resolvedRef) continue;
2019-10-22 15:44:51 +03:00
printInfo("upgrading '%s' from flake '%s' to '%s'",
element.source->attrPath, element.source->resolvedRef, *info.resolvedRef);
2019-10-22 15:44:51 +03:00
element.source = ProfileElementSource {
.originalRef = installable->flakeRef,
.resolvedRef = *info.resolvedRef,
.attrPath = *info.attrPath,
.outputs = installable->extendedOutputsSpec,
2019-10-22 15:44:51 +03:00
};
2022-03-02 21:37:46 +02:00
installables.push_back(installable);
indices.push_back(i);
2019-10-22 15:44:51 +03:00
}
}
if (upgradedCount == 0) {
for (auto & matcher : matchers) {
2022-03-02 12:46:15 +02:00
if (const size_t * index = std::get_if<size_t>(&matcher)){
warn("'%d' is not a valid index", *index);
} else if (const Path * path = std::get_if<Path>(&matcher)){
warn("'%s' does not match any paths", *path);
} else if (const RegexPattern * regex = std::get_if<RegexPattern>(&matcher)){
warn("'%s' does not match any packages", regex->pattern);
}
}
warn ("Use 'nix profile list' to see the current profile.");
}
auto builtPaths = builtPathsPerInstallable(
Installable::build2(
getEvalStore(), store, Realise::Outputs, installables, bmNormal));
2022-03-02 21:37:46 +02:00
for (size_t i = 0; i < installables.size(); ++i) {
auto & installable = installables.at(i);
auto & element = manifest.elements[indices.at(i)];
element.updateStorePaths(getEvalStore(), store, builtPaths[installable.get()].first);
2022-03-02 21:37:46 +02:00
}
2019-10-22 15:44:51 +03:00
updateProfile(manifest.build(store));
}
};
2021-01-12 20:57:05 +02:00
struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultProfile
2019-10-22 01:21:58 +03:00
{
std::string description() override
{
2019-10-22 14:06:32 +03:00
return "list installed packages";
2019-10-22 01:21:58 +03:00
}
2020-12-18 15:25:36 +02:00
std::string doc() override
2019-10-22 01:21:58 +03:00
{
2020-12-18 15:25:36 +02:00
return
2021-01-12 20:57:05 +02:00
#include "profile-list.md"
2020-12-18 15:25:36 +02:00
;
2019-10-22 01:21:58 +03:00
}
void run(ref<Store> store) override
{
2019-10-22 16:16:57 +03:00
ProfileManifest manifest(*getEvalState(), *profile);
2019-10-22 01:21:58 +03:00
2019-10-22 01:28:16 +03:00
for (size_t i = 0; i < manifest.elements.size(); ++i) {
auto & element(manifest.elements[i]);
logger->cout("%d %s %s %s", i,
element.source ? element.source->originalRef.to_string() + "#" + element.source->attrPath + element.source->outputs.to_string() : "-",
element.source ? element.source->resolvedRef.to_string() + "#" + element.source->attrPath + element.source->outputs.to_string() : "-",
concatStringsSep(" ", store->printStorePathSet(element.storePaths)));
2019-10-22 01:21:58 +03:00
}
}
};
struct CmdProfileDiffClosures : virtual StoreCommand, MixDefaultProfile
{
std::string description() override
{
2020-12-18 15:25:36 +02:00
return "show the closure difference between each version of a profile";
}
2020-12-18 15:25:36 +02:00
std::string doc() override
{
2020-12-18 15:25:36 +02:00
return
#include "profile-diff-closures.md"
;
}
void run(ref<Store> store) override
{
auto [gens, curGen] = findGenerations(*profile);
std::optional<Generation> prevGen;
bool first = true;
for (auto & gen : gens) {
if (prevGen) {
if (!first) std::cout << "\n";
first = false;
2020-12-18 15:25:36 +02:00
std::cout << fmt("Version %d -> %d:\n", prevGen->number, gen.number);
printClosureDiff(store,
store->followLinksToStorePath(prevGen->path),
store->followLinksToStorePath(gen.path),
" ");
}
prevGen = gen;
}
}
};
struct CmdProfileHistory : virtual StoreCommand, EvalCommand, MixDefaultProfile
{
std::string description() override
{
return "show all versions of a profile";
}
std::string doc() override
{
return
#include "profile-history.md"
;
}
void run(ref<Store> store) override
{
auto [gens, curGen] = findGenerations(*profile);
std::optional<std::pair<Generation, ProfileManifest>> prevGen;
bool first = true;
for (auto & gen : gens) {
ProfileManifest manifest(*getEvalState(), gen.path);
if (!first) std::cout << "\n";
first = false;
2021-09-14 21:47:33 +03:00
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(),
manifest,
" ");
prevGen = {gen, std::move(manifest)};
}
}
};
2021-09-14 20:05:28 +03:00
struct CmdProfileRollback : virtual StoreCommand, MixDefaultProfile, MixDryRun
{
std::optional<GenerationNumber> version;
CmdProfileRollback()
{
addFlag({
.longName = "to",
.description = "The profile version to roll back to.",
.labels = {"version"},
.handler = {&version},
});
}
std::string description() override
{
2021-09-14 20:57:45 +03:00
return "roll back to the previous version or a specified version of a profile";
2021-09-14 20:05:28 +03:00
}
std::string doc() override
{
return
#include "profile-rollback.md"
;
}
void run(ref<Store> store) override
{
switchGeneration(*profile, version, dryRun);
}
};
2021-09-14 21:35:12 +03:00
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
2019-10-22 01:21:58 +03:00
{
CmdProfile()
: MultiCommand({
{"install", []() { return make_ref<CmdProfileInstall>(); }},
2019-10-22 14:06:32 +03:00
{"remove", []() { return make_ref<CmdProfileRemove>(); }},
2019-10-22 15:44:51 +03:00
{"upgrade", []() { return make_ref<CmdProfileUpgrade>(); }},
2021-01-12 20:57:05 +02:00
{"list", []() { return make_ref<CmdProfileList>(); }},
{"diff-closures", []() { return make_ref<CmdProfileDiffClosures>(); }},
{"history", []() { return make_ref<CmdProfileHistory>(); }},
2021-09-14 20:05:28 +03:00
{"rollback", []() { return make_ref<CmdProfileRollback>(); }},
2021-09-14 21:35:12 +03:00
{"wipe-history", []() { return make_ref<CmdProfileWipeHistory>(); }},
2019-10-22 01:21:58 +03:00
})
{ }
std::string description() override
{
return "manage Nix profiles";
}
2020-12-18 15:25:36 +02:00
std::string doc() override
{
return
#include "profile.md"
;
}
2019-10-22 01:21:58 +03:00
void run() override
{
if (!command)
throw UsageError("'nix profile' requires a sub-command.");
command->second->prepare();
command->second->run();
2019-10-22 01:21:58 +03:00
}
};
static auto rCmdProfile = registerCommand<CmdProfile>("profile");