diff --git a/clanServices/flake-module.nix b/clanServices/flake-module.nix index d4459b750..e107c2a98 100644 --- a/clanServices/flake-module.nix +++ b/clanServices/flake-module.nix @@ -1,8 +1,5 @@ -{ self, lib, ... }: { - clan.inventory.modules = { - zerotier-redux = lib.modules.importApply ./zerotier-redux/default.nix { - inherit (self) packages; - }; - }; + imports = [ + ./hello-world/flake-module.nix + ]; } diff --git a/clanServices/hello-world/default.nix b/clanServices/hello-world/default.nix new file mode 100644 index 000000000..01837d828 --- /dev/null +++ b/clanServices/hello-world/default.nix @@ -0,0 +1,8 @@ +{ packages }: +{ ... }: +{ + _class = "clan.service"; + manifest.name = "clan-core/hello-word"; + + roles.peer = { }; +} diff --git a/clanServices/hello-world/flake-module.nix b/clanServices/hello-world/flake-module.nix new file mode 100644 index 000000000..1dd50bc3f --- /dev/null +++ b/clanServices/hello-world/flake-module.nix @@ -0,0 +1,37 @@ +{ + self, + inputs, + lib, + ... +}: +let + module = lib.modules.importApply ./default.nix { + inherit (self) packages; + }; +in +{ + clan.inventory.modules = { + hello-world = module; + }; + perSystem = + { ... }: + let + # Module that contains the tests + # This module adds: + # - legacyPackages..eval-tests-hello-world + # - checks..eval-tests-hello-world + unit-test-module = ( + self.clanLib.test.flakeModules.makeEvalChecks { + inherit module; + inherit self inputs; + testName = "hello-world"; + tests = ./tests/eval-tests.nix; + # Optional arguments passed to the test + testArgs = { }; + } + ); + in + { + imports = [ unit-test-module ]; + }; +} diff --git a/clanServices/hello-world/tests/eval-tests.nix b/clanServices/hello-world/tests/eval-tests.nix new file mode 100644 index 000000000..daed19a5c --- /dev/null +++ b/clanServices/hello-world/tests/eval-tests.nix @@ -0,0 +1,53 @@ +{ + module, + clanLib, + ... +}: +let + testFlake = clanLib.buildClan { + # Point to the folder of the module + # TODO: make this optional in buildClan + directory = ./..; + + # Create some test machines + machines.jon = { + nixpkgs.hostPlatform = "x86_64-linux"; + }; + machines.sara = { + nixpkgs.hostPlatform = "x86_64-linux"; + }; + + # Register the module for the test + inventory.modules.hello-world = module; + + # Use the module in the test + inventory.instances = { + "hello" = { + module.name = "hello-world"; + + roles.peer.machines.jon = { }; + }; + }; + }; + # NOTE: + # If you wonder why 'self-zerotier-redux': + # A local module has prefix 'self', otherwise it is the name of the 'input' + # The rest is the name of the service as in the instance 'module.name'; + # + # -> ${module.input}-${module.name} + # In this case it is 'self-zerotier-redux' + # This is usually only used internally, but we can use it to test the evaluation of service module in isolation + # evaluatedService = + # testFlake.clanInternals.inventoryClass.distributedServices.importedModulesEvaluated.self-zerotier-redux.config; +in +{ + test_simple = { + inherit testFlake; + + expr = { + }; + expected = { + + }; + }; +} diff --git a/clanServices/zerotier-redux/default.nix b/clanServices/zerotier-redux/default.nix deleted file mode 100644 index 12206a53d..000000000 --- a/clanServices/zerotier-redux/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ packages }: -{ ... }: -{ - _class = "clan.service"; - manifest.name = "clan-core/zerotier"; - - # TODO: Migrate the behavior from nixosModules/clanCore/zerotier - # Expose a flag, to disable the clanCore/zerotier module if this module is used - # To ensure conflict free behavior - roles.moon = { }; - roles.peer = { }; - roles.controller = { }; -} diff --git a/lib/test/default.nix b/lib/test/default.nix index 072a45801..d6d140216 100644 --- a/lib/test/default.nix +++ b/lib/test/default.nix @@ -6,6 +6,9 @@ let ; in { + flakeModules = clanLib.callLib ./flakeModules.nix { }; + + # minifyModule = ./minify.nix; sopsModule = ./sops.nix; # A function that returns an extension to runTest diff --git a/lib/test/flakeModules.nix b/lib/test/flakeModules.nix new file mode 100644 index 000000000..0ab161f3f --- /dev/null +++ b/lib/test/flakeModules.nix @@ -0,0 +1,56 @@ +{ lib, clanLib }: +{ + + /** + This function is used to create a module that can be imported into flake-parts + and used to run eval tests. + + Usage: `lib.modules.importApply makeEvalChecks { module = module-under-test; testArgs = { }; }` + + returns a module to be imported into flake-parts + + Which in turn adds to the flake: + + - legacyPackages..eval-tests-: The attribute set passed to nix-unit. (Exposed for debugging i.e. via nix repl). + - checks..eval-tests-: A derivation that can be built and fails if nix-unit fails + */ + makeEvalChecks = + { + self, + inputs, + testName, + tests, + module, + testArgs ? { }, + }: + let + inputOverrides = builtins.concatStringsSep " " ( + builtins.map (input: " --override-input ${input} ${inputs.${input}}") (builtins.attrNames inputs) + ); + attrName = "eval-tests-${testName}"; + in + { + pkgs, + system, + ... + }: + { + legacyPackages.${attrName} = import tests ( + { + inherit clanLib lib module; + } + // testArgs + ); + checks.${attrName} = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' + export HOME="$(realpath .)" + + nix-unit --eval-store "$HOME" \ + --extra-experimental-features flakes \ + --show-trace \ + ${inputOverrides} \ + --flake ${self}#legacyPackages.${system}.${attrName} + touch $out + ''; + + }; +}