xmake
xmake copied to clipboard
Linux kernel cannot be built in NixOS
Xmake Version
v2.9.1+20240422
Operating System Version and Architecture
6.9.1
Describe Bug
$ git clone --depth=1 --recursive https://github.com/xmake-io/xmake
$ cd xmake/tests/projects/linux/driver/hello_custom
$ nix-env -iA nixos.linuxPackages_latest.kernel.dev
$ nix eval --impure --expr '"${(import <nixpkgs> {}).linuxPackages_latest.kernel.dev}"'
"/nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev"
$ xmake -f --linux-headers=/nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1/source
$ xmake -rv
[ 40%]: cache compiling.release src/add.c
/nix/store/9bv7dcvmfcjnmg5mnqwqlq2wxfn8d7yi-gcc-wrapper-13.2.0/bin/gcc -c -m64 -DKBUILD_MODNAME=\"hello\" -DKBUILD_BASENAME=\"add\" -o build/.objs/hello/linux/x86_64/release/src/add.c.o src/add.c
[ 60%]: cache compiling.release src/hello.c
/nix/store/9bv7dcvmfcjnmg5mnqwqlq2wxfn8d7yi-gcc-wrapper-13.2.0/bin/gcc -c -m64 -DKBUILD_MODNAME=\"hello\" -DKBUILD_BASENAME=\"hello\" -o build/.objs/hello/linux/x86_64/release/src/hello.c.o src/hello.c
error: src/hello.c:1:10: fatal error: linux/init.h: No such file or directory
1 | #include <linux/init.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I think that is because NixOS's linux headers is a little different:
That is ArchLinux:
$ ls /usr/src/linux -dl
Permissions Size User Date Modified Name
lrwxrwxrwx - root 18 May 00:56 î—¿ /usr/src/linux -> ../lib/modules/6.9.1-arch1-1/build
$ ls /usr/src/linux -l
 arch  crypto  include  kernel  localversion.20-pkgname  Module.symvers  scripts  System.map  version  vmlinux.h
 block  drivers  init  lib  Makefile  net  security  tools  virt
 certs  fs  Kconfig  localversion.10-pkgrel  mm  samples  sound  usr  vmlinux
This is NixOS:
$ nix eval --impure --expr '"${(import <nixpkgs> {}).linuxPackages_latest.kernel.dev}"'
"/nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev"
$ ls /nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1
î—¿ build î—¿ source
$ ls /nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1/build
 arch  include  kernel  Makefile  Module.symvers  scripts  source  tools
$ ls /nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1/source
 arch  certs  Documentation  include  io_uring  kernel  Makefile  net  samples  security  tools  virt
î—¿ block î—¿ crypto î—¿ fs î—¿ init î—¿ ipc î—¿ lib î—¿ mm î—¿ rust î—¿ scripts î—¿ sound î—¿ usr
$ ls /nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1/build/source -dl
Permissions Size User Date Modified Name
lrwxrwxrwx - root 1 Jan 1970 î—¿ /nix/store/cajf6mwyfm0r8dpa12chcj2hpla1yzk0-linux-6.9.1-dev/lib/modules/6.9.1/build/source -> ../source
Expected Behavior
Can work.
Project Configuration
xmake test example.
Additional Information and Error Logs
If we cannot find some files in /nix/store/XXX/lib/modules/6.9.1/build, we should search /nix/store/XXX/lib/modules/6.9.1/build/source as a fallback for NixOS.
you can try use linux-header from xmake-repo.
https://github.com/xmake-io/xmake/blob/d396bdbcd399d1bd9dd8ea94bfbe5a3bba7b3717/tests/projects/linux/driver/hello/xmake.lua#L1
Now xmake f --linux-headers=/nix/store/pmaqx4znv06123vz8q75w0mnbl3arwaq-linux-6.10.2-dev/lib/modules/6.10.2/build can work.