From 67e208ca663c93ce740e20c8e735b3c86b039805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 25 Sep 2024 12:14:33 +0200 Subject: [PATCH 1/5] move all hardware detection to its own namespace This allows us to debug things easier in the repl by using the :p option. Also makes generating options documentation easier because we can distinguish between general option and detected hardware options. --- docs.nix | 21 ++------------------- modules/nixos/bluetooth.nix | 4 ++-- modules/nixos/boot.nix | 4 ++-- modules/nixos/firmware.nix | 2 +- modules/nixos/graphics.nix | 4 ++-- modules/nixos/networking/broadcom.nix | 4 ++-- modules/nixos/networking/intel.nix | 4 ++-- modules/nixos/virtualisation.nix | 4 ++-- 8 files changed, 15 insertions(+), 32 deletions(-) diff --git a/docs.nix b/docs.nix index 2c5c123..7baa81e 100644 --- a/docs.nix +++ b/docs.nix @@ -65,25 +65,9 @@ pkgs.mkShellNoCC { cat ${optionsDoc.optionsCommonMark} > $out/${snakeCase name}.md ''; - # Allows us to gather all options that are immediate children of `facter` and which have no child options. - # e.g. facter.reportPath, facter.report. - # For all other options we group them by the first immediate child of `facter`. - # e.g. facter.bluetooth, facter.boot and so on. - # This allows us to have a page for root facter options "facter.md", and a page each for the major sub modules. - facterOptionsFilter = - _: - { - loc ? [ ], - options ? [ ], - ... - }: - (lib.length loc) == 2 && ((lib.elemAt loc 0) == "facter") && (lib.length options) == 0; - - otherOptionsFilter = n: v: !(facterOptionsFilter n v); - - facterMarkdown = mkMarkdown "facter" (lib.filterAttrs facterOptionsFilter eval.options.facter); + facterMarkdown = mkMarkdown "facter" eval.options.facter.detected; otherMarkdown = lib.mapAttrsToList mkMarkdown ( - lib.filterAttrs otherOptionsFilter eval.options.facter + lib.filterAttrs (n: v: n != "detected") eval.options.facter ); optionsMarkdown = pkgs.symlinkJoin { @@ -92,7 +76,6 @@ pkgs.mkShellNoCC { }; in - with pkgs; [ (pkgs.writeScriptBin "mkdocs" '' # rsync in NixOS modules doc to avoid issues with symlinks being owned by root diff --git a/modules/nixos/bluetooth.nix b/modules/nixos/bluetooth.nix index 276c6e0..f8fabb8 100644 --- a/modules/nixos/bluetooth.nix +++ b/modules/nixos/bluetooth.nix @@ -1,8 +1,8 @@ { lib, config, ... }: { - options.facter.bluetooth.enable = lib.mkEnableOption "Enable the Facter bluetooth module" // { + options.facter.detected.bluetooth.enable = lib.mkEnableOption "Enable the Facter bluetooth module" // { default = builtins.length (config.facter.report.hardware.bluetooth or []) > 0; }; - config.hardware.bluetooth.enable = lib.mkIf config.facter.bluetooth.enable (lib.mkDefault true); + config.hardware.bluetooth.enable = lib.mkIf config.facter.detected.bluetooth.enable (lib.mkDefault true); } diff --git a/modules/nixos/boot.nix b/modules/nixos/boot.nix index 199492c..6b57bd1 100644 --- a/modules/nixos/boot.nix +++ b/modules/nixos/boot.nix @@ -2,11 +2,11 @@ let facterLib = import ../../lib/lib.nix lib; - cfg = config.facter.boot; + cfg = config.facter.detected.boot; inherit (config.facter) report; in { - options.facter.boot.enable = lib.mkEnableOption "Enable the Facter Boot module" // { + options.facter.detected.boot.enable = lib.mkEnableOption "Enable the Facter Boot module" // { default = true; }; diff --git a/modules/nixos/firmware.nix b/modules/nixos/firmware.nix index 2ec9c00..4f3ae78 100644 --- a/modules/nixos/firmware.nix +++ b/modules/nixos/firmware.nix @@ -3,7 +3,7 @@ let facterLib = import ../../lib/lib.nix lib; inherit (config.facter) report; - isBaremetal = config.facter.virtualisation.none.enable; + isBaremetal = config.facter.detected.virtualisation.none.enable; hasAmdCpu = facterLib.hasAmdCpu report; hasIntelCpu = facterLib.hasIntelCpu report; in diff --git a/modules/nixos/graphics.nix b/modules/nixos/graphics.nix index 64d1dc8..62c41cf 100644 --- a/modules/nixos/graphics.nix +++ b/modules/nixos/graphics.nix @@ -3,11 +3,11 @@ let facterLib = import ../../lib/lib.nix lib; in { - options.facter.graphics.enable = lib.mkEnableOption "Enable the Graphics module" // { + options.facter.detected.graphics.enable = lib.mkEnableOption "Enable the Graphics module" // { default = builtins.length (config.facter.report.hardware.monitor or [ ]) > 0; }; - config = lib.mkIf config.facter.graphics.enable { + config = lib.mkIf config.facter.detected.graphics.enable { hardware.graphics.enable = lib.mkDefault true; boot.initrd.kernelModules = facterLib.stringSet ( facterLib.collectDrivers (config.facter.report.hardware.graphics_card or [ ]) diff --git a/modules/nixos/networking/broadcom.nix b/modules/nixos/networking/broadcom.nix index b80ae08..9f62084 100644 --- a/modules/nixos/networking/broadcom.nix +++ b/modules/nixos/networking/broadcom.nix @@ -1,10 +1,10 @@ { lib, config, ... }: let inherit (config.facter) report; - cfg = config.facter.networking.broadcom; + cfg = config.facter.detected.networking.broadcom; in { - options.facter.networking.broadcom = with lib; { + options.facter.detected.networking.broadcom = with lib; { full_mac.enable = mkEnableOption "Enable the Facter Broadcom Full MAC module" // { default = lib.any ( diff --git a/modules/nixos/networking/intel.nix b/modules/nixos/networking/intel.nix index fbae8f6..5e116bd 100644 --- a/modules/nixos/networking/intel.nix +++ b/modules/nixos/networking/intel.nix @@ -1,10 +1,10 @@ { lib, config, ... }: let inherit (config.facter) report; - cfg = config.facter.networking.intel; + cfg = config.facter.detected.networking.intel; in { - options.facter.networking.intel = with lib; { + options.facter.detected.networking.intel = with lib; { _2200BG.enable = mkEnableOption "Enable the Facter Intel 2200BG module" // { default = lib.any ( diff --git a/modules/nixos/virtualisation.nix b/modules/nixos/virtualisation.nix index b21be61..6c89c81 100644 --- a/modules/nixos/virtualisation.nix +++ b/modules/nixos/virtualisation.nix @@ -6,10 +6,10 @@ }: let inherit (config.facter) report; - cfg = config.facter.virtualisation; + cfg = config.facter.detected.virtualisation; in { - options.facter.virtualisation = { + options.facter.detected.virtualisation = { virtio_scsi.enable = lib.mkEnableOption "Enable the Facter Virtualisation Virtio SCSI module" // { default = lib.any ( { vendor, device, ... }: From 52d18fa41e451e5a545708a97ba6a59803bc1bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 25 Sep 2024 14:46:58 +0200 Subject: [PATCH 2/5] split boot.nix into keyboard.nix and disk.nix --- modules/nixos/{boot.nix => disk.nix} | 11 ++++------- modules/nixos/facter.nix | 3 ++- modules/nixos/keyboard.nix | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) rename modules/nixos/{boot.nix => disk.nix} (55%) create mode 100644 modules/nixos/keyboard.nix diff --git a/modules/nixos/boot.nix b/modules/nixos/disk.nix similarity index 55% rename from modules/nixos/boot.nix rename to modules/nixos/disk.nix index 6b57bd1..e51886f 100644 --- a/modules/nixos/boot.nix +++ b/modules/nixos/disk.nix @@ -2,23 +2,20 @@ let facterLib = import ../../lib/lib.nix lib; - cfg = config.facter.detected.boot; + cfg = config.facter.detected.boot.disk; inherit (config.facter) report; in { - options.facter.detected.boot.enable = lib.mkEnableOption "Enable the Facter Boot module" // { + options.facter.detected.boot.disk.enable = lib.mkEnableOption "Enable Disk drivers in initrd" // { default = true; }; config = - with lib; - mkIf cfg.enable { + lib.mkIf cfg.enable { boot.initrd.availableKernelModules = facterLib.stringSet ( facterLib.collectDrivers ( - # Needed if we want to use the keyboard when things go wrong in the initrd. - (report.hardware.usb_controller or [ ]) # A disk might be attached. - ++ (report.hardware.firewire_controller or [ ]) + (report.hardware.firewire_controller or [ ]) # definitely important ++ (report.hardware.disk or [ ]) ++ (report.hardware.storage_controller or [ ]) diff --git a/modules/nixos/facter.nix b/modules/nixos/facter.nix index c3e2c5e..0e21f43 100644 --- a/modules/nixos/facter.nix +++ b/modules/nixos/facter.nix @@ -11,7 +11,8 @@ in { imports = [ ./bluetooth.nix - ./boot.nix + ./disk.nix + ./keyboard.nix ./firmware.nix ./graphics.nix ./networking diff --git a/modules/nixos/keyboard.nix b/modules/nixos/keyboard.nix new file mode 100644 index 0000000..cb819b4 --- /dev/null +++ b/modules/nixos/keyboard.nix @@ -0,0 +1,22 @@ +{ lib, config, ... }: +let + facterLib = import ../../lib/lib.nix lib; + + cfg = config.facter.detected.boot.keyboard; + inherit (config.facter) report; +in +{ + options.facter.detected.boot.keyboard.enable = + lib.mkEnableOption "Enable Keyboard support in the initrd" + // { + default = true; + }; + + config = + lib.mkIf cfg.enable { + boot.initrd.availableKernelModules = facterLib.stringSet ( + # Needed if we want to use the keyboard when things go wrong in the initrd. + facterLib.collectDrivers (report.hardware.usb_controller or [ ]) + ); + }; +} From 7bc4cd41c33bc11e60f565de643df96dba59641e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 25 Sep 2024 15:24:54 +0200 Subject: [PATCH 3/5] graphics: exclude noveau kernel module --- modules/nixos/graphics.nix | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/nixos/graphics.nix b/modules/nixos/graphics.nix index 62c41cf..e34e0d8 100644 --- a/modules/nixos/graphics.nix +++ b/modules/nixos/graphics.nix @@ -1,16 +1,28 @@ { lib, config, ... }: let facterLib = import ../../lib/lib.nix lib; + cfg = config.facter.detected.graphics; in { - options.facter.detected.graphics.enable = lib.mkEnableOption "Enable the Graphics module" // { - default = builtins.length (config.facter.report.hardware.monitor or [ ]) > 0; + options.facter.detected = { + graphics.enable = lib.mkEnableOption "Enable the Graphics module" // { + default = builtins.length (config.facter.report.hardware.monitor or [ ]) > 0; + }; + 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" ( + facterLib.stringSet (facterLib.collectDrivers (config.facter.report.hardware.graphics_card or [ ])) + ); + description = '' + List of kernel modules to load at boot for the graphics card. + ''; + }; }; - config = lib.mkIf config.facter.detected.graphics.enable { + config = lib.mkIf cfg.enable { hardware.graphics.enable = lib.mkDefault true; - boot.initrd.kernelModules = facterLib.stringSet ( - facterLib.collectDrivers (config.facter.report.hardware.graphics_card or [ ]) - ); + boot.initrd.kernelModules = config.facter.detected.boot.graphics.kernelModules; }; } From fb7b157b4a8f0ae29304fdc7b16742cf2e0997cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 25 Sep 2024 17:35:05 +0200 Subject: [PATCH 4/5] disk: don't expose list instead of enable option the latter one is easier to introspect --- modules/nixos/disk.nix | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/modules/nixos/disk.nix b/modules/nixos/disk.nix index e51886f..dcd14ba 100644 --- a/modules/nixos/disk.nix +++ b/modules/nixos/disk.nix @@ -2,17 +2,12 @@ let facterLib = import ../../lib/lib.nix lib; - cfg = config.facter.detected.boot.disk; inherit (config.facter) report; in { - options.facter.detected.boot.disk.enable = lib.mkEnableOption "Enable Disk drivers in initrd" // { - default = true; - }; - - config = - lib.mkIf cfg.enable { - boot.initrd.availableKernelModules = facterLib.stringSet ( + options.facter.detected.boot.disk.kernelModules = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = facterLib.stringSet ( facterLib.collectDrivers ( # A disk might be attached. (report.hardware.firewire_controller or [ ]) @@ -21,5 +16,12 @@ in ++ (report.hardware.storage_controller or [ ]) ) ); - }; + description = '' + List of kernel modules that are needed to access the disk. + ''; + }; + + config = { + boot.initrd.availableKernelModules = config.facter.detected.boot.disk.kernelModules; + }; } From fdf703e13c24235ac4fd167b614e3a259a03e6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 25 Sep 2024 17:37:50 +0200 Subject: [PATCH 5/5] also expose keyboard kernel modules as a list --- modules/nixos/keyboard.nix | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/nixos/keyboard.nix b/modules/nixos/keyboard.nix index cb819b4..e8991e2 100644 --- a/modules/nixos/keyboard.nix +++ b/modules/nixos/keyboard.nix @@ -2,21 +2,19 @@ let facterLib = import ../../lib/lib.nix lib; - cfg = config.facter.detected.boot.keyboard; inherit (config.facter) report; in { - options.facter.detected.boot.keyboard.enable = - lib.mkEnableOption "Enable Keyboard support in the initrd" - // { - default = true; - }; + options.facter.detected.boot.keyboard.kernelModules = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = facterLib.collectDrivers (report.hardware.usb_controller or [ ]); + example = [ "usbhid" ]; + description = '' + List of kernel modules to include in the initrd to support the keyboard. + ''; + }; - config = - lib.mkIf cfg.enable { - boot.initrd.availableKernelModules = facterLib.stringSet ( - # Needed if we want to use the keyboard when things go wrong in the initrd. - facterLib.collectDrivers (report.hardware.usb_controller or [ ]) - ); - }; + config = { + boot.initrd.availableKernelModules = config.facter.detected.boot.keyboard.kernelModules; + }; }