slug |
---|
/health |
nix-health1 is a program that checks the health of your Nix install. Furthermore, individual projects can configure their own health checks in their flake.nix
. For example, the nammayatri project checks that the cachix cache is in use.
Check | Configurable in flake.nix ? |
---|---|
Flakes are enabled | - |
Nix version is not too old | Yes |
Nix runs natively (no rosetta)2 | Yes |
Builds use multiple cores (max-jobs ) |
Yes |
Nix Caches in use | Yes |
$USER is in trusted-users |
- |
Direnv: installed and activated | Yes |
Min RAM / Disk space | Yes |
Note that some checks are considered non-essential. For eg., the disk space check looks for 1TB+ disk space, but if the user is on a laptop with 256GB SSD, the check will report a warning instead of failing. This can also be configured in per-project basis from flake.nix
(see below).
Note
nix-health 0.3.0
is available on nixpkgs.
To run the development version,
# NOTE: You may have to add `--extra-experimental-features "flakes nix-command"`
# if flakes are not already enabled
nix --accept-flake-config run github:juspay/nix-health
To run nix-health along with health check configuration specified in a project flake, pass that flake as an argument. For eg., to run nix-health with additional checks from the nammayatri project, run:
# The argument can be any flake URL (including a local path)
nix --accept-flake-config run github:juspay/nix-health github:nammayatri/nammayatri
To add project specific health checks or configure health checks, add the following flake output:
{
outputs = inputs: {
nix-health.default = {
# Add configuration here
caches.required = [ "https://ourproject.cachix.org" ];
};
};
}
To see all available configuration options, run nix-health --dump-schema
. This will dump the schema of the configuration in JSON format. Convert that to a Nix attrset to see what can be added under the nix-health.default
attrset of your flake.
$ nix-health --dump-schema > schema.json
$ nix eval --impure --expr 'builtins.fromJSON (builtins.readFile ./schema.json)' | nix run nixpkgs#alejandra -- --quiet
{
caches = {required = ["https://cache.nixos.org/"];};
direnv = {
enable = true;
required = false;
};
flake-enabled = {};
max-jobs = {};
nix-version = {min-required = "2.13.0";};
rosetta = {
enable = true;
required = true;
};
system = {
enable = true;
min_disk_space = "1024.0 GB";
min_ram = null;
required = false;
};
trusted-users = {};
}
You can automatically run nix-health
whenever your Nix dev shell starts. To do this, import the flake module in your flake and use it in your devShell:
{
inputs = {
# NOTE: refers to ./module flake.
nix-health.url = "github:juspay/nix-health?dir=module";
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.nix-health.flakeModule
];
perSystem = { config, pkgs, ... }: {
devShells.default = pkgs.mkShell {
inputsFrom = [
config.nix-health.outputs.devShell
]
};
};
};
}
Now suppose you have Nix 2.18 installed, but your project requires 2.19 or above due to the following config in its flake.nix
:
flake.nix-health.default = {
nix-version.min-required = "2.19.0";
};
you can expect the devShell to print a giant message like this:
![image](https://private-user-images.githubusercontent.com/3998/334574985-9f3b3141-611f-484f-b897-3e375c02dff5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU0OTEyODgsIm5iZiI6MTcyNTQ5MDk4OCwicGF0aCI6Ii8zOTk4LzMzNDU3NDk4NS05ZjNiMzE0MS02MTFmLTQ4NGYtYjg5Ny0zZTM3NWMwMmRmZjUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDRUMjMwMzA4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjNlMGVlN2ZjNzg2NzQxZTZmOTYxNmQxYmUyMzQyNTg2MDgwYTBkZmZkZmIzMmJjZGEwZjIyNzUyNjUwNjQyOCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.cP3G_JHI2k5qG4_jW9es8upJdYfttYKdXqW8gLlJ340)
Note that you will still be dropped into the Nix dev shell (there's no way to abrupt the launching of a dev Shell).
Footnotes
-
nix-health originally began as a script https://github.com/srid/nix-health which is now deprecated. ↩
-
This check is only performed on macOS with Apple Silicon. ↩