-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add repl-overlays
setting
#10203
base: master
Are you sure you want to change the base?
Add repl-overlays
setting
#10203
Changes from all commits
0f4c55a
4d725fc
751960c
237ae78
34a73c9
84cca7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
synopsis: Add `repl-overlays` option | ||
prs: 10203 | ||
--- | ||
|
||
A `repl-overlays` option has been added, 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`: | ||
|
||
```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}`: | ||
|
||
```ShellSession | ||
$ 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» | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
info: | ||
initial: | ||
functions: | ||
let final = builtins.foldl' | ||
(prev: function: prev // (function info final prev)) | ||
initial | ||
functions; | ||
in final |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,42 @@ struct EvalSettings : Config | |
|
||
This is useful for debugging warnings in third-party Nix code. | ||
)"}; | ||
|
||
PathsSetting replOverlays{this, Paths(), "repl-overlays", | ||
R"( | ||
A list of files containing Nix expressions that can be used to add | ||
default bindings to [`nix | ||
repl`](@docroot@/command-ref/new-cli/nix3-repl.md) sessions. | ||
|
||
Each file is called with three arguments: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The help text documents what the arguments are, but not what the function is supposed to return. |
||
1. An [attribute set](@docroot@/language/values.html#attribute-set) | ||
containing a | ||
[`currentSystem`](@docroot@/language/builtin-constants.md#builtins-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`. | ||
|
||
For example, the following file would alias `pkgs` to | ||
`legacyPackages.${info.currentSystem}` (if that attribute is defined): | ||
|
||
```nix | ||
info: prev: final: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having three positional arguments, of which one is an attrset with fields like
This would make it easier to add more fields in the future. |
||
if prev ? legacyPackages | ||
&& prev.legacyPackages ? ${info.currentSystem} | ||
then | ||
{ | ||
pkgs = prev.legacyPackages.${info.currentSystem}; | ||
} | ||
else | ||
{ } | ||
``` | ||
)"}; | ||
}; | ||
|
||
extern EvalSettings evalSettings; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the interest of convention over configuration, it would be good if this had a default value like
~/.nix/config/repl.nix
.