Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix copy: Add --profile and --out-link flags #11657

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,41 +179,45 @@ BuiltPathsCommand::BuiltPathsCommand(bool recursive)

void BuiltPathsCommand::run(ref<Store> store, Installables && installables)
{
BuiltPaths paths;
BuiltPaths rootPaths, allPaths;

if (all) {
if (installables.size())
throw UsageError("'--all' does not expect arguments");
// XXX: Only uses opaque paths, ignores all the realisations
for (auto & p : store->queryAllValidPaths())
paths.emplace_back(BuiltPath::Opaque{p});
rootPaths.emplace_back(BuiltPath::Opaque{p});
allPaths = rootPaths;
} else {
paths = Installable::toBuiltPaths(getEvalStore(), store, realiseMode, operateOn, installables);
rootPaths = Installable::toBuiltPaths(getEvalStore(), store, realiseMode, operateOn, installables);
allPaths = rootPaths;

if (recursive) {
// XXX: This only computes the store path closure, ignoring
// intermediate realisations
StorePathSet pathsRoots, pathsClosure;
for (auto & root : paths) {
for (auto & root : rootPaths) {
auto rootFromThis = root.outPaths();
pathsRoots.insert(rootFromThis.begin(), rootFromThis.end());
}
store->computeFSClosure(pathsRoots, pathsClosure);
for (auto & path : pathsClosure)
paths.emplace_back(BuiltPath::Opaque{path});
allPaths.emplace_back(BuiltPath::Opaque{path});
}
}

run(store, std::move(paths));
run(store, std::move(allPaths), std::move(rootPaths));
}

StorePathsCommand::StorePathsCommand(bool recursive)
: BuiltPathsCommand(recursive)
{
}

void StorePathsCommand::run(ref<Store> store, BuiltPaths && paths)
void StorePathsCommand::run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths)
{
StorePathSet storePaths;
for (auto & builtPath : paths)
for (auto & builtPath : allPaths)
for (auto & p : builtPath.outPaths())
storePaths.insert(p);

Expand Down Expand Up @@ -245,7 +249,7 @@ MixProfile::MixProfile()
void MixProfile::updateProfile(const StorePath & storePath)
{
if (!profile) return;
auto store = getStore().dynamic_pointer_cast<LocalFSStore>();
auto store = getDstStore().dynamic_pointer_cast<LocalFSStore>();
if (!store) throw Error("'--profile' is not supported for this Nix store");
auto profile2 = absPath(*profile);
switchLink(profile2,
Expand Down Expand Up @@ -308,7 +312,8 @@ MixEnvironment::MixEnvironment() : ignoreEnvironment(false)
});
}

void MixEnvironment::setEnviron() {
void MixEnvironment::setEnviron()
{
if (ignoreEnvironment) {
if (!unset.empty())
throw UsageError("--unset does not make sense with --ignore-environment");
Expand Down
19 changes: 16 additions & 3 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ struct StoreCommand : virtual Command
{
StoreCommand();
void run() override;

/**
* Return the default Nix store.
*/
ref<Store> getStore();

/**
* Return the destination Nix store.
*/
virtual ref<Store> getDstStore()
{
return getStore();
}

virtual ref<Store> createStore();
/**
* Main entry point, with a `Store` provided
Expand All @@ -68,7 +81,7 @@ struct CopyCommand : virtual StoreCommand

ref<Store> createStore() override;

ref<Store> getDstStore();
ref<Store> getDstStore() override;
};

/**
Expand Down Expand Up @@ -238,7 +251,7 @@ public:

BuiltPathsCommand(bool recursive = false);

virtual void run(ref<Store> store, BuiltPaths && paths) = 0;
virtual void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) = 0;

void run(ref<Store> store, Installables && installables) override;

Expand All @@ -251,7 +264,7 @@ struct StorePathsCommand : public BuiltPathsCommand

virtual void run(ref<Store> store, StorePaths && storePaths) = 0;

void run(ref<Store> store, BuiltPaths && paths) override;
void run(ref<Store> store, BuiltPaths && allPaths, BuiltPaths && rootPaths) override;
};

/**
Expand Down
8 changes: 5 additions & 3 deletions src/nix/copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using namespace nix;

struct CmdCopy : virtual CopyCommand, virtual BuiltPathsCommand
struct CmdCopy : virtual CopyCommand, virtual BuiltPathsCommand, MixProfile
{
CheckSigsFlag checkSigs = CheckSigs;

Expand Down Expand Up @@ -43,19 +43,21 @@ struct CmdCopy : virtual CopyCommand, virtual BuiltPathsCommand

Category category() override { return catSecondary; }

void run(ref<Store> srcStore, BuiltPaths && paths) override
void run(ref<Store> srcStore, BuiltPaths && allPaths, BuiltPaths && rootPaths) override
{
auto dstStore = getDstStore();

RealisedPath::Set stuffToCopy;

for (auto & builtPath : paths) {
for (auto & builtPath : allPaths) {
auto theseRealisations = builtPath.toRealisedPaths(*srcStore);
stuffToCopy.insert(theseRealisations.begin(), theseRealisations.end());
}

copyPaths(
*srcStore, *dstStore, stuffToCopy, NoRepair, checkSigs, substitute);

updateProfile(rootPaths);
}
};

Expand Down
9 changes: 9 additions & 0 deletions src/nix/copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ R""(
# nix copy --to /tmp/nix nixpkgs#hello --no-check-sigs
```

* Update the NixOS system profile to point to a closure copied from a
remote machine:

```console
# nix copy --from ssh://server \
--profile /nix/var/nix/profiles/system \
/nix/store/r14v3km89zm3prwsa521fab5kgzvfbw4-nixos-system-foobar-24.05.20240925.759537f
```

# Description

`nix copy` copies store path closures between two Nix stores. The
Expand Down
2 changes: 1 addition & 1 deletion src/nix/realisation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct CmdRealisationInfo : BuiltPathsCommand, MixJSON

Category category() override { return catSecondary; }

void run(ref<Store> store, BuiltPaths && paths) override
void run(ref<Store> store, BuiltPaths && paths, BuiltPaths && rootPaths) override
{
experimentalFeatureSettings.require(Xp::CaDerivations);
RealisedPath::Set realisations;
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/zstd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ HASH=$(nix hash path $outPath)
clearStore
clearCacheCache

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

[[ -e $TEST_ROOT/profile ]]

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