summaryrefslogtreecommitdiff
path: root/nix/desktop.nix
blob: de2c8660fd8f4d7d3fc10ebc0c3e4a2bf8fcca60 (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
75
76
77
78
79
80
81
82
83
84
85
86
# Electron desktop app: tsc-compiled main process + the static web export,
# wrapped with nixpkgs electron (no electron-builder, no asar).
# ffmpeg-static is satisfied by nixpkgs ffmpeg.
{
  lib,
  stdenvNoCC,
  bun,
  nodejs,
  callPackage,
  electron,
  ffmpeg,
  makeWrapper,
  sources ? import ../npins,
}:

let
  bunDeps = callPackage ./bun-deps.nix { };
  web = callPackage ./web.nix { inherit sources; };
  desktopPkg = builtins.fromJSON (builtins.readFile ../apps/desktop/package.json);
in
stdenvNoCC.mkDerivation {
  pname = "opencut-desktop";
  version = desktopPkg.version;
  src = lib.cleanSource ../.;

  nativeBuildInputs = [
    bun
    nodejs
    makeWrapper
  ];

  configurePhase = ''
    runHook preConfigure
    cp -r ${bunDeps}/node_modules node_modules
    cp -r ${bunDeps}/apps/desktop/node_modules apps/desktop/node_modules
    chmod -R u+w node_modules apps/desktop/node_modules
    patchShebangs node_modules apps/desktop/node_modules
    runHook postConfigure
  '';

  buildPhase = ''
    runHook preBuild
    export HOME=$TMPDIR
    cd apps/desktop
    bun run build
    cd $NIX_BUILD_TOP/source
    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall

    appdir=$out/share/opencut
    mkdir -p $appdir/node_modules/ffmpeg-static
    cp apps/desktop/package.json $appdir/package.json
    cp -r apps/desktop/dist $appdir/dist
    cp -r ${web} $appdir/web

    # main.js resolves ffmpeg via require("ffmpeg-static"); point it at nixpkgs ffmpeg.
    cp apps/desktop/node_modules/ffmpeg-static/index.js $appdir/node_modules/ffmpeg-static/index.js
    cp apps/desktop/node_modules/ffmpeg-static/package.json $appdir/node_modules/ffmpeg-static/package.json
    ln -s ${lib.getExe ffmpeg} $appdir/node_modules/ffmpeg-static/ffmpeg

    makeWrapper ${lib.getExe electron} $out/bin/opencut \
      --add-flags "$appdir"

    mkdir -p $out/share/applications
    cat > $out/share/applications/opencut.desktop <<EOF
    [Desktop Entry]
    Type=Application
    Name=OpenCut
    Comment=Offline-first video editor
    Exec=opencut
    Categories=AudioVideo;Video;
    EOF

    runHook postInstall
  '';

  meta = {
    description = "OpenCut desktop (Electron)";
    license = lib.licenses.mit;
    mainProgram = "opencut";
    platforms = lib.platforms.linux;
  };
}