about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2016-04-22T12·16+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2016-04-22T12·33+0200
commit95abf9c40266687b5525261bad964c6856a58f18 (patch)
tree944df8c020d5dd8b050d37c062b153a69de63e84 /src
parent0207272b28f1dad119b418dfafcfb18d22b6d3f6 (diff)
Add "nix copy" command
This replaces nix-push. For example,

  $ nix copy --to file:///tmp/cache -r $(type -p firefox)

copies the closure of firefox to the specified binary cache. And

  $ nix copy --from file:///tmp/cache --to s3://my-cache /nix/store/abcd...

copies between two binary caches.

It will also replace nix-copy-closure, once we have an SSHStore class,
e.g.

  $ nix copy --from ssh://alice@machine /nix/store/abcd...
Diffstat (limited to 'src')
-rw-r--r--src/nix/copy.cc87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/nix/copy.cc b/src/nix/copy.cc
new file mode 100644
index 000000000000..87e6f604a0ae
--- /dev/null
+++ b/src/nix/copy.cc
@@ -0,0 +1,87 @@
+#include "command.hh"
+#include "progress-bar.hh"
+#include "shared.hh"
+#include "store-api.hh"
+#include "sync.hh"
+
+#include <atomic>
+
+using namespace nix;
+
+struct CmdCopy : StorePathsCommand
+{
+    std::string srcUri, dstUri;
+
+    CmdCopy()
+    {
+        mkFlag(0, "from", "store-uri", "URI of the source Nix store", &srcUri);
+        mkFlag(0, "to", "store-uri", "URI of the destination Nix store", &dstUri);
+    }
+
+    std::string name() override
+    {
+        return "copy";
+    }
+
+    std::string description() override
+    {
+        return "copy paths between Nix stores";
+    }
+
+    Examples examples() override
+    {
+        return {
+            Example{
+                "To copy Firefox to the local store to a binary cache in file:///tmp/cache:",
+                "nix copy --to file:///tmp/cache -r $(type -p firefox)"
+            },
+        };
+    }
+
+    void run(ref<Store> store, Paths storePaths) override
+    {
+        if (srcUri.empty() && dstUri.empty())
+            throw UsageError("you must pass ‘--from’ and/or ‘--to’");
+
+        ref<Store> srcStore = srcUri.empty() ? store : openStoreAt(srcUri);
+        ref<Store> dstStore = dstUri.empty() ? store : openStoreAt(dstUri);
+
+        ProgressBar progressBar;
+
+        std::atomic<size_t> done{0};
+        std::atomic<size_t> total{storePaths.size()};
+
+        auto showProgress = [&]() {
+            return (format("[%d/%d copied]") % done % total).str();
+        };
+
+        progressBar.updateStatus(showProgress());
+
+        storePaths.reverse(); // FIXME: assumes reverse topo sort
+
+        for (auto & storePath : storePaths) {
+            checkInterrupt();
+
+            if (dstStore->isValidPath(storePath)) {
+                total--;
+                progressBar.updateStatus(showProgress());
+                continue;
+            }
+
+            auto activity(progressBar.startActivity(format("copying ‘%s’...") % storePath));
+
+            StringSink sink;
+            srcStore->exportPaths({storePath}, false, sink);
+
+            StringSource source(*sink.s);
+            dstStore->importPaths(false, source, 0);
+
+            done++;
+            progressBar.updateStatus(showProgress());
+        }
+
+        progressBar.done();
+    }
+};
+
+static RegisterCommand r1(make_ref<CmdCopy>());