ngi-nix / ngipkgs

Nix packages and services for projects supported through the NGI program
https://ngi-nix.github.io/ngipkgs
MIT License
35 stars 18 forks source link

(re-)exposing upstream (nixpkgs) modules #329

Open stepbrobd opened 2 months ago

stepbrobd commented 2 months ago

Context: https://github.com/ngi-nix/ngipkgs/pull/305#discussion_r1654628524

TLDR:

Currently, in ngipkgs, we are re-exposing nixpkgs modules like this:

modules.services.forgejo = "${sources.inputs.nixpkgs}/nixos/modules/services/misc/forgejo.nix";

But the ideal way to expose those modules would be this:

modules.services.forejo = nixos.modules.services.forejo;

Proposed solution:

We can call evalModules on https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/module-list.nix (AFAIK this is where all default NixOS modules are imported from) and get options on the result, finally, use unsafeGetAttrPos to get the file location.

Am I missing anything? I want to double check here before I go on and implement this

stepbrobd commented 2 months ago

cc @fricklerhandwerk

fricklerhandwerk commented 2 months ago

This is in principle possible but likely very inefficient, already because by default the entire module list's import graph is traversed eagerly. I recommend doing a quick hack and running a comparison.

Also file location should be stored in the module value IIRC, please check the module system source.

stepbrobd commented 2 months ago

I tried running the following for a couple times, and the result seem to settle around 0.72s

$ time nix-instantiate --eval --expr 'let lib = import <nixpkgs/lib>; in (lib.evalModules { modules = (map (m: lib.setDefaultModuleLocation m m) (import (<nixpkgs> + "/nixos/modules/module-list.nix"))) ++ [{ nixpkgs.hostPlatform = builtins.currentSystem; }]; }).options' > /dev/null
nix-instantiate --eval --expr  > /dev/null  0.71s user 0.08s system 97% cpu 0.805 total

However, in the REPL, unsafeGetAttrPos seem to return null for all attrsets under options