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

shell.nix: Support nix-shell -A #353240

Merged
merged 1 commit into from
Nov 6, 2024
Merged

Conversation

roberth
Copy link
Member

@roberth roberth commented Nov 2, 2024

It used to be that nix-shell -A hello would launch the build shell for the hello package.
By adding /shell.nix, that stopped working, as all versions of nix-shell resolve the unspecified file to $PWD/shell.nix if it exists, and now it does.

I have to admit that this use of // is not pretty, but the UX/DX is hard to match.

Things done

Tested nix-shell and nix-shell -A hello.

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

It used to be that `nix-shell -A hello` would launch the build shell
for the `hello` package.
By adding `/shell.nix`, that stopped working, as all versions of
`nix-shell` resolve the unspecified file to `$PWD/shell.nix` if it
exists, and now it does.

I have to admit that this use of `//` is not pretty, but the UX/DX
hard to match.
@Frontear
Copy link
Member

Frontear commented Nov 2, 2024

Will test this in a couple hours!

@emilazy emilazy requested review from K900 and alyssais November 2, 2024 23:22
@K900
Copy link
Contributor

K900 commented Nov 3, 2024

I hate it, but I don't hate it conceptually.

@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux labels Nov 3, 2024
@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one reputable person label Nov 3, 2024
Copy link
Contributor

@MattSturgeon MattSturgeon left a comment

Choose a reason for hiding this comment

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

I agree this is ugly, but I can't think of any better way to achieve the desired behaviour.

Thanks for working on this 🚀

Comment on lines +29 to +30
curPkgs
// pkgs.mkShellNoCC {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better/worse to use stdenv's passthru for this instead of a // update?

I.e. passthru = curPkgs

Copy link
Member

Choose a reason for hiding this comment

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

From my testing this seems to do the exact same thing. I feel like passthru is a slightly nicer alternative to avoid potentially attrset shadowing from //, not that mkShellNoCC and pkgs have anything in common now, but maybe in the future?

Copy link
Member Author

Choose a reason for hiding this comment

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

They're similar, except for two differences:

  • the effect of pkg // foo is not preserved in the return value of pkg.overrideAttrs or any other such functions that re-evaluate the package function
  • // is simple and not subject to change

For these reasons I would

  • use passthru in cases where the new attributes relate strongly to the package (in this case the shell) and if the new attrs should be preserved in the return value of overrideAttrs
  • use // in cases where the addition is circumstantial. Another example would be testing a static build of a package in the tests attribute. Using passthru for that would create an expectation that overrides are applied to that test, but they're not, so it'd be better for such a tests test to be omitted after overrideAttrs.

In this case, calling overrideAttrs only needs to produce a shell, and not the whole package set, so we don't need passthru.
I also think // is nicer because it makes it clear which attrset wins when there's a conflict; the shell, and we don't need to check how mkDerivation works or test it to confirm.

@wegank wegank added 12.approvals: 2 This PR was reviewed and approved by two reputable people and removed 12.approvals: 1 This PR was reviewed and approved by one reputable person labels Nov 3, 2024
Copy link
Member

@Frontear Frontear left a comment

Choose a reason for hiding this comment

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

Changes work as expected, LGTM!

@wegank wegank added 12.approvals: 3+ This PR was reviewed and approved by three or more reputable people and removed 12.approvals: 2 This PR was reviewed and approved by two reputable people labels Nov 4, 2024
@roberth roberth merged commit 603cc87 into NixOS:master Nov 6, 2024
38 checks passed
@roberth
Copy link
Member Author

roberth commented Nov 6, 2024

Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux 12.approvals: 3+ This PR was reviewed and approved by three or more reputable people
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants