Electron apps were historically hard to package in Nix (see this cool meme), but apparently it is not true anymore.
How can I package an electron application in nix, and quickly develop npm-related things in NixOs?
Electron apps were historically hard to package in Nix (see this cool meme), but apparently it is not true anymore.
How can I package an electron application in nix, and quickly develop npm-related things in NixOs?
Copyright © 2021 Jogjafile Inc.
(disclamer: I am not a JS expert at all, just trying to wrap my head around electron packaging)
TL;DR: If you only want to package, and already have an existing project, you can jump straight to the section "do the packaging part" and copy/paste the
.nixfile (quite simple, nothing too magic). Yet, I explain before how to develop efficiently in NixOs etc. I also made a minimal project here that you can test with:There is nothing more to do when using electron-forge or an external library, yet I did some demo here:
How to quickly develop on NixOs
Npm/electron has a culture of often packing pre-built binaries (which is not great, for instance in term of security but it is another question), that expects a loader in, e.g.,
/lib64/ld-linux-x86-64.so.2. This can be a problem if you are on NixOs as NixOs removes by default the loaders for maximum reproducibility (see more details and solution here). What I recommend if you do not want to bother anymore and just follow the usual tutorials usable on any standard linux distribution, is to enable in yourconfiguration.nix:(you might need to reboot to propagate correctly the environment variables)
With that, you can use
npmlike in any other system!Now, you might want to package your programs in a clean and pure way, that you can install with your favorite flake etc… Let's see how to do it now.
Create an electron project (nothing interesting here)
For this tutorial, we will create a basic electron project as explained in the tutorial (as I have
nix-ldenabled):then add in the
scriptsection ofpackage.jsonastartcommand like in:Then, create the files listed in https://www.electronjs.org/docs/latest/tutorial/quick-start, notably:
main.html:index.html:and
preload.js:Run the project (optional)
If you only want to package without testing "interactively" your software, you can skip this section.
Method 1: If you do not have
nix-ldinstalled, or want to maximize purity, you might want here to useelectronpackaged from NixOs. Just enter into a shell with:and it will start your project. Later, once we create a
default.nix, you can just run:and it will automatically install electron (it also works if you are using external libraries)
Method 2: On the other hand, if you run:
it will try to run the npm-installed electron. If you have
nix-ldinstalled with all the libraries, it should work. If you get an error like:or an error saying that you cannot run binaries on NixOs, then it likely means that you run NixOs and don't have
nix-ldenabled (cf section `How to quickly develop on NixOs, make sure to reboot after that). If you get instead:then it means that you need to install in
nix-ldthe package providinglibdrm.so.2:Here, you see that you want the library
libdrm. Just add it toprograms.nix-ld.enableas shown above, and keep going with other libraries (in my test I needed no reboot in KDE Plasma, but my setting is a bit different (older version), but if you see that your changes are not taken into account, you might need a reboot). It might be a bit annoying the first time, but once your list is long enough, it should work for most programs (maybe we could propose a more complete set of options in nixpkgs). In my case, I notably needed to add:(but I already made a long enough list that I describe here)
Do the packaging (interesting part)
To package this, just create this file called, say,
package.nix:and a file
default.nixcontaining:Build and run with:
(you will need to update the hash in the above code, as you will get an error the first time with the valid hash)
Enjoy!
You can also type:
and you will get a shell with electron installed so that you can debug with:
Alternative: using electron-forge
electron-forge is the officially recommended solution to package electron application. You can prepare your project as follows:
In theory, if you want to package a
.debor.rpmand havenix-ldenabled, you should be able to do:but I don't know why it complains with an error:
file "../../../../../../../nix/store/pryizz83lb9hvjknaqyl5f54d5bai3xd-my-electron-app-0.1" links out of the package.Nevermind, the goal is to package it for nix now. And good luck: you have nothing more to do, the above script also works for this one (let me know if for more complex cases it fails).
You can try this version also on the same repository on the
electron-forgebranch:Yarn
If you use yarn, you can also use
mkYarnPackage, I've not tried but I expect this to be similar (even if I heard that yarn was not as nice as npm to package in nix).Further readings
Here are some other related tutorials I wrote:
Other related resources:
rg "buildNpmPackage" $(rg "electron" -l)and you will see all files that contain bothbuildNpmPackageandelectron: useful to get some examples. If you only want to read the simpler packages, you can see the length of the files usingfor x in $(rg "buildNpmPackage" $(rg "electron" -l) -l); do echo "$x"; cat "$x" | wc -l; done. You also have a few examples using yarn (usefor x in $(rg "mkYarnPackage" $(rg "electron" -l) -l); do echo "$x"; cat "$x" | wc -l; done)