2004-02-06 14:57:10 +00:00
|
|
|
#include "profiles.hh"
|
2004-02-06 14:49:41 +00:00
|
|
|
#include "names.hh"
|
2003-11-19 17:27:16 +00:00
|
|
|
#include "globals.hh"
|
2005-01-19 16:39:47 +00:00
|
|
|
#include "build.hh"
|
2003-11-19 17:27:16 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "parser.hh"
|
|
|
|
#include "eval.hh"
|
2003-12-01 15:55:05 +00:00
|
|
|
#include "help.txt.hh"
|
2004-10-29 11:22:49 +00:00
|
|
|
#include "nixexpr-ast.hh"
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <ctime>
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2005-02-11 16:56:45 +00:00
|
|
|
typedef enum {
|
|
|
|
srcNixExprDrvs,
|
|
|
|
srcNixExprs,
|
|
|
|
srcStorePaths,
|
|
|
|
srcProfile,
|
|
|
|
srcUnknown
|
|
|
|
} InstallSourceType;
|
|
|
|
|
|
|
|
|
|
|
|
struct InstallSourceInfo
|
|
|
|
{
|
|
|
|
InstallSourceType type;
|
|
|
|
Path nixExprPath; /* for srcNixExprDrvs, srcNixExprs */
|
|
|
|
Path profile; /* for srcProfile */
|
|
|
|
string systemFilter; /* for srcNixExprDrvs */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
struct Globals
|
|
|
|
{
|
2005-02-11 16:56:45 +00:00
|
|
|
InstallSourceInfo instSource;
|
2004-02-06 10:30:20 +00:00
|
|
|
Path profile;
|
2003-12-21 22:34:41 +00:00
|
|
|
EvalState state;
|
2004-02-09 11:59:39 +00:00
|
|
|
bool dryRun;
|
2004-06-28 14:40:26 +00:00
|
|
|
bool preserveInstalled;
|
2005-02-14 13:07:09 +00:00
|
|
|
bool keepDerivations;
|
2003-12-21 22:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef void (* Operation) (Globals & globals,
|
2003-11-19 17:27:16 +00:00
|
|
|
Strings opFlags, Strings opArgs);
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
struct UserEnvElem
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
|
|
|
string name;
|
2004-07-01 13:13:37 +00:00
|
|
|
string system;
|
2003-11-19 17:27:16 +00:00
|
|
|
Path drvPath;
|
|
|
|
Path outPath;
|
|
|
|
};
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
typedef map<Path, UserEnvElem> UserEnvElems;
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
|
2003-12-01 15:55:05 +00:00
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
cout << string((char *) helpText, sizeof helpText);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static bool parseDerivation(EvalState & state, Expr e, UserEnvElem & elem)
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
2004-10-26 22:54:26 +00:00
|
|
|
ATermList es;
|
2003-11-19 17:27:16 +00:00
|
|
|
e = evalExpr(state, e);
|
2004-10-26 22:54:26 +00:00
|
|
|
if (!matchAttrs(e, es)) return false;
|
2003-11-19 17:27:16 +00:00
|
|
|
Expr a = queryAttr(e, "type");
|
|
|
|
if (!a || evalString(state, a) != "derivation") return false;
|
|
|
|
|
|
|
|
a = queryAttr(e, "name");
|
|
|
|
if (!a) throw badTerm("derivation name missing", e);
|
2005-02-14 16:16:02 +00:00
|
|
|
elem.name = evalString(state, a);
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2004-07-01 13:13:37 +00:00
|
|
|
a = queryAttr(e, "system");
|
|
|
|
if (!a)
|
2005-02-14 16:16:02 +00:00
|
|
|
elem.system = "unknown";
|
2004-07-01 13:13:37 +00:00
|
|
|
else
|
2005-02-14 16:16:02 +00:00
|
|
|
elem.system = evalString(state, a);
|
2004-07-01 13:13:37 +00:00
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
a = queryAttr(e, "drvPath");
|
2005-02-14 16:16:02 +00:00
|
|
|
if (a) elem.drvPath = evalPath(state, a);
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
a = queryAttr(e, "outPath");
|
|
|
|
if (!a) throw badTerm("output path missing", e);
|
2005-02-14 16:16:02 +00:00
|
|
|
elem.outPath = evalPath(state, a);
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 17:07:43 +00:00
|
|
|
static void parseDerivations(EvalState & state, Expr e, UserEnvElems & elems)
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
2003-11-19 21:32:03 +00:00
|
|
|
ATermList es;
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElem elem;
|
2003-11-19 21:32:03 +00:00
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
e = evalExpr(state, e);
|
2003-11-24 11:01:19 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
if (parseDerivation(state, e, elem))
|
|
|
|
elems[elem.outPath] = elem;
|
2003-11-24 11:01:19 +00:00
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchAttrs(e, es)) {
|
2003-11-19 21:32:03 +00:00
|
|
|
ATermMap drvMap;
|
|
|
|
queryAllAttrs(e, drvMap);
|
|
|
|
for (ATermIterator i(drvMap.keys()); i; ++i) {
|
|
|
|
debug(format("evaluating attribute `%1%'") % *i);
|
2005-02-14 16:16:02 +00:00
|
|
|
if (parseDerivation(state, drvMap.get(*i), elem))
|
|
|
|
elems[elem.outPath] = elem;
|
2004-04-21 09:37:37 +00:00
|
|
|
else
|
2005-02-14 16:16:02 +00:00
|
|
|
parseDerivations(state, drvMap.get(*i), elems);
|
2003-11-19 21:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
else if (matchList(e, es)) {
|
2003-11-19 21:32:03 +00:00
|
|
|
for (ATermIterator i(es); i; ++i) {
|
2003-11-20 13:48:48 +00:00
|
|
|
debug(format("evaluating list element"));
|
2005-02-14 16:16:02 +00:00
|
|
|
if (parseDerivation(state, *i, elem))
|
|
|
|
elems[elem.outPath] = elem;
|
2004-04-21 09:37:37 +00:00
|
|
|
else
|
2005-02-14 16:16:02 +00:00
|
|
|
parseDerivations(state, *i, elems);
|
2003-11-19 21:32:03 +00:00
|
|
|
}
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-11 16:56:45 +00:00
|
|
|
static void loadDerivations(EvalState & state, Path nixExprPath,
|
2005-02-14 16:16:02 +00:00
|
|
|
string systemFilter, UserEnvElems & elems)
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
2005-02-14 17:07:43 +00:00
|
|
|
parseDerivations(state,
|
|
|
|
parseExprFromFile(state, absPath(nixExprPath)), elems);
|
2004-07-01 13:56:56 +00:00
|
|
|
|
|
|
|
/* Filter out all derivations not applicable to the current
|
|
|
|
system. */
|
2005-02-14 16:16:02 +00:00
|
|
|
for (UserEnvElems::iterator i = elems.begin(), j; i != elems.end(); i = j) {
|
2004-07-01 13:56:56 +00:00
|
|
|
j = i; j++;
|
|
|
|
if (systemFilter != "*" && i->second.system != systemFilter)
|
2005-02-14 16:16:02 +00:00
|
|
|
elems.erase(i);
|
2005-02-11 16:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-05 16:26:43 +00:00
|
|
|
static Path getHomeDir()
|
|
|
|
{
|
2004-05-12 09:35:51 +00:00
|
|
|
Path homeDir(getEnv("HOME", ""));
|
2004-01-05 16:26:43 +00:00
|
|
|
if (homeDir == "") throw Error("HOME environment variable not set");
|
|
|
|
return homeDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Path getDefNixExprPath()
|
|
|
|
{
|
|
|
|
return getHomeDir() + "/.nix-defexpr";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 22:27:41 +00:00
|
|
|
struct AddPos : TermFun
|
|
|
|
{
|
|
|
|
ATerm operator () (ATerm e)
|
|
|
|
{
|
|
|
|
ATerm x, y, z;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchBind(e, x, y, z)) return e;
|
|
|
|
if (matchBind2(e, x, y))
|
|
|
|
return makeBind(x, y, makeNoPos());
|
2004-04-05 22:27:41 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static void queryInstalled(EvalState & state, UserEnvElems & elems,
|
2003-12-21 22:34:41 +00:00
|
|
|
const Path & userEnv)
|
2003-11-19 21:32:03 +00:00
|
|
|
{
|
2003-12-21 22:34:41 +00:00
|
|
|
Path path = userEnv + "/manifest";
|
2003-11-19 21:32:03 +00:00
|
|
|
|
|
|
|
if (!pathExists(path)) return; /* not an error, assume nothing installed */
|
|
|
|
|
|
|
|
Expr e = ATreadFromNamedFile(path.c_str());
|
|
|
|
if (!e) throw Error(format("cannot read Nix expression from `%1%'") % path);
|
|
|
|
|
2004-04-05 22:27:41 +00:00
|
|
|
/* Compatibility: Bind(x, y) -> Bind(x, y, NoPos). */
|
|
|
|
AddPos addPos;
|
|
|
|
e = bottomupRewrite(addPos, e);
|
|
|
|
|
2005-02-14 17:07:43 +00:00
|
|
|
parseDerivations(state, e, elems);
|
2003-11-19 21:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static void createUserEnv(EvalState & state, const UserEnvElems & elems,
|
2005-02-14 13:07:09 +00:00
|
|
|
const Path & profile, bool keepDerivations)
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
2005-02-11 16:56:45 +00:00
|
|
|
/* Build the components in the user environment, if they don't
|
|
|
|
exist already. */
|
|
|
|
PathSet drvsToBuild;
|
2005-02-14 16:16:02 +00:00
|
|
|
for (UserEnvElems::const_iterator i = elems.begin();
|
|
|
|
i != elems.end(); ++i)
|
2005-02-11 16:56:45 +00:00
|
|
|
if (i->second.drvPath != "")
|
|
|
|
drvsToBuild.insert(i->second.drvPath);
|
|
|
|
|
|
|
|
debug(format("building user environment dependencies"));
|
|
|
|
buildDerivations(drvsToBuild);
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
/* Get the environment builder expression. */
|
2004-02-04 16:03:29 +00:00
|
|
|
Expr envBuilder = parseExprFromFile(state,
|
|
|
|
nixDataDir + "/nix/corepkgs/buildenv"); /* !!! */
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
/* Construct the whole top level derivation. */
|
2005-02-11 16:56:45 +00:00
|
|
|
PathSet references;
|
|
|
|
ATermList manifest = ATempty;
|
2003-11-19 17:27:16 +00:00
|
|
|
ATermList inputs = ATempty;
|
2005-02-14 16:16:02 +00:00
|
|
|
for (UserEnvElems::const_iterator i = elems.begin();
|
|
|
|
i != elems.end(); ++i)
|
2003-11-19 17:27:16 +00:00
|
|
|
{
|
2005-02-14 13:07:09 +00:00
|
|
|
Path drvPath = keepDerivations ? i->second.drvPath : "";
|
|
|
|
ATerm t = makeAttrs(ATmakeList5(
|
2004-11-03 18:12:03 +00:00
|
|
|
makeBind(toATerm("type"),
|
|
|
|
makeStr(toATerm("derivation")), makeNoPos()),
|
|
|
|
makeBind(toATerm("name"),
|
|
|
|
makeStr(toATerm(i->second.name)), makeNoPos()),
|
|
|
|
makeBind(toATerm("system"),
|
|
|
|
makeStr(toATerm(i->second.system)), makeNoPos()),
|
2005-02-14 13:07:09 +00:00
|
|
|
makeBind(toATerm("drvPath"),
|
|
|
|
makePath(toATerm(drvPath)), makeNoPos()),
|
2004-11-03 18:12:03 +00:00
|
|
|
makeBind(toATerm("outPath"),
|
|
|
|
makePath(toATerm(i->second.outPath)), makeNoPos())
|
2004-10-26 22:54:26 +00:00
|
|
|
));
|
2005-02-11 16:56:45 +00:00
|
|
|
manifest = ATinsert(manifest, t);
|
|
|
|
inputs = ATinsert(inputs, makeStr(toATerm(i->second.outPath)));
|
|
|
|
references.insert(i->second.outPath);
|
2005-02-14 13:07:09 +00:00
|
|
|
if (drvPath != "") references.insert(drvPath);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Also write a copy of the list of inputs to the store; we need
|
|
|
|
it for future modifications of the environment. */
|
2005-02-11 16:56:45 +00:00
|
|
|
Path manifestFile = addTextToStore("env-manifest",
|
|
|
|
atPrint(makeList(ATreverse(manifest))), references);
|
|
|
|
|
2004-10-26 22:54:26 +00:00
|
|
|
Expr topLevel = makeCall(envBuilder, makeAttrs(ATmakeList3(
|
2004-11-03 18:12:03 +00:00
|
|
|
makeBind(toATerm("system"),
|
|
|
|
makeStr(toATerm(thisSystem)), makeNoPos()),
|
2005-02-11 16:56:45 +00:00
|
|
|
makeBind(toATerm("derivations"),
|
|
|
|
makeList(ATreverse(inputs)), makeNoPos()),
|
2004-11-03 18:12:03 +00:00
|
|
|
makeBind(toATerm("manifest"),
|
2005-02-11 16:56:45 +00:00
|
|
|
makeAttrs(ATmakeList2(
|
|
|
|
makeBind(toATerm("type"),
|
|
|
|
makeStr(toATerm("storePath")), makeNoPos()),
|
|
|
|
makeBind(toATerm("outPath"),
|
|
|
|
makePath(toATerm(manifestFile)), makeNoPos())
|
|
|
|
)), makeNoPos())
|
2004-10-26 22:54:26 +00:00
|
|
|
)));
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
/* Instantiate it. */
|
|
|
|
debug(format("evaluating builder expression `%1%'") % topLevel);
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElem topLevelDrv;
|
2003-11-19 17:27:16 +00:00
|
|
|
if (!parseDerivation(state, topLevel, topLevelDrv))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
/* Realise the resulting store expression. */
|
2005-01-19 11:16:11 +00:00
|
|
|
debug(format("building user environment"));
|
2005-02-11 16:56:45 +00:00
|
|
|
buildDerivations(singleton<PathSet>(topLevelDrv.drvPath));
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
/* Switch the current user environment to the output path. */
|
|
|
|
debug(format("switching to new user environment"));
|
2005-02-14 16:16:02 +00:00
|
|
|
Path generation = createGeneration(profile, topLevelDrv.outPath);
|
2004-02-06 10:30:20 +00:00
|
|
|
switchLink(profile, generation);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static void queryInstSources(EvalState & state,
|
|
|
|
const InstallSourceInfo & instSource, const Strings & args,
|
|
|
|
UserEnvElems & elems)
|
2003-11-20 13:48:48 +00:00
|
|
|
{
|
2005-02-14 16:16:02 +00:00
|
|
|
switch (instSource.type) {
|
2003-11-20 13:48:48 +00:00
|
|
|
|
2005-02-14 16:16:02 +00: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: {
|
|
|
|
|
|
|
|
DrvNames selectors = drvNamesFromArgs(args);
|
|
|
|
|
|
|
|
/* Load the derivations from the (default or specified)
|
|
|
|
Nix expression. */
|
|
|
|
UserEnvElems allElems;
|
|
|
|
loadDerivations(state, instSource.nixExprPath,
|
|
|
|
instSource.systemFilter, allElems);
|
|
|
|
|
|
|
|
/* Filter out the ones we're not interested in. */
|
|
|
|
for (UserEnvElems::iterator i = allElems.begin();
|
|
|
|
i != allElems.end(); ++i)
|
|
|
|
{
|
|
|
|
DrvName drvName(i->second.name);
|
|
|
|
for (DrvNames::iterator j = selectors.begin();
|
|
|
|
j != selectors.end(); ++j)
|
|
|
|
{
|
|
|
|
if (j->matches(drvName)) {
|
|
|
|
j->hits++;
|
|
|
|
elems.insert(*i);
|
|
|
|
}
|
|
|
|
}
|
2003-12-21 23:58:56 +00:00
|
|
|
}
|
2005-02-14 16:16:02 +00:00
|
|
|
|
|
|
|
/* Check that all selectors have been used. */
|
|
|
|
for (DrvNames::iterator i = selectors.begin();
|
|
|
|
i != selectors.end(); ++i)
|
|
|
|
if (i->hits == 0)
|
|
|
|
throw Error(format("selector `%1%' matches no derivations")
|
|
|
|
% i->fullName);
|
|
|
|
|
|
|
|
break;
|
2003-11-24 11:01:19 +00:00
|
|
|
}
|
2005-02-14 16:16:02 +00:00
|
|
|
|
2005-02-14 17:07:43 +00: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-14 16:16:02 +00:00
|
|
|
case srcNixExprs:
|
2005-02-14 17:07:43 +00:00
|
|
|
|
|
|
|
Expr e1 = parseExprFromFile(state,
|
|
|
|
absPath(instSource.nixExprPath));
|
|
|
|
|
|
|
|
for (Strings::const_iterator i = args.begin();
|
|
|
|
i != args.end(); ++i)
|
|
|
|
{
|
|
|
|
Expr e2 = parseExprFromString(state, *i, absPath("."));
|
|
|
|
Expr call = makeCall(e2, e1);
|
|
|
|
parseDerivations(state, call, elems);
|
|
|
|
}
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case srcStorePaths:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case srcProfile:
|
|
|
|
break;
|
2003-11-20 13:48:48 +00:00
|
|
|
}
|
2005-02-14 16:16:02 +00:00
|
|
|
}
|
2003-11-20 13:48:48 +00:00
|
|
|
|
2004-06-28 14:40:26 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static void installDerivations(Globals & globals,
|
|
|
|
const Strings & args, const Path & profile)
|
|
|
|
{
|
|
|
|
debug(format("installing derivations"));
|
|
|
|
|
|
|
|
/* Get the set of user environment elements to be installed. */
|
|
|
|
UserEnvElems newElems;
|
|
|
|
queryInstSources(globals.state, globals.instSource, args, newElems);
|
|
|
|
|
|
|
|
StringSet newNames;
|
|
|
|
for (UserEnvElems::iterator i = newElems.begin(); i != newElems.end(); ++i) {
|
|
|
|
printMsg(lvlInfo,
|
|
|
|
format("installing `%1%'") % i->second.name);
|
|
|
|
newNames.insert(DrvName(i->second.name).name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add in the already installed derivations, unless they have the
|
|
|
|
same name as a to-be-installed element. */
|
|
|
|
UserEnvElems installedElems;
|
|
|
|
queryInstalled(globals.state, installedElems, profile);
|
|
|
|
|
|
|
|
for (UserEnvElems::iterator i = installedElems.begin();
|
|
|
|
i != installedElems.end(); ++i)
|
2004-06-28 14:40:26 +00:00
|
|
|
{
|
|
|
|
DrvName drvName(i->second.name);
|
2005-02-14 13:07:09 +00:00
|
|
|
if (!globals.preserveInstalled &&
|
2005-02-14 16:16:02 +00:00
|
|
|
newNames.find(drvName.name) != newNames.end())
|
2004-06-28 14:40:26 +00:00
|
|
|
printMsg(lvlInfo,
|
|
|
|
format("uninstalling `%1%'") % i->second.name);
|
|
|
|
else
|
2005-02-14 16:16:02 +00:00
|
|
|
newElems.insert(*i);
|
2004-06-28 14:40:26 +00:00
|
|
|
}
|
2003-11-20 13:48:48 +00:00
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 11:59:39 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
createUserEnv(globals.state, newElems,
|
2005-02-14 13:07:09 +00:00
|
|
|
profile, globals.keepDerivations);
|
2003-11-20 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
static void opInstall(Globals & globals,
|
2003-11-19 17:27:16 +00:00
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2003-12-21 22:34:41 +00:00
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flags `%1%'") % opFlags.front());
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
installDerivations(globals, opArgs, globals.profile);
|
2003-12-21 23:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-09 11:59:39 +00:00
|
|
|
typedef enum { utLt, utLeq, utAlways } UpgradeType;
|
|
|
|
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
static void upgradeDerivations(Globals & globals,
|
2005-02-14 16:16:02 +00:00
|
|
|
const Strings & args, const Path & profile,
|
2005-02-14 13:07:09 +00:00
|
|
|
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 16:04:00 +00:00
|
|
|
{
|
2005-02-11 16:56:45 +00:00
|
|
|
debug(format("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 16:04:00 +00: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. */
|
|
|
|
|
|
|
|
/* Load the currently installed derivations. */
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems installedElems;
|
|
|
|
queryInstalled(globals.state, installedElems, 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 16:04:00 +00:00
|
|
|
|
|
|
|
/* Fetch all derivations from the input file. */
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems availElems;
|
|
|
|
queryInstSources(globals.state, globals.instSource, args, availElems);
|
* 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 16:04:00 +00:00
|
|
|
|
|
|
|
/* Go through all installed derivations. */
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems newElems;
|
|
|
|
for (UserEnvElems::iterator i = installedElems.begin();
|
|
|
|
i != installedElems.end(); ++i)
|
* 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 16:04:00 +00:00
|
|
|
{
|
|
|
|
DrvName drvName(i->second.name);
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
/* Find the derivation in the input Nix expression with the
|
|
|
|
same name and satisfying the version constraints specified
|
|
|
|
by upgradeType. If there are multiple matches, take the
|
|
|
|
one with highest version. */
|
|
|
|
UserEnvElems::iterator bestElem = availElems.end();
|
2004-02-09 11:59:39 +00:00
|
|
|
DrvName bestName;
|
2005-02-14 16:16:02 +00:00
|
|
|
for (UserEnvElems::iterator j = availElems.begin();
|
|
|
|
j != availElems.end(); ++j)
|
2004-02-09 11:59:39 +00:00
|
|
|
{
|
|
|
|
DrvName newName(j->second.name);
|
|
|
|
if (newName.name == drvName.name) {
|
|
|
|
int d = compareVersions(drvName.version, newName.version);
|
|
|
|
if (upgradeType == utLt && d < 0 ||
|
|
|
|
upgradeType == utLeq && d <= 0 ||
|
|
|
|
upgradeType == utAlways)
|
|
|
|
{
|
2005-02-14 16:16:02 +00:00
|
|
|
if ((bestElem == availElems.end() ||
|
2004-06-28 13:37:05 +00:00
|
|
|
compareVersions(
|
|
|
|
bestName.version, newName.version) < 0))
|
2004-02-09 11:59:39 +00:00
|
|
|
{
|
2005-02-14 16:16:02 +00:00
|
|
|
bestElem = j;
|
2004-02-09 11:59:39 +00:00
|
|
|
bestName = newName;
|
|
|
|
}
|
|
|
|
}
|
2003-12-23 22:13:36 +00: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 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
if (bestElem != availElems.end() &&
|
|
|
|
i->second.outPath != bestElem->second.outPath)
|
2004-02-09 11:59:39 +00: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 16:04:00 +00:00
|
|
|
printMsg(lvlInfo,
|
|
|
|
format("upgrading `%1%' to `%2%'")
|
2005-02-14 16:16:02 +00:00
|
|
|
% i->second.name % bestElem->second.name);
|
|
|
|
newElems.insert(*bestElem);
|
|
|
|
} else newElems.insert(*i);
|
* 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 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 11:59:39 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
createUserEnv(globals.state, newElems,
|
2005-02-14 13:07:09 +00:00
|
|
|
profile, globals.keepDerivations);
|
* 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 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-21 23:58:56 +00:00
|
|
|
static void opUpgrade(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2004-02-09 11:59:39 +00:00
|
|
|
UpgradeType upgradeType = utLt;
|
|
|
|
for (Strings::iterator i = opFlags.begin();
|
|
|
|
i != opFlags.end(); ++i)
|
|
|
|
if (*i == "--lt") upgradeType = utLt;
|
|
|
|
else if (*i == "--leq") upgradeType = utLeq;
|
|
|
|
else if (*i == "--always") upgradeType = utAlways;
|
|
|
|
else throw UsageError(format("unknown flag `%1%'") % *i);
|
2003-12-21 23:58:56 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
upgradeDerivations(globals, opArgs, globals.profile, upgradeType);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
static void uninstallDerivations(Globals & globals, DrvNames & selectors,
|
|
|
|
Path & profile)
|
2003-11-20 13:48:48 +00:00
|
|
|
{
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems installedElems;
|
|
|
|
queryInstalled(globals.state, installedElems, profile);
|
2003-11-20 13:48:48 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
for (UserEnvElems::iterator i = installedElems.begin();
|
|
|
|
i != installedElems.end(); ++i)
|
2003-11-20 13:48:48 +00:00
|
|
|
{
|
2003-12-21 23:58:56 +00:00
|
|
|
DrvName drvName(i->second.name);
|
|
|
|
for (DrvNames::iterator j = selectors.begin();
|
|
|
|
j != selectors.end(); ++j)
|
* 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 16:04:00 +00:00
|
|
|
if (j->matches(drvName)) {
|
|
|
|
printMsg(lvlInfo,
|
|
|
|
format("uninstalling `%1%'") % i->second.name);
|
2005-02-14 16:16:02 +00:00
|
|
|
installedElems.erase(i);
|
* 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 16:04:00 +00:00
|
|
|
}
|
2003-11-20 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
if (globals.dryRun) return;
|
2004-02-09 11:59:39 +00:00
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
createUserEnv(globals.state, installedElems,
|
2005-02-14 13:07:09 +00:00
|
|
|
profile, globals.keepDerivations);
|
2003-11-20 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
static void opUninstall(Globals & globals,
|
2003-11-20 13:48:48 +00:00
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2003-12-21 22:34:41 +00:00
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flags `%1%'") % opFlags.front());
|
|
|
|
|
2003-12-21 23:58:56 +00:00
|
|
|
DrvNames drvNames = drvNamesFromArgs(opArgs);
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
uninstallDerivations(globals, drvNames,
|
|
|
|
globals.profile);
|
2003-11-20 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-14 15:09:55 +00:00
|
|
|
static bool cmpChars(char a, char b)
|
|
|
|
{
|
|
|
|
return toupper(a) < toupper(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static bool cmpElemByName(const UserEnvElem & a, const UserEnvElem & b)
|
2004-02-02 10:51:54 +00:00
|
|
|
{
|
2004-10-14 15:09:55 +00:00
|
|
|
return lexicographical_compare(
|
|
|
|
a.name.begin(), a.name.end(),
|
|
|
|
b.name.begin(), b.name.end(), cmpChars);
|
2004-02-02 10:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-01 13:35:10 +00:00
|
|
|
typedef list<Strings> Table;
|
|
|
|
|
|
|
|
|
|
|
|
void printTable(Table & table)
|
|
|
|
{
|
2004-10-29 11:22:49 +00:00
|
|
|
unsigned int nrColumns = table.size() > 0 ? table.front().size() : 0;
|
2004-07-01 13:35:10 +00:00
|
|
|
|
2004-10-29 11:22:49 +00:00
|
|
|
vector<unsigned int> widths;
|
2004-07-01 13:35:10 +00:00
|
|
|
widths.resize(nrColumns);
|
|
|
|
|
|
|
|
for (Table::iterator i = table.begin(); i != table.end(); ++i) {
|
|
|
|
assert(i->size() == nrColumns);
|
2004-10-29 11:22:49 +00:00
|
|
|
Strings::iterator j;
|
|
|
|
unsigned int column;
|
2004-07-01 13:35:10 +00:00
|
|
|
for (j = i->begin(), column = 0; j != i->end(); ++j, ++column)
|
|
|
|
if (j->size() > widths[column]) widths[column] = j->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Table::iterator i = table.begin(); i != table.end(); ++i) {
|
2004-10-29 11:22:49 +00:00
|
|
|
Strings::iterator j;
|
|
|
|
unsigned int column;
|
2004-07-01 13:35:10 +00:00
|
|
|
for (j = i->begin(), column = 0; j != i->end(); ++j, ++column)
|
|
|
|
{
|
|
|
|
cout << *j;
|
|
|
|
if (column < nrColumns - 1)
|
|
|
|
cout << string(widths[column] - j->size() + 2, ' ');
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
static void opQuery(Globals & globals,
|
2003-11-19 17:27:16 +00:00
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2004-07-01 13:13:37 +00:00
|
|
|
bool printStatus = false;
|
|
|
|
bool printName = true;
|
|
|
|
bool printSystem = false;
|
|
|
|
bool printDrvPath = false;
|
2005-02-11 16:56:45 +00:00
|
|
|
bool printOutPath = false;
|
2004-07-01 13:13:37 +00:00
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
enum { sInstalled, sAvailable } source = sInstalled;
|
|
|
|
|
2004-10-27 10:24:44 +00:00
|
|
|
readOnlyMode = true; /* makes evaluation a bit faster */
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
for (Strings::iterator i = opFlags.begin();
|
|
|
|
i != opFlags.end(); ++i)
|
2004-07-01 13:13:37 +00:00
|
|
|
if (*i == "--status" || *i == "-s") printStatus = true;
|
|
|
|
else if (*i == "--no-name") printName = false;
|
|
|
|
else if (*i == "--system") printSystem = true;
|
2005-02-11 16:56:45 +00:00
|
|
|
else if (*i == "--drv-path") printDrvPath = true;
|
|
|
|
else if (*i == "--out-path") printOutPath = true;
|
2003-11-19 17:27:16 +00:00
|
|
|
else if (*i == "--installed") source = sInstalled;
|
2004-01-05 16:26:43 +00:00
|
|
|
else if (*i == "--available" || *i == "-a") source = sAvailable;
|
2003-11-19 17:27:16 +00:00
|
|
|
else throw UsageError(format("unknown flag `%1%'") % *i);
|
|
|
|
|
|
|
|
/* Obtain derivation information from the specified source. */
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems elems;
|
2003-11-19 17:27:16 +00:00
|
|
|
|
|
|
|
switch (source) {
|
|
|
|
|
|
|
|
case sInstalled:
|
2005-02-14 16:16:02 +00:00
|
|
|
queryInstalled(globals.state, elems, globals.profile);
|
2003-11-19 17:27:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case sAvailable: {
|
2005-02-11 16:56:45 +00:00
|
|
|
loadDerivations(globals.state, globals.instSource.nixExprPath,
|
2005-02-14 16:16:02 +00:00
|
|
|
globals.instSource.systemFilter, elems);
|
2003-11-19 17:27:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
|
2003-11-19 21:32:03 +00:00
|
|
|
if (opArgs.size() != 0) throw UsageError("no arguments expected");
|
2004-02-02 10:51:54 +00:00
|
|
|
|
|
|
|
/* Sort them by name. */
|
2005-02-14 16:16:02 +00:00
|
|
|
vector<UserEnvElem> elems2;
|
|
|
|
for (UserEnvElems::iterator i = elems.begin(); i != elems.end(); ++i)
|
|
|
|
elems2.push_back(i->second);
|
|
|
|
sort(elems2.begin(), elems2.end(), cmpElemByName);
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2004-07-01 13:13:37 +00:00
|
|
|
/* We only need to know the installed paths when we are querying
|
|
|
|
the status of the derivation. */
|
2005-02-14 16:16:02 +00:00
|
|
|
UserEnvElems installed; /* installed paths */
|
2004-07-01 13:13:37 +00:00
|
|
|
|
2005-02-11 16:56:45 +00:00
|
|
|
if (printStatus)
|
2004-07-01 13:13:37 +00:00
|
|
|
queryInstalled(globals.state, installed, globals.profile);
|
2004-01-15 14:43:00 +00:00
|
|
|
|
2004-07-01 13:13:37 +00:00
|
|
|
/* Print the desired columns. */
|
2004-07-01 13:35:10 +00:00
|
|
|
Table table;
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
for (vector<UserEnvElem>::iterator i = elems2.begin();
|
|
|
|
i != elems2.end(); ++i)
|
|
|
|
{
|
2004-07-01 13:13:37 +00:00
|
|
|
Strings columns;
|
|
|
|
|
|
|
|
if (printStatus) {
|
2005-01-27 15:21:29 +00:00
|
|
|
Substitutes subs = querySubstitutes(noTxn, i->drvPath);
|
2004-07-01 13:13:37 +00:00
|
|
|
columns.push_back(
|
2005-02-11 16:56:45 +00:00
|
|
|
(string) (installed.find(i->outPath)
|
|
|
|
!= installed.end() ? "I" : "-")
|
2004-07-01 13:13:37 +00:00
|
|
|
+ (isValidPath(i->outPath) ? "P" : "-")
|
|
|
|
+ (subs.size() > 0 ? "S" : "-"));
|
2003-11-19 21:32:03 +00:00
|
|
|
}
|
2004-07-01 13:13:37 +00:00
|
|
|
|
|
|
|
if (printName) columns.push_back(i->name);
|
|
|
|
|
|
|
|
if (printSystem) columns.push_back(i->system);
|
|
|
|
|
2005-02-11 16:56:45 +00:00
|
|
|
if (printDrvPath) columns.push_back(i->drvPath == "" ? "-" : i->drvPath);
|
|
|
|
|
|
|
|
if (printOutPath) columns.push_back(i->outPath);
|
2004-07-01 13:35:10 +00:00
|
|
|
|
|
|
|
table.push_back(columns);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
2004-07-01 13:35:10 +00:00
|
|
|
|
|
|
|
printTable(table);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-05 11:18:59 +00:00
|
|
|
static void opSwitchProfile(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
2004-02-06 16:03:27 +00:00
|
|
|
throw UsageError(format("unknown flag `%1%'") % opFlags.front());
|
2004-02-06 10:59:06 +00:00
|
|
|
if (opArgs.size() != 1)
|
2004-02-06 16:03:27 +00:00
|
|
|
throw UsageError(format("exactly one argument expected"));
|
2004-01-05 11:18:59 +00:00
|
|
|
|
2004-02-06 10:59:06 +00:00
|
|
|
Path profile = opArgs.front();
|
|
|
|
Path profileLink = getHomeDir() + "/.nix-profile";
|
2004-01-05 11:18:59 +00:00
|
|
|
|
2004-09-09 21:12:53 +00:00
|
|
|
SwitchToOriginalUser sw;
|
2004-02-06 10:30:20 +00:00
|
|
|
switchLink(profileLink, profile);
|
2004-01-05 16:26:43 +00:00
|
|
|
}
|
2004-01-05 11:18:59 +00:00
|
|
|
|
2004-01-05 16:26:43 +00:00
|
|
|
|
2004-02-08 14:07:43 +00:00
|
|
|
static const int prevGen = -2;
|
|
|
|
|
|
|
|
|
|
|
|
static void switchGeneration(Globals & globals, int dstGen)
|
|
|
|
{
|
|
|
|
int curGen;
|
|
|
|
Generations gens = findGenerations(globals.profile, curGen);
|
|
|
|
|
|
|
|
Generation dst;
|
|
|
|
for (Generations::iterator i = gens.begin(); i != gens.end(); ++i)
|
|
|
|
if ((dstGen == prevGen && i->number < curGen) ||
|
|
|
|
(dstGen >= 0 && i->number == dstGen))
|
|
|
|
dst = *i;
|
|
|
|
|
|
|
|
if (!dst)
|
|
|
|
if (dstGen == prevGen)
|
|
|
|
throw Error(format("no generation older than the current (%1%) exists")
|
|
|
|
% curGen);
|
|
|
|
else
|
|
|
|
throw Error(format("generation %1% does not exist") % dstGen);
|
|
|
|
|
2004-02-10 13:42:58 +00:00
|
|
|
printMsg(lvlInfo, format("switching from generation %1% to %2%")
|
|
|
|
% curGen % dst.number);
|
|
|
|
|
|
|
|
if (globals.dryRun) return;
|
|
|
|
|
2004-02-08 14:07:43 +00:00
|
|
|
switchLink(globals.profile, dst.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void opSwitchGeneration(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flag `%1%'") % opFlags.front());
|
|
|
|
if (opArgs.size() != 1)
|
|
|
|
throw UsageError(format("exactly one argument expected"));
|
|
|
|
|
|
|
|
int dstGen;
|
2004-09-10 13:32:08 +00:00
|
|
|
if (!string2Int(opArgs.front(), dstGen))
|
2004-02-08 14:07:43 +00:00
|
|
|
throw UsageError(format("expected a generation number"));
|
|
|
|
|
|
|
|
switchGeneration(globals, dstGen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void opRollback(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flag `%1%'") % opFlags.front());
|
|
|
|
if (opArgs.size() != 0)
|
|
|
|
throw UsageError(format("no arguments expected"));
|
|
|
|
|
|
|
|
switchGeneration(globals, prevGen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
static void opListGenerations(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flag `%1%'") % opFlags.front());
|
|
|
|
if (opArgs.size() != 0)
|
|
|
|
throw UsageError(format("no arguments expected"));
|
|
|
|
|
2004-02-06 16:16:55 +00:00
|
|
|
int curGen;
|
|
|
|
Generations gens = findGenerations(globals.profile, curGen);
|
2004-02-06 16:03:27 +00:00
|
|
|
|
|
|
|
for (Generations::iterator i = gens.begin(); i != gens.end(); ++i) {
|
|
|
|
tm t;
|
|
|
|
if (!localtime_r(&i->creationTime, &t)) throw Error("cannot convert time");
|
2004-02-06 16:16:55 +00:00
|
|
|
cout << format("%|4| %|4|-%|02|-%|02| %|02|:%|02|:%|02| %||\n")
|
2004-02-06 16:03:27 +00:00
|
|
|
% i->number
|
|
|
|
% (t.tm_year + 1900) % (t.tm_mon + 1) % t.tm_mday
|
2004-02-06 16:16:55 +00:00
|
|
|
% t.tm_hour % t.tm_min % t.tm_sec
|
|
|
|
% (i->number == curGen ? "(current)" : "");
|
2004-02-06 16:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 13:32:08 +00:00
|
|
|
static void deleteGeneration2(const Path & profile, unsigned int gen)
|
|
|
|
{
|
|
|
|
printMsg(lvlInfo, format("removing generation %1%") % gen);
|
|
|
|
deleteGeneration(profile, gen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void opDeleteGenerations(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flag `%1%'") % opFlags.front());
|
|
|
|
|
|
|
|
int curGen;
|
|
|
|
Generations gens = findGenerations(globals.profile, curGen);
|
|
|
|
|
|
|
|
for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); ++i) {
|
|
|
|
|
|
|
|
if (*i == "old") {
|
|
|
|
for (Generations::iterator j = gens.begin(); j != gens.end(); ++j)
|
|
|
|
if (j->number != curGen)
|
|
|
|
deleteGeneration2(globals.profile, j->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
int n;
|
|
|
|
if (!string2Int(*i, n) || n < 0)
|
|
|
|
throw UsageError(format("invalid generation specifier `%1%'") % *i);
|
|
|
|
bool found = false;
|
|
|
|
for (Generations::iterator j = gens.begin(); j != gens.end(); ++j) {
|
|
|
|
if (j->number == n) {
|
|
|
|
deleteGeneration2(globals.profile, j->number);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
printMsg(lvlError, format("generation %1% does not exist") % n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-05 16:26:43 +00:00
|
|
|
static void opDefaultExpr(Globals & globals,
|
|
|
|
Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (opFlags.size() > 0)
|
|
|
|
throw UsageError(format("unknown flags `%1%'") % opFlags.front());
|
|
|
|
if (opArgs.size() != 1)
|
2004-02-06 16:03:27 +00:00
|
|
|
throw UsageError(format("exactly one argument expected"));
|
2004-01-05 16:26:43 +00:00
|
|
|
|
2004-01-20 20:36:58 +00:00
|
|
|
Path defNixExpr = absPath(opArgs.front());
|
2004-01-05 16:26:43 +00:00
|
|
|
Path defNixExprLink = getDefNixExprPath();
|
2004-01-05 11:18:59 +00:00
|
|
|
|
2004-09-09 21:12:53 +00:00
|
|
|
SwitchToOriginalUser sw;
|
2004-01-05 16:26:43 +00:00
|
|
|
switchLink(defNixExprLink, defNixExpr);
|
2004-01-05 11:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
static string needArg(Strings::iterator & i,
|
|
|
|
const Strings & args, const string & arg)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i == args.end()) throw UsageError(
|
|
|
|
format("`%1%' requires an argument") % arg);
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
void run(Strings args)
|
|
|
|
{
|
|
|
|
Strings opFlags, opArgs;
|
|
|
|
Operation op = 0;
|
2003-12-21 22:34:41 +00:00
|
|
|
|
|
|
|
Globals globals;
|
2005-02-14 13:07:09 +00:00
|
|
|
|
2005-02-11 16:56:45 +00:00
|
|
|
globals.instSource.type = srcUnknown;
|
|
|
|
globals.instSource.nixExprPath = getDefNixExprPath();
|
|
|
|
globals.instSource.systemFilter = thisSystem;
|
|
|
|
|
2004-02-09 11:59:39 +00:00
|
|
|
globals.dryRun = false;
|
2004-06-28 14:40:26 +00:00
|
|
|
globals.preserveInstalled = false;
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
globals.keepDerivations =
|
|
|
|
queryBoolSetting("env-keep-derivations", false);
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ++i) {
|
|
|
|
string arg = *i;
|
|
|
|
|
|
|
|
Operation oldOp = op;
|
|
|
|
|
|
|
|
if (arg == "--install" || arg == "-i")
|
|
|
|
op = opInstall;
|
2005-02-14 16:16:02 +00:00
|
|
|
else 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);
|
|
|
|
}
|
2003-12-21 22:34:41 +00:00
|
|
|
else if (arg == "--uninstall" || arg == "-e")
|
2003-11-20 13:48:48 +00:00
|
|
|
op = opUninstall;
|
2003-12-21 23:58:56 +00:00
|
|
|
else if (arg == "--upgrade" || arg == "-u")
|
|
|
|
op = opUpgrade;
|
2003-11-20 13:48:48 +00:00
|
|
|
else if (arg == "--query" || arg == "-q")
|
2003-11-19 17:27:16 +00:00
|
|
|
op = opQuery;
|
2004-01-05 16:26:43 +00:00
|
|
|
else if (arg == "--import" || arg == "-I") /* !!! bad name */
|
|
|
|
op = opDefaultExpr;
|
2004-02-06 10:30:20 +00:00
|
|
|
else if (arg == "--profile" || arg == "-p") {
|
2005-02-14 16:16:02 +00:00
|
|
|
globals.profile = absPath(needArg(i, args, arg));
|
2003-12-21 22:34:41 +00:00
|
|
|
}
|
2004-01-05 16:26:43 +00:00
|
|
|
else if (arg == "--file" || arg == "-f") {
|
2005-02-14 16:16:02 +00:00
|
|
|
globals.instSource.nixExprPath = absPath(needArg(i, args, arg));
|
2004-01-05 16:26:43 +00:00
|
|
|
}
|
2004-02-06 10:30:20 +00:00
|
|
|
else if (arg == "--switch-profile" || arg == "-S")
|
2004-01-05 11:18:59 +00:00
|
|
|
op = opSwitchProfile;
|
2004-02-08 14:07:43 +00:00
|
|
|
else if (arg == "--switch-generation" || arg == "-G")
|
|
|
|
op = opSwitchGeneration;
|
|
|
|
else if (arg == "--rollback")
|
|
|
|
op = opRollback;
|
2004-02-06 16:03:27 +00:00
|
|
|
else if (arg == "--list-generations")
|
|
|
|
op = opListGenerations;
|
2004-09-10 13:32:08 +00:00
|
|
|
else if (arg == "--delete-generations")
|
|
|
|
op = opDeleteGenerations;
|
2004-02-09 11:59:39 +00:00
|
|
|
else if (arg == "--dry-run") {
|
|
|
|
printMsg(lvlInfo, "(dry run; not doing anything)");
|
|
|
|
globals.dryRun = true;
|
|
|
|
}
|
2004-06-28 14:40:26 +00:00
|
|
|
else if (arg == "--preserve-installed" || arg == "-P")
|
|
|
|
globals.preserveInstalled = true;
|
2004-07-01 13:56:56 +00:00
|
|
|
else if (arg == "--system-filter") {
|
2005-02-14 16:16:02 +00:00
|
|
|
globals.instSource.systemFilter = needArg(i, args, arg);
|
2004-07-01 13:56:56 +00:00
|
|
|
}
|
2003-12-02 10:21:40 +00:00
|
|
|
else if (arg[0] == '-')
|
|
|
|
opFlags.push_back(arg);
|
2003-11-19 17:27:16 +00:00
|
|
|
else
|
|
|
|
opArgs.push_back(arg);
|
|
|
|
|
|
|
|
if (oldOp && oldOp != op)
|
|
|
|
throw UsageError("only one operation may be specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!op) throw UsageError("no operation specified");
|
|
|
|
|
2004-02-06 10:59:06 +00:00
|
|
|
if (globals.profile == "") {
|
2004-09-09 21:12:53 +00:00
|
|
|
SwitchToOriginalUser sw;
|
2004-02-06 10:59:06 +00:00
|
|
|
Path profileLink = getHomeDir() + "/.nix-profile";
|
|
|
|
globals.profile = pathExists(profileLink)
|
|
|
|
? absPath(readLink(profileLink), dirOf(profileLink))
|
|
|
|
: canonPath(nixStateDir + "/profiles/default");
|
|
|
|
}
|
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
openDB();
|
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
op(globals, opFlags, opArgs);
|
2003-11-19 17:27:16 +00:00
|
|
|
|
2003-12-21 22:34:41 +00:00
|
|
|
printEvalStats(globals.state);
|
2003-11-19 17:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string programId = "nix-env";
|