I found the provided test.assertions system quite difficult to grasp when combined with submodules. TBH I couldn't get it to work after many hours trying back and forth.
Therefore, I decided to test a simpler approach by using upstream NixOS assertions and warnings:
{
description = "Minimal example: NixOS-style assertions in KubeNix";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
kubenix.url = "github:hall/kubenix";
};
outputs = {nixpkgs, kubenix, ...}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system}.default = kubenix.packages.${system}.default.override {
module = {kubenix, ...}: {
imports = [
(nixpkgs + "/nixos/modules/misc/assertions.nix")
kubenix.modules.k8s
];
config = {
# Define assertions
assertions = [
{
assertion = config.kubernetes.namespace != "";
message = "Namespace cannot be empty";
}
];
# Define warnings
warnings = pkgs.lib.optional
(config.kubernetes.namespace == "default")
"Using 'default' namespace is not recommended";
kubernetes = {
namespace = "default";
resources.deployments.nginx =
pkgs.lib.asserts.checkAssertWarn config.assertions config.warnings {
spec.template.spec.containers = [{
name = "nginx";
image = "nginx:latest";
}];
};
};
};
};
};
};
}
This just works, supports warnings and is more familiar.
IMHO it should be the default way of supporting assertions. For example, by prefixing lib.asserts.checkAssertWarn config.assertions config.warnings to the main derivations built by kubenix, such as kubernetes.result and kubernetes.resultYAML.
In case you are interested, I can PR. Or I can just add a note to tips and tricks on the docs. Otherwise, just close it and, at least, this issue can stay as "docs".
Thanks.
I found the provided
test.assertionssystem quite difficult to grasp when combined with submodules. TBH I couldn't get it to work after many hours trying back and forth.Therefore, I decided to test a simpler approach by using upstream NixOS assertions and warnings:
This just works, supports warnings and is more familiar.
IMHO it should be the default way of supporting assertions. For example, by prefixing
lib.asserts.checkAssertWarn config.assertions config.warningsto the main derivations built by kubenix, such askubernetes.resultandkubernetes.resultYAML.In case you are interested, I can PR. Or I can just add a note to tips and tricks on the docs. Otherwise, just close it and, at least, this issue can stay as "docs".
Thanks.