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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@
}:
let
inherit (import ./ci { inherit nixpkgs system; }) pkgs;

# For `nix-shell -A hello`
curPkgs = builtins.removeAttrs (import ./. { inherit system; }) [
# Although this is what anyone may expect from a `_type = "pkgs"`,
# this file is intended to produce a shell in the first place,
# and a `_type` tag could confuse some code.
"_type"
];
in
pkgs.mkShellNoCC {
curPkgs
// pkgs.mkShellNoCC {
Comment on lines +29 to +30
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.

packages = with pkgs; [
# The default formatter for Nix code
# See https://github.com/NixOS/nixfmt
Expand Down