Unverified Commit 5ab11bb2 authored by Marcin Serwin's avatar Marcin Serwin
Browse files

signal-desktop-source: build sqlcipher with extension from source

parent bc0f86ab
Loading
Loading
Loading
Loading
+7 −9
Original line number Diff line number Diff line
@@ -6,10 +6,10 @@
  electron_33,
  python3,
  makeWrapper,
  callPackage,
  libpulseaudio,
  fetchFromGitHub,
  runCommand,
  fetchurl,
  fetchzip,
  autoPatchelfHook,
  makeDesktopItem,
@@ -28,11 +28,7 @@ let
    tar -C $out --strip-components=1 -xvf ${electron.headers}
  '';

  sqlcipher = fetchurl {
    # https://github.com/signalapp/Signal-Sqlcipher-Extension
    url = "https://build-artifacts.signal.org/desktop/sqlcipher-v2-4.6.1-signal-patch2--0.2.1-asm2-6253f886c40e49bf892d5cdc92b2eb200b12cd8d80c48ce5b05967cfd01ee8c7.tar.gz";
    hash = "sha256-YlP4hsQOSb+JLVzckrLrIAsSzY2AxIzlsFlnz9Ae6Mc=";
  };
  sqlcipher-signal-extension = callPackage ./sqlcipher-signal-extension.nix { };

  ringrtc = stdenv.mkDerivation (finalAttrs: {
    pname = "ringrtc-bin";
@@ -123,8 +119,7 @@ stdenv.mkDerivation (finalAttrs: {
  };

  preBuild = ''
    substituteInPlace node_modules/@signalapp/better-sqlite3/deps/download.js \
      --replace-fail "path.join(__dirname, 'sqlcipher.tar.gz')" "'${sqlcipher}'"
    cp ${sqlcipher-signal-extension}/share/sqlite3.gyp node_modules/@signalapp/better-sqlite3/deps/sqlite3.gyp

    cp -r ${ringrtc} node_modules/@signalapp/ringrtc/build
  '';
@@ -187,6 +182,10 @@ stdenv.mkDerivation (finalAttrs: {
    })
  ];

  passthru = {
    inherit sqlcipher-signal-extension;
  };

  meta = {
    description = "Private, simple, and secure messenger (nixpkgs build)";
    longDescription = ''
@@ -215,7 +214,6 @@ stdenv.mkDerivation (finalAttrs: {
    sourceProvenance = with lib.sourceTypes; [
      fromSource

      # sqlcipher
      # ringrtc
      # node_modules/@signalapp/libsignal-client/prebuilds/
      binaryNativeCode
+105 −0
Original line number Diff line number Diff line
{
  rustPlatform,
  lib,
  fetchFromGitHub,
  sqlcipher,
  fetchpatch,
  stdenv,
  openssl,
  tcl,
  buildEnv,
  rust-cbindgen,
}:
let
  signal-sqlcipher-extension = rustPlatform.buildRustPackage (finalAttrs: {
    pname = "signal-sqlcipher-extension";
    version = "0.2.1";

    src = fetchFromGitHub {
      owner = "signalapp";
      repo = "Signal-Sqlcipher-Extension";
      tag = "v${finalAttrs.version}";
      hash = "sha256-INSkm7ZuetPASuIqezzzG/bXoEHClUb9XpxWbxLVXRc=";
    };
    useFetchCargoVendor = true;
    cargoHash = "sha256-qT4HM/FRL8qugKKNlMYM/0zgUsC6cDOa9fgd1d4VIrc=";

    meta = {
      description = "SQLite extension used by Signal Desktop";
      homepage = "https://github.com/signalapp/Signal-Sqlcipher-Extension";
      license = lib.licenses.agpl3Only;
      maintainers = with lib.maintainers; [ marcin-serwin ];
      platforms = lib.platforms.all;
    };
  });

  sqlcipher-amalgamation = stdenv.mkDerivation {
    pname = "sqlcipher-with-signal-extension";

    inherit (sqlcipher) version src meta;

    patches = [
      (fetchpatch {
        # https://github.com/sqlcipher/sqlcipher/pull/529
        name = "custom-crypto-provider.patch";
        url = "https://github.com/sqlcipher/sqlcipher/commit/0e3b20c155df8a2943b62a9f3cc0f4d3dba9e152.patch";
        hash = "sha256-OKh6qCGHBQWZyzXfyEveAs71wrNwlWLuG9jNqDeKNG4=";
      })
    ];

    nativeBuildInputs = [ tcl ];

    buildInputs = [ openssl ];

    CFLAGS = [ "-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1" ];

    makeFlags = [ "sqlite3.c" ];

    installPhase = ''
      install -Dm644 sqlite3.c $out/src/sqlite3.c
      install -Dm644 sqlite3.h $out/include/sqlite3.h
      install -Dm644 sqlite3ext.h $out/include/sqlite3ext.h
    '';
  };

  signal-tokenizer-headers = rustPlatform.buildRustPackage (finalAttrs: {
    pname = "Signal-FTS5-Extension";
    version = "0.2.1";

    src = fetchFromGitHub {
      owner = "signalapp";
      repo = "Signal-FTS5-Extension";
      tag = "v${finalAttrs.version}";
      hash = "sha256-MzgdRuRsfL3yhlVU0RAAUtAaOukMpqSSa42nRYhpmh0=";
    };
    useFetchCargoVendor = true;
    cargoHash = "sha256-0DDX3ciXk5/3MqsHzxV8s4qEhqYmrwGg7cSbrkFRZbw=";

    nativeBuildInputs = [ rust-cbindgen ];

    buildPhase = ''
      cbindgen --profile release . -o signal-tokenizer.h
    '';
    installPhase = ''
      install -Dm644 signal-tokenizer.h $out/include/signal-tokenizer.h
    '';
    doCheck = false;
  });

in
buildEnv {
  name = "sqlcipher-signal";

  paths = [
    sqlcipher-amalgamation
    signal-tokenizer-headers
    signal-sqlcipher-extension
  ];

  postBuild = ''
    install -Dm644 ${./sqlite3.gyp} $out/share/sqlite3.gyp
    substituteInPlace $out/share/sqlite3.gyp \
      --replace-fail "@extension@" "$out" \
      --replace-fail "@static_lib_ext@" "${stdenv.hostPlatform.extensions.staticLibrary}"
  '';
}
+40 −0
Original line number Diff line number Diff line
{
  'includes': ['common.gypi'],
  'targets': [
    {
      'target_name': 'locate_sqlite3',
      'type': 'none',
      'copies': [{
        'files': [
          '@extension@/src/sqlite3.c',
        ],
        'destination': '<(SHARED_INTERMEDIATE_DIR)/sqlite3',
      }],
    },
    {
      'target_name': 'sqlite3',
      'type': 'static_library',
      'dependencies': ['locate_sqlite3'],
      'sources': ['<(SHARED_INTERMEDIATE_DIR)/sqlite3/sqlite3.c'],
      'include_dirs': [
        '<(SHARED_INTERMEDIATE_DIR)/sqlite3/',
      ],
      'direct_dependent_settings': {
        'include_dirs': [
          '@extension@/include',
        ],
      },
      'cflags': ['-std=c99', '-w'],
      'xcode_settings': {
        'OTHER_CFLAGS': ['-std=c99'],
        'WARNING_CFLAGS': ['-w'],
      },
      'includes': ['defines.gypi'],
      'link_settings': {
        'libraries': [
          '@extension@/lib/libsignal_sqlcipher_extension@static_lib_ext@',
        ]
      }
    },
  ],
}