Unverified Commit 56ff4ea4 authored by Jörg Thalheim's avatar Jörg Thalheim Committed by GitHub
Browse files

nixVersions.nix_2_26: Update and improve packaging (#383508)

parents 01d4f93b 2be4c2c0
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
2.26.1
 No newline at end of file
+0 −182
Original line number Diff line number Diff line
# These overrides are applied to the dependencies of the Nix components.

{
  src,

  # The raw Nixpkgs, not affected by this scope
  pkgs,

  stdenv,
}:

let
  prevStdenv = stdenv;
in

let
  inherit (pkgs) lib;

  root = ./.;

  stdenv = if prevStdenv.isDarwin && prevStdenv.isx86_64 then darwinStdenv else prevStdenv;

  # Fix the following error with the default x86_64-darwin SDK:
  #
  #     error: aligned allocation function of type 'void *(std::size_t, std::align_val_t)' is only available on macOS 10.13 or newer
  #
  # Despite the use of the 10.13 deployment target here, the aligned
  # allocation function Clang uses with this setting actually works
  # all the way back to 10.6.
  darwinStdenv = pkgs.overrideSDK prevStdenv { darwinMinVersion = "10.13"; };

  resolveRelPath = p: lib.path.removePrefix root p;
  resolvePath = p: src + "/${resolveRelPath p}";

  # Indirection for Nixpkgs to override when package.nix files are vendored
  # fileset filtering is not possible without IFD on src, so we ignore the fileset
  # and produce a path containing _more_, but the extra files generally won't be
  # accessed.
  # The Nix flake uses fileset.toSource for this.
  filesetToSource = { root, fileset }: resolvePath root;

  /**
    Given a set of layers, create a mkDerivation-like function
  */
  mkPackageBuilder =
    exts: userFn: stdenv.mkDerivation (lib.extends (lib.composeManyExtensions exts) userFn);

  localSourceLayer =
    finalAttrs: prevAttrs:
    let
      workDirPath =
        # Ideally we'd pick finalAttrs.workDir, but for now `mkDerivation` has
        # the requirement that everything except passthru and meta must be
        # serialized by mkDerivation, which doesn't work for this.
        prevAttrs.workDir;

      workDirSubpath = resolveRelPath workDirPath;
      # sources = assert prevAttrs.fileset._type == "fileset"; prevAttrs.fileset;
      # src = lib.fileset.toSource { fileset = sources; inherit root; };

    in
    {
      sourceRoot = "${src.name}/" + workDirSubpath;
      inherit src;

      # Clear what `derivation` can't/shouldn't serialize; see prevAttrs.workDir.
      fileset = null;
      workDir = null;
    };

  mesonLayer = finalAttrs: prevAttrs: {
    # NOTE:
    # As of https://github.com/NixOS/nixpkgs/blob/8baf8241cea0c7b30e0b8ae73474cb3de83c1a30/pkgs/by-name/me/meson/setup-hook.sh#L26,
    # `mesonBuildType` defaults to `plain` if not specified. We want our Nix-built binaries to be optimized by default.
    # More on build types here: https://mesonbuild.com/Builtin-options.html#details-for-buildtype.
    mesonBuildType = "release";
    # NOTE:
    # Users who are debugging Nix builds are expected to set the environment variable `mesonBuildType`, per the
    # guidance in https://github.com/NixOS/nix/blob/8a3fc27f1b63a08ac983ee46435a56cf49ebaf4a/doc/manual/source/development/debugging.md?plain=1#L10.
    # For this reason, we don't want to refer to `finalAttrs.mesonBuildType` here, but rather use the environment variable.
    preConfigure =
      prevAttrs.preConfigure or ""
      +
        lib.optionalString
          (
            !stdenv.hostPlatform.isWindows
            # build failure
            && !stdenv.hostPlatform.isStatic
            # LTO breaks exception handling on x86-64-darwin.
            && stdenv.system != "x86_64-darwin"
          )
          ''
            case "$mesonBuildType" in
            release|minsize) appendToVar mesonFlags "-Db_lto=true"  ;;
            *)               appendToVar mesonFlags "-Db_lto=false" ;;
            esac
          '';
    nativeBuildInputs = [
      pkgs.buildPackages.meson
      pkgs.buildPackages.ninja
    ] ++ prevAttrs.nativeBuildInputs or [ ];
    mesonCheckFlags = prevAttrs.mesonCheckFlags or [ ] ++ [
      "--print-errorlogs"
    ];
  };

  mesonBuildLayer = finalAttrs: prevAttrs: {
    nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [
      pkgs.buildPackages.pkg-config
    ];
    separateDebugInfo = !stdenv.hostPlatform.isStatic;
    hardeningDisable = lib.optional stdenv.hostPlatform.isStatic "pie";
    env =
      prevAttrs.env or { }
      // lib.optionalAttrs (
        stdenv.isLinux
        && !(stdenv.hostPlatform.isStatic && stdenv.system == "aarch64-linux")
        && !(stdenv.hostPlatform.useLLVM or false)
      ) { LDFLAGS = "-fuse-ld=gold"; };
  };

  mesonLibraryLayer = finalAttrs: prevAttrs: {
    outputs = prevAttrs.outputs or [ "out" ] ++ [ "dev" ];
  };

  # Work around weird `--as-needed` linker behavior with BSD, see
  # https://github.com/mesonbuild/meson/issues/3593
  bsdNoLinkAsNeeded =
    finalAttrs: prevAttrs:
    lib.optionalAttrs stdenv.hostPlatform.isBSD {
      mesonFlags = [ (lib.mesonBool "b_asneeded" false) ] ++ prevAttrs.mesonFlags or [ ];
    };

  miscGoodPractice = finalAttrs: prevAttrs: {
    strictDeps = prevAttrs.strictDeps or true;
    enableParallelBuilding = true;
  };
in
scope: {
  inherit stdenv;

  aws-sdk-cpp =
    (pkgs.aws-sdk-cpp.override {
      apis = [
        "s3"
        "transfer"
      ];
      customMemoryManagement = false;
    }).overrideAttrs
      {
        # only a stripped down version is built, which takes a lot less resources
        # to build, so we don't need a "big-parallel" machine.
        requiredSystemFeatures = [ ];
      };

  boehmgc = pkgs.boehmgc.override {
    enableLargeConfig = true;
  };

  inherit resolvePath filesetToSource;

  mkMesonDerivation = mkPackageBuilder [
    miscGoodPractice
    localSourceLayer
    mesonLayer
  ];
  mkMesonExecutable = mkPackageBuilder [
    miscGoodPractice
    bsdNoLinkAsNeeded
    localSourceLayer
    mesonLayer
    mesonBuildLayer
  ];
  mkMesonLibrary = mkPackageBuilder [
    miscGoodPractice
    bsdNoLinkAsNeeded
    localSourceLayer
    mesonLayer
    mesonBuildLayer
    mesonLibraryLayer
  ];
}
+0 −68
Original line number Diff line number Diff line
{
  lib,
  src,
  officialRelease,
}:

scope:

let
  inherit (scope) callPackage;

  baseVersion = lib.fileContents ../.version;

  versionSuffix = lib.optionalString (!officialRelease) "pre";

  fineVersionSuffix =
    lib.optionalString (!officialRelease)
      "pre${
        builtins.substring 0 8 (src.lastModifiedDate or src.lastModified or "19700101")
      }_${src.shortRev or "dirty"}";

  fineVersion = baseVersion + fineVersionSuffix;
in

# This becomes the pkgs.nixComponents attribute set
{
  version = baseVersion + versionSuffix;
  inherit versionSuffix;

  nix-util = callPackage ../src/libutil/package.nix { };
  nix-util-c = callPackage ../src/libutil-c/package.nix { };
  nix-util-test-support = callPackage ../src/libutil-test-support/package.nix { };
  nix-util-tests = callPackage ../src/libutil-tests/package.nix { };

  nix-store = callPackage ../src/libstore/package.nix { };
  nix-store-c = callPackage ../src/libstore-c/package.nix { };
  nix-store-test-support = callPackage ../src/libstore-test-support/package.nix { };
  nix-store-tests = callPackage ../src/libstore-tests/package.nix { };

  nix-fetchers = callPackage ../src/libfetchers/package.nix { };
  nix-fetchers-tests = callPackage ../src/libfetchers-tests/package.nix { };

  nix-expr = callPackage ../src/libexpr/package.nix { };
  nix-expr-c = callPackage ../src/libexpr-c/package.nix { };
  nix-expr-test-support = callPackage ../src/libexpr-test-support/package.nix { };
  nix-expr-tests = callPackage ../src/libexpr-tests/package.nix { };

  nix-flake = callPackage ../src/libflake/package.nix { };
  nix-flake-c = callPackage ../src/libflake-c/package.nix { };
  nix-flake-tests = callPackage ../src/libflake-tests/package.nix { };

  nix-main = callPackage ../src/libmain/package.nix { };
  nix-main-c = callPackage ../src/libmain-c/package.nix { };

  nix-cmd = callPackage ../src/libcmd/package.nix { };

  nix-cli = callPackage ../src/nix/package.nix { version = fineVersion; };

  nix-functional-tests = callPackage ../tests/functional/package.nix { version = fineVersion; };

  nix-manual = callPackage ../doc/manual/package.nix { version = fineVersion; };
  nix-internal-api-docs = callPackage ../src/internal-api-docs/package.nix { version = fineVersion; };
  nix-external-api-docs = callPackage ../src/external-api-docs/package.nix { version = fineVersion; };

  nix-perl-bindings = callPackage ../src/perl/package.nix { };

  nix-everything = callPackage ../packaging/everything.nix { };
}
+0 −208
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  buildEnv,

  nix-util,
  nix-util-c,
  nix-util-tests,

  nix-store,
  nix-store-c,
  nix-store-tests,

  nix-fetchers,
  nix-fetchers-tests,

  nix-expr,
  nix-expr-c,
  nix-expr-tests,

  nix-flake,
  nix-flake-c,
  nix-flake-tests,

  nix-main,
  nix-main-c,

  nix-cmd,

  nix-cli,

  nix-functional-tests,

  nix-manual,
  nix-internal-api-docs,
  nix-external-api-docs,

  nix-perl-bindings,

  testers,
  runCommand,
}:

let
  libs =
    {
      inherit
        nix-util
        nix-util-c
        nix-store
        nix-store-c
        nix-fetchers
        nix-expr
        nix-expr-c
        nix-flake
        nix-flake-c
        nix-main
        nix-main-c
        nix-cmd
        ;
    }
    // lib.optionalAttrs
      (!stdenv.hostPlatform.isStatic && stdenv.buildPlatform.canExecute stdenv.hostPlatform)
      {
        # Currently fails in static build
        inherit
          nix-perl-bindings
          ;
      };

  dev =
    nixAttrs:
    stdenv.mkDerivation (finalAttrs: {
      name = "nix-${nix-cli.version}-dev";
      pname = "nix";
      version = nix-cli.version;
      dontUnpack = true;
      dontBuild = true;
      libs = map lib.getDev (lib.attrValues libs);
      installPhase = ''
        mkdir -p $out/nix-support
        echo $libs >> $out/nix-support/propagated-build-inputs
        echo ${nixAttrs.finalPackage} >> $out/nix-support/propagated-build-inputs
      '';
      passthru = {
        tests = {
          # TODO: is this supposed to work for a dev output?
          # pkg-config = testers.hasPkgConfigModules {
          #   package = finalAttrs.finalPackage;
          # };
        };

        # If we were to fully emulate output selection here, we'd confuse the Nix CLIs,
        # because they rely on `drvPath`.
        dev = finalAttrs.finalPackage.out;
        out = nixAttrs.finalPackage.out;

        libs = throw "`nix.dev.libs` is not meant to be used; use `nix.libs` instead.";
      };
      meta = {
        pkgConfigModules = [
          "nix-cmd"
          "nix-expr"
          "nix-expr-c"
          "nix-fetchers"
          "nix-flake"
          "nix-flake-c"
          "nix-main"
          "nix-main-c"
          "nix-store"
          "nix-store-c"
          "nix-util"
          "nix-util-c"
        ];
      };
    });
  devdoc = buildEnv {
    name = "nix-${nix-cli.version}-devdoc";
    paths = [
      nix-internal-api-docs
      nix-external-api-docs
    ];
  };

in
(buildEnv {
  name = "nix-${nix-cli.version}";
  paths = [
    nix-cli
    nix-manual.man
  ];

  meta.mainProgram = "nix";
}).overrideAttrs
  (
    finalAttrs: prevAttrs: {
      doCheck = true;
      doInstallCheck = true;

      checkInputs =
        [
          # Make sure the unit tests have passed
          nix-util-tests.tests.run
          nix-store-tests.tests.run
          nix-expr-tests.tests.run
          nix-fetchers-tests.tests.run
          nix-flake-tests.tests.run

          # Make sure the functional tests have passed
          nix-functional-tests
        ]
        ++ lib.optionals
          (!stdenv.hostPlatform.isStatic && stdenv.buildPlatform.canExecute stdenv.hostPlatform)
          [
            # Perl currently fails in static build
            # TODO: Split out tests into a separate derivation?
            nix-perl-bindings
          ];
      passthru = prevAttrs.passthru // {
        inherit (nix-cli) version;

        /**
          These are the libraries that are part of the Nix project. They are used
          by the Nix CLI and other tools.

          If you need to use these libraries in your project, we recommend to use
          the `-c` C API libraries exclusively, if possible.

          We also recommend that you build the complete package to ensure that the unit tests pass.
          You could do this in CI, or by passing it in an unused environment variable. e.g in a `mkDerivation` call:

          ```nix
            buildInputs = [ nix.libs.nix-util-c nix.libs.nix-store-c ];
            # Make sure the nix libs we use are ok
            unusedInputsForTests = [ nix ];
            disallowedReferences = nix.all;
          ```
        */
        inherit libs;

        tests = prevAttrs.passthru.tests or { } // {
          pkg-config = testers.hasPkgConfigModules {
            package = finalAttrs.finalPackage;
          };
        };

        /**
          A derivation referencing the `dev` outputs of the Nix libraries.
        */
        dev = dev finalAttrs;
        inherit devdoc;
        doc = nix-manual;
        outputs = [
          "out"
          "dev"
          "devdoc"
          "doc"
        ];
        all = lib.attrValues (
          lib.genAttrs finalAttrs.passthru.outputs (outName: finalAttrs.finalPackage.${outName})
        );
      };
      meta = prevAttrs.meta // {
        description = "The Nix package manager";
        pkgConfigModules = (dev finalAttrs).meta.pkgConfigModules;
      };
    }
  )
+0 −6
Original line number Diff line number Diff line
{
    "owner": "NixOS",
    "repo": "nix",
    "rev": "36bd92736faaf81c6af3dff8f560963eb4e76b14",
    "hash": "sha256-1T7WRNfUMsiiNB77BuHElzjavguL8oJx+wBtfMcobq8="
}
Loading