From 4bc11fadb850e90fd0a8322834101498ee5935b1 Mon Sep 17 00:00:00 2001 From: cdecker Date: Wed, 21 Oct 2026 18:00:24 +0200 Subject: [PATCH 01/30] configure: make cross and fully-static builds possible Three changes a static or cross build needs, none of which affects a native dynamic one: - the configurator itself is compiled with CONFIGURATOR_CC. It runs on the build machine, so when cross-compiling it must not be built for the host; its test programs are still compiled with CC and are now run through CONFIGURATOR_WRAPPER (e.g. qemu-aarch64), which makes the five places the build runs its own output explicit instead of relying on the kernel's binfmt registration. - POSTGRES_LDLIBS and POSTGRES_INCLUDE can come from the environment. A static libpq.a needs archives pg_config does not report (-lpgcommon -lpgport -lssl -lcrypto), and the no-pg_config branch used to *assign* these empty, discarding what the caller set -- which silently produced a lightningd with no PostgreSQL support at all. --- configure | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/configure b/configure index 52792463642d..a84cbd425e5a 100755 --- a/configure +++ b/configure @@ -36,6 +36,11 @@ fi # line enables the automagic detection that most users want. : ${PG_CONFIG=pg_config} +# You can set POSTGRES_LDLIBS (and POSTGRES_INCLUDE) in the environment to +# override what 'pg_config' reports, e.g. to link libpq statically where +# libpq.a needs extra archives: POSTGRES_LDLIBS="-lpq -lpgcommon -lpgport +# -lssl -lcrypto". Leave unset for the automagic pg_config detection. + usage_with_default() { if [ $# = 4 ]; then @@ -224,6 +229,7 @@ set_defaults() PYTEST=${PYTEST-$(default_pytest $PYTHON)} COPTFLAGS=${COPTFLAGS-$(default_coptflags "$DEBUGBUILD")} CONFIGURATOR_CC=${CONFIGURATOR_CC-$CC} + CONFIGURATOR_WRAPPER=${CONFIGURATOR_WRAPPER-} VALGRIND=${VALGRIND:-$(default_valgrind_setting)} TEST_NETWORK=${TEST_NETWORK:-regtest} RUST=${RUST:-$(default_rust_setting)} @@ -258,7 +264,10 @@ usage() echo " Note: On macOS, -g is used instead of -g3 for libbacktrace compatibility" fi usage_with_default "CONFIGURATOR_CC" "${CONFIGURATOR_CC:-$CC}" - echo " To override compile line for configurator itself" + echo " To override compile line for configurator itself (it runs on" + echo " the build machine, so set it to a native compiler when cross-compiling)" + usage_with_default "CONFIGURATOR_WRAPPER" "$CONFIGURATOR_WRAPPER" + echo " Command prefix to run configurator's test programs, e.g. qemu-aarch64-static -L /" usage_with_default "PYTEST" "$PYTEST" usage_with_default "VALGRIND" "$VALGRIND" usage_with_default "SED" "$SED" @@ -324,6 +333,7 @@ for opt in "$@"; do ;; CC=*) CC="${opt#CC=}";; CONFIGURATOR_CC=*) CONFIGURATOR_CC="${opt#CONFIGURATOR_CC=}";; + CONFIGURATOR_WRAPPER=*) CONFIGURATOR_WRAPPER="${opt#CONFIGURATOR_WRAPPER=}";; CWARNFLAGS=*) CWARNFLAGS="${opt#CWARNFLAGS=}";; CDEBUGFLAGS=*) CDEBUGFLAGS="${opt#CDEBUGFLAGS=}";; COPTFLAGS=*) COPTFLAGS="${opt#COPTFLAGS=}";; @@ -400,8 +410,11 @@ else fi # We assume warning flags don't affect congfigurator that much! +# The configurator itself runs here, on the build machine, so it is compiled +# with CONFIGURATOR_CC (defaults to CC; a native compiler when cross-compiling). +# Its test programs are compiled with CC and run via CONFIGURATOR_WRAPPER. echo -n "Compiling $CONFIGURATOR..." -$CC ${CWARNFLAGS-$BASE_WARNFLAGS} $CDEBUGFLAGS $COPTFLAGS $LDFLAGS -o $CONFIGURATOR $CONFIGURATOR.c +$CONFIGURATOR_CC ${CWARNFLAGS-$BASE_WARNFLAGS} $CDEBUGFLAGS $COPTFLAGS $LDFLAGS -o $CONFIGURATOR $CONFIGURATOR.c echo "done" if [ "$CLANG_COVERAGE" = "1" ]; then @@ -441,11 +454,16 @@ if command -v "${PKG_CONFIG}" >/dev/null; then SODIUM_LDLIBS="$("${PKG_CONFIG}" --silence-errors --libs libsodium || :)" fi -POSTGRES_INCLUDE="" -POSTGRES_LDLIBS="" if command -v "${PG_CONFIG}" >/dev/null; then - POSTGRES_INCLUDE="-I$("${PG_CONFIG}" --includedir)" - POSTGRES_LDLIBS="-L$("${PG_CONFIG}" --libdir) -lpq" + : ${POSTGRES_INCLUDE="-I$("${PG_CONFIG}" --includedir)"} + : ${POSTGRES_LDLIBS="-L$("${PG_CONFIG}" --libdir) -lpq"} +else + # No pg_config, but the environment may still say how to reach libpq -- + # which is how a static or cross build states it (the comment above). + # Assigning here unconditionally would throw that answer away and + # silently drop PostgreSQL support, so default rather than assign. + : ${POSTGRES_INCLUDE=} + : ${POSTGRES_LDLIBS=} fi # Clean up on exit. @@ -455,7 +473,7 @@ CONFIGURATOR_PATH_FLAGS= [ -z "$CPATH" ] || CONFIGURATOR_PATH_FLAGS="$CONFIGURATOR_PATH_FLAGS -I$CPATH" [ -z "$LIBRARY_PATH" ] || CONFIGURATOR_PATH_FLAGS="$CONFIGURATOR_PATH_FLAGS -L$LIBRARY_PATH" -$CONFIGURATOR --extra-tests --autotools-style --var-file=$CONFIG_VAR_FILE.$$ --header-file=$CONFIG_HEADER.$$ --configurator-cc="$CONFIGURATOR_CC" --wrapper="$CONFIGURATOR_WRAPPER" "$CC" ${CWARNFLAGS-$BASE_WARNFLAGS} $CDEBUGFLAGS $COPTFLAGS $CSANFLAGS $CONFIGURATOR_PATH_FLAGS $SQLITE3_CFLAGS $SODIUM_CFLAGS $POSTGRES_INCLUDE < Date: Wed, 21 Oct 2026 18:00:39 +0200 Subject: [PATCH 02/30] configure: give the function-sections probe a main() have_function_sections() links its test program with -o, so without a main() the link dies on "undefined reference to `main'" -- on every platform, not just a fully static one against musl's crt1.o. The probe therefore answered "no" however capable the toolchain was, and HAVE_FUNCTION_SECTIONS has been 0 everywhere: -ffunction-sections and -Wl,--gc-sections were silently off for every build this project shipped. Adding the main() turns them on wherever the toolchain supports them, which is a change to native builds too: smaller binaries through section gc, and nothing else -- the flags do not alter semantics, and warnings are unaffected. --- configure | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/configure b/configure index a84cbd425e5a..b5fb2db4f7ba 100755 --- a/configure +++ b/configure @@ -242,7 +242,10 @@ have_function_sections() TMPCFILE=$CONFIG_VAR_FILE.$$.c TMPOBJFILE=$CONFIG_VAR_FILE.$$.o - echo "int foo(void); int foo(void) { return 0; }" > $TMPCFILE + # Needs a main(). Without one the link fails -- "undefined reference to + # `main'" -- on every platform, not just a fully static one against musl's + # crt1.o, so the probe answered "no" however capable the toolchain was. + echo "int foo(void); int foo(void) { return 0; } int main(void) { return foo(); }" > $TMPCFILE # We *want* this to fail if we get a warning, hence use -Werror. $1 $2 -Werror -ffunction-sections -Wl,--gc-sections $TMPCFILE -o $TMPOBJFILE } From 197eed5b2f82881e103606fe57280966b3fd13ac Mon Sep 17 00:00:00 2001 From: cdecker Date: Wed, 21 Oct 2026 18:01:07 +0200 Subject: [PATCH 03/30] configure: do not silently drop PostgreSQL on --reconfigure The configurator's loop sets every variable config.vars mentions, so afterwards a stored empty value is indistinguishable from an environment setting, and `: ${POSTGRES_LDLIBS=...}` skipped the pg_config probe. A --reconfigure whose stored value was empty therefore built a lightningd with no PostgreSQL support at all, without a word about it. Whether the value came from the environment is now recorded before config.vars is read; anything else is probed again, as a first run would. --- configure | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/configure b/configure index b5fb2db4f7ba..812b80d67dda 100755 --- a/configure +++ b/configure @@ -40,6 +40,15 @@ fi # override what 'pg_config' reports, e.g. to link libpq statically where # libpq.a needs extra archives: POSTGRES_LDLIBS="-lpq -lpgcommon -lpgport # -lssl -lcrypto". Leave unset for the automagic pg_config detection. +# +# Recorded here, before --reconfigure loads config.vars: that loop sets any +# variable config.vars mentions, so afterwards a stored *empty* value looks +# exactly like an environment setting, and the `: ${VAR=...}` defaults below +# would skip the pg_config probe and silently drop PostgreSQL support. Only +# the environment speaks for the caller; a value that merely came back from +# config.vars gets probed again. +POSTGRES_LDLIBS_FROM_ENV=${POSTGRES_LDLIBS+y} +POSTGRES_INCLUDE_FROM_ENV=${POSTGRES_INCLUDE+y} usage_with_default() { @@ -457,6 +466,10 @@ if command -v "${PKG_CONFIG}" >/dev/null; then SODIUM_LDLIBS="$("${PKG_CONFIG}" --silence-errors --libs libsodium || :)" fi +# Anything that did not come from the environment is re-probed, exactly as a +# first run would: see POSTGRES_*_FROM_ENV above. +[ -n "$POSTGRES_LDLIBS_FROM_ENV" ] || unset POSTGRES_LDLIBS +[ -n "$POSTGRES_INCLUDE_FROM_ENV" ] || unset POSTGRES_INCLUDE if command -v "${PG_CONFIG}" >/dev/null; then : ${POSTGRES_INCLUDE="-I$("${PG_CONFIG}" --includedir)"} : ${POSTGRES_LDLIBS="-L$("${PG_CONFIG}" --libdir) -lpq"} From da18b8ef12997dcd1064ba452881148f7b28ad95 Mon Sep 17 00:00:00 2001 From: cdecker Date: Wed, 21 Oct 2026 18:01:58 +0200 Subject: [PATCH 04/30] build: keep build-machine programs off the host toolchain The build runs some of its own output, which a cross build cannot do: - cdump-enumstr is compiled with CC_FOR_BUILD and taken out of ALL_PROGRAMS: it is run to generate source, so it must be a build-machine program, while libccan.a is host code. - tools/headerversions and plugins/sql --print-docs run through CONFIGURATOR_WRAPPER. The sql one had its failure masked by a pipe to jq, which shipped a lightning-sql.7 with no TABLES section. - external/Makefile passes BUILD/MAKE_HOST so libwally and libbacktrace configure as cross rather than native, and libbacktrace's generated backtrace-supported.h is declared as a dependency of the objects -- a -j race that only showed up where gcc did not happen to provide its own copy. - cln-rpc honours RUST_TARGET_DIR so the Rust artifacts land per target. That last dependency is an *order-only* prerequisite, and the header is deliberately not a member of EXTERNAL_HEADERS, which is subtler than it looks. EXTERNAL_HEADERS depends on the phony submodcheck, so every member of it is permanently out of date; that is harmless for headers we merely check out, because a target with no recipe and no missing file is not propagated as updated. A generated header behaves differently at both ends of that rule: as a member with no recipe it is a nonexistent file, which make imagines to have been updated and so invalidates every object; as a member with a recipe it re-runs libbacktrace's configure on every single make. Order-only says what is actually meant -- it must exist before anything compiles, and its timestamp is not a reason to recompile. A grouped target then says the archive and the header come from one recipe, and the header joins TESTPACK_EXTRAS for the same reason $(EXTERNAL_LIBS) already had to: the testpack prunes external/, so otherwise it never reaches the phase that only checks, and make would build it there with a compiler stubbed out to refuse. --- Makefile | 25 ++++++++++++++++--------- doc/Makefile | 2 +- external/Makefile | 23 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 96495adc1a1f..0dfe5dbd9031 100644 --- a/Makefile +++ b/Makefile @@ -784,11 +784,17 @@ TAGS: tags: $(RM) tags; find * -name test -type d -prune -o \( -name '*.[ch]' -o -name '*.py' \) -print0 | xargs -0 ctags --append -ccan/ccan/cdump/tools/cdump-enumstr: ccan/ccan/cdump/tools/cdump-enumstr.o libccan.a - -ALL_PROGRAMS += ccan/ccan/cdump/tools/cdump-enumstr -# Can't add to ALL_OBJS, as that makes a circular dep. -ccan/ccan/cdump/tools/cdump-enumstr.o: $(CCAN_HEADERS) Makefile +# cdump-enumstr is *run* during the build (it generates the *_names_gen.h +# headers), so it is built for the build machine with CC_FOR_BUILD, which only +# differs from CC when cross-compiling: make CC=aarch64-linux-gnu-gcc CC_FOR_BUILD=cc +# It is linked from its own sources rather than libccan.a, which is target code. +CC_FOR_BUILD ?= $(CC) +CDUMP_ENUMSTR_SRCS := ccan/ccan/cdump/tools/cdump-enumstr.c \ + $(patsubst %,$(CCANDIR)/ccan/%.c,cdump/cdump tal/tal tal/str/str tal/grab_file/grab_file take/take list/list read_write_all/read_write_all strmap/strmap noerr/noerr err/err) +ccan/ccan/cdump/tools/cdump-enumstr: $(CDUMP_ENUMSTR_SRCS) $(CCAN_HEADERS) ccan/config.h Makefile + @$(call VERBOSE, "cc-for-build $@", $(CC_FOR_BUILD) -Wall -I $(CCANDIR) -I . -o $@ $(CDUMP_ENUMSTR_SRCS)) + +# Not in ALL_PROGRAMS: that would hand it the generic (target) link rule. # Without a working git, you can't generate this file, so assume if it exists # it is ok (fixes "sudo make install"). @@ -804,7 +810,7 @@ endif # That forces this rule to be run every time, too. header_versions_gen.h: tools/headerversions $(FORCE) - @tools/headerversions $@ + @$(CONFIGURATOR_WRAPPER) tools/headerversions $@ # Once you have libccan.a, you don't need these. .INTERMEDIATE: $(CCAN_OBJS) @@ -856,7 +862,7 @@ endif $(CCAN_OBJS) $(CDUMP_OBJS): $(CCAN_HEADERS) Makefile ccan_compat.h # Except for CCAN, we treat everything else as dependent on external/ bitcoin/ common/ wire/ and all generated headers, and Makefile -$(ALL_OBJS): $(BITCOIN_HEADERS) $(COMMON_HEADERS) $(CCAN_HEADERS) $(WIRE_HEADERS) $(ALL_GEN_HEADERS) $(EXTERNAL_HEADERS) Makefile +$(ALL_OBJS): $(BITCOIN_HEADERS) $(COMMON_HEADERS) $(CCAN_HEADERS) $(WIRE_HEADERS) $(ALL_GEN_HEADERS) $(EXTERNAL_HEADERS) Makefile | $(EXTERNAL_GEN_HEADERS) # Test files can literally #include generated C files. $(ALL_TEST_PROGRAMS:=.o): $(ALL_GEN_SOURCES) @@ -900,7 +906,7 @@ clean: obsclean $(RM) $(ALL_FUZZ_TARGETS) $(RM) $(MSGGEN_GEN_ALL) $(RM) ccan/tools/configurator/configurator - $(RM) ccan/ccan/cdump/tools/cdump-enumstr.o + $(RM) ccan/ccan/cdump/tools/cdump-enumstr ccan/ccan/cdump/tools/cdump-enumstr.o find . -name '*gcda' -delete find . -name '*gcno' -delete find . -name '*.nccout' -delete @@ -1038,7 +1044,8 @@ TESTPACK_EXTRAS := \ config.vars ccan/config.h \ header_versions_gen.h \ $(DEFAULT_TARGETS) \ - $(EXTERNAL_LIBS) + $(EXTERNAL_LIBS) \ + $(EXTERNAL_GEN_HEADERS) # The testpack is used in CI to transfer built artefacts between the # build and the test phase. Only useful on a freshly build tree! diff --git a/doc/Makefile b/doc/Makefile index 2775eb1c9c49..a7c2a1dbc5ed 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -205,7 +205,7 @@ OLD_MARKDOWNPAGES_WITH_EXT := $(PREFIXED_MANPAGES:=.md) # - Generate the tables schema via plugins/sql # - Merge both and generate final schemas/sql.json doc/schemas/sql.json: doc/schemas/sql-template.json plugins/sql - @plugins/sql --print-docs | jq --arg sqldata "$$(awk '{printf "%s\n", $$0}')" '.tables += [$$sqldata]' $< > "$@.tmp" && mv "$@.tmp" "$@"; + @$(CONFIGURATOR_WRAPPER) plugins/sql --print-docs | jq --arg sqldata "$$(awk '{printf "%s\n", $$0}')" '.tables += [$$sqldata]' $< > "$@.tmp" && mv "$@.tmp" "$@"; doc-all: $(MANPAGES) doc/index.rst diff --git a/external/Makefile b/external/Makefile index 4ff220539eb5..03d5e3791ae5 100644 --- a/external/Makefile +++ b/external/Makefile @@ -23,6 +23,24 @@ JSMN_HEADERS := external/jsmn/jsmn.h GHEAP_HEADERS := external/gheap/gheap.h LIBBACKTRACE_HEADERS := external/libbacktrace/backtrace.h +# backtrace-supported.h is generated by libbacktrace's configure into the build +# dir; objects that include it must wait for that build, otherwise they race it +# (and, where the compiler ships its own copy of the header, silently pick that +# one up instead -- as gcc's libgcc-dev does). +# +# Deliberately *not* in EXTERNAL_HEADERS, and an order-only prerequisite of the +# objects. EXTERNAL_HEADERS depends on the phony submodcheck, which is always +# newer, so every member of it is permanently out of date -- harmless for +# checked-out headers that have no recipe, but a header we *generate* would then +# re-run libbacktrace's configure on every single make. Order-only says what we +# actually mean: it has to exist before anything compiles, and its timestamp is +# not a reason to recompile. +# +# Like $(EXTERNAL_LIBS), and for the same reason, it also has to be named in +# TESTPACK_EXTRAS: the testpack prunes external/, so otherwise it does not reach +# the phase that only checks. +EXTERNAL_GEN_HEADERS := $(TARGET_DIR)/libbacktrace-build/backtrace-supported.h + EXTERNAL_HEADERS := $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS) $(JSMN_HEADERS) $(GHEAP_HEADERS) $(LIBBACKTRACE_HEADERS) EXTERNAL_LIBS := ${TARGET_DIR}/libwallycore.a ${TARGET_DIR}/libsecp256k1.a ${TARGET_DIR}/libjsmn.a ${TARGET_DIR}/libbacktrace.a @@ -92,7 +110,10 @@ $(TARGET_DIR)/libjsmn.a: $(TARGET_DIR)/jsmn-build/jsmn.o .INTERMEDIATE: $(TARGET_DIR)/jsmn-build/jsmn.o # Need separate build dir: changes inside submodule make git think it's dirty. -$(TARGET_DIR)/libbacktrace.a: external/libbacktrace/backtrace.h +# One recipe produces the archive and the generated header, so say so with a +# grouped target: a plain recipe-less rule would let make treat the missing +# header as freshly made, and silently rebuild every object instead. +$(TARGET_DIR)/libbacktrace.a $(EXTERNAL_GEN_HEADERS) &: external/libbacktrace/backtrace.h mkdir -p $(TARGET_DIR)/libbacktrace-build cd $(TARGET_DIR)/libbacktrace-build && $(TOP)/libbacktrace/configure CC="$(CC)" --enable-static=yes $(CROSSCOMPILE_OPTS) --enable-shared=no --prefix=/ --libdir=/ && $(MAKE) $(MAKE) -C $(TARGET_DIR)/libbacktrace-build DESTDIR=$$(pwd)/$(TARGET_DIR) install-exec From cf76ba31912da49ae97eb321bb9af1ff300ab65e Mon Sep 17 00:00:00 2001 From: cdecker Date: Wed, 21 Oct 2026 18:01:58 +0200 Subject: [PATCH 05/30] nix: build the release as a static/cross variant of the in-tree derivation The release artifacts come from the same derivation as packages.default, evaluated under pkgsStatic (and pkgsCross for the arm rows), rather than from a second release-only expression: the thing tested hardest each release is then the thing Nix users install. release = true switches on what the reprobuild contract needs and nothing the ordinary build wants: version and mtime from the driver, one `make install` (install's own prerequisite graph already covers everything shipped), an offline cargo vendor dir, --prefix=/usr into a DESTDIR, strip --strip-all with an in-derivation assertion that no shipped ELF keeps .symtab/.debug_* or loses its build-id, and dontPatchELF so fixupPhase cannot rewrite signed bytes. static-pie.nix carries the link recipe, including two gaps nixpkgs does not cover: rustc still links crt-static musl targets non-PIE and self-contained, so every row rewrites its link line; and gcc cannot static-pie on armv7 at all, so that row links through an explicit-crt wrapper. Both are needed to keep ASLR on every shipped executable. Three changes stop the release bytes tracking the source tree: a constant unpacked source directory, a remap for the cargo vendor dir, and a pinned NIX_OUTPATH_USED_AS_RANDOM_SEED -- without the last one, nixpkgs seeds every C compile from $out and --build-id=sha1 hashes the unstripped output, so a tracked file that is never compiled moved every build-id. --- flake.nix | 25 +- nix/pkgs/default.nix | 498 ++++++++++++++++++++++++++++++++++++---- nix/pkgs/static-pie.nix | 122 ++++++++++ 3 files changed, 602 insertions(+), 43 deletions(-) create mode 100644 nix/pkgs/static-pie.nix diff --git a/flake.nix b/flake.nix index 9cd4aad229b5..25e1bbf7469b 100644 --- a/flake.nix +++ b/flake.nix @@ -49,7 +49,30 @@ { _module.args.pkgs = import inputs.nixpkgs { inherit system; - overlays = [ (final: prev: { craneLib = (inputs.crane.mkLib pkgs); }) ]; + overlays = [ + (final: prev: { craneLib = (inputs.crane.mkLib pkgs); }) + # crates.io now 403s any + # User-Agent starting with "curl/", which is exactly what + # nixpkgs' fetchurl sends + # (`curl/$curlVersion Nixpkgs/$nixpkgsVersion`), so every + # crate tarball that is not already in cache.nixos.org fails + # to fetch. Send a UA it accepts. fetchurl outputs are + # fixed-output, so this changes no store path -- only whether + # the fetch succeeds. + (final: prev: { + fetchurl = + args: + prev.fetchurl ( + args + // { + curlOptsList = (args.curlOptsList or [ ]) ++ [ + "--user-agent" + "Nixpkgs fetchurl (Core Lightning reprobuild)" + ]; + } + ); + }) + ]; }; }; }; diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix index 8820741b9432..51ab0dd457c4 100644 --- a/nix/pkgs/default.nix +++ b/nix/pkgs/default.nix @@ -4,59 +4,221 @@ pkgs, config, postgresSupport ? false, + # Release mode. + # + # The release artifacts are built from *this* derivation, as a + # static/cross variant, not from a second release-only one (map Notes, + # The release artifacts are built from *this* derivation, as a + # static/cross variant, not from a second release-only one. + # `release = true` switches on everything the reprobuild + # contract needs and nothing the ordinary `nix build .#cln` wants: + # + # * version/mtime come from the driver (REPRO_VERSION / REPRO_MTIME), + # not from `.version` and `self.lastModifiedDate` -- bit-identity + # across two runs of different checkouts depends on it; + # * one `make install` builds the Rust plugins itself, + # so the crane side-build and NO_PYTHON=1 are off and cargo is fed + # an offline vendor dir instead of the network; + # * -static-pie on the compiler driver, with PIE_LDFLAGS overridden + # (the Makefile's trailing -pie turns -static-pie into + # a *dynamic* link); + # * strip --strip-all on every shipped ELF. + release ? false, + releaseVersion ? null, + releaseMtime ? null, + # The row's arch token from nix/release-rows (amd64, arm64, + # armhf). It only names the artifact; which package set the + # row builds under is decided by the caller. + releaseArch ? null, }: with pkgs; let - version = builtins.readFile ../../.version; - py3 = python3.withPackages (p: [ + version = if release then releaseVersion else builtins.readFile ../../.version; + mtime = if release then releaseMtime else self.lastModifiedDate; + # Build-machine tools. `with pkgs;` bypasses callPackage's splicing, so + # under pkgsStatic a bare `cargo`/`python3` is the *static* build of that + # tool (cargo's is even marked broken) rather than the native one that + # targets musl. For a native package set buildPackages == pkgs, so this + # is a no-op off the release path. + bp = pkgs.buildPackages; + py3 = bp.python3.withPackages (p: [ p.grpcio-tools p.mako ]); -in -stdenv.mkDerivation { - name = "cln"; - src = ../../.; - inherit version; - makeFlags = [ - "VERSION=${version}" - "MTIME=${self.lastModifiedDate}" - "NO_PYTHON=1" - ]; + # Offline cargo vendor dir. The reprobuild contract allowed + # Lockfile-hashed fetches are allowed at build time; a Nix sandbox has + # no network, so the same guarantee is expressed as a vendor derivation + # keyed on Cargo.lock. Same input, same pinning, earlier. + cargoVendor = buildPackages.rustPlatform.importCargoLock { + lockFile = ../../Cargo.lock; + }; + rustTarget = stdenv.hostPlatform.rust.rustcTarget; + rustTargetEnv = lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] rustTarget); + # The cross rows. + # + # `canExecute` is nixpkgs' name for a fact this build had to discover the + # hard way: the build machine cannot run what a cross row produces, and + # CLN's build *does* run its own output in five places (the configurator's + # test programs, tools/headerversions, `plugins/sql --print-docs`, ...). + # On the host those had been running silently under the kernel's binfmt + # registration -- which is `F`-flagged here, so it reaches inside the Nix + # sandbox too. A release build must not depend on how the captain's + # kernel happens to be configured, so the emulator comes from *this + # closure*: `hostPlatform.emulator buildPackages` is the qemu-user binary + # nixpkgs would use, pinned by the same revision as everything else. + crossRow = !(stdenv.buildPlatform.canExecute stdenv.hostPlatform); + emulator = stdenv.hostPlatform.emulator bp; + # The static-pie link recipe lives in static-pie.nix, so the VLS + # to `static-pie.nix` so the VLS derivation links exactly as this one does. + # `arm32`, `staticPieFlags`, `staticPieCc`, `releaseCc` and + # derivation links exactly as this one does; that file carries their + # reasoning. + inherit (import ./static-pie.nix { inherit pkgs lib; }) + arm32 + staticPieFlags + staticPieCc + releaseCc + rustStaticPieCc + noRpathEnv + ; + # The row's libpq, bound once. On arm32, postgres cannot even + # *configure*: its "whether the C compiler works" test links an + # executable, and nixpkgs' `pie` hardening plus pkgsStatic's `-static` + # make gcc pick the non-PIE crtbeginT.o for a PIE link -- "relocation + # R_ARM_MOVW_ABS_NC against a local symbol can not be used when making a + # shared object". Isolated to the hardening flag on a one-line program: + # `pie` on fails, `pie` off links, and a command-line `-no-pie` does not + # rescue it. Only the library's own build is affected; our binaries still + # link static-pie, via `staticPieCc` above. + # + # Bound here rather than inline in buildInputs because `POSTGRES_LDLIBS` + # below needs the *same* derivation: referring to the un-overridden + # `libpq` there pulled both into the closure, and the build then died on + # the one this override exists to avoid. + libpqRow = + if arm32 then + libpq.overrideAttrs (o: { + hardeningDisable = (o.hardeningDisable or [ ]) ++ [ "pie" ]; + }) + else + libpq; # when building on darwin we need cctools to provide the correct libtool # as libwally-core detects the host as darwin and tries to add the -static # option to libtool, also we have to add the modified gsed package. - nativeBuildInputs = [ - autoconf - autogen - automake - gettext - gitMinimal - libtool - # lowdown calls sandbox_init(3) on darwin, which fails inside a nix build. - lowdown-unsandboxed - pkgconf - py3 - unzip - which - ] - ++ lib.optionals postgresSupport [ - libpq - libpq.pg_config - ] - ++ lib.optionals stdenv.isDarwin [ - cctools - darwin.autoSignDarwinBinariesHook - ]; + nativeInputs = + with bp; + [ + autoconf + autogen + automake + gettext + gitMinimal + libtool + # lowdown calls sandbox_init(3) on darwin, which fails inside a nix build. + lowdown-unsandboxed + pkgconf + py3 + unzip + which + ] + ++ lib.optionals (postgresSupport && !release) [ + pkgs.libpq + pkgs.libpq.pg_config + ] + ++ lib.optionals release [ + cargo + rustc + protobuf + file + # configure needs a jq it can *run* ("*** We need jq!"); the one in + # buildInputs is the musl-static target build. The Alpine builder + # same requirement for the Alpine builder. + jq + # A build-platform C compiler reachable as plain `cc`. cargo compiles + # build scripts and proc macros *for the build machine* and asks rustc + # for its default linker, which is `cc`; the cross stdenv only puts + # `x86_64-unknown-linux-musl-gcc` on PATH, so every build script fails + # with "linker `cc` not found". Same class as CC_FOR_BUILD below. + stdenv.cc + ] + ++ lib.optionals stdenv.isDarwin [ + cctools + darwin.autoSignDarwinBinariesHook + ]; - buildInputs = [ + hostInputs = [ gmp jq libsodium sqlite zlib + ] + ++ lib.optionals (postgresSupport && release) [ + libpqRow + # libpq.a is linked, not loaded, so *its* dependencies become ours: + # nixpkgs records them in libpq's own `LIBS` as + # `-lpgcommon -lpgport -lssl -lcrypto -lz -lm`. zlib is already here; + # OpenSSL is not, and libpq propagates only its `-dev` output (whose + # `lib/` holds nothing but pkgconfig), so without this the link path + # has no `libssl.a`/`libcrypto.a` at all. + openssl ]; + # Everything make is told on the release path except version and mtime, + # which are per-release labels; both are listed in the input manifest. + releaseMakeFlags = + lib.optionals release [ + "RUST_PROFILE=release" + "PIE_LDFLAGS=-static-pie" + "TARGET=${rustTarget}" + # $(AR) has no assignment in the Makefile, so it would fall back to + # make's built-in `ar` -- the *build* machine's, which cannot index a + # foreign archive. Stated rather than left to the stdenv's exported AR. + "AR=${stdenv.cc.targetPrefix}ar" + ] + ++ lib.optionals (release && crossRow) [ + # external/Makefile switches to --host/--build only when BUILD is set; + # without it libwally/libbacktrace configure as if native and their own + # run-tests decide wrong. CC_FOR_BUILD is there because + # cdump-enumstr is a build-machine program (it is *run* to generate + # source), so it must not be cross-compiled. + "BUILD=${stdenv.buildPlatform.config}" + "MAKE_HOST=${stdenv.hostPlatform.config}" + "CC_FOR_BUILD=cc" + ]; + + configureFlags = [ "--disable-valgrind" ] ++ lib.optionals release [ "--prefix=/usr" ]; + + # Environment the release build needs set; also listed in the input + # manifest, since each of these moves release bytes. + releaseEnv = { + NIX_OUTPATH_USED_AS_RANDOM_SEED = "cln-release"; + } + // noRpathEnv; + # Stated once so the input manifest can list it: these reach every Rust + # artifact's bytes, and a reviewer who does not read Nix should see a diff + # when they change. The vendor path itself is deliberately *not* + # in the manifest line -- remapping is what stops it mattering -- but its + # presence is. + vendorRemap = "--remap-path-prefix=${cargoVendor}=/home/clightning/vendor"; + rustflagsShape = "--remap-path-prefix=$NIX_BUILD_TOP=/home/clightning --remap-path-prefix==/home/clightning/vendor -C link-arg=-Wl,--build-id=sha1"; + +in +stdenv.mkDerivation { + name = "cln"; + src = ../../.; + inherit version; + makeFlags = [ + "VERSION=${version}" + "MTIME=${mtime}" + ] + ++ lib.optionals (!release) [ "NO_PYTHON=1" ] + ++ releaseMakeFlags; + + nativeBuildInputs = nativeInputs; + buildInputs = hostInputs; + # this causes some python trouble on a darwin host so we skip this step. # also we have to tell libwally-core to use sed instead of gsed. postPatch = @@ -76,22 +238,274 @@ stdenv.mkDerivation { substituteInPlace external/libwally-core/configure.ac --replace gsed sed ''; - configureFlags = [ "--disable-valgrind" ]; + # The unpacked source directory is named after the *source + # store path* (`-source`), so its name changes with any tree + # change, shipped or not. It reaches the shipped bytes through rustc panic + # locations, DWARF `comp_dir` and -- once a -ffile-prefix-map names it -- + # `CCAN_CFLAGS` in the generated `ccan/config.h`; the latter two move the + # link-time `--build-id=sha1` and are then stripped away, so a build-id is + # all that is left to see. A constant name removes all of them, and the + # $NIX_BUILD_TOP map below then yields a constant `/home/clightning/source`. + ${if release then "postUnpack" else null} = '' + mv -- "$sourceRoot" source + export sourceRoot=source + ''; + + inherit configureFlags; + + # CLN's ./configure is hand-rolled, not autotools: it rejects the flags + # nixpkgs' configurePhase adds for a static cross build + # (`--enable-static --disable-shared`, `--build=`, `--host=`) with + # "Unknown option". Suppress them and hand it the one prefix it wants; + # `prefix` (not a configureFlags entry) so nixpkgs emits it exactly once. + # The tarball must unpack over `/`, so the release installs to + # DESTDIR=$out with --prefix=/usr, as the Alpine hook did. + # `prefix` is not the knob to use here: nixpkgs feeds it to configure + # *and* runs `mkdir -p "$prefix"` in installPhase, so prefix=/usr fails + # with "mkdir: cannot create directory '/usr': Permission denied". + # Suppress nixpkgs' own --prefix and pass ours in configureFlags; $out + # stays the prefix nixpkgs creates, and DESTDIR=$out + --prefix=/usr + # reproduces the Alpine tarball layout (unpacks over `/`). + dontAddPrefix = release; + dontAddStaticConfigureFlags = release; + configurePlatforms = [ ]; + + preConfigure = + lib.optionalString release '' + export PYTEST= + export SOURCE_DATE_EPOCH LANG=C LC_ALL=C TZ=UTC + + # cargo, offline, against the vendored crates. + export CARGO_HOME=$NIX_BUILD_TOP/cargo-home + mkdir -p $CARGO_HOME + cat > $CARGO_HOME/config.toml <