How can I use a custom libc with nixpkgs?

I want to package a custom build of bionic libc (the nixpkgs derivation is prebuilt) but I’m not sure what the best way of overriding the libc package would be outside of nixpkgs. I can only seem to find a way to set it to glibc or musl, not a custom derivation.

Thanks.

1 Like

There’s not package overlay that allows you to just plug in your libc.
If you look into the file pkgs/top-level/stage.nix you will find a definition for pkgsMusl, which builds all packages with musl instead of glibc.
What you need to do is copy that and adapt it to your library.

In any case, I doubt it will be as straightforward as that, I bet you’ll find problems with a lot of packages because practically everyone on Earth assumes libc == glibc.

I have another use case which is building GitHub - managarm/mlibc: Portable C standard library and using it with a custom stdenv. I am not looking to replace NixOS’s glibc at all, just for cross compiling via flakes. pkgsMusl does not seem to help as it uses crossSystem, I need to be able to set something like libc = stdenv.mkDerivation { name = "mlibc"; }.

I ended up making an issue for this: Using a custom libc derivation · Issue #307600 · NixOS/nixpkgs · GitHub. However I have still not solved the problem.

I tried to solve this using the following nix expression:

mkStdenvCustomLibc =
            { libc, stdenv }:
            let
              bintools = stdenv.cc.bintools.override {
                libc = libc;
              };
            in
            stdenv.override {
              cc = stdenv.cc.override {
                libc = libc;
                extraPackages = [ ];
                inherit bintools;
              };
              allowedRequisites = nixpkgs.lib.mapNullable (rs: rs ++ [ bintools ]) (
                stdenv.allowedRequisites or null
              );
            };
 

However using it on libcxx doesn’t work - it still tries to link it against glibc.
Example:

          # mlibc-stdenv was created using a custom stdenv and mkStdenvCustomLibc
          mlibc-libcxx = pkgs.pkgsLLVM.llvmPackages.libcxx.override {
            stdenv = mlibc-stdenv;
          };