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

nix-shell -p is too magical #726

Closed
copumpkin opened this issue Dec 2, 2015 · 9 comments
Closed

nix-shell -p is too magical #726

copumpkin opened this issue Dec 2, 2015 · 9 comments
Assignees

Comments

@copumpkin
Copy link
Member

With nix-shell -A, I can pass it a .nix file to run expressions against, but I can't emulate that behavior easily with nix-shell -p. For example, nix-shell -p foo will install foo in my shell. If I run nix-shell '<nixpkgs>' -A foo, that doesn't give me the same thing. If I want something similar, @puffnfresh on IRC suggested this nasty thing:

nix-shell -E 'with import <nixpkgs> { }; runCommand "ignored" { buildInputs = [ (import ./.).foo ]; } ""'

which doesn't seem great. Can we have an easy way to get behavior like -p without relying on NIX_PATH voodoo or the incantation above?

@Ericson2314
Copy link
Member

A general nix question that might turn out to be relevant: is https://github.com/NixOS/nix/blob/master/corepkgs/buildenv.nix used just by nix-env or also used for making build environments?

@fkz
Copy link
Contributor

fkz commented Dec 2, 2015

The nix expression used is constructed here: https://github.com/NixOS/nix/blob/master/scripts/nix-build.in#L225

'with import <nixpkgs> { }; runCommand "shell" { buildInputs = [ '
. (join " ", map { "($_)" } @exprs) . ']; } ""');

@puffnfresh
Copy link
Contributor

@fkz thanks! Basically the same as I wrote above. 👍

@fkz
Copy link
Contributor

fkz commented Dec 2, 2015

Yes, so as an alternative to your suggestion, the following could be used instead:

nix-shell -p "(import ./.).foo"

This might still not be exactly what we want because we have to repeat this "import ./." for every single package we want to include. On the other hand, we can mix nixpkgs with our own, so things like the following are possible

nix-shell -p valgrind "import ./. {}"

@Ericson2314
Copy link
Member

The reason I ask my question is I wonder if nix-shell should by default just make an environment with the specified packages, and a CLI analogous to nix-env. There could be some function dependenciesOf that would pull the dependencies out of a derivation, maybe exposed as a flag too, that would recover the current behavior.

There is sort of a chicken-and-egg problem: "which is more fundamental to nix, setting up environments or building packages". I don't know what the answer is, but I think nix-shell should focus on exposing the more fundamental one whatever it is.

@puffnfresh
Copy link
Contributor

@fkz nice! I didn't realise that was possible. Means we can do things like:

nix-shell -p "haskellPackages.ghcWithPackages (p: [p.lens p.JuicyPixels])"

Thanks 👍

@gilligan
Copy link
Contributor

bump

I just hit a wall with nix-shell -p as well until @copumpkin told me that I could use

nix-shell -p "(import ./. {}).foo"

which was what I needed in my scenario.

@edolstra
Copy link
Member

Closing since nix run (the replacement for nix-shell -p) has more consistent behaviour.

573 referenced this issue in tweag/nickel Apr 8, 2021
When supplying a url to nix-shell:

nix-shell https://github/tweag/nickel/archive/master.tar.gz

It does not attempt to load the shell.nix file contained in the tarball, instead only loading default.nix, which fails if it doesn't provide a viable shell.

I would expect the behavior to be consistent with launching from the base of a repository, namely, load shell.nix if it exists, default.nix otherwise.

(see divnix/digga@e2172c8)
@AleXoundOS
Copy link

nix-shell -E 'with import <nixpkgs> { }; runCommand "ignored" { buildInputs = [ (import ./.).foo ]; } ""'

As an alternative, this works, though still quite verbose:

$ nix-shell -E 'with (import ./pkgs.nix); mkShell { buildInputs = with gst_all_1; [ gstreamer gst-plugins-base gst-plugins-good ]; }'

The aim here is to supply nixpkgs with project specific overlays to nix-shell with several packages. Whereas it works well for a single package (nix-shell -p '(import ./pkgs.nix).gst_all_1.gstreamer') matching expectations to be concise.


I imagine it could work simply like this:

$ nix-shell -p 'with (import ./pkgs.nix).gst_all_1; [ gstreamer gst-plugins-base gst-plugins-good ]'

Moreover, it enters the shell but $PATH is not properly set.

zolodev pushed a commit to zolodev/nix that referenced this issue Jan 1, 2024
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

9 participants