nixFlakes: can't figure out nix-instantiate equivalent

Created on 6 Aug 2020  路  6Comments  路  Source: NixOS/nix

Describe the bug

I can't figure out the equivalent for many nix-instantiate commands. Another bug #???? describes how nix eval and nix eval --{raw|json} behave differently. And the latter that works for me, returns the out path, rather than the derivation (this feels similar to how nix copy aggressively builds instead of just copying drvs #3696).

How do I get the drv path for a flake output? This is what I have now, I don't like it:


function instantiate() {
  machine="${1}"; shift
  # do it
  true
  #drv="$(nix eval --pure-eval ".#nixosConfigurations.${machine}.config.system.build.toplevel")" # TODO: why????
  set -e
  drv="$(set -eu; nix --experimental-features 'nix-command flakes' --pure-eval \
    eval \
    --raw ".#machines.${machine}")"
  drv="$(set -euo pipefail; nix --experimental-features 'nix-command flakes' --pure-eval \
    show-derivation "${drv}" | jq -r 'to_entries[].key')"
  echo -e "${drv}"
}
nix (Nix) 2.4pre20200721_ff314f1

EDIT (workaround)

You can do something like this instead:

thing=".#packages.x86_64-linux.testPackage" # pkg example
thing=".#nixosConfigurations.yourHostName.config.system.build.toplevel" # nixos cfg example
nix build \
  --experimental-features 'nix-command flakes' \
  "${thing}.drvPath"

This gives you a /nix/store/abc123..thing.drv path that you can then nix copy --derivation or nix-copy-closure to a remote machine to build, etc.

bug

Most helpful comment

This blocks "the good way" of doing large remote nix builds. I sympathize with the desire to iterate on the format, but can that be done in a backward compatible way that still allows all of the places where I've been using drvs?

I know I'm not the only one that nix-instantiates, copies drvs, and then uses cachix to move binaries.

All 6 comments

I think it's on purpose, Eelco wanted to hide the .drv implementation detail. With the idea that it will allow us to change the format down the line.

This blocks "the good way" of doing large remote nix builds. I sympathize with the desire to iterate on the format, but can that be done in a backward compatible way that still allows all of the places where I've been using drvs?

I know I'm not the only one that nix-instantiates, copies drvs, and then uses cachix to move binaries.

I know I'm not the only one that nix-instantiates, copies drvs, and then uses cachix to move binaries.

We do the same but on-premise cache.

I remembered that --derivation works for nix copy and was hoping that it might apply here, but it appears not. While --derivation appears in the help for nix eval, it still outputs the build output path, rather than the path to derivation. This is another thing that is forcing me to keep extra tooling around to work with flakes.

It should always possible to get the drv path with something like nix eval --raw foo#bar.drvPath. But indeed as @zimbatm pointed out, this might not be a viable long-term solution and there should be a cleaner way of doing that

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/to-flake-or-not-to-flake/10047/4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rrnewton picture rrnewton  路  34Comments

vcunat picture vcunat  路  36Comments

domenkozar picture domenkozar  路  53Comments

LisannaAtHome picture LisannaAtHome  路  42Comments

lukego picture lukego  路  34Comments