Search code examples
nix

Nix mkDerivation can't recognize local filesystem zip file as src


According to https://nixos.org/nixpkgs/manual/#ssec-unpack-phase I can use a zip file as a 'source'.

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "test-environment";
  nativeBuildInputs = [ unzip ];
  buildInputs = [ unzip ];
  src = "./x.zip";
}
nix-build
these derivations will be built:
  /nix/store/431i1riasgh2hjs5rd9azfh0ssjlg7qj-test-environment.drv
building '/nix/store/431i1riasgh2hjs5rd9azfh0ssjlg7qj-test-environment.drv'...
unpacking sources
unpacking source archive ./x.zip
unzip:  cannot find or open ./x.zip, ./x.zip.zip or ./x.zip.ZIP.
unzip:  cannot find or open ./x.zip, ./x.zip.zip or ./x.zip.ZIP.
do not know how to unpack source archive ./x.zip
builder for '/nix/store/431i1riasgh2hjs5rd9azfh0ssjlg7qj-test-environment.drv' failed with exit code 1
error: build of '/nix/store/431i1riasgh2hjs5rd9azfh0ssjlg7qj-test-environment.drv' failed

There is a x.zip file in my working directory. Why is it failing?


Update:

If I set unpackCmd = "ls -altr"; I see the following output:

total 16
drwxr-x--- 9 nobody nixbld 4096 May 26 12:39 ..
drwx------ 2 nixbld nixbld 4096 May 26 12:39 .
-rw-r--r-- 1 nixbld nixbld 5806 May 26 12:39 env-vars

Why isn't my zip file appearing in that context?


Solution

  • You don't want your Nix derivation to reference local filesystem elements! If anything hasn't been hashed and put into the store, your build is impure, and won't work in sandboxed mode.

    Change it to src = ./x.zip, and the file will be hashed and added to the store, and src will be set to a /nix/store/.....-x.zip path.

    with import <nixpkgs> {};
    
    stdenv.mkDerivation {
      name = "test-environment";
      nativeBuildInputs = [ unzip ];
      buildInputs = [ unzip ];
      src = ./x.zip;  ## NO QUOTES!
    }