aboutsummaryrefslogtreecommitdiff
path: root/src/nix/command.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-07-12 15:32:17 +0200
committerEelco Dolstra <edolstra@gmail.com>2019-07-12 15:32:17 +0200
commit990b5b2dcfaacd82caf4c92789a9c234b342c3b4 (patch)
treeb32563473d8a60172277a82369e915799ddbfd69 /src/nix/command.cc
parentb45628a172dd3a3ba863cd231f1446c7cd901cf7 (diff)
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').
Diffstat (limited to 'src/nix/command.cc')
-rw-r--r--src/nix/command.cc41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/nix/command.cc b/src/nix/command.cc
index 89fa0cba4..8191cb831 100644
--- a/src/nix/command.cc
+++ b/src/nix/command.cc
@@ -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);
+}
+
}