Skip to content

Write health checks as NixOS options to quickly verify if your services are properly running.

License

Notifications You must be signed in to change notification settings

mrVanDalo/nixos-healthchecks

Repository files navigation

NixOS health checks

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.

How to define checks

You can define checks using the (newly introduced) healthchecks NixOS Option.

Check the http responses

healthchecks.http.nextcloud = {
  url = "https://example.com/login";
  expectedContent = "Login";
};

check if a port is actually closed

healthchecks.closed.public.host = "example.com";
healthchecks.closed.public.ports.opentelemetry = [ 4317 ];

custom command

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.

How to set up with flake parts

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
            ];
          };
        }; 
      }
    );
}

About

Write health checks as NixOS options to quickly verify if your services are properly running.

Topics

Resources

License

Stars

Watchers

Forks