From bd7b92cf5741910de90fd929be3770a7f622c589 Mon Sep 17 00:00:00 2001 From: Yifei Sun Date: Mon, 28 Apr 2025 17:19:56 -0400 Subject: [PATCH] lib: add moduleLocFromOptionString --- default.nix | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/default.nix b/default.nix index d0103f62b..6ec4afe33 100644 --- a/default.nix +++ b/default.nix @@ -36,6 +36,45 @@ rec { if lib.isAttrs value then f (path + name + separator) value else { ${path + name} = value; }; in f ""; + + # get the path of NixOS module from string + # example: + # lib'.moduleLocFromOptionString "services.ntpd-rs" + # => "/nix/store/...-source/nixos/modules/services/networking/ntp/ntpd-rs.nix" + moduleLocFromOptionString = + let + inherit + (lib.evalModules { + class = "nixos"; + specialArgs.modulesPath = "${sources.nixpkgs}/nixos/modules"; + modules = [ + ({ + config = { + _module.check = false; + nixpkgs.hostPlatform = if builtins.isNull system then builtins.currentSystem else system; + }; + }) + ] ++ import "${sources.nixpkgs}/nixos/modules/module-list.nix"; + }) + options + ; + in + opt: + let + locList = lib.splitString "." opt; + optAttrs = lib.getAttrFromPath locList options; + + # collect all file paths from all options + collectFiles = + attrs: + let + # get value of `files` attr or empty list + getFiles = + attr: if attr.value ? files && builtins.isList attr.value.files then attr.value.files else [ ]; + in + lib.concatMap getFiles (lib.attrsToList attrs); + in + lib.head (collectFiles optAttrs); }; overlays.default =