Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtins.getFlake: also support path argument #12100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zimbatm
Copy link
Member

@zimbatm zimbatm commented Dec 24, 2024

Motivation

This is a papercut that has been bugging me for a while. :lf . is fine but doesn't show you what variables it adds to the context. And builtins.getFlake (toString ./.) works but gets tedious after a while.

Context

When inspecting a flake in the repl, the natural thing to do is to call:

x = builtins.getFlake ./.

Before this change, Nix would fail with:

error: expected a string but found a path: /path/to/flake

After this change, Nix automatically coerces the path to a string.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

This is a papercut that has been bugging me for a while. `:lf .` is fine
but doesn't show you what variables it adds to the context. And
`builtins.getFlake (toString ./.)` works but gets tedious after a while.

When inspecting a flake in the repl, the natural thing to do is to call:

    x = builtins.getFlake ./.

Before this change, Nix would fail with:

    error: expected a string but found a path: /path/to/flake

After this change, Nix automatically coerces the path to a string.
@zimbatm zimbatm requested a review from edolstra as a code owner December 24, 2024 14:36
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Dec 24, 2024
@zimbatm zimbatm requested review from Ericson2314 and Mic92 and removed request for edolstra December 24, 2024 15:25
Copy link
Member

@Mic92 Mic92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea.

@@ -69,7 +69,7 @@ EOF
cat >> "$scriptDir/shebang-inline-expr.sh" <<"EOF"
#! nix --offline shell
#! nix --impure --expr ``
#! nix let flake = (builtins.getFlake (toString ../flake1)).packages;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave this test and add this as a second one so it ensures the backwards compatibility?

Comment on lines +826 to +829
NixStringContext context; // no context
auto flakeRefS = state.coerceToString(pos, *args[0], context,
"while evaluating the argument passed to builtins.getFlake",
false, false, true).toOwned();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this now allows a derivation output to be passed, but it won't be realised, resulting in an error if the output isn't already in the store.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that because of the true argument, or a general coerceToString property since it also converts { outPath = "foo"; } to "foo"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a couple of things I guess. I think coerceToPath is a better match for this use case, although the SourcePath doesn't slot nicely into the flakeref parsing, but we shouldn't be using any flakeref parsing for a path value anyway.

It seems best to have a separate code path for when the argument's .type() is nPath.

@edolstra
Copy link
Member

We should carefully consider (also with respect to lazy trees) what the intended behaviour of these relative path arguments is. Presumably, builtins.getFlake ./. will currently result in the equivalent of builtin.getFlake "path:/nix/store/X (where /nix/store/X is a copy of ./.), but I'm not sure if we want that. For instance, it won't be able to access parent directories in the same source tree. It would probably be better if it uses the source accessor of ./. directly.

This PR also conflicts with #11952.

@roberth
Copy link
Member

roberth commented Dec 31, 2024

That would be in line with coerceToPath.

Nothing is copied to the store.

Presumably this does not produce a restricted root either.

@roberth
Copy link
Member

roberth commented Dec 31, 2024

This PR also conflicts with #11952.

Path-based behavior would have to follow from the combination of that plus #10089, though probably needs a bit of specific code beyond merging those PRs, especially if we don't want to require an argument like { url = ./path; }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants