2019-10-21 22:21:58 +00:00
|
|
|
#include "command.hh"
|
2023-02-03 19:53:40 +00:00
|
|
|
#include "installable-flake.hh"
|
2019-10-21 22:21:58 +00:00
|
|
|
#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 12:29:29 +00:00
|
|
|
#include "../nix-env/user-env.hh"
|
2020-07-16 15:00:42 +00:00
|
|
|
#include "profiles.hh"
|
2021-01-12 22:51:07 +00:00
|
|
|
#include "names.hh"
|
2019-10-21 22:21:58 +00:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2019-10-22 11:06:32 +00:00
|
|
|
#include <regex>
|
2021-09-14 18:47:33 +00:00
|
|
|
#include <iomanip>
|
2019-10-21 22:21:58 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
struct ProfileElementSource
|
|
|
|
{
|
|
|
|
FlakeRef originalRef;
|
2019-10-21 22:28:16 +00:00
|
|
|
// FIXME: record original attrpath.
|
2019-10-21 22:21:58 +00:00
|
|
|
FlakeRef resolvedRef;
|
|
|
|
std::string attrPath;
|
2023-01-11 07:00:44 +00:00
|
|
|
ExtendedOutputsSpec outputs;
|
2021-01-12 22:51:07 +00:00
|
|
|
|
|
|
|
bool operator < (const ProfileElementSource & other) const
|
|
|
|
{
|
|
|
|
return
|
2022-05-03 12:37:28 +00:00
|
|
|
std::tuple(originalRef.to_string(), attrPath, outputs) <
|
|
|
|
std::tuple(other.originalRef.to_string(), other.attrPath, other.outputs);
|
2021-01-12 22:51:07 +00:00
|
|
|
}
|
2019-10-21 22:21:58 +00:00
|
|
|
};
|
|
|
|
|
2022-12-15 21:09:32 +00:00
|
|
|
const int defaultPriority = 5;
|
|
|
|
|
2019-10-21 22:21:58 +00:00
|
|
|
struct ProfileElement
|
|
|
|
{
|
2019-12-11 13:53:30 +00:00
|
|
|
StorePathSet storePaths;
|
2019-10-21 22:21:58 +00:00
|
|
|
std::optional<ProfileElementSource> source;
|
|
|
|
bool active = true;
|
2022-12-15 21:09:32 +00:00
|
|
|
int priority = defaultPriority;
|
2021-01-12 22:51:07 +00:00
|
|
|
|
|
|
|
std::string describe() const
|
|
|
|
{
|
|
|
|
if (source)
|
2023-01-11 06:51:14 +00:00
|
|
|
return fmt("%s#%s%s", source->originalRef, source->attrPath, source->outputs.to_string());
|
2021-01-12 22:51:07 +00:00
|
|
|
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 19:37:46 +00:00
|
|
|
|
2022-03-28 12:21:35 +00:00
|
|
|
void updateStorePaths(
|
|
|
|
ref<Store> evalStore,
|
|
|
|
ref<Store> store,
|
|
|
|
const BuiltPaths & builtPaths)
|
2022-03-02 19:37:46 +00:00
|
|
|
{
|
|
|
|
storePaths.clear();
|
2022-03-28 12:21:35 +00:00
|
|
|
for (auto & buildable : builtPaths) {
|
2022-03-02 19:37:46 +00:00
|
|
|
std::visit(overloaded {
|
|
|
|
[&](const BuiltPath::Opaque & bo) {
|
|
|
|
storePaths.insert(bo.path);
|
|
|
|
},
|
|
|
|
[&](const BuiltPath::Built & bfd) {
|
2022-03-28 12:21:35 +00:00
|
|
|
for (auto & output : bfd.outputs)
|
2022-03-02 19:37:46 +00:00
|
|
|
storePaths.insert(output.second);
|
|
|
|
},
|
|
|
|
}, buildable.raw());
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 22:21:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ProfileManifest
|
|
|
|
{
|
|
|
|
std::vector<ProfileElement> elements;
|
|
|
|
|
2019-10-22 11:06:32 +00:00
|
|
|
ProfileManifest() { }
|
|
|
|
|
2019-10-22 13:16:57 +00:00
|
|
|
ProfileManifest(EvalState & state, const Path & profile)
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
|
|
|
auto manifestPath = profile + "/manifest.json";
|
|
|
|
|
|
|
|
if (pathExists(manifestPath)) {
|
|
|
|
auto json = nlohmann::json::parse(readFile(manifestPath));
|
|
|
|
|
|
|
|
auto version = json.value("version", 0);
|
2022-03-30 20:35:26 +00:00
|
|
|
std::string sUrl;
|
|
|
|
std::string sOriginalUrl;
|
2022-05-03 12:37:28 +00:00
|
|
|
switch (version) {
|
2022-03-30 20:35:26 +00:00
|
|
|
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-21 22:21:58 +00:00
|
|
|
|
|
|
|
for (auto & e : json["elements"]) {
|
|
|
|
ProfileElement element;
|
|
|
|
for (auto & p : e["storePaths"])
|
2019-12-11 13:53:30 +00:00
|
|
|
element.storePaths.insert(state.store->parseStorePath((std::string) p));
|
2019-10-21 22:21:58 +00:00
|
|
|
element.active = e["active"];
|
2022-05-11 10:15:08 +00:00
|
|
|
if(e.contains("priority")) {
|
|
|
|
element.priority = e["priority"];
|
|
|
|
}
|
2022-05-03 12:37:28 +00:00
|
|
|
if (e.value(sUrl, "") != "") {
|
|
|
|
element.source = ProfileElementSource {
|
2022-03-30 20:35:26 +00:00
|
|
|
parseFlakeRef(e[sOriginalUrl]),
|
|
|
|
parseFlakeRef(e[sUrl]),
|
2022-05-03 12:37:28 +00:00
|
|
|
e["attrPath"],
|
2023-01-11 07:00:44 +00:00
|
|
|
e["outputs"].get<ExtendedOutputsSpec>()
|
2019-10-21 22:21:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
elements.emplace_back(std::move(element));
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 13:16:57 +00:00
|
|
|
|
|
|
|
else if (pathExists(profile + "/manifest.nix")) {
|
|
|
|
// FIXME: needed because of pure mode; ugly.
|
2021-10-07 10:11:00 +00:00
|
|
|
state.allowPath(state.store->followLinksToStore(profile));
|
|
|
|
state.allowPath(state.store->followLinksToStore(profile + "/manifest.nix"));
|
2019-10-22 13:16:57 +00:00
|
|
|
|
|
|
|
auto drvInfos = queryInstalled(state, state.store->followLinksToStore(profile));
|
|
|
|
|
|
|
|
for (auto & drvInfo : drvInfos) {
|
|
|
|
ProfileElement element;
|
2022-03-02 09:57:19 +00:00
|
|
|
element.storePaths = {drvInfo.queryOutPath()};
|
2019-10-22 13:16:57 +00:00
|
|
|
elements.emplace_back(std::move(element));
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
std::string toJSON(Store & store) const
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
|
|
|
auto array = nlohmann::json::array();
|
|
|
|
for (auto & element : elements) {
|
|
|
|
auto paths = nlohmann::json::array();
|
|
|
|
for (auto & path : element.storePaths)
|
2019-12-11 13:53:30 +00:00
|
|
|
paths.push_back(store.printStorePath(path));
|
2019-10-21 22:21:58 +00:00
|
|
|
nlohmann::json obj;
|
|
|
|
obj["storePaths"] = paths;
|
|
|
|
obj["active"] = element.active;
|
2022-05-11 10:15:08 +00:00
|
|
|
obj["priority"] = element.priority;
|
2019-10-21 22:21:58 +00:00
|
|
|
if (element.source) {
|
2022-03-24 11:28:38 +00:00
|
|
|
obj["originalUrl"] = element.source->originalRef.to_string();
|
2022-03-30 20:35:26 +00:00
|
|
|
obj["url"] = element.source->resolvedRef.to_string();
|
2019-10-21 22:21:58 +00:00
|
|
|
obj["attrPath"] = element.source->attrPath;
|
2022-05-03 12:37:28 +00:00
|
|
|
obj["outputs"] = element.source->outputs;
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
array.push_back(obj);
|
|
|
|
}
|
|
|
|
nlohmann::json json;
|
2022-03-30 20:35:26 +00:00
|
|
|
json["version"] = 2;
|
2019-10-21 22:21:58 +00:00
|
|
|
json["elements"] = array;
|
|
|
|
return json.dump();
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
StorePath build(ref<Store> store)
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
|
|
|
auto tempDir = createTempDir();
|
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
StorePathSet references;
|
2019-10-21 22:21:58 +00:00
|
|
|
|
|
|
|
Packages pkgs;
|
|
|
|
for (auto & element : elements) {
|
|
|
|
for (auto & path : element.storePaths) {
|
|
|
|
if (element.active)
|
2022-05-11 10:15:08 +00:00
|
|
|
pkgs.emplace_back(store->printStorePath(path), true, element.priority);
|
2020-06-17 08:26:52 +00:00
|
|
|
references.insert(path);
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildProfile(tempDir, std::move(pkgs));
|
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
writeFile(tempDir + "/manifest.json", toJSON(*store));
|
2019-10-21 22:21:58 +00:00
|
|
|
|
|
|
|
/* Add the symlink tree to the store. */
|
|
|
|
StringSink sink;
|
|
|
|
dumpPath(tempDir, sink);
|
|
|
|
|
2022-01-17 21:20:05 +00:00
|
|
|
auto narHash = hashString(htSHA256, sink.s);
|
2019-12-14 22:09:57 +00:00
|
|
|
|
2020-08-06 18:31:48 +00:00
|
|
|
ValidPathInfo info {
|
|
|
|
store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, "profile", references),
|
|
|
|
narHash,
|
|
|
|
};
|
2019-12-11 13:53:30 +00:00
|
|
|
info.references = std::move(references);
|
2022-01-17 21:20:05 +00:00
|
|
|
info.narSize = sink.s.size();
|
2020-08-05 18:42:48 +00:00
|
|
|
info.ca = FixedOutputHash { .method = FileIngestionMethod::Recursive, .hash = info.narHash };
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2022-01-17 21:20:05 +00:00
|
|
|
StringSource source(sink.s);
|
2020-06-03 14:15:22 +00:00
|
|
|
store->addToStore(info, source);
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
return std::move(info.path);
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
2021-01-12 22:51:07 +00: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())) {
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("%s%s: ∅ -> %s", indent, j->describe(), j->versions());
|
2021-01-12 22:51:07 +00:00
|
|
|
changes = true;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
else if (i != prevElems.end() && (j == curElems.end() || i->describe() < j->describe())) {
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("%s%s: %s -> ∅", indent, i->describe(), i->versions());
|
2021-01-12 22:51:07 +00:00
|
|
|
changes = true;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto v1 = i->versions();
|
|
|
|
auto v2 = j->versions();
|
|
|
|
if (v1 != v2) {
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("%s%s: %s -> %s", indent, i->describe(), v1, v2);
|
2021-01-12 22:51:07 +00:00
|
|
|
changes = true;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changes)
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("%sNo changes.", indent);
|
2021-01-12 22:51:07 +00:00
|
|
|
}
|
2019-10-21 22:21:58 +00:00
|
|
|
};
|
|
|
|
|
2023-02-06 04:28:18 +00:00
|
|
|
static std::map<Installable *, std::pair<BuiltPaths, ref<ExtraPathInfo>>>
|
2022-03-28 12:21:35 +00:00
|
|
|
builtPathsPerInstallable(
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
const std::vector<std::pair<ref<Installable>, BuiltPathWithResult>> & builtPaths)
|
2022-03-28 12:21:35 +00:00
|
|
|
{
|
2023-02-06 04:28:18 +00:00
|
|
|
std::map<Installable *, std::pair<BuiltPaths, ref<ExtraPathInfo>>> res;
|
2022-12-15 21:09:32 +00:00
|
|
|
for (auto & [installable, builtPath] : builtPaths) {
|
2023-02-06 04:28:18 +00:00
|
|
|
auto & r = res.insert({
|
|
|
|
&*installable,
|
|
|
|
{
|
|
|
|
{},
|
|
|
|
make_ref<ExtraPathInfo>(),
|
|
|
|
}
|
|
|
|
}).first->second;
|
2022-12-15 21:09:32 +00:00
|
|
|
/* Note that there could be conflicting info
|
|
|
|
(e.g. meta.priority fields) if the installable returned
|
2023-01-10 14:20:30 +00:00
|
|
|
multiple derivations. So pick one arbitrarily. FIXME:
|
|
|
|
print a warning? */
|
2022-12-15 21:09:32 +00:00
|
|
|
r.first.push_back(builtPath.path);
|
|
|
|
r.second = builtPath.info;
|
|
|
|
}
|
2022-03-28 12:21:35 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-10-21 22:21:58 +00:00
|
|
|
struct CmdProfileInstall : InstallablesCommand, MixDefaultProfile
|
|
|
|
{
|
2022-05-25 13:05:39 +00:00
|
|
|
std::optional<int64_t> priority;
|
|
|
|
|
2022-05-11 10:15:08 +00:00
|
|
|
CmdProfileInstall() {
|
|
|
|
addFlag({
|
|
|
|
.longName = "priority",
|
|
|
|
.description = "The priority of the package to install.",
|
|
|
|
.labels = {"priority"},
|
2022-05-13 20:02:28 +00:00
|
|
|
.handler = {&priority},
|
2022-05-11 10:15:08 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-21 22:21:58 +00:00
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "install a package into a profile";
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return
|
|
|
|
#include "profile-install.md"
|
|
|
|
;
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
void run(ref<Store> store, Installables && installables) override
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
2019-10-22 13:16:57 +00:00
|
|
|
ProfileManifest manifest(*getEvalState(), *profile);
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2022-03-28 12:21:35 +00:00
|
|
|
auto builtPaths = builtPathsPerInstallable(
|
|
|
|
Installable::build2(
|
|
|
|
getEvalStore(), store, Realise::Outputs, installables, bmNormal));
|
2019-10-21 22:21:58 +00:00
|
|
|
|
|
|
|
for (auto & installable : installables) {
|
2022-03-02 19:37:46 +00:00
|
|
|
ProfileElement element;
|
|
|
|
|
2023-02-06 04:28:18 +00:00
|
|
|
auto iter = builtPaths.find(&*installable);
|
|
|
|
if (iter == builtPaths.end()) continue;
|
|
|
|
auto & [res, info] = iter->second;
|
2022-05-13 20:02:28 +00:00
|
|
|
|
2023-02-06 04:28:18 +00:00
|
|
|
if (auto * info2 = dynamic_cast<ExtraPathInfoFlake *>(&*info)) {
|
2022-05-03 12:37:28 +00:00
|
|
|
element.source = ProfileElementSource {
|
2023-02-06 04:28:18 +00:00
|
|
|
.originalRef = info2->flake.originalRef,
|
|
|
|
.resolvedRef = info2->flake.resolvedRef,
|
|
|
|
.attrPath = info2->value.attrPath,
|
|
|
|
.outputs = info2->value.extendedOutputsSpec,
|
2019-10-21 22:21:58 +00:00
|
|
|
};
|
2022-03-02 19:37:46 +00:00
|
|
|
}
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2022-12-15 21:09:32 +00:00
|
|
|
// If --priority was specified we want to override the
|
|
|
|
// priority of the installable.
|
|
|
|
element.priority =
|
|
|
|
priority
|
|
|
|
? *priority
|
2023-02-06 04:28:18 +00:00
|
|
|
: ({
|
|
|
|
auto * info2 = dynamic_cast<ExtraPathInfoValue *>(&*info);
|
|
|
|
info2
|
|
|
|
? info2->value.priority.value_or(defaultPriority)
|
|
|
|
: defaultPriority;
|
|
|
|
});
|
2022-05-13 20:02:28 +00:00
|
|
|
|
2022-12-15 21:09:32 +00:00
|
|
|
element.updateStorePaths(getEvalStore(), store, res);
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2022-03-02 19:37:46 +00:00
|
|
|
manifest.elements.push_back(std::move(element));
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 13:18:27 +00:00
|
|
|
try {
|
|
|
|
updateProfile(manifest.build(store));
|
|
|
|
} catch (BuildEnvFileConflictError & conflictError) {
|
2023-03-01 06:40:44 +00:00
|
|
|
// FIXME use C++20 std::ranges once macOS has it
|
|
|
|
// See https://github.com/NixOS/nix/compare/3efa476c5439f8f6c1968a6ba20a31d1239c2f04..1fe5d172ece51a619e879c4b86f603d9495cc102
|
2023-02-10 13:18:27 +00:00
|
|
|
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-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 11:06:32 +00:00
|
|
|
class MixProfileElementMatchers : virtual Args
|
|
|
|
{
|
|
|
|
std::vector<std::string> _matchers;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
MixProfileElementMatchers()
|
|
|
|
{
|
|
|
|
expectArgs("elements", &_matchers);
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:23:19 +00:00
|
|
|
struct RegexPattern {
|
|
|
|
std::string pattern;
|
|
|
|
std::regex reg;
|
|
|
|
};
|
|
|
|
typedef std::variant<size_t, Path, RegexPattern> Matcher;
|
2019-10-22 11:06:32 +00:00
|
|
|
|
|
|
|
std::vector<Matcher> getMatchers(ref<Store> store)
|
|
|
|
{
|
|
|
|
std::vector<Matcher> res;
|
|
|
|
|
|
|
|
for (auto & s : _matchers) {
|
2021-01-08 11:22:21 +00:00
|
|
|
if (auto n = string2Int<size_t>(s))
|
|
|
|
res.push_back(*n);
|
2019-10-22 11:06:32 +00:00
|
|
|
else if (store->isStorePath(s))
|
|
|
|
res.push_back(s);
|
|
|
|
else
|
2022-02-11 15:23:19 +00:00
|
|
|
res.push_back(RegexPattern{s,std::regex(s, std::regex::extended | std::regex::icase)});
|
2019-10-22 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:53:30 +00:00
|
|
|
bool matches(const Store & store, const ProfileElement & element, size_t pos, const std::vector<Matcher> & matchers)
|
2019-10-22 11:06:32 +00: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)) {
|
2019-12-11 13:53:30 +00:00
|
|
|
if (element.storePaths.count(store.parseStorePath(*path))) return true;
|
2022-02-11 15:23:19 +00:00
|
|
|
} else if (auto regex = std::get_if<RegexPattern>(&matcher)) {
|
2019-10-22 11:06:32 +00:00
|
|
|
if (element.source
|
2022-02-11 15:23:19 +00:00
|
|
|
&& std::regex_match(element.source->attrPath, regex->reg))
|
2019-10-22 11:06:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 13:16:57 +00:00
|
|
|
struct CmdProfileRemove : virtual EvalCommand, MixDefaultProfile, MixProfileElementMatchers
|
2019-10-22 11:06:32 +00:00
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "remove packages from a profile";
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
2019-10-22 11:06:32 +00:00
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return
|
|
|
|
#include "profile-remove.md"
|
|
|
|
;
|
2019-10-22 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-10-22 13:16:57 +00:00
|
|
|
ProfileManifest oldManifest(*getEvalState(), *profile);
|
2019-10-22 11:06:32 +00:00
|
|
|
|
|
|
|
auto matchers = getMatchers(store);
|
|
|
|
|
|
|
|
ProfileManifest newManifest;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < oldManifest.elements.size(); ++i) {
|
|
|
|
auto & element(oldManifest.elements[i]);
|
2022-02-11 15:23:19 +00:00
|
|
|
if (!matches(*store, element, i, matchers)) {
|
2019-12-11 13:53:30 +00:00
|
|
|
newManifest.elements.push_back(std::move(element));
|
2022-02-11 15:23:19 +00:00
|
|
|
} else {
|
|
|
|
notice("removing '%s'", element.describe());
|
|
|
|
}
|
2019-10-22 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 15:23:19 +00:00
|
|
|
auto removedCount = oldManifest.elements.size() - newManifest.elements.size();
|
2019-10-22 11:06:32 +00:00
|
|
|
printInfo("removed %d packages, kept %d packages",
|
2022-02-11 15:23:19 +00:00
|
|
|
removedCount,
|
2019-10-22 11:06:32 +00:00
|
|
|
newManifest.elements.size());
|
|
|
|
|
2022-02-11 15:23:19 +00:00
|
|
|
if (removedCount == 0) {
|
|
|
|
for (auto matcher: matchers) {
|
2022-03-02 10:46:15 +00: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-02-11 15:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-02 10:46:15 +00:00
|
|
|
warn ("Use 'nix profile list' to see the current profile.");
|
2022-02-11 15:23:19 +00:00
|
|
|
}
|
2019-10-22 11:06:32 +00:00
|
|
|
updateProfile(newManifest.build(store));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 12:44:51 +00:00
|
|
|
struct CmdProfileUpgrade : virtual SourceExprCommand, MixDefaultProfile, MixProfileElementMatchers
|
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "upgrade packages using their most recent flake";
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
2019-10-22 12:44:51 +00:00
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return
|
|
|
|
#include "profile-upgrade.md"
|
|
|
|
;
|
2019-10-22 12:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-10-22 13:16:57 +00:00
|
|
|
ProfileManifest manifest(*getEvalState(), *profile);
|
2019-10-22 12:44:51 +00:00
|
|
|
|
|
|
|
auto matchers = getMatchers(store);
|
|
|
|
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
Installables installables;
|
2022-03-02 19:37:46 +00:00
|
|
|
std::vector<size_t> indices;
|
2019-10-22 12:44:51 +00:00
|
|
|
|
2022-02-21 05:46:11 +00:00
|
|
|
auto upgradedCount = 0;
|
|
|
|
|
2019-10-22 12:44:51 +00:00
|
|
|
for (size_t i = 0; i < manifest.elements.size(); ++i) {
|
|
|
|
auto & element(manifest.elements[i]);
|
|
|
|
if (element.source
|
2022-02-24 17:09:00 +00:00
|
|
|
&& !element.source->originalRef.input.isLocked()
|
2019-12-11 13:53:30 +00:00
|
|
|
&& matches(*store, element, i, matchers))
|
2019-10-22 12:44:51 +00:00
|
|
|
{
|
2022-02-21 05:46:11 +00:00
|
|
|
upgradedCount++;
|
|
|
|
|
2019-10-22 12:44:51 +00:00
|
|
|
Activity act(*logger, lvlChatty, actUnknown,
|
|
|
|
fmt("checking '%s' for updates", element.source->attrPath));
|
|
|
|
|
Make command infra less stateful and more regular
Already, we had classes like `BuiltPathsCommand` and `StorePathsCommand`
which provided alternative `run` virtual functions providing the
implementation with more arguments. This was a very nice and easy way to
make writing command; just fill in the virtual functions and it is
fairly clear what to do.
However, exception to this pattern were `Installable{,s}Command`. These
two classes instead just had a field where the installables would be
stored, and various side-effecting `prepare` and `load` machinery too
fill them in. Command would wish out those fields.
This isn't so clear to use.
What this commit does is make those command classes like the others,
with richer `run` functions.
Not only does this restore the pattern making commands easier to write,
it has a number of other benefits:
- `prepare` and `load` are gone entirely! One command just hands just
hands off to the next.
- `useDefaultInstallables` because `defaultInstallables`. This takes
over `prepare` for the one case that needs it, and provides enough
flexiblity to handle `nix repl`'s idiosyncratic migration.
- We can use `ref` instead of `std::shared_ptr`. The former must be
initialized (so it is like Rust's `Box` rather than `Option<Box>`,
This expresses the invariant that the installable are in fact
initialized much better.
This is possible because since we just have local variables not
fields, we can stop worrying about the not-yet-initialized case.
- Fewer lines of code! (Finally I have a large refactor that makes the
number go down not up...)
- `nix repl` is now implemented in a clearer way.
The last item deserves further mention. `nix repl` is not like the other
installable commands because instead working from once-loaded
installables, it needs to be able to load them again and again.
To properly support this, we make a new superclass
`RawInstallablesCommand`. This class has the argument parsing and
completion logic, but does *not* hand off parsed installables but
instead just the raw string arguments.
This is exactly what `nix repl` needs, and allows us to instead of
having the logic awkwardly split between `prepare`,
`useDefaultInstallables,` and `load`, have everything right next to each
other. I think this will enable future simplifications of that argument
defaulting logic, but I am saving those for a future PR --- best to keep
code motion and more complicated boolean expression rewriting separate
steps.
The "diagnostic ignored `-Woverloaded-virtual`" pragma helps because C++
doesn't like our many `run` methods. In our case, we don't mind the
shadowing it all --- it is *intentional* that the derived class only
provides a `run` method, and doesn't call any of the overridden `run`
methods.
Helps with https://github.com/NixOS/rfcs/pull/134
2023-02-04 17:03:47 +00:00
|
|
|
auto installable = make_ref<InstallableFlake>(
|
2021-02-17 16:32:10 +00:00
|
|
|
this,
|
|
|
|
getEvalState(),
|
|
|
|
FlakeRef(element.source->originalRef),
|
2022-02-14 19:39:44 +00:00
|
|
|
"",
|
2022-05-03 12:37:28 +00:00
|
|
|
element.source->outputs,
|
2022-03-02 19:37:46 +00:00
|
|
|
Strings{element.source->attrPath},
|
|
|
|
Strings{},
|
2021-02-17 16:32:10 +00:00
|
|
|
lockFlags);
|
2019-10-22 12:44:51 +00:00
|
|
|
|
2022-12-15 21:09:32 +00:00
|
|
|
auto derivedPaths = installable->toDerivedPaths();
|
|
|
|
if (derivedPaths.empty()) continue;
|
2023-02-06 04:28:18 +00:00
|
|
|
auto * infop = dynamic_cast<ExtraPathInfoFlake *>(&*derivedPaths[0].info);
|
|
|
|
// `InstallableFlake` should use `ExtraPathInfoFlake`.
|
|
|
|
assert(infop);
|
|
|
|
auto & info = *infop;
|
2019-10-22 12:44:51 +00:00
|
|
|
|
2023-02-06 04:28:18 +00:00
|
|
|
if (element.source->resolvedRef == info.flake.resolvedRef) continue;
|
2019-10-22 12:44:51 +00:00
|
|
|
|
|
|
|
printInfo("upgrading '%s' from flake '%s' to '%s'",
|
2023-02-06 04:28:18 +00:00
|
|
|
element.source->attrPath, element.source->resolvedRef, info.flake.resolvedRef);
|
2019-10-22 12:44:51 +00:00
|
|
|
|
2022-05-03 12:37:28 +00:00
|
|
|
element.source = ProfileElementSource {
|
2022-12-15 21:09:32 +00:00
|
|
|
.originalRef = installable->flakeRef,
|
2023-02-06 04:28:18 +00:00
|
|
|
.resolvedRef = info.flake.resolvedRef,
|
|
|
|
.attrPath = info.value.attrPath,
|
2023-01-11 07:00:44 +00:00
|
|
|
.outputs = installable->extendedOutputsSpec,
|
2019-10-22 12:44:51 +00:00
|
|
|
};
|
|
|
|
|
2022-03-02 19:37:46 +00:00
|
|
|
installables.push_back(installable);
|
|
|
|
indices.push_back(i);
|
2019-10-22 12:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 05:46:11 +00:00
|
|
|
if (upgradedCount == 0) {
|
2022-02-21 16:04:04 +00:00
|
|
|
for (auto & matcher : matchers) {
|
2022-03-02 10:46:15 +00: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-02-21 05:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 16:04:04 +00:00
|
|
|
warn ("Use 'nix profile list' to see the current profile.");
|
2022-02-21 05:46:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 12:21:35 +00:00
|
|
|
auto builtPaths = builtPathsPerInstallable(
|
|
|
|
Installable::build2(
|
|
|
|
getEvalStore(), store, Realise::Outputs, installables, bmNormal));
|
2022-03-02 19:37:46 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < installables.size(); ++i) {
|
|
|
|
auto & installable = installables.at(i);
|
|
|
|
auto & element = manifest.elements[indices.at(i)];
|
2023-02-06 04:28:18 +00:00
|
|
|
element.updateStorePaths(
|
|
|
|
getEvalStore(),
|
|
|
|
store,
|
|
|
|
builtPaths.find(&*installable)->second.first);
|
2022-03-02 19:37:46 +00:00
|
|
|
}
|
2019-10-22 12:44:51 +00:00
|
|
|
|
|
|
|
updateProfile(manifest.build(store));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-12 18:57:05 +00:00
|
|
|
struct CmdProfileList : virtual EvalCommand, virtual StoreCommand, MixDefaultProfile
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
2019-10-22 11:06:32 +00:00
|
|
|
return "list installed packages";
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return
|
2021-01-12 18:57:05 +00:00
|
|
|
#include "profile-list.md"
|
2020-12-18 13:25:36 +00:00
|
|
|
;
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-10-22 13:16:57 +00:00
|
|
|
ProfileManifest manifest(*getEvalState(), *profile);
|
2019-10-21 22:21:58 +00:00
|
|
|
|
2019-10-21 22:28:16 +00:00
|
|
|
for (size_t i = 0; i < manifest.elements.size(); ++i) {
|
|
|
|
auto & element(manifest.elements[i]);
|
2020-09-25 15:30:04 +00:00
|
|
|
logger->cout("%d %s %s %s", i,
|
2023-01-11 06:51:14 +00:00
|
|
|
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() : "-",
|
2019-12-11 13:53:30 +00:00
|
|
|
concatStringsSep(" ", store->printStorePathSet(element.storePaths)));
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-17 10:36:12 +00:00
|
|
|
struct CmdProfileDiffClosures : virtual StoreCommand, MixDefaultProfile
|
2020-07-16 15:00:42 +00:00
|
|
|
{
|
|
|
|
std::string description() override
|
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return "show the closure difference between each version of a profile";
|
2020-07-16 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
2020-07-16 15:00:42 +00:00
|
|
|
{
|
2020-12-18 13:25:36 +00:00
|
|
|
return
|
|
|
|
#include "profile-diff-closures.md"
|
|
|
|
;
|
2020-07-16 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
|
|
|
auto [gens, curGen] = findGenerations(*profile);
|
|
|
|
|
|
|
|
std::optional<Generation> prevGen;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for (auto & gen : gens) {
|
|
|
|
if (prevGen) {
|
2023-03-02 14:02:24 +00:00
|
|
|
if (!first) logger->cout("");
|
2020-07-16 15:00:42 +00:00
|
|
|
first = false;
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("Version %d -> %d:", prevGen->number, gen.number);
|
2020-07-16 15:00:42 +00:00
|
|
|
printClosureDiff(store,
|
|
|
|
store->followLinksToStorePath(prevGen->path),
|
|
|
|
store->followLinksToStorePath(gen.path),
|
|
|
|
" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
prevGen = gen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-12 22:51:07 +00:00
|
|
|
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);
|
|
|
|
|
2023-03-02 14:02:24 +00:00
|
|
|
if (!first) logger->cout("");
|
2021-01-12 22:51:07 +00:00
|
|
|
first = false;
|
|
|
|
|
2023-03-02 14:02:24 +00:00
|
|
|
logger->cout("Version %s%d" ANSI_NORMAL " (%s)%s:",
|
2021-09-14 18:47:33 +00:00
|
|
|
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) : "");
|
2021-01-12 22:51:07 +00:00
|
|
|
|
|
|
|
ProfileManifest::printDiff(
|
|
|
|
prevGen ? prevGen->second : ProfileManifest(),
|
|
|
|
manifest,
|
|
|
|
" ");
|
|
|
|
|
|
|
|
prevGen = {gen, std::move(manifest)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-14 17:05:28 +00: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 17:57:45 +00:00
|
|
|
return "roll back to the previous version or a specified version of a profile";
|
2021-09-14 17:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string doc() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "profile-rollback.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
|
|
|
switchGeneration(*profile, version, dryRun);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-14 18:35:12 +00: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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-17 15:44:52 +00:00
|
|
|
struct CmdProfile : NixMultiCommand
|
2019-10-21 22:21:58 +00:00
|
|
|
{
|
|
|
|
CmdProfile()
|
|
|
|
: MultiCommand({
|
|
|
|
{"install", []() { return make_ref<CmdProfileInstall>(); }},
|
2019-10-22 11:06:32 +00:00
|
|
|
{"remove", []() { return make_ref<CmdProfileRemove>(); }},
|
2019-10-22 12:44:51 +00:00
|
|
|
{"upgrade", []() { return make_ref<CmdProfileUpgrade>(); }},
|
2021-01-12 18:57:05 +00:00
|
|
|
{"list", []() { return make_ref<CmdProfileList>(); }},
|
2020-07-16 15:00:42 +00:00
|
|
|
{"diff-closures", []() { return make_ref<CmdProfileDiffClosures>(); }},
|
2021-01-12 22:51:07 +00:00
|
|
|
{"history", []() { return make_ref<CmdProfileHistory>(); }},
|
2021-09-14 17:05:28 +00:00
|
|
|
{"rollback", []() { return make_ref<CmdProfileRollback>(); }},
|
2021-09-14 18:35:12 +00:00
|
|
|
{"wipe-history", []() { return make_ref<CmdProfileWipeHistory>(); }},
|
2019-10-21 22:21:58 +00:00
|
|
|
})
|
|
|
|
{ }
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "manage Nix profiles";
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:25:36 +00:00
|
|
|
std::string doc() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "profile.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-10-21 22:21:58 +00:00
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
if (!command)
|
|
|
|
throw UsageError("'nix profile' requires a sub-command.");
|
2020-05-05 16:59:33 +00:00
|
|
|
command->second->run();
|
2019-10-21 22:21:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static auto rCmdProfile = registerCommand<CmdProfile>("profile");
|