aboutsummaryrefslogtreecommitdiff
path: root/src/libcmd
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-12-15 23:01:15 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-12-20 15:11:44 +0100
commit5c97b5a3988c7dd28e617734c2eba669ee0c1288 (patch)
tree52280cda6afc355347cfd861925f7bc551f2720b /src/libcmd
parentbda879170fbf8ff8c5397948ccc0e1695d23871a (diff)
InstallableFlake::toDerivedPaths(): Support paths and store paths
This makes 'nix build' work on paths (which will be copied to the store) and store paths (returned as is). E.g. the following flake output attributes can be built using 'nix build .#foo': foo = ./src; foo = self.outPath; foo = builtins.fetchTarball { ... }; foo = (builtins.fetchTree { .. }).outPath; foo = builtins.fetchTree { .. } + "/README.md"; foo = builtins.storePath /nix/store/...; Note that this is potentially risky, e.g. foo = /.; will cause Nix to try to copy the entire file system to the store. What doesn't work yet: foo = self; foo = builtins.fetchTree { .. }; because we don't handle attrsets with an outPath attribute in it yet, and foo = builtins.storePath /nix/store/.../README.md; since result symlinks have to point to a store path currently (rather than a file inside a store path). Fixes #7417.
Diffstat (limited to 'src/libcmd')
-rw-r--r--src/libcmd/installables.cc34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc
index 97bad5c45..f4486bc2f 100644
--- a/src/libcmd/installables.cc
+++ b/src/libcmd/installables.cc
@@ -610,8 +610,38 @@ DerivedPathsWithInfo InstallableFlake::toDerivedPaths()
auto attrPath = attr->getAttrPathStr();
- if (!attr->isDerivation())
- throw Error("flake output attribute '%s' is not a derivation", attrPath);
+ if (!attr->isDerivation()) {
+
+ // FIXME: use eval cache?
+ auto v = attr->forceValue();
+
+ if (v.type() == nPath) {
+ PathSet context;
+ auto storePath = state->copyPathToStore(context, Path(v.path));
+ return {{
+ .path = DerivedPath::Opaque {
+ .path = std::move(storePath),
+ }
+ }};
+ }
+
+ else if (v.type() == nString) {
+ PathSet context;
+ auto s = state->forceString(v, context);
+ auto storePath = state->store->maybeParseStorePath(s);
+ if (storePath && context.count(std::string(s))) {
+ return {{
+ .path = DerivedPath::Opaque {
+ .path = std::move(*storePath),
+ }
+ }};
+ } else
+ throw Error("flake output attribute '%s' evaluates to a string that does not denote a store path", attrPath);
+ }
+
+ else
+ throw Error("flake output attribute '%s' is not a derivation or path", attrPath);
+ }
auto drvPath = attr->forceDerivation();