Add repl-overlays

Adds a `repl-overlays` option, which specifies files that can overlay
and modify the top-level bindings in `nix repl`. For example, with the
following contents in `~/.config/nix/repl.nix`:

    info: final: prev: let
      optionalAttrs = predicate: attrs:
        if predicate
        then attrs
        else {};
    in
      optionalAttrs (prev ? legacyPackages && prev.legacyPackages ? ${info.currentSystem})
      {
        pkgs = prev.legacyPackages.${info.currentSystem};
      }

We can run `nix repl` and use `pkgs` to refer to `legacyPackages.${currentSystem}`:

    $ nix repl --repl-overlays ~/.config/nix/repl.nix nixpkgs
    Nix 2.21.0pre20240309_4111bb6
    Type :? for help.
    Loading installable 'flake:nixpkgs#'...
    Added 5 variables.
    Loading 'repl-overlays'...
    Added 6 variables.
    nix-repl> pkgs.bash
    «derivation /nix/store/g08b5vkwwh0j8ic9rkmd8mpj878rk62z-bash-5.2p26.drv»

Change-Id: Ic12e0f2f210b2f46e920c33088dfe1083f42391a
This commit is contained in:
Rebecca Turner
2024-03-29 19:35:39 -07:00
parent abbd855e93
commit e2a1f79490
20 changed files with 352 additions and 5 deletions
@@ -0,0 +1 @@
info: final: prev: builtins.abort "uh oh!"
@@ -0,0 +1,6 @@
let
puppy = "doggy";
in
{currentSystem}: final: prev: {
inherit puppy;
}
@@ -0,0 +1 @@
info: final: prev: {}
@@ -0,0 +1,4 @@
info: final: prev:
{
pkgs = final.packages.x86_64-linux;
}
@@ -0,0 +1,7 @@
info: final: prev:
{
var = prev.var + "b";
# We can access the final value of `var` here even though it isn't defined yet:
varUsingFinal = "final value is: " + final.newVar;
}
@@ -0,0 +1,6 @@
info: final: prev:
{
var = prev.var + "c";
newVar = "puppy";
}