NixOS flake to write health checks as options, intended to be written right next to the service definitions to verify right after deployment or whenever you like if your services are running correctly.
You can define checks using the (newly introduced) healthchecks
NixOS Option.
healthchecks.http.nextcloud = {
url = "https://example.com/login";
expectedContent = "Login";
};
healthchecks.closed.public.host = "example.com";
healthchecks.closed.public.ports.opentelemetry = [ 4317 ];
healthchecks.localCommand.bashTest = pkgs.writers.writeBash "test" ''
echo "this is a bash test"
'';
healthchecks.localCommand.pythonTest = pkgs.writers.writePython "test" {} ''
print("this is a python test")
'';
Failure or Success is decided on exit code of the script. The output of the command will only be printed if the exit code is not 0.
You have to import the healthchecks.flakeModule
and the
healthchecks.nixosModules.default
.
{
inputs = {
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
flake-parts.url = "github:hercules-ci/flake-parts";
healthchecks.inputs.nixpkgs.follows = "nixpkgs";
healthchecks.url = "github:mrvandalo/nixos-healthchecks";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs =
inputs@{
flake-parts,
healthchecks,
nixpkgs,
self,
}:
flake-parts.lib.mkFlake { inherit inputs; } (
{ }:
{
systems = [ "x86_64-linux" ]; # feel free to use other systems
# 1. import healthchecks flakeModule
imports = [ healthchecks.flakeModule ];
flake = {
nixosConfigurations.example = inputs.nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
# 2. import healthchecks nixosModule
healthchecks.nixosModules.default
];
};
};
}
);
}