Unverified Commit 368ed53d authored by Jörg Thalheim's avatar Jörg Thalheim Committed by GitHub
Browse files

nixos/facter: add core library and system detection (#454237)

parents bdf1151f ab49e37a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -4,6 +4,10 @@
  ...
}:
{
  imports = [
    ./system.nix
  ];

  meta.maintainers = with lib.maintainers; [ mic92 ];

  options.hardware.facter = with lib; {
+59 −0
Original line number Diff line number Diff line
# Internal library functions for hardware.facter modules
# Eventually we can think about moving this under lib/
# These are facter-specific helpers for querying nixos-facter reports
lib:
let

  inherit (lib) assertMsg;

  # Query if a facter report contains a CPU with the given vendor name
  hasCpu =
    name:
    {
      hardware ? { },
      ...
    }:
    let
      cpus = hardware.cpu or [ ];
    in
    assert assertMsg (hardware != { }) "no hardware entries found in the report";
    assert assertMsg (cpus != [ ]) "no cpu entries found in the report";
    builtins.any (
      {
        vendor_name ? null,
        ...
      }:
      assert assertMsg (vendor_name != null) "detail.vendor_name not found in cpu entry";
      vendor_name == name
    ) cpus;

  # Extract all driver_modules from a list of hardware entries
  collectDrivers = list: lib.catAttrs "driver_modules" list;

  # Convert number to zero-padded 4-digit hex string (for USB device IDs)
  toZeroPaddedHex =
    n:
    let
      hex = lib.toHexString n;
      len = builtins.stringLength hex;
    in
    if len == 1 then
      "000${hex}"
    else if len == 2 then
      "00${hex}"
    else if len == 3 then
      "0${hex}"
    else
      hex;
in
{
  inherit
    hasCpu
    collectDrivers
    toZeroPaddedHex
    ;

  hasAmdCpu = hasCpu "AuthenticAMD";
  hasIntelCpu = hasCpu "GenuineIntel";

}
+15 −0
Original line number Diff line number Diff line
{
  config,
  options,
  lib,
  ...
}:
{
  # Skip setting hostPlatform in test VMs where it's read-only
  # Tests have virtualisation.test options and import read-only.nix
  config.nixpkgs = lib.optionalAttrs (!(options ? virtualisation.test)) {
    hostPlatform = lib.mkIf (
      config.hardware.facter.report.system or null != null && !options.nixpkgs.pkgs.isDefined
    ) (lib.mkDefault config.hardware.facter.report.system);
  };
}