diff --git a/checks/default.nix b/checks/default.nix index 0e3bebb..bec963f 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -1,6 +1,6 @@ {inputs, ...}: { imports = [ - ./testing.nix + ./nixosTests.nix ./licenses.nix ]; perSystem = { diff --git a/checks/testing.nix b/checks/nixosTests.nix similarity index 50% rename from checks/testing.nix rename to checks/nixosTests.nix index 1efe54e..bbaa9b2 100644 --- a/checks/testing.nix +++ b/checks/nixosTests.nix @@ -2,6 +2,7 @@ lib, inputs, config, + withSystem, ... }: let inherit (lib) mkOption types mapAttrs' nameValuePair; @@ -14,31 +15,53 @@ in { pkgs, ... }: let - cfg = config.cardanoNix; + cfg = config.nixosTests; in { - options.cardanoNix = { + options.nixosTests = { tests = mkOption { + description = "NixOS tests as modules."; type = types.lazyAttrsOf (types.submodule ({config, ...}: { options = { name = mkOption { + description = "The name of the test."; type = types.str; default = config._module.args.name; internal = true; }; systems = mkOption { + description = "The systems to run the tests on."; type = types.listOf types.str; + default = ["x86_64-linux"]; }; module = mkOption { + description = "The test module. Required."; type = types.deferredModule; }; documentation = mkOption { + description = "Wether to generate documentation for the testnixos configuraion. False by default to speed up builds."; type = types.bool; default = false; }; specialArgs = mkOption { + description = "The specialArgs to pass to the test node."; type = types.attrsOf types.anything; default = {}; }; + impure = mkOption { + description = "Wether the test requires internet access and should be run as an effect instead of a nix build."; + type = types.bool; + default = false; + }; + check = mkOption { + description = "The test derivation composed with _mkCheckFromTest from the module."; + type = types.package; + default = cfg._mkCheckFromTest config; + }; + checkEffect = mkOption { + description = "The test hercules-ci-effect composed with _mkEffectFromTest from the module."; + type = types.package; + default = cfg._mkEffectFromTest config; + }; }; })); }; @@ -59,16 +82,17 @@ in { (cfg._nixosLib.runTest { hostPkgs = pkgs; - # false by default, it speeds up evaluation by skipping docs generation defaults.documentation.enable = test.documentation; node = { inherit (test) specialArgs; }; - # import all of our flake nixos modules by default defaults.imports = [ + # import all of our flake nixos modules by default nixosModules.default + # fix missing pkgs.system in tests + {nixpkgs.overlays = [(_: _: {inherit system;})];} ]; # import the test module @@ -77,14 +101,26 @@ in { .config .result; }; + _mkEffectFromTest = mkOption { + type = types.functionTo types.package; + internal = true; + default = test: + withSystem system ({hci-effects, ...}: + hci-effects.modularEffect { + mounts."/dev/kvm" = "kvm"; + effectScript = '' + ${test.check.driver}/bin/nixos-test-driver + ''; + }); + }; }; config = { checks = mapAttrs' - (name: test: nameValuePair "testing-${test.name}" (cfg._mkCheckFromTest test)) + (name: test: nameValuePair "nixosTests-${test.name}" test.check) (lib.filterAttrs - (_: v: lib.elem system v.systems) + (_: v: lib.elem system v.systems && !v.impure) cfg.tests); apps.run-test.program = lib.getExe cfg.runTestScript; @@ -99,4 +135,11 @@ in { ]; }; }; + + herculesCI.onPush.default.outputs.effects = + mapAttrs' + (name: test: nameValuePair "nixosTests-${test.name}" test.checkEffect) + (lib.filterAttrs + (_: v: lib.elem config.defaultEffectSystem v.systems && v.impure) + (config.perSystem config.defaultEffectSystem).nixosTests.tests); } diff --git a/docs/default.nix b/docs/default.nix index ace7303..529abeb 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -32,7 +32,7 @@ chmod +x $out/bin/mkdocs ''; - eachOptions = removeAttrs rootConfig.flake.nixosModules ["default"]; + eachOptions = removeAttrs rootConfig.flake.nixosModules ["default" "cardano-overlay"]; eachOptionsDoc = lib.mapAttrs' ( diff --git a/flake.lock b/flake.lock index aee7f12..9c99974 100644 --- a/flake.lock +++ b/flake.lock @@ -650,11 +650,11 @@ ] }, "locked": { - "lastModified": 1701473968, - "narHash": "sha256-YcVE5emp1qQ8ieHUnxt1wCZCC3ZfAS+SRRWZ2TMda7E=", + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "34fed993f1674c8d06d58b37ce1e0fe5eebcb9f5", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", "type": "github" }, "original": { @@ -986,11 +986,11 @@ "nixpkgs": "nixpkgs_10" }, "locked": { - "lastModified": 1707187737, - "narHash": "sha256-1vdTyh8dclFK/fVoxFnJmzQis370IteOKERRExn9wXU=", + "lastModified": 1710396488, + "narHash": "sha256-yniBB5i1un44uzR4+luTWvZ6uGvsHSYIBiDZ8Xox4nQ=", "owner": "mlabs-haskell", "repo": "hercules-ci-effects", - "rev": "832a4a30d646bf0a6fbbd7a0d88aa3f748584af8", + "rev": "f5ed263ab0585dfb7b067301419fb80d64e8c021", "type": "github" }, "original": { @@ -1634,11 +1634,11 @@ }, "nixpkgs_10": { "locked": { - "lastModified": 1703637592, - "narHash": "sha256-8MXjxU0RfFfzl57Zy3OfXCITS0qWDNLzlBAdwxGZwfY=", + "lastModified": 1709961763, + "narHash": "sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "cfc3698c31b1fb9cdcf10f36c9643460264d0ca8", + "rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34", "type": "github" }, "original": { diff --git a/modules/cardano-cli/default.nix b/modules/cardano-cli/default.nix index cfe941c..6728546 100644 --- a/modules/cardano-cli/default.nix +++ b/modules/cardano-cli/default.nix @@ -4,11 +4,11 @@ pkgs, ... }: { - options.cardanoNix.cardano-cli.enable = lib.mkEnableOption "Install cardano CLI tools and scripts"; + options.cardanoNix.cardano-cli.enable = lib.mkEnableOption "cardano-cli in systemPackages"; config = lib.mkIf config.cardanoNix.cardano-cli.enable { environment.systemPackages = [ - pkgs.git # TODO: replace with `cardano-cli` (milestone 2) + pkgs.cardano-cli ]; }; } diff --git a/modules/cardano-node/default.nix b/modules/cardano-node/default.nix new file mode 100644 index 0000000..f44e21a --- /dev/null +++ b/modules/cardano-node/default.nix @@ -0,0 +1,24 @@ +{ + config, + lib, + ... +}: let + cfg = config.cardanoNix.cardano-node; +in { + options.cardanoNix.cardano-node = { + enable = lib.mkEnableOption "cardano-node service"; + }; + + config = lib.mkIf cfg.enable { + environment.variables = { + CARDANO_NODE_SOCKET_PATH = config.services.cardano-node.socketPath 0; + }; + + services.cardano-node = { + enable = true; + hostAddr = "0.0.0.0"; + + environment = config.cardanoNix.globals.network; + }; + }; +} diff --git a/modules/default.nix b/modules/default.nix index 33c93b4..f95cbe3 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1,7 +1,26 @@ -{config, ...}: { - flake.nixosModules = { +{ + inputs, + config, + ... +}: { + flake.nixosModules = rec { globals = ./globals; - cardano-cli = ./cardano-cli; + cardano-node = { + imports = [ + inputs.cardano-node.nixosModules.cardano-node + cardano-overlay + ./cardano-node + ]; + }; + cardano-cli = { + imports = [ + cardano-overlay + ./cardano-cli + ]; + }; + cardano-overlay = { + nixpkgs.overlays = [config.flake.overlays.default]; + }; # the default module imports all modules default = { imports = with builtins; attrValues (removeAttrs config.flake.nixosModules ["default"]); diff --git a/tests/cardano-cli.nix b/tests/cardano-cli.nix index 1c09bad..10ee63d 100644 --- a/tests/cardano-cli.nix +++ b/tests/cardano-cli.nix @@ -1,27 +1,19 @@ { - cardanoNix.tests = { - dummy = { - systems = ["x86_64-linux"]; + nixosTests.tests.cardano-cli.module = { + name = "cardano-cli-test"; - module = { - name = "cli-test"; - - nodes = { - machine = { - virtualisation = { - cores = 2; - memorySize = 1024; - writableStore = true; - }; - cardanoNix.cardano-cli.enable = true; - }; + nodes = { + machine = { + virtualisation = { + cores = 2; + memorySize = 1024; }; - - # TODO `git` will be replaced by `cardano-cli` (milestone 2) - testScript = '' - machine.succeed("git --version") - ''; + cardanoNix.cardano-cli.enable = true; }; }; + + testScript = '' + machine.succeed("cardano-cli --version") + ''; }; } diff --git a/tests/cardano-node.nix b/tests/cardano-node.nix new file mode 100644 index 0000000..f675268 --- /dev/null +++ b/tests/cardano-node.nix @@ -0,0 +1,31 @@ +{ + nixosTests.tests.cardano-node = { + impure = true; + module = { + name = "cardano-node-test"; + + nodes = { + machine = {pkgs, ...}: { + virtualisation = { + cores = 1; + memorySize = 1024; + }; + cardanoNix = { + globals.network = "preview"; + cardano-cli.enable = true; + cardano-node = { + enable = true; + }; + }; + + environment.systemPackages = with pkgs; [jq bc]; + }; + }; + + testScript = '' + machine.wait_for_unit("cardano-node") + machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 0.01" | bc) == "1" ]]""") + ''; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 837ced2..b1e0354 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -2,6 +2,7 @@ perSystem = _: { imports = [ ./cardano-cli.nix + ./cardano-node.nix ]; }; }