nix/package.nix

369 lines
10 KiB
Nix
Raw Normal View History

2023-11-30 22:48:44 +00:00
{ lib
, stdenv
2023-12-03 19:10:09 +00:00
, releaseTools
2023-11-30 22:48:44 +00:00
, autoconf-archive
, autoreconfHook
, aws-sdk-cpp
, boehmgc
, nlohmann_json
, bison
, boost
, brotli
, bzip2
, curl
, editline
, readline
2023-11-30 22:48:44 +00:00
, flex
, git
, gtest
, jq
, libarchive
, libcpuid
, libgit2
, libseccomp
, libsodium
2024-03-04 15:07:03 +00:00
, man
2023-11-30 22:48:44 +00:00
, lowdown
, mdbook
, mdbook-linkcheck
, mercurial
, openssh
, openssl
, pkg-config
, rapidcheck
, sqlite
, toml11
2024-06-28 18:26:04 +00:00
, unixtools
2023-11-30 22:48:44 +00:00
, 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
2023-12-03 23:12:05 +00:00
#:
2023-12-03 17:47:07 +00:00
# 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 of these
# options to CI.
2023-12-03 17:47:07 +00:00
2023-12-03 19:10:09 +00:00
, pname ? "nix"
, version
, versionSuffix
2023-12-03 23:12:05 +00:00
# Whether to build Nix. Useful to skip for tasks like testing existing pre-built versions of Nix
2023-12-03 19:10:09 +00:00
, doBuild ? true
# Run the unit tests as part of the build. See `installUnitTests` for an
# alternative to this.
, doCheck ? __forDefaults.canRunInstalled
# Run the functional tests as part of the build.
2023-12-03 23:12:05 +00:00
, doInstallCheck ? test-client != null || __forDefaults.canRunInstalled
2023-12-03 19:10:09 +00:00
# Check test coverage of Nix. Probably want to use with at least
# one of `doCHeck` or `doInstallCheck` enabled.
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
# Whether to use garbage collection for the Nix language evaluator.
#
# If it is disabled, we just leak memory, but this is not as bad as it
# sounds so long as evaluation just takes places within short-lived
# processes. (When the process exits, the memory is reclaimed; it is
# only leaked *within* the process.)
#
# Temporarily disabled on Windows because the `GC_throw_bad_alloc`
# symbol is missing during linking.
, enableGC ? !stdenv.hostPlatform.isWindows
# Whether to enable Markdown rendering in the Nix binary.
, enableMarkdown ? !stdenv.hostPlatform.isWindows
# Which interactive line editor library to use for Nix's repl.
#
# Currently supported choices are:
#
# - editline (default)
# - readline
, readlineFlavor ? if stdenv.hostPlatform.isWindows then "readline" else "editline"
# 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.
, installUnitTests ? doBuild && !__forDefaults.canExecuteHost
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
# Avoid setting things that would interfere with a functioning devShell
, forDevShell ? false
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 ? {
canExecuteHost = stdenv.buildPlatform.canExecute stdenv.hostPlatform;
canRunInstalled = doBuild && __forDefaults.canExecuteHost;
2023-12-03 21:48:50 +00:00
}
}:
2023-11-30 22:48:44 +00:00
let
inherit (lib) fileset;
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-03 19:10:09 +00:00
};
mkDerivation =
if withCoverageChecks
2023-12-03 23:12:05 +00:00
then
# TODO support `finalAttrs` args function in
# `releaseTools.coverageAnalysis`.
argsFun:
releaseTools.coverageAnalysis (let args = argsFun args; in args)
2023-12-03 19:10:09 +00:00
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
2023-12-03 19:10:09 +00:00
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.
buildUnitTests = doCheck || installUnitTests;
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
baseFiles = fileset.fileFilter (f: f.name != ".gitignore") ./.;
2023-11-30 22:48:44 +00:00
in
fileset.toSource {
root = ./.;
fileset = fileset.intersection baseFiles (fileset.unions ([
# For configure
./.version
./configure.ac
./m4
# TODO: do we really need README.md? It doesn't seem used in the build.
./README.md
# This could be put behind a conditional
./maintainers/local.mk
# For make, regardless of what we are building
./local.mk
./Makefile
./Makefile.config.in
./mk
(fileset.fileFilter (f: lib.strings.hasPrefix "nix-profile" f.name) ./scripts)
2023-12-03 19:10:09 +00:00
] ++ lib.optionals doBuild [
2023-11-30 22:48:44 +00:00
./doc
./misc
./precompiled-headers.h
(fileset.difference ./src ./src/perl)
2023-11-30 22:48:44 +00:00
./COPYING
./scripts/local.mk
2024-06-27 15:28:08 +00:00
] ++ lib.optionals enableManual [
./doc/manual
] ++ lib.optionals buildUnitTests [
./tests/unit
] ++ lib.optionals doInstallCheck [
./tests/functional
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) "doc"
++ lib.optional installUnitTests "check"
++ lib.optional doCheck "testresults"
2024-06-16 14:34:49 +00:00
;
2023-11-30 22:48:44 +00:00
nativeBuildInputs = [
2023-12-03 23:12:05 +00:00
autoconf-archive
autoreconfHook
pkg-config
] ++ lib.optionals doBuild [
2023-11-30 22:48:44 +00:00
bison
flex
2023-12-03 23:12:05 +00:00
] ++ lib.optionals enableManual [
2023-11-30 22:48:44 +00:00
(lib.getBin lowdown)
mdbook
mdbook-linkcheck
] ++ lib.optionals doInstallCheck [
git
mercurial
openssh
2024-03-04 15:07:03 +00:00
man # for testing `nix-* --help`
2023-12-03 23:12:05 +00:00
] ++ lib.optionals (doInstallCheck || enableManual) [
jq # Also for custom mdBook preprocessor.
2024-06-28 18:26:04 +00:00
] ++ lib.optional stdenv.hostPlatform.isStatic unixtools.hexdump
2023-12-03 23:12:05 +00:00
;
2023-11-30 22:48:44 +00:00
buildInputs = lib.optionals doBuild (
[
2023-11-30 22:48:44 +00:00
brotli
bzip2
curl
libarchive
libgit2
libsodium
openssl
sqlite
2024-06-27 23:30:58 +00:00
toml11
2023-11-30 22:48:44 +00:00
xz
({ inherit readline editline; }.${readlineFlavor})
] ++ lib.optionals enableMarkdown [
lowdown
] ++ lib.optionals buildUnitTests [
gtest
rapidcheck
] ++ lib.optional stdenv.isLinux libseccomp
2023-12-03 21:48:50 +00:00
++ 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
);
2023-12-01 11:25:22 +00:00
propagatedBuildInputs = lib.optionals doBuild ([
2024-06-27 21:28:31 +00:00
boost
2023-12-03 19:10:09 +00:00
nlohmann_json
] ++ lib.optional enableGC boehmgc
);
2023-12-03 19:10:09 +00:00
dontBuild = !attrs.doBuild;
doCheck = attrs.doCheck;
2023-12-01 11:25:22 +00:00
2023-12-03 19:10:09 +00:00
configureFlags = [
(lib.enableFeature doBuild "build")
(lib.enableFeature buildUnitTests "unit-tests")
(lib.enableFeature doInstallCheck "functional-tests")
2023-12-03 21:48:50 +00:00
(lib.enableFeature enableManual "doc-gen")
(lib.enableFeature enableGC "gc")
(lib.enableFeature enableMarkdown "markdown")
(lib.enableFeature installUnitTests "install-unit-tests")
(lib.withFeatureAs true "readline-flavor" readlineFlavor)
] ++ lib.optionals (!forDevShell) [
"--sysconfdir=/etc"
] ++ lib.optionals installUnitTests [
"--with-check-bin-dir=${builtins.placeholder "check"}/bin"
"--with-check-lib-dir=${builtins.placeholder "check"}/lib"
] ++ lib.optionals (doBuild) [
2023-12-03 19:10:09 +00:00
"--with-boost=${boost}/lib"
] ++ lib.optionals (doBuild && stdenv.isLinux) [
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-11-30 22:48:44 +00:00
enableParallelBuilding = true;
makeFlags = "profiledir=$(out)/etc/profile.d PRECOMPILE_HEADERS=1";
2024-06-16 14:34:49 +00:00
preCheck = ''
mkdir $testresults
'';
installTargets = lib.optional doBuild "install";
2023-12-03 19:10:09 +00:00
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 (
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 enableManual ''
mkdir -p ''${!outputDoc}/nix-support
echo "doc manual ''${!outputDoc}/share/doc/nix/manual" >> ''${!outputDoc}/nix-support/hydra-build-products
2023-11-30 22:48:44 +00:00
'';
# So the check output gets links for DLLs in the out output.
preFixup = lib.optionalString (stdenv.hostPlatform.isWindows && builtins.elem "check" finalAttrs.outputs) ''
ln -s "$check/lib/"*.dll "$check/bin"
ln -s "$out/bin/"*.dll "$check/bin"
'';
2023-12-03 19:10:09 +00:00
doInstallCheck = attrs.doInstallCheck;
2023-11-30 22:48:44 +00:00
installCheckFlags = "sysconfdir=$(out)/etc";
2023-12-03 23:57:16 +00:00
# Work around buggy detection in stdenv.
2023-12-03 23:12:05 +00:00
installCheckTarget = "installcheck";
2023-12-03 23:57:16 +00:00
# Work around weird bug where it doesn't think there is a Makefile.
2023-12-03 23:12:05 +00:00
installCheckPhase = if (!doBuild && doInstallCheck) then ''
runHook preInstallCheck
2023-12-03 23:12:05 +00:00
mkdir -p src/nix-channel
make installcheck -j$NIX_BUILD_CORES -l$NIX_BUILD_CORES
'' else null;
2023-11-30 22:48:44 +00:00
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
''
# See https://github.com/NixOS/nix/issues/2523
# Occurs often in tests since https://github.com/NixOS/nix/pull/9900
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
'';
2023-12-03 19:10:09 +00:00
2023-11-30 22:48:44 +00:00
separateDebugInfo = !stdenv.hostPlatform.isStatic;
Build `nix-util` with Meson The idea is two-fold: - Replace autotools with Meson - Build each library in its own derivation The interaction of these two features is that Meson's "subprojects" feature (https://mesonbuild.com/Subprojects) allows us to have single dev shell for building all libraries still, while also building things separately. This allows us to break up the build without a huge productivity lost. I tested the Linux native build, and NetBSD and Windows cross builds. Also do some clean ups of the Flake in the process of supporting new jobs. Special thanks to everyone that has worked on a Meson port so far, @p01arst0rm and @Qyriad in particular. Co-Authored-By: p01arst0rm <polar@ever3st.com> Co-Authored-By: Artemis Tosini <lix@artem.ist> Co-Authored-By: Artemis Tosini <me@artem.ist> Co-Authored-By: Felix Uhl <felix.uhl@outlook.com> Co-Authored-By: Jade Lovelace <lix@jade.fyi> Co-Authored-By: Lunaphied <lunaphied@lunaphied.me> Co-Authored-By: Maximilian Bosch <maximilian@mbosch.me> Co-Authored-By: Pierre Bourdon <delroth@gmail.com> Co-Authored-By: Qyriad <qyriad@qyriad.me> Co-Authored-By: Rebecca Turner <rbt@sent.as> Co-Authored-By: Winter <winter@winter.cafe> Co-Authored-By: eldritch horrors <pennae@lix.systems> Co-Authored-By: jade <lix@jade.fyi> Co-Authored-By: julia <midnight@trainwit.ch> Co-Authored-By: rebecca “wiggles” turner <rbt@sent.as> Co-Authored-By: wiggles dog <rbt@sent.as> Co-Authored-By: fricklerhandwerk <valentin@fricklerhandwerk.de> Co-authored-By: Eli Schwartz <eschwartz93@gmail.com> Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2024-06-04 13:28:27 +00:00
# TODO Always true after https://github.com/NixOS/nixpkgs/issues/318564
2023-12-03 23:12:05 +00:00
strictDeps = !withCoverageChecks;
2023-11-30 22:48:44 +00:00
hardeningDisable = lib.optional stdenv.hostPlatform.isStatic "pie";
2023-12-03 19:10:09 +00:00
meta = {
platforms = lib.platforms.unix ++ lib.platforms.windows;
2023-12-03 19:10:09 +00:00
mainProgram = "nix";
broken = !(lib.all (a: a) [
# We cannot run or install unit tests if we don't build them or
# Nix proper (which they depend on).
(installUnitTests -> doBuild)
(doCheck -> doBuild)
2023-12-03 23:53:05 +00:00
# The build process for the manual currently requires extracting
# data from the Nix executable we are trying to document.
(enableManual -> doBuild)
2023-12-03 19:10:09 +00:00
]);
};
} // 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
})