🏡 index : quine-global/quorum.git

{
  # quorum.exe, built reproducibly from Linux (charter M4): anyone running
  # `nix build .#windows` at the same commit should get the same bytes.
  #
  # Toolchain: the official Rust release with the Windows target's standard
  # library (fenix, pinned by flake.lock), linked by zig through
  # cargo-zigbuild: no mingw GCC or Windows rustc to build from source.
  # Crates come from Cargo.lock, vendored; the build runs offline.
  description = "quorum: trust a file when enough rebuilders you chose reproduced it";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    fenix = {
      url = "github:nix-community/fenix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, fenix }:
    let
      systems = [ "x86_64-linux" "aarch64-linux" ];
      forAll = f: nixpkgs.lib.genAttrs systems (system:
        f nixpkgs.legacyPackages.${system} fenix.packages.${system});
      everywhere = nixpkgs.lib.genAttrs (systems ++ [ "aarch64-darwin" "x86_64-darwin" ]);

      # The producer side is off the shelf: cosign signs rebuilders'
      # attestations, Tessera writes the transparency log, litewitness
      # cosigns it, tuftool makes the TUF repository. Pinned here (cosign by
      # flake.lock's nixpkgs) for the tests and for whoever runs them.
      tools = pkgs: {
        # quorum's own runtime need: GnuPG checks OpenPGP feeds (guix.sigs).
        inherit (pkgs) cosign gnupg;
        posix-oneshot = pkgs.buildGoModule {
          pname = "tessera-posix-oneshot";
          version = "1.0.4";
          src = pkgs.fetchFromGitHub {
            owner = "transparency-dev";
            repo = "tessera";
            rev = "v1.0.4";
            hash = "sha256-y+uhfhKOk90taHO8Og+XFyZtcLNhS0GwCTqliWfrRao=";
          };
          vendorHash = "sha256-QSSablNUOxDpiNZSsMyOXWT09UDABkr64A41Ogp0poc=";
          subPackages = [ "cmd/examples/posix-oneshot" ];
        };
        litewitness = pkgs.buildGoModule {
          pname = "litewitness";
          version = "0.10.0";
          src = pkgs.fetchFromGitHub {
            owner = "FiloSottile";
            repo = "torchwood";
            rev = "v0.10.0";
            hash = "sha256-eSDbIOzWKRlmGXj+/LlaT22HDC77Uyz96fTTR9vWbWk=";
          };
          vendorHash = "sha256-S1/PInF5TvvuY/lcn5DPQS5NJJaiH3y1gXZZzJzWvtU=";
          subPackages = [ "cmd/litewitness" "cmd/witnessctl" ];
          # Its tests want an ssh-agent and short socket paths; ours run it instead.
          doCheck = false;
        };
        # The TUF repository's maker (awslabs/tough): create, release, rotate.
        tuftool = pkgs.rustPlatform.buildRustPackage rec {
          pname = "tuftool";
          version = "0.17.0";
          src = pkgs.fetchCrate {
            inherit pname version;
            hash = "sha256-BlC152/Vd3hRSOI2/jf7DZ0ri27lw+kNLuOJ+0OaiUA=";
          };
          cargoHash = "sha256-GUbHOCnCy3hrlDL+2NDQwoK0V4ejRXiN2iwVdyFCCto=";
          nativeBuildInputs = [ pkgs.cmake pkgs.perl ]; # aws-lc-sys
          doCheck = false; # its integration tests want AWS
        };
      };
      # Linux only: quorum.exe, reproducibly.
      windowsPackages = forAll (pkgs: fx:
        let
          target = "x86_64-pc-windows-gnu";
          toolchain = fx.combine [
            fx.stable.rustc
            fx.stable.cargo
            fx.targets.${target}.stable.rust-std
          ];
          vendor = pkgs.rustPlatform.importCargoLock { lockFile = ./Cargo.lock; };
          src = pkgs.lib.fileset.toSource {
            root = ./.;
            fileset = pkgs.lib.fileset.unions [ ./Cargo.toml ./Cargo.lock ./src ];
          };
        in
        rec {
          windows = pkgs.stdenv.mkDerivation {
            pname = "quorum-windows";
            version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
            inherit src;
            nativeBuildInputs = [ toolchain pkgs.zig_0_16 pkgs.cargo-zigbuild ];
            dontConfigure = true;
            buildPhase = ''
              export HOME=$TMPDIR CARGO_HOME=$TMPDIR/cargo XDG_CACHE_HOME=$TMPDIR/cache
              export ZIG_GLOBAL_CACHE_DIR=$TMPDIR/zig-global ZIG_LOCAL_CACHE_DIR=$TMPDIR/zig-local
              # windows-sys makes import libraries with dlltool: zig's (LLVM's).
              mkdir -p $TMPDIR/bin
              printf '#!/bin/sh\nexec zig dlltool "$@"\n' > $TMPDIR/bin/x86_64-w64-mingw32-dlltool
              chmod +x $TMPDIR/bin/x86_64-w64-mingw32-dlltool
              export PATH=$TMPDIR/bin:$PATH
              # Rust's std links Windows' synchronization library, which zig doesn't
              # ship: an import library for the DLL that has the functions std uses.
              mkdir -p $TMPDIR/lib
              printf 'LIBRARY api-ms-win-core-synch-l1-2-0.dll\nEXPORTS\nWaitOnAddress\nWakeByAddressAll\nWakeByAddressSingle\n' > $TMPDIR/synchronization.def
              zig dlltool -m i386:x86-64 -d $TMPDIR/synchronization.def -l $TMPDIR/lib/libsynchronization.a
              mkdir -p .cargo
              cat > .cargo/config.toml <<EOF
              [source.crates-io]
              replace-with = "vendored"
              [source.vendored]
              directory = "${vendor}"
              EOF
              # No build-machine paths in the binary.
              export RUSTFLAGS="-L native=$TMPDIR/lib --remap-path-prefix=$PWD=/quorum --remap-path-prefix=${vendor}=/vendor --remap-path-prefix=${toolchain}=/rust --remap-path-prefix=$TMPDIR=/tmp"
              cargo zigbuild --release --frozen --offline --target ${target}
            '';
            installPhase = ''
              install -Dm755 target/${target}/release/quorum.exe $out/bin/quorum.exe
              sha256sum $out/bin/quorum.exe | cut -d' ' -f1 > $out/quorum.exe.sha256
            '';
            dontStrip = true; # the release profile strips; Linux tools would corrupt a PE
            dontFixup = true;
          };
          default = windows;
        });
    in
    {
      devShells = everywhere (system:
        let pkgs = nixpkgs.legacyPackages.${system}; in {
          default = pkgs.mkShell {
            packages = builtins.attrValues (tools pkgs) ++ [ pkgs.openssh ];
          };
        });

      # cargo test, the transparency log tests against the real Tessera and litewitness.
      checks = everywhere (system:
        let pkgs = nixpkgs.legacyPackages.${system}; in {
          tests = pkgs.rustPlatform.buildRustPackage {
            pname = "quorum-tests";
            version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
            src = pkgs.lib.fileset.toSource {
              root = ./.;
              fileset = pkgs.lib.fileset.unions [ ./Cargo.toml ./Cargo.lock ./src ./tests ];
            };
            cargoLock.lockFile = ./Cargo.lock;
            nativeCheckInputs = builtins.attrValues (tools pkgs) ++ [ pkgs.openssh ];
            buildType = "debug";
          };
        });


      # Every system: quorum itself, the producer-side tools, and a
      # rebuilder's one command (contrib/rebuild.sh, docs/REBUILDER.md).
      packages = nixpkgs.lib.recursiveUpdate
        (everywhere (system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            quorum = pkgs.rustPlatform.buildRustPackage {
              pname = "quorum";
              version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
              src = pkgs.lib.fileset.toSource {
                root = ./.;
                fileset = pkgs.lib.fileset.unions [ ./Cargo.toml ./Cargo.lock ./src ];
              };
              cargoLock.lockFile = ./Cargo.lock;
              doCheck = false; # checks.tests
            };
          in
          tools pkgs // {
            inherit quorum;
            rebuild = pkgs.writeShellApplication {
              name = "rebuild";
              runtimeInputs = [ quorum pkgs.cosign (tools pkgs).posix-oneshot pkgs.coreutils pkgs.findutils ];
              text = builtins.readFile ./contrib/rebuild.sh;
            };
          }))
        windowsPackages;
    };
}