diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 56541fa57..047906786 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -397,4 +397,11 @@ void createOutLinks(const std::filesystem::path & outLink, const BuiltPaths & bu } } +void MixOutLinkBase::createOutLinksMaybe(const std::vector & buildables, ref & store) +{ + if (outLink != "") + if (auto store2 = store.dynamic_pointer_cast()) + createOutLinks(outLink, toBuiltPaths(buildables), *store2); +} + } diff --git a/src/libcmd/include/nix/cmd/command.hh b/src/libcmd/include/nix/cmd/command.hh index 6b6418f51..92377be6b 100644 --- a/src/libcmd/include/nix/cmd/command.hh +++ b/src/libcmd/include/nix/cmd/command.hh @@ -374,4 +374,41 @@ void printClosureDiff( */ void createOutLinks(const std::filesystem::path & outLink, const BuiltPaths & buildables, LocalFSStore & store); -} +/** `outLink` parameter, `createOutLinksMaybe` method. See `MixOutLinkByDefault`. */ +struct MixOutLinkBase : virtual Args +{ + /** Prefix for any output symlinks. Empty means do not write an output symlink. */ + Path outLink; + + MixOutLinkBase(const std::string & defaultOutLink) + : outLink(defaultOutLink) + { + } + + void createOutLinksMaybe(const std::vector & buildables, ref & store); +}; + +/** `--out-link`, `--no-link`, `createOutLinksMaybe` */ +struct MixOutLinkByDefault : MixOutLinkBase, virtual Args +{ + MixOutLinkByDefault() + : MixOutLinkBase("result") + { + addFlag({ + .longName = "out-link", + .shortName = 'o', + .description = "Use *path* as prefix for the symlinks to the build results. It defaults to `result`.", + .labels = {"path"}, + .handler = {&outLink}, + .completer = completePath, + }); + + addFlag({ + .longName = "no-link", + .description = "Do not create symlinks to the build results.", + .handler = {&outLink, Path("")}, + }); + } +}; + +} // namespace nix diff --git a/src/nix/build.cc b/src/nix/build.cc index daf50082e..bd0c8862b 100644 --- a/src/nix/build.cc +++ b/src/nix/build.cc @@ -41,29 +41,13 @@ static nlohmann::json builtPathsWithResultToJSON(const std::vectorcout("%s", builtPathsWithResultToJSON(buildables, *store).dump()); - if (outLink != "") - if (auto store2 = store.dynamic_pointer_cast()) - createOutLinks(outLink, toBuiltPaths(buildables), *store2); + createOutLinksMaybe(buildables, store); if (printOutputPaths) { logger->stop();