From 79d683f4ad85450287f8e723afc09c191d5839b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 21 Oct 2025 17:12:14 +0200 Subject: [PATCH 1/4] nixos/facter: add graphics hardware detection This adds automatic graphics card configuration: Builds on PR #456698 (virtualization & firmware). Part of incremental upstreaming from nixos-facter-modules. --- nixos/modules/hardware/facter/default.nix | 1 + .../modules/hardware/facter/graphics/amd.nix | 18 ++++++++ .../hardware/facter/graphics/default.nix | 42 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 nixos/modules/hardware/facter/graphics/amd.nix create mode 100644 nixos/modules/hardware/facter/graphics/default.nix diff --git a/nixos/modules/hardware/facter/default.nix b/nixos/modules/hardware/facter/default.nix index f3bd58443dc7..30db0d10e48a 100644 --- a/nixos/modules/hardware/facter/default.nix +++ b/nixos/modules/hardware/facter/default.nix @@ -7,6 +7,7 @@ imports = [ ./disk.nix ./firmware.nix + ./graphics ./keyboard.nix ./networking ./system.nix diff --git a/nixos/modules/hardware/facter/graphics/amd.nix b/nixos/modules/hardware/facter/graphics/amd.nix new file mode 100644 index 000000000000..666868035c3d --- /dev/null +++ b/nixos/modules/hardware/facter/graphics/amd.nix @@ -0,0 +1,18 @@ +{ lib, config, ... }: +let + facterLib = import ../lib.nix lib; + cfg = config.hardware.facter.detected.graphics.amd; +in +{ + options.hardware.facter.detected.graphics = { + amd.enable = lib.mkEnableOption "Enable the AMD Graphics module" // { + default = builtins.elem "amdgpu" ( + facterLib.collectDrivers (config.hardware.facter.report.hardware.graphics_card or [ ]) + ); + defaultText = "hardware dependent"; + }; + }; + config = lib.mkIf cfg.enable { + services.xserver.videoDrivers = [ "modesetting" ]; + }; +} diff --git a/nixos/modules/hardware/facter/graphics/default.nix b/nixos/modules/hardware/facter/graphics/default.nix new file mode 100644 index 000000000000..cdb987e0d46e --- /dev/null +++ b/nixos/modules/hardware/facter/graphics/default.nix @@ -0,0 +1,42 @@ +{ lib, config, ... }: +let + facterLib = import ../lib.nix lib; + cfg = config.hardware.facter.detected.graphics; +in +{ + imports = [ + ./amd.nix + ]; + options.hardware.facter.detected = { + graphics.enable = lib.mkEnableOption "Enable the Graphics module" // { + default = builtins.length (config.hardware.facter.report.hardware.monitor or [ ]) > 0; + defaultText = "hardware dependent"; + }; + boot.graphics.kernelModules = lib.mkOption { + type = lib.types.listOf lib.types.str; + # We currently don't auto import nouveau, in case the user might want to use the proprietary nvidia driver, + # We might want to change this in future, if we have a better idea, how to handle this. + default = lib.remove "nouveau" ( + lib.uniqueStrings ( + facterLib.collectDrivers (config.hardware.facter.report.hardware.graphics_card or [ ]) + ) + ); + defaultText = "hardware dependent"; + description = '' + List of kernel modules to load at boot for the graphics card. + ''; + }; + }; + + config = lib.mkIf cfg.enable ( + { + boot.initrd.kernelModules = config.hardware.facter.detected.boot.graphics.kernelModules; + } + // ( + if lib.versionOlder lib.version "24.11pre" then + { hardware.opengl.enable = lib.mkDefault true; } + else + { hardware.graphics.enable = lib.mkDefault true; } + ) + ); +} From 3e758c612d74919965abcf2be64e533589fd0151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 4 Nov 2025 19:19:36 +0100 Subject: [PATCH 2/4] nixos/facter: add reportPath guard to all modules Add lib.mkIf condition checking if config.hardware.facter.reportPath != null to all facter modules. This shortcuts the module effects when no facter report is available, preventing unnecessary evaluation and potential errors when the report is not provided. --- nixos/modules/hardware/facter/disk.nix | 2 +- nixos/modules/hardware/facter/firmware.nix | 2 +- nixos/modules/hardware/facter/graphics/amd.nix | 2 +- nixos/modules/hardware/facter/graphics/default.nix | 2 +- nixos/modules/hardware/facter/keyboard.nix | 2 +- nixos/modules/hardware/facter/networking/default.nix | 12 +++++++----- nixos/modules/hardware/facter/networking/initrd.nix | 2 +- nixos/modules/hardware/facter/networking/intel.nix | 2 +- nixos/modules/hardware/facter/system.nix | 2 +- nixos/modules/hardware/facter/virtualisation.nix | 2 +- 10 files changed, 16 insertions(+), 14 deletions(-) diff --git a/nixos/modules/hardware/facter/disk.nix b/nixos/modules/hardware/facter/disk.nix index 4bb76f16366d..4158bd171bb9 100644 --- a/nixos/modules/hardware/facter/disk.nix +++ b/nixos/modules/hardware/facter/disk.nix @@ -22,7 +22,7 @@ in ''; }; - config = { + config = lib.mkIf (config.hardware.facter.reportPath != null) { boot.initrd.availableKernelModules = config.hardware.facter.detected.boot.disk.kernelModules; }; } diff --git a/nixos/modules/hardware/facter/firmware.nix b/nixos/modules/hardware/facter/firmware.nix index 10962018890c..b614cab85253 100644 --- a/nixos/modules/hardware/facter/firmware.nix +++ b/nixos/modules/hardware/facter/firmware.nix @@ -8,7 +8,7 @@ let hasIntelCpu = facterLib.hasIntelCpu report; in { - config = lib.mkIf isBaremetal { + config = lib.mkIf (config.hardware.facter.reportPath != null && isBaremetal) { # none (e.g. bare-metal) # provide firmware for devices that might not have been detected by nixos-facter hardware.enableRedistributableFirmware = lib.mkDefault true; diff --git a/nixos/modules/hardware/facter/graphics/amd.nix b/nixos/modules/hardware/facter/graphics/amd.nix index 666868035c3d..b2ec114677a0 100644 --- a/nixos/modules/hardware/facter/graphics/amd.nix +++ b/nixos/modules/hardware/facter/graphics/amd.nix @@ -12,7 +12,7 @@ in defaultText = "hardware dependent"; }; }; - config = lib.mkIf cfg.enable { + config = lib.mkIf (config.hardware.facter.reportPath != null && cfg.enable) { services.xserver.videoDrivers = [ "modesetting" ]; }; } diff --git a/nixos/modules/hardware/facter/graphics/default.nix b/nixos/modules/hardware/facter/graphics/default.nix index cdb987e0d46e..cd29040ce67d 100644 --- a/nixos/modules/hardware/facter/graphics/default.nix +++ b/nixos/modules/hardware/facter/graphics/default.nix @@ -28,7 +28,7 @@ in }; }; - config = lib.mkIf cfg.enable ( + config = lib.mkIf (config.hardware.facter.reportPath != null && cfg.enable) ( { boot.initrd.kernelModules = config.hardware.facter.detected.boot.graphics.kernelModules; } diff --git a/nixos/modules/hardware/facter/keyboard.nix b/nixos/modules/hardware/facter/keyboard.nix index d19e17cd6901..62baf5bd32f1 100644 --- a/nixos/modules/hardware/facter/keyboard.nix +++ b/nixos/modules/hardware/facter/keyboard.nix @@ -15,7 +15,7 @@ in ''; }; - config = { + config = lib.mkIf (config.hardware.facter.reportPath != null) { boot.initrd.availableKernelModules = config.hardware.facter.detected.boot.keyboard.kernelModules; }; } diff --git a/nixos/modules/hardware/facter/networking/default.nix b/nixos/modules/hardware/facter/networking/default.nix index dfbc54c8396d..7f662d8785a0 100644 --- a/nixos/modules/hardware/facter/networking/default.nix +++ b/nixos/modules/hardware/facter/networking/default.nix @@ -60,10 +60,12 @@ in ]; }; }; - config = lib.mkIf config.hardware.facter.detected.dhcp.enable { - networking.useDHCP = lib.mkDefault true; + config = + lib.mkIf (config.hardware.facter.reportPath != null && config.hardware.facter.detected.dhcp.enable) + { + networking.useDHCP = lib.mkDefault true; - # Per-interface DHCP configuration - networking.interfaces = perInterfaceConfig; - }; + # Per-interface DHCP configuration + networking.interfaces = perInterfaceConfig; + }; } diff --git a/nixos/modules/hardware/facter/networking/initrd.nix b/nixos/modules/hardware/facter/networking/initrd.nix index 56be802b7cd9..8c3135354f93 100644 --- a/nixos/modules/hardware/facter/networking/initrd.nix +++ b/nixos/modules/hardware/facter/networking/initrd.nix @@ -14,7 +14,7 @@ in ''; }; - config = lib.mkIf config.boot.initrd.network.enable { + config = lib.mkIf (config.hardware.facter.reportPath != null && config.boot.initrd.network.enable) { boot.initrd.kernelModules = config.hardware.facter.detected.boot.initrd.networking.kernelModules; }; } diff --git a/nixos/modules/hardware/facter/networking/intel.nix b/nixos/modules/hardware/facter/networking/intel.nix index 4ae693a3c636..a28e8ecf580d 100644 --- a/nixos/modules/hardware/facter/networking/intel.nix +++ b/nixos/modules/hardware/facter/networking/intel.nix @@ -49,7 +49,7 @@ in }; }; - config = { + config = lib.mkIf (config.hardware.facter.reportPath != null) { networking.enableIntel2200BGFirmware = lib.mkIf cfg._2200BG.enable (lib.mkDefault true); hardware.enableRedistributableFirmware = lib.mkIf cfg._3945ABG.enable (lib.mkDefault true); }; diff --git a/nixos/modules/hardware/facter/system.nix b/nixos/modules/hardware/facter/system.nix index ef51b0e44f1d..32648f7d07e4 100644 --- a/nixos/modules/hardware/facter/system.nix +++ b/nixos/modules/hardware/facter/system.nix @@ -6,7 +6,7 @@ }: { # Skip setting hostPlatform if it's read-only - nixpkgs = + config.nixpkgs = lib.optionalAttrs (config.hardware.facter.report.system or null != null && !options.nixpkgs.hostPlatform.readOnly) { diff --git a/nixos/modules/hardware/facter/virtualisation.nix b/nixos/modules/hardware/facter/virtualisation.nix index 15cf6bab5aee..9f418ecf3488 100644 --- a/nixos/modules/hardware/facter/virtualisation.nix +++ b/nixos/modules/hardware/facter/virtualisation.nix @@ -51,7 +51,7 @@ in }; }; - config = { + config = lib.mkIf (config.hardware.facter.reportPath != null) { # KVM support boot.kernelModules = From a555914285e9d29a88c85763cbb507f0df71793c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 4 Nov 2025 19:26:31 +0100 Subject: [PATCH 3/4] nixos/facter: make hasCPUFeature more fault-tolerant Add default value for features attribute in hasCPUFeature function to handle CPU records that might not have a features field, preventing errors when the report structure varies. --- nixos/modules/hardware/facter/virtualisation.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nixos/modules/hardware/facter/virtualisation.nix b/nixos/modules/hardware/facter/virtualisation.nix index 9f418ecf3488..e1a9a1647708 100644 --- a/nixos/modules/hardware/facter/virtualisation.nix +++ b/nixos/modules/hardware/facter/virtualisation.nix @@ -57,7 +57,14 @@ in boot.kernelModules = let hasCPUFeature = - feature: lib.any ({ features, ... }: lib.elem feature features) (report.hardware.cpu or [ ]); + feature: + lib.any ( + { + features ? [ ], + ... + }: + lib.elem feature features + ) (report.hardware.cpu or [ ]); in lib.mkMerge [ (lib.mkIf (hasCPUFeature "vmx") [ "kvm-intel" ]) From 484feccb9f07b95b9bd9fb217a8232396d8edce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 20 Nov 2025 12:40:09 +0100 Subject: [PATCH 4/4] nixos/facter: handle case where we do not have readOnly included --- nixos/modules/hardware/facter/system.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nixos/modules/hardware/facter/system.nix b/nixos/modules/hardware/facter/system.nix index 32648f7d07e4..2019bfa522d3 100644 --- a/nixos/modules/hardware/facter/system.nix +++ b/nixos/modules/hardware/facter/system.nix @@ -8,7 +8,10 @@ # Skip setting hostPlatform if it's read-only config.nixpkgs = lib.optionalAttrs - (config.hardware.facter.report.system or null != null && !options.nixpkgs.hostPlatform.readOnly) + ( + config.hardware.facter.report.system or null != null + && !(options.nixpkgs.hostPlatform.readOnly or false) + ) { hostPlatform = lib.mkDefault config.hardware.facter.report.system; };