From 87ad50e5e5298d692b14e2c8e9c086f1381ab0a9 Mon Sep 17 00:00:00 2001 From: Mikael Voss Date: Sun, 13 Oct 2024 11:24:31 +0200 Subject: [PATCH] Build with LLVM and musl --- flake.nix | 27 ++++-- home/config/nil/home.nix | 3 +- home/config/nil/music.nix | 2 +- lib/platforms.nix | 12 +++ lib/stdenv.nix | 56 ++++++++++++ nixos/module/default.nix | 2 +- nixos/module/musl.nix | 24 ++++++ overlay/fixes.nix | 176 ++++++++++++++++++++++++++++++++++++-- overlay/mimalloc.nix | 52 ++--------- 9 files changed, 291 insertions(+), 63 deletions(-) create mode 100644 lib/stdenv.nix create mode 100644 nixos/module/musl.nix diff --git a/flake.nix b/flake.nix index 87a3097..905d6d0 100644 --- a/flake.nix +++ b/flake.nix @@ -99,13 +99,23 @@ }; overlays = load ./overlay "overlay"; - legacyPackages = eachSystem (system: platform: - import nixpkgs { - localSystem = builtins.currentSystem or platform; + legacyPackages = eachSystem (system: platform: let + pkgs = import nixpkgs { + localSystem = builtins.currentSystem or system; crossSystem = platform; overlays = [ self.overlays.default ]; - config.allowUnsupportedSystem = true; - }); + config = { + allowBroken = true; + allowUnsupportedSystem = true; + }; + }; + in pkgs // { + config = pkgs.config // { + replaceStdenv = { pkgs }: self.lib.stdenv pkgs pkgs.stdenv; + /*replaceCrossStdenv = { buildPackages, baseStdenv }: + self.lib.stdenv self.legacyPackages.${system} baseStdenv;*/ + }; + }); packages = eachSystem (system: platform: let pkgs = self.legacyPackages.${system}; @@ -126,6 +136,13 @@ targetHost = config.networking.fqdnOrHostName; targetUser = null; }; + + nixpkgs = let + platform = self.lib.platforms.x86_64-linux; + in { + buildPlatform = builtins.currentSystem or platform.system; + hostPlatform = platform; + }; }; }; diff --git a/home/config/nil/home.nix b/home/config/nil/home.nix index 4f2b66b..4581274 100644 --- a/home/config/nil/home.nix +++ b/home/config/nil/home.nix @@ -11,7 +11,6 @@ in { imports = [ nur.hmModules.nur self.homeModules.greedy - self.homeModules.locale-en_EU catppuccin.homeManagerModules.catppuccin niri.homeModules.config ] ++ self.lib.mods [ @@ -205,7 +204,7 @@ in { enable = true; envFile.text = let ls-colours = pkgs.runCommand "ls-colours" { } '' - ${lib.getExe pkgs.vivid} generate catppuccin-mocha >$out + ${lib.getExe pkgs.buildPackages.vivid} generate catppuccin-mocha >$out '' |> builtins.readFile; in '' load-env { diff --git a/home/config/nil/music.nix b/home/config/nil/music.nix index 1a46d8f..0af65a3 100644 --- a/home/config/nil/music.nix +++ b/home/config/nil/music.nix @@ -33,7 +33,7 @@ in lib.mkIf (osConfig.hardware.graphics.enable or false) { }; services.mopidy = { - enable = true; +# enable = true; extensionPackages = with pkgs; [ mopidy-iris mopidy-local diff --git a/lib/platforms.nix b/lib/platforms.nix index 52308d9..1f4fcf0 100644 --- a/lib/platforms.nix +++ b/lib/platforms.nix @@ -4,13 +4,25 @@ let inherit (nixpkgs) lib; in lib.mapAttrs (system: platform: lib.systems.elaborate platform) { "x86_64-linux" = { system = "x86_64-linux"; + config = "x86_64-unknown-linux-musl"; + useLLVM = true; + linker = "lld"; + gcc.arch = "x86-64-v3"; }; "aarch64-linux" = { system = "aarch64-linux"; + config = "aarch64-unknown-linux-musl"; + useLLVM = true; + linker = "lld"; + gcc.arch = "armv8-a"; }; "riscv64-linux" = { system = "riscv64-linux"; + config = "riscv64-unknown-linux-musl"; + useLLVM = true; + linker = "lld"; + gcc.arch = "rv64imacfd"; }; } diff --git a/lib/stdenv.nix b/lib/stdenv.nix new file mode 100644 index 0000000..3b0a786 --- /dev/null +++ b/lib/stdenv.nix @@ -0,0 +1,56 @@ +{ ... }: pkgs: stdenv: + +let + inherit (lib) optionalAttrs optionals toList; + inherit (pkgs) lib; + inherit (stdenv) targetPlatform; +in pkgs.addAttrsToDerivation (prevAttrs: let + overrideAlloc = prevAttrs.overrideAlloc or true; + + inputs = optionals overrideAlloc [ pkgs.mimalloc ]; + + cflagsE = [ "-flto" ]; + cflagsC = [ "-pipe" ]; + cflagsL = [ /*"-fuse-ld=lld"*/ ]; + + ldflags = [ + "-O2" + "--hash-style=gnu" + /*"--icf=safe" + "--lto-O2"*/ + ] ++ optionals overrideAlloc [ "-lmimalloc" ]; + + rustflags = [ + "-C" "opt-level=2" + "-C" "linker-flavor=ld.lld" + "-C" "lto" + "-C" "linker-plugin-lto" + ] ++ optionals (targetPlatform.isx86_64 && targetPlatform ? gcc.arch) [ + "-C" "target-cpu=${targetPlatform.gcc.arch}" + ] ++ (map (flag: [ "-C" "link-arg=${flag}" ]) ldflags |> lib.flatten); + + goflags = [ "-ldflags=-linkmode=external" ]; +in { + buildInputs = prevAttrs.buildInputs or [ ] ++ inputs; + + env = prevAttrs.env or { } // optionalAttrs (!prevAttrs ? CFLAGS) { + CFLAGS = toList prevAttrs.CFLAGS or [ ] ++ cflagsE |> toString; + } // optionalAttrs (!prevAttrs ? NIX_CFLAGS_COMPILE) { + NIX_CFLAGS_COMPILE = toList prevAttrs.env.NIX_CFLAGS_COMPILE or [ ] ++ cflagsC |> toString; + } // optionalAttrs (!prevAttrs ? NIX_CFLAGS_LINK) { + NIX_CFLAGS_LINK = toList prevAttrs.env.NIX_CFLAGS_LINK or [ ] ++ cflagsL |> toString; + } // optionalAttrs (prevAttrs ? env.NIX_LDFLAGS) { + NIX_LDFLAGS = toList prevAttrs.NIX_LDFLAGS or [ ] ++ ldflags |> toString; + }; + + NIX_RUSTFLAGS = prevAttrs.NIX_RUSTFLAGS or [ ] ++ rustflags; + GOFLAGS = prevAttrs.GOFLAGS or [ ] ++ goflags; +} // optionalAttrs (prevAttrs ? CFLAGS) { + CFLAGS = toList prevAttrs.CFLAGS or [ ] ++ cflagsE; +} // optionalAttrs (prevAttrs ? NIX_CFLAGS_COMPILE) { + NIX_CFLAGS_COMPILE = toList prevAttrs.NIX_CFLAGS_COMPILE or [ ] ++ cflagsC; +} // optionalAttrs (prevAttrs ? NIX_CFLAGS_LINK) { + NIX_CFLAGS_LINK = toList prevAttrs.NIX_CFLAGS_LINK or [ ] ++ cflagsL; +} // optionalAttrs (!prevAttrs ? env.NIX_LDFLAGS) { + NIX_LDFLAGS = toList prevAttrs.NIX_LDFLAGS or [ ] ++ ldflags; +}) stdenv diff --git a/nixos/module/default.nix b/nixos/module/default.nix index 27c4c2a..5d44f61 100644 --- a/nixos/module/default.nix +++ b/nixos/module/default.nix @@ -10,7 +10,7 @@ ephemeral iosched kernel - locale-en_EU + musl network nix openssh diff --git a/nixos/module/musl.nix b/nixos/module/musl.nix new file mode 100644 index 0000000..cb1f82c --- /dev/null +++ b/nixos/module/musl.nix @@ -0,0 +1,24 @@ +{ ...}: { modulesPath, config, lib, pkgs, ... }: { + disabledModules = [ + (modulesPath + "/config/ldso.nix") + (modulesPath + "/config/stub-ld.nix") + (modulesPath + "/programs/nix-ld.nix") + ]; + + config = lib.mkIf pkgs.hostPlatform.isMusl { + i18n.glibcLocales = pkgs.stdenvNoCC.mkDerivation { + pname = "locale-archive-stub"; + version = "0"; + + buildCommand = '' + mkdir -p "$out/lib/locale" + touch "$out/lib/locale/locale-archive" + ''; + } |> lib.mkDefault; + + i18n.supportedLocales = lib.mkDefault [ ]; + security.pam.services.login.updateWtmp = lib.mkForce false; + services.nscd.enable = lib.mkForce false; + system.nssModules = lib.mkForce [ ]; + }; +} diff --git a/overlay/fixes.nix b/overlay/fixes.nix index cbf7c5b..5aa989a 100644 --- a/overlay/fixes.nix +++ b/overlay/fixes.nix @@ -1,9 +1,112 @@ -{ nixpkgs, ... }: final: prev: +{ self, nixpkgs, ... }: final: prev: let inherit (nixpkgs) lib; inherit (lib) toList; - inherit (prev.stdenv) hostPlatform; + inherit (prev.stdenv) buildPlatform hostPlatform; + inherit (self.lib) removePackages; + + prev' = prev; + final' = final; in { + gst_all_1 = prev.gst_all_1 // { + gst-plugins-bad = prev.gst_all_1.gst-plugins-bad.overrideAttrs (prevAttrs: { + buildInputs = prevAttrs.buildInputs or [ ] |> removePackages [ "libajantv2" ]; + }); + }; + + perlPackages = prev.perlPackages.overrideScope (final: prev: { + DBI = prev.DBI.overrideAttrs (prevAttrs: { + nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ + final'.stdenv.cc + final'.stdenv.cc.binutils + ]; + + makeMakerFlags = prevAttrs.makeMakerFlags or [ ] ++ [ + "CC=${final'.stdenv.cc.targetPrefix}cc" + "LD=${final'.stdenv.cc.targetPrefix}cc" + "CCFLAGS=-Doff64_t=off_t" + ]; + }); + }); + + bind = prev.bind.overrideAttrs (prevAttrs: { + buildInputs = prevAttrs.buildInputs or [ ] |> removePackages [ "jemalloc" ]; + }); + + diffutils = prev.diffutils.overrideAttrs (prevAttrs: { + configureFlags = prevAttrs.configureFlags or [ ] ++ [ "--disable-nls" ]; + + postPatch = '' + sed -E -i 's/test-getopt-(gnu|posix)//g' gnulib-tests/Makefile.in + ''; + }); + + gnu-efi = prev.gnu-efi.overrideAttrs (prevAttrs: { + nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ final.buildPackages.binutils ]; + makeFlags = prevAttrs.makeFlags or [ ] ++ [ "CC=${final.stdenv.cc.targetPrefix}cc" ]; + }); + + gsm = prev.gsm.overrideAttrs (prevAttrs: { + makeFlags = prevAttrs.makeFlags or [ ] ++ [ "CC=${final.stdenv.cc.targetPrefix}cc" ]; + }); + + keyutils = prev.keyutils.overrideAttrs (prevAttrs: { + NIX_LDFLAGS = toList prevAttrs.NIX_LDFLAGS or [ ] ++ [ "--undefined-version" ]; + }); + + kexec-tools = prev.kexec-tools.override { stdenv = final.gccStdenv; }; + + level-zero = prev.level-zero.overrideAttrs (prevAttrs: { + cmakeFlags = prevAttrs.cmakeFlags or [ ] ++ [ "-DCMAKE_CXX_FLAGS=-Wno-error=deprecated" ]; + }); + + libcdio = prev.libcdio.overrideAttrs (prevAttrs: { + nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ final.buildPackages.binutils ]; + }); + + libjpeg = prev.libjpeg.overrideAttrs (prevAttrs: { + cmakeFlags = prevAttrs.cmakeFlags or [ ] ++ [ "-DFLOATTEST12=fp-contract" ]; + }); + + liboping = prev.liboping.overrideAttrs (prevAttrs: { + configureFlags = prevAttrs.configureFlags or [ ] ++ [ + "ac_cv_func_malloc_0_nonnull=yes" + "ac_cv_func_realloc_0_nonnull=yes" + ]; + }); + + libselinux = prev.libselinux.override { enablePython = false; }; + + liburing = prev.liburing.overrideAttrs (finalAttrs: prevAttrs: { + version = "2.8"; + + src = final.fetchFromGitHub { + owner = "axboe"; + repo = "liburing"; + rev = "refs/tags/liburing-${finalAttrs.version}"; + hash = "sha256-10zmoMDzO41oNRVXE/6FzDGPVRVJTJTARVUmc1b7f+o="; + }; + }); + + llvmPackages = prev.llvmPackages // { + libcxx = prev.llvmPackages.libcxx.override { + devExtraCmakeFlags = [ "-DLIBCXX_HAS_MUSL_LIBC=1" ]; + }; + }; + + lua = prev.lua.overrideAttrs (prevAttrs: { + env = prevAttrs.env or { } // { LD = "${final.stdenv.cc.targetPrefix}ld"; }; + makeFlags = prevAttrs.makeFlags or [ ] ++ [ "LD=${final.stdenv.cc.targetPrefix}ld" ]; + }); + + netbsd = prev.netbsd.overrideScope (final: prev: { + compatIfNeeded = [ final.compat ]; + + compat = prev.compat.overrideAttrs (prevAttrs: { + makeFlags = prevAttrs.makeFlags ++ [ "OBJCOPY=:" ]; + }); + }); + numactl = prev.numactl.overrideAttrs (prevAttrs: { patches = prevAttrs.patches or [ ] ++ [ (final.fetchpatch { @@ -13,19 +116,60 @@ in { ]; }); - redis = prev.redis.overrideAttrs { - doCheck = false; - }; + python3 = (prev.python3.overrideAttrs (prevAttrs: { + env = prevAttrs.env or { } // { + LD = "${final.stdenv.cc.bintools}/bin/${final.stdenv.cc.targetPrefix}ld"; + }; - python312 = prev.python312.override { + configureFlags = prevAttrs.configureFlags or [ ] ++ [ + "LD=${final.stdenv.cc.bintools}/bin/${final.stdenv.cc.targetPrefix}ld" + ]; + + postFixup = prevAttrs.postFixup + '' + cat <>"$out/nix-support/setup-hook" + setuptoolsDistutilsHook() { + export SETUPTOOLS_USE_DISTUTILS="stdlib" + } + + addEnvHooks "\$hostOffset" setuptoolsDistutilsHook + EOF + ''; + })).override { packageOverrides = final: prev: { - pywebview = prev.pywebview.overrideAttrs ({ + defusedxml = prev.defusedxml.overrideAttrs { doCheck = false; doInstallCheck = false; + }; + + html5-parser = prev.html5-parser.overrideAttrs (prevAttrs: { + env = prevAttrs.env or { } // { + LD = "${final.stdenv.cc.targetPrefix}cc"; + }; }); + + netifaces = prev.netifaces.overrideAttrs (prevAttrs: { + env = prevAttrs.env or { } // { + NIX_CFLAGS_COMPILE = toList prevAttrs.env.NIX_CFLAGS_COMPILE or [ ] ++ [ + "-Wno-error=int-conversion" + ] |> toString; + }; + }); + + pycparser = prev.pycparser.overrideAttrs { + doCheck = false; + doInstallCheck = false; + }; }; }; + python3Packages = final.python3.pkgs; + + redis = prev.redis.overrideAttrs { doCheck = false; }; + + sane-backends = prev.sane-backends.overrideAttrs (prevAttrs: { + buildInputs = prevAttrs.buildInputs or [ ] |> removePackages [ "net-snmp" ]; + }); + sioyek = prev.sioyek.overrideAttrs (prevAttrs: { env = prevAttrs.env or { } // { NIX_CFLAGS_COMPILE = toList prevAttrs.env.NIX_CFLAGS_COMPILE or [ ] @@ -33,10 +177,28 @@ in { }; }); + soundtouch = prev.soundtouch.overrideAttrs (prevAttrs: { + nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ final.binutils ]; + }); + + time = prev.time.overrideAttrs (prevAttrs: { + env = prevAttrs.env or { } // { + CFLAGS = toList prevAttrs.env.CFLAGS or [ ] ++ [ + "-Wno-error=implicit-function-declaration" + ] |> toString; + }; + }); + + usrsctp = prev.usrsctp.overrideAttrs (prevAttrs: { + cmakeFlags = prevAttrs.cmakeFlags or [ ] ++ [ "-Dsctp_werror=0" ]; + }); + zeromq = prev.zeromq.overrideAttrs (prevAttrs: { postPatch = prevAttrs.postPatch or "" + '' substituteInPlace CMakeLists.txt \ --replace-fail 'CACHELINE_SIZE EQUAL "undefined"' 'CACHELINE_SIZE STREQUAL "undefined"' ''; }); + + zlib = prev.zlib.override { splitStaticOutput = false; }; } diff --git a/overlay/mimalloc.nix b/overlay/mimalloc.nix index 3c16203..86c2269 100644 --- a/overlay/mimalloc.nix +++ b/overlay/mimalloc.nix @@ -4,6 +4,7 @@ let inherit (nixpkgs) lib; in { mimalloc = (prev.mimalloc.overrideAttrs (prevAttrs: { + overrideAlloc = false; postPatch = prevAttrs.postPatch or "" + '' sed -E -i \ -e 's/(\{ )1(, UNINIT, MI_OPTION_LEGACY\(purge_decommits,reset_decommits\) \})/\10\2/' \ @@ -33,10 +34,6 @@ in { fractal = prev.fractal.overrideAttrs (prevAttrs: { nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ final.makeBinaryWrapper ]; - buildInputs = prevAttrs.buildInputs or [ ] ++ [ final.mimalloc ]; - - NIX_RUSTFLAGS = lib.toList prevAttrs.NIX_RUSTFLAGS or [ ] ++ [ "-C" "link-arg=-lmimalloc" ]; - postInstall = prevAttrs.postInstall or "" + '' wrapProgram "$out/bin/fractal" \ --set MIMALLOC_RESERVE_HUGE_OS_PAGES 1 @@ -47,47 +44,8 @@ in { mpv = final.mpv-unwrapped; extraMakeWrapperArgs = [ "--set" "MIMALLOC_RESERVE_HUGE_OS_PAGES" "1" ]; }; -} // lib.genAttrs [ - "bat" - "bottom" - "cryptsetup" - "dbus-broker" - "erlang" - "fd" - "firefox-unwrapped" - "fuzzel" - "helix" - "kitty" - "mako" - "mpv-unwrapped" - "niri" - "nix" - "nushell" - "openssh" - "pipewire" - "pueue" - "ripgrep" - "sd" - "sioyek" - "sudo-rs" - "systemd" - "swayidle" - "swaylock" - "swaylock-effects" - "thunderbird-unwrapped" - "uutils-coreutils" - "uutils-coreutils-noprefix" - "waybar" - "wirepluber" - "xdg-desktop-portal-gnome" - "xdg-desktop-portal-gtk" -] (pkg: prev.${pkg}.overrideAttrs (prevAttrs: { - buildInputs = prevAttrs.buildInputs or [ ] ++ [ final.mimalloc ]; - env = prevAttrs.env or { } // lib.optionalAttrs (prevAttrs ? env.NIX_LDFLAGS) { - NIX_LDFLAGS = toString (lib.toList prevAttrs.env.NIX_LDFLAGS or [ ] ++ [ "-lmimalloc" ]); - }; - NIX_RUSTFLAGS = lib.toList prevAttrs.NIX_RUSTFLAGS or [ ] ++ [ "-C" "link-arg=-lmimalloc" ]; -} // lib.optionalAttrs (!prevAttrs ? env.NIX_LDFLAGS) { - NIX_LDFLAGS = lib.toList prevAttrs.NIX_LDFLAGS or [ ] ++ [ "-lmimalloc" ]; -})) + perl = prev.perl.overrideAttrs { + overrideAlloc = false; + }; +}