diff options
author | Sam Grayson <sam@samgrayson.me> | 2023-10-25 23:09:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-25 23:09:33 -0500 |
commit | 14ffe0e240a19beafd39921ed0f9351fa63b6a7e (patch) | |
tree | db13b8508985ad3da040e60583df1cbfa37a38ab /nixos/modules/hardware/cpu | |
parent | da32d38b1c52ba6d324ae382ec9d852b7c9da0f0 (diff) | |
parent | 77ed358e3d7945116cd0641d10928b5ee14c4db1 (diff) | |
download | nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar.gz nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar.bz2 nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar.lz nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar.xz nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.tar.zst nixlib-14ffe0e240a19beafd39921ed0f9351fa63b6a7e.zip |
Merge branch 'NixOS:master' into patch-1
Diffstat (limited to 'nixos/modules/hardware/cpu')
-rw-r--r-- | nixos/modules/hardware/cpu/x86-msr.nix | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/nixos/modules/hardware/cpu/x86-msr.nix b/nixos/modules/hardware/cpu/x86-msr.nix new file mode 100644 index 000000000000..554bec1b7db1 --- /dev/null +++ b/nixos/modules/hardware/cpu/x86-msr.nix @@ -0,0 +1,91 @@ +{ lib +, config +, options +, ... +}: +let + inherit (builtins) hasAttr; + inherit (lib) mkIf mdDoc; + cfg = config.hardware.cpu.x86.msr; + opt = options.hardware.cpu.x86.msr; + defaultGroup = "msr"; + isDefaultGroup = cfg.group == defaultGroup; + set = "to set for devices of the `msr` kernel subsystem."; + + # Generates `foo=bar` parameters to pass to the kernel. + # If `module = baz` is passed, generates `baz.foo=bar`. + # Adds double quotes on demand to handle `foo="bar baz"`. + kernelParam = { module ? null }: name: value: + assert lib.asserts.assertMsg (!lib.strings.hasInfix "=" name) "kernel parameter cannot have '=' in name"; + let + key = (if module == null then "" else module + ".") + name; + valueString = lib.generators.mkValueStringDefault {} value; + quotedValueString = if lib.strings.hasInfix " " valueString + then lib.strings.escape ["\""] valueString + else valueString; + in "${key}=${quotedValueString}"; + msrKernelParam = kernelParam { module = "msr"; }; +in +{ + options.hardware.cpu.x86.msr = with lib.options; with lib.types; { + enable = mkEnableOption (mdDoc "the `msr` (Model-Specific Registers) kernel module and configure `udev` rules for its devices (usually `/dev/cpu/*/msr`)"); + owner = mkOption { + type = str; + default = "root"; + example = "nobody"; + description = mdDoc "Owner ${set}"; + }; + group = mkOption { + type = str; + default = defaultGroup; + example = "nobody"; + description = mdDoc "Group ${set}"; + }; + mode = mkOption { + type = str; + default = "0640"; + example = "0660"; + description = mdDoc "Mode ${set}"; + }; + settings = mkOption { + type = submodule { + freeformType = attrsOf (oneOf [ bool int str ]); + options.allow-writes = mkOption { + type = nullOr (enum ["on" "off"]); + default = null; + description = "Whether to allow writes to MSRs (`\"on\"`) or not (`\"off\"`)."; + }; + }; + default = {}; + description = "Parameters for the `msr` kernel module."; + }; + }; + + config = mkIf cfg.enable { + assertions = [ + { + assertion = hasAttr cfg.owner config.users.users; + message = "Owner '${cfg.owner}' set in `${opt.owner}` is not configured via `${options.users.users}.\"${cfg.owner}\"`."; + } + { + assertion = isDefaultGroup || (hasAttr cfg.group config.users.groups); + message = "Group '${cfg.group}' set in `${opt.group}` is not configured via `${options.users.groups}.\"${cfg.group}\"`."; + } + ]; + + boot = { + kernelModules = [ "msr" ]; + kernelParams = lib.attrsets.mapAttrsToList msrKernelParam (lib.attrsets.filterAttrs (_: value: value != null) cfg.settings); + }; + + users.groups.${cfg.group} = mkIf isDefaultGroup { }; + + services.udev.extraRules = '' + SUBSYSTEM=="msr", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}" + ''; + }; + + meta = with lib; { + maintainers = with maintainers; [ lorenzleutgeb ]; + }; +} |