Skip to content

Commit

Permalink
feat(modules): moduleNamespace more configurable (#69)
Browse files Browse the repository at this point in the history
* 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;`
  • Loading branch information
BirdeeHub authored Dec 7, 2024
1 parent ad531a8 commit 89be437
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 231 deletions.
4 changes: 4 additions & 0 deletions builder/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 0 additions & 16 deletions nix/flake.nix

This file was deleted.

5 changes: 5 additions & 0 deletions nixCatsHelp/nixCats_format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [ <packagename> ];
};
<

Expand Down
13 changes: 10 additions & 3 deletions nixCatsHelp/nixCats_modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ We create the module by exporting the following in our flake outputs.

<mkNixosModules> {
defaultPackageName = "nixCats";
moduleNamespace = [ "nixCats" ];
luaPath = "${./.}";
inherit nixpkgs dependencyOverlays
categoryDefinitions packageDefinitions extra_pkg_config;
};

<mkHomeModules> {
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,
Expand Down
30 changes: 15 additions & 15 deletions tests/home/main.nix
Original file line number Diff line number Diff line change
@@ -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 = [
];
Expand All @@ -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;
};
};
};
};
}
})

8 changes: 6 additions & 2 deletions tests/libT/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +47,7 @@
})
];
};
in hmcfg.config.${packagename}.out;
in lib.attrByPath ([ "config" ] ++ moduleNamespace ++ [ "out" ]) {} hmcfg;

mkNixOSmodulePkgs = {
package
Expand All @@ -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
Expand All @@ -82,6 +85,7 @@
specialArgs = {
inherit
packagename
moduleNamespace
pkgs
lib
inputs
Expand All @@ -90,7 +94,7 @@
;
};
};
in nixoscfg.config.${packagename}.out;
in lib.attrByPath ([ "config" ] ++ moduleNamespace ++ [ "out" ]) {} nixoscfg;

mkRunPkgTest = {
package,
Expand Down
35 changes: 17 additions & 18 deletions tests/nixos/main.nix
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
};
};
};
Expand Down
21 changes: 14 additions & 7 deletions utils/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -202,6 +206,7 @@ with builtins; let lib = import ./lib.nix; in rec {
, categoryDefinitions ? (_:{})
, packageDefinitions ? {}
, defaultPackageName
, moduleNamespace ? [ defaultPackageName ]
, nixpkgs ? null
, extra_pkg_config ? {}
, ... }:
Expand All @@ -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;
});

/**
Expand All @@ -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
Expand Down Expand Up @@ -272,6 +281,7 @@ with builtins; let lib = import ./lib.nix; in rec {
, categoryDefinitions ? (_:{})
, packageDefinitions ? {}
, defaultPackageName
, moduleNamespace ? [ defaultPackageName ]
, nixpkgs ? null
, extra_pkg_config ? {}
, ... }:
Expand All @@ -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;
});

/**
Expand Down Expand Up @@ -728,7 +738,4 @@ with builtins; let lib = import ./lib.nix; in rec {
in
listToAttrs (mapfunc allnames);

# DEPRECATED
inherit (lib) catsWithDefault;

}
Loading

0 comments on commit 89be437

Please sign in to comment.