diff --git a/lix/nix/repl.md b/lix/nix/repl.md index fdb310b9c..a8776dd90 100644 --- a/lix/nix/repl.md +++ b/lix/nix/repl.md @@ -83,4 +83,69 @@ On startup, it loads the Nix expressions named *files* and adds them into the lexical scope. You can load addition files using the `:l ` command, or reload all files using `:r`. +# Adding default variables in REPL sessions + +It is possible to automatically load variables from a list of files +into each new REPL session using the +[`repl-overlays`](@docroot@/command-ref/conf-file.html#conf-repl-overlays) +configuration option. + +Each file should contain a Nix function taking three taking and +returning an +[attribute set](@docroot@/language/values.html#attribute-set). +These three arguments are: + +1. An [attribute set](@docroot@/language/values.html#attribute-set) + containing at least a `currentSystem` attribute (this is identical + to + [`builtins.currentSystem`](@docroot@/language/builtin-constants.md#builtins-currentSystem), + except that it's available in + [`pure-eval`](@docroot@/command-ref/conf-file.html#conf-pure-eval) + mode). +2. The top-level bindings produced by the previous `repl-overlays` + value (or the default top-level bindings). +3. The final top-level bindings produced by calling all + `repl-overlays`. + +## Examples + +* Aliasing `legacyPackages.${currentSystem}` to `pkgs` + + A file named `/home/alice/my-overlays.nix` containing the following code + would, if `legacyPackages` exists, add a variable named `pkgs` into + the global REPL scope, which returns the value of + `legacyPackages.${currentSystem}`. + + ```nix + info: final: prev: + if prev ? legacyPackages + && prev.legacyPackages ? ${info.currentSystem} + then + { + pkgs = prev.legacyPackages.${info.currentSystem}; + } + else + { } + ``` + + This file can be loaded automatically for every REPL session by + adding it to the value of + [`repl-overlays`](@docroot@/command-ref/conf-file.html#conf-repl-overlays) + inside `nix.conf`. For a single session, it is possible to add it + using `--option repl-overlays /home/alice/my-overlay.nix`: + + ```console + # nix repl --option repl-overlays /home/alice/my-overlay.nix nixpkgs + + nix-repl> pkgs == legacyPackages.${builtins.currentSystem} + true + + nix-repl> pkgs.hello + «derivation /nix/store/qdzln99hynf92vrz8sz91hlf1dmb1vdy-hello-2.12.2.drv» + ``` + +See the `nix.conf` +[`repl-overlays`](@docroot@/command-ref/conf-file.html#conf-repl-overlays) +documentation for more information. + )""