Get nix store path from Flake input

I’m working on a Bash script that can evaluate a Nix expression with nix eval. This is all happening inside of a Flake. The Nix expression needs a reference to nixpkgs. I could use fetchTarball to get a copy of Nixpkgs, however I would rather somehow get a reference to the derivation loaded as an input for the flake. Is there a way to do this?

Here’s some pseudo-nix to convey the idea. obviously coercing the nixpkgs input directly wouldn’t work, but is there a way to find its derivation?

{
  inputs.nixpkgs.url = "...";
  outputs = {nixpkgs}: {
    script = pkgs.runCommand "script" {} ''
      nix eval 'import ${./other_nix_file.nix} ${nixpkgs}' > $out
    '';
  };
}

This is almost certainly an XY problem; if you explain what you’re actually doing and why you think you need this there is probably a better solution.

Why not? The stringifying is implicitly accessing nixpkgs.outPath. It’s just weird to do whatever you’re trying to do at runtime (relying on a nix eval command), when it could be done at build-time - you’re already writing nix code, so you already have access to the nix evaluator.

There’s no “derivation” involved though; flake inputs are fetched using builtins.fetchTree. There’s nothing to build with respect to fetching nixpkgs, so there’s no “derivation” associated with nixpkgs.

Somehow I see a deep misunderstanding of what nix is doing here, so I agree that you should tell us what you’re actually trying to accomplish instead of tunnel-visioning on a solution that doesn’t make sense at first glance.

More specifically: what is the content of other_nix_file.nix?

1 Like

Ahh, you’re right, I was mixing up the nixpkgs input with the imported nixpkgs object. That would work.

I’ve since reached the conclusion that I don’t need nix eval. What I was trying to do was provide a script to pass as the --filter option to Pandoc (to apply a transform to a document tree). But I figured out that I can split up the Pandoc call into two steps, one to parse a document, then I can transform the results with nix, then call Pandoc again on the transformed document tree to produce the output document format.

You can still access the same path from a nixpkgs instance (pkgs.path).

1 Like

Yes, it seems there are some extra properties on the inputs object including outPath.

https://nix.dev/manual/nix/2.24/command-ref/new-cli/nix3-flake#flake-format

In addition to the outputs of each input, each input in inputs also contains some metadata about the inputs. These are:

  • outPath: The path in the Nix store of the flake’s source tree. This way, the attribute set can be passed to import as if it was a path, as in the example above (import nixpkgs).