Skip to content

Comments

nix repl: load flakes from cli args#5824

Closed
nrdxp wants to merge 1 commit intoNixOS:masterfrom
nrdxp:nix-repl-flakes
Closed

nix repl: load flakes from cli args#5824
nrdxp wants to merge 1 commit intoNixOS:masterfrom
nrdxp:nix-repl-flakes

Conversation

@nrdxp
Copy link

@nrdxp nrdxp commented Dec 22, 2021

I think when working with flakes it is quite common to load the flake into a repl for inspecting and debugging. Therefore, when experimental feature "flakes" is enabled, it makes sense to consider the arguments as flake refs instead of files.
As a small bonus, I've implemented the ability for the :load-flake command to parse flake ref fragments (i.e. the part after the #).

I still need to modify the documentation, and I'm debating on whether I should add new --files, -f flag for when the old behavior is desired while using flakes, so I'm opening as draft for feedback. It would also be nice to include the same search behavior as other flake ref parsing commands, e.g. nixpkgs#zlib becomes nixpkgs#legacyPackages.$current_system.zlib.

If experimental feature "flakes" is enabled, args passed to `nix repl`
will now be considered flake refs and imported using the existing
`:load-flake` machinery.

In addition, `:load-flake` now supports loading flake fragments.
Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

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

As a small bonus, I've implemented the ability for the :load-flake command to parse flake ref fragments (i.e. the part after the #)

Yay! That’s cool 😎

when experimental feature "flakes" is enabled, it makes sense to consider the arguments as flake refs instead of files

That’s a tricky thing to decide. Because it means breaking (again) backwards-compatibility once flake aren’t experimental anymore.

Maybe a nicer path would be to

  • Add a --file flag for loading a file
  • Add a --flake flag for loading a flake
  • Deprecate (but keep) the current behavior and recommend to use --file instead
  • Eventually (after flakes are non-experimental anymore, and after at least 2 or 3 6w cycles) change the behavior

Possibly (dunno how much work that would be), the last step could be done by moving the cli of nix repl to use the same mechanism as the other commands (making it an instance of InstallableCommand) to make it fully consistent with the rest

It would also be nice to include the same search behavior as other flake ref parsing commands, e.g. nixpkgs#zlib becomes nixpkgs#legacyPackages.$current_system.zlib

Yes, that sounds like that would be great (and probably also for :lf)

throw Error("cannot use ':load-flake' on mutable flake reference '%s' (use --impure to override)", flakeRefS);

Value v;
auto v = state->allocValue();
Copy link
Member

Choose a reason for hiding this comment

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

What’s the reason for the change? Having v as a plain stack-allocated Value is nice since it has a lexical lifetime.

Copy link
Author

Choose a reason for hiding this comment

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

Oh I think I copied that from somewhere else while try to debug a stack overflow. I'll change it back 👍

addAttrsToScope(v);
*v);

auto f = v->attrs->get(state->symbols.create(fragment));
Copy link
Member

Choose a reason for hiding this comment

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

I think this won’t work properly if fragment has more than one level (foo.bar). You probably need to split it first with parseAttrPath and recursively get the right value.

Copy link
Author

Choose a reason for hiding this comment

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

I'll give that a shot. While we are at it, is there a function or method somewhere used to traverse the multiple outputs to find a match?

@edolstra edolstra added this to the nix-2.7 milestone Feb 3, 2022
Copy link
Member

@edolstra edolstra left a comment

Choose a reason for hiding this comment

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

Could you mention this in the release notes?


auto f = v->attrs->get(state->symbols.create(fragment));

if (f == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (f == 0) {
if (!f) {

auto f = v->attrs->get(state->symbols.create(fragment));

if (f == 0) {
warn("no attribute %s, nothing loaded", fragment);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
warn("no attribute %s, nothing loaded", fragment);
warn("attribute '%s' does not exist, nothing loaded", fragment);

return;
};

fragment != "" ? addAttrsToScope(*f->value) : addAttrsToScope(*v);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fragment != "" ? addAttrsToScope(*f->value) : addAttrsToScope(*v);
addAttrsToScope(fragment != "" ? *f->value : *v);

Comment on lines +667 to +669
settings.isExperimentalFeatureEnabled(Xp::Flakes)
? loadFlake(i)
: loadFile(i);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
settings.isExperimentalFeatureEnabled(Xp::Flakes)
? loadFlake(i)
: loadFile(i);
if (settings.isExperimentalFeatureEnabled(Xp::Flakes))
loadFlake(i);
else
loadFile(i);

since it's not really idiomatic in C++ (IMHO) to have the conditional operator at statement level.

@tomberek tomberek added the good first issue Quick win for first-time contributors label Feb 17, 2022
@tomberek
Copy link
Contributor

Added a bit of cleanup here: flox@da4dcb5

After this i wanted to start using InstallableCommand,InstallablesCommand, or SourceExprCommand so that we are using the Installable concept instead of having a conditional on an experimental flag. Directly using that works, but also brings some unwanted default behavior:

nix repl .#

should probably NOT use a default attrpath of defaultPackages.${system}. At least my expectation is to preserve @nrdxp 's implemented behavior where that would load the output attrs. (Though it would also be good to have the inputs?)

@edolstra edolstra modified the milestones: nix-2.7, nix-2.8 Mar 3, 2022
@thufschmitt thufschmitt assigned thufschmitt and unassigned edolstra and Radvendii Mar 10, 2022
@tomberek tomberek mentioned this pull request Mar 11, 2022
@tomberek tomberek self-assigned this Mar 11, 2022
@thufschmitt thufschmitt added feature Feature request or proposal and removed good first issue Quick win for first-time contributors labels Mar 12, 2022
@edolstra edolstra modified the milestones: nix-2.8, nix-2.9 Apr 14, 2022
@tomberek
Copy link
Contributor

Working on #6233

@tomberek tomberek closed this May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Feature request or proposal

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants