Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Last active August 19, 2025 15:24
Show Gist options
  • Select an option

  • Save izelnakri/06d7286fe9ab5f15acee9cbeace1c1b1 to your computer and use it in GitHub Desktop.

Select an option

Save izelnakri/06d7286fe9ab5f15acee9cbeace1c1b1 to your computer and use it in GitHub Desktop.
NixOS/nixpkgs Testing Conventions. The most advanced tutorial on the internet!
{
description = "Example advanced flake.nix for an advanced/general-purpose software package";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in rec {
checks.pkg-config = pkgs.testers.hasPkgConfigModules {
package = self.packages.${system}.default;
moduleNames = [ "OpenCL-Headers" ]; # Testing a list of `$ pkg-config --list-all` dynamically loaded modules for their availability.
};
# or get the reference from
# checks.pkg-config = self.packages.${system}.passthru.tests.pkg-config; # it gets it from the key below to keep it DRY since we have `rec` above.
# So `$ nix flake check` cli command can be run on all checks or specific provided check: `nix build ".#checks.x86_64-linux.specific-check"
packages.default = pkgs.stdenv.mkDerivation (finalAttrs: {
name = "opencl-headers";
nativeBuildInputs = [ pkgs.cmake ];
src = ./.;
# General purpose unit, integration, e2e tests(could be separated) should be included in the derivation.
# However long running specific ones can be included in passthru.tests.my-custom-test with its own derivation/build:
# The most basic unit of build/computation in nix.
# NOTE: This can be run via: $ nix build .#packages.x86_64-linux.default.passthru.tests.pkg-config
# or: $ nix build ".#default.tests.pkg-config", passthru namespace is used for custom targets/keys outside of a basic derivation build
passthru.tests = with pkgs; {
pkg-config = pkgs.testers.hasPkgConfigModules { # To run it from CLI: $ nix build ".#default.tests.pkg-config" # or: $ nix build .#packages.x86_64-linux.default.passthru.tests.pkg-config
package = finalAttrs.finalPackage;
moduleNames = [ "OpenCL-Headers" ];
};
# pkgs.testers.nixosTest instead of `pkgs.testers.hasPkgConfigModules` used for long-running services or one-shot binary exist & runs sanity tests
# or custom tests based on GPU, NPU can be run here and referenced above for `nix flake check`
# NOTE: Instead this could be another derivation/build with checkPhase for most minimal config/runtime, or pkgs.testers.nixosTest
intel-basic-gpu-test-can-be-advanced-derivation-with-tests = pkgs.runCommand "gpu-check" { buildInputs = [ pkgs.pciutils ]; } ''
if lspci | grep -q "Intel Corporation.*Graphics"; then
echo "Intel GPU found, running CMake with GPU tests"
cmake -DENABLE_GPU_TESTS=ON ..
ctest --output-on-failure
else
echo "No Intel GPU, skipping"
fi
touch $out
'';
# NOTE: Instead this could be another derivation/build with checkPhase for most minimal config/runtime, or pkgs.testers.nixosTest
intel-npu = pkgs.runCommand "npu-check" { buildInputs = [ pkgs.pciutils ]; } ''
if lspci | grep -q "Intel Corporation.*Graphics"; then
echo "Intel NPU found, running CMake with NPU tests"
cmake -DENABLE_ACCELERATOR_TESTS=ON ..
ctest --output-on-failure
else
echo "No Intel NPU, skipping"
fi
touch $out
'';
};
});
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.pkg-config
self.packages.${system}.default
];
shellHook = ''
export ZDOTDIR=$(mktemp -d)
cat > "$ZDOTDIR/.zshrc" << 'EOF'
source ~/.zshrc # Source the original ~/.zshrc, required.
# NOTE: Only way to source/execute zsh code after sourcing ~/.zshrc:
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ ->\ \1/'
}
function display_jobs_count_if_needed {
local job_count=$(jobs -s | wc -l | tr -d " ")
if [ $job_count -gt 0 ]; then
echo "%B%F{yellow}%j| ";
fi
}
PROMPT="%F{blue}$(date +%H:%M:%S) $(display_jobs_count_if_needed)%B%F{green}%n %F{blue}%~%F{cyan} ❄%F{yellow}$(parse_git_branch) %f%{$reset_color%}"
EOF
if [ -z "$DIRENV_IN_ENVRC" ]; then # This makes `$ nix develop` universally working
exec ${pkgs.zsh}/bin/zsh -i
fi
'';
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment