Packing an Electron project with electron-forge

Hey all, Nix beginner here, hopefully this is the right place to ask this question.

I’m trying to create a derivation for a Yarn + Vite + Electron project which use the electron-forge CLI tool for packing, however I’m having some troubles. When I use the electron-forge package command inside the mkYarnPackage it fails with the EAI_AGAIN error when trying to download something from the internet (I suppose its trying to download the Electron binary, but I’m not sure if is only that), which make sense as in the nix build process the network access is disabled.

Giving some search, I did find this issue which indicated that in order to run the electron-forge offline I would need to populate the Electron binary cache myself, however I don’t really know how to do that using the nix language, as the cache should look something like this (reference):

├── a91b089b5dc5b1279966511344b805ec84869b6cd60af44f800b363bba25b915
│   └── electron-v15.3.1-darwin-x64.zip

So I’m stuck with that for quite some time now, I would love to know if someone already had this problem or if there is other path that I should take which would make packing this project easier.

Here is my current flake.nix config file:

{
  description = "Dash project";

  inputs = {
    nixpkgs = {url = "github:NixOS/nixpkgs/nixos-23.11";};
    nixpkgs-unstable = {url = "github:NixOS/nixpkgs/nixos-unstable";};
    flake-utils = {url = "github:numtide/flake-utils";};
  };

  outputs = {
    self,
    nixpkgs,
    nixpkgs-unstable,
    flake-utils,
  }:
    flake-utils.lib.eachDefaultSystem (system: let
      inherit (pkgs.lib) optional optionals;
      pkgs = import nixpkgs {inherit system;};
      unstable = import nixpkgs-unstable {inherit system;};

      electron = pkgs.electron_28;
      executableName = "dash";

      shellHook = ''
        # Go to the user $SHELL
        export PATH="''${PWD}/node_modules/.bin:''${PATH}"
        export ELECTRON_OVERRIDE_DIST_PATH="${electron}/bin/"

        $SHELL
      '';
    in
      with pkgs; {
        formatter = nixpkgs.legacyPackages.${system}.alejandra;

        devShell = pkgs.mkShell {
          shellHook = shellHook;
          buildInputs = [
            nodejs
            yarn
            nodePackages.prettier
            electron_28
          ];
        };

        packages.default = mkYarnPackage rec {
          name = "dash";
          src = ./.;

          packageJSON = ./package.json;
          yarnNix = ./yarn.nix;
          nativeBuildInputs = [makeWrapper git];

          buildPhase = ''
            export ELECTRON_SKIP_BINARY_DOWNLOAD=1
            # Runs the `electron-forge package` command
            yarn --offline run package
          '';

          installPhase = ''
            # executable wrapper
            makeWrapper '${electron}/bin/electron' "$out/bin/${executableName}"
          '';

          distPhase = ''
            true
          '';
        };
      });
}

And here is the logs of the failed build:

$ nix build
[...]
❯ Packaging application
  ❯ Packaging for x64 on linux
    ✖ Copying files
      › getaddrinfo EAI_AGAIN github.com
    ◼ Preparing native dependencies
    ◼ Finalizing package
◼ Running postPackage hook

An unhandled rejection has occurred inside Forge:
RequestError: getaddrinfo EAI_AGAIN github.com
at ClientRequest.<anonymous> (/nix/store/lxn8fpla0kr11i5id9g0fgb615pi0k4x-dash-modules-1.0.0/node_modules/got/dist/source/core/index.js:970:111)
    at Object.onceWrapper (node:events:632:26)
    at ClientRequest.emit (node:events:529:35)
    at ClientRequest.emit (node:domain:489:12)
    at ClientRequest.origin.emit (/nix/store/lxn8fpla0kr11i5id9g0fgb615pi0k4x-dash-modules-1.0.0/node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)
    at TLSSocket.socketErrorListener (node:_http_client:501:9)
    at TLSSocket.emit (node:events:517:28)
    at TLSSocket.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:108:26)

Check out GitHub - fetsorn/electron-react-esm-nix: ESM Electron Demo Project using Nix
Download electron binaries in a derivation, point electron_zip_dir to it and set ELECTRON_SKIP_BINARY_DOWNLOAD = "1"
vite template is on the way

Hey,

So I’ve not tried with yarn, but I just made a tutorial here javascript - Package electron application in nix - Stack Overflow

You can test it using:

$ nix run github:tobiasBora/basic-nix-packaging/using-library