Changing NIX_STORE_DIR breaks IFD (import from derivation)

I set my store path with:

export NIX_STORE_DIR=/home/tobor/test/store
export NIX_STATE_DIR=/home/tobor/test/state
export NIX_LOG_DIR=/home/tobor/test/log

I then make a very simple derivation in test.nix:

derivation {
    system = "aarch64-linux";
    name = "test";
    builder = "/bin/sh";
    args = [
      "-c"
      "echo hello > $out"
    ];
}

This works just fine with both nix-instantiate test.nix and nix-build test.nix.

I then delete the store and restart the process. This time I use IFD:

let
  a = derivation {
    system = "aarch64-linux";
    name = "test";
    builder = "/bin/sh";
    args = [
      "-c"
      "echo hello > $out"
    ];
  };
in
builtins.readFile a

Now, when I run nix-instantiate --eval test.nix:

error:
       … while calling the 'readFile' builtin
         at /home/tobor/test/test.nix:12:1:
           11| in
           12| builtins.readFile a
             | ^
           13|

       … while realising the context of path '/home/tobor/test/store/b9pmpsr4mc1lwf3havcl6g1901msyyr1-test'

       error: path '/home/tobor/test/store/m53j1p0dq2hhdnxzykrs0zw2qdrbgicc-test.drv' is not valid

The drv file does not exist in the store.
I then go back to using the first file and run nix-instantiate test.nix, which creates the drv file. I then try IFD again:

error:
       … while calling the 'readFile' builtin
         at /home/tobor/test/test.nix:12:1:
           11| in
           12| builtins.readFile a
             | ^
           13|

       … while realising the context of path '/home/tobor/test/store/b9pmpsr4mc1lwf3havcl6g1901msyyr1-test'

       error: cannot substitute path '/home/tobor/test/store/b9pmpsr4mc1lwf3havcl6g1901msyyr1-test' - no write access to the Nix store

If I run nix-build /home/tobor/test/store/m53j1p0dq2hhdnxzykrs0zw2qdrbgicc-test.drv and try again, then it finally works:

"hello\n"

Does anyone have any clue why this could be happening and how I could fix it? I want to use nix in a derivation without recursive-nix (I don’t want to enable it system-wide) to evaluate a file that does IFD.

A separate question, is there also by any chance a way to tell it to find the built derivation for the IFD from a specific path in the real /nix/store (or it can even just somehow get it from the real /nix/store if the inputs match)?

Thank you in advance for any help.

did you solve this?
I’m curious – it looks similar to what i was trying with dynamic-derivations

No, unfortunately not yet. The reason I’m doing it is probably completely different than your interest though (I did read your posts, they’re very interesting! Although I’m probably not skilled enough yet to fully understand them).

I’m actually trying to get the evaluation warnings/traces (builtins.trace) of my configuration in a derivation. I modified the config attribute passed to the nixpkgs base modules to log what options are being evaluated. The idea is to collect these traces (which works with a basic configuration) and then make a smaller list of base nixpkgs modules to import containing only the ones that define these evaluated options (which I plan to find via option.definitions and option.declarations). In the end, it’s to make the evaluation time faster.

I simply evaluate system.build.toplevel and system.build.vm inside of a derivation, collecting the traces and then building the reduced list of nixpkgs modules. This is probably not a great idea, but I could not find/think of any other possibility to automatically generate such list.

It seems I need to enable recursive-nix system wide to use it, but I prefer for derivations on my system not to be allowed to use it without my explicit consent. nix-instantiate works without recursive-nix just fine with a temporary nix store in the derivation, so I tried that.

The problem is that my configuration uses IFD to generate a color scheme from my wallpaper and that seems to break in the temporary store in the derivation (and outside, as in the example above).

I’ll let you know if I find any solutions to IFD breaking with a custom store path.

it’s not changing NIX_STORE_DIR breaks IFD, it’s because
–eval

Just parse and evaluate the input files, and print the resulting values on standard output. No instantiation of store derivations takes place.

if you want to realize the derivation pass --read-write-mode
nix-instantiate --eval --read-write-mode test.nix

1 Like

You’re right, that fixed it. Thank you!