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
74 changes: 40 additions & 34 deletions checks/default.nix
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
{inputs, ...}: {
{
inputs,
...
}:
{
imports = [
./vmTests.nix
./licenses.nix
];
perSystem = {
pkgs,
config,
...
}: {
apps = {
nix-build-all.program = pkgs.writeShellApplication {
name = "nix-build-all";
runtimeInputs = [
(pkgs.callPackage inputs.devour-flake {})
];
text = ''
# Make sure that flake.lock is sync
nix flake lock --no-update-lock-file
perSystem =
{
pkgs,
config,
...
}:
{
apps = {
nix-build-all.program = pkgs.writeShellApplication {
name = "nix-build-all";
runtimeInputs = [
(pkgs.callPackage inputs.devour-flake { })
];
text = ''
# Make sure that flake.lock is sync
nix flake lock --no-update-lock-file

# Do a full nix build (all outputs)
devour-flake . "$@"
'';
# Do a full nix build (all outputs)
devour-flake . "$@"
'';
};
};
};

devshells.default.commands = [
{
category = "tests";
name = "build-all";
help = "build all packages and checks with `devour-flake`";
command = config.apps.nix-build-all.program;
}
{
category = "tests";
name = "check";
help = "run `nix flake check`";
command = "nix flake check";
}
];
};
devshells.default.commands = [
{
category = "tests";
name = "build-all";
help = "build all packages and checks with `devour-flake`";
command = config.apps.nix-build-all.program;
}
{
category = "tests";
name = "check";
help = "run `nix flake check`";
command = "nix flake check";
}
];
};
}
29 changes: 17 additions & 12 deletions checks/licenses.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{self, ...}: {
perSystem = {pkgs, ...}: {
checks = {
reuse =
pkgs.runCommandLocal "reuse-lint" {
buildInputs = [pkgs.reuse];
} ''
cd ${self}
reuse --suppress-deprecation lint
touch $out
'';
{ self, ... }:
{
perSystem =
{ pkgs, ... }:
{
checks = {
reuse =
pkgs.runCommandLocal "reuse-lint"
{
buildInputs = [ pkgs.reuse ];
}
''
cd ${self}
reuse --suppress-deprecation lint
touch $out
'';
};
};
};
}
2 changes: 1 addition & 1 deletion checks/run-vm-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
writeShellApplication {
name = "run-vm-test";

runtimeInputs = [];
runtimeInputs = [ ];

text = ''
cmd_name=$(basename "$0")
Expand Down
262 changes: 128 additions & 134 deletions checks/vmTests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,145 +4,139 @@
config,
withSystem,
...
}: let
inherit (lib) mkOption types mapAttrs' nameValuePair;
}:
let
inherit (lib)
mkOption
types
mapAttrs'
nameValuePair
;
inherit (config.flake) nixosModules;
in {
perSystem = {
config,
lib,
system,
pkgs,
...
}: let
cfg = config.vmTests;
in {
options.vmTests = {
tests = mkOption {
description = "Run integration tests in networks of virtual machines.";
type = types.lazyAttrsOf (types.submodule ({config, ...}: {
options = {
name = mkOption {
description = "The name of the test. Defaults to attribute name.";
internal = true;
type = types.str;
default = config._module.args.name;
};
systems = mkOption {
description = "The systems to run the test on.";
type = types.listOf types.str;
default = ["x86_64-linux"];
};
module = mkOption {
description = "The NixOS test module. Required. See https://nixos.org/manual/nixos/stable/#sec-nixos-tests .";
type = types.deferredModule;
};
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. Result of calling `_mkCheck` with this test.";
type = types.package;
default = cfg._mkCheck config;
};
effect = mkOption {
description = "The test hercules-ci-effect. Result of calling `_mkEffect` with this test.";
type = types.package;
default = cfg._mkEffect config;
};
};
}));
};
runVmTestScript = mkOption {
description = "Script that lists and runs integration tests on networks of virtual machines.";
type = types.package;
default = pkgs.callPackage ./run-vm-test.nix {inherit (cfg) tests;};
};
_nixosLib = mkOption {
description = "Convenience access to `nixpkgs/nixos/lib`.";
internal = true;
type = types.anything;
default = import (inputs.nixpkgs.outPath + "/nixos/lib") {};
};
_mkCheck = mkOption {
description = "Function that takes a test `module` and returns a derivation that runs the test when built.";
internal = true;
type = types.functionTo types.package;
default = test:
(cfg._nixosLib.runTest {
name = lib.mkDefault test.name;
imports = [test.module];
hostPkgs = pkgs;
defaults = {
imports = [
# Import all of our NixOS modules by default.
nixosModules.default
# Fix missing `pkgs.system` in tests.
{nixpkgs.overlays = [(_: _: {inherit system;})];}
];
documentation.enable = lib.mkDefault false;
};
})
.config
.result;
};
_mkEffect = mkOption {
description = "Function that takes a test `module` and returns a Hercules CI effect that runs the test.";
internal = true;
type = types.functionTo types.package;
default = testModule:
withSystem system ({hci-effects, ...}:
hci-effects.modularEffect {
mounts."/dev/kvm" = "kvm";
effectScript = ''
${testModule.check.driver}/bin/nixos-test-driver
'';
});
in
{
perSystem =
{
config,
lib,
system,
pkgs,
...
}:
let
cfg = config.vmTests;
in
{
options.vmTests = {
tests = mkOption {
description = "Run integration tests in networks of virtual machines.";
type = types.lazyAttrsOf (
types.submodule (
{ config, ... }:
{
options = {
name = mkOption {
description = "The name of the test. Defaults to attribute name.";
internal = true;
type = types.str;
default = config._module.args.name;
};
systems = mkOption {
description = "The systems to run the test on.";
type = types.listOf types.str;
default = [ "x86_64-linux" ];
};
module = mkOption {
description = "The NixOS test module. Required. See https://nixos.org/manual/nixos/stable/#sec-nixos-tests .";
type = types.deferredModule;
};
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. Result of calling `_mkCheck` with this test.";
type = types.package;
default = cfg._mkCheck config;
};
effect = mkOption {
description = "The test hercules-ci-effect. Result of calling `_mkEffect` with this test.";
type = types.package;
default = cfg._mkEffect config;
};
};
}
)
);
};
runVmTestScript = mkOption {
description = "Script that lists and runs integration tests on networks of virtual machines.";
type = types.package;
default = pkgs.callPackage ./run-vm-test.nix { inherit (cfg) tests; };
};
_nixosLib = mkOption {
description = "Convenience access to `nixpkgs/nixos/lib`.";
internal = true;
type = types.anything;
default = import (inputs.nixpkgs.outPath + "/nixos/lib") { };
};
_mkCheck = mkOption {
description = "Function that takes a test `module` and returns a derivation that runs the test when built.";
internal = true;
type = types.functionTo types.package;
default =
test:
(cfg._nixosLib.runTest {
name = lib.mkDefault test.name;
imports = [ test.module ];
hostPkgs = pkgs;
defaults = {
imports = [
# Import all of our NixOS modules by default.
nixosModules.default
# Fix missing `pkgs.system` in tests.
{ nixpkgs.overlays = [ (_: _: { inherit system; }) ]; }
];
documentation.enable = lib.mkDefault false;
};
}).config.result;
};
_mkEffect = mkOption {
description = "Function that takes a test `module` and returns a Hercules CI effect that runs the test.";
internal = true;
type = types.functionTo types.package;
default =
testModule:
withSystem system (
{ hci-effects, ... }:
hci-effects.modularEffect {
mounts."/dev/kvm" = "kvm";
effectScript = ''
${testModule.check.driver}/bin/nixos-test-driver
'';
}
);
};
};
};

config = {
checks =
mapAttrs'
(name: test:
nameValuePair
"vmTests-${test.name}"
test.check)
(lib.filterAttrs
(_: v: lib.elem system v.systems && !v.impure)
cfg.tests);
config = {
checks = mapAttrs' (name: test: nameValuePair "vmTests-${test.name}" test.check) (lib.filterAttrs (_: v: lib.elem system v.systems && !v.impure) cfg.tests);

apps =
{run-vm-tests.program = lib.getExe cfg.runVmTestScript;}
// mapAttrs'
(name: test:
nameValuePair
"vmTests-${test.name}"
{program = "${test.check.driver}/bin/nixos-test-driver";})
(lib.filterAttrs
(_: v: lib.elem system v.systems)
cfg.tests);
apps = {
run-vm-tests.program = lib.getExe cfg.runVmTestScript;
} // mapAttrs' (name: test: nameValuePair "vmTests-${test.name}" { program = "${test.check.driver}/bin/nixos-test-driver"; }) (lib.filterAttrs (_: v: lib.elem system v.systems) cfg.tests);

devshells.default.commands = [
{
name = "run-vm-test";
category = "tests";
help = "list and run virtual machine integration tests";
command = "${lib.getExe cfg.runVmTestScript} $@";
}
];
devshells.default.commands = [
{
name = "run-vm-test";
category = "tests";
help = "list and run virtual machine integration tests";
command = "${lib.getExe cfg.runVmTestScript} $@";
}
];
};
};
};

herculesCI.onPush.default.outputs.effects =
mapAttrs'
(name: test:
nameValuePair
"vmTests-${test.name}"
test.effect)
(lib.filterAttrs
(_: v: lib.elem config.defaultEffectSystem v.systems && v.impure)
(config.perSystem config.defaultEffectSystem).vmTests.tests);
herculesCI.onPush.default.outputs.effects = mapAttrs' (name: test: nameValuePair "vmTests-${test.name}" test.effect) (lib.filterAttrs (_: v: lib.elem config.defaultEffectSystem v.systems && v.impure) (config.perSystem config.defaultEffectSystem).vmTests.tests);
}
Loading