NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.04k stars 14.04k forks source link

Mask `hostname`, `whoami` and `date` in builds #22849

Open danbst opened 7 years ago

danbst commented 7 years ago

Issue description

I've tried to package a tcptrace tool. It was very easy, but during building it injects current hostname, build user and current datetime into executable, thus rendering it non-deterministic.

What is the correct way to handle this? Should I patch inplace or hostname, whoami and date should be replaced with smth immutable in default stdenv?

Steps to reproduce

{ stdenv, fetchurl, libpcap }:

stdenv.mkDerivation rec {
  name = "tcptrace-${version}";
  version = "6.6.7";
  src = fetchurl {
    url = "http://www.tcptrace.org/download/${name}.tar.gz";
    sha256 = null; #nevermind :)
  };
  buildInputs = [ libpcap ];
  patchPhase = ''
    sed -i \
      -e 's/BUILT_USER/"nix"/' \
      -e 's/BUILT_DATE/"start of time"/' \
      -e 's/BUILT_HOST/"everywhere"/' \
     version.c
  '';
  installPhase = ''
    mkdir -p $out/bin $out/man/man1
    install tcptrace $out/bin/tcptrace
    install tcptrace.man $out/man/man1/tcptrace.1
  '';
}

Relevant part of makefile:

    192 # special rule for version.c
    193 # needs to be recompiled EVERY time
    194 #
    195 # If you have problems getting "whoami", "hostname", or "date" to run on
    196 # your machine, just hack in a quick string below in place of the command.
    197 version.o: ${CFILES} Makefile
    198         ${CC} ${CFLAGS} -o version.o -c $(srcdir)/version.c \
    199         -DBUILT_USER="\"`whoami`\"" -DBUILT_HOST="\"`hostname`\"" -DBUILT_DATE="\"`date`\""
grahamc commented 7 years ago

You might check out Debian to see if they have patches fixing this indeterminate issue

grahamc commented 7 years ago

https://sources.debian.net/src/tcptrace/6.6.7-5/debian/patches/bug829714-reproducible_builds/

abbradar commented 7 years ago

We might want to use CLONE_NEWUTS in Nix instead and set some bogus hostname. whoami is already handled (not sure though but I think the user is always root or nixbld1 now). That leaves only date.

abbradar commented 7 years ago

Looking through Nix's source code we already use CLONE_NEWUTS -- nice!

bjornfor commented 7 years ago

Perhaps use faketime (from libfaketime package)?

tilpner commented 5 years ago

Could we have a setup hook that sets LD_PRELOAD or DYLD_INSERT_LIBRARIES (depending on platform) to use libfaketime (with SOURCE_DATE_EPOCH) everywhere? It seems more robust than hunting and patching every last place that sets dates.

stale[bot] commented 4 years ago

Thank you for your contributions.

This has been automatically marked as stale because it has had no activity for 180 days.

If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse.
  3. Ask on the #nixos channel on irc.freenode.net.
davidak commented 3 years ago

The best solution would be that upstream stops doing that. It's not that useful, especially when we add arbitrary values in our build.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

rnhmjoj commented 5 months ago

The best solution IMHO is to remove the nondeterminism at the source with something like hermit, so no matter what upstream does, the result is going to be reproducible.