nix dev-shell: Use 'provides.devShell' by default

Thus

  $ nix dev-shell

will now build the 'provides.devShell' attribute from the flake in the
current directory. If it doesn't exist, it falls back to
'provides.defaultPackage'.
This commit is contained in:
Eelco Dolstra 2019-05-02 21:10:13 +02:00
parent 7dcf5b011a
commit 2919c496ea
5 changed files with 51 additions and 13 deletions

View file

@ -17,5 +17,10 @@
packages.nix = hydraJobs.build.x86_64-linux; packages.nix = hydraJobs.build.x86_64-linux;
defaultPackage = packages.nix; defaultPackage = packages.nix;
devShell = import ./shell.nix {
nixpkgs = deps.nixpkgs;
};
}; };
} }

View file

@ -1,6 +1,8 @@
{ useClang ? false }: { useClang ? false
, nixpkgs ? builtins.fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.03.tar.gz
}:
with import (builtins.fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-19.03.tar.gz) {}; with import nixpkgs { system = builtins.currentSystem or "x86_64-linux"; };
with import ./release-common.nix { inherit pkgs; }; with import ./release-common.nix { inherit pkgs; };

View file

@ -88,6 +88,11 @@ struct SourceExprCommand : virtual Args, StoreCommand, MixEvalArgs
std::shared_ptr<Installable> parseInstallable( std::shared_ptr<Installable> parseInstallable(
ref<Store> store, const std::string & installable); ref<Store> store, const std::string & installable);
virtual Strings getDefaultFlakeAttrPaths()
{
return {"defaultPackage"};
}
private: private:
std::shared_ptr<EvalState> evalState; std::shared_ptr<EvalState> evalState;

View file

@ -142,13 +142,18 @@ struct InstallableAttrPath : InstallableValue
struct InstallableFlake : InstallableValue struct InstallableFlake : InstallableValue
{ {
FlakeRef flakeRef; FlakeRef flakeRef;
std::string attrPath; Strings attrPaths;
bool searchPackages = false;
InstallableFlake(SourceExprCommand & cmd, FlakeRef && flakeRef, const std::string & attrPath) InstallableFlake(SourceExprCommand & cmd, FlakeRef && flakeRef, Strings attrPaths)
: InstallableValue(cmd), flakeRef(flakeRef), attrPath(attrPath) : InstallableValue(cmd), flakeRef(flakeRef), attrPaths(std::move(attrPaths))
{ } { }
std::string what() override { return flakeRef.to_string() + ":" + attrPath; } InstallableFlake(SourceExprCommand & cmd, FlakeRef && flakeRef, std::string attrPath)
: InstallableValue(cmd), flakeRef(flakeRef), attrPaths{attrPath}, searchPackages(true)
{ }
std::string what() override { return flakeRef.to_string() + ":" + *attrPaths.begin(); }
Value * toValue(EvalState & state) override Value * toValue(EvalState & state) override
{ {
@ -166,18 +171,31 @@ struct InstallableFlake : InstallableValue
auto emptyArgs = state.allocBindings(0); auto emptyArgs = state.allocBindings(0);
if (auto aPackages = *vProvides->attrs->get(state.symbols.create("packages"))) { // As a convenience, look for the attribute in
// 'provides.packages'.
if (searchPackages) {
if (auto aPackages = *vProvides->attrs->get(state.symbols.create("packages"))) {
try {
auto * v = findAlongAttrPath(state, *attrPaths.begin(), *emptyArgs, *aPackages->value);
state.forceValue(*v);
return v;
} catch (AttrPathNotFound & e) {
}
}
}
// Otherwise, look for it in 'provides'.
for (auto & attrPath : attrPaths) {
try { try {
auto * v = findAlongAttrPath(state, attrPath, *emptyArgs, *aPackages->value); auto * v = findAlongAttrPath(state, attrPath, *emptyArgs, *vProvides);
state.forceValue(*v); state.forceValue(*v);
return v; return v;
} catch (AttrPathNotFound & e) { } catch (AttrPathNotFound & e) {
} }
} }
auto * v = findAlongAttrPath(state, attrPath, *emptyArgs, *vProvides); throw Error("flake '%s' does not provide attribute %s",
state.forceValue(*v); flakeRef, concatStringsSep(", ", quoteStrings(attrPaths)));
return v;
} }
}; };
@ -216,7 +234,8 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
else if (hasPrefix(s, "nixpkgs.")) { else if (hasPrefix(s, "nixpkgs.")) {
bool static warned; bool static warned;
warnOnce(warned, "the syntax 'nixpkgs.<attr>' is deprecated; use 'nixpkgs:<attr>' instead"); warnOnce(warned, "the syntax 'nixpkgs.<attr>' is deprecated; use 'nixpkgs:<attr>' instead");
result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef("nixpkgs"), std::string(s, 8))); result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef("nixpkgs"),
Strings{"packages." + std::string(s, 8)}));
} }
else if ((colon = s.rfind(':')) != std::string::npos) { else if ((colon = s.rfind(':')) != std::string::npos) {
@ -233,7 +252,8 @@ std::vector<std::shared_ptr<Installable>> SourceExprCommand::parseInstallables(
if (storePath != "") if (storePath != "")
result.push_back(std::make_shared<InstallableStorePath>(storePath)); result.push_back(std::make_shared<InstallableStorePath>(storePath));
else else
result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef(s, true), "defaultPackage")); result.push_back(std::make_shared<InstallableFlake>(*this, FlakeRef(s, true),
getDefaultFlakeAttrPaths()));
} }
else else

View file

@ -125,6 +125,7 @@ struct Common : InstallableCommand
"BASHOPTS", "BASHOPTS",
"EUID", "EUID",
"NIX_BUILD_TOP", "NIX_BUILD_TOP",
"NIX_ENFORCE_PURITY",
"PPID", "PPID",
"PWD", "PWD",
"SHELLOPTS", "SHELLOPTS",
@ -156,6 +157,11 @@ struct Common : InstallableCommand
for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"}) for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"})
out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i); out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i);
} }
Strings getDefaultFlakeAttrPaths() override
{
return {"devShell", "defaultPackage"};
}
}; };
std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix") std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix")