Skip to content

Nixification #81

@piegamesde

Description

@piegamesde

As soon as I read that mold uses Gentoo for testing because it's source-based, I instantly thought about Nix being a perfect fit for it. Achieving this however turned out to be a lot harder than I initially thought, because the bootstrapping code is rather complex compared to the simple overrides one usually does.

Nevertheless, here's a file that builds any selected packages (list at the bottom) and all their transitive dependencies using mold as links (if they used bfd before). (Edit: updated file here)

let
  # Pin some fairly new nixpkgs
  sources = builtins.fetchTarball {
    name = "nixpkgs-unstable-2021-07-06";
    url = "https://github.com/nixos/nixpkgs/archive/291b3ff5af268eb7a656bb11c73f2fe535ea2170.tar.gz";
    sha256 = "1z2l7q4cmiaqb99cd8yfisdr1n6xbwcczr9020ss47y2z1cn1x7x";
  };

  # For bootstrapping
  pkgs0 = import sources {
    overlays = [
      (pkgs: super: {
        # TODO replace with proper packaging once https://github.com/NixOS/nixpkgs/pull/128889 is merged
        mold = pkgs.stdenv.mkDerivation {
          pname = "mold";
          version = "0.9.1";
          src = pkgs.fetchgit {
            url = "https://github.com/rui314/mold";
            rev = "v0.9.1";
            sha256 = "sha256-yIkW6OCXhlHZ1jC8/yMAdJbSgY9K40POT2zWv6wYr5E=";
          };
          nativeBuildInputs = with pkgs; [ clang_12 cmake lld_12 tbb xxHash zlib openssl git ];
          dontUseCmakeConfigure = "true";
          buildPhase = "make -j $NIX_BUILD_CORES";
          installPhase = "mkdir -p $out $out/bin $out/share/man/man1 && PREFIX=$out make install";
        };

        binutils_mold = pkgs.wrapBintoolsWith {
          bintools = pkgs.binutils-unwrapped.overrideAttrs (old: {
            postInstall = ''
              rm $out/bin/ld.gold
              rm $out/bin/ld.bfd
              ln -sf ${pkgs.mold}/bin/mold $out/bin/ld.bfd
            '';
          });
        };
        
        stdenv_mold = super.overrideCC super.stdenv (super.wrapCCWith rec {
          cc = super.gcc-unwrapped;
          bintools = pkgs.binutils_mold;
        });
      })
    ];
  };

  # Actual nixpkgs with patched linker in all packages
  pkgs = import sources {
    overlays = [
      (pkgs: super: {
        stdenv = pkgs0.stdenv_mold;
        mold = pkgs0.mold;
      })
    ];
  };
in with pkgs;
  # Packages we want to build
  linkFarmFromDrvs "packages-with-mold" [
    mold
    binutils
    stdenv
    opencv
    jack2
    chromium
  ]

Let me know what you think.

CC @nitsky

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions