NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.94k stars 1.53k forks source link

Nix Flakes: Include Flake URI and/or store path in "nix flake list-inputs --json" #4536

Open ClemensB opened 3 years ago

ClemensB commented 3 years ago

Is your feature request related to a problem? Please describe. I'm using Nix Flakes for all of my development environments in conjunction with Direnv. One issue I had was that my flake's inputs are occasionally garbage collected, so I tried to automatically add GC roots for them as part of my ".envrc".

But while "nix flake list-inputs" lists the Flake URI of all input flakes, it is missing when using the "--json" argument. I'm using the Flake URI to get the input flake's store path using "nix flake info" to then create a GC root. Right now I'm reconstructing the Flake URI from the info available in the JSON output since I don't think parsing the human-readable output is a good idea. But this is quite cumbersome and much more so if you want to cover all types of Flake URIs.

Describe the solution you'd like It would be helpful to have the JSON output of "nix flake list-inputs" also contain the complete Flake URI since it's already part of the human-readable output. Having the store path there as well would make this task even easier.

Describe alternatives you've considered Creating GC roots for development shells and the containing flake including its inputs seems to be a very common use case so maybe this is something which could be incorporated into "nix develop" itself since it isn't even necessarily related to Direnv.

Additional context Right now my ".envrc" looks like this which works only for flakes on Github. This could be greatly simplified by having the Flake URI available:

use_flake() {
  watch_file flake.nix
  watch_file flake.lock

  [ -d "$(direnv_layout_dir)" ] || mkdir "$(direnv_layout_dir)"

  # Add GC root for development environment
  eval "$(nix print-dev-env --profile "$(direnv_layout_dir)/flake-profile")"
  nix-store --realise "$(direnv_layout_dir)/flake-profile" --add-root "$(direnv_layout_dir)/flake-profile-root" --indirect > /dev/null

  # Add GC roots for flake inputs
  while read node flake_url; do
    # For this command, the original Flake URI is required
    flake_path=$(nix flake info --json "$flake_url" | jq -r '.path')
    ln -sfT "$flake_path" "$(direnv_layout_dir)/flake-input-$node"
    nix-store --realise "$(direnv_layout_dir)/flake-input-$node" --add-root "$(direnv_layout_dir)/flake-input-${node}-root" --indirect > /dev/null
  done < <(nix flake list-inputs --json | jq -r '.nodes
    | keys[] as $k
    | select(.[$k].locked.type == "github")
    | .[$k].locked
    | "\($k) github:\(.owner)/\(.repo)/\(.rev)"'
  # The block above reconstructs the Flake URI, which seems like it should not be necessary
}

use flake
stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info