Skip to content

nix copy uses too much memory #1681

Open

Description

@LisannaAtHome

I'm running nix copy in runInLinuxVM, and notice that for any nontrivial closures, the VM will run out of memory during the copying process. I left it set at the default 512 megabytes. I could obviously increase the amount of memory the VM is given, but that doesn't scale for copying complex derivations with many dependencies.

I suggest adding an option to only load and copy the contents of the paths one at a time, or even better, a way to specify an upper bound on the memory to be used while copying.

Activity

  1. copumpkin commented on Nov 15, 2017

    @copumpkin
    Member

    Intuitively it feels that it should be possible for it to run in constant memory. What am I missing?

  2. lheckemann commented on Feb 23, 2018

    @lheckemann
    Member

    I'm encountering this issue with a single path — nix copy, nix-store --import, and a number of other commands I've tried all fail to import the path. Would be great to know if there's any way at all I can import it…

  3. LisannaAtHome commented on Mar 17, 2018

    @LisannaAtHome
    Author

    Possibly related to #1969 ? Looks like some patches have gone in recently that might improve things here: 48662d1 3e6b194

  4. Ralith commented on Apr 2, 2018

    @Ralith

    I see commits purporting to address this for a number of different cases, but none concerning uploading to a S3 bucket. Trying to copy a 2.8GB store path to a S3 bucket took nearly 4GB of memory and more than twenty minutes of 100% CPU. Has that been fixed?

  5. andrewchambers commented on Apr 8, 2018

    @andrewchambers

    Hitting this issue trying to do something like - nixos-rebuild build ; nix copy ./result --to ssh://low_ram_machine

    @dtzWill will those experimental changes help with ssh copy?

  6. edolstra commented on Apr 12, 2018

    @edolstra
    Member

    @Ralith I'm probably not going to make S3BinaryCacheStore do uploads in constant space. It might not even be supported by aws-sdk-cpp.

    I assume the 100% CPU is caused by compression, which you can disable.

  7. copumpkin commented on Apr 12, 2018

    @copumpkin
    Member

    FWIW I too am another big-upload-to-S3 guy using nix copy 😄

    It would surprise me if aws-sdk-cpp didn't support it, given that S3 supports almost arbitrarily large objects and multi-part uploads. If someone figured out how to implement it, would you accept the PR?

  8. Ralith commented on Apr 13, 2018

    @Ralith

    I assume the 100% CPU is caused by compression, which you can disable.

    It seems very strange that it would take twenty minutes on my i7-4980HQ, even so. 2.8GB is big but it's not that big.

  9. 55 remaining items

  10. added
    new-cli
    on Sep 13, 2022
  11. tshakah commented on Nov 7, 2024

    @tshakah

    I'm getting this copying a closure from an arm builder onto a Pi Zero, which has 512mb of ram. This is on nix 2.18.8

  12. removed their assignment
    on Nov 9, 2024
  13. poliorcetics commented on Apr 25, 2025

    @poliorcetics
    Contributor

    Continuing on @nh2 search of 7 years ago:

    Goal

    Have imports to the nix store (nix copy, nix-store --import, ...) implemented entirely in a streaming fashion.

    Reproducing

    A somewhat simple reproducer I whipped up to experiment:

    dd if=/dev/zero of=large-file bs=1M count=5120
    large_file_store_path=$(nix-store --add large-file)
    
    nix-store --export "$large_file_store_path" > large-file-closure
    
    # Each time you repeat this you will see a 5GiB in RAM consumption during the import
    nix-store --delete "$large_file_store_path"
    nix-store --import < large-file-closure

    Causes

    We enter through nix-store --import:

    We reach Store::importhPaths here.

    In this part of the method, the entire file is parsed once (via parseDump) and at the same time the whole string is saved in memory via TeeSource and StringSink (any read from TeeSource appends to StringSink).

    Then the rest of the function will actually add to the nix store, redoing the parsing+dumping done previously but from the string saved in the previous part and writing to disk.

    I'm not familiar with how Nix is implemented but I'm guessing it probably could be done in a streaming fashion ? One big thing stands in the way though:

    The narHash is precomputed and seem to be the main reason for buffering the whole string. See this line

    • For local-store only it's probably unnecessary (the hash ends up checked later, but since the source is the same, of course it will always work barring memory corruption)
    • For non-local-stores (I'm guessing daemon & remote ?) the hash must be sent first so that it can be checked on the receiving side (and I don't think it's possible to do away with it given we like the integrity guarantees of Nix)

    As said above I'm not all that familiar with the Nix codebase so I can't say if that's a fixable issue or not. I may also have gotten stuff wrong because of this so don't hesitate to correct my understanding!

  14. tomberek commented on Aug 13, 2026

    @tomberek
    Contributor

    Closing as stale. Re-open if needed.

  15. nwf commented on Aug 13, 2026

    @nwf

    At a very quick glance, things have not changed on master in a way that would invalidate #1681 (comment) observations. Please consider re-opening.

  16. xokdvium commented on Aug 13, 2026

    @xokdvium
    Contributor

    nix copy (via addToStore) is implemented in a streaming fashion and does use constant memory with new enough nix - as are uploads to binary caches. import/export still buffers though yeah

  17. nh2 commented on Aug 13, 2026

    @nh2
    Contributor

    Reopening. I think this bug should only be closed once it's actually fixed and that can be shown by providing the according maxresident memory usage.

  18. reopened this on Aug 13, 2026
  19. poliorcetics commented on Aug 15, 2026

    @poliorcetics
  20. xokdvium commented on Aug 15, 2026

    @xokdvium
    Contributor

    That is not the NAR format - but rather the export/import bespoke serialisation. And notably, nix copy doesn't use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    new-cliperformance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions