summaryrefslogtreecommitdiff
path: root/modules/llvm_kernel.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/llvm_kernel.nix')
-rw-r--r--modules/llvm_kernel.nix59
1 files changed, 59 insertions, 0 deletions
diff --git a/modules/llvm_kernel.nix b/modules/llvm_kernel.nix
new file mode 100644
index 00000000..71d96320
--- /dev/null
+++ b/modules/llvm_kernel.nix
@@ -0,0 +1,59 @@
1{ config, pkgs, lib, ... }:
2let
3 cfg = config.boot.kernel.llvm;
4in {
5 options = {
6 boot.kernel.llvm = {
7 enable = lib.mkEnableOption "building linux kernel with clang/LLVM" // {
8 default = true;
9 };
10
11 kernel = lib.mkPackageOption pkgs [ "linuxKernel" "kernels" "linux_default" ] {};
12 };
13 };
14
15 config = lib.mkIf cfg.enable {
16 boot.kernelPackages =
17 with pkgs; # Cut down on repeated `pkgs.` declarations
18 let
19 # Choose whichever LLVM version you please. `llvmPackages` is the
20 # default version, `llvmPackages_latest` is the latest (at the time of
21 # writing, LLVM 17.0.6 and 18.1.8 respectively).
22 llvm = llvmPackages_latest;
23 # Same deal as LLVM; choose whichever kernel version you like.
24 # `linux` is the latest LTS, `linux_latest` is the latest stable.
25 inherit (cfg) kernel;
26 in
27 # Generate kernel modules for our custom kernel.
28 linuxPackagesFor (
29 # Override our chosen kernel version with our custom settings.
30 (kernel.override {
31 # Set our chosen version of LLVM as our standard environment.
32 stdenv = overrideCC llvm.stdenv (
33 # Tell our C compiler (Clang) to use LLVM bintools--normally GNU
34 # binutils are used even with Clang as the compiler.
35 llvm.stdenv.cc.override {
36 bintools = llvm.bintools;
37 }
38 );
39
40 # Tell Linux that we're compiling with Clang and LLVM.
41 extraMakeFlags = [ "LLVM=1" ];
42
43 # If you'd like to edit your kernel configuration, use
44 # `structuredExtraConfig`. For example, some options available to us
45 # when compiling with Clang and linking with LLD:
46 structuredExtraConfig = {
47 CFI_CLANG = lib.kernel.yes;
48 # LTO_CLANG_THIN = lib.kernel.yes;
49 };
50 }
51 ).overrideAttrs
52 # Work around another NixOS specific issue where builds with WERROR=y
53 # are stopped by a benign error. See reference 1 below for details.
54 # Technically, this fix is only necessary with WERROR=y but the issue
55 # still causes a warning on builds where WERROR is unset.
56 { env.NIX_CFLAGS_COMPILE = "-Wno-unused-command-line-argument"; }
57 );
58 };
59}