Install nodejs package to env

Alright so the fact that these are needed as CLI tools in PATH rather than members of a node_modules/ directory makes things significantly easier.

Essentially the process will be to package those two tools up using a node to nix translation tool to create a custom package that flox can use.

I’ll use moon as an example to package, which you can follow along with. I chose the translator floco, but node2nix or other translators would work fine as well.

Here’s the process from scratch in an empty project area, you could perform roughly the same steps in an existing flox project ( probably edit flake.nix manually for an existing project ).

$ mkdir -p moon;
$ cd moon;
$ git init;

# Based on the usual `flox init -t project' template:
# Add `floco' to our inputs, and set a new description
$ cat <<'EOF' > flake.nix
{
  description = "moon command line and core system";

  inputs.flox-floxpkgs.url = "github:flox/floxpkgs";
  inputs.floco.url         = "github:aakropotkin/floco";

  outputs = args @ {flox-floxpkgs, ...}: flox-floxpkgs.project args (_: {});
}
EOF

$ mkdir -p pkgs/moon;
$ pushd pkgs/moon;
# Generate a `nix' build for the package.
$ flox nix run 'github:aakropotkin/floco#fromRegistry' -- @moonrepo/cli@1.1.1;

# Adapt some boilerplate taken from `floco' "registry" project template to play
# nicely with `flox':
$ cat <<'EOF' > ./default.nix
{
  lib
, inputs
, system
}: let
  ident   = "@moonrepo/cli";
  version = "1.1.1";
  fmod    = lib.evalModules {
    modules = [
      inputs.floco.nixosModules.floco
      { config.floco.settings = { inherit system; }; }
      ./pdefs.nix
      {
        config.floco.packages.${ident}.${version} = {
          # Force copying of tree during `install' so we can copy binaries.
          installed.override.copyTree = true;
          # This package has no runtime deps, the declared dependencies are only
          # required during installation, so we can drop them.
          trees.global = null;
        };
      }
    ];
  };
in fmod.config.floco.packages.${ident}.${version}.global // {
  meta.description = "moon command line and core system";
  meta.mainProgram = "moon";
  meta.license     = lib.getLicenseFromSpdxId "MIT";
}
EOF

$ popd;
$ git add ./flake.nix ./pkgs;
$ flox build moon;
$ ls ./result/bin;
$ ls ./result/lib/node_modules/@moonrepo/cli/;

Because moon has an “install script” we added a few extra lines to a default.nix file, but for prettier and any other package that doesn’t have a postinstall script you could drop lines 14-22.

This is essentially similar to packaging any node project using nix, the main difference is that flox expects a standardized file hierarchy, so you’ll want to place any default.nix files under ./pkgs/<NAME>/default.nix.

Ideally we’ll get a more streamlined process for creating node packages, but for now this should get you up and running. Let me know if there’s anything else I can help with.