mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-13 05:05:29 +03:00
lib/modules: add class
to specialArgs
Co-Authored-By: Johannes Kirschbauer <hsjobeki@gmail.com>
This commit is contained in:
parent
c8cd81426f
commit
6de9039315
7 changed files with 105 additions and 1 deletions
|
@ -257,6 +257,7 @@ let
|
|||
config
|
||||
specialArgs
|
||||
;
|
||||
_class = class;
|
||||
}
|
||||
// specialArgs
|
||||
);
|
||||
|
|
|
@ -681,6 +681,18 @@ checkConfigOutput '^true$' config.viaConfig ./mkDefinition.nix
|
|||
checkConfigOutput '^true$' config.mkMerge ./mkDefinition.nix
|
||||
checkConfigOutput '^true$' config.mkForce ./mkDefinition.nix
|
||||
|
||||
# specialArgs._class
|
||||
checkConfigOutput '"nixos"' config.nixos.config.foo ./specialArgs-class.nix
|
||||
checkConfigOutput '"bar"' config.conditionalImportAsNixos.config.foo ./specialArgs-class.nix
|
||||
checkConfigError 'attribute .*bar.* not found' config.conditionalImportAsNixos.config.bar ./specialArgs-class.nix
|
||||
checkConfigError 'attribute .*foo.* not found' config.conditionalImportAsDarwin.config.foo ./specialArgs-class.nix
|
||||
checkConfigOutput '"foo"' config.conditionalImportAsDarwin.config.bar ./specialArgs-class.nix
|
||||
checkConfigOutput '"nixos"' config.sub.nixos.foo ./specialArgs-class.nix
|
||||
checkConfigOutput '"bar"' config.sub.conditionalImportAsNixos.foo ./specialArgs-class.nix
|
||||
checkConfigError 'attribute .*bar.* not found' config.sub.conditionalImportAsNixos.bar ./specialArgs-class.nix
|
||||
checkConfigError 'attribute .*foo.* not found' config.sub.conditionalImportAsDarwin.foo ./specialArgs-class.nix
|
||||
checkConfigOutput '"foo"' config.sub.conditionalImportAsDarwin.bar ./specialArgs-class.nix
|
||||
|
||||
cat <<EOF
|
||||
====== module tests ======
|
||||
$pass Pass
|
||||
|
|
3
lib/tests/modules/assert-module-class-is-nixos.nix
Normal file
3
lib/tests/modules/assert-module-class-is-nixos.nix
Normal file
|
@ -0,0 +1,3 @@
|
|||
{ _class, ... }:
|
||||
assert _class == "nixos";
|
||||
{ }
|
|
@ -5,7 +5,9 @@
|
|||
nixosOk = lib.mkOption {
|
||||
type = lib.types.submoduleWith {
|
||||
class = "nixos";
|
||||
modules = [ ];
|
||||
modules = [
|
||||
./assert-module-class-is-nixos.nix
|
||||
];
|
||||
};
|
||||
};
|
||||
# Same but will have bad definition
|
||||
|
@ -45,6 +47,7 @@
|
|||
class = "nixos";
|
||||
modules = [
|
||||
./module-class-is-nixos.nix
|
||||
./assert-module-class-is-nixos.nix
|
||||
];
|
||||
};
|
||||
|
||||
|
|
8
lib/tests/modules/expose-module-class.nix
Normal file
8
lib/tests/modules/expose-module-class.nix
Normal file
|
@ -0,0 +1,8 @@
|
|||
{ _class, lib, ... }:
|
||||
{
|
||||
options = {
|
||||
foo = lib.mkOption {
|
||||
default = _class;
|
||||
};
|
||||
};
|
||||
}
|
23
lib/tests/modules/polymorphic-module.nix
Normal file
23
lib/tests/modules/polymorphic-module.nix
Normal file
|
@ -0,0 +1,23 @@
|
|||
{ _class, lib, ... }:
|
||||
let
|
||||
nixosModule =
|
||||
{ ... }:
|
||||
{
|
||||
options.foo = lib.mkOption {
|
||||
default = "bar";
|
||||
};
|
||||
};
|
||||
darwinModule =
|
||||
{ ... }:
|
||||
{
|
||||
options.bar = lib.mkOption {
|
||||
default = "foo";
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
(lib.optionalAttrs (_class == "nixos") nixosModule)
|
||||
(lib.optionalAttrs (_class == "darwin") darwinModule)
|
||||
];
|
||||
}
|
54
lib/tests/modules/specialArgs-class.nix
Normal file
54
lib/tests/modules/specialArgs-class.nix
Normal file
|
@ -0,0 +1,54 @@
|
|||
{ lib, ... }:
|
||||
{
|
||||
options = {
|
||||
sub = {
|
||||
nixos = lib.mkOption {
|
||||
type = lib.types.submoduleWith {
|
||||
class = "nixos";
|
||||
modules = [
|
||||
./expose-module-class.nix
|
||||
];
|
||||
};
|
||||
default = { };
|
||||
};
|
||||
|
||||
conditionalImportAsNixos = lib.mkOption {
|
||||
type = lib.types.submoduleWith {
|
||||
class = "nixos";
|
||||
modules = [
|
||||
./polymorphic-module.nix
|
||||
];
|
||||
};
|
||||
default = { };
|
||||
};
|
||||
|
||||
conditionalImportAsDarwin = lib.mkOption {
|
||||
type = lib.types.submoduleWith {
|
||||
class = "darwin";
|
||||
modules = [
|
||||
./polymorphic-module.nix
|
||||
];
|
||||
};
|
||||
default = { };
|
||||
};
|
||||
};
|
||||
};
|
||||
config = {
|
||||
_module.freeformType = lib.types.anything;
|
||||
|
||||
nixos = lib.evalModules {
|
||||
class = "nixos";
|
||||
modules = [ ./expose-module-class.nix ];
|
||||
};
|
||||
|
||||
conditionalImportAsNixos = lib.evalModules {
|
||||
class = "nixos";
|
||||
modules = [ ./polymorphic-module.nix ];
|
||||
};
|
||||
|
||||
conditionalImportAsDarwin = lib.evalModules {
|
||||
class = "darwin";
|
||||
modules = [ ./polymorphic-module.nix ];
|
||||
};
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue