Glad to see someone else that’s dived into the evalModules source!
since a few years back
nixpkgs.lib implementation deserves more attention
I did a fair amount of work this summer to optimize key lib functions - which gave great results in lowering eval times. But more work still needs to be done.
@llakala has this work landed already?
I can’t actually follow the argument being presented in this link. I mean:
{ inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; outputs = inputs@{ self, nixpkgs }: { nixosModules.version = { inputs, ... }: { # DO NOT DO THIS THIS IS JUST AN EXAMPLE # read `system.stateVersion` docs very carefully system.stateVersion = with inputs.nixpkgs.lib; versions.majorMinor version; }; nixosConfigurations.works = nixpkgs.lib.nixosSystem { system = "aarch64-linux"; specialArgs = { inherit inputs; }; modules = [ self.nixosModules.version ]; }; nixosConfigurations.breaks = nixpkgs.lib.nixosSystem { system = "aarch64-linux"; modules = [ self.nixosModules.version ]; }; }; }[snip demo of how
nixosConfigurations.worksworks andnixosConfigurations.breaksdoesn’t]
… Now maybe you are convinced usingspecialArgsis a bad idea
That sure looks like specialArgs fixed a problem to me! How, then, is using it a bad idea?
Yes - in dozens of PRs. See the list of my merged PRs here.
Generally, I can vouch for the overall implementation of the most critical functions (mkDerivation, callPackage, etc). There are still wins to be made, but they require more work. Off the top of my head:
buildPythonPackagestill usesuniquefor dependencies, which is O(n^2) in nixpkgs. I tried to fix this withbuiltins.genericClosure, but behavior was still different, and I’m not totally sure why.- Our use of
extendDerivationis rather bad, and redoes a lot of work done bybuiltins.derivation. I have a PR that fixes this. mkDerivationstill supports nested lists for build inputs. I managed to get this deprecated last release, but if we can remove support for it altogether, we can do a single pass over build inputs instead of two. This is done in this PR.haskellPackagesis hellish. I have a PR that simply prevents us from redoing the same work over and over, and has very great results.- NixOS uses submodules too much. This PR by MattSturgeon would dramatically improve the situation.
If you have an itchy merging finger, check out the full list of my unmerged Nixpkgs PRs here :).
It’s bad for the modules you push downstream, because you have no idea what the downstream user’s inputs look like.
It created a problem. It’s especially egregious when you make public modules.
lib.importApply is the correct solution.
Yes. I personally see no problem with using inputs or self' or whatever as specialArgs in your personal config (copypasters may have to make adaptations to their specific config layout anyway), but PLEASE don’t depend on them in public modules.
The old REAPER config flake I used (before @9Prestidigitator’s excellent reaper-flake) had this issue.
apparently i was too slow to reply! thanks @waffle8946 and @bitbloxhub
but nonetheless i feel like the example i gave at the beginning is a bit too simplified
[
specialArgs] created a problem. It’s especially egregious when you make public modules.
I believe you, but is there a good writeup anywhere of what that problem is? Because it’s really not apparent from the discussion at the link.
Here: nixpkgs/lib/modules.nix at 6510408d147d8c5e6d76043e899e9251756f6a75 · NixOS/nixpkgs · GitHub
It’s only a problem in things intended to be used as libraries or otherwise depended on. It makes it impossible for the module system to interact with those args, so composition becomes awkward.
An example is e.g. flake-parts, where you write modules that need to depend on things internal to the defining module set but also declare things that may use context from the importing module. If it used specialArgs and expected users to add flake-parts modules into the module system that way that’d result in awkward UX (though even with importApply IMO that gets mind-bendy).
The way you probably use it (in flake.nix files describing NixOS configurations) OTOH is in fact exactly the intended use case of specialArgs; Alternatives cannot be used in imports, but that use case uses it precisely to import third-party NixOS modules.
I’m guessing this post means there are many non-leaf third-party projects that rely on specialArgs when they shouldn’t. I haven’t really noticed, personally, but if so that’s a nice PSA.
The issue here in this post is:
- The post title is overly generic, so the post addresses people who shouldn’t be worrying about this.
- The title is pretty inflammatory, so if you feel addressed you’re gonna read and comment (yay engagement, isn’t media wonderful).
- Flakes make it hard to distinguish between “leaf” and other use cases (see also: 1000-nixpkgs-instances,
follows,system, “dendritic”), which makes it easy to accidentally write a post that’s overly generic and inflammatory.- We also still lack a good public API to integrate flakes with NixOS that could help with this.
Is there a way to make this work when the subflake needs to access a value given by the host flake? I have a flake which currently requires users to pass self via specialArgs, which I’d be happy to remove if possible given the annoyance. I tried testing with importApply as shown here, but the module receives the self value of the subflake, not the host flake, which doesn’t work for this usecase.
For a situation like that, the I believe the canonically correct solution is to create an option, appropriately named to not clash with anything, and have the consumer supply the value through it.