From 653ed6376f5b0e900e6067aade36a52e4e4137f5 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Mon, 7 Jul 2025 06:58:15 -0700 Subject: [PATCH 1/4] zig.cc: set main program --- pkgs/development/compilers/zig/cc.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkgs/development/compilers/zig/cc.nix b/pkgs/development/compilers/zig/cc.nix index bfbfb49de365..f2716d395e69 100644 --- a/pkgs/development/compilers/zig/cc.nix +++ b/pkgs/development/compilers/zig/cc.nix @@ -14,7 +14,7 @@ in runCommand "zig-cc-${zig.version}" { pname = "zig-cc"; - inherit (zig) version meta; + inherit (zig) version; nativeBuildInputs = [ makeWrapper ]; @@ -24,6 +24,10 @@ runCommand "zig-cc-${zig.version}" }; inherit zig; + + meta = zig.meta // { + mainProgram = "${targetPrefix}clang"; + }; } '' mkdir -p $out/bin From a155990361349d1e871c0e09a0a81ce754c759b6 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Mon, 28 Jul 2025 00:07:28 -0700 Subject: [PATCH 2/4] gcc-unwrapped: set main program --- .../development/compilers/gcc/common/configure-flags.nix | 5 +---- pkgs/development/compilers/gcc/common/meta.nix | 7 ++++++- pkgs/development/compilers/gcc/default.nix | 9 +++++++-- pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.cxx.nix | 1 + pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.nix | 1 + pkgs/os-specific/linux/minimal-bootstrap/gcc/8.nix | 1 + pkgs/os-specific/linux/minimal-bootstrap/gcc/latest.nix | 1 + 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkgs/development/compilers/gcc/common/configure-flags.nix b/pkgs/development/compilers/gcc/common/configure-flags.nix index 7cedb913b1a7..7afc8ca9079f 100644 --- a/pkgs/development/compilers/gcc/common/configure-flags.nix +++ b/pkgs/development/compilers/gcc/common/configure-flags.nix @@ -20,6 +20,7 @@ enablePlugin, disableGdbPlugin ? !enablePlugin, enableShared, + targetPrefix, langC, langCC, @@ -59,10 +60,6 @@ let crossDarwin = (!lib.systems.equals targetPlatform hostPlatform) && targetPlatform.libc == "libSystem"; - targetPrefix = lib.optionalString ( - !lib.systems.equals stdenv.targetPlatform stdenv.hostPlatform - ) "${stdenv.targetPlatform.config}-"; - crossConfigureFlags = # Ensure that -print-prog-name is able to find the correct programs. [ diff --git a/pkgs/development/compilers/gcc/common/meta.nix b/pkgs/development/compilers/gcc/common/meta.nix index 716b6e875f24..c69f9956ddfc 100644 --- a/pkgs/development/compilers/gcc/common/meta.nix +++ b/pkgs/development/compilers/gcc/common/meta.nix @@ -1,4 +1,8 @@ -{ lib, version }: +{ + lib, + version, + targetPrefix, +}: let inherit (lib) @@ -24,5 +28,6 @@ in platforms = platforms.unix; teams = [ teams.gcc ]; + mainProgram = "${targetPrefix}gcc"; } diff --git a/pkgs/development/compilers/gcc/default.nix b/pkgs/development/compilers/gcc/default.nix index 0e4a70f870d5..62993846d7a8 100644 --- a/pkgs/development/compilers/gcc/default.nix +++ b/pkgs/development/compilers/gcc/default.nix @@ -119,6 +119,10 @@ let !lib.systems.equals targetPlatform hostPlatform ) "${targetPlatform.config}${stageNameAddon}-"; + targetPrefix = lib.optionalString ( + !lib.systems.equals stdenv.targetPlatform stdenv.hostPlatform + ) "${stdenv.targetPlatform.config}-"; + callFile = callPackageWith { # lets inherit @@ -340,7 +344,7 @@ pipe "target" ]; - configureFlags = callFile ./common/configure-flags.nix { }; + configureFlags = callFile ./common/configure-flags.nix { inherit targetPrefix; }; inherit targetConfig; @@ -449,13 +453,14 @@ pipe inherit enableShared enableMultilib; meta = { - inherit (callFile ./common/meta.nix { }) + inherit (callFile ./common/meta.nix { inherit targetPrefix; }) homepage license description longDescription platforms teams + mainProgram ; } // optionalAttrs (!atLeast11) { diff --git a/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.cxx.nix b/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.cxx.nix index e899a0a7413e..f4bc0d4784c3 100644 --- a/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.cxx.nix +++ b/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.cxx.nix @@ -102,6 +102,7 @@ bash.runCommand "${pname}-${version}" license = licenses.gpl3Plus; teams = [ teams.minimal-bootstrap ]; platforms = platforms.unix; + mainProgram = "gcc"; }; } '' diff --git a/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.nix b/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.nix index 7b98276305b5..0acddf7e042c 100644 --- a/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.nix +++ b/pkgs/os-specific/linux/minimal-bootstrap/gcc/4.6.nix @@ -84,6 +84,7 @@ bash.runCommand "${pname}-${version}" license = licenses.gpl3Plus; teams = [ teams.minimal-bootstrap ]; platforms = platforms.unix; + mainProgram = "gcc"; }; } '' diff --git a/pkgs/os-specific/linux/minimal-bootstrap/gcc/8.nix b/pkgs/os-specific/linux/minimal-bootstrap/gcc/8.nix index f0911017d221..8f996d7dea22 100644 --- a/pkgs/os-specific/linux/minimal-bootstrap/gcc/8.nix +++ b/pkgs/os-specific/linux/minimal-bootstrap/gcc/8.nix @@ -101,6 +101,7 @@ bash.runCommand "${pname}-${version}" license = licenses.gpl3Plus; teams = [ teams.minimal-bootstrap ]; platforms = platforms.unix; + mainProgram = "gcc"; }; } '' diff --git a/pkgs/os-specific/linux/minimal-bootstrap/gcc/latest.nix b/pkgs/os-specific/linux/minimal-bootstrap/gcc/latest.nix index 25c9f25e5c87..262232d669e5 100644 --- a/pkgs/os-specific/linux/minimal-bootstrap/gcc/latest.nix +++ b/pkgs/os-specific/linux/minimal-bootstrap/gcc/latest.nix @@ -100,6 +100,7 @@ bash.runCommand "${pname}-${version}" license = licenses.gpl3Plus; teams = [ teams.minimal-bootstrap ]; platforms = platforms.unix; + mainProgram = "gcc"; }; } '' From e9b2edb4916328647571aee01b2a83c02e585df5 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Wed, 19 Mar 2025 12:26:02 +0100 Subject: [PATCH 3/4] linux: un-simplify toolchain selection This reverts commit 70cc251554d883130ae1a94ffd3508c2d521db6a. This commit caused gcc to be pulled in as a target compiler for pkgsLLVM. Co-authored-by: Tristan Ross --- .../linux/kernel/common-config.nix | 4 ++- .../os-specific/linux/kernel/common-flags.nix | 34 +++++++++++++++++++ pkgs/os-specific/linux/kernel/generic.nix | 15 ++++---- .../linux/kernel/manual-config.nix | 26 ++++++-------- 4 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 pkgs/os-specific/linux/kernel/common-flags.nix diff --git a/pkgs/os-specific/linux/kernel/common-config.nix b/pkgs/os-specific/linux/kernel/common-config.nix index 99a187200997..ca4d2837da3f 100644 --- a/pkgs/os-specific/linux/kernel/common-config.nix +++ b/pkgs/os-specific/linux/kernel/common-config.nix @@ -525,7 +525,9 @@ let DRM_AMD_DC_DCN = lib.mkIf (with stdenv.hostPlatform; isx86 || isPower64) ( whenBetween "5.11" "6.4" yes ); - DRM_AMD_DC_FP = whenAtLeast "6.4" yes; + # Not available when using clang + # See: https://github.com/torvalds/linux/blob/172a9d94339cea832d89630b89d314e41d622bd8/drivers/gpu/drm/amd/display/Kconfig#L14 + DRM_AMD_DC_FP = lib.mkIf (!stdenv.cc.isClang) (whenAtLeast "6.4" yes); DRM_AMD_DC_HDCP = whenBetween "5.5" "6.4" yes; DRM_AMD_DC_SI = whenAtLeast "5.10" yes; diff --git a/pkgs/os-specific/linux/kernel/common-flags.nix b/pkgs/os-specific/linux/kernel/common-flags.nix new file mode 100644 index 000000000000..2fb92049195d --- /dev/null +++ b/pkgs/os-specific/linux/kernel/common-flags.nix @@ -0,0 +1,34 @@ +{ + lib, + stdenv, + buildPackages, + extraMakeFlags ? [ ], +}: +# Absolute paths for compilers avoid any PATH-clobbering issues. +[ + # + # We use the unwrapped compiler, because the clang-wrapper doesn't like -target. + "CC=${lib.getExe stdenv.cc.cc}" + # The wrapper for ld.lld breaks linking the kernel. We use the unwrapped linker as workaround. See: + # https://github.com/NixOS/nixpkgs/issues/321667 + "LD=${lib.getExe' stdenv.cc.bintools.bintools "${stdenv.cc.targetPrefix}ld"}" + "AR=${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}ar"}" + "NM=${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}nm"}" + "STRIP=${lib.getExe' stdenv.cc.bintools.bintools "${stdenv.cc.targetPrefix}strip"}" + "OBJCOPY=${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}objcopy"}" + "OBJDUMP=${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}objdump"}" + "READELF=${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}readelf"}" + "HOSTCC=${lib.getExe' buildPackages.stdenv.cc "${buildPackages.stdenv.cc.targetPrefix}cc"}" + "HOSTCXX=${lib.getExe' buildPackages.stdenv.cc "${buildPackages.stdenv.cc.targetPrefix}c++"}" + "HOSTAR=${lib.getExe' buildPackages.stdenv.cc.bintools "${buildPackages.stdenv.cc.targetPrefix}ar"}" + "HOSTLD=${lib.getExe' buildPackages.stdenv.cc.bintools "${buildPackages.stdenv.cc.targetPrefix}ld"}" + "ARCH=${stdenv.hostPlatform.linuxArch}" + "CROSS_COMPILE=${stdenv.cc.targetPrefix}" +] +# Add the built in headers the kernel needs +++ lib.optionals (stdenv.cc.isClang) [ + "CFLAGS_MODULE=-I${lib.getLib stdenv.cc.cc}/lib/clang/${lib.versions.major stdenv.cc.cc.version}/include" + "CFLAGS_KERNEL=-I${lib.getLib stdenv.cc.cc}/lib/clang/${lib.versions.major stdenv.cc.cc.version}/include" +] +++ (stdenv.hostPlatform.linux-kernel.makeFlags or [ ]) +++ extraMakeFlags diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index 65583ad03f29..e98229c7e1d4 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -191,7 +191,7 @@ let ++ lib.optional (lib.versionAtLeast version "5.2") pahole ++ lib.optionals withRust [ rust-bindgen - rustc + rustc.unwrapped ]; RUST_LIB_SRC = lib.optionalString withRust rustPlatform.rustLibSrc; @@ -201,11 +201,14 @@ let kernelBaseConfig = if defconfig != null then defconfig else stdenv.hostPlatform.linux-kernel.baseConfig; - makeFlags = - lib.optionals ( - stdenv.hostPlatform.linux-kernel ? makeFlags - ) stdenv.hostPlatform.linux-kernel.makeFlags - ++ extraMakeFlags; + makeFlags = import ./common-flags.nix { + inherit + lib + stdenv + buildPackages + extraMakeFlags + ; + }; postPatch = kernel.postPatch + '' # Patch kconfig to print "###" after every question so that diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix index c0c6713fc346..e054ab30ab4e 100644 --- a/pkgs/os-specific/linux/kernel/manual-config.nix +++ b/pkgs/os-specific/linux/kernel/manual-config.nix @@ -170,7 +170,7 @@ lib.makeOverridable ( ++ optional (lib.versionAtLeast version "5.13") zstd ++ optionals withRust [ rustc - rust-bindgen + rust-bindgen.unwrapped ]; in @@ -230,7 +230,7 @@ lib.makeOverridable ( ] ++ optionals withRust [ rustc - rust-bindgen + rust-bindgen.unwrapped ]; RUST_LIB_SRC = lib.optionalString withRust rustPlatform.rustLibSrc; @@ -531,20 +531,14 @@ lib.makeOverridable ( // extraMeta; }; - # Absolute paths for compilers avoid any PATH-clobbering issues. - commonMakeFlags = [ - "ARCH=${stdenv.hostPlatform.linuxArch}" - "CROSS_COMPILE=${stdenv.cc.targetPrefix}" - ] - ++ lib.optionals (stdenv.isx86_64 && stdenv.cc.bintools.isLLVM) [ - # The wrapper for ld.lld breaks linking the kernel. We use the - # unwrapped linker as workaround. See: - # - # https://github.com/NixOS/nixpkgs/issues/321667 - "LD=${stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ld" - ] - ++ (stdenv.hostPlatform.linux-kernel.makeFlags or [ ]) - ++ extraMakeFlags; + commonMakeFlags = import ./common-flags.nix { + inherit + lib + stdenv + buildPackages + extraMakeFlags + ; + }; in stdenv.mkDerivation ( From c2420281d2c015ddbc3e14d0b5b19174f684dbd7 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Sat, 26 Apr 2025 21:27:02 -0700 Subject: [PATCH 4/4] linux: verify basic configuration sanity Co-authored-by: Tristan Ross --- pkgs/os-specific/linux/kernel/generic.nix | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix index e98229c7e1d4..11d7450a7e65 100644 --- a/pkgs/os-specific/linux/kernel/generic.nix +++ b/pkgs/os-specific/linux/kernel/generic.nix @@ -236,6 +236,24 @@ let KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \ PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \ perl -w $generateConfig + '' + + lib.optionalString stdenv.cc.isClang '' + if ! grep -Fq CONFIG_CC_IS_CLANG=y $buildRoot/.config; then + echo "Kernel config didn't recognize the clang compiler?" + exit 1 + fi + '' + + lib.optionalString stdenv.cc.bintools.isLLVM '' + if ! grep -Fq CONFIG_LD_IS_LLD=y $buildRoot/.config; then + echo "Kernel config didn't recognize the LLVM linker?" + exit 1 + fi + '' + + lib.optionalString withRust '' + if ! grep -Fq CONFIG_RUST_IS_AVAILABLE=y $buildRoot/.config; then + echo "Kernel config didn't find Rust toolchain?" + exit 1 + fi ''; installPhase = "mv $buildRoot/.config $out";