From ec9cfceda600e9fd1585119c9dc6bc9fbe5358d1 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Tue, 7 Nov 2023 21:27:57 +0100 Subject: [PATCH] [Boot+Treewide] rename aviallon.boot.kernel to aviallon.boot.kernel.package Rename aviallon.boot.extraKCflags to aviallon.boot.kernel.addOptimizationAttributes Also add an option to add non-optimization attributes to kernel derivation. --- boot.nix | 73 ++++++++++++++++++++++++++---------------- desktop/general.nix | 2 +- security/hardening.nix | 2 +- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/boot.nix b/boot.nix index a2a2fd8..bf81359 100644 --- a/boot.nix +++ b/boot.nix @@ -104,7 +104,7 @@ let isXanmod = kernel: ! isNull (strings.match ".*(xanmod).*" kernel.modDirVersion); - kernelVersionOlder = ver: versionOlder cfg.kernel.version ver; + kernelVersionOlder = ver: versionOlder cfg.kernel.package.version ver; cfg = config.aviallon.boot; generalCfg = config.aviallon.general; @@ -173,23 +173,36 @@ in { type = types.attrsOf (types.oneOf [ types.bool types.int types.str (types.listOf types.str) ]); }; - kernel = mkOption { - description = "Linux kernel to use"; - default = options.boot.kernelPackages.default.kernel; - example = "pkgs.kernel"; - type = types.package; - }; + kernel = { + package = mkOption { + description = "Linux kernel to use"; + default = options.boot.kernelPackages.default.kernel; + example = "pkgs.kernel"; + type = types.package; + }; - extraKCflags = mkOption { - description = "If optimizations are enabled, add the specified values to kernel KCFLAGS"; - default = []; - type = types.listOf types.string; - example = [ "-fipa-pta" ]; + addAttributes = mkOption { + description = "Merge specified attributes to kernel derivation (via special overideAttrs)"; + default = {}; + type = with types; attrs; + example = { KCFLAGS = "-Wall"; }; + }; + + addOptimizationAttributes = mkOption { + description = "Merge specified attributes to kernel derivation IF aviallon.optimizations.enabled is true"; + default = {}; + type = with types; attrs; + example = { KCFLAGS = "-O3 -fipa-pta"; }; + }; }; removeKernelDRM = mkEnableOption "convert all EXPORT_SYMBOL_GPL to EXPORT_SYMBOL. Warning: might be illegal in your region."; }; + imports = [ + ( mkRemovedOptionModule [ "aviallon" "boot" "extraKCflags" ] "Replaced by aviallon.boot.kernel.addOptimizationAttributes attrset" ) + ]; + config = mkMerge [ { assertions = [ @@ -231,27 +244,33 @@ in { initrd.kernelModules = [ ]; initrd.availableKernelModules = [ "ehci_pci" ]; - kernelPackages = let - baseKernel = cfg.kernel; + kernelPackages = with myLib.debug; let + baseKernel = cfg.kernel.package; + # Possible CFLAGS source : (myLib.optimizations.makeOptimizationFlags {}).CFLAGS - kCflags = + kCflags = traceValWithPrefix "kCflags" ( [ "-march=${cpuConfig.arch}" "-mtune=${cpuConfig.tune or cpuConfig.arch}" ] ++ optional (! isNull cpuConfig.caches.lastLevel ) "--param l2-cache-size=${toString cpuConfig.caches.lastLevel}" ++ optional (! isNull cpuConfig.caches.l1d ) "--param l1-cache-size=${toString cpuConfig.caches.l1d}" - ++ cfg.extraKCflags; - optimizedKernel = - if config.aviallon.optimizations.enable then - baseKernel.overrideAttrs (old: { - KCFLAGS = (old.KCFLAGS or "") + (toString kCflags); - passthru = baseKernel.passthru; - }) - else - baseKernel - ; - in mkOverride 2 (pkgs.linuxPackagesFor optimizedKernel); + ); + + optimizedKernelAttrs = traceValWithPrefix "optimizedKernelAttrs" ( + optionalAttrs config.aviallon.optimizations.enable ( + myLib.attrsets.mergeAttrsRecursive + { + KCFLAGS = kCflags; + } + (traceValWithPrefix "aviallon.boot.kernel.addOptimizationAttributes" cfg.kernel.addOptimizationAttributes) + ) + ); + moddedKernelAttrs = traceValWithPrefix "moddedKernelAttrs" ( + myLib.attrsets.mergeAttrsRecursive (traceValWithPrefix "aviallon.boot.kernel.addAttributes" cfg.kernel.addAttributes) optimizedKernelAttrs + ); + moddedKernel = myLib.optimizations.addAttrs baseKernel moddedKernelAttrs; + in mkOverride 2 (pkgs.linuxPackagesFor moddedKernel); kernelPatches = [] ++ optional cfg.x32abi.enable customKernelPatches.enableX32ABI @@ -259,7 +278,7 @@ in { ++ optional cfg.energyModel.enable customKernelPatches.enableEnergyModel ++ optional (cfg.patches.amdClusterId.enable && kernelVersionOlder "6.4") customKernelPatches.amdClusterId ++ optional (cfg.patches.zenLLCIdle.enable && kernelVersionOlder "6.5") customKernelPatches.backports.zenLLCIdle - ++ optional (isXanmod cfg.kernel && config.aviallon.optimizations.enable) (customKernelPatches.optimizeForCPUArch config.aviallon.general.cpu.arch) + ++ optional (isXanmod cfg.kernel.package && config.aviallon.optimizations.enable) (customKernelPatches.optimizeForCPUArch config.aviallon.general.cpu.arch) ++ optional cfg.removeKernelDRM customKernelPatches.removeKernelDRM ; diff --git a/desktop/general.nix b/desktop/general.nix index f4fdb2e..9114b2b 100644 --- a/desktop/general.nix +++ b/desktop/general.nix @@ -57,7 +57,7 @@ in { { aviallon.network.backend = mkDefault "NetworkManager"; - aviallon.boot.kernel = pkgs.linuxKernel.kernels.linux_xanmod; + aviallon.boot.kernel.package = pkgs.linuxKernel.kernels.linux_xanmod; # Enable the X11 windowing system. services.xserver.enable = true; diff --git a/security/hardening.nix b/security/hardening.nix index 72e6be5..a03d076 100644 --- a/security/hardening.nix +++ b/security/hardening.nix @@ -41,7 +41,7 @@ in # imports = [ # (modulesPath + "/profiles/hardened.nix") # ]; - aviallon.boot.kernel = mkIf cfg.hardcore pkgs.linuxKernel.kernels.linux_hardened; + aviallon.boot.kernel.package = mkIf cfg.hardcore pkgs.linuxKernel.kernels.linux_hardened; security.lockKernelModules = mkIf cfg.hardcore (mkOverride 500 true); # security.protectKernelImage = mkIf cfg.hardcore (mkOverride 500 false); # needed for kexec