-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
nixos: allow overriding lib #51109
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
nixos: allow overriding lib #51109
Conversation
Allows overriding the `lib` argument used in configurations, which in turn allows using library functions while working with imports, like e.g.: ``` { lib, ... }: { imports = lib.my-imports-generator; } ``` The same wouldn't be possible with `pkgs.lib`, as it would cause an infinite recursion.
@@ -7,12 +16,18 @@ let | |||
eval = import ./lib/eval-config.nix { | |||
inherit system; | |||
modules = [ configuration ]; | |||
specialArgs = { | |||
inherit lib; | |||
}; |
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.
eval-config.nix
takes a lib
argument which you can use instead.
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.
Except that lib
would then not propagate to the module arguments like you might expect. That argument is there as an optimization. In my lib overlays prototype I made that argument work as you expect, but this requires changes to lib/modules.nix
, e.g. like this: { inherit config options; lib = args.lib or lib; } // specialArgs
.
{ configuration ? import ./lib/from-env.nix "NIXOS_CONFIG" <nixos-config> | ||
, lib ? if builtins.any looks-like-nixos-lib builtins.nixPath | ||
then import <nixos-lib> | ||
else import ../lib |
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.
Oh actually, we can just use lib ? import <nixpkgs/lib>
here. Then users can override it by setting
NIX_PATH=nixpkgs/lib=/path/to/lib:nixpkgs=/path/to/nixpkgs
This is then a single-line change pretty much accomplishing the same :)
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.
Rather than setting lib like this, I think they would prefer to use the modular way (as mentioned in various comments), setting _module.args.lib
. This requires some changes to lib/modules.nix
to not make lib a special case any more, but I think this would be more in line with what we already have for e.g. pkgs.
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.
I wasn't aware nixpkgs/lib
can be overridden ❤️
I was planning on writing a PR to extend lib myself when @infinisil mentioned this PR. I experimented with making overlays for lib as we have for nixpkgs, which works, but like you mentioned they would have to be defined outside of your configuration, because most modules tend to include lib inside the scope at the top of the file (i.e. I was using I have some other suggestions, but I better place them at the source code directly. |
@ottidmes, |
One downside I'm seeing with overriding import <nixpkgs/lib> // {
mystuff = "...";
} Now this causes an infinite recursion, and the only solution I'm seeing is to reimplement EDIT import <nixpkgs/nixos/../lib> // {
mystuff = "...";
} I'm going to drop d145173 from this PR unless there's a reason to keep the redirection in |
The idea behind the redirection was that there are still many places in nixpkgs that references nixpkgs/lib directly via path imports (search nixpkgs with this regex: |
Now, after trying to actually use an overridden
Therefore I'm reverting this PR to the original state again. I'm not sure the implementation is the best possible, but having a separate path like |
I get the issues you have with |
@@ -1,4 +1,13 @@ | |||
let | |||
looks-like-nixos-lib = { path, prefix }: |
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.
camelCase
please.
Not really in favor of adding another ad-hoc override mechanism. Also, in the future, the Nix search path is likely to be deprecated in favor of flakes, where the Nixpkgs library may well be factored out into a separate flake, which would allow it to be overriden easily. |
I will eagerly await more news about these flakes! In the meantime I made a different proposal (#51797) on how to extend lib, which while still ad hoc, tries to address the problems I had with this proposal. |
Looking forward to seeing flakes. |
Motivation for this change
Allows overriding the
lib
argument used in configurations, which in turn allows using library functions while working with imports, like e.g.:The same wouldn't be possible with
pkgs.lib
, as it would cause an infinite recursion.I'm not sure this is the best way to implement this feature.
Things done
sandbox
innix.conf
on non-NixOS)nix-shell -p nox --run "nox-review wip"
./result/bin/
)nix path-info -S
before and after)