diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b19470138..74fa1a4d50 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,3 +38,12 @@ jobs: run: | export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" make test -j$(nproc) + build-with-nix: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: cachix/install-nix-action@v13 + with: + nix_path: nixpkgs=channel:nixos-stable + - name: Build the CI package set + run: nix-build default.nix -A ciPackages diff --git a/README.md b/README.md index c5f6487c3a..d5af1ed1c9 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,31 @@ String dump of section '.comment': If `mold` is in `.comment`, the file is created by mold. +### Use with Nix + +The `default.nix` of this repository provides an easy method to build +packages with mold as linker. General usage is `nix-build -A $attribute`, +the following attributes are provided: + +- `pkgs`: A baseline package set for comparison. Sometimes packages are simply + broken and mold has nothing to do with it. Use `pkgs.packageName` to build specific + packages, and `nix search` or https://search.nixos.org/packages to find the + attribute name. +- `pkgsMold`: Equivalent to `pkgs`, except that `bfd` is replaced by mold as linker. + This has a few implications that are important to know: + - This will bootstrap the entire toolchain and build all transitive dependencies every + time mold changes. + - Packages that require a custom linker (like lld or some special GCC) won't be built with + mold. +- `pkgsMoldCI`: Equivalent to `pkgsMold`, except that mold is disabled for known failing + packages until they are resolved. +- `ciPackages`: A set of working packages that are built as part of the CI in order to catch + regressions. + +After a successful build, `./result` will be a symlink to the output. +You can install Nix alongside your usual package manager, see https://nixos.org/download.html. +There are also Docker images providing a local Nix installation. + # Design and implementation of mold For the rest of this documentation, I'll explain the design and the diff --git a/default.nix b/default.nix new file mode 100644 index 0000000000..1f251f667e --- /dev/null +++ b/default.nix @@ -0,0 +1,69 @@ +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 + pkgs = 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 = ./.; + 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 + pkgsMold = import sources { + overlays = [ + (self: super: { + stdenv = pkgs.stdenv_mold; + mold = pkgs.mold; + }) + ]; + }; + + # Like pkgsMold, but we disable mold individually for known failing packages until their issues are resolved + pkgsMoldCI = pkgsMold.appendOverlays [ + (self: super: { + inherit (pkgs) + valgrind # https://github.com/rui314/mold/issues/81#issuecomment-876425070 + ; + }) + ]; +in { + inherit pkgs pkgsMold pkgsMoldCI; + + # Packages we already know that work in order to catch regressions + ciPackages = with pkgsMoldCI; linkFarmFromDrvs "packages-with-mold" [ + binutils + stdenv + # TODO + ]; +}