nix/package.nix

357 lines
9.5 KiB
Nix
Raw Normal View History

2023-11-30 22:48:44 +00:00
{ lib
, callPackage
, stdenv
2023-12-03 19:10:09 +00:00
, releaseTools
2023-11-30 22:48:44 +00:00
, versionSuffix ? ""
, officialRelease ? false
, buildUnreleasedNotes ? false
, autoconf-archive
, autoreconfHook
, aws-sdk-cpp
, boehmgc
, nlohmann_json
, bison
, boost
, brotli
, bzip2
, changelog-d
, curl
, editline
, fileset
, flex
, git
, gtest
, jq
2023-12-03 19:10:09 +00:00
, doxygen
2023-11-30 22:48:44 +00:00
, libarchive
, libcpuid
, libgit2
, libseccomp
, libsodium
, lowdown
, mdbook
, mdbook-linkcheck
, mercurial
, openssh
, openssl
, pkg-config
, rapidcheck
, sqlite
, util-linux
, xz
2023-12-03 17:47:07 +00:00
2023-12-03 21:48:50 +00:00
, busybox-sandbox-shell ? null
2023-12-03 17:47:07 +00:00
# Configuration Options
#
# This probably seems like too many degrees of freedom, but it
# faithfully reflects how the underlying configure + make build system
# work. The top-level flake.nix will choose useful combinations.
2023-12-03 19:10:09 +00:00
, pname ? "nix"
, doBuild ? true
2023-12-03 21:48:50 +00:00
, doCheck ? __forDefaults.canRunInstalled
, doInstallCheck ? __forDefaults.canRunInstalled
2023-12-03 19:10:09 +00:00
, withCoverageChecks ? false
2023-12-03 21:48:50 +00:00
# Whether to build the regular manual
, enableManual ? __forDefaults.canRunInstalled
2023-12-03 19:10:09 +00:00
# Whether to build the internal API docs, can be done separately from
# everything else.
, enableInternalAPIDocs ? false
2023-12-03 17:47:07 +00:00
# Whether to install unit tests. This is useful when cross compiling
# since we cannot run them natively during the build, but can do so
# later.
2023-12-03 21:48:50 +00:00
, installUnitTests ? __forDefaults.canRunInstalled
2023-12-03 19:10:09 +00:00
2023-12-03 21:48:50 +00:00
# For running the functional tests against a pre-built Nix. Probably
# want to use in conjunction with `doBuild = false;`.
2023-12-03 19:10:09 +00:00
, test-daemon ? null
, test-client ? null
2023-12-03 21:48:50 +00:00
# Not a real argument, just the only way to approximate let-binding some
# stuff for argument defaults.
, __forDefaults ? {
canRunInstalled = doBuild && stdenv.buildPlatform.canExecute stdenv.hostPlatform;
}
} @ attrs0:
2023-11-30 22:48:44 +00:00
let
version = lib.fileContents ./.version + versionSuffix;
2023-12-02 17:25:47 +00:00
2023-12-03 21:48:50 +00:00
# selected attributes with defaults, will be used to define some
# things which should instead be gotten via `finalAttrs` in order to
# work with overriding.
2023-12-03 19:10:09 +00:00
attrs = {
inherit doBuild doCheck doInstallCheck;
};
2023-12-02 17:25:47 +00:00
filesets = {
baseFiles = fileset.fileFilter (f: f.name != ".gitignore") ./.;
configureFiles = fileset.unions [
./.version
./configure.ac
./m4
# TODO: do we really need README.md? It doesn't seem used in the build.
./README.md
];
topLevelBuildFiles = fileset.unions [
./local.mk
./Makefile
./Makefile.config.in
./mk
];
functionalTestFiles = fileset.unions [
./tests/functional
(fileset.fileFilter (f: lib.strings.hasPrefix "nix-profile" f.name) ./scripts)
];
};
2023-12-03 19:10:09 +00:00
mkDerivation =
if withCoverageChecks
then releaseTools.coverageAnalysis
else stdenv.mkDerivation;
2023-11-30 22:48:44 +00:00
in
2023-12-03 19:10:09 +00:00
mkDerivation (finalAttrs: let
inherit (finalAttrs)
doCheck
doInstallCheck
;
doBuild = !finalAttrs.dontBuild;
2023-12-03 17:47:07 +00:00
# Either running the unit tests during the build, or installing them
# to be run later, requiresthe unit tests to be built.
2023-12-03 19:10:09 +00:00
buildUnitTests = doCheck || installUnitTests;
anySortOfTesting = buildUnitTests || doInstallCheck;
2023-12-03 17:47:07 +00:00
in {
2023-12-03 19:10:09 +00:00
inherit pname version;
2023-11-30 22:48:44 +00:00
src =
let
in
fileset.toSource {
root = ./.;
2023-12-03 19:10:09 +00:00
fileset = fileset.intersect filesets.baseFiles (fileset.unions ([
2023-12-02 17:25:47 +00:00
filesets.configureFiles
filesets.topLevelBuildFiles
2023-12-03 19:10:09 +00:00
] ++ lib.optionals doBuild [
2023-11-30 22:48:44 +00:00
./boehmgc-coroutine-sp-fallback.diff
./doc
./misc
./precompiled-headers.h
./src
./unit-test-data
./COPYING
./scripts/local.mk
2023-12-03 19:10:09 +00:00
] ++ lib.optionals anySortOfTesting [
2023-12-02 17:25:47 +00:00
filesets.functionalTestFiles
2023-12-03 19:10:09 +00:00
]));
2023-11-30 22:48:44 +00:00
};
VERSION_SUFFIX = versionSuffix;
2023-12-03 21:48:50 +00:00
outputs = [ "out" ]
++ lib.optional doBuild "dev"
# If we are doing just build or just docs, the one thing will use
# "out". We only need additional outputs if we are doing both.
++ lib.optional (doBuild && (enableManual || enableInternalAPIDocs)) "doc"
2023-12-03 17:47:07 +00:00
++ lib.optional installUnitTests "check";
2023-11-30 22:48:44 +00:00
nativeBuildInputs = [
bison
flex
(lib.getBin lowdown)
2023-12-01 11:25:22 +00:00
jq # Also for custom mdBook preprocessor.
2023-11-30 22:48:44 +00:00
mdbook
mdbook-linkcheck
autoconf-archive
autoreconfHook
pkg-config
]
++ lib.optional stdenv.hostPlatform.isLinux util-linux
2023-12-03 21:48:50 +00:00
# Official releases don't have rl-next, so we don't need to compile a
# changelog
2023-11-30 22:48:44 +00:00
++ lib.optional (!officialRelease && buildUnreleasedNotes) changelog-d;
2023-12-03 21:48:50 +00:00
buildInputs = lib.optionals doBuild [
2023-11-30 22:48:44 +00:00
boost
brotli
bzip2
curl
editline
libarchive
libgit2
libsodium
lowdown
openssl
sqlite
xz
2023-12-03 21:48:50 +00:00
] ++ lib.optional stdenv.isLinux libseccomp
++ lib.optional stdenv.hostPlatform.isx86_64 libcpuid
# There have been issues building these dependencies
++ lib.optional (stdenv.hostPlatform == stdenv.buildPlatform && (stdenv.isLinux || stdenv.isDarwin))
(aws-sdk-cpp.override {
apis = ["s3" "transfer"];
customMemoryManagement = false;
})
2023-12-01 11:25:22 +00:00
;
2023-12-03 19:10:09 +00:00
propagatedBuildInputs = [
boehmgc
nlohmann_json
];
dontBuild = !attrs.doBuild;
doCheck = attrs.doCheck;
2023-12-01 11:25:22 +00:00
checkInputs = [
2023-12-03 21:48:50 +00:00
gtest
rapidcheck
2023-11-30 22:49:02 +00:00
];
2023-11-30 22:48:44 +00:00
2023-12-01 11:25:22 +00:00
nativeCheckInputs = [
git
2023-12-02 16:08:06 +00:00
mercurial
openssh
2023-12-03 19:10:09 +00:00
] ++ lib.optionals enableInternalAPIDocs [
doxygen
2023-11-30 22:48:44 +00:00
];
disallowedReferences = [ boost ];
2023-12-01 11:25:22 +00:00
preConfigure = lib.optionalString (! stdenv.hostPlatform.isStatic) ''
# Copy libboost_context so we don't get all of Boost in our closure.
# https://github.com/NixOS/nixpkgs/issues/45462
mkdir -p $out/lib
cp -pd ${boost}/lib/{libboost_context*,libboost_thread*,libboost_system*,libboost_regex*} $out/lib
rm -f $out/lib/*.a
${lib.optionalString stdenv.hostPlatform.isLinux ''
chmod u+w $out/lib/*.so.*
patchelf --set-rpath $out/lib:${stdenv.cc.cc.lib}/lib $out/lib/libboost_thread.so.*
''}
${lib.optionalString stdenv.hostPlatform.isDarwin ''
for LIB in $out/lib/*.dylib; do
chmod u+w $LIB
install_name_tool -id $LIB $LIB
install_name_tool -delete_rpath ${boost}/lib/ $LIB || true
done
install_name_tool -change ${boost}/lib/libboost_system.dylib $out/lib/libboost_system.dylib $out/lib/libboost_thread.dylib
''}
'';
2023-11-30 22:48:44 +00:00
2023-12-03 19:10:09 +00:00
configureFlags = [
"--sysconfdir=/etc"
(lib.enableFeature doBuild "build")
(lib.enableFeature anySortOfTesting "test")
(lib.enableFeature enableInternalAPIDocs "internal-api-docs")
2023-12-03 21:48:50 +00:00
(lib.enableFeature enableManual "doc-gen")
2023-12-03 19:10:09 +00:00
(lib.enableFeature installUnitTests "install-unit-tests")
] ++ lib.optionals installUnitTests [
"--with-check-bin-dir=${builtins.placeholder "check"}/bin"
"--with-check-lib-dir=${builtins.placeholder "check"}/lib"
2023-12-03 21:48:50 +00:00
] ++ lib.optionals (doBuild && stdenv.isLinux) [
2023-12-03 19:10:09 +00:00
"--with-boost=${boost}/lib"
2023-12-03 21:48:50 +00:00
"--with-sandbox-shell=${busybox-sandbox-shell}/bin/busybox"
] ++ lib.optional (doBuild && stdenv.isLinux && !(stdenv.hostPlatform.isStatic && stdenv.system == "aarch64-linux"))
2023-12-03 19:10:09 +00:00
"LDFLAGS=-fuse-ld=gold"
2023-12-03 21:48:50 +00:00
++ lib.optional (doBuild && stdenv.hostPlatform.isStatic) "--enable-embedded-sandbox-shell"
2023-12-03 19:10:09 +00:00
++ lib.optional buildUnitTests "RAPIDCHECK_HEADERS=${lib.getDev rapidcheck}/extras/gtest/include";
2023-11-30 22:48:44 +00:00
enableParallelBuilding = true;
makeFlags = "profiledir=$(out)/etc/profile.d PRECOMPILE_HEADERS=1";
2023-12-03 19:10:09 +00:00
installTargets = lib.optional doBuild "install"
++ lib.optional enableInternalAPIDocs "internal-api-html";
2023-11-30 22:48:44 +00:00
installFlags = "sysconfdir=$(out)/etc";
2023-12-03 19:10:09 +00:00
# In this case we are probably just running tests, and so there isn't
# anything to install, we just make an empty directory to signify tests
# succeeded.
installPhase = if finalAttrs.installTargets != [] then null else ''
mkdir -p $out
'';
2023-12-03 21:48:50 +00:00
postInstall = lib.optionalString doBuild (
''
mkdir -p $doc/nix-support
echo "doc manual $doc/share/doc/nix/manual" >> $doc/nix-support/hydra-build-products
'' + lib.optionalString stdenv.hostPlatform.isStatic ''
2023-11-30 22:48:44 +00:00
mkdir -p $out/nix-support
echo "file binary-dist $out/bin/nix" >> $out/nix-support/hydra-build-products
2023-12-03 21:48:50 +00:00
'' + lib.optionalString stdenv.isDarwin ''
2023-11-30 22:48:44 +00:00
install_name_tool \
-change ${boost}/lib/libboost_context.dylib \
$out/lib/libboost_context.dylib \
$out/lib/libnixutil.dylib
install_name_tool \
-change ${boost}/lib/libboost_regex.dylib \
$out/lib/libboost_regex.dylib \
$out/lib/libnixexpr.dylib
2023-12-03 21:48:50 +00:00
''
) + lib.optionalString enableInternalAPIDocs ''
mkdir -p ''${!outputDoc}/nix-support
echo "doc internal-api-docs $out/share/doc/nix/internal-api/html" >> ''${!outputDoc}/nix-support/hydra-build-products
2023-11-30 22:48:44 +00:00
'';
2023-12-03 19:10:09 +00:00
doInstallCheck = attrs.doInstallCheck;
2023-11-30 22:48:44 +00:00
installCheckFlags = "sysconfdir=$(out)/etc";
installCheckTarget = "installcheck"; # work around buggy detection in stdenv
2023-12-03 19:10:09 +00:00
# Needed for tests if we are not doing a build, but testing existing
# built Nix.
preInstallCheck = lib.optionalString (! doBuild) ''
mkdir -p src/nix-channel
'';
2023-11-30 22:48:44 +00:00
separateDebugInfo = !stdenv.hostPlatform.isStatic;
strictDeps = true;
hardeningDisable = lib.optional stdenv.hostPlatform.isStatic "pie";
2023-12-03 19:10:09 +00:00
passthru = {
2023-12-02 17:25:47 +00:00
inherit filesets;
perl-bindings = callPackage ./perl {
inherit fileset stdenv;
};
2023-11-30 22:48:44 +00:00
};
2023-12-03 19:10:09 +00:00
meta = {
platforms = lib.platforms.unix;
mainProgram = "nix";
broken = !(lib.all (a: a) [
(installUnitTests -> doBuild)
(doCheck -> doBuild)
]);
};
} // lib.optionalAttrs withCoverageChecks {
lcovFilter = [ "*/boost/*" "*-tab.*" ];
hardeningDisable = ["fortify"];
NIX_CFLAGS_COMPILE = "-DCOVERAGE=1";
dontInstall = false;
} // lib.optionalAttrs (test-daemon != null) {
NIX_DAEMON_PACKAGE = test-daemon;
} // lib.optionalAttrs (test-client != null) {
NIX_CLIENT_PACKAGE = test-client;
2023-11-30 22:48:44 +00:00
})