From 89be437c0737ff0ad723a04358ea177544e66bcf Mon Sep 17 00:00:00 2001 From: Birdee <85372418+BirdeeHub@users.noreply.github.com> Date: Sat, 7 Dec 2024 04:45:17 -0800 Subject: [PATCH] feat(modules): moduleNamespace more configurable (#69) * feat(mkModules):moduleNamespace can specify [ "attr" "path" ] for the options for your module. utils.mkNixosModule and utils.mkHomeModule now optionally accept a list of strings as an attribute path to your module options, if you want the module options to be in a different namespace than `${defaultPackageName}.enable = true;` --- builder/default.nix | 4 + nix/flake.nix | 16 ---- nixCatsHelp/nixCats_format.txt | 5 ++ nixCatsHelp/nixCats_modules.txt | 13 +++- tests/home/main.nix | 30 ++++---- tests/libT/default.nix | 8 +- tests/nixos/main.nix | 35 +++++---- utils/default.nix | 21 ++++-- utils/lib.nix | 126 -------------------------------- utils/mkModules.nix | 51 +++++-------- utils/mkOpts.nix | 20 +++-- 11 files changed, 98 insertions(+), 231 deletions(-) delete mode 100644 nix/flake.nix diff --git a/builder/default.nix b/builder/default.nix index 004ece3..c930c81 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -51,6 +51,7 @@ let disablePythonSafePath = false; disablePythonPath = true; # <- you almost certainly want this set to true collate_grammars = true; + moduleNamespace = [ name ]; } // (thisPackage.settings or {}); final_cat_defs_set = ({ @@ -242,16 +243,19 @@ import ./wrapNeovim.nix { keepLuaBuilder = utils.baseBuilder luaPath; nixCats_packageName = name; inherit categoryDefinitions packageDefinitions dependencyOverlays luaPath utils; + inherit (settings) moduleNamespace; nixosModule = utils.mkNixosModules { defaultPackageName = name; inherit dependencyOverlays luaPath categoryDefinitions packageDefinitions extra_pkg_config nixpkgs; + inherit (settings) moduleNamespace; }; # and the same for home manager homeModule = utils.mkHomeModules { defaultPackageName = name; inherit dependencyOverlays luaPath categoryDefinitions packageDefinitions extra_pkg_config nixpkgs; + inherit (settings) moduleNamespace; }; }); inherit pkgs; diff --git a/nix/flake.nix b/nix/flake.nix deleted file mode 100644 index 91055b4..0000000 --- a/nix/flake.nix +++ /dev/null @@ -1,16 +0,0 @@ -{ - outputs = { ... }: let - utils = import ../.; - in { - utils = builtins.trace '' - Deprecation warning: github:BirdeeHub/nixCats-nvim?dir=nix flake input is being deprecated. - Please use github:BirdeeHub/nixCats-nvim instead. - This flake input will be removed before 2025 - '' utils; - templates = builtins.trace '' - Deprecation warning: github:BirdeeHub/nixCats-nvim?dir=nix flake input is being deprecated. - Please use github:BirdeeHub/nixCats-nvim instead. - This flake input will be removed before 2025 - '' utils.templates; - }; -} diff --git a/nixCatsHelp/nixCats_format.txt b/nixCatsHelp/nixCats_format.txt index 22c99ca..cbd8eae 100644 --- a/nixCatsHelp/nixCats_format.txt +++ b/nixCatsHelp/nixCats_format.txt @@ -704,6 +704,11 @@ These are the defaults: # It works great using https://github.com/nix-community/neovim-nightly-overlay # so this seems to be an upstream issue, as all the expected variables # are still being set correctly. + + # each package outputs a module via passthru. + # this will set the namespace for the module options + # by default will be at config.${packagename} + moduleNamespace = [ ]; }; < diff --git a/nixCatsHelp/nixCats_modules.txt b/nixCatsHelp/nixCats_modules.txt index 2900c21..69b765a 100644 --- a/nixCatsHelp/nixCats_modules.txt +++ b/nixCatsHelp/nixCats_modules.txt @@ -7,6 +7,7 @@ We create the module by exporting the following in our flake outputs. { defaultPackageName = "nixCats"; + moduleNamespace = [ "nixCats" ]; luaPath = "${./.}"; inherit nixpkgs dependencyOverlays categoryDefinitions packageDefinitions extra_pkg_config; @@ -14,14 +15,20 @@ We create the module by exporting the following in our flake outputs. { defaultPackageName = "nixCats"; + moduleNamespace = [ "nixCats" ]; luaPath = "${./.}"; inherit nixpkgs dependencyOverlays categoryDefinitions packageDefinitions extra_pkg_config; }; -where dependencyOverlays is a set of system names -with lists of overlays in each item. Such that -the lists would be accessed via dependencyOverlays.${system} +If moduleNamespace is omitted, it will default to `[ defaultPackageName ]` + +moduleNamespace controls the namespace for the module options. + +This means if `moduleNamespace = [ "my_mods" "nixCats" ];` + +Then you would `my_mods.nixCats.enable = true;` +and `my_mods.nixCats.packageNames = [ "package" "names" "toinstall" ];` If you do not have a luaPath, you may pass it a keepLua builder. utils.mkNixosModules exports a nixos module with the following options, diff --git a/tests/home/main.nix b/tests/home/main.nix index 46335f5..7b944c4 100644 --- a/tests/home/main.nix +++ b/tests/home/main.nix @@ -1,4 +1,4 @@ -{ config, pkgs, package, utils, lib, username, inputs, packagename, ... }@args: let +{ config, pkgs, package, utils, lib, username, inputs, packagename, moduleNamespace, ... }@args: let in { imports = [ ]; @@ -17,21 +17,21 @@ in { ]; home.file = { }; - - # nixCats module: - ${packagename} = { - enable = true; - # packageNames = [ packagename ]; - packageDefinitions.replace = { - ${packagename} = { pkgs, ... }: { - settings = { - }; - categories = { - nixCats_test_names = { - home_hello = true; - }; +} // +# nixCats module: +(lib.setAttrByPath moduleNamespace { + enable = true; + # packageNames = [ packagename ]; + packageDefinitions.replace = { + ${packagename} = { pkgs, ... }: { + settings = { + }; + categories = { + nixCats_test_names = { + home_hello = true; }; }; }; }; -} +}) + diff --git a/tests/libT/default.nix b/tests/libT/default.nix index 8c85780..d8269a4 100644 --- a/tests/libT/default.nix +++ b/tests/libT/default.nix @@ -20,11 +20,13 @@ , ... }: let packagename = package.nixCats_packageName; + moduleNamespace = package.moduleNamespace; hmcfg = inputs.home-manager.lib.homeManagerConfiguration { inherit pkgs; extraSpecialArgs = { inherit packagename + moduleNamespace username package inputs @@ -45,7 +47,7 @@ }) ]; }; - in hmcfg.config.${packagename}.out; + in lib.attrByPath ([ "config" ] ++ moduleNamespace ++ [ "out" ]) {} hmcfg; mkNixOSmodulePkgs = { package @@ -57,6 +59,7 @@ # so we use lib.evalModules and make our own options to set # to mirror the ones the nixCats module uses. packagename = package.nixCats_packageName; + moduleNamespace = package.moduleNamespace; nixoscfg = inputs.nixpkgs.lib.evalModules { modules = [ entrymodule @@ -82,6 +85,7 @@ specialArgs = { inherit packagename + moduleNamespace pkgs lib inputs @@ -90,7 +94,7 @@ ; }; }; - in nixoscfg.config.${packagename}.out; + in lib.attrByPath ([ "config" ] ++ moduleNamespace ++ [ "out" ]) {} nixoscfg; mkRunPkgTest = { package, diff --git a/tests/nixos/main.nix b/tests/nixos/main.nix index 1131e86..12d45c1 100644 --- a/tests/nixos/main.nix +++ b/tests/nixos/main.nix @@ -1,5 +1,19 @@ -{ config, pkgs, package, utils, lib, inputs, packagename, ... }@args: { - ${packagename} = { +{ config, pkgs, package, utils, lib, inputs, packagename, moduleNamespace, ... }@args: +lib.setAttrByPath moduleNamespace { + enable = true; + packageNames = [ packagename ]; + packageDefinitions.replace = { + ${packagename} = { pkgs, ... }: { + settings = { + }; + categories = { + nixCats_test_names = { + nixos_hello = true; + }; + }; + }; + }; + users.testuser = { enable = true; packageNames = [ packagename ]; packageDefinitions.replace = { @@ -8,22 +22,7 @@ }; categories = { nixCats_test_names = { - nixos_hello = true; - }; - }; - }; - }; - users.testuser = { - enable = true; - packageNames = [ packagename ]; - packageDefinitions.replace = { - ${packagename} = { pkgs, ... }: { - settings = { - }; - categories = { - nixCats_test_names = { - nixos_user_hello = true; - }; + nixos_user_hello = true; }; }; }; diff --git a/utils/default.nix b/utils/default.nix index fb9fe93..189b3e7 100644 --- a/utils/default.nix +++ b/utils/default.nix @@ -166,7 +166,11 @@ with builtins; let lib = import ./lib.nix; in rec { `defaultPackageName` (`string`) : the only truly required argument - : controls the namespace of the generated module and the default package installed + : by default controls the namespace of the generated module and the default package installed + + `moduleNamespace` ('listOf string') + : can be used to override the namespace of the module options + : `[ "programs" "nixCats" ]` would mean options like `programs.nixCats.enable = true` `dependencyOverlays` (`listOf overlays` or `attrsOf (listOf overlays)` or `null`) : default = null @@ -202,6 +206,7 @@ with builtins; let lib = import ./lib.nix; in rec { , categoryDefinitions ? (_:{}) , packageDefinitions ? {} , defaultPackageName + , moduleNamespace ? [ defaultPackageName ] , nixpkgs ? null , extra_pkg_config ? {} , ... }: @@ -211,7 +216,7 @@ with builtins; let lib = import ./lib.nix; in rec { nclib = lib; utils = import ./.; inherit nixpkgs luaPath keepLuaBuilder categoryDefinitions - packageDefinitions defaultPackageName extra_pkg_config; + packageDefinitions defaultPackageName extra_pkg_config moduleNamespace; }); /** @@ -236,7 +241,11 @@ with builtins; let lib = import ./lib.nix; in rec { `defaultPackageName` (`string`) : the only truly required argument - : controls the namespace of the generated module and the default package installed + : by default controls the namespace of the generated module and the default package installed + + `moduleNamespace` ('listOf string') + : can be used to override the namespace of the module options + : `[ "programs" "nixCats" ]` would mean options like `programs.nixCats.enable = true` `dependencyOverlays` (`listOf overlays` or `attrsOf (listOf overlays)` or `null`) : default = null @@ -272,6 +281,7 @@ with builtins; let lib = import ./lib.nix; in rec { , categoryDefinitions ? (_:{}) , packageDefinitions ? {} , defaultPackageName + , moduleNamespace ? [ defaultPackageName ] , nixpkgs ? null , extra_pkg_config ? {} , ... }: @@ -281,7 +291,7 @@ with builtins; let lib = import ./lib.nix; in rec { nclib = lib; utils = import ./.; inherit nixpkgs luaPath keepLuaBuilder categoryDefinitions - packageDefinitions defaultPackageName extra_pkg_config; + packageDefinitions defaultPackageName extra_pkg_config moduleNamespace; }); /** @@ -728,7 +738,4 @@ with builtins; let lib = import ./lib.nix; in rec { in listToAttrs (mapfunc allnames); - # DEPRECATED - inherit (lib) catsWithDefault; - } diff --git a/utils/lib.nix b/utils/lib.nix index 421b3d1..2565752 100644 --- a/utils/lib.nix +++ b/utils/lib.nix @@ -72,130 +72,4 @@ with builtins; rec { ); in f [] [rhs lhs]; - - # DEPRECATED - catsWithDefault = categories: attrpath: defaults: subcategories: let - filterAttrsRecursive = - pred: - set: - listToAttrs ( - concatMap (name: - let v = set.${name}; in - if pred name v then [ - (nameValuePair name ( - if isAttrs v then filterAttrsRecursive pred v - else v - )) - ] else [] - ) (attrNames set) - ); - - attrByPath = - attrPath: - default: - set: - let - lenAttrPath = length attrPath; - attrByPath' = n: s: ( - if n == lenAttrPath then s - else ( - let - attr = elemAt attrPath n; - in - if s ? ${attr} then attrByPath' (n + 1) s.${attr} - else default - ) - ); - in - attrByPath' 0 set; - - setAttrByPath = - attrPath: - value: - let - len = length attrPath; - atDepth = n: - if n == len - then value - else { ${elemAt attrPath n} = atDepth (n + 1); }; - in atDepth 0; - - mapAttrsRecursiveCond = - cond: - f: - set: - let - recurse = path: - mapAttrs - (name: value: - if isAttrs value && cond value - then recurse (path ++ [ name ]) value - else f (path ++ [ name ]) value); - in - recurse [ ] set; - - - include_path = let - flattener = cats: let - mapper = attrs: map (v: if isAttrs v then mapper v else v) (attrValues attrs); - flatten = accum: LoLoS: foldl' (acc: v: if any (i: isList i) v then flatten acc v else acc ++ [ v ]) accum LoLoS; - in flatten [] (mapper cats); - - mapToSetOfPaths = cats: let - removeNullPaths = attrs: filterAttrsRecursive (n: v: v != null) attrs; - mapToPaths = attrs: mapAttrsRecursiveCond (as: ! isDerivation as) (path: v: if v == true then path else null) attrs; - in removeNullPaths (mapToPaths cats); - - result = let - final_cats = attrByPath attrpath false categories; - allIncPaths = flattener (mapToSetOfPaths final_cats); - in if isAttrs final_cats && ! isDerivation final_cats && allIncPaths != [] - then head allIncPaths - else []; - in - result; - - toMerge = let - firstGet = if isAttrs subcategories && ! isDerivation subcategories - then attrByPath include_path [] subcategories - else if isList subcategories then subcategories else [ subcategories ]; - - fIncPath = if isAttrs firstGet && ! isDerivation firstGet - then include_path ++ [ "default" ] else include_path; - - normed = let - listType = if isAttrs firstGet && ! isDerivation firstGet - then attrByPath fIncPath [] subcategories - else if isList firstGet then firstGet else [ firstGet ]; - attrType = let - pre = if isAttrs firstGet && ! isDerivation firstGet - then attrByPath fIncPath {} subcategories - else firstGet; - basename = if fIncPath != [] then tail fIncPath else "default"; - fin = if isAttrs pre && ! isDerivation pre then pre else { ${basename} = pre; }; - in - fin; - in - if isList defaults then listType else if isAttrs defaults then attrType else throw "defaults must be a list or a set"; - - final = setAttrByPath fIncPath (if isList defaults then normed ++ defaults else { inherit normed; default = defaults; }); - in - builtins.trace '' - nixCats.utils.catsWithDefault is being deprecated, due to be removed before 2025. - - It is being removed due to not playing well - with merging categoryDefinitions together. - - A new, more capable method has been added. - - To create default values, use extraCats section of categoryDefinitions - as outlined in :h nixCats.flake.outputs.categoryDefinitions.default_values, - and demonstrated in the main example template - '' final; - - in - if isAttrs subcategories && ! isDerivation subcategories then - recUpdateHandleInlineORdrv subcategories toMerge - else toMerge; - } diff --git a/utils/mkModules.nix b/utils/mkModules.nix index f377bc7..128ecf2 100644 --- a/utils/mkModules.nix +++ b/utils/mkModules.nix @@ -3,6 +3,7 @@ { isHomeManager , defaultPackageName + , moduleNamespace ? [ defaultPackageName ] , oldDependencyOverlays ? null , luaPath ? "" , keepLuaBuilder ? null @@ -17,7 +18,7 @@ { config, pkgs, lib, ... }: { imports = [ (import ./mkOpts.nix { - inherit nclib defaultPackageName luaPath packageDefinitions isHomeManager; + inherit nclib defaultPackageName luaPath packageDefinitions isHomeManager moduleNamespace; }) ]; config = let @@ -30,7 +31,7 @@ pkgs.overlays ++ [(utils.mergeOverlayLists oldDependencyOverlays overlaylists)] else pkgs.overlays ++ overlaylists; - mapToPackages = options_set: dependencyOverlays: atp: (let + mapToPackages = options_set: dependencyOverlays: (let getStratWithExisting = enumstr: if enumstr == "merge" then utils.deepmergeCats else if enumstr == "replace" @@ -53,19 +54,7 @@ in oldAttrs // merged; stratWithExisting = getStratWithExisting options_set.packageDefinitions.existing; - modulePkgDefs = let - # TODO: this `repments` step can be removed when options_set.packages is removed - # In addition, `mapToPackages` will once again no longer need to know its attrpath - repments = if options_set.packages != null then builtins.trace (let - basepath = builtins.concatStringsSep "." atp; - in '' - Deprecation warning: ${basepath}.packages renamed to: ${basepath}.packageDefinitions.replace - Done in order to achieve consistency with ${basepath}.categoryDefinitions module options, and provide better control - Old option will be removed before 2025 - '') (pkgmerger utils.mergeCatDefs options_set.packages options_set.packageDefinitions.replace) - else options_set.packageDefinitions.replace; - in - pkgmerger utils.deepmergeCats repments options_set.packageDefinitions.merge; + modulePkgDefs = pkgmerger utils.deepmergeCats options_set.packageDefinitions.replace options_set.packageDefinitions.merge; in pkgmerger stratWithExisting packageDefinitions modulePkgDefs; newLuaBuilder = (if options_set.luaPath != "" then (utils.baseBuilder options_set.luaPath) @@ -76,8 +65,8 @@ newNixpkgs = if options_set.nixpkgs_version != null then options_set.nixpkgs_version - else if config.${defaultPackageName}.nixpkgs_version != null - then config.${defaultPackageName}.nixpkgs_version + else if lib.attrByPath (moduleNamespace ++ [ "nixpkgs_version" ]) null config != null + then lib.attrByPath (moduleNamespace ++ [ "nixpkgs_version" ]) null config else if nixpkgs != null then nixpkgs else pkgs; @@ -93,36 +82,32 @@ { name = catName; value = boxedCat; }) options_set.packageNames)) ); - main_options_set = config.${defaultPackageName}; - mappedPackageAttrs = mapToPackages main_options_set (dependencyOverlaysFunc { inherit main_options_set;}) [ "${defaultPackageName}" ]; + main_options_set = lib.attrByPath moduleNamespace {} config; + mappedPackageAttrs = mapToPackages main_options_set (dependencyOverlaysFunc { inherit main_options_set;}); mappedPackages = builtins.attrValues mappedPackageAttrs; in - (if isHomeManager then { - ${defaultPackageName}.out.packages = lib.mkIf main_options_set.enable mappedPackageAttrs; + (if isHomeManager then (lib.setAttrByPath (moduleNamespace ++ [ "out" ]) { + packages = lib.mkIf main_options_set.enable mappedPackageAttrs; + }) // { home.packages = lib.mkIf (main_options_set.enable && ! main_options_set.dontInstall) mappedPackages; } else (let - newUserPackageOutputs = builtins.mapAttrs ( uname: _: let - user_options_set = config.${defaultPackageName}.users.${uname}; - in { + userops = lib.attrByPath (moduleNamespace ++ [ "users" ]) {} config; + newUserPackageOutputs = builtins.mapAttrs ( uname: user_options_set: { packages = lib.mkIf user_options_set.enable (mapToPackages user_options_set (dependencyOverlaysFunc { inherit main_options_set user_options_set; }) - [ defaultPackageName "users" uname ] ); } - ) config.${defaultPackageName}.users; - newUserPackageDefinitions = builtins.mapAttrs ( uname: _: let - user_options_set = config.${defaultPackageName}.users.${uname}; - in { + ) userops; + newUserPackageDefinitions = builtins.mapAttrs ( uname: user_options_set: { packages = lib.mkIf (user_options_set.enable && ! user_options_set.dontInstall) (builtins.attrValues newUserPackageOutputs.${uname}.packages); } - ) config.${defaultPackageName}.users; - in { - ${defaultPackageName}.out = { + ) userops; + in (lib.setAttrByPath (moduleNamespace ++ [ "out" ]) { users = newUserPackageOutputs; packages = lib.mkIf main_options_set.enable mappedPackageAttrs; - }; + }) // { users.users = newUserPackageDefinitions; environment.systemPackages = lib.mkIf (main_options_set.enable && ! main_options_set.dontInstall) mappedPackages; })); diff --git a/utils/mkOpts.nix b/utils/mkOpts.nix index f33ead7..16bd8d1 100644 --- a/utils/mkOpts.nix +++ b/utils/mkOpts.nix @@ -3,6 +3,7 @@ { isHomeManager , defaultPackageName + , moduleNamespace ? [ defaultPackageName ] , luaPath ? "" , packageDefinitions ? {} , nclib @@ -12,9 +13,7 @@ pkgDef = nclib.mkCatDefType lib.mkOptionType true; in { - options = with lib; { - - ${defaultPackageName} = { + options = with lib; lib.setAttrByPath moduleNamespace ({ nixpkgs_version = mkOption { default = null; @@ -42,7 +41,7 @@ in { enable = mkOption { default = false; type = types.bool; - description = "Enable the ${defaultPackageName} module"; + description = "Enable the ${concatStringsSep "." moduleNamespace} module"; }; dontInstall = mkOption { @@ -50,7 +49,7 @@ in { type = types.bool; description = '' If true, do not output to packages list, - output only to config.${defaultPackageName}.out + output only to config.${concatStringsSep "." moduleNamespace}.out ''; }; @@ -253,7 +252,7 @@ in { enable = mkOption { default = false; type = types.bool; - description = "Enable the ${defaultPackageName} module for a user"; + description = "Enable the ${concatStringsSep "." moduleNamespace}.users module for a user"; }; dontInstall = mkOption { @@ -261,7 +260,7 @@ in { type = types.bool; description = '' If true, do not output to packages list, - output only to config.${defaultPackageName}.out + output only to config.${concatStringsSep "." moduleNamespace}.out.users ''; }; @@ -270,7 +269,7 @@ in { type = types.nullOr (types.anything); description = '' a different nixpkgs import to use for this users nvim. - By default will use the one from ${defaultPackageName}.nixpkgs_version, or flake, or system pkgs. + By default will use the one from ${concatStringsSep "." moduleNamespace}.nixpkgs_version, or flake, or system pkgs. ''; example = '' nixpkgs_version = inputs.nixpkgs @@ -284,7 +283,7 @@ in { A list of overlays to make available to this user's nixCats packages from this module but not to your system. Will have access to system overlays regardless of this setting. - This per user version of addOverlays is merged with the value of ${defaultPackageName}.addOverlays + This per user version of addOverlays is merged with the value of ${concatStringsSep "." moduleNamespace}.addOverlays ''; example = '' addOverlays = [ (self: super: { nvimPlugins = { pluginDerivationName = pluginDerivation; }; }) ] @@ -455,7 +454,6 @@ in { }; }); }; - }); - }; + })); }