From 03554797153aa90263161784c296ef6518af3358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 20 Dec 2022 18:38:47 +0100 Subject: [PATCH 1/6] lib/versions: add `pad` Pad a version string with zeros to match a given number of components. --- lib/tests/misc.nix | 15 +++++++++++++++ lib/versions.nix | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index c719fcf5d4fae..faf2b96530c1c 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -212,6 +212,21 @@ runTests { expected = [ "1" "2" "3" ]; }; + testPadVersionLess = { + expr = versions.pad 3 "1.2"; + expected = "1.2.0"; + }; + + testPadVersionLessExtra = { + expr = versions.pad 3 "1.3-rc1"; + expected = "1.3.0-rc1"; + }; + + testPadVersionMore = { + expr = versions.pad 3 "1.2.3.4"; + expected = "1.2.3"; + }; + testIsStorePath = { expr = let goodPath = diff --git a/lib/versions.nix b/lib/versions.nix index 0e9d81ac78b1e..986e7e5f9b37d 100644 --- a/lib/versions.nix +++ b/lib/versions.nix @@ -46,4 +46,19 @@ rec { builtins.concatStringsSep "." (lib.take 2 (splitVersion v)); + /* Pad a version string with zeros to match the given number of components. + + Example: + pad 3 "1.2" + => "1.2.0" + pad 3 "1.3-rc1" + => "1.3.0-rc1" + pad 3 "1.2.3.4" + => "1.2.3" + */ + pad = n: version: let + numericVersion = lib.head (lib.splitString "-" version); + versionSuffix = lib.removePrefix numericVersion version; + in lib.concatStringsSep "." (lib.take n (lib.splitVersion numericVersion ++ lib.genList (_: "0") n)) + versionSuffix; + } From fc346c5e6282c8e5a4115da7420505a2fbdd3d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Wed, 21 Dec 2022 12:49:37 +0100 Subject: [PATCH 2/6] linux: use `lib.versions.pad` for `modDirVersion` --- pkgs/os-specific/linux/kernel/generic.nix | 9 +++++---- pkgs/os-specific/linux/kernel/linux-4.14.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-4.19.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-5.10.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-5.15.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-5.4.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-6.0.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-6.1.nix | 3 ++- pkgs/os-specific/linux/kernel/linux-rt-5.10.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-testing.nix | 3 ++- pkgs/os-specific/linux/kernel/manual-config.nix | 7 ++++--- pkgs/os-specific/linux/kernel/xanmod-kernels.nix | 2 +- pkgs/os-specific/linux/kernel/zen-kernels.nix | 2 +- pkgs/top-level/all-packages.nix | 2 +- pkgs/top-level/linux-kernels.nix | 6 +++--- 15 files changed, 32 insertions(+), 23 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index 5a4c2858f95f7..dce2ed074a05a 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -29,7 +29,8 @@ structuredExtraConfig ? {} , # The version number used for the module directory - modDirVersion ? version + # If unspecified, this is determined automatically from the version. + modDirVersion ? null , # An attribute set whose attributes express the availability of # certain features in this kernel. E.g. `{iwlwifi = true;}' @@ -195,15 +196,15 @@ let }; # end of configfile derivation kernel = (callPackage ./manual-config.nix { inherit buildPackages; }) (basicArgs // { - inherit modDirVersion kernelPatches randstructSeed lib stdenv extraMakeFlags extraMeta configfile; + inherit kernelPatches randstructSeed lib stdenv extraMakeFlags extraMeta configfile; pos = builtins.unsafeGetAttrPos "version" args; config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; }; - }); + } // lib.optionalAttrs (modDirVersion != null) { inherit modDirVersion; }); passthru = basicArgs // { features = kernelFeatures; - inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre modDirVersion; + inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre; isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true; passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]); tests = let diff --git a/pkgs/os-specific/linux/kernel/linux-4.14.nix b/pkgs/os-specific/linux/kernel/linux-4.14.nix index 02893f731e3ee..777aa852650a6 100644 --- a/pkgs/os-specific/linux/kernel/linux-4.14.nix +++ b/pkgs/os-specific/linux/kernel/linux-4.14.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "4.14.302"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "102c9h0byr9v4bxzkdh7mnw1grm47ji6lf6l1gjlwah7f46j6ap3"; }; } // (args.argsOverride or {})) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-4.19.nix b/pkgs/os-specific/linux/kernel/linux-4.19.nix index 44a976ef0c7d9..6c5531afd6fdf 100644 --- a/pkgs/os-specific/linux/kernel/linux-4.19.nix +++ b/pkgs/os-specific/linux/kernel/linux-4.19.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "4.19.269"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "02mjb16xxfj984vibpxvhjl84y5yg0jgzjccjdxnn8db4k9aa2vf"; }; } // (args.argsOverride or {})) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.10.nix b/pkgs/os-specific/linux/kernel/linux-5.10.nix index 5614aeb1cabfc..7662f6be96c9c 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.10.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.10.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "5.10.159"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "19yfi5vknxnw0cb8274q3pb5zjs6ny04n16m8xjdfdmznrbvza8v"; }; } // (args.argsOverride or {})) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.15.nix b/pkgs/os-specific/linux/kernel/linux-5.15.nix index 8e07ff461ef69..f72a304d30c9e 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.15.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.15.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "5.15.83"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "1wvzfhzqq9dps508wmp2gblfz93ipppnjzqm0n8pi1acq11hhna0"; }; } // (args.argsOverride or { })) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.4.nix b/pkgs/os-specific/linux/kernel/linux-5.4.nix index b45ef4f3b502c..b41f18d400a39 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.4.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.4.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "5.4.227"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "14q5gy48j78vwnqivrgpdhj778n2jq5l7yiw5na1rwqmfh1wbvsy"; }; } // (args.argsOverride or {})) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-6.0.nix b/pkgs/os-specific/linux/kernel/linux-6.0.nix index 2bd2a3f07c1d1..ea98462bdf40e 100644 --- a/pkgs/os-specific/linux/kernel/linux-6.0.nix +++ b/pkgs/os-specific/linux/kernel/linux-6.0.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "6.0.13"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "191dlxcmbx8vy6z2k04jq2kr6hwnaknsnsyycvqnjmvmdf6i3lq8"; }; } // (args.argsOverride or { })) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-6.1.nix b/pkgs/os-specific/linux/kernel/linux-6.1.nix index 64ed1a717caf2..aab12f77786ae 100644 --- a/pkgs/os-specific/linux/kernel/linux-6.1.nix +++ b/pkgs/os-specific/linux/kernel/linux-6.1.nix @@ -6,7 +6,7 @@ buildLinux (args // rec { version = "6.1"; # modDirVersion needs to be x.y.z, will automatically add .0 if needed - modDirVersion = if (modDirVersionArg == null) then concatStringsSep "." (take 3 (splitVersion "${version}.0")) else modDirVersionArg; + modDirVersion = versions.pad 3 version; # branchVersion needs to be x.y extraMeta.branch = versions.majorMinor version; @@ -16,3 +16,4 @@ buildLinux (args // rec { sha256 = "sha256-LKHxcFGkMPb+0RluSVJxdQcXGs/ZfZZXchJQJwOyXes="; }; } // (args.argsOverride or { })) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-rt-5.10.nix b/pkgs/os-specific/linux/kernel/linux-rt-5.10.nix index 103b142054af7..a97b1acb5aad9 100644 --- a/pkgs/os-specific/linux/kernel/linux-rt-5.10.nix +++ b/pkgs/os-specific/linux/kernel/linux-rt-5.10.nix @@ -13,8 +13,7 @@ in buildLinux (args // { inherit version; # modDirVersion needs a patch number, change X.Y-rtZ to X.Y.0-rtZ. - modDirVersion = if (builtins.match "[^.]*[.][^.]*-.*" version) == null then version - else lib.replaceStrings ["-"] [".0-"] version; + modDirVersion = lib.versions.pad 3 version; src = fetchurl { url = "mirror://kernel/linux/kernel/v5.x/linux-${kversion}.tar.xz"; diff --git a/pkgs/os-specific/linux/kernel/linux-testing.nix b/pkgs/os-specific/linux/kernel/linux-testing.nix index 76db1b11bbd25..859df2c53e6cf 100644 --- a/pkgs/os-specific/linux/kernel/linux-testing.nix +++ b/pkgs/os-specific/linux/kernel/linux-testing.nix @@ -7,7 +7,7 @@ buildLinux (args // rec { extraMeta.branch = lib.versions.majorMinor version; # modDirVersion needs to be x.y.z, will always add .0 - modDirVersion = if (modDirVersionArg == null) then builtins.replaceStrings ["-"] [".0-"] version else modDirVersionArg; + modDirVersion = versions.pad 3 version; src = fetchurl { url = "https://git.kernel.org/torvalds/t/linux-${version}.tar.gz"; @@ -18,3 +18,4 @@ buildLinux (args // rec { extraMeta.hydraPlatforms = []; } // (args.argsOverride or {})) + // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix index edfd1f7dbc28a..1ba5495fc90e7 100644 --- a/pkgs/os-specific/linux/kernel/manual-config.nix +++ b/pkgs/os-specific/linux/kernel/manual-config.nix @@ -22,8 +22,9 @@ in { pos ? null, # Additional kernel make flags extraMakeFlags ? [], - # The version of the kernel module directory - modDirVersion ? version, + # The name of the kernel module directory + # Needs to be X.Y.Z[-extra], so pad with zeros if needed. + modDirVersion ? lib.versions.pad 3 version, # The kernel source (tarball, git checkout, etc.) src, # a list of { name=..., patch=..., extraConfig=...} patches @@ -36,7 +37,7 @@ in { # Custom seed used for CONFIG_GCC_PLUGIN_RANDSTRUCT if enabled. This is # automatically extended with extra per-version and per-config values. randstructSeed ? "", - # Use defaultMeta // extraMeta + # Extra meta attributes extraMeta ? {}, # for module compatibility diff --git a/pkgs/os-specific/linux/kernel/xanmod-kernels.nix b/pkgs/os-specific/linux/kernel/xanmod-kernels.nix index cd1c827780722..42997b5302ec8 100644 --- a/pkgs/os-specific/linux/kernel/xanmod-kernels.nix +++ b/pkgs/os-specific/linux/kernel/xanmod-kernels.nix @@ -16,7 +16,7 @@ let xanmodKernelFor = { version, suffix ? "xanmod1", hash, variant }: buildLinux (args // rec { inherit version; - modDirVersion = "${version}-${suffix}"; + modDirVersion = lib.versions.pad 3 "${version}-${suffix}"; src = fetchFromGitHub { owner = "xanmod"; diff --git a/pkgs/os-specific/linux/kernel/zen-kernels.nix b/pkgs/os-specific/linux/kernel/zen-kernels.nix index a58782828478a..08671e83ffc51 100644 --- a/pkgs/os-specific/linux/kernel/zen-kernels.nix +++ b/pkgs/os-specific/linux/kernel/zen-kernels.nix @@ -18,7 +18,7 @@ let }; zenKernelsFor = { version, suffix, sha256, isLqx }: buildLinux (args // { inherit version; - modDirVersion = "${lib.concatStringsSep "." (lib.take 3 (lib.splitVersion version ++ [ "0" "0" ]))}-${suffix}"; + modDirVersion = lib.versions.pad 3 "${version}-${suffix}"; isZen = true; src = fetchFromGitHub { diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index bff0e2f7591c6..53f4f77a0cbfe 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -25470,7 +25470,7 @@ with pkgs; linuxPackages_custom_tinyconfig_kernel = let base = linuxPackages.kernel; tinyLinuxPackages = linuxKernel.customPackage { - inherit (base) version src; + inherit (base) version modDirVersion src; allowImportFromDerivation = false; configfile = linuxConfig { makeTarget = "tinyconfig"; diff --git a/pkgs/top-level/linux-kernels.nix b/pkgs/top-level/linux-kernels.nix index ac0511a649bf1..da4270896f67d 100644 --- a/pkgs/top-level/linux-kernels.nix +++ b/pkgs/top-level/linux-kernels.nix @@ -40,6 +40,7 @@ let }; argsOverride = { inherit version; + modDirVersion = modDirVersion' + kernelPatches.hardened.${kernel.meta.branch}.extra; src = fetchurl { url = "mirror://kernel/linux/kernel/v${major}.x/linux-${version}.tar.xz"; inherit sha256; @@ -48,7 +49,6 @@ let kernelPatches = kernel.kernelPatches ++ [ kernelPatches.hardened.${kernel.meta.branch} ]; - modDirVersionArg = modDirVersion' + (kernelPatches.hardened.${kernel.meta.branch}).extra; isHardened = true; }; in { @@ -595,9 +595,9 @@ in { manualConfig = makeOverridable (callPackage ../os-specific/linux/kernel/manual-config.nix {}); - customPackage = { version, src, configfile, allowImportFromDerivation ? true }: + customPackage = { version, src, modDirVersion ? lib.versions.pad 3 version, configfile, allowImportFromDerivation ? true }: recurseIntoAttrs (packagesFor (manualConfig { - inherit version src configfile lib stdenv allowImportFromDerivation; + inherit version src modDirVersion configfile lib stdenv allowImportFromDerivation; })); # Derive one of the default .config files From cf568d20d5bc094292fc02fdce92201b11a7f169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 20 Dec 2022 14:15:04 +0100 Subject: [PATCH 3/6] linux: remove `modDirVersionArg` argument It's redundant with `argsOverride.modDirVersion`. --- pkgs/os-specific/linux/kernel/linux-4.14.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-4.19.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-5.10.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-5.15.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-5.4.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-6.0.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-6.1.nix | 3 +-- pkgs/os-specific/linux/kernel/linux-testing.nix | 3 +-- 8 files changed, 8 insertions(+), 16 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/linux-4.14.nix b/pkgs/os-specific/linux/kernel/linux-4.14.nix index 777aa852650a6..0b954a0ef902c 100644 --- a/pkgs/os-specific/linux/kernel/linux-4.14.nix +++ b/pkgs/os-specific/linux/kernel/linux-4.14.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "102c9h0byr9v4bxzkdh7mnw1grm47ji6lf6l1gjlwah7f46j6ap3"; }; } // (args.argsOverride or {})) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-4.19.nix b/pkgs/os-specific/linux/kernel/linux-4.19.nix index 6c5531afd6fdf..07583ccb4df6c 100644 --- a/pkgs/os-specific/linux/kernel/linux-4.19.nix +++ b/pkgs/os-specific/linux/kernel/linux-4.19.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "02mjb16xxfj984vibpxvhjl84y5yg0jgzjccjdxnn8db4k9aa2vf"; }; } // (args.argsOverride or {})) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.10.nix b/pkgs/os-specific/linux/kernel/linux-5.10.nix index 7662f6be96c9c..cd74e6f29372a 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.10.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.10.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "19yfi5vknxnw0cb8274q3pb5zjs6ny04n16m8xjdfdmznrbvza8v"; }; } // (args.argsOverride or {})) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.15.nix b/pkgs/os-specific/linux/kernel/linux-5.15.nix index f72a304d30c9e..ee73320163d1a 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.15.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.15.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "1wvzfhzqq9dps508wmp2gblfz93ipppnjzqm0n8pi1acq11hhna0"; }; } // (args.argsOverride or { })) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-5.4.nix b/pkgs/os-specific/linux/kernel/linux-5.4.nix index b41f18d400a39..3e219578fd674 100644 --- a/pkgs/os-specific/linux/kernel/linux-5.4.nix +++ b/pkgs/os-specific/linux/kernel/linux-5.4.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "14q5gy48j78vwnqivrgpdhj778n2jq5l7yiw5na1rwqmfh1wbvsy"; }; } // (args.argsOverride or {})) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-6.0.nix b/pkgs/os-specific/linux/kernel/linux-6.0.nix index ea98462bdf40e..c15f0d7338cca 100644 --- a/pkgs/os-specific/linux/kernel/linux-6.0.nix +++ b/pkgs/os-specific/linux/kernel/linux-6.0.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "191dlxcmbx8vy6z2k04jq2kr6hwnaknsnsyycvqnjmvmdf6i3lq8"; }; } // (args.argsOverride or { })) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-6.1.nix b/pkgs/os-specific/linux/kernel/linux-6.1.nix index aab12f77786ae..342f2b4ea7620 100644 --- a/pkgs/os-specific/linux/kernel/linux-6.1.nix +++ b/pkgs/os-specific/linux/kernel/linux-6.1.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -16,4 +16,3 @@ buildLinux (args // rec { sha256 = "sha256-LKHxcFGkMPb+0RluSVJxdQcXGs/ZfZZXchJQJwOyXes="; }; } // (args.argsOverride or { })) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy diff --git a/pkgs/os-specific/linux/kernel/linux-testing.nix b/pkgs/os-specific/linux/kernel/linux-testing.nix index 859df2c53e6cf..f9060325051c7 100644 --- a/pkgs/os-specific/linux/kernel/linux-testing.nix +++ b/pkgs/os-specific/linux/kernel/linux-testing.nix @@ -1,4 +1,4 @@ -{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, modDirVersionArg ? null, ... } @ args: +{ lib, buildPackages, fetchurl, perl, buildLinux, nixosTests, ... } @ args: with lib; @@ -18,4 +18,3 @@ buildLinux (args // rec { extraMeta.hydraPlatforms = []; } // (args.argsOverride or {})) - // lib.optionalAttrs (modDirVersionArg != null) { modDirVersion = modDirVersionArg; } # legacy From f677cbabe93e039a9d4ac995a49f9ca7991d0a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 20 Dec 2022 14:28:47 +0100 Subject: [PATCH 4/6] nixos/tests: remove minimal-kernel module It's not used, doesn't build, and seems like the only reason to have `manualConfig` take `stdenv` as an argument. --- nixos/lib/testing/legacy.nix | 3 ++- nixos/lib/testing/nodes.nix | 10 +-------- nixos/modules/testing/minimal-kernel.nix | 28 ------------------------ 3 files changed, 3 insertions(+), 38 deletions(-) delete mode 100644 nixos/modules/testing/minimal-kernel.nix diff --git a/nixos/lib/testing/legacy.nix b/nixos/lib/testing/legacy.nix index 868b8b65b17d5..b310575566015 100644 --- a/nixos/lib/testing/legacy.nix +++ b/nixos/lib/testing/legacy.nix @@ -3,9 +3,10 @@ let inherit (lib) mkIf mkOption types; in { - # This needs options.warnings, which we don't have (yet?). + # This needs options.warnings and options.assertions, which we don't have (yet?). # imports = [ # (lib.mkRenamedOptionModule [ "machine" ] [ "nodes" "machine" ]) + # (lib.mkRemovedOptionModule [ "minimal" ] "The minimal kernel module was removed as it was broken and not used any more in nixpkgs.") # ]; options = { diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index 8e620c96b3bb1..c538ab468c526 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -23,7 +23,7 @@ let nixpkgs.config.allowAliases = false; }) testModuleArgs.config.extraBaseModules - ] ++ optional config.minimal ../../modules/testing/minimal-kernel.nix; + ]; }; @@ -78,14 +78,6 @@ in ''; }; - minimal = mkOption { - type = types.bool; - default = false; - description = mdDoc '' - Enable to configure all [{option}`nodes`](#test-opt-nodes) to run with a minimal kernel. - ''; - }; - nodesCompat = mkOption { internal = true; description = mdDoc '' diff --git a/nixos/modules/testing/minimal-kernel.nix b/nixos/modules/testing/minimal-kernel.nix deleted file mode 100644 index 7c2b9c05cf9a0..0000000000000 --- a/nixos/modules/testing/minimal-kernel.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ config, pkgs, lib, ... }: - -let - configfile = builtins.storePath (builtins.toFile "config" (lib.concatStringsSep "\n" - (map (builtins.getAttr "configLine") config.system.requiredKernelConfig)) - ); - - origKernel = pkgs.buildLinux { - inherit (pkgs.linux) src version stdenv; - inherit configfile; - allowImportFromDerivation = true; - kernelPatches = [ pkgs.kernelPatches.cifs_timeout_2_6_38 ]; - }; - - kernel = origKernel // (derivation (origKernel.drvAttrs // { - configurePhase = '' - runHook preConfigure - mkdir ../build - make $makeFlags "''${makeFlagsArray[@]}" mrproper - make $makeFlags "''${makeFlagsArray[@]}" KCONFIG_ALLCONFIG=${configfile} allnoconfig - runHook postConfigure - ''; - })); - - kernelPackages = pkgs.linuxPackagesFor kernel; -in { - boot.kernelPackages = kernelPackages; -} From 6c563f30fe9415fff6ab33df738a7b9ddc84cf4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 20 Dec 2022 14:42:38 +0100 Subject: [PATCH 5/6] linuxManualConfig: don't require lib and stdenv arguments Reverts https://github.com/NixOS/nixpkgs/commit/7c7c83e2335c3aa6f26a4f9c905c49c430b5be09 which was only needed for the minimal-kernel.nix test module and clutters the call site. stdenv can still be overridden with `linuxManualConfig.override { stdenv = ...; }`. --- pkgs/os-specific/linux/kernel/generic.nix | 4 ++-- pkgs/os-specific/linux/kernel/manual-config.nix | 14 +++++++------- pkgs/top-level/linux-kernels.nix | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index dce2ed074a05a..51ad268a5fe73 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -195,8 +195,8 @@ let }; }; # end of configfile derivation - kernel = (callPackage ./manual-config.nix { inherit buildPackages; }) (basicArgs // { - inherit kernelPatches randstructSeed lib stdenv extraMakeFlags extraMeta configfile; + kernel = (callPackage ./manual-config.nix { inherit lib stdenv buildPackages; }) (basicArgs // { + inherit kernelPatches randstructSeed extraMakeFlags extraMeta configfile; pos = builtins.unsafeGetAttrPos "version" args; config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; }; diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix index 1ba5495fc90e7..45281d5d3bdb4 100644 --- a/pkgs/os-specific/linux/kernel/manual-config.nix +++ b/pkgs/os-specific/linux/kernel/manual-config.nix @@ -1,8 +1,11 @@ -{ lib, buildPackages, runCommand, nettools, bc, bison, flex, perl, rsync, gmp, libmpc, mpfr, openssl +{ lib, stdenv, buildPackages, runCommand, nettools, bc, bison, flex, perl, rsync, gmp, libmpc, mpfr, openssl , libelf, cpio, elfutils, zstd, python3Minimal, zlib, pahole }: let + lib_ = lib; + stdenv_ = stdenv; + readConfig = configfile: import (runCommand "config.nix" {} '' echo "{" > "$out" while IFS='=' read key val; do @@ -12,10 +15,7 @@ let done < "${configfile}" echo "}" >> $out '').outPath; -in { - lib, - # Allow overriding stdenv on each buildLinux call - stdenv, +in lib.makeOverridable ({ # The kernel version version, # Position of the Linux build expression @@ -48,7 +48,7 @@ in { # Whether to utilize the controversial import-from-derivation feature to parse the config allowImportFromDerivation ? false, # ignored - features ? null, + features ? null, lib ? lib_, stdenv ? stdenv_, }: let @@ -387,4 +387,4 @@ stdenv.mkDerivation ((drvAttrs config stdenv.hostPlatform.linux-kernel kernelPat ++ extraMakeFlags; karch = stdenv.hostPlatform.linuxArch; -} // (optionalAttrs (pos != null) { inherit pos; })) +} // (optionalAttrs (pos != null) { inherit pos; }))) diff --git a/pkgs/top-level/linux-kernels.nix b/pkgs/top-level/linux-kernels.nix index da4270896f67d..d6c9bd3ceeaef 100644 --- a/pkgs/top-level/linux-kernels.nix +++ b/pkgs/top-level/linux-kernels.nix @@ -593,11 +593,11 @@ in { linux_hardkernel_latest = packages.hardkernel_4_14; }; - manualConfig = makeOverridable (callPackage ../os-specific/linux/kernel/manual-config.nix {}); + manualConfig = callPackage ../os-specific/linux/kernel/manual-config.nix {}; customPackage = { version, src, modDirVersion ? lib.versions.pad 3 version, configfile, allowImportFromDerivation ? true }: recurseIntoAttrs (packagesFor (manualConfig { - inherit version src modDirVersion configfile lib stdenv allowImportFromDerivation; + inherit version src modDirVersion configfile allowImportFromDerivation; })); # Derive one of the default .config files From a8fd50b79c899b3b4958f8bb95bb5717906a37b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 20 Dec 2022 16:49:27 +0100 Subject: [PATCH 6/6] nixos/doc: update custom kernel instructions Document the `linux.override` way first, then `linuxManualConfig`. Add a `linux.configEnv` passthru attribute for quickly getting a `make nconfig`-ready shell. --- .../configuration/linux-kernel.chapter.md | 83 +++++++------- .../configuration/linux-kernel.chapter.xml | 105 ++++++++++-------- pkgs/os-specific/linux/kernel/generic.nix | 9 ++ 3 files changed, 115 insertions(+), 82 deletions(-) diff --git a/nixos/doc/manual/configuration/linux-kernel.chapter.md b/nixos/doc/manual/configuration/linux-kernel.chapter.md index 7b84416a86465..f5bce99dd1bbe 100644 --- a/nixos/doc/manual/configuration/linux-kernel.chapter.md +++ b/nixos/doc/manual/configuration/linux-kernel.chapter.md @@ -82,61 +82,68 @@ boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 120; sets the kernel's TCP keepalive time to 120 seconds. To see the available parameters, run `sysctl -a`. -## Customize your kernel {#sec-linux-config-customizing} +## Building a custom kernel {#sec-linux-config-customizing} -The first step before compiling the kernel is to generate an appropriate -`.config` configuration. Either you pass your own config via the -`configfile` setting of `linuxKernel.manualConfig`: +You can customize the default kernel configuration by overriding the arguments for your kernel package: ```nix -custom-kernel = let base_kernel = linuxKernel.kernels.linux_4_9; - in super.linuxKernel.manualConfig { - inherit (super) stdenv hostPlatform; - inherit (base_kernel) src; - version = "${base_kernel.version}-custom"; - - configfile = /home/me/my_kernel_config; - allowImportFromDerivation = true; -}; +pkgs.linux_latest.override { + ignoreConfigErrors = true; + autoModules = false; + kernelPreferBuiltin = true; + extraStructuredConfig = with lib.kernel; { + DEBUG_KERNEL = yes; + FRAME_POINTER = yes; + KGDB = yes; + KGDB_SERIAL_CONSOLE = yes; + DEBUG_INFO = yes; + }; +} ``` -You can edit the config with this snippet (by default `make - menuconfig` won\'t work out of the box on nixos): +See `pkgs/os-specific/linux/kernel/generic.nix` for details on how these arguments +affect the generated configuration. You can also build a custom version of Linux by calling +`pkgs.buildLinux` directly, which requires the `src` and `version` arguments to be specified. -```ShellSession -nix-shell -E 'with import {}; kernelToOverride.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkg-config ncurses ];})' +To use your custom kernel package in your NixOS configuration, set + +```nix +boot.kernelPackages = pkgs.linuxPackagesFor yourCustomKernel; ``` -or you can let nixpkgs generate the configuration. Nixpkgs generates it -via answering the interactive kernel utility `make config`. The answers -depend on parameters passed to -`pkgs/os-specific/linux/kernel/generic.nix` (which you can influence by -overriding `extraConfig, autoModules, - modDirVersion, preferBuiltin, extraConfig`). +Note that this method will use the common configuration defined in `pkgs/os-specific/linux/kernel/common-config.nix`, +which is suitable for a NixOS system. + +If you already have a generated configuration file, you can build a kernel that uses it with `pkgs.linuxManualConfig`: ```nix -mptcp93.override ({ - name="mptcp-local"; +let + baseKernel = pkgs.linux_latest; +in pkgs.linuxManualConfig { + inherit (baseKernel) src modDirVersion; + version = "${baseKernel.version}-custom"; + configfile = ./my_kernel_config; + allowImportFromDerivation = true; +} +``` - ignoreConfigErrors = true; - autoModules = false; - kernelPreferBuiltin = true; +::: {.note} +The build will fail if `modDirVersion` does not match the source's `kernel.release` file, +so `modDirVersion` should remain tied to `src`. +::: - enableParallelBuilding = true; +To edit the `.config` file for Linux X.Y, proceed as follows: - extraConfig = '' - DEBUG_KERNEL y - FRAME_POINTER y - KGDB y - KGDB_SERIAL_CONSOLE y - DEBUG_INFO y - ''; -}); +```ShellSession +$ nix-shell '' -A linuxKernel.kernels.linux_X_Y.configEnv +$ unpackPhase +$ cd linux-* +$ make nconfig ``` ## Developing kernel modules {#sec-linux-config-developing-modules} -When developing kernel modules it\'s often convenient to run +When developing kernel modules it's often convenient to run edit-compile-run loop as quickly as possible. See below snippet as an example of developing `mellanox` drivers. diff --git a/nixos/doc/manual/from_md/configuration/linux-kernel.chapter.xml b/nixos/doc/manual/from_md/configuration/linux-kernel.chapter.xml index dd570e1d66c27..058a890d7a3e3 100644 --- a/nixos/doc/manual/from_md/configuration/linux-kernel.chapter.xml +++ b/nixos/doc/manual/from_md/configuration/linux-kernel.chapter.xml @@ -96,65 +96,82 @@ boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 120; available parameters, run sysctl -a.
- Customize your kernel + Building a custom kernel - The first step before compiling the kernel is to generate an - appropriate .config configuration. Either you - pass your own config via the configfile setting - of linuxKernel.manualConfig: + You can customize the default kernel configuration by overriding + the arguments for your kernel package: -custom-kernel = let base_kernel = linuxKernel.kernels.linux_4_9; - in super.linuxKernel.manualConfig { - inherit (super) stdenv hostPlatform; - inherit (base_kernel) src; - version = "${base_kernel.version}-custom"; - - configfile = /home/me/my_kernel_config; - allowImportFromDerivation = true; -}; +pkgs.linux_latest.override { + ignoreConfigErrors = true; + autoModules = false; + kernelPreferBuiltin = true; + extraStructuredConfig = with lib.kernel; { + DEBUG_KERNEL = yes; + FRAME_POINTER = yes; + KGDB = yes; + KGDB_SERIAL_CONSOLE = yes; + DEBUG_INFO = yes; + }; +} - You can edit the config with this snippet (by default - make menuconfig won't work out of the box on - nixos): + See pkgs/os-specific/linux/kernel/generic.nix + for details on how these arguments affect the generated + configuration. You can also build a custom version of Linux by + calling pkgs.buildLinux directly, which + requires the src and version + arguments to be specified. - -nix-shell -E 'with import <nixpkgs> {}; kernelToOverride.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkg-config ncurses ];})' + + To use your custom kernel package in your NixOS configuration, set + + +boot.kernelPackages = pkgs.linuxPackagesFor yourCustomKernel; - or you can let nixpkgs generate the configuration. Nixpkgs - generates it via answering the interactive kernel utility - make config. The answers depend on parameters - passed to - pkgs/os-specific/linux/kernel/generic.nix - (which you can influence by overriding - extraConfig, autoModules, modDirVersion, preferBuiltin, extraConfig). + Note that this method will use the common configuration defined in + pkgs/os-specific/linux/kernel/common-config.nix, + which is suitable for a NixOS system. + + + If you already have a generated configuration file, you can build + a kernel that uses it with + pkgs.linuxManualConfig: -mptcp93.override ({ - name="mptcp-local"; - - ignoreConfigErrors = true; - autoModules = false; - kernelPreferBuiltin = true; - - enableParallelBuilding = true; - - extraConfig = '' - DEBUG_KERNEL y - FRAME_POINTER y - KGDB y - KGDB_SERIAL_CONSOLE y - DEBUG_INFO y - ''; -}); +let + baseKernel = pkgs.linux_latest; +in pkgs.linuxManualConfig { + inherit (baseKernel) src modDirVersion; + version = "${baseKernel.version}-custom"; + configfile = ./my_kernel_config; + allowImportFromDerivation = true; +} + + + + The build will fail if modDirVersion does not + match the source’s kernel.release file, so + modDirVersion should remain tied to + src. + + + + To edit the .config file for Linux X.Y, proceed + as follows: + + +$ nix-shell '<nixpkgs>' -A linuxKernel.kernels.linux_X_Y.configEnv +$ unpackPhase +$ cd linux-* +$ make nconfig
Developing kernel modules - When developing kernel modules it's often convenient to run + When developing kernel modules it’s often convenient to run edit-compile-run loop as quickly as possible. See below snippet as an example of developing mellanox drivers. diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index 51ad268a5fe73..5a39ef9150006 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -206,6 +206,15 @@ let features = kernelFeatures; inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre; isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true; + + # Adds dependencies needed to edit the config: + # nix-shell '' -A linux.configEnv --command 'make nconfig' + configEnv = kernel.overrideAttrs (old: { + nativeBuildInputs = old.nativeBuildInputs or [] ++ (with buildPackages; [ + pkg-config ncurses + ]); + }); + passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]); tests = let overridableKernel = finalKernel // {