about summary refs log tree commit diff
path: root/pkgs/tools/security/onlykey/default.nix
blob: c63173d889b2e4ea9f7984d211c06bd617ea37f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ lib
, node_webkit
, pkgs
, copyDesktopItems
, makeDesktopItem
, stdenv
, writeShellScript
, wrapGAppsHook
}:

let
  # parse the version from package.json
  version =
    let
      packageJson = lib.importJSON ./package.json;
      splits = builtins.split "^.*#v(.*)$" (builtins.getAttr "onlykey" (builtins.head packageJson));
      matches = builtins.elemAt splits 1;
      elem = builtins.head matches;
    in
    elem;

  # this must be updated anytime this package is updated.
  onlykeyPkg = "onlykey-git+https://github.com/trustcrypto/OnlyKey-App.git#v${version}";

  # define a shortcut to get to onlykey.
  onlykey = self."${onlykeyPkg}";

  super = import ./onlykey.nix {
    inherit pkgs;
    inherit (stdenv.hostPlatform) system;
  };

  self = super // {
    "${onlykeyPkg}" = super."${onlykeyPkg}".override (attrs: {
      # when installing packages, nw tries to download nwjs in its postInstall
      # script. There are currently no other postInstall scripts, so this
      # should not break other things.
      npmFlags = attrs.npmFlags or "" + " --ignore-scripts";

      # this package requires to be built in order to become runnable.
      postInstall = ''
        cd $out/lib/node_modules/${attrs.packageName}
        npm run build
      '';
    });
  };

  script = writeShellScript "${onlykey.packageName}-starter-${onlykey.version}" ''
    ${node_webkit}/bin/nw ${onlykey}/lib/node_modules/${onlykey.packageName}/build
  '';
in
stdenv.mkDerivation {
  pname = "${onlykey.packageName}";
  inherit (onlykey) version;
  dontUnpack = true;
  nativeBuildInputs = [ wrapGAppsHook copyDesktopItems ];
  desktopItems = [
    (makeDesktopItem {
      name = onlykey.packageName;
      exec = script;
      icon = "${onlykey}/lib/node_modules/${onlykey.packageName}/resources/onlykey_logo_128.png";
      desktopName = onlykey.packageName;
      genericName = onlykey.packageName;
    })
  ];
  installPhase = ''
    runHook preInstall

    mkdir -p $out/bin
    ln -s ${script} $out/bin/onlykey

    runHook postInstall
  '';
}