2023-10-25 07:43:36 +03:00
|
|
|
#include "users.hh"
|
2006-07-26 18:05:15 +03:00
|
|
|
#include "attr-path.hh"
|
2017-10-24 13:45:11 +03:00
|
|
|
#include "common-eval-args.hh"
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
#include "derivations.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "get-drvs.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "names.hh"
|
|
|
|
#include "profiles.hh"
|
2021-03-02 05:50:41 +02:00
|
|
|
#include "path-with-outputs.hh"
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
#include "shared.hh"
|
2006-11-30 19:43:04 +02:00
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2010-04-19 13:47:56 +03:00
|
|
|
#include "user-env.hh"
|
2013-11-19 01:41:45 +02:00
|
|
|
#include "value-to-json.hh"
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
#include "xml-writer.hh"
|
2021-01-26 13:22:24 +02:00
|
|
|
#include "legacy.hh"
|
2023-07-24 21:02:05 +03:00
|
|
|
#include "eval-settings.hh" // for defexpr
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2004-02-06 18:03:27 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <ctime>
|
2005-05-04 19:33:20 +03:00
|
|
|
#include <algorithm>
|
2006-03-01 19:44:28 +02:00
|
|
|
#include <iostream>
|
2006-08-03 18:52:09 +03:00
|
|
|
#include <sstream>
|
2004-02-06 18:03:27 +02:00
|
|
|
|
2007-09-17 19:08:24 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2005-10-06 18:01:46 +03:00
|
|
|
#include <unistd.h>
|
2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2005-10-06 18:01:46 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
using namespace nix;
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
|
2005-02-11 18:56:45 +02:00
|
|
|
typedef enum {
|
|
|
|
srcNixExprDrvs,
|
|
|
|
srcNixExprs,
|
|
|
|
srcStorePaths,
|
|
|
|
srcProfile,
|
2006-07-25 14:53:22 +03:00
|
|
|
srcAttrPath,
|
2005-02-11 18:56:45 +02:00
|
|
|
srcUnknown
|
|
|
|
} InstallSourceType;
|
|
|
|
|
|
|
|
|
|
|
|
struct InstallSourceInfo
|
|
|
|
{
|
|
|
|
InstallSourceType type;
|
2023-04-06 14:15:50 +03:00
|
|
|
std::shared_ptr<SourcePath> nixExprPath; /* for srcNixExprDrvs, srcNixExprs */
|
2005-02-11 18:56:45 +02:00
|
|
|
Path profile; /* for srcProfile */
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string systemFilter; /* for srcNixExprDrvs */
|
2014-09-19 17:49:41 +03:00
|
|
|
Bindings * autoArgs;
|
2005-02-11 18:56:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-12-22 00:34:41 +02:00
|
|
|
struct Globals
|
|
|
|
{
|
2005-02-11 18:56:45 +02:00
|
|
|
InstallSourceInfo instSource;
|
2004-02-06 12:30:20 +02:00
|
|
|
Path profile;
|
2014-08-13 04:50:44 +03:00
|
|
|
std::shared_ptr<EvalState> state;
|
2004-02-09 13:59:39 +02:00
|
|
|
bool dryRun;
|
2004-06-28 17:40:26 +03:00
|
|
|
bool preserveInstalled;
|
2013-09-03 22:15:47 +03:00
|
|
|
bool removeAll;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string forceName;
|
2011-04-11 19:27:05 +03:00
|
|
|
bool prebuiltOnly;
|
2003-12-22 00:34:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef void (* Operation) (Globals & globals,
|
2014-08-13 04:50:44 +03:00
|
|
|
Strings opFlags, Strings opArgs);
|
2003-12-01 17:55:05 +02:00
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
static std::string needArg(Strings::iterator & i,
|
|
|
|
Strings & args, const std::string & arg)
|
2007-09-17 22:24:07 +03:00
|
|
|
{
|
2020-05-14 00:56:39 +03:00
|
|
|
if (i == args.end()) throw UsageError("'%1%' requires an argument", arg);
|
2007-09-17 22:24:07 +03:00
|
|
|
return *i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool parseInstallSourceOptions(Globals & globals,
|
2022-02-25 17:00:00 +02:00
|
|
|
Strings::iterator & i, Strings & args, const std::string & arg)
|
2007-09-17 22:24:07 +03:00
|
|
|
{
|
|
|
|
if (arg == "--from-expression" || arg == "-E")
|
|
|
|
globals.instSource.type = srcNixExprs;
|
|
|
|
else if (arg == "--from-profile") {
|
|
|
|
globals.instSource.type = srcProfile;
|
|
|
|
globals.instSource.profile = needArg(i, args, arg);
|
|
|
|
}
|
|
|
|
else if (arg == "--attr" || arg == "-A")
|
|
|
|
globals.instSource.type = srcAttrPath;
|
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
static bool isNixExpr(const SourcePath & path, struct InputAccessor::Stat & st)
|
2007-09-17 19:08:24 +03:00
|
|
|
{
|
2023-04-06 14:15:50 +03:00
|
|
|
return
|
|
|
|
st.type == InputAccessor::tRegular
|
2023-12-06 00:02:59 +02:00
|
|
|
|| (st.type == InputAccessor::tDirectory && (path + "default.nix").resolveSymlinks().pathExists());
|
2007-09-17 19:08:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-04 18:39:16 +02:00
|
|
|
static constexpr size_t maxAttrs = 1024;
|
|
|
|
|
|
|
|
|
2007-09-17 19:08:24 +03:00
|
|
|
static void getAllExprs(EvalState & state,
|
2023-04-06 14:15:50 +03:00
|
|
|
const SourcePath & path, StringSet & seen, BindingsBuilder & attrs)
|
2007-09-17 19:08:24 +03:00
|
|
|
{
|
2014-08-01 17:37:47 +03:00
|
|
|
StringSet namesSorted;
|
2023-04-06 14:15:50 +03:00
|
|
|
for (auto & [name, _] : path.readDirectory()) namesSorted.insert(name);
|
2007-09-17 19:08:24 +03:00
|
|
|
|
2014-08-01 17:37:47 +03:00
|
|
|
for (auto & i : namesSorted) {
|
2012-08-01 23:43:36 +03:00
|
|
|
/* Ignore the manifest.nix used by profiles. This is
|
|
|
|
necessary to prevent it from showing up in channels (which
|
|
|
|
are implemented using profiles). */
|
2014-08-01 17:37:47 +03:00
|
|
|
if (i == "manifest.nix") continue;
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2023-12-06 00:02:59 +02:00
|
|
|
auto path2 = (path + i).resolveSymlinks();
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
InputAccessor::Stat st;
|
|
|
|
try {
|
2023-12-06 00:02:59 +02:00
|
|
|
st = path2.lstat();
|
2023-04-06 14:15:50 +03:00
|
|
|
} catch (Error &) {
|
2007-09-18 17:01:14 +03:00
|
|
|
continue; // ignore dangling symlinks in ~/.nix-defexpr
|
2023-04-06 14:15:50 +03:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
if (isNixExpr(path2, st) && (st.type != InputAccessor::tRegular || hasSuffix(path2.baseName(), ".nix"))) {
|
2008-08-25 16:31:57 +03:00
|
|
|
/* Strip off the `.nix' filename suffix (if applicable),
|
|
|
|
otherwise the attribute cannot be selected with the
|
|
|
|
`-A' option. Useful if you want to stick a Nix
|
|
|
|
expression directly in ~/.nix-defexpr. */
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string attrName = i;
|
2008-08-25 16:31:57 +03:00
|
|
|
if (hasSuffix(attrName, ".nix"))
|
2022-02-25 17:00:00 +02:00
|
|
|
attrName = std::string(attrName, 0, attrName.size() - 4);
|
2022-01-04 18:39:16 +02:00
|
|
|
if (!seen.insert(attrName).second) {
|
2022-03-12 23:58:29 +02:00
|
|
|
std::string suggestionMessage = "";
|
2023-04-06 14:15:50 +03:00
|
|
|
if (path2.path.abs().find("channels") != std::string::npos && path.path.abs().find("channels") != std::string::npos)
|
2022-03-12 23:58:29 +02:00
|
|
|
suggestionMessage = fmt("\nsuggestion: remove '%s' from either the root channels or the user channels", attrName);
|
|
|
|
printError("warning: name collision in input Nix expressions, skipping '%1%'"
|
|
|
|
"%2%", path2, suggestionMessage);
|
2013-09-03 16:25:51 +03:00
|
|
|
continue;
|
|
|
|
}
|
2013-09-03 16:45:32 +03:00
|
|
|
/* Load the expression on demand. */
|
2022-01-04 18:39:16 +02:00
|
|
|
auto vArg = state.allocValue();
|
2023-04-06 14:15:50 +03:00
|
|
|
vArg->mkString(path2.path.abs());
|
2022-01-04 18:39:16 +02:00
|
|
|
if (seen.size() == maxAttrs)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("too many Nix expressions in directory '%1%'", path);
|
2022-01-04 18:39:16 +02:00
|
|
|
attrs.alloc(attrName).mkApp(&state.getBuiltin("import"), vArg);
|
2008-08-25 16:31:57 +03:00
|
|
|
}
|
2023-04-06 14:15:50 +03:00
|
|
|
else if (st.type == InputAccessor::tDirectory)
|
2008-08-25 16:31:57 +03:00
|
|
|
/* `path2' is a directory (with no default.nix in it);
|
|
|
|
recurse into it. */
|
2022-01-04 18:39:16 +02:00
|
|
|
getAllExprs(state, path2, seen, attrs);
|
2007-09-17 19:08:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-22 02:07:07 +03:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
static void loadSourceExpr(EvalState & state, const SourcePath & path, Value & v)
|
2007-09-17 19:08:24 +03:00
|
|
|
{
|
2023-04-06 14:15:50 +03:00
|
|
|
auto st = path.resolveSymlinks().lstat();
|
2013-09-03 16:17:51 +03:00
|
|
|
|
2018-09-17 17:36:30 +03:00
|
|
|
if (isNixExpr(path, st))
|
2013-09-03 16:17:51 +03:00
|
|
|
state.evalFile(path, v);
|
2007-09-17 19:08:24 +03:00
|
|
|
|
|
|
|
/* The path is a directory. Put the Nix expressions in the
|
2013-10-24 17:41:04 +03:00
|
|
|
directory in a set, with the file name of each expression as
|
|
|
|
the attribute name. Recurse into subdirectories (but keep the
|
|
|
|
set flat, not nested, to make it easier for a user to have a
|
|
|
|
~/.nix-defexpr directory that includes some system-wide
|
|
|
|
directory). */
|
2023-04-06 14:15:50 +03:00
|
|
|
else if (st.type == InputAccessor::tDirectory) {
|
2022-01-04 18:39:16 +02:00
|
|
|
auto attrs = state.buildBindings(maxAttrs);
|
2023-11-12 18:51:09 +02:00
|
|
|
state.mkList(attrs.alloc("_combineChannels"), 0);
|
2022-01-04 18:39:16 +02:00
|
|
|
StringSet seen;
|
|
|
|
getAllExprs(state, path, seen, attrs);
|
|
|
|
v.mkAttrs(attrs);
|
2013-09-03 16:17:51 +03:00
|
|
|
}
|
2018-09-17 17:36:30 +03:00
|
|
|
|
|
|
|
else throw Error("path '%s' is not a directory or a Nix expression", path);
|
2007-09-17 19:08:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
static void loadDerivations(EvalState & state, const SourcePath & nixExprPath,
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string systemFilter, Bindings & autoArgs,
|
|
|
|
const std::string & pathPrefix, DrvInfos & elems)
|
2003-11-19 19:27:16 +02:00
|
|
|
{
|
2013-09-03 16:17:51 +03:00
|
|
|
Value vRoot;
|
|
|
|
loadSourceExpr(state, nixExprPath, vRoot);
|
|
|
|
|
2020-02-07 15:08:24 +02:00
|
|
|
Value & v(*findAlongAttrPath(state, pathPrefix, autoArgs, vRoot).first);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2012-10-04 22:22:25 +03:00
|
|
|
getDerivations(state, v, pathPrefix, autoArgs, elems, true);
|
2004-07-01 16:56:56 +03:00
|
|
|
|
|
|
|
/* Filter out all derivations not applicable to the current
|
|
|
|
system. */
|
2006-02-08 15:21:16 +02:00
|
|
|
for (DrvInfos::iterator i = elems.begin(), j; i != elems.end(); i = j) {
|
2004-07-01 16:56:56 +03:00
|
|
|
j = i; j++;
|
2017-07-17 20:02:56 +03:00
|
|
|
if (systemFilter != "*" && i->querySystem() != systemFilter)
|
2005-02-14 18:16:02 +02:00
|
|
|
elems.erase(i);
|
2005-02-11 18:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
static long getPriority(EvalState & state, DrvInfo & drv)
|
2009-06-30 18:53:39 +03:00
|
|
|
{
|
2013-11-19 15:09:03 +02:00
|
|
|
return drv.queryMetaInt("priority", 0);
|
2009-06-30 18:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 14:56:34 +03:00
|
|
|
static long comparePriorities(EvalState & state, DrvInfo & drv1, DrvInfo & drv2)
|
2007-05-01 23:33:18 +03:00
|
|
|
{
|
2009-06-30 18:53:39 +03:00
|
|
|
return getPriority(state, drv2) - getPriority(state, drv1);
|
2007-05-01 23:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 22:01:41 +02:00
|
|
|
// FIXME: this function is rather slow since it checks a single path
|
|
|
|
// at a time.
|
2013-11-19 15:09:03 +02:00
|
|
|
static bool isPrebuilt(EvalState & state, DrvInfo & elem)
|
2007-10-29 16:31:45 +02:00
|
|
|
{
|
2022-03-02 11:57:19 +02:00
|
|
|
auto path = elem.queryOutPath();
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
if (state.store->isValidPath(path)) return true;
|
2020-06-16 23:20:18 +03:00
|
|
|
return state.store->querySubstitutablePaths({path}).count(path);
|
2007-10-29 16:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-20 14:56:42 +02:00
|
|
|
static void checkSelectorUse(DrvNames & selectors)
|
|
|
|
{
|
|
|
|
/* Check that all selectors have been used. */
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : selectors)
|
|
|
|
if (i.hits == 0 && i.fullName != "*")
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("selector '%1%' matches no derivations", i.fullName);
|
2013-12-20 14:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 15:32:52 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::set<std::string> searchByPrefix(const DrvInfos & allElems, std::string_view prefix) {
|
|
|
|
constexpr std::size_t maxResults = 3;
|
|
|
|
std::set<std::string> result;
|
|
|
|
for (const auto & drvInfo : allElems) {
|
|
|
|
const auto drvName = DrvName { drvInfo.queryName() };
|
|
|
|
if (hasPrefix(drvName.name, prefix)) {
|
|
|
|
result.emplace(drvName.name);
|
|
|
|
|
|
|
|
if (result.size() >= maxResults) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Match
|
|
|
|
{
|
|
|
|
DrvInfo drvInfo;
|
|
|
|
std::size_t index;
|
|
|
|
|
|
|
|
Match(DrvInfo drvInfo_, std::size_t index_)
|
|
|
|
: drvInfo{std::move(drvInfo_)}
|
|
|
|
, index{index_}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* If a selector matches multiple derivations
|
|
|
|
with the same name, pick the one matching the current
|
|
|
|
system. If there are still multiple derivations, pick the
|
|
|
|
one with the highest priority. If there are still multiple
|
|
|
|
derivations, pick the one with the highest version.
|
|
|
|
Finally, if there are still multiple derivations,
|
|
|
|
arbitrarily pick the first one. */
|
|
|
|
std::vector<Match> pickNewestOnly(EvalState & state, std::vector<Match> matches) {
|
|
|
|
/* Map from package names to derivations. */
|
|
|
|
std::map<std::string, Match> newest;
|
|
|
|
StringSet multiple;
|
|
|
|
|
|
|
|
for (auto & match : matches) {
|
|
|
|
auto & oneDrv = match.drvInfo;
|
|
|
|
|
|
|
|
const auto drvName = DrvName { oneDrv.queryName() };
|
|
|
|
long comparison = 1;
|
|
|
|
|
|
|
|
const auto itOther = newest.find(drvName.name);
|
|
|
|
|
|
|
|
if (itOther != newest.end()) {
|
|
|
|
auto & newestDrv = itOther->second.drvInfo;
|
|
|
|
|
|
|
|
comparison =
|
|
|
|
oneDrv.querySystem() == newestDrv.querySystem() ? 0 :
|
|
|
|
oneDrv.querySystem() == settings.thisSystem ? 1 :
|
|
|
|
newestDrv.querySystem() == settings.thisSystem ? -1 : 0;
|
|
|
|
if (comparison == 0)
|
|
|
|
comparison = comparePriorities(state, oneDrv, newestDrv);
|
|
|
|
if (comparison == 0)
|
|
|
|
comparison = compareVersions(drvName.version, DrvName { newestDrv.queryName() }.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comparison > 0) {
|
|
|
|
newest.erase(drvName.name);
|
|
|
|
newest.emplace(drvName.name, match);
|
|
|
|
multiple.erase(drvName.fullName);
|
|
|
|
} else if (comparison == 0) {
|
|
|
|
multiple.insert(drvName.fullName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
for (auto & [name, match] : newest) {
|
|
|
|
if (multiple.find(name) != multiple.end())
|
2021-11-19 17:29:55 +02:00
|
|
|
warn(
|
|
|
|
"there are multiple derivations named '%1%'; using the first one",
|
2021-11-18 15:32:52 +02:00
|
|
|
name);
|
|
|
|
matches.push_back(match);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
2007-10-29 16:31:45 +02:00
|
|
|
static DrvInfos filterBySelector(EvalState & state, const DrvInfos & allElems,
|
2011-04-11 19:27:05 +03:00
|
|
|
const Strings & args, bool newestOnly)
|
2005-02-15 12:49:31 +02:00
|
|
|
{
|
|
|
|
DrvNames selectors = drvNamesFromArgs(args);
|
2013-09-03 16:56:33 +03:00
|
|
|
if (selectors.empty())
|
2020-09-21 19:22:45 +03:00
|
|
|
selectors.emplace_back("*");
|
2005-02-15 12:49:31 +02:00
|
|
|
|
2006-02-08 15:21:16 +02:00
|
|
|
DrvInfos elems;
|
2021-11-18 15:32:52 +02:00
|
|
|
std::set<std::size_t> done;
|
|
|
|
|
|
|
|
for (auto & selector : selectors) {
|
|
|
|
std::vector<Match> matches;
|
|
|
|
for (const auto & [index, drvInfo] : enumerate(allElems)) {
|
|
|
|
const auto drvName = DrvName { drvInfo.queryName() };
|
|
|
|
if (selector.matches(drvName)) {
|
|
|
|
++selector.hits;
|
|
|
|
matches.emplace_back(drvInfo, index);
|
2006-02-17 19:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newestOnly) {
|
2021-11-18 15:32:52 +02:00
|
|
|
matches = pickNewestOnly(state, std::move(matches));
|
2006-02-17 19:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert only those elements in the final list that we
|
|
|
|
haven't inserted before. */
|
2021-11-18 15:32:52 +02:00
|
|
|
for (auto & match : matches)
|
|
|
|
if (done.insert(match.index).second)
|
|
|
|
elems.push_back(match.drvInfo);
|
|
|
|
|
|
|
|
if (selector.hits == 0 && selector.fullName != "*") {
|
|
|
|
const auto prefixHits = searchByPrefix(allElems, selector.name);
|
2022-02-25 17:00:00 +02:00
|
|
|
|
2021-11-18 15:32:52 +02:00
|
|
|
if (prefixHits.empty()) {
|
2022-02-25 17:00:00 +02:00
|
|
|
throw Error("selector '%1%' matches no derivations", selector.fullName);
|
2021-11-18 15:32:52 +02:00
|
|
|
} else {
|
|
|
|
std::string suggestionMessage = ", maybe you meant:";
|
|
|
|
for (const auto & drvName : prefixHits) {
|
|
|
|
suggestionMessage += fmt("\n%s", drvName);
|
|
|
|
}
|
|
|
|
throw Error("selector '%1%' matches no derivations" + suggestionMessage, selector.fullName);
|
|
|
|
}
|
|
|
|
}
|
2006-02-17 19:47:54 +02:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-15 12:49:31 +02:00
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
static bool isPath(std::string_view s)
|
2007-11-29 18:18:24 +02:00
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
return s.find('/') != std::string_view::npos;
|
2007-11-29 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
static void queryInstSources(EvalState & state,
|
2010-10-22 17:47:42 +03:00
|
|
|
InstallSourceInfo & instSource, const Strings & args,
|
2006-02-17 19:47:54 +02:00
|
|
|
DrvInfos & elems, bool newestOnly)
|
2003-11-20 15:48:48 +02:00
|
|
|
{
|
2005-02-14 19:35:10 +02:00
|
|
|
InstallSourceType type = instSource.type;
|
2007-11-29 18:18:24 +02:00
|
|
|
if (type == srcUnknown && args.size() > 0 && isPath(args.front()))
|
2005-02-14 19:35:10 +02:00
|
|
|
type = srcStorePaths;
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-14 19:35:10 +02:00
|
|
|
switch (type) {
|
2003-11-20 15:48:48 +02:00
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
/* Get the available user environment elements from the
|
|
|
|
derivations specified in a Nix expression, including only
|
|
|
|
those with names matching any of the names in `args'. */
|
|
|
|
case srcUnknown:
|
|
|
|
case srcNixExprDrvs: {
|
|
|
|
|
|
|
|
/* Load the derivations from the (default or specified)
|
|
|
|
Nix expression. */
|
2006-02-08 15:21:16 +02:00
|
|
|
DrvInfos allElems;
|
2023-04-06 14:15:50 +03:00
|
|
|
loadDerivations(state, *instSource.nixExprPath,
|
2014-09-19 17:49:41 +03:00
|
|
|
instSource.systemFilter, *instSource.autoArgs, "", allElems);
|
2005-02-14 18:16:02 +02:00
|
|
|
|
2011-04-11 19:27:05 +03:00
|
|
|
elems = filterBySelector(state, allElems, args, newestOnly);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
break;
|
2003-11-24 13:01:19 +02:00
|
|
|
}
|
2005-02-14 18:16:02 +02:00
|
|
|
|
2005-02-14 19:07:43 +02:00
|
|
|
/* Get the available user environment elements from the Nix
|
|
|
|
expressions specified on the command line; these should be
|
|
|
|
functions that take the default Nix expression file as
|
|
|
|
argument, e.g., if the file is `./foo.nix', then the
|
|
|
|
argument `x: x.bar' is equivalent to `(x: x.bar)
|
|
|
|
(import ./foo.nix)' = `(import ./foo.nix).bar'. */
|
2005-02-15 14:05:47 +02:00
|
|
|
case srcNixExprs: {
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
Value vArg;
|
2023-04-06 14:15:50 +03:00
|
|
|
loadSourceExpr(state, *instSource.nixExprPath, vArg);
|
2005-02-14 19:07:43 +02:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : args) {
|
2023-04-06 14:15:50 +03:00
|
|
|
Expr * eFun = state.parseExprFromString(i, state.rootPath(CanonPath::fromCwd()));
|
2013-09-03 16:17:51 +03:00
|
|
|
Value vFun, vTmp;
|
|
|
|
state.eval(eFun, vFun);
|
2022-01-04 19:40:39 +02:00
|
|
|
vTmp.mkApp(&vFun, &vArg);
|
2014-09-19 17:49:41 +03:00
|
|
|
getDerivations(state, vTmp, "", *instSource.autoArgs, elems, true);
|
2005-02-14 19:07:43 +02:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
break;
|
2005-02-15 14:05:47 +02:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-15 14:05:47 +02:00
|
|
|
/* The available user environment elements are specified as a
|
|
|
|
list of store paths (which may or may not be
|
|
|
|
derivations). */
|
|
|
|
case srcStorePaths: {
|
2005-02-14 19:35:10 +02:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : args) {
|
2019-12-05 20:11:09 +02:00
|
|
|
auto path = state.store->followLinksToStorePath(i);
|
2005-02-14 19:35:10 +02:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
std::string name(path.name());
|
2005-02-14 19:35:10 +02:00
|
|
|
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvInfo elem(state, "", nullptr);
|
|
|
|
elem.setName(name);
|
2013-11-19 15:09:03 +02:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
if (path.isDerivation()) {
|
2022-03-02 11:57:19 +02:00
|
|
|
elem.setDrvPath(path);
|
2020-07-23 17:34:20 +03:00
|
|
|
auto outputs = state.store->queryDerivationOutputMap(path);
|
2022-03-02 11:57:19 +02:00
|
|
|
elem.setOutPath(outputs.at("out"));
|
2005-02-14 19:35:10 +02:00
|
|
|
if (name.size() >= drvExtension.size() &&
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string(name, name.size() - drvExtension.size()) == drvExtension)
|
|
|
|
name = name.substr(0, name.size() - drvExtension.size());
|
2005-02-14 19:35:10 +02:00
|
|
|
}
|
2022-03-02 11:57:19 +02:00
|
|
|
else
|
|
|
|
elem.setOutPath(path);
|
2005-02-14 19:35:10 +02:00
|
|
|
|
2006-02-08 16:32:33 +02:00
|
|
|
elems.push_back(elem);
|
2005-02-14 19:35:10 +02:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
break;
|
2005-02-15 14:05:47 +02:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2005-02-15 14:05:47 +02:00
|
|
|
/* Get the available user environment elements from another
|
|
|
|
user environment. These are then filtered as in the
|
|
|
|
`srcNixExprDrvs' case. */
|
|
|
|
case srcProfile: {
|
2006-02-17 19:47:54 +02:00
|
|
|
elems = filterBySelector(state,
|
|
|
|
queryInstalled(state, instSource.profile),
|
2011-04-11 19:27:05 +03:00
|
|
|
args, newestOnly);
|
2005-02-14 18:16:02 +02:00
|
|
|
break;
|
2005-02-15 14:05:47 +02:00
|
|
|
}
|
2006-07-25 14:53:22 +03:00
|
|
|
|
|
|
|
case srcAttrPath: {
|
2013-09-03 16:17:51 +03:00
|
|
|
Value vRoot;
|
2023-04-06 14:15:50 +03:00
|
|
|
loadSourceExpr(state, *instSource.nixExprPath, vRoot);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : args) {
|
2020-02-07 15:08:24 +02:00
|
|
|
Value & v(*findAlongAttrPath(state, i, *instSource.autoArgs, vRoot).first);
|
2014-09-19 17:49:41 +03:00
|
|
|
getDerivations(state, v, "", *instSource.autoArgs, elems, true);
|
2010-04-07 18:47:06 +03:00
|
|
|
}
|
2006-07-25 14:53:22 +03:00
|
|
|
break;
|
|
|
|
}
|
2003-11-20 15:48:48 +02:00
|
|
|
}
|
2005-02-14 18:16:02 +02:00
|
|
|
}
|
2003-11-20 15:48:48 +02:00
|
|
|
|
2004-06-28 17:40:26 +03:00
|
|
|
|
2013-11-19 15:09:03 +02:00
|
|
|
static void printMissing(EvalState & state, DrvInfos & elems)
|
2006-03-06 13:21:15 +02:00
|
|
|
{
|
2021-04-05 16:48:18 +03:00
|
|
|
std::vector<DerivedPath> targets;
|
2022-03-02 11:57:19 +02:00
|
|
|
for (auto & i : elems)
|
|
|
|
if (auto drvPath = i.queryDrvPath())
|
2023-11-03 12:58:47 +02:00
|
|
|
targets.emplace_back(DerivedPath::Built{
|
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-16 00:39:04 +02:00
|
|
|
.drvPath = makeConstantStorePathRef(*drvPath),
|
2023-01-11 23:32:30 +02:00
|
|
|
.outputs = OutputsSpec::All { },
|
|
|
|
});
|
2006-03-06 13:21:15 +02:00
|
|
|
else
|
2023-11-03 12:58:47 +02:00
|
|
|
targets.emplace_back(DerivedPath::Opaque{
|
2023-01-11 23:32:30 +02:00
|
|
|
.path = i.queryOutPath(),
|
|
|
|
});
|
2006-03-06 13:21:15 +02:00
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
printMissing(state.store, targets);
|
2006-03-06 13:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 15:09:03 +02:00
|
|
|
static bool keep(DrvInfo & drv)
|
2009-06-30 18:53:39 +03:00
|
|
|
{
|
2013-11-19 15:09:03 +02:00
|
|
|
return drv.queryMetaBool("keep", false);
|
2009-06-30 18:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
static void installDerivations(Globals & globals,
|
|
|
|
const Strings & args, const Path & profile)
|
|
|
|
{
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("installing derivations");
|
2005-02-14 18:16:02 +02:00
|
|
|
|
|
|
|
/* Get the set of user environment elements to be installed. */
|
2011-04-11 19:27:05 +03:00
|
|
|
DrvInfos newElems, newElemsTmp;
|
2014-08-13 04:50:44 +03:00
|
|
|
queryInstSources(*globals.state, globals.instSource, args, newElemsTmp, true);
|
2011-04-11 19:27:05 +03:00
|
|
|
|
|
|
|
/* If --prebuilt-only is given, filter out source-only packages. */
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : newElemsTmp)
|
|
|
|
if (!globals.prebuiltOnly || isPrebuilt(*globals.state, i))
|
|
|
|
newElems.push_back(i);
|
2005-02-14 18:16:02 +02:00
|
|
|
|
|
|
|
StringSet newNames;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : newElems) {
|
2006-09-25 17:00:59 +03:00
|
|
|
/* `forceName' is a hack to get package names right in some
|
|
|
|
one-click installs, namely those where the name used in the
|
|
|
|
path is not the one we want (e.g., `java-front' versus
|
|
|
|
`java-front-0.9pre15899'). */
|
|
|
|
if (globals.forceName != "")
|
2017-07-17 20:02:56 +03:00
|
|
|
i.setName(globals.forceName);
|
|
|
|
newNames.insert(DrvName(i.queryName()).name);
|
2006-09-25 17:00:59 +03:00
|
|
|
}
|
2005-02-14 18:16:02 +02:00
|
|
|
|
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
while (true) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto lockToken = optimisticLockProfile(profile);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
DrvInfos allElems(newElems);
|
2003-11-20 15:48:48 +02:00
|
|
|
|
2013-09-03 22:15:47 +03:00
|
|
|
/* Add in the already installed derivations, unless they have
|
|
|
|
the same name as a to-be-installed element. */
|
|
|
|
if (!globals.removeAll) {
|
2014-08-13 04:50:44 +03:00
|
|
|
DrvInfos installedElems = queryInstalled(*globals.state, profile);
|
2013-09-03 22:15:47 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : installedElems) {
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName drvName(i.queryName());
|
2013-09-03 22:15:47 +03:00
|
|
|
if (!globals.preserveInstalled &&
|
|
|
|
newNames.find(drvName.name) != newNames.end() &&
|
2015-07-17 20:24:28 +03:00
|
|
|
!keep(i))
|
2017-07-30 14:27:57 +03:00
|
|
|
printInfo("replacing old '%s'", i.queryName());
|
2013-09-03 22:15:47 +03:00
|
|
|
else
|
2015-07-17 20:24:28 +03:00
|
|
|
allElems.push_back(i);
|
2013-09-03 22:15:47 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : newElems)
|
2017-07-30 14:27:57 +03:00
|
|
|
printInfo("installing '%s'", i.queryName());
|
2013-09-03 22:15:47 +03:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
printMissing(*globals.state, newElems);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 13:59:39 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
if (createUserEnv(*globals.state, allElems,
|
2012-07-31 02:55:41 +03:00
|
|
|
profile, settings.envKeepDerivations, lockToken)) break;
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2003-11-20 15:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opInstall(Globals & globals, Strings opFlags, Strings opArgs)
|
2003-11-19 19:27:16 +02:00
|
|
|
{
|
2007-09-17 22:24:07 +03:00
|
|
|
for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto arg = *i++;
|
2007-09-17 22:24:07 +03:00
|
|
|
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
|
|
|
|
else if (arg == "--preserve-installed" || arg == "-P")
|
|
|
|
globals.preserveInstalled = true;
|
2013-09-03 22:15:47 +03:00
|
|
|
else if (arg == "--remove-all" || arg == "-r")
|
|
|
|
globals.removeAll = true;
|
2020-04-22 02:07:07 +03:00
|
|
|
else throw UsageError("unknown flag '%1%'", arg);
|
2007-09-17 22:24:07 +03:00
|
|
|
}
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2005-02-14 18:16:02 +02:00
|
|
|
installDerivations(globals, opArgs, globals.profile);
|
2003-12-22 01:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-27 15:17:25 +03:00
|
|
|
typedef enum { utLt, utLeq, utEq, utAlways } UpgradeType;
|
2004-02-09 13:59:39 +02:00
|
|
|
|
|
|
|
|
2005-02-14 15:07:09 +02:00
|
|
|
static void upgradeDerivations(Globals & globals,
|
2007-02-02 03:52:42 +02:00
|
|
|
const Strings & args, UpgradeType upgradeType)
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
{
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("upgrading derivations");
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
|
|
|
|
/* Upgrade works as follows: we take all currently installed
|
|
|
|
derivations, and for any derivation matching any selector, look
|
|
|
|
for a derivation in the input Nix expression that has the same
|
|
|
|
name and a higher version number. */
|
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
while (true) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto lockToken = optimisticLockProfile(globals.profile);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
DrvInfos installedElems = queryInstalled(*globals.state, globals.profile);
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
/* Fetch all derivations from the input file. */
|
|
|
|
DrvInfos availElems;
|
2014-08-13 04:50:44 +03:00
|
|
|
queryInstSources(*globals.state, globals.instSource, args, availElems, false);
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
/* Go through all installed derivations. */
|
|
|
|
DrvInfos newElems;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : installedElems) {
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName drvName(i.queryName());
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
|
2009-10-13 12:30:17 +03:00
|
|
|
try {
|
2007-04-28 02:48:14 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
if (keep(i)) {
|
|
|
|
newElems.push_back(i);
|
2009-10-13 12:30:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the derivation in the input Nix expression
|
|
|
|
with the same name that satisfies the version
|
|
|
|
constraints specified by upgradeType. If there are
|
|
|
|
multiple matches, take the one with the highest
|
|
|
|
priority. If there are still multiple matches,
|
2015-09-17 11:34:54 +03:00
|
|
|
take the one with the highest version.
|
|
|
|
Do not upgrade if it would decrease the priority. */
|
2009-10-13 12:30:17 +03:00
|
|
|
DrvInfos::iterator bestElem = availElems.end();
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string bestVersion;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto j = availElems.begin(); j != availElems.end(); ++j) {
|
2015-09-17 11:34:54 +03:00
|
|
|
if (comparePriorities(*globals.state, i, *j) > 0)
|
|
|
|
continue;
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName newName(j->queryName());
|
2009-10-13 12:30:17 +03:00
|
|
|
if (newName.name == drvName.name) {
|
2015-09-17 11:34:54 +03:00
|
|
|
int d = compareVersions(drvName.version, newName.version);
|
2009-10-13 12:30:17 +03:00
|
|
|
if ((upgradeType == utLt && d < 0) ||
|
|
|
|
(upgradeType == utLeq && d <= 0) ||
|
|
|
|
(upgradeType == utEq && d == 0) ||
|
|
|
|
upgradeType == utAlways)
|
|
|
|
{
|
2018-05-02 14:56:34 +03:00
|
|
|
long d2 = -1;
|
2009-10-13 12:30:17 +03:00
|
|
|
if (bestElem != availElems.end()) {
|
2014-08-13 04:50:44 +03:00
|
|
|
d2 = comparePriorities(*globals.state, *bestElem, *j);
|
2015-09-17 11:34:54 +03:00
|
|
|
if (d2 == 0) d2 = compareVersions(bestVersion, newName.version);
|
2009-10-13 12:30:17 +03:00
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
if (d2 < 0 && (!globals.prebuiltOnly || isPrebuilt(*globals.state, *j))) {
|
2009-10-13 12:30:17 +03:00
|
|
|
bestElem = j;
|
2015-09-17 11:34:54 +03:00
|
|
|
bestVersion = newName.version;
|
2009-10-13 12:30:17 +03:00
|
|
|
}
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2004-02-09 13:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2009-10-13 12:30:17 +03:00
|
|
|
if (bestElem != availElems.end() &&
|
2015-07-17 20:24:28 +03:00
|
|
|
i.queryOutPath() !=
|
2013-11-19 15:09:03 +02:00
|
|
|
bestElem->queryOutPath())
|
2009-10-13 12:30:17 +03:00
|
|
|
{
|
2015-09-17 13:08:49 +03:00
|
|
|
const char * action = compareVersions(drvName.version, bestVersion) <= 0
|
|
|
|
? "upgrading" : "downgrading";
|
2017-07-30 14:27:57 +03:00
|
|
|
printInfo("%1% '%2%' to '%3%'",
|
2017-07-17 20:02:56 +03:00
|
|
|
action, i.queryName(), bestElem->queryName());
|
2009-10-13 12:30:17 +03:00
|
|
|
newElems.push_back(*bestElem);
|
2015-07-17 20:24:28 +03:00
|
|
|
} else newElems.push_back(i);
|
2009-10-13 12:30:17 +03:00
|
|
|
|
|
|
|
} catch (Error & e) {
|
2022-12-13 01:48:04 +02:00
|
|
|
e.addTrace(nullptr, "while trying to find an upgrade for '%s'", i.queryName());
|
2009-10-13 12:30:17 +03:00
|
|
|
throw;
|
2003-12-24 00:13:36 +02:00
|
|
|
}
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
printMissing(*globals.state, newElems);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 13:59:39 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
if (createUserEnv(*globals.state, newElems,
|
2012-07-31 02:55:41 +03:00
|
|
|
globals.profile, settings.envKeepDerivations, lockToken)) break;
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
* Upgrade operation in `nix-env'. For instance, you can say
nix-env -u foo.nix strategoxt
to replace the installed `strategoxt' derivation with the one from `foo.nix', if
the latter has a higher version number. This is a no-op if `strategoxt' is not
installed. Wildcards are also accepted, so
nix-env -u foo.nix '*'
will replace any installed derivation with newer versions from `foo.nix', if
available.
The notion of "version number" is somewhat ad hoc, but should be useful in most
cases, as evidenced by the following unit tests for the version comparator:
TEST("1.0", "2.3", -1);
TEST("2.1", "2.3", -1);
TEST("2.3", "2.3", 0);
TEST("2.5", "2.3", 1);
TEST("3.1", "2.3", 1);
TEST("2.3.1", "2.3", 1);
TEST("2.3.1", "2.3a", 1);
TEST("2.3pre1", "2.3", -1);
TEST("2.3pre3", "2.3pre12", -1);
TEST("2.3a", "2.3c", -1);
TEST("2.3pre1", "2.3c", -1);
TEST("2.3pre1", "2.3q", -1);
(-1 = less, 0 = equal, 1 = greater)
* A new verbosity level `lvlInfo', between `lvlError' and `lvlTalkative'. This is
the default for `nix-env', so without any `-v' flags users should get useful
output, e.g.,
$ nix-env -u foo.nix strategoxt
upgrading `strategoxt-0.9.2' to `strategoxt-0.9.3'
2003-12-22 18:04:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opUpgrade(Globals & globals, Strings opFlags, Strings opArgs)
|
2003-12-22 01:58:56 +02:00
|
|
|
{
|
2004-02-09 13:59:39 +02:00
|
|
|
UpgradeType upgradeType = utLt;
|
2007-09-17 22:24:07 +03:00
|
|
|
for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string arg = *i++;
|
2007-09-17 22:24:07 +03:00
|
|
|
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
|
|
|
|
else if (arg == "--lt") upgradeType = utLt;
|
|
|
|
else if (arg == "--leq") upgradeType = utLeq;
|
|
|
|
else if (arg == "--eq") upgradeType = utEq;
|
|
|
|
else if (arg == "--always") upgradeType = utAlways;
|
2020-04-22 02:07:07 +03:00
|
|
|
else throw UsageError("unknown flag '%1%'", arg);
|
2007-09-17 22:24:07 +03:00
|
|
|
}
|
2003-12-22 01:58:56 +02:00
|
|
|
|
2007-02-02 03:52:42 +02:00
|
|
|
upgradeDerivations(globals, opArgs, upgradeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setMetaFlag(EvalState & state, DrvInfo & drv,
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string & name, const std::string & value)
|
2007-02-02 03:52:42 +02:00
|
|
|
{
|
2022-01-04 19:24:42 +02:00
|
|
|
auto v = state.allocValue();
|
|
|
|
v->mkString(value);
|
2013-11-19 15:09:03 +02:00
|
|
|
drv.setMeta(name, v);
|
2007-02-02 03:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opSetFlag(Globals & globals, Strings opFlags, Strings opArgs)
|
2007-02-02 03:52:42 +02:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2007-02-02 03:52:42 +02:00
|
|
|
if (opArgs.size() < 2)
|
2017-07-30 14:27:57 +03:00
|
|
|
throw UsageError("not enough arguments to '--set-flag'");
|
2007-02-02 03:52:42 +02:00
|
|
|
|
|
|
|
Strings::iterator arg = opArgs.begin();
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string flagName = *arg++;
|
|
|
|
std::string flagValue = *arg++;
|
2007-02-02 03:52:42 +02:00
|
|
|
DrvNames selectors = drvNamesFromArgs(Strings(arg, opArgs.end()));
|
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
while (true) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string lockToken = optimisticLockProfile(globals.profile);
|
2007-02-02 03:52:42 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
DrvInfos installedElems = queryInstalled(*globals.state, globals.profile);
|
2008-08-04 19:21:45 +03:00
|
|
|
|
|
|
|
/* Update all matching derivations. */
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : installedElems) {
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName drvName(i.queryName());
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : selectors)
|
|
|
|
if (j.matches(drvName)) {
|
2017-07-30 14:27:57 +03:00
|
|
|
printInfo("setting flag on '%1%'", i.queryName());
|
2015-07-17 20:24:28 +03:00
|
|
|
j.hits++;
|
|
|
|
setMetaFlag(*globals.state, i, flagName, flagValue);
|
2008-08-04 19:21:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 03:52:42 +02:00
|
|
|
|
2013-12-20 14:56:42 +02:00
|
|
|
checkSelectorUse(selectors);
|
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
/* Write the new user environment. */
|
2014-08-13 04:50:44 +03:00
|
|
|
if (createUserEnv(*globals.state, installedElems,
|
2012-07-31 02:55:41 +03:00
|
|
|
globals.profile, settings.envKeepDerivations, lockToken)) break;
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2003-11-19 19:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opSet(Globals & globals, Strings opFlags, Strings opArgs)
|
2006-12-12 21:06:02 +02:00
|
|
|
{
|
2016-06-02 14:33:49 +03:00
|
|
|
auto store2 = globals.state->store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
if (!store2) throw Error("--set is not supported for this Nix store");
|
|
|
|
|
2007-09-17 22:24:07 +03:00
|
|
|
for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string arg = *i++;
|
2007-09-17 22:24:07 +03:00
|
|
|
if (parseInstallSourceOptions(globals, i, opFlags, arg)) ;
|
2020-04-22 02:07:07 +03:00
|
|
|
else throw UsageError("unknown flag '%1%'", arg);
|
2007-09-17 22:24:07 +03:00
|
|
|
}
|
2006-12-12 21:06:02 +02:00
|
|
|
|
|
|
|
DrvInfos elems;
|
2014-08-13 04:50:44 +03:00
|
|
|
queryInstSources(*globals.state, globals.instSource, opArgs, elems, true);
|
2006-12-12 21:06:02 +02:00
|
|
|
|
|
|
|
if (elems.size() != 1)
|
|
|
|
throw Error("--set requires exactly one derivation");
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2006-12-12 21:06:02 +02:00
|
|
|
DrvInfo & drv(elems.front());
|
|
|
|
|
2014-09-16 20:34:59 +03:00
|
|
|
if (globals.forceName != "")
|
2017-07-17 20:02:56 +03:00
|
|
|
drv.setName(globals.forceName);
|
2014-09-16 20:34:59 +03:00
|
|
|
|
2022-03-02 11:57:19 +02:00
|
|
|
auto drvPath = drv.queryDrvPath();
|
2021-04-05 16:48:18 +03:00
|
|
|
std::vector<DerivedPath> paths {
|
2022-03-02 11:57:19 +02:00
|
|
|
drvPath
|
2023-01-11 23:32:30 +02:00
|
|
|
? (DerivedPath) (DerivedPath::Built {
|
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-16 00:39:04 +02:00
|
|
|
.drvPath = makeConstantStorePathRef(*drvPath),
|
2023-01-11 23:32:30 +02:00
|
|
|
.outputs = OutputsSpec::All { },
|
|
|
|
})
|
|
|
|
: (DerivedPath) (DerivedPath::Opaque {
|
|
|
|
.path = drv.queryOutPath(),
|
|
|
|
}),
|
2021-03-02 05:50:41 +02:00
|
|
|
};
|
|
|
|
printMissing(globals.state->store, paths);
|
|
|
|
if (globals.dryRun) return;
|
|
|
|
globals.state->store->buildPaths(paths, globals.state->repair ? bmRepair : bmNormal);
|
2006-12-12 21:06:02 +02:00
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("switching to new user environment");
|
2020-09-03 12:06:56 +03:00
|
|
|
Path generation = createGeneration(
|
2023-06-19 07:04:59 +03:00
|
|
|
*store2,
|
2022-03-02 11:57:19 +02:00
|
|
|
globals.profile,
|
|
|
|
drv.queryOutPath());
|
2006-12-12 21:06:02 +02:00
|
|
|
switchLink(globals.profile, generation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-29 18:18:24 +02:00
|
|
|
static void uninstallDerivations(Globals & globals, Strings & selectors,
|
2005-02-14 15:07:09 +02:00
|
|
|
Path & profile)
|
2003-11-20 15:48:48 +02:00
|
|
|
{
|
2008-08-04 19:21:45 +03:00
|
|
|
while (true) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto lockToken = optimisticLockProfile(profile);
|
2008-08-04 19:21:45 +03:00
|
|
|
|
2020-04-25 14:18:39 +03:00
|
|
|
DrvInfos workingElems = queryInstalled(*globals.state, profile);
|
|
|
|
|
|
|
|
for (auto & selector : selectors) {
|
|
|
|
DrvInfos::iterator split = workingElems.begin();
|
|
|
|
if (isPath(selector)) {
|
|
|
|
StorePath selectorStorePath = globals.state->store->followLinksToStorePath(selector);
|
|
|
|
split = std::partition(
|
|
|
|
workingElems.begin(), workingElems.end(),
|
|
|
|
[&selectorStorePath, globals](auto &elem) {
|
2022-03-02 11:57:19 +02:00
|
|
|
return selectorStorePath != elem.queryOutPath();
|
2020-04-25 14:18:39 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
DrvName selectorName(selector);
|
|
|
|
split = std::partition(
|
|
|
|
workingElems.begin(), workingElems.end(),
|
|
|
|
[&selectorName](auto &elem){
|
|
|
|
DrvName elemName(elem.queryName());
|
|
|
|
return !selectorName.matches(elemName);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (split == workingElems.end())
|
2020-04-28 18:56:01 +03:00
|
|
|
warn("selector '%s' matched no installed derivations", selector);
|
2020-04-25 14:18:39 +03:00
|
|
|
for (auto removedElem = split; removedElem != workingElems.end(); removedElem++) {
|
|
|
|
printInfo("uninstalling '%s'", removedElem->queryName());
|
|
|
|
}
|
|
|
|
workingElems.erase(split, workingElems.end());
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2003-11-20 15:48:48 +02:00
|
|
|
|
2008-08-04 19:21:45 +03:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 13:59:39 +02:00
|
|
|
|
2020-04-25 14:18:39 +03:00
|
|
|
if (createUserEnv(*globals.state, workingElems,
|
2012-07-31 02:55:41 +03:00
|
|
|
profile, settings.envKeepDerivations, lockToken)) break;
|
2008-08-04 19:21:45 +03:00
|
|
|
}
|
2003-11-20 15:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opUninstall(Globals & globals, Strings opFlags, Strings opArgs)
|
2003-11-20 15:48:48 +02:00
|
|
|
{
|
2003-12-22 00:34:41 +02:00
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2007-11-29 18:18:24 +02:00
|
|
|
uninstallDerivations(globals, opArgs, globals.profile);
|
2003-11-20 15:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-14 18:09:55 +03:00
|
|
|
static bool cmpChars(char a, char b)
|
|
|
|
{
|
|
|
|
return toupper(a) < toupper(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-08 15:21:16 +02:00
|
|
|
static bool cmpElemByName(const DrvInfo & a, const DrvInfo & b)
|
2004-02-02 12:51:54 +02:00
|
|
|
{
|
2017-07-17 20:02:56 +03:00
|
|
|
auto a_name = a.queryName();
|
|
|
|
auto b_name = b.queryName();
|
2004-10-14 18:09:55 +03:00
|
|
|
return lexicographical_compare(
|
2017-07-17 20:02:56 +03:00
|
|
|
a_name.begin(), a_name.end(),
|
|
|
|
b_name.begin(), b_name.end(), cmpChars);
|
2004-02-02 12:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 14:39:38 +02:00
|
|
|
typedef std::list<Strings> Table;
|
2004-07-01 16:35:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
void printTable(Table & table)
|
|
|
|
{
|
2018-05-02 14:56:34 +03:00
|
|
|
auto nrColumns = table.size() > 0 ? table.front().size() : 0;
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2022-02-21 17:32:34 +02:00
|
|
|
std::vector<size_t> widths;
|
2004-07-01 16:35:10 +03:00
|
|
|
widths.resize(nrColumns);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : table) {
|
|
|
|
assert(i.size() == nrColumns);
|
2004-10-29 14:22:49 +03:00
|
|
|
Strings::iterator j;
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t column;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (j = i.begin(), column = 0; j != i.end(); ++j, ++column)
|
2004-07-01 16:35:10 +03:00
|
|
|
if (j->size() > widths[column]) widths[column] = j->size();
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : table) {
|
2004-10-29 14:22:49 +03:00
|
|
|
Strings::iterator j;
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t column;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (j = i.begin(), column = 0; j != i.end(); ++j, ++column) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s = *j;
|
2009-07-02 11:52:12 +03:00
|
|
|
replace(s.begin(), s.end(), '\n', ' ');
|
|
|
|
cout << s;
|
2004-07-01 16:35:10 +03:00
|
|
|
if (column < nrColumns - 1)
|
2022-02-25 17:00:00 +02:00
|
|
|
cout << std::string(widths[column] - s.size() + 2, ' ');
|
2004-07-01 16:35:10 +03:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
cout << std::endl;
|
2004-07-01 16:35:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-11 00:36:16 +03:00
|
|
|
/* This function compares the version of an element against the
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
versions in the given set of elements. `cvLess' means that only
|
|
|
|
lower versions are in the set, `cvEqual' means that at most an
|
|
|
|
equal version is in the set, and `cvGreater' means that there is at
|
|
|
|
least one element with a higher version in the set. `cvUnavail'
|
|
|
|
means that there are no elements with the same name in the set. */
|
|
|
|
|
|
|
|
typedef enum { cvLess, cvEqual, cvGreater, cvUnavail } VersionDiff;
|
|
|
|
|
|
|
|
static VersionDiff compareVersionAgainstSet(
|
2022-02-25 17:00:00 +02:00
|
|
|
const DrvInfo & elem, const DrvInfos & elems, std::string & version)
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
{
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName name(elem.queryName());
|
2012-12-03 19:19:49 +02:00
|
|
|
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
VersionDiff diff = cvUnavail;
|
|
|
|
version = "?";
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems) {
|
2017-07-17 20:02:56 +03:00
|
|
|
DrvName name2(i.queryName());
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
if (name.name == name2.name) {
|
|
|
|
int d = compareVersions(name.version, name2.version);
|
|
|
|
if (d < 0) {
|
|
|
|
diff = cvGreater;
|
|
|
|
version = name2.version;
|
|
|
|
}
|
|
|
|
else if (diff != cvGreater && d == 0) {
|
|
|
|
diff = cvEqual;
|
|
|
|
version = name2.version;
|
|
|
|
}
|
|
|
|
else if (diff != cvGreater && diff != cvEqual && d > 0) {
|
|
|
|
diff = cvLess;
|
|
|
|
if (version == "" || compareVersions(version, name2.version) < 0)
|
|
|
|
version = name2.version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
`nix-env --query`: fix `--json` ignoring `--drv-path`
```json
{
"AMB-plugins": {
"drvPath": "/nix/store/l99cb7h2hy8dg005arsjbd9kx0w05d3h-AMB-plugins-0.8.1.drv",
"name": "AMB-plugins-0.8.1",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "AMB-plugins",
"system": "x86_64-linux",
"version": "0.8.1"
},
"ArchiSteamFarm": {
"drvPath": "/nix/store/nhplgyjj34fz6hjmnyih25gxscfh8s7b-ArchiSteamFarm-5.4.12.5.drv",
"name": "ArchiSteamFarm-5.4.12.5",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "ArchiSteamFarm",
"system": "x86_64-linux",
"version": "5.4.12.5"
},
...
```
2023-10-31 01:36:13 +02:00
|
|
|
static void queryJSON(Globals & globals, std::vector<DrvInfo> & elems, bool printOutPath, bool printDrvPath, bool printMeta)
|
2013-11-19 01:41:45 +02:00
|
|
|
{
|
2022-11-16 17:49:49 +02:00
|
|
|
using nlohmann::json;
|
|
|
|
json topObj = json::object();
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems) {
|
2022-01-16 05:36:07 +02:00
|
|
|
try {
|
|
|
|
if (i.hasFailed()) continue;
|
|
|
|
|
|
|
|
|
|
|
|
auto drvName = DrvName(i.queryName());
|
2022-11-16 17:49:49 +02:00
|
|
|
json &pkgObj = topObj[i.attrPath];
|
|
|
|
pkgObj = {
|
|
|
|
{"name", drvName.fullName},
|
|
|
|
{"pname", drvName.name},
|
|
|
|
{"version", drvName.version},
|
|
|
|
{"system", i.querySystem()},
|
|
|
|
{"outputName", i.queryOutputName()},
|
|
|
|
};
|
2022-01-16 05:36:07 +02:00
|
|
|
|
2022-03-12 14:47:01 +02:00
|
|
|
{
|
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs(printOutPath);
|
2022-11-16 17:49:49 +02:00
|
|
|
json &outputObj = pkgObj["outputs"];
|
|
|
|
outputObj = json::object();
|
2022-03-12 14:47:01 +02:00
|
|
|
for (auto & j : outputs) {
|
|
|
|
if (j.second)
|
2022-11-16 17:49:49 +02:00
|
|
|
outputObj[j.first] = globals.state->store->printStorePath(*j.second);
|
2022-03-12 14:47:01 +02:00
|
|
|
else
|
2022-11-16 17:49:49 +02:00
|
|
|
outputObj[j.first] = nullptr;
|
2022-03-12 14:47:01 +02:00
|
|
|
}
|
2022-01-06 19:13:18 +02:00
|
|
|
}
|
|
|
|
|
`nix-env --query`: fix `--json` ignoring `--drv-path`
```json
{
"AMB-plugins": {
"drvPath": "/nix/store/l99cb7h2hy8dg005arsjbd9kx0w05d3h-AMB-plugins-0.8.1.drv",
"name": "AMB-plugins-0.8.1",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "AMB-plugins",
"system": "x86_64-linux",
"version": "0.8.1"
},
"ArchiSteamFarm": {
"drvPath": "/nix/store/nhplgyjj34fz6hjmnyih25gxscfh8s7b-ArchiSteamFarm-5.4.12.5.drv",
"name": "ArchiSteamFarm-5.4.12.5",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "ArchiSteamFarm",
"system": "x86_64-linux",
"version": "5.4.12.5"
},
...
```
2023-10-31 01:36:13 +02:00
|
|
|
if (printDrvPath) {
|
|
|
|
auto drvPath = i.queryDrvPath();
|
|
|
|
if (drvPath) pkgObj["drvPath"] = globals.state->store->printStorePath(*drvPath);
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:36:07 +02:00
|
|
|
if (printMeta) {
|
2022-11-16 17:49:49 +02:00
|
|
|
json &metaObj = pkgObj["meta"];
|
|
|
|
metaObj = json::object();
|
2022-01-16 05:36:07 +02:00
|
|
|
StringSet metaNames = i.queryMetaNames();
|
|
|
|
for (auto & j : metaNames) {
|
|
|
|
Value * v = i.queryMeta(j);
|
|
|
|
if (!v) {
|
|
|
|
printError("derivation '%s' has invalid meta attribute '%s'", i.queryName(), j);
|
2022-11-16 17:49:49 +02:00
|
|
|
metaObj[j] = nullptr;
|
2022-01-16 05:36:07 +02:00
|
|
|
} else {
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
NixStringContext context;
|
2022-11-16 17:49:49 +02:00
|
|
|
metaObj[j] = printValueAsJSON(*globals.state, true, *v, noPos, context);
|
2022-01-16 05:36:07 +02:00
|
|
|
}
|
2022-01-07 00:40:02 +02:00
|
|
|
}
|
2013-11-19 15:29:39 +02:00
|
|
|
}
|
2022-01-16 05:36:07 +02:00
|
|
|
} catch (AssertionError & e) {
|
|
|
|
printMsg(lvlTalkative, "skipping derivation named '%1%' which gives an assertion failure", i.queryName());
|
|
|
|
} catch (Error & e) {
|
2022-12-13 01:48:04 +02:00
|
|
|
e.addTrace(nullptr, "while querying the derivation named '%1%'", i.queryName());
|
2022-01-25 05:12:13 +02:00
|
|
|
throw;
|
2013-11-19 01:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-16 17:49:49 +02:00
|
|
|
std::cout << topObj.dump(2);
|
2013-11-19 01:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
|
2003-11-19 19:27:16 +02:00
|
|
|
{
|
2022-03-03 11:02:11 +02:00
|
|
|
auto & store { *globals.state->store };
|
2022-03-02 11:57:19 +02:00
|
|
|
|
2007-09-17 22:24:07 +03:00
|
|
|
Strings remaining;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string attrPath;
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2004-07-01 16:13:37 +03:00
|
|
|
bool printStatus = false;
|
|
|
|
bool printName = true;
|
2006-07-25 19:40:38 +03:00
|
|
|
bool printAttrPath = false;
|
2004-07-01 16:13:37 +03:00
|
|
|
bool printSystem = false;
|
|
|
|
bool printDrvPath = false;
|
2005-02-11 18:56:45 +02:00
|
|
|
bool printOutPath = false;
|
2006-03-10 18:20:42 +02:00
|
|
|
bool printDescription = false;
|
2007-05-01 14:30:52 +03:00
|
|
|
bool printMeta = false;
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
bool compareVersions = false;
|
2006-08-03 18:52:09 +03:00
|
|
|
bool xmlOutput = false;
|
2013-11-19 01:41:45 +02:00
|
|
|
bool jsonOutput = false;
|
2004-07-01 16:13:37 +03:00
|
|
|
|
2003-11-19 19:27:16 +02:00
|
|
|
enum { sInstalled, sAvailable } source = sInstalled;
|
|
|
|
|
2012-07-31 02:55:41 +03:00
|
|
|
settings.readOnlyMode = true; /* makes evaluation a bit faster */
|
2004-10-27 13:24:44 +03:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
for (Strings::iterator i = opFlags.begin(); i != opFlags.end(); ) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto arg = *i++;
|
2007-09-17 22:24:07 +03:00
|
|
|
if (arg == "--status" || arg == "-s") printStatus = true;
|
|
|
|
else if (arg == "--no-name") printName = false;
|
|
|
|
else if (arg == "--system") printSystem = true;
|
|
|
|
else if (arg == "--description") printDescription = true;
|
|
|
|
else if (arg == "--compare-versions" || arg == "-c") compareVersions = true;
|
|
|
|
else if (arg == "--drv-path") printDrvPath = true;
|
|
|
|
else if (arg == "--out-path") printOutPath = true;
|
|
|
|
else if (arg == "--meta") printMeta = true;
|
|
|
|
else if (arg == "--installed") source = sInstalled;
|
|
|
|
else if (arg == "--available" || arg == "-a") source = sAvailable;
|
|
|
|
else if (arg == "--xml") xmlOutput = true;
|
2013-11-19 01:41:45 +02:00
|
|
|
else if (arg == "--json") jsonOutput = true;
|
2007-09-17 22:24:07 +03:00
|
|
|
else if (arg == "--attr-path" || arg == "-P") printAttrPath = true;
|
|
|
|
else if (arg == "--attr" || arg == "-A")
|
2014-08-13 04:50:44 +03:00
|
|
|
attrPath = needArg(i, opFlags, arg);
|
|
|
|
else
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", arg);
|
2006-03-10 12:24:46 +02:00
|
|
|
}
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2020-03-01 10:11:22 +02:00
|
|
|
if (printAttrPath && source != sAvailable)
|
|
|
|
throw UsageError("--attr-path(-P) only works with --available");
|
2012-12-03 19:19:49 +02:00
|
|
|
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
/* Obtain derivation information from the specified source. */
|
2006-02-08 15:21:16 +02:00
|
|
|
DrvInfos availElems, installedElems;
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2007-09-17 22:24:07 +03:00
|
|
|
if (source == sInstalled || compareVersions || printStatus)
|
2014-08-13 04:50:44 +03:00
|
|
|
installedElems = queryInstalled(*globals.state, globals.profile);
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2007-09-17 22:24:07 +03:00
|
|
|
if (source == sAvailable || compareVersions)
|
2023-04-06 14:15:50 +03:00
|
|
|
loadDerivations(*globals.state, *globals.instSource.nixExprPath,
|
2014-09-19 17:49:41 +03:00
|
|
|
globals.instSource.systemFilter, *globals.instSource.autoArgs,
|
2007-09-17 22:24:07 +03:00
|
|
|
attrPath, availElems);
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
DrvInfos elems_ = filterBySelector(*globals.state,
|
2006-03-10 12:24:46 +02:00
|
|
|
source == sInstalled ? installedElems : availElems,
|
2014-08-13 04:50:44 +03:00
|
|
|
opArgs, false);
|
2011-04-11 19:27:05 +03:00
|
|
|
|
2006-02-08 15:21:16 +02:00
|
|
|
DrvInfos & otherElems(source == sInstalled ? availElems : installedElems);
|
2004-02-02 12:51:54 +02:00
|
|
|
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2004-02-02 12:51:54 +02:00
|
|
|
/* Sort them by name. */
|
2006-02-08 15:21:16 +02:00
|
|
|
/* !!! */
|
2022-02-21 17:32:34 +02:00
|
|
|
std::vector<DrvInfo> elems;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems_) elems.push_back(i);
|
2013-11-19 01:41:45 +02:00
|
|
|
sort(elems.begin(), elems.end(), cmpElemByName);
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2004-07-01 16:13:37 +03:00
|
|
|
/* We only need to know the installed paths when we are querying
|
|
|
|
the status of the derivation. */
|
2022-03-02 11:57:19 +02:00
|
|
|
StorePathSet installed; /* installed paths */
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2022-03-02 11:57:19 +02:00
|
|
|
if (printStatus)
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : installedElems)
|
|
|
|
installed.insert(i.queryOutPath());
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
|
2012-07-11 17:14:06 +03:00
|
|
|
|
|
|
|
/* Query which paths have substitutes. */
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet validPaths;
|
|
|
|
StorePathSet substitutablePaths;
|
2012-12-03 22:01:41 +02:00
|
|
|
if (printStatus || globals.prebuiltOnly) {
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet paths;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems)
|
2012-07-11 17:14:06 +03:00
|
|
|
try {
|
2022-03-02 11:57:19 +02:00
|
|
|
paths.insert(i.queryOutPath());
|
2012-07-11 17:14:06 +03:00
|
|
|
} catch (AssertionError & e) {
|
2017-07-30 14:27:57 +03:00
|
|
|
printMsg(lvlTalkative, "skipping derivation named '%s' which gives an assertion failure", i.queryName());
|
2015-07-17 20:24:28 +03:00
|
|
|
i.setFailed();
|
2012-07-11 17:14:06 +03:00
|
|
|
}
|
2022-03-02 11:57:19 +02:00
|
|
|
validPaths = store.queryValidPaths(paths);
|
|
|
|
substitutablePaths = store.querySubstitutablePaths(paths);
|
2012-07-11 17:14:06 +03:00
|
|
|
}
|
|
|
|
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
/* Print the desired columns, or XML output. */
|
2013-11-19 01:41:45 +02:00
|
|
|
if (jsonOutput) {
|
`nix-env --query`: fix `--json` ignoring `--drv-path`
```json
{
"AMB-plugins": {
"drvPath": "/nix/store/l99cb7h2hy8dg005arsjbd9kx0w05d3h-AMB-plugins-0.8.1.drv",
"name": "AMB-plugins-0.8.1",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "AMB-plugins",
"system": "x86_64-linux",
"version": "0.8.1"
},
"ArchiSteamFarm": {
"drvPath": "/nix/store/nhplgyjj34fz6hjmnyih25gxscfh8s7b-ArchiSteamFarm-5.4.12.5.drv",
"name": "ArchiSteamFarm-5.4.12.5",
"outputName": "out",
"outputs": {
"out": null
},
"pname": "ArchiSteamFarm",
"system": "x86_64-linux",
"version": "5.4.12.5"
},
...
```
2023-10-31 01:36:13 +02:00
|
|
|
queryJSON(globals, elems, printOutPath, printDrvPath, printMeta);
|
2022-03-16 17:15:11 +02:00
|
|
|
cout << '\n';
|
2013-11-19 01:41:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:26:37 +03:00
|
|
|
bool tty = isatty(STDOUT_FILENO);
|
|
|
|
RunPager pager;
|
|
|
|
|
2004-07-01 16:35:10 +03:00
|
|
|
Table table;
|
2006-09-05 00:06:23 +03:00
|
|
|
std::ostringstream dummy;
|
2006-08-16 13:32:30 +03:00
|
|
|
XMLWriter xml(true, *(xmlOutput ? &cout : &dummy));
|
2006-08-03 18:52:09 +03:00
|
|
|
XMLOpenElement xmlRoot(xml, "items");
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : elems) {
|
2006-03-08 18:03:58 +02:00
|
|
|
try {
|
2015-07-17 20:24:28 +03:00
|
|
|
if (i.hasFailed()) continue;
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
//Activity act(*logger, lvlDebug, "outputting query result '%1%'", i.attrPath);
|
2006-08-03 18:52:09 +03:00
|
|
|
|
2012-12-03 22:01:41 +02:00
|
|
|
if (globals.prebuiltOnly &&
|
2022-03-02 11:57:19 +02:00
|
|
|
!validPaths.count(i.queryOutPath()) &&
|
|
|
|
!substitutablePaths.count(i.queryOutPath()))
|
2012-12-03 22:01:41 +02:00
|
|
|
continue;
|
2011-04-11 19:27:05 +03:00
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
/* For table output. */
|
2006-03-08 18:03:58 +02:00
|
|
|
Strings columns;
|
2006-08-03 18:52:09 +03:00
|
|
|
|
|
|
|
/* For XML output. */
|
|
|
|
XMLAttrs attrs;
|
2007-04-26 17:20:31 +03:00
|
|
|
|
2006-03-08 18:03:58 +02:00
|
|
|
if (printStatus) {
|
2022-03-02 11:57:19 +02:00
|
|
|
auto outPath = i.queryOutPath();
|
|
|
|
bool hasSubs = substitutablePaths.count(outPath);
|
|
|
|
bool isInstalled = installed.count(outPath);
|
|
|
|
bool isValid = validPaths.count(outPath);
|
2006-08-03 18:52:09 +03:00
|
|
|
if (xmlOutput) {
|
|
|
|
attrs["installed"] = isInstalled ? "1" : "0";
|
|
|
|
attrs["valid"] = isValid ? "1" : "0";
|
2006-12-01 00:43:55 +02:00
|
|
|
attrs["substitutable"] = hasSubs ? "1" : "0";
|
2006-08-03 18:52:09 +03:00
|
|
|
} else
|
|
|
|
columns.push_back(
|
2022-02-25 17:00:00 +02:00
|
|
|
(std::string) (isInstalled ? "I" : "-")
|
2006-08-03 18:52:09 +03:00
|
|
|
+ (isValid ? "P" : "-")
|
2006-12-01 00:43:55 +02:00
|
|
|
+ (hasSubs ? "S" : "-"));
|
2006-03-08 18:03:58 +02:00
|
|
|
}
|
2004-07-01 16:13:37 +03:00
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
if (xmlOutput)
|
2015-07-17 20:24:28 +03:00
|
|
|
attrs["attrPath"] = i.attrPath;
|
2006-08-03 18:52:09 +03:00
|
|
|
else if (printAttrPath)
|
2015-07-17 20:24:28 +03:00
|
|
|
columns.push_back(i.attrPath);
|
2006-07-25 19:40:38 +03:00
|
|
|
|
2019-07-28 05:40:19 +03:00
|
|
|
if (xmlOutput) {
|
|
|
|
auto drvName = DrvName(i.queryName());
|
|
|
|
attrs["name"] = drvName.fullName;
|
|
|
|
attrs["pname"] = drvName.name;
|
|
|
|
attrs["version"] = drvName.version;
|
|
|
|
} else if (printName) {
|
2017-07-17 20:02:56 +03:00
|
|
|
columns.push_back(i.queryName());
|
2019-07-28 05:40:19 +03:00
|
|
|
}
|
2006-03-08 18:03:58 +02:00
|
|
|
|
|
|
|
if (compareVersions) {
|
|
|
|
/* Compare this element against the versions of the
|
|
|
|
same named packages in either the set of available
|
|
|
|
elements, or the set of installed elements. !!!
|
|
|
|
This is O(N * M), should be O(N * lg M). */
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string version;
|
2015-07-17 20:24:28 +03:00
|
|
|
VersionDiff diff = compareVersionAgainstSet(i, otherElems, version);
|
2006-08-03 18:52:09 +03:00
|
|
|
|
2006-03-08 18:03:58 +02:00
|
|
|
char ch;
|
|
|
|
switch (diff) {
|
|
|
|
case cvLess: ch = '>'; break;
|
|
|
|
case cvEqual: ch = '='; break;
|
|
|
|
case cvGreater: ch = '<'; break;
|
|
|
|
case cvUnavail: ch = '-'; break;
|
|
|
|
default: abort();
|
|
|
|
}
|
2006-08-03 18:52:09 +03:00
|
|
|
|
|
|
|
if (xmlOutput) {
|
|
|
|
if (diff != cvUnavail) {
|
|
|
|
attrs["versionDiff"] = ch;
|
|
|
|
attrs["maxComparedVersion"] = version;
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto column = (std::string) "" + ch + " " + version;
|
2014-08-20 22:26:37 +03:00
|
|
|
if (diff == cvGreater && tty)
|
2014-08-20 17:01:16 +03:00
|
|
|
column = ANSI_RED + column + ANSI_NORMAL;
|
2006-08-03 18:52:09 +03:00
|
|
|
columns.push_back(column);
|
|
|
|
}
|
* New query option: `--compare-versions' or `-c' to compare installed
versions to available versions, or vice versa.
For example, the following compares installed versions to available
versions:
$ nix-env -qc
autoconf-2.59 = 2.59
automake-1.9.4 < 1.9.6
f-spot-0.0.10 - ?
firefox-1.0.4 < 1.0.7
...
I.e., there are newer versions available (in the current default Nix
expression) for Automake and Firefox, but not for Autoconf, and
F-Spot is missing altogether.
Conversely, the available versions can be compared to the installed
versions:
$ nix-env -qac
autoconf-2.59 = 2.59
automake-1.9.6 > 1.9.4
bash-3.0 - ?
firefox-1.0.7 > 1.0.4
...
Note that bash is available but no version of it is installed.
If multiple versions are available for comparison, then the highest
is used. E.g., if Subversion 1.2.0 is installed, and Subversion
1.1.4 and 1.2.3 are available, then `nix-env -qc' will print `<
1.2.3', not `> 1.1.4'.
If higher versions are available, the version column is printed in
red (using ANSI escape codes).
2005-10-06 17:44:54 +03:00
|
|
|
}
|
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
if (xmlOutput) {
|
2017-07-17 20:02:56 +03:00
|
|
|
if (i.querySystem() != "") attrs["system"] = i.querySystem();
|
2006-08-03 18:52:09 +03:00
|
|
|
}
|
2012-11-28 14:49:44 +02:00
|
|
|
else if (printSystem)
|
2017-07-17 20:02:56 +03:00
|
|
|
columns.push_back(i.querySystem());
|
2005-10-06 18:51:59 +03:00
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
if (printDrvPath) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto drvPath = i.queryDrvPath();
|
2006-08-03 18:52:09 +03:00
|
|
|
if (xmlOutput) {
|
2022-03-02 11:57:19 +02:00
|
|
|
if (drvPath) attrs["drvPath"] = store.printStorePath(*drvPath);
|
2006-08-03 18:52:09 +03:00
|
|
|
} else
|
2022-03-02 11:57:19 +02:00
|
|
|
columns.push_back(drvPath ? store.printStorePath(*drvPath) : "-");
|
2006-08-03 18:52:09 +03:00
|
|
|
}
|
2012-11-28 14:49:44 +02:00
|
|
|
|
2022-03-12 14:47:01 +02:00
|
|
|
if (xmlOutput)
|
|
|
|
attrs["outputName"] = i.queryOutputName();
|
|
|
|
|
2012-11-28 14:49:44 +02:00
|
|
|
if (printOutPath && !xmlOutput) {
|
2015-07-17 20:24:28 +03:00
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs();
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : outputs) {
|
2012-11-28 14:49:44 +02:00
|
|
|
if (!s.empty()) s += ';';
|
2015-07-17 20:24:28 +03:00
|
|
|
if (j.first != "out") { s += j.first; s += "="; }
|
2022-03-12 14:47:01 +02:00
|
|
|
s += store.printStorePath(*j.second);
|
2012-11-28 14:49:44 +02:00
|
|
|
}
|
|
|
|
columns.push_back(s);
|
2006-08-03 18:52:09 +03:00
|
|
|
}
|
2006-03-10 18:20:42 +02:00
|
|
|
|
|
|
|
if (printDescription) {
|
2022-02-25 17:00:00 +02:00
|
|
|
auto descr = i.queryMetaString("description");
|
2006-08-03 18:52:09 +03:00
|
|
|
if (xmlOutput) {
|
|
|
|
if (descr != "") attrs["description"] = descr;
|
|
|
|
} else
|
|
|
|
columns.push_back(descr);
|
2006-03-10 18:20:42 +02:00
|
|
|
}
|
2006-08-03 18:52:09 +03:00
|
|
|
|
2012-11-28 14:49:44 +02:00
|
|
|
if (xmlOutput) {
|
2022-03-12 14:47:01 +02:00
|
|
|
XMLOpenElement item(xml, "item", attrs);
|
|
|
|
DrvInfo::Outputs outputs = i.queryOutputs(printOutPath);
|
|
|
|
for (auto & j : outputs) {
|
|
|
|
XMLAttrs attrs2;
|
|
|
|
attrs2["name"] = j.first;
|
|
|
|
if (j.second)
|
|
|
|
attrs2["path"] = store.printStorePath(*j.second);
|
|
|
|
xml.writeEmptyElement("output", attrs2);
|
|
|
|
}
|
|
|
|
if (printMeta) {
|
|
|
|
StringSet metaNames = i.queryMetaNames();
|
|
|
|
for (auto & j : metaNames) {
|
|
|
|
XMLAttrs attrs2;
|
|
|
|
attrs2["name"] = j;
|
|
|
|
Value * v = i.queryMeta(j);
|
|
|
|
if (!v)
|
|
|
|
printError(
|
|
|
|
"derivation '%s' has invalid meta attribute '%s'",
|
|
|
|
i.queryName(), j);
|
|
|
|
else {
|
|
|
|
if (v->type() == nString) {
|
|
|
|
attrs2["type"] = "string";
|
2023-09-26 04:30:41 +03:00
|
|
|
attrs2["value"] = v->c_str();
|
2022-03-12 14:47:01 +02:00
|
|
|
xml.writeEmptyElement("meta", attrs2);
|
|
|
|
} else if (v->type() == nInt) {
|
|
|
|
attrs2["type"] = "int";
|
2023-03-02 16:44:19 +02:00
|
|
|
attrs2["value"] = fmt("%1%", v->integer);
|
2022-03-12 14:47:01 +02:00
|
|
|
xml.writeEmptyElement("meta", attrs2);
|
|
|
|
} else if (v->type() == nFloat) {
|
|
|
|
attrs2["type"] = "float";
|
2023-03-02 16:44:19 +02:00
|
|
|
attrs2["value"] = fmt("%1%", v->fpoint);
|
2022-03-12 14:47:01 +02:00
|
|
|
xml.writeEmptyElement("meta", attrs2);
|
|
|
|
} else if (v->type() == nBool) {
|
|
|
|
attrs2["type"] = "bool";
|
|
|
|
attrs2["value"] = v->boolean ? "true" : "false";
|
|
|
|
xml.writeEmptyElement("meta", attrs2);
|
|
|
|
} else if (v->type() == nList) {
|
|
|
|
attrs2["type"] = "strings";
|
|
|
|
XMLOpenElement m(xml, "meta", attrs2);
|
|
|
|
for (auto elem : v->listItems()) {
|
|
|
|
if (elem->type() != nString) continue;
|
|
|
|
XMLAttrs attrs3;
|
2023-09-26 04:30:41 +03:00
|
|
|
attrs3["value"] = elem->c_str();
|
2022-03-12 14:47:01 +02:00
|
|
|
xml.writeEmptyElement("string", attrs3);
|
2013-11-19 15:09:03 +02:00
|
|
|
}
|
2022-03-12 14:47:01 +02:00
|
|
|
} else if (v->type() == nAttrs) {
|
|
|
|
attrs2["type"] = "strings";
|
|
|
|
XMLOpenElement m(xml, "meta", attrs2);
|
|
|
|
Bindings & attrs = *v->attrs;
|
|
|
|
for (auto &i : attrs) {
|
|
|
|
Attr & a(*attrs.find(i.name));
|
|
|
|
if(a.value->type() != nString) continue;
|
|
|
|
XMLAttrs attrs3;
|
2022-03-05 15:40:24 +02:00
|
|
|
attrs3["type"] = globals.state->symbols[i.name];
|
2023-09-26 04:30:41 +03:00
|
|
|
attrs3["value"] = a.value->c_str();
|
2022-03-12 14:47:01 +02:00
|
|
|
xml.writeEmptyElement("string", attrs3);
|
|
|
|
}
|
2012-11-28 14:49:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 14:47:01 +02:00
|
|
|
}
|
2012-11-28 14:49:44 +02:00
|
|
|
} else
|
2006-08-03 18:52:09 +03:00
|
|
|
table.push_back(columns);
|
|
|
|
|
2006-10-17 17:01:45 +03:00
|
|
|
cout.flush();
|
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
} catch (AssertionError & e) {
|
2017-07-30 14:27:57 +03:00
|
|
|
printMsg(lvlTalkative, "skipping derivation named '%1%' which gives an assertion failure", i.queryName());
|
2013-11-19 15:09:03 +02:00
|
|
|
} catch (Error & e) {
|
2022-12-13 01:48:04 +02:00
|
|
|
e.addTrace(nullptr, "while querying the derivation named '%1%'", i.queryName());
|
2013-11-19 15:09:03 +02:00
|
|
|
throw;
|
2006-03-08 18:03:58 +02:00
|
|
|
}
|
2003-11-19 19:27:16 +02:00
|
|
|
}
|
2004-07-01 16:35:10 +03:00
|
|
|
|
2006-08-03 18:52:09 +03:00
|
|
|
if (!xmlOutput) printTable(table);
|
2003-11-19 19:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opSwitchProfile(Globals & globals, Strings opFlags, Strings opArgs)
|
2004-01-05 13:18:59 +02:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2004-02-06 12:59:06 +02:00
|
|
|
if (opArgs.size() != 1)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("exactly one argument expected");
|
2004-01-05 13:18:59 +02:00
|
|
|
|
2006-12-02 18:41:36 +02:00
|
|
|
Path profile = absPath(opArgs.front());
|
2021-11-17 22:35:21 +02:00
|
|
|
Path profileLink = settings.useXDGBaseDirectories ? createNixStateDir() + "/profile" : getHome() + "/.nix-profile";
|
2004-01-05 13:18:59 +02:00
|
|
|
|
2004-02-06 12:30:20 +02:00
|
|
|
switchLink(profileLink, profile);
|
2004-01-05 18:26:43 +02:00
|
|
|
}
|
2004-01-05 13:18:59 +02:00
|
|
|
|
2004-01-05 18:26:43 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opSwitchGeneration(Globals & globals, Strings opFlags, Strings opArgs)
|
2004-02-08 16:07:43 +02:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2004-02-08 16:07:43 +02:00
|
|
|
if (opArgs.size() != 1)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("exactly one argument expected");
|
2004-02-08 16:07:43 +02:00
|
|
|
|
2021-01-08 13:22:21 +02:00
|
|
|
if (auto dstGen = string2Int<GenerationNumber>(opArgs.front()))
|
2021-09-14 20:05:28 +03:00
|
|
|
switchGeneration(globals.profile, *dstGen, globals.dryRun);
|
2021-01-08 13:22:21 +02:00
|
|
|
else
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("expected a generation number");
|
2004-02-08 16:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opRollback(Globals & globals, Strings opFlags, Strings opArgs)
|
2004-02-08 16:07:43 +02:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2004-02-08 16:07:43 +02:00
|
|
|
if (opArgs.size() != 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("no arguments expected");
|
2004-02-08 16:07:43 +02:00
|
|
|
|
2021-09-14 20:05:28 +03:00
|
|
|
switchGeneration(globals.profile, {}, globals.dryRun);
|
2004-02-08 16:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opListGenerations(Globals & globals, Strings opFlags, Strings opArgs)
|
2004-02-06 18:03:27 +02:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2004-02-06 18:03:27 +02:00
|
|
|
if (opArgs.size() != 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("no arguments expected");
|
2004-02-06 18:03:27 +02:00
|
|
|
|
2006-09-15 01:33:53 +03:00
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, globals.profile);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2020-07-16 16:14:22 +03:00
|
|
|
auto [gens, curGen] = findGenerations(globals.profile);
|
2004-02-06 18:03:27 +02:00
|
|
|
|
2014-08-20 22:26:37 +03:00
|
|
|
RunPager pager;
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : gens) {
|
2004-02-06 18:03:27 +02:00
|
|
|
tm t;
|
2015-07-17 20:24:28 +03:00
|
|
|
if (!localtime_r(&i.creationTime, &t)) throw Error("cannot convert time");
|
2023-03-02 16:44:19 +02:00
|
|
|
logger->cout("%|4| %|4|-%|02|-%|02| %|02|:%|02|:%|02| %||",
|
|
|
|
i.number,
|
|
|
|
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
|
|
|
|
t.tm_hour, t.tm_min, t.tm_sec,
|
|
|
|
i.number == curGen ? "(current)" : "");
|
2004-02-06 18:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opArgs)
|
2004-09-10 16:32:08 +03:00
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("unknown flag '%1%'", opFlags.front());
|
2004-09-10 16:32:08 +03:00
|
|
|
|
2015-05-21 17:26:03 +03:00
|
|
|
if (opArgs.size() == 1 && opArgs.front() == "old") {
|
|
|
|
deleteOldGenerations(globals.profile, globals.dryRun);
|
2022-02-25 17:00:00 +02:00
|
|
|
} else if (opArgs.size() == 1 && opArgs.front().find('d') != std::string::npos) {
|
2023-06-19 07:04:59 +03:00
|
|
|
auto t = parseOlderThanTimeSpec(opArgs.front());
|
|
|
|
deleteGenerationsOlderThan(globals.profile, t, globals.dryRun);
|
2022-02-25 17:00:00 +02:00
|
|
|
} else if (opArgs.size() == 1 && opArgs.front().find('+') != std::string::npos) {
|
2021-09-14 20:48:16 +03:00
|
|
|
if (opArgs.front().size() < 2)
|
|
|
|
throw Error("invalid number of generations '%1%'", opArgs.front());
|
2022-02-25 17:00:00 +02:00
|
|
|
auto str_max = opArgs.front().substr(1);
|
2021-01-08 13:22:21 +02:00
|
|
|
auto max = string2Int<GenerationNumber>(str_max);
|
2023-06-19 07:04:59 +03:00
|
|
|
if (!max)
|
2021-09-14 20:48:16 +03:00
|
|
|
throw Error("invalid number of generations to keep '%1%'", opArgs.front());
|
2021-01-08 13:22:21 +02:00
|
|
|
deleteGenerationsGreaterThan(globals.profile, *max, globals.dryRun);
|
2015-05-21 17:26:03 +03:00
|
|
|
} else {
|
2020-07-16 16:14:22 +03:00
|
|
|
std::set<GenerationNumber> gens;
|
2015-05-21 17:26:03 +03:00
|
|
|
for (auto & i : opArgs) {
|
2021-01-08 13:22:21 +02:00
|
|
|
if (auto n = string2Int<GenerationNumber>(i))
|
|
|
|
gens.insert(*n);
|
|
|
|
else
|
2020-04-22 02:07:07 +03:00
|
|
|
throw UsageError("invalid generation number '%1%'", i);
|
2004-09-10 16:32:08 +03:00
|
|
|
}
|
2015-05-21 17:26:03 +03:00
|
|
|
deleteGenerations(globals.profile, gens, globals.dryRun);
|
2004-09-10 16:32:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 15:19:49 +03:00
|
|
|
static void opVersion(Globals & globals, Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
printVersion("nix-env");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static int main_nix_env(int argc, char * * argv)
|
2003-11-19 19:27:16 +02:00
|
|
|
{
|
2018-10-26 12:35:46 +03:00
|
|
|
{
|
2017-10-24 13:45:11 +03:00
|
|
|
Strings opFlags, opArgs;
|
2014-08-13 04:50:44 +03:00
|
|
|
Operation op = 0;
|
2023-03-23 17:27:41 +02:00
|
|
|
std::string opName;
|
|
|
|
bool showHelp = false;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string file;
|
2014-08-13 04:50:44 +03:00
|
|
|
|
|
|
|
Globals globals;
|
|
|
|
|
|
|
|
globals.instSource.type = srcUnknown;
|
|
|
|
globals.instSource.systemFilter = "*";
|
|
|
|
|
2023-07-24 21:02:05 +03:00
|
|
|
Path nixExprPath = getNixDefExpr();
|
2023-04-06 14:15:50 +03:00
|
|
|
|
|
|
|
if (!pathExists(nixExprPath)) {
|
2019-10-10 10:14:05 +03:00
|
|
|
try {
|
2023-04-06 14:15:50 +03:00
|
|
|
createDirs(nixExprPath);
|
2019-10-09 20:50:46 +03:00
|
|
|
replaceSymlink(
|
2023-03-21 14:37:19 +02:00
|
|
|
defaultChannelsDir(),
|
2023-04-06 14:15:50 +03:00
|
|
|
nixExprPath + "/channels");
|
2019-10-10 10:14:05 +03:00
|
|
|
if (getuid() != 0)
|
|
|
|
replaceSymlink(
|
2023-03-21 14:37:19 +02:00
|
|
|
rootChannelsDir(),
|
2023-04-06 14:15:50 +03:00
|
|
|
nixExprPath + "/channels_root");
|
2019-10-10 10:14:05 +03:00
|
|
|
} catch (Error &) { }
|
2019-10-09 20:50:46 +03:00
|
|
|
}
|
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
globals.dryRun = false;
|
|
|
|
globals.preserveInstalled = false;
|
|
|
|
globals.removeAll = false;
|
|
|
|
globals.prebuiltOnly = false;
|
|
|
|
|
2017-10-24 13:45:11 +03:00
|
|
|
struct MyArgs : LegacyArgs, MixEvalArgs
|
|
|
|
{
|
|
|
|
using LegacyArgs::LegacyArgs;
|
|
|
|
};
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
MyArgs myArgs(std::string(baseNameOf(argv[0])), [&](Strings::iterator & arg, const Strings::iterator & end) {
|
2014-08-13 04:50:44 +03:00
|
|
|
Operation oldOp = op;
|
|
|
|
|
|
|
|
if (*arg == "--help")
|
2023-03-23 17:27:41 +02:00
|
|
|
showHelp = true;
|
2014-08-13 04:50:44 +03:00
|
|
|
else if (*arg == "--version")
|
2015-07-23 15:19:49 +03:00
|
|
|
op = opVersion;
|
2023-03-23 17:27:41 +02:00
|
|
|
else if (*arg == "--install" || *arg == "-i") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opInstall;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-install";
|
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
else if (*arg == "--force-name") // undocumented flag for nix-install-package
|
|
|
|
globals.forceName = getArg(*arg, arg, end);
|
2023-03-23 17:27:41 +02:00
|
|
|
else if (*arg == "--uninstall" || *arg == "-e") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opUninstall;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-uninstall";
|
|
|
|
}
|
|
|
|
else if (*arg == "--upgrade" || *arg == "-u") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opUpgrade;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-upgrade";
|
|
|
|
}
|
|
|
|
else if (*arg == "--set-flag") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opSetFlag;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = arg->substr(1);
|
|
|
|
}
|
|
|
|
else if (*arg == "--set") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opSet;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = arg->substr(1);
|
|
|
|
}
|
|
|
|
else if (*arg == "--query" || *arg == "-q") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opQuery;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-query";
|
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
else if (*arg == "--profile" || *arg == "-p")
|
|
|
|
globals.profile = absPath(getArg(*arg, arg, end));
|
|
|
|
else if (*arg == "--file" || *arg == "-f")
|
|
|
|
file = getArg(*arg, arg, end);
|
2023-03-23 17:27:41 +02:00
|
|
|
else if (*arg == "--switch-profile" || *arg == "-S") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opSwitchProfile;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-switch-profile";
|
|
|
|
}
|
|
|
|
else if (*arg == "--switch-generation" || *arg == "-G") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opSwitchGeneration;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = "-switch-generation";
|
|
|
|
}
|
|
|
|
else if (*arg == "--rollback") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opRollback;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = arg->substr(1);
|
|
|
|
}
|
|
|
|
else if (*arg == "--list-generations") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opListGenerations;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = arg->substr(1);
|
|
|
|
}
|
|
|
|
else if (*arg == "--delete-generations") {
|
2014-08-13 04:50:44 +03:00
|
|
|
op = opDeleteGenerations;
|
2023-03-23 17:27:41 +02:00
|
|
|
opName = arg->substr(1);
|
|
|
|
}
|
2014-08-13 04:50:44 +03:00
|
|
|
else if (*arg == "--dry-run") {
|
2016-09-21 17:11:01 +03:00
|
|
|
printInfo("(dry run; not doing anything)");
|
2014-08-13 04:50:44 +03:00
|
|
|
globals.dryRun = true;
|
|
|
|
}
|
|
|
|
else if (*arg == "--system-filter")
|
|
|
|
globals.instSource.systemFilter = getArg(*arg, arg, end);
|
|
|
|
else if (*arg == "--prebuilt-only" || *arg == "-b")
|
|
|
|
globals.prebuiltOnly = true;
|
|
|
|
else if (*arg != "" && arg->at(0) == '-') {
|
|
|
|
opFlags.push_back(*arg);
|
2014-08-18 16:48:23 +03:00
|
|
|
/* FIXME: hacky */
|
|
|
|
if (*arg == "--from-profile" ||
|
|
|
|
(op == opQuery && (*arg == "--attr" || *arg == "-A")))
|
2014-08-13 04:50:44 +03:00
|
|
|
opFlags.push_back(getArg(*arg, arg, end));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
opArgs.push_back(*arg);
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
if (oldOp && oldOp != op)
|
|
|
|
throw UsageError("only one operation may be specified");
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
return true;
|
|
|
|
});
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2017-10-24 13:45:11 +03:00
|
|
|
myArgs.parseCmdline(argvToStrings(argc, argv));
|
|
|
|
|
2023-03-23 17:27:41 +02:00
|
|
|
if (showHelp) showManPage("nix-env" + opName);
|
2014-08-13 04:50:44 +03:00
|
|
|
if (!op) throw UsageError("no operation specified");
|
2003-11-19 19:27:16 +02:00
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 15:28:26 +02:00
|
|
|
auto store = openStore();
|
2015-05-05 18:09:42 +03:00
|
|
|
|
2017-10-24 13:45:11 +03:00
|
|
|
globals.state = std::shared_ptr<EvalState>(new EvalState(myArgs.searchPath, store));
|
2023-04-28 17:57:37 +03:00
|
|
|
globals.state->repair = myArgs.repair;
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2023-04-06 14:15:50 +03:00
|
|
|
globals.instSource.nixExprPath = std::make_shared<SourcePath>(
|
|
|
|
file != ""
|
|
|
|
? lookupFileArg(*globals.state, file)
|
|
|
|
: globals.state->rootPath(CanonPath(nixExprPath)));
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2017-10-24 13:45:11 +03:00
|
|
|
globals.instSource.autoArgs = myArgs.getAutoArgs(*globals.state);
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2014-08-13 04:50:44 +03:00
|
|
|
if (globals.profile == "")
|
2019-11-22 17:06:44 +02:00
|
|
|
globals.profile = getEnv("NIX_PROFILE").value_or("");
|
2012-12-12 17:01:46 +02:00
|
|
|
|
2020-03-24 15:26:13 +02:00
|
|
|
if (globals.profile == "")
|
|
|
|
globals.profile = getDefaultProfile();
|
2012-12-03 19:19:49 +02:00
|
|
|
|
2022-06-09 17:26:36 +03:00
|
|
|
op(globals, std::move(opFlags), std::move(opArgs));
|
2003-11-19 19:27:16 +02:00
|
|
|
|
2023-10-09 17:25:53 +03:00
|
|
|
globals.state->maybePrintStats();
|
2018-10-26 12:35:46 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-19 19:27:16 +02:00
|
|
|
}
|
2018-10-26 12:35:46 +03:00
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static RegisterLegacyCommand r_nix_env("nix-env", main_nix_env);
|