Linux kernel: Difference between revisions

Rrdpad (talk | contribs)
update list available kernels
DHCP (talk | contribs)
m prefix shell comments with $ to get "comment" highlighting instead of "root shell"
 
(36 intermediate revisions by 27 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.


nix-repl> pkgs.linuxPackages
nix-repl> pkgs.linuxKernel.packages
pkgs.linuxPackages                          pkgs.linuxPackages_custom
pkgs.linuxPackages                          pkgs.linuxPackages_custom
pkgs.linuxPackages-libre                    pkgs.linuxPackages_custom_tinyconfig_kernel
pkgs.linuxPackages-libre                    pkgs.linuxPackages_custom_tinyconfig_kernel
Line 115: Line 115:
* {{ic|b}}: Reboot the system.
* {{ic|b}}: Reboot the system.


Check {{journalclt}} to see if you are triggering the shortcuts correctly, which might be different for your keyboard, as noted in the Wikipedia page.
Check <code>journalctl</code> to see if you are triggering the shortcuts correctly, which might be different for your keyboard, as noted in the Wikipedia page.


Also see {{nixos:config|services.earlyoom.enable}} and {{nixos:config|systemd.oomd.enable}}.
Also see {{nixos:option|services.earlyoom.enable}} and {{nixos:option|systemd.oomd.enable}}.


=== Custom configuration ===
=== Custom configuration ===
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 148: Line 149:
     overlays = [
     overlays = [
       (self: super: {
       (self: super: {
         linuxZenWMuQSS = pkgs.linuxPackagesFor (pkgs.linux_zen.kernel.override {
         linuxZenWMuQSS = pkgs.linuxPackagesFor (pkgs.linuxKernel.kernels.linux_zen.override {
           structuredExtraConfig = with lib.kernel; {
           structuredExtraConfig = with lib.kernel; {
             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 164: Line 167:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{
{
   boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_4_19.override {
   boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linuxKernel.kernels.linux_4_19.override {
     argsOverride = rec {
     argsOverride = rec {
       src = pkgs.fetchurl {
       src = pkgs.fetchurl {
             url = "mirror://kernel/linux/kernel/v4.x/linux-${version}.tar.xz";
             url = "mirror://kernel/linux/kernel/v${lib.versions.major version}.x/linux-${version}.tar.xz";
             sha256 = "0ibayrvrnw2lw7si78vdqnr20mm1d3z0g6a0ykndvgn5vdax5x9a";
             sha256 = "0ibayrvrnw2lw7si78vdqnr20mm1d3z0g6a0ykndvgn5vdax5x9a";
       };
       };
       version = "4.19.60";
       version = "4.19.60";
       modDirVersion = "4.19.60";
       modDirVersion = "4.19.60";
      };
    };
   });
   });
}
}
</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 186: Line 191:
To configure and cross-compile Linux kernels for embedded development, often distributed separately instead of using the stock kernel, you can setup a development environment as shown below:  
To configure and cross-compile Linux kernels for embedded development, often distributed separately instead of using the stock kernel, you can setup a development environment as shown below:  


{{file|shell.nix|nix|<nowiki>
{{file|shell.nix|nix|3=let
let
   pkgs = import <nixpkgs> { };
   pkgs = import <nixpkgs> { };
in
in
Line 194: Line 198:
   targetPkgs = pkgs: (with pkgs;
   targetPkgs = pkgs: (with pkgs;
     [
     [
       pkgconfig
       pkg-config
       ncurses
       ncurses
       qt5.qtbase
       qt5.qtbase
Line 211: Line 215:
     exec bash
     exec bash
   '';
   '';
}).env
}).env}}
</nowiki>}}


Clone the kernel sources, enter the environment using {{ic|nix-shell}}, and then do development normally.
Clone the kernel sources, enter the environment using {{ic|nix-shell}}, and then do development normally.


== make menuconfig ==
== make menuconfig ==
It is (currently) not possible to run <code>make menuconfig</code> in the checked out linux kernel sources. This is because <code>ncurses</code> is not part of your working environment when you start it with <code>nix-shell '<nixpkgs>' -A linuxPackages.kernel</code>.
It is not possible to run <code>make menuconfig</code> in the standard kernel shell. This is because <code>ncurses</code> is not part of your working environment when you start it with <code>nix-shell '<nixpkgs>' -A linuxPackages.kernel</code>.


This nix-shell hack adds ncurses as a build dependency to the kernel:
Use <code>linuxPackages.kernel.configEnv</code> instead, which adds ncurses as a build dependency to the kernel:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix-shell -E 'with import <nixpkgs> {}; linux.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkg-config ncurses ];})'
$ nix-shell '<nixpkgs>' -A linuxPackages.kernel.configEnv
[nix-shell] $ unpackPhase && cd linux-*
[nix-shell] $ unpackPhase && cd linux-*
[nix-shell] $ patchPhase
[nix-shell] $ patchPhase
Line 238: Line 241:


Please provide a comparison with other distributions' kernel:
Please provide a comparison with other distributions' kernel:
- arch: https://github.com/archlinux/svntogit-packages/blob/packages/linux/trunk/config
 
- debian: https://salsa.debian.org/kernel-team/linux/blob/master/debian/config/config and the ARCH specific ones
* Arch: https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/blob/main/config
* Debian: https://salsa.debian.org/kernel-team/linux/blob/master/debian/config/config and the ARCH specific ones


== Booting a kernel from a custom source ==
== Booting a kernel from a custom source ==
Line 263: Line 267:
           kernelPatches = [];
           kernelPatches = [];


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


           extraMeta.branch = "5.4";
           extraMeta.branch = "5.4";
Line 278: Line 282:
=== Packaging out-of-tree kernel modules ===
=== Packaging out-of-tree kernel modules ===
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 {
   name = "v4l2loopback-dc-${version}-${kernel.version}";
   pname = "v4l2loopback-dc";
   version = "1.6";
   version = "1.6";


Line 288: Line 292:
     owner = "aramg";
     owner = "aramg";
     repo = "droidcam";
     repo = "droidcam";
     rev = "v${version}";
     tag = "v${version}";
     sha256 = "1d9qpnmqa3pfwsrpjnxdz76ipk4w37bbxyrazchh4vslnfc886fx";
     hash = "1d9qpnmqa3pfwsrpjnxdz76ipk4w37bbxyrazchh4vslnfc886fx";
   };
   };


Line 296: Line 300:
   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 302: Line 306:
   ];
   ];


   meta = with lib; {
   meta = {
     description = "A kernel module to create V4L2 loopback devices";
     description = "A kernel module to create V4L2 loopback devices";
     homepage = "https://github.com/aramg/droidcam";
     homepage = "https://github.com/aramg/droidcam";
     license = licenses.gpl2;
     license = lib.licenses.gpl2;
     maintainers = [ maintainers.makefu ];
     maintainers = [ lib.maintainers.makefu ];
     platforms = platforms.linux;
     platforms = lib.platforms.linux;
   };
   };
}
}
</syntaxHighlight>
</syntaxhighlight>


1. For kernel modules it is necessary to disable <code>pic</code> in compiler hardenings as the kernel need different compiler flags.
1. For kernel modules it is necessary to disable <code>pic</code> in compiler hardenings as the kernel need different compiler flags.
Line 325: Line 329:


=== Developing out-of-tree kernel modules ===
=== Developing out-of-tree kernel modules ===
See also:  {{manual:nixos|sec=#sec-linux-config-developing-modules|chapter=12.2. Developing kernel modules}}
See also:  {{Nixpkgs Manual|name=Nixpkgs Manual, Developing kernel modules|anchor=#sec-linux-kernel-developing-modules}}


If you work on an out-of-tree kernel module the workflow could look as follow:
If you work on an out-of-tree kernel module the workflow could look as follow:
Line 391: Line 395:
and the module will be automatically loaded after a reboot. If you want instead to load it at stage 1 (before the root is even mounted), you need to add it to <code>boot.initrd.availableKernelModules</code> and <code>boot.initrd.kernelModules</code>.
and the module will be automatically loaded after a reboot. If you want instead to load it at stage 1 (before the root is even mounted), you need to add it to <code>boot.initrd.availableKernelModules</code> and <code>boot.initrd.kernelModules</code>.


Note that if you don't reboot, you can still load manually the module using <code>modprobe yourmodulename></code>, and to automatically enable a module during configuration switch/reboot, you can put <code>modprobe yourmodulename || true</code> inside the script of a systemctl service (it is for example what does wireguard).
Note that if you don't reboot, you can still load manually the module using <code>modprobe <yourmodulename></code>, and to automatically enable a module during configuration switch/reboot, you can put <code>modprobe yourmodulename || true</code> inside the script of a systemctl service (it is for example what does wireguard).


Finally, if you want to define some options by default (used when you load manually a module using <code>modprobe</code>, or when the system boots), you can specify them in:
Finally, if you want to define some options by default (used when you load manually a module using <code>modprobe</code>, or when the system boots), you can specify them in:
Line 425: Line 429:
Save the following as <code>shell.nix</code> for your nix-shell:
Save the following as <code>shell.nix</code> for your nix-shell:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> {} }:
let
let
Line 434: Line 438:
   # the override is optional if you need for example more build dependencies
   # the override is optional if you need for example more build dependencies
   nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.gllvm ];
   nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.gllvm ];
}
})
</syntaxHighlight>
</syntaxhighlight>


Then you can run the following commands (in the nix-shell <code>$makeFlags</code> will contain
Then you can run the following commands (in the nix-shell <code>$makeFlags</code> will contain
Line 442: Line 446:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix-shell
$ nix-shell
# to configure
$ # to configure
$ nix-shell> make $makeFlags defconfig -j $(nproc)
$ nix-shell> make $makeFlags defconfig -j $(nproc)
# to build
$ # to build
$ nix-shell> make $makeFlags -j $(nproc)
$ nix-shell> make $makeFlags -j $(nproc)
</syntaxHighlight>
</syntaxHighlight>
Line 463: Line 467:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix-shell
$ nix-shell
# to configure
$ # to configure
$ nix-shell> make $makeFlags defconfig -j $(nproc)
$ nix-shell> make $makeFlags defconfig -j $(nproc)
# to build
$ # to build
$ nix-shell> make $makeFlags -j $(nproc)
$ nix-shell> make $makeFlags -j $(nproc)
</syntaxHighlight>
</syntaxHighlight>
Line 471: Line 475:
=== 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 495: Line 499:
     })
     })
   ];
   ];
</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 551: Line 574:
After packaging the kernel module you can add it to your system just like an out-of-tree kernel module, it will replace the default module provided by the linux package because it has the same name:
After packaging the kernel module you can add it to your system just like an out-of-tree kernel module, it will replace the default module provided by the linux package because it has the same name:


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{pkgs, config, ...}:
{pkgs, config, ...}:
let
let
Line 563: Line 586:
     (amdgpu-kernel-module.overrideAttrs (_: {
     (amdgpu-kernel-module.overrideAttrs (_: {
       patches = [ ./patches/amdgpu-foo-bar.patch ];
       patches = [ ./patches/amdgpu-foo-bar.patch ];
     })
     }))
   ];
   ];
}
}
</syntaxHighlight>
</syntaxhighlight>
 
== Sysctls ==
 
Example of configuring kernel sysctls:
<syntaxhighlight lang="nix">
{
  boot.kernel.sysctl."net.ipv4.tcp_ecn" = 1;
}
</syntaxhighlight>
 
== Sysfs ==


== Troubleshooting ==
Example of configuring kernel sysfs:
=== Build fails ===
<syntaxhighlight lang="nix">
==== Too high ram usage ====
{
turn off `DEBUG_INFO_BTF`
  boot.kernel.sysfs = {
    kernel.mm.transparent_hugepage = {
      enabled = "always";
      defrag = "defer";
      shmem_enabled = "within_size";
    };
  };
}
</syntaxhighlight>


== See also ==
== See also ==