Linux kernel: Difference between revisions

m update the link for developing kernel modules from the nixos manual to the nixpkgs manual
 
(8 intermediate revisions by 8 users not shown)
Line 24: Line 24:
</syntaxHighlight>
</syntaxHighlight>
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
nix-repl> :l <nixpkgs>
nix-repl> pkgs = import <nixpkgs> {}
Added 12607 variables.
Added 12607 variables.


Line 127: Line 127:
<syntaxhighlight lang=nix>
<syntaxhighlight lang=nix>
{
{
      boot.kernelPatches = [ {
  boot.kernelPatches = [ {
        name = "crashdump-config";
    name = "crashdump-config";
        patch = null;
    patch = null;
        extraConfig = ''
    structuredExtraConfig = with lib.kernel; {
                CRASH_DUMP y
      CRASH_DUMP = yes;
                DEBUG_INFO y
      DEBUG_INFO = yes;
                PROC_VMCORE y
      PROC_VMCORE = yes;
                LOCKUP_DETECTOR y
      LOCKUP_DETECTOR = yes;
                HARDLOCKUP_DETECTOR y
      HARDLOCKUP_DETECTOR = yes;
              '';
    };
        } ];
  } ];
}
}
</syntaxhighlight>
</syntaxhighlight>


Another way is to create a overlay to configure either {{ic|extraConfig}} or {{ic|structuredExtraConfig}}. This allows for more flexibility, since you can basically override any available kernel option<ref>[https://github.com/NixOS/nixpkgs/blob/e6db435973160591fe7348876a5567c729495175/pkgs/os-specific/linux/kernel/generic.nix#L11-L56<tt>pkgs/os-specific/linux/kernel/generic.nix#L11-L56</tt>]</ref>. Using {{ic|structuredExtraConfig}} is recommended as it checks if the configured option are correct. You may also want to set {{ic|ignoreConfigErrors}} to avoid {{ic|error: unused option}} during builds.
Another way is to create an overlay to configure {{ic|structuredExtraConfig}}. This allows for more flexibility, since you can basically override any available kernel option<ref>[https://github.com/NixOS/nixpkgs/blob/e6db435973160591fe7348876a5567c729495175/pkgs/os-specific/linux/kernel/generic.nix#L11-L56<tt>pkgs/os-specific/linux/kernel/generic.nix#L11-L56</tt>]</ref>.


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 152: Line 153:
             SCHED_MUQSS = yes;
             SCHED_MUQSS = yes;
           };
           };
           ignoreConfigErrors = true;
           ignoreConfigErrors = true; # not necessarily a good idea
         });
         });
       })
       })
Line 159: Line 160:
}
}
</nowiki>}}
</nowiki>}}
If the kernel config fails to build with messages like <code>error: unused option: DRM_I915_GVT</code>, try setting e.g. <code>DRM_I915_GVT = lib.kernel.unset</code>. Setting {{ic|ignoreConfigErrors}} will also silence these errors, at the cost of ignoring potential problems. ([https://github.com/NixOS/nixpkgs/blob/3ab9a9d12ba76f6a544c207e1364da5b3087b77c/pkgs/os-specific/linux/kernel/generate-config.pl generate-config.pl] is responsible for these; they are often produced when [https://github.com/NixOS/nixpkgs/blob/3ab9a9d12ba76f6a544c207e1364da5b3087b77c/pkgs/os-specific/linux/kernel/common-config.nix the default config] doesn't use <code>lib.kernel.option</code> for an option that is implicitly disabled by whatever change you made.)


=== Pinning a kernel version ===
=== Pinning a kernel version ===
Line 176: Line 179:
}
}
</nowiki>}}
</nowiki>}}
Note that pinning the kernel version will likely require manual recompilation, which may be slow on older hardware. See [https://discourse.nixos.org/t/pin-kernel-version/50630/3 this thread] for a related discussion.


=== Debugging a failed configuration  ===
=== Debugging a failed configuration  ===
Line 261: Line 266:
           kernelPatches = [];
           kernelPatches = [];


           extraConfig = ''
           structuredExtraConfig = with lib.kernel; {
             INTEL_SGX y
             INTEL_SGX = yes;
           '';
           };


           extraMeta.branch = "5.4";
           extraMeta.branch = "5.4";
Line 277: Line 282:
There are a couple of steps that you will most likely need to do a couple of things. Here is an annotated example:
There are a couple of steps that you will most likely need to do a couple of things. Here is an annotated example:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ stdenv, lib, fetchFromGitHub, kernel, kmod }:
{ stdenv, lib, fetchFromGitHub, kernel, kernelModuleMakeFlags, kmod }:


stdenv.mkDerivation rec {
stdenv.mkDerivation rec {
Line 294: Line 299:
   nativeBuildInputs = kernel.moduleBuildDependencies;                      # 2
   nativeBuildInputs = kernel.moduleBuildDependencies;                      # 2


   makeFlags = [
   makeFlags = kernelModuleMakeFlags ++ [
     "KERNELRELEASE=${kernel.modDirVersion}"                                # 3
     "KERNELRELEASE=${kernel.modDirVersion}"                                # 3
     "KERNEL_DIR=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"    # 4
     "KERNEL_DIR=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"    # 4
Line 469: Line 474:
=== Overriding kernel packages ===
=== Overriding kernel packages ===


In order to override <code>linuxPackages</code>, use the <code>extend</code> attribute. Example:
In order to override <code>linuxPackages</code>, an alias of <code>linuxKernel.packages</code>, use the <code>extend</code> attribute. Example:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 493: Line 498:
     })
     })
   ];
   ];
</syntaxHighlight>
You can also use <code>kernelPackagesExtensions</code> in a similar fashion to override <code>linuxKernel.packages</code>:
<syntaxHighlight lang=nix>
nixpkgs.overlays = [ 
  (_: prev: {
    kernelPackagesExtensions = prev.kernelPackagesExtensions ++ [
      (_: linuxPrev: {
        digimend = linuxPrev.digimend.overrideAttrs (prevAttrs: {
          postPatch = prevAttrs.postPatch or "" + ''
            substituteInPlace "hid-uclogic-core.c" \
              --replace-fail "del_timer_sync" "timer_shutdown_sync"
          '';
        });
      })
    ];
  })
];
</syntaxHighlight>
</syntaxHighlight>


Line 572: Line 596:
{
{
   boot.kernel.sysctl."net.ipv4.tcp_ecn" = 1;
   boot.kernel.sysctl."net.ipv4.tcp_ecn" = 1;
}
</syntaxhighlight>
== Sysfs ==
Example of configuring kernel sysfs:
<syntaxhighlight lang="nix">
{
  boot.kernel.sysfs = {
    kernel.mm.transparent_hugepage = {
      enabled = "always";
      defrag = "defer";
      shmem_enabled = "within_size";
    };
  };
}
}
</syntaxhighlight>
</syntaxhighlight>