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

Flake-based nix-shell replacement #2854

Closed
edolstra opened this issue Feb 13, 2019 · 9 comments
Closed

Flake-based nix-shell replacement #2854

edolstra opened this issue Feb 13, 2019 · 9 comments
Milestone

Comments

@edolstra
Copy link
Member

The nix command currently has no modern replacement for nix-shell. It should provide something that can run/modify a shell based on a flake attribute, e.g. provides.shell, e.g.

provides.shell = ... {
  packages = [ libfoo libbar ];
};

There should also be a command for querying the environment variables to be set for an environment. This could be useful for a direnv-based setup.

@zimbatm
Copy link
Member

zimbatm commented Feb 14, 2019

buildInputs is a bit obscure and could be replaced with something more generic like packages. In the shell context there is no distinction between build, native and propagated inputs.

@CSVdB CSVdB self-assigned this Apr 8, 2019
@CSVdB CSVdB removed their assignment Apr 30, 2019
@edolstra edolstra closed this as completed May 9, 2019
@grahamc grahamc transferred this issue from another repository May 16, 2019
@grahamc grahamc added this to the flakes-v2 milestone May 16, 2019
@kirelagin
Copy link
Member

It’s a little bit unfortunate how this issue got closed without any explanation of its state. It would be great to add some pointers to the work that has been done, if any, and to some up to date resources that document the integration of nix-shell with flakes.

@edolstra
Copy link
Member Author

The replacement is the nix develop command which uses the devShell.${system} flake output if it exists or defaultPackage.${system} otherwise.

@nixos-discourse
Copy link

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

https://discourse.nixos.org/t/build-a-yocto-rootfs-inside-nix/2643/26

@superherointj
Copy link

The replacement is the nix develop command which uses the devShell.${system} flake output if it exists or defaultPackage.${system} otherwise.

Will shellHook work when using nix develop? @grahamc

@fluffynukeit
Copy link

fluffynukeit commented Aug 23, 2021

The replacement is the nix develop command which uses the devShell.${system} flake output if it exists or defaultPackage.${system} otherwise.

I have found this approach confusing because there are often several distinct cases in which I might want to run a 'shellHook'.

  1. I want a particular shell environment to set up my automated build environment for a derivation when I run nix build. Typically, I don't need any kind of hook for this because I can override the various stdenv phases to configure the shell however I wish.
  2. I want a particular shell environment so I can interactively inspect and iterate through the build phases of 1) above, which is what I'd expect from nix develop.
  3. I want a particular shell environment so I can correctly run interactively the installed package(s), which might require some kind of runtime setup that doesn't fit properly with wrapProgram or makeWrapper. I'd expect this from nix shell.

It's entirely unclear to me under which circumstances what shellHooks and setupHooks get sourced. I've had to try a lot of things out. Maybe it's documented somewhere but I haven't seen it.

As of build nix (Nix) 2.4pre20210802_47e96bb, the following are my findings. Diagnoses were made by exporting a variable and then inspecting the value in the resulting shell environment.

  • In a flake with no devShell specified, nix develop . will run the shellHook that is defined in the default package, but nix shell . will not. Neither will run setupHook.
  • In a flake with devShell specified and the default package in buildInputs but no shellHook on the devShell, nix develop . will run the setupHook of the default package but not the shellHook. nix shell will run neither.
  • In a flake with devShell specified and the default package in buildInputs and shellHook on the devShell, nix develop . will run the devShell's shellHook and the default package's setupHook. nix shell will run neither.

Conclusions:

  • nix shell never runs a shellHook or a setupHook. This is what is especially counterintuitive for me based on naming conventions.
  • shellHooks are not inherited from dependencies. The only shellHook that will run is the one that is defined by the target of the nix develop invocation, which could be either the default package or the devShell, depending on what is defined by the flake.
  • setupHook will only run if listed as a dependency e.g. as buildInputs or packages entries. In other words, it sets up the the shell when other recipes depend on the package. One way to do this without defining a whole additional package is to use mkShell to and specify the package whose setupHook you want to run in packages. Again, you must use nix develop to run the hooks.

So my opinion is that nix develop is serving double duty right now, which means that neither can be used at the same time. I have to write my flake one way in order to get a development environment for testing the build steps of the flake, and I have to write my flake in another way to get a shell that properly sets the environment up for using the flake's offerings.

My assumption was that that nix develop would handle the former and nix shell would handle the latter. One workaround is that I could provided separate flake targets for each usage: e.g. nix develop . and nix develop .#deploy or use separate flakes entirely for the two different use cases.

@jrolfs
Copy link

jrolfs commented Sep 24, 2021

For anyone else coming just for the shell use case, the wiki has a nice example including direnv integration.

@milahu
Copy link
Contributor

milahu commented Nov 6, 2021

I want a particular shell environment so I can interactively inspect and iterate through the build phases of 1) above, which is what I'd expect from nix develop.

TLDR: in nix develop, how to run unpackPhase && configurePhase && buildPhase && ...?

compare:

@fluffynukeit
Copy link

TLDR: in nix develop, how to run unpackPhase && configurePhase && buildPhase && ...?

I was not asking how to do it. I simply was listing separate use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants