Help with local binary cache

I’m trying to copy packages from cache.nixos.org to a local binary cache, and then from the cache to the local store on a different machine - something Nix is supposed to support. I’ve encountered many obstacles along the way - some I was able to work around, and some have me stuck. I’ve included my full battle log, in case someone can help me with any stage.

  • I start with cloning nixpkgs, so the versions always match:

    $ git clone -b master --depth 1 https://github.com/NixOS/nixpkgs
    Cloning into 'nixpkgs'...
    ...
    Updating files: 100% (34428/34428), done.
    
  • Then what should be the correct nix copy command:

    $ nix copy --from https://cache.nixos.org/ --to file://$PWD/cache ./nixpkgs#hello
    warning: error: unable to upload 
    'https://cache.nixos.org/nar/0r0c0cyw43fa6nrb5z6mdg0wr1nbc6x2v2bbpl6vs731d7h8nxrv.nar.xz': HTTP error 503
    <aborted after a long time trying to upload>
    

    I seem to have run into nix copy --from ... nixpkgs.hello tries to upload instead of download · Issue #2637 · NixOS/nix · GitHub - nix copy tries to evaluate the expression within the --from store.

  • I try the workaround suggested there(nix copy --from ... nixpkgs.hello tries to upload instead of download · Issue #2637 · NixOS/nix · GitHub):

    $ nix copy --from https://cache.nixos.org/ --to file://$PWD/cache ./nixpkgs#hello --eval-store ""
    don't know how to build these paths:
      /nix/store/xcj88diixazdl70bqsd2sn7yflm5dqz8-hello-2.12.1.drv
    error: path '/nix/store/xcj88diixazdl70bqsd2sn7yflm5dqz8-hello-2.12.1.drv' is not a valid store path
    

    No idea what to make of that.

  • I try just using the local store, using substitution:

    $ nix copy --to file://$PWD/cache ./nixpkgs#hello
    

    This exits successfully, and leaves the cache directory populated, so whilst going via the local store is not what I intended, I’ll take on as a working hypothesis that I have a working cache, and move forward for now.

On the remote machine:

$ nix copy --from file:///$PWD/cache/ --eval-store "" ./nixpkgs#hello
don't know how to build these paths:
   /nix/store/xcj88diixazdl70bqsd2sn7yflm5dqz8-hello-2.12.1.drv
error: path '/nix/store/xcj88diixazdl70bqsd2sn7yflm5dqz8-hello-2.12.1.drv' is not a valid store path

Same problem again.

I throw my hands up. Anyone have any suggestions?

1 Like

same question… need some help

While reading the documentation I think it has to do with this:

Therefore one can only copy store objects to a different store if

  • The source and target stores’ directories match
or
  • The store object in question has no references, that is, contains no store paths

One cannot copy a store object to a store with a different store directory. Instead, it has to be rebuilt, together with all its dependencies. It is in general not enough to replace the store directory string in file contents, as this may render executables unusable by invalidating their internal offsets or checksums.

https://nix.dev/manual/nix/2.22/store/types/local-store

It is also possible, but not recommended, to change the “logical” location of the Nix store from its default of /nix/store. This makes it impossible to use default substituters such as https://cache.nixos.org/, and thus you may have to build everything locally. Here is an example:

 nix build --store 'local?store=/tmp/my-nix/store&state=/tmp/my-nix/state&log=/tmp/my-nix/log' nixpkgs#hello

https://nix.dev/manual/nix/2.22/store/types/local-store

https://nix.dev/manual/nix/2.22/store/types/local-binary-cache-store

nix copy --to file:///tmp/binary-cache nixpkgs#hello

I think binary cache should work out-of-box, not sure if I’m wrong.