1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 23:03:53 -04:00
nix/src
Maximilian Bosch 98946e2d9c
nix-shell: restore backwards-compat with old nixpkgs
Basically an attempt to resume fixing #5543 for a breakage introduced
earlier[1]. Basically, when evaluating an older `nixpkgs` with
`nix-shell` the following error occurs:

    λ ma27 [~] → nix-shell -I nixpkgs=channel:nixos-18.03 -p nix
    error: anonymous function at /nix/store/zakqwc529rb6xcj8pwixjsxscvlx9fbi-source/pkgs/top-level/default.nix:20:1 called with unexpected argument 'inNixShell'

           at /nix/store/zakqwc529rb6xcj8pwixjsxscvlx9fbi-source/pkgs/top-level/impure.nix:82:1:

               81|
               82| import ./. (builtins.removeAttrs args [ "system" "platform" ] // {
                 | ^
               83|   inherit config overlays crossSystem;

This is a problem because one of the main selling points of Nix is that
you can evaluate any old Nix expression and still get the same result
(which also means that it *still evaluates*). In fact we're deprecating,
but not removing a lot of stuff for that reason such as unquoted URLs[2]
or `builtins.toPath`. However this property was essentially thrown away
here.

The change is rather simple: check if `inNixShell` is specified in the
formals of an auto-called function. This means that

    { inNixShell ? false }:
    builtins.trace inNixShell
      (with import <nixpkgs> { }; makeShell { name = "foo"; })

will show `trace: true` while

    args@{ ... }:
    builtins.trace args.inNixShell
      (with import <nixpkgs> { }; makeShell { name = "foo"; })

will throw the following error:

    error: attribute 'inNixShell' missing

This is explicitly needed because the function in
`pkgs/top-level/impure.nix` of e.g. NixOS 18.03 has an ellipsis[3], but
passes the attribute-set on to another lambda with formals that doesn't
have an ellipsis anymore (hence the error from above). This was perhaps
a mistake, but we can't fix it anymore. This also means that there's
AFAICS no proper way to check if the attr-set that's passed to the Nix
code via `EvalState::autoCallFunction` is eventually passed to a lambda
with formals where `inNixShell` is missing.

However, this fix comes with a certain price. Essentially every
`shell.nix` that assumes `inNixShell` to be passed to the formals even
without explicitly specifying it would break with this[4]. However I think
that this is ugly, but preferable:

* Nix 2.3 was declared stable by NixOS up until recently (well, it still
  is as long as 21.11 is alive), so most people might not have even
  noticed that feature.

* We're talking about a way shorter time-span with this change being
  in the wild, so the fallout should be smaller IMHO.

[1] 9d612c393a
[2] https://github.com/NixOS/rfcs/pull/45#issuecomment-488232537
[3] https://github.com/NixOS/nixpkgs/blob/release-18.03/pkgs/top-level/impure.nix#L75
[4] See e.g. the second expression in this commit-message or the changes
    for `tests/ca/nix-shell.sh`.
2022-06-13 23:29:28 +02:00
..
build-remote Generalize DerivationType in preparation for impure derivations 2022-03-18 14:59:56 +00:00
libcmd Mention that -f implies --impure for eval in docs 2022-06-10 19:16:17 +04:00
libexpr Shut up clang warnings 2022-06-02 21:19:54 +02:00
libfetchers fix sourcehut brach/tag resolving regression 2022-06-11 16:52:20 -03:00
libmain Avoid fmt when constructor already does it 2022-04-19 01:44:11 +00:00
libstore Merge pull request #6600 from asymmetric/hash-comment 2022-06-03 08:51:07 +02:00
libutil Fix incorrect comment in hiliteMatches 2022-06-05 20:30:18 +02:00
nix Fix missing ` in key manual 2022-06-08 11:46:50 +02:00
nix-build nix-shell: restore backwards-compat with old nixpkgs 2022-06-13 23:29:28 +02:00
nix-channel Fix segfault in headerCallback() 2022-03-03 11:11:16 +01:00
nix-collect-garbage Deduplicate the Store downcasting with a template 2022-03-11 13:32:33 +00:00
nix-copy-closure copyPaths: Pass store by reference 2021-07-22 09:59:51 +02:00
nix-env nix-env: A small std::move() optimization 2022-06-09 20:01:01 +05:30
nix-instantiate store Symbols in a table as well, like positions 2022-04-21 21:56:31 +02:00
nix-store nix-store: small std::move() optimization 2022-06-09 16:25:26 +05:30
resolve-system-dependencies Get rid of most .at calls (#6393) 2022-05-04 07:44:32 +02:00
toml11 Replace cpptoml with toml11 2021-12-17 22:03:33 +01:00