Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checks/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{inputs, ...}: {
imports = [
./testing.nix
./nixosTests.nix
./licenses.nix
];
perSystem = {
Expand Down
55 changes: 49 additions & 6 deletions checks/testing.nix → checks/nixosTests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
lib,
inputs,
config,
withSystem,
...
}: let
inherit (lib) mkOption types mapAttrs' nameValuePair;
Expand All @@ -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;
};
};
}));
};
Expand All @@ -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;})];}
Copy link
Member

@aciceri aciceri Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still wondering why in tests there is not pkgs.system...

];

# import the test module
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end you kept a prefix for checks which are tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but i named it the same as the module :)

(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;
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion docs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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' (
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/cardano-cli/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
};
}
24 changes: 24 additions & 0 deletions modules/cardano-node/default.nix
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

@aciceri aciceri Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do we want to keep globals as name?

Copy link
Contributor Author

@brainrake brainrake Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, in fact, we may want to move the options one level up under cardanoNix

};
};
}
25 changes: 22 additions & 3 deletions modules/default.nix
Original file line number Diff line number Diff line change
@@ -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"]);
Expand Down
32 changes: 12 additions & 20 deletions tests/cardano-cli.nix
Original file line number Diff line number Diff line change
@@ -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")
'';
};
}
31 changes: 31 additions & 0 deletions tests/cardano-node.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
nixosTests.tests.cardano-node = {
impure = true;
module = {
name = "cardano-node-test";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop name here if you want, by the default it would be cardano-node and the corresponding check nixosTests-cardano-node

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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" ]]""")
'';
};
};
}
1 change: 1 addition & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
perSystem = _: {
imports = [
./cardano-cli.nix
./cardano-node.nix
];
};
}