From c1917e56fa1ca172ddd07a598b7e66775e32fe89 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 17 May 2024 18:27:36 +0100 Subject: [PATCH] Simplify the Nix toolchain --- flake.lock | 87 ++++++++----------------- flake.nix | 162 +++++++++++++++++++++++++---------------------- nix/overlays.nix | 137 +++++++++++++++++++++------------------ nix/utils.nix | 7 +- 4 files changed, 188 insertions(+), 205 deletions(-) diff --git a/flake.lock b/flake.lock index 2aff7bc..63a8f38 100644 --- a/flake.lock +++ b/flake.lock @@ -16,56 +16,37 @@ "type": "github" } }, - "flake-utils": { + "flake-parts": { "inputs": { - "systems": "systems" + "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "lastModified": 1715865404, + "narHash": "sha256-/GJvTdTpuDjNn84j82cU6bXztE0MSkdnTWClUCRub78=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "8dc45382d5206bd292f9c2768b8058a8fd8311d9", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_2": { - "inputs": { - "systems": "systems_2" - }, - "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "hercules-ci", + "repo": "flake-parts", "type": "github" } }, "git-hooks": { "inputs": { "flake-compat": "flake-compat", - "flake-utils": "flake-utils_2", "gitignore": "gitignore", "nixpkgs": "nixpkgs", "nixpkgs-stable": "nixpkgs-stable" }, "locked": { - "lastModified": 1712055707, - "narHash": "sha256-4XLvuSIDZJGS17xEwSrNuJLL7UjDYKGJSbK1WWX2AK8=", + "lastModified": 1715870890, + "narHash": "sha256-nacSOeXtUEM77Gn0G4bTdEOeFIrkCBXiyyFZtdGwuH0=", "owner": "cachix", "repo": "git-hooks.nix", - "rev": "e35aed5fda3cc79f88ed7f1795021e559582093a", + "rev": "fa606cccd7b0ccebe2880051208e4a0f61bfc8c1", "type": "github" }, "original": { @@ -111,6 +92,18 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1714640452, + "narHash": "sha256-QBx10+k6JWz6u7VsohfSw8g8hjdBZEf8CFzXH1/1Z94=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/50eb7ecf4cd0a5756d7275c8ba36790e5bd53e33.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/50eb7ecf4cd0a5756d7275c8ba36790e5bd53e33.tar.gz" + } + }, "nixpkgs-stable": { "locked": { "lastModified": 1710695816, @@ -145,40 +138,10 @@ }, "root": { "inputs": { - "flake-utils": "flake-utils", + "flake-parts": "flake-parts", "git-hooks": "git-hooks", "nixpkgs": "nixpkgs_2" } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, - "systems_2": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index d7b00e3..fdc51d6 100644 --- a/flake.nix +++ b/flake.nix @@ -20,103 +20,113 @@ "github:lxsameer/nixpkgs/c738ee8ad1c35383037c20fa13eaac17c8ae98c5"; #inputs.nixpkgs.url = "/home/lxsameer/src/nixpkgs/"; inputs.git-hooks.url = "github:cachix/git-hooks.nix"; - inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.flake-parts.url = "github:hercules-ci/flake-parts"; - outputs = { self, nixpkgs, git-hooks, flake-utils, ... }: - flake-utils.lib.eachDefaultSystem (system: - let - version = "1.0.0"; + outputs = { self, nixpkgs, git-hooks, flake-parts, ... }@inputs: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "aarch64-darwin" "riscv64-linux" "x86_64-linux" ]; - overlays = [ (import ./nix/overlays.nix { }).muslComp ]; + flake = { }; + perSystem = { config, self', inputs', system, pkgs, lib, ... }: + let + version = "1.0.0"; - utils = import ./nix/utils.nix { inherit nixpkgs; }; + overlays = (lib.attrsets.attrValues (import ./nix/overlays.nix { })); + utils = import ./nix/utils.nix { inherit nixpkgs; }; - # Create a package set based on the build system - pkgs_set = utils.get_pkgs system overlays; - hostPkgs = pkgs_set.host; - pkgs = pkgs_set.target; + hostPkgs = pkgs; + targetPkgs = utils.getPkgs system overlays; - # Create a stdenv based on LLVM - stdenv = pkgs.stdenvAdapters.overrideCC pkgs.stdenv - pkgs.llvmPackages_18.clangUseLLVM; + # Create a stdenv based on LLVM + stdenv = targetPkgs.stdenvAdapters.overrideCC targetPkgs.stdenv + targetPkgs.llvmPackages_18.clangUseLLVM; - gc = pkgs.callPackage ./nix/boehmgc.nix { inherit stdenv; }; + gc = targetPkgs.callPackage ./nix/boehmgc.nix { inherit stdenv; }; - zlib' = pkgs.zlib-ng.overrideAttrs (old: { - cmakeFlags = [ - "-DCMAKE_INSTALL_PREFIX=/" - "-DBUILD_SHARED_LIBS=OFF" - "-DINSTALL_UTILS=ON" - "-DZLIB_COMPAT=ON" - ]; - }); + zlib' = targetPkgs.zlib-ng.overrideAttrs (old: { + cmakeFlags = [ + "-DCMAKE_INSTALL_PREFIX=/" + "-DBUILD_SHARED_LIBS=OFF" + "-DINSTALL_UTILS=ON" + "-DZLIB_COMPAT=ON" + ]; + }); - # By default llvm adds zlib to `propagatedBuildInputs` which means any - # package that uses llvm will indirectly depends on zlib. And since - # by default that zlib is built as a shared lib (since our packageset - # is not static), We can't statically link to it. So, we just replace - # that zlib with our override of zlib-ng - clang' = stdenv.cc.overrideAttrs (old: { - propagatedBuildInputs = [ stdenv.cc.bintools ] - ++ [ pkgs.zlib.static ]; - }); + # By default llvm adds zlib to `propagatedBuildInputs` which means any + # package that uses llvm will indirectly depends on zlib. And since + # by default that zlib is built as a shared lib (since our packageset + # is not static), We can't statically link to it. So, we just replace + # that zlib with our override of zlib-ng + clang' = stdenv.cc.overrideAttrs (old: { + propagatedBuildInputs = [ stdenv.cc.bintools ] + ++ [ targetPkgs.zlib.static ]; + }); - llvm = pkgs.llvmPackages_18.llvm.overrideAttrs - (old: { propagatedBuildInputs = [ pkgs.zlib.static ]; }); + llvm = targetPkgs.llvmPackages_18.llvm.overrideAttrs + (old: { propagatedBuildInputs = [ targetPkgs.zlib.static ]; }); - # This is the actual stdenv that we need to use anywhere else - stdenv' = pkgs.stdenvAdapters.overrideCC pkgs.stdenv clang'; + # This is the actual stdenv that we need to use anywhere else + stdenv' = + targetPkgs.stdenvAdapters.overrideCC targetPkgs.stdenv clang'; - nativeBuildToolsDeps = (with hostPkgs; [ cmake ninja ccache ]); + nativeBuildToolsDeps = (with hostPkgs; [ cmake ninja ccache ]); - buildToolsDeps = - (with pkgs; [ llvm llvmPackages_18.mlir llvmPackages_18.clang iwyu ]); + buildToolsDeps = (with targetPkgs; [ + llvm + llvmPackages_18.mlir + llvmPackages_18.clang + iwyu + ]); - buildDeps = (with pkgs; [ - gc - zlib' - llvm - llvmPackages_18.mlir - llvmPackages_18.clang - ]); + buildDeps = (with targetPkgs; [ + gc + zlib' + llvm + llvmPackages_18.mlir + llvmPackages_18.clang + ]); - testDeps = (with hostPkgs; [ gtest gmock gbenchmark ]); + testDeps = (with hostPkgs; [ gtest gmock gbenchmark ]); - in { - inherit pkgs; - devShells.default = (pkgs.mkShell.override { stdenv = stdenv'; }) { - inherit (self.checks.${system}.git-hook-check) shellHook; + in { + devShells.default = + (targetPkgs.mkShell.override { stdenv = stdenv'; }) { + inherit (self.checks.${system}.git-hook-check) shellHook; - nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps; - buildInputs = buildDeps ++ testDeps - ++ self.checks.${system}.git-hook-check.enabledPackages; - CPP_LS = "serene-clangd"; - }; + nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps; + buildInputs = buildDeps ++ testDeps + ++ self.checks.${system}.git-hook-check.enabledPackages; + }; - packages.devshell = stdenv'.mkDerivation { - inherit version; - name = "devshell"; + packages.blah = buildDeps ++ testDeps ++ nativeBuildToolsDeps + ++ buildToolsDeps; + packages.devshell = stdenv'.mkDerivation { + inherit version; + name = "devshell"; - doUnpack = false; - doCheck = false; - nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps; - buildInputs = buildDeps ++ testDeps; - }; - - checks = { - git-hook-check = git-hooks.lib.${system}.run { src = ./.; - hooks = { - nixfmt.enable = true; - clang-format = { - enable = true; - types_or = hostPkgs.lib.mkForce [ "c" "c++" ]; - }; - shellcheck.enable = true; - cmake-format.enable = true; + doBuild = false; + doUnpack = false; + doCheck = false; + nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps; + buildInputs = buildDeps ++ testDeps; + }; + checks = { + git-hook-check = git-hooks.lib.${system}.run { + src = ./.; + hooks = { + nixfmt.enable = true; + clang-format = { + enable = true; + types_or = hostPkgs.lib.mkForce [ "c" "c++" ]; + }; + shellcheck.enable = true; + cmake-format.enable = true; + + }; }; }; }; - }); + }; } diff --git a/nix/overlays.nix b/nix/overlays.nix index 6c6fdc8..cd4a881 100644 --- a/nix/overlays.nix +++ b/nix/overlays.nix @@ -13,76 +13,86 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -{ ... }: { +{ ... }: +with builtins; +let getEnv = p: (p.env or { }).NIX_LDFLAGS or p.NIX_LDFLAGS or ""; +in { # Most of these overlays are do to bugs and problems # in upstream nixpkgs. But thanks to their design # We can fix them using these overlays and contribute # them upstream little by little. - muslComp = (final: prev: { - p11-kit = prev.p11-kit.overrideAttrs - (old: { patches = [ ./nix/patches/p11-kit_skip_test.patch ]; }); + sereneDev = (final: prev: + if !prev.stdenv.hostPlatform.isLinux then + prev + else { + + p11-kit = prev.p11-kit.overrideAttrs + (old: { patches = [ ./nix/patches/p11-kit_skip_test.patch ]; }); + + cpio = prev.cpio.overrideAttrs (old: { + nativeBuildInputs = [ final.autoreconfHook ]; + NIX_CFLAGS_COMPILE = "-Wno-implicit-function-declaration"; + }); + + libedit = prev.libedit.overrideAttrs (old: { + # Musl is ISO 10646 compliant but doesn't define __STDC_ISO_10646__ we need to do it ourselves + NIX_CFLAGS_COMPILE = "-D__STDC_ISO_10646__=201103L"; + }); + + elfutils = prev.elfutils.overrideAttrs (old: { + # libcxx does not have __cxa_demangle + configureFlags = old.configureFlags ++ [ "--disable-demangler" ]; + }); + + ccache = prev.ccache.overrideAttrs (old: { + nativeBuildInputs = old.nativeBuildInputs ++ [ final.elfutils ]; + }); + + # We don't need systemd at all + util-linux = prev.util-linux.override { systemdSupport = false; }; + + # libpam exmaples use glibc. We need to disable them + linux-pam = prev.linux-pam.overrideAttrs (old: { + postConfigure = '' + sed 's/examples//' -i Makefile + ''; + }); + + #============================================================= + # Since we're using lld-18, and --no-undefined-version is the + # default in lld-18. We need to explicitly turn it off for + # these problematic packages untill they fix it upstream. + libxcrypt = prev.libxcrypt.overrideAttrs (old: { + env.NIX_LDFLAGS = "${getEnv old} --undefined-version"; + + #old.NIX_FLAGS ++ final.lib.optional (prev.stdenv.cc.isClang) + }); + + ncurses = prev.ncurses.overrideAttrs + (old: { env.NIX_LDFLAGS = "${getEnv old} --undefined-version"; }); + + libbsd = prev.libbsd.overrideAttrs (old: { + env.NIX_LDFLAGS = "${getEnv old} --undefined-version"; + # NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) + # [ "--undefined-version" ]; + }); + + libxml2 = prev.libxml2.overrideAttrs (old: { + env.NIX_LDFLAGS = "${getEnv old} --undefined-version"; + propagatedBuildInputs = old.propagatedBuildInputs + ++ [ final.zlib.static ]; + # NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) + # [ "--undefined-version" ]; + }); + + # binutils = prev.binutils.overrideAttrs (old: { + # env.NIX_LDFLAGS = (getEnv old NIX_LDFLAGS " ") ++ "--undefined-version"; + # buildInputs = [ final.zlib final.gettext final.zlib.static ]; + # }); - cpio = prev.cpio.overrideAttrs (old: { - nativeBuildInputs = [ prev.autoreconfHook ]; - NIX_CFLAGS_COMPILE = "-Wno-implicit-function-declaration"; }); - libedit = prev.libedit.overrideAttrs (old: { - # Musl is ISO 10646 compliant but doesn't define __STDC_ISO_10646__ we need to do it ourselves - NIX_CFLAGS_COMPILE = "-D__STDC_ISO_10646__=201103L"; - }); - - elfutils = prev.elfutils.overrideAttrs (old: { - # libcxx does not have __cxa_demangle - configureFlags = old.configureFlags ++ [ "--disable-demangler" ]; - }); - - ccache = prev.ccache.overrideAttrs (old: { - nativeBuildInputs = old.nativeBuildInputs ++ [ final.elfutils ]; - }); - - # We don't need systemd at all - util-linux = prev.util-linux.override { systemdSupport = false; }; - - # libpam exmaples use glibc. We need to disable them - linux-pam = prev.linux-pam.overrideAttrs (old: { - postConfigure = '' - sed 's/examples//' -i Makefile - ''; - }); - - #============================================================= - # Since we're using lld-18, and --no-undefined-version is the - # default in lld-18. We need to explicitly turn it off for - # these problematic packages untill they fix it upstream. - libxcrypt = prev.libxcrypt.overrideAttrs (old: { - NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) - [ "--undefined-version" ]; - }); - - ncurses = prev.ncurses.overrideAttrs (old: { - NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) - [ "--undefined-version" ]; - }); - - libbsd = prev.libbsd.overrideAttrs (old: { - NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) - [ "--undefined-version" ]; - }); - - libxml2 = prev.libxml2.overrideAttrs (old: { - propagatedBuildInputs = old.propagatedBuildInputs ++ [ prev.zlib.static ]; - NIX_LDFLAGS = [ ] ++ final.lib.optional (prev.stdenv.cc.isClang) - [ "--undefined-version" ]; - }); - - binutils = prev.binutils.overrideAttrs (old: { - buildInputs = [ prev.zlib prev.gettext prev.zlib.static ]; - NIX_LDFLAGS = [ "--undefined-version" ]; - }); - - #============================================================== - + iwyu = (final: prev: { iwyu = (prev.include-what-you-use.overrideAttrs (old: let version = "0.22"; in { @@ -96,5 +106,6 @@ cmakeFlags = [ "-DCMAKE_PREFIX_PATH=${prev.llvmPackages_18.llvm.dev}" ]; })).override { llvmPackages = prev.__splicedPackages.llvmPackages_18; }; + }); } diff --git a/nix/utils.nix b/nix/utils.nix index 0034537..522f1d6 100644 --- a/nix/utils.nix +++ b/nix/utils.nix @@ -14,9 +14,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . { nixpkgs }: { - get_pkgs = system: overlays: { - host = import nixpkgs { inherit system; }; - target = if system == "x86_64-linux" then + getPkgs = system: overlays: + if system == "x86_64-linux" then import nixpkgs { inherit system overlays; linker = "lld"; @@ -26,5 +25,5 @@ } else import nixpkgs { inherit system overlays; }; - }; + }