Simplify the Nix toolchain

This commit is contained in:
Sameer Rahmani 2024-05-17 18:27:36 +01:00
parent 4b38dbb145
commit c1917e56fa
Signed by: lxsameer
GPG Key ID: 8741FACBF412FFA5
4 changed files with 188 additions and 205 deletions

View File

@ -16,56 +16,37 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils": { "flake-parts": {
"inputs": { "inputs": {
"systems": "systems" "nixpkgs-lib": "nixpkgs-lib"
}, },
"locked": { "locked": {
"lastModified": 1710146030, "lastModified": 1715865404,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "narHash": "sha256-/GJvTdTpuDjNn84j82cU6bXztE0MSkdnTWClUCRub78=",
"owner": "numtide", "owner": "hercules-ci",
"repo": "flake-utils", "repo": "flake-parts",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "rev": "8dc45382d5206bd292f9c2768b8058a8fd8311d9",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "numtide", "owner": "hercules-ci",
"repo": "flake-utils", "repo": "flake-parts",
"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",
"type": "github" "type": "github"
} }
}, },
"git-hooks": { "git-hooks": {
"inputs": { "inputs": {
"flake-compat": "flake-compat", "flake-compat": "flake-compat",
"flake-utils": "flake-utils_2",
"gitignore": "gitignore", "gitignore": "gitignore",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"nixpkgs-stable": "nixpkgs-stable" "nixpkgs-stable": "nixpkgs-stable"
}, },
"locked": { "locked": {
"lastModified": 1712055707, "lastModified": 1715870890,
"narHash": "sha256-4XLvuSIDZJGS17xEwSrNuJLL7UjDYKGJSbK1WWX2AK8=", "narHash": "sha256-nacSOeXtUEM77Gn0G4bTdEOeFIrkCBXiyyFZtdGwuH0=",
"owner": "cachix", "owner": "cachix",
"repo": "git-hooks.nix", "repo": "git-hooks.nix",
"rev": "e35aed5fda3cc79f88ed7f1795021e559582093a", "rev": "fa606cccd7b0ccebe2880051208e4a0f61bfc8c1",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -111,6 +92,18 @@
"type": "github" "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": { "nixpkgs-stable": {
"locked": { "locked": {
"lastModified": 1710695816, "lastModified": 1710695816,
@ -145,40 +138,10 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-parts": "flake-parts",
"git-hooks": "git-hooks", "git-hooks": "git-hooks",
"nixpkgs": "nixpkgs_2" "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", "root": "root",

162
flake.nix
View File

@ -20,103 +20,113 @@
"github:lxsameer/nixpkgs/c738ee8ad1c35383037c20fa13eaac17c8ae98c5"; "github:lxsameer/nixpkgs/c738ee8ad1c35383037c20fa13eaac17c8ae98c5";
#inputs.nixpkgs.url = "/home/lxsameer/src/nixpkgs/"; #inputs.nixpkgs.url = "/home/lxsameer/src/nixpkgs/";
inputs.git-hooks.url = "github:cachix/git-hooks.nix"; 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, ... }: outputs = { self, nixpkgs, git-hooks, flake-parts, ... }@inputs:
flake-utils.lib.eachDefaultSystem (system: flake-parts.lib.mkFlake { inherit inputs; } {
let systems = [ "aarch64-darwin" "riscv64-linux" "x86_64-linux" ];
version = "1.0.0";
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 hostPkgs = pkgs;
pkgs_set = utils.get_pkgs system overlays; targetPkgs = utils.getPkgs system overlays;
hostPkgs = pkgs_set.host;
pkgs = pkgs_set.target;
# Create a stdenv based on LLVM # Create a stdenv based on LLVM
stdenv = pkgs.stdenvAdapters.overrideCC pkgs.stdenv stdenv = targetPkgs.stdenvAdapters.overrideCC targetPkgs.stdenv
pkgs.llvmPackages_18.clangUseLLVM; 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: { zlib' = targetPkgs.zlib-ng.overrideAttrs (old: {
cmakeFlags = [ cmakeFlags = [
"-DCMAKE_INSTALL_PREFIX=/" "-DCMAKE_INSTALL_PREFIX=/"
"-DBUILD_SHARED_LIBS=OFF" "-DBUILD_SHARED_LIBS=OFF"
"-DINSTALL_UTILS=ON" "-DINSTALL_UTILS=ON"
"-DZLIB_COMPAT=ON" "-DZLIB_COMPAT=ON"
]; ];
}); });
# By default llvm adds zlib to `propagatedBuildInputs` which means any # By default llvm adds zlib to `propagatedBuildInputs` which means any
# package that uses llvm will indirectly depends on zlib. And since # package that uses llvm will indirectly depends on zlib. And since
# by default that zlib is built as a shared lib (since our packageset # 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 # is not static), We can't statically link to it. So, we just replace
# that zlib with our override of zlib-ng # that zlib with our override of zlib-ng
clang' = stdenv.cc.overrideAttrs (old: { clang' = stdenv.cc.overrideAttrs (old: {
propagatedBuildInputs = [ stdenv.cc.bintools ] propagatedBuildInputs = [ stdenv.cc.bintools ]
++ [ pkgs.zlib.static ]; ++ [ targetPkgs.zlib.static ];
}); });
llvm = pkgs.llvmPackages_18.llvm.overrideAttrs llvm = targetPkgs.llvmPackages_18.llvm.overrideAttrs
(old: { propagatedBuildInputs = [ pkgs.zlib.static ]; }); (old: { propagatedBuildInputs = [ targetPkgs.zlib.static ]; });
# This is the actual stdenv that we need to use anywhere else # This is the actual stdenv that we need to use anywhere else
stdenv' = pkgs.stdenvAdapters.overrideCC pkgs.stdenv clang'; stdenv' =
targetPkgs.stdenvAdapters.overrideCC targetPkgs.stdenv clang';
nativeBuildToolsDeps = (with hostPkgs; [ cmake ninja ccache ]); nativeBuildToolsDeps = (with hostPkgs; [ cmake ninja ccache ]);
buildToolsDeps = buildToolsDeps = (with targetPkgs; [
(with pkgs; [ llvm llvmPackages_18.mlir llvmPackages_18.clang iwyu ]); llvm
llvmPackages_18.mlir
llvmPackages_18.clang
iwyu
]);
buildDeps = (with pkgs; [ buildDeps = (with targetPkgs; [
gc gc
zlib' zlib'
llvm llvm
llvmPackages_18.mlir llvmPackages_18.mlir
llvmPackages_18.clang llvmPackages_18.clang
]); ]);
testDeps = (with hostPkgs; [ gtest gmock gbenchmark ]); testDeps = (with hostPkgs; [ gtest gmock gbenchmark ]);
in { in {
inherit pkgs; devShells.default =
devShells.default = (pkgs.mkShell.override { stdenv = stdenv'; }) { (targetPkgs.mkShell.override { stdenv = stdenv'; }) {
inherit (self.checks.${system}.git-hook-check) shellHook; inherit (self.checks.${system}.git-hook-check) shellHook;
nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps; nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps;
buildInputs = buildDeps ++ testDeps buildInputs = buildDeps ++ testDeps
++ self.checks.${system}.git-hook-check.enabledPackages; ++ self.checks.${system}.git-hook-check.enabledPackages;
CPP_LS = "serene-clangd"; };
};
packages.devshell = stdenv'.mkDerivation { packages.blah = buildDeps ++ testDeps ++ nativeBuildToolsDeps
inherit version; ++ buildToolsDeps;
name = "devshell"; 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 = ./.; src = ./.;
hooks = { doBuild = false;
nixfmt.enable = true; doUnpack = false;
clang-format = { doCheck = false;
enable = true; nativeBuildInputs = nativeBuildToolsDeps ++ buildToolsDeps;
types_or = hostPkgs.lib.mkForce [ "c" "c++" ]; buildInputs = buildDeps ++ testDeps;
}; };
shellcheck.enable = true;
cmake-format.enable = true;
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;
};
}; };
}; };
}; };
}); };
} }

View File

@ -13,76 +13,86 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
{ ... }: { { ... }:
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 # Most of these overlays are do to bugs and problems
# in upstream nixpkgs. But thanks to their design # in upstream nixpkgs. But thanks to their design
# We can fix them using these overlays and contribute # We can fix them using these overlays and contribute
# them upstream little by little. # them upstream little by little.
muslComp = (final: prev: { sereneDev = (final: prev:
p11-kit = prev.p11-kit.overrideAttrs if !prev.stdenv.hostPlatform.isLinux then
(old: { patches = [ ./nix/patches/p11-kit_skip_test.patch ]; }); 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: { iwyu = (final: prev: {
# 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 = (prev.include-what-you-use.overrideAttrs (old: iwyu = (prev.include-what-you-use.overrideAttrs (old:
let version = "0.22"; let version = "0.22";
in { in {
@ -96,5 +106,6 @@
cmakeFlags = [ "-DCMAKE_PREFIX_PATH=${prev.llvmPackages_18.llvm.dev}" ]; cmakeFlags = [ "-DCMAKE_PREFIX_PATH=${prev.llvmPackages_18.llvm.dev}" ];
})).override { llvmPackages = prev.__splicedPackages.llvmPackages_18; }; })).override { llvmPackages = prev.__splicedPackages.llvmPackages_18; };
}); });
} }

View File

@ -14,9 +14,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
{ nixpkgs }: { { nixpkgs }: {
get_pkgs = system: overlays: { getPkgs = system: overlays:
host = import nixpkgs { inherit system; }; if system == "x86_64-linux" then
target = if system == "x86_64-linux" then
import nixpkgs { import nixpkgs {
inherit system overlays; inherit system overlays;
linker = "lld"; linker = "lld";
@ -26,5 +25,5 @@
} }
else else
import nixpkgs { inherit system overlays; }; import nixpkgs { inherit system overlays; };
};
} }