nix build: Add '--profile' flag

This replaces 'nix-env --set'. For example:

  $ nix build --profile /nix/var/nix/profiles/system \
      ~/Misc/eelco-configurations:nixosConfigurations.vyr.config.system.build.toplevel

updates the NixOS system profile from a flake.

This could have been a separate command (e.g. 'nix set-profile') but
1) '--profile' is pretty similar to '--out-link'; and 2) '--profile'
could be useful for other command (like 'nix dev-shell').
This commit is contained in:
Eelco Dolstra 2019-07-12 15:32:17 +02:00
parent b45628a172
commit 990b5b2dcf
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
3 changed files with 66 additions and 6 deletions

View file

@ -6,7 +6,7 @@
using namespace nix;
struct CmdBuild : MixDryRun, InstallablesCommand
struct CmdBuild : MixDryRun, MixProfile, InstallablesCommand
{
Path outLink = "result";
@ -41,6 +41,10 @@ struct CmdBuild : MixDryRun, InstallablesCommand
"To build the build.x86_64-linux attribute from release.nix:",
"nix build -f release.nix build.x86_64-linux"
},
Example{
"To make a profile point at GNU Hello:",
"nix build --profile /tmp/profile nixpkgs:hello"
},
};
}
@ -52,18 +56,19 @@ struct CmdBuild : MixDryRun, InstallablesCommand
evalState->addRegistryOverrides(registryOverrides);
if (dryRun) return;
for (size_t i = 0; i < buildables.size(); ++i) {
auto & b(buildables[i]);
if (outLink != "")
for (auto & output : b.outputs)
if (outLink != "") {
for (size_t i = 0; i < buildables.size(); ++i) {
for (auto & output : buildables[i].outputs)
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>()) {
std::string symlink = outLink;
if (i) symlink += fmt("-%d", i);
if (output.first != "out") symlink += fmt("-%s", output.first);
store2->addPermRoot(output.second, absPath(symlink), true);
}
}
}
updateProfile(buildables);
}
};

View file

@ -1,6 +1,7 @@
#include "command.hh"
#include "store-api.hh"
#include "derivations.hh"
#include "profiles.hh"
namespace nix {
@ -81,4 +82,44 @@ void StorePathCommand::run(ref<Store> store)
run(store, *storePaths.begin());
}
MixProfile::MixProfile()
{
mkFlag()
.longName("profile")
.description("profile to update")
.labels({"path"})
.dest(&profile);
}
void MixProfile::updateProfile(const Path & storePath)
{
if (!profile) return;
auto store = getStore().dynamic_pointer_cast<LocalFSStore>();
if (!store) throw Error("'--profile' is not supported for this Nix store");
switchLink(*profile,
createGeneration(
ref<LocalFSStore>(store),
*profile, storePath));
}
void MixProfile::updateProfile(const Buildables & buildables)
{
if (!profile) return;
std::optional<Path> result;
for (auto & buildable : buildables) {
for (auto & output : buildable.outputs) {
if (result)
throw Error("'--profile' requires that the arguments produce a single store path, but there are multiple");
result = output.second;
}
}
if (!result)
throw Error("'--profile' requires that the arguments produce a single store path, but there are none");
updateProfile(*result);
}
}

View file

@ -219,4 +219,18 @@ PathSet toDerivations(ref<Store> store,
std::vector<std::shared_ptr<Installable>> installables,
bool useDeriver = false);
struct MixProfile : virtual Args, virtual StoreCommand
{
std::optional<Path> profile;
MixProfile();
/* If 'profile' is set, make it point at 'storePath'. */
void updateProfile(const Path & storePath);
/* If 'profile' is set, make it point at the store path produced
by 'buildables'. */
void updateProfile(const Buildables & buildables);
};
}