Skip to content

Commit

Permalink
nix copy: Add --out-link
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Oct 8, 2024
1 parent 43ad8c5 commit 7f6d006
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
25 changes: 25 additions & 0 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,29 @@ void MixEnvironment::setEnviron()
}
}

void createOutLinks(
const std::filesystem::path & outLink,
const BuiltPaths & buildables,
LocalFSStore & store)
{
for (const auto & [_i, buildable] : enumerate(buildables)) {
auto i = _i;
std::visit(overloaded {
[&](const BuiltPath::Opaque & bo) {
auto symlink = outLink;
if (i) symlink += fmt("-%d", i);
store.addPermRoot(bo.path, absPath(symlink.string()));
},
[&](const BuiltPath::Built & bfd) {
for (auto & output : bfd.outputs) {
auto symlink = outLink;
if (i) symlink += fmt("-%d", i);
if (output.first != "out") symlink += fmt("-%s", output.first);
store.addPermRoot(output.second, absPath(symlink.string()));
}
},
}, buildable.raw());
}
}

}
10 changes: 10 additions & 0 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern char * * savedArgv;
class EvalState;
struct Pos;
class Store;
class LocalFSStore;

static constexpr Command::Category catHelp = -1;
static constexpr Command::Category catSecondary = 100;
Expand Down Expand Up @@ -367,4 +368,13 @@ void printClosureDiff(
const StorePath & afterPath,
std::string_view indent);

/**
* Create symlinks prefixed by `outLink` to the store paths in
* `buildables`.
*/
void createOutLinks(
const std::filesystem::path & outLink,
const BuiltPaths & buildables,
LocalFSStore & store);

}
8 changes: 8 additions & 0 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,12 @@ void BuiltPathsCommand::applyDefaultInstallables(std::vector<std::string> & rawI
rawInstallables.push_back(".");
}

BuiltPaths toBuiltPaths(const std::vector<BuiltPathWithResult> & builtPathsWithResult)
{
BuiltPaths res;
for (auto & i : builtPathsWithResult)
res.push_back(i.path);
return res;
}

}
2 changes: 2 additions & 0 deletions src/libcmd/installables.hh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct BuiltPathWithResult
std::optional<BuildResult> result;
};

BuiltPaths toBuiltPaths(const std::vector<BuiltPathWithResult> & builtPathsWithResult);

/**
* Shorthand, for less typing and helping us keep the choice of
* collection in sync.
Expand Down
25 changes: 1 addition & 24 deletions src/nix/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,6 @@ static nlohmann::json builtPathsWithResultToJSON(const std::vector<BuiltPathWith
return res;
}

// TODO deduplicate with other code also setting such out links.
static void createOutLinks(const std::filesystem::path& outLink, const std::vector<BuiltPathWithResult>& buildables, LocalFSStore& store2)
{
for (const auto & [_i, buildable] : enumerate(buildables)) {
auto i = _i;
std::visit(overloaded {
[&](const BuiltPath::Opaque & bo) {
auto symlink = outLink;
if (i) symlink += fmt("-%d", i);
store2.addPermRoot(bo.path, absPath(symlink.string()));
},
[&](const BuiltPath::Built & bfd) {
for (auto & output : bfd.outputs) {
auto symlink = outLink;
if (i) symlink += fmt("-%d", i);
if (output.first != "out") symlink += fmt("-%s", output.first);
store2.addPermRoot(output.second, absPath(symlink.string()));
}
},
}, buildable.path.raw());
}
}

struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile
{
Path outLink = "result";
Expand Down Expand Up @@ -140,7 +117,7 @@ struct CmdBuild : InstallablesCommand, MixDryRun, MixJSON, MixProfile

if (outLink != "")
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
createOutLinks(outLink, buildables, *store2);
createOutLinks(outLink, toBuiltPaths(buildables), *store2);

if (printOutputPaths) {
stopProgressBar();
Expand Down
18 changes: 18 additions & 0 deletions src/nix/copy.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include "local-fs-store.hh"

using namespace nix;

struct CmdCopy : virtual CopyCommand, virtual BuiltPathsCommand, MixProfile
{
std::optional<std::filesystem::path> outLink;
CheckSigsFlag checkSigs = CheckSigs;

SubstituteFlag substitute = NoSubstitute;

CmdCopy()
: BuiltPathsCommand(true)
{
addFlag({
.longName = "out-link",
.shortName = 'o',
.description = "Create symlinks prefixed with *path* to the top-level store paths fetched from the source store.",
.labels = {"path"},
.handler = {&outLink},
.completer = completePath
});

addFlag({
.longName = "no-check-sigs",
.description = "Do not require that paths are signed by trusted keys.",
Expand Down Expand Up @@ -58,6 +69,13 @@ struct CmdCopy : virtual CopyCommand, virtual BuiltPathsCommand, MixProfile
*srcStore, *dstStore, stuffToCopy, NoRepair, checkSigs, substitute);

updateProfile(rootPaths);

if (outLink) {
if (auto store2 = dstStore.dynamic_pointer_cast<LocalFSStore>())
createOutLinks(*outLink, rootPaths, *store2);
else
throw Error("'--out-link' is not supported for this Nix store");
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion tests/functional/zstd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ HASH=$(nix hash path $outPath)
clearStore
clearCacheCache

nix copy --from $cacheURI $outPath --no-check-sigs --profile $TEST_ROOT/profile
nix copy --from $cacheURI $outPath --no-check-sigs --profile $TEST_ROOT/profile --out-link $TEST_ROOT/result

[[ -e $TEST_ROOT/profile ]]
[[ -e $TEST_ROOT/result ]]

if ls $cacheDir/nar/*.zst &> /dev/null; then
echo "files do exist"
Expand Down

0 comments on commit 7f6d006

Please sign in to comment.