Refactor a bit

This commit is contained in:
Eelco Dolstra 2019-07-12 16:16:27 +02:00
parent 7ba928116e
commit 731bc65ec0
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -104,7 +104,7 @@ Path getDerivationEnvironment(ref<Store> store, Derivation drv)
return shellOutPath; return shellOutPath;
} }
struct Common : InstallableCommand struct Common : InstallableCommand, MixProfile
{ {
/* /*
std::set<string> keepVars{ std::set<string> keepVars{
@ -173,9 +173,26 @@ struct Common : InstallableCommand
{ {
return {"devShell", "defaultPackage"}; return {"devShell", "defaultPackage"};
} }
BuildEnvironment getBuildEnvironment(ref<Store> store)
{
auto drvs = toDerivations(store, {installable});
if (drvs.size() != 1)
throw Error("'%s' needs to evaluate to a single derivation, but it evaluated to %d derivations",
installable->what(), drvs.size());
auto & drvPath = *drvs.begin();
auto shellOutPath = getDerivationEnvironment(store, store->derivationFromPath(drvPath));
updateProfile(shellOutPath);
return readEnvironment(shellOutPath);
}
}; };
struct CmdDevShell : Common, MixProfile struct CmdDevShell : Common
{ {
std::string description() override std::string description() override
{ {
@ -202,19 +219,7 @@ struct CmdDevShell : Common, MixProfile
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
auto drvs = toDerivations(store, {installable}); auto buildEnvironment = getBuildEnvironment(store);
if (drvs.size() != 1)
throw Error("'%s' needs to evaluate to a single derivation, but it evaluated to %d derivations",
installable->what(), drvs.size());
auto & drvPath = *drvs.begin();
auto shellOutPath = getDerivationEnvironment(store, store->derivationFromPath(drvPath));
updateProfile(shellOutPath);
auto buildEnvironment = readEnvironment(shellOutPath);
auto [rcFileFd, rcFilePath] = createTempFile("nix-shell"); auto [rcFileFd, rcFilePath] = createTempFile("nix-shell");
@ -259,17 +264,7 @@ struct CmdPrintDevEnv : Common
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
auto drvs = toDerivations(store, {installable}); auto buildEnvironment = getBuildEnvironment(store);
if (drvs.size() != 1)
throw Error("'%s' needs to evaluate to a single derivation, but it evaluated to %d derivations",
installable->what(), drvs.size());
auto & drvPath = *drvs.begin();
auto buildEnvironment = readEnvironment(
getDerivationEnvironment(store,
store->derivationFromPath(drvPath)));
stopProgressBar(); stopProgressBar();