aboutsummaryrefslogtreecommitdiff
path: root/pkgs/copilot-api/default.nix
blob: a66fe1a7a4adbb578f51f00b33830917f48ef4ae (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{
  pkgs,
  lib,
  ...
}: let
  inherit (pkgs) stdenv bun makeBinaryWrapper fetchFromGitHub;

  node_modules = stdenv.mkDerivation {
    pname = "copilot-api-node_modules";
    version = "0.7.0";

    src = fetchFromGitHub {
      owner = "ericc-ch";
      repo = "copilot-api";
      tag = "v0.7.0";
      hash = "sha256-rUUqf9QalVZDN3aw9ze5Uh+y5xvH6zdSgGN6ZLDjkDQ=";
    };

    nativeBuildInputs = [bun];

    impureEnvVars = lib.fetchers.proxyImpureEnvVars;
    dontPatchShebangs = true;

    patchPhase = ''
      # Remove prepare script that tries to setup git hooks
      sed -i '/"prepare":/d' package.json
    '';

    buildPhase = ''
      runHook preBuild
      export HOME=$TMPDIR
      bun install --no-progress --frozen-lockfile
      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall
      mkdir -p $out
      cp -R ./node_modules $out
      runHook postInstall
    '';

    outputHash = "sha256-+AH8eRG3SwwU86Pa/hFSW5nWD0v1po6z7xoLKUzq3qY=";
    outputHashAlgo = "sha256";
    outputHashMode = "recursive";
  };
in
  stdenv.mkDerivation {
    pname = "copilot-api";
    version = "0.7.0";

    src = fetchFromGitHub {
      owner = "ericc-ch";
      repo = "copilot-api";
      tag = "v0.7.0";
      hash = "sha256-rUUqf9QalVZDN3aw9ze5Uh+y5xvH6zdSgGN6ZLDjkDQ=";
    };

    nativeBuildInputs = [makeBinaryWrapper];
    buildInputs = [bun pkgs.nodejs];

    configurePhase = ''
      runHook preConfigure
      # Don't copy node_modules - let bun install them fresh
      runHook postConfigure
    '';

    buildPhase = ''
      runHook preBuild
      export HOME=$TMPDIR
      # Use the cached node_modules
      cp -R ${node_modules}/node_modules .
      chmod -R +w node_modules
      # Try using bun's native build with correct entry point
      mkdir -p dist
      ${bun}/bin/bun build src/main.ts --outdir dist --target bun --format esm --sourcemap
      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall

      mkdir -p $out/lib/copilot-api
      cp -r dist node_modules package.json $out/lib/copilot-api/

      mkdir -p $out/bin
      makeWrapper ${bun}/bin/bun $out/bin/copilot-api \
        --prefix PATH : ${lib.makeBinPath [bun]} \
        --add-flags "$out/lib/copilot-api/dist/main.js"

      runHook postInstall
    '';
  }