Home > Back-end >  How to modify this flake.nix so that I don't have to load the nix package every time I load the
How to modify this flake.nix so that I don't have to load the nix package every time I load the

Time:01-13

I've taken this post as example for my question :https://johns.codes/blog/building-typescript-node-apps-with-nix

I try to make development environment with a npm package that has not a nix equivalent. I think that I'm very near from the end but I probably miss something important and it doesn't work.

in the folder node-gyp-build:

npm init -y

npm install  node-gyp-build

mkdir nix
rm -fr node_module
node2nix -16 --development --input package.json --lock package-lock.json --node-env ./nix/node-env.nix --composition ./nix/default.nix  --output ./nix/node-package.nix
nix-build nix/default.nix

Now my question is how to use the inout in in a flake.nix in order to have developement environment with this package

I've tried with that flake.nix

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

  outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import (./nix/default.nix);
      in with pkgs;{
        devShell = mkShell { buildInputs = [ npm_pack];};
        ]; };
      });
}

and this command:

 nix develop --extra-experimental-features nix-command --extra-experimental-features flakes --ignore-environment

error: Dependency is not of a valid type: element 1 of buildInputs for nix-shell

CodePudding user response:

The npm_pack var in your example has the following attributes (many omitted)

  • package - The nix output of the package you are building (which can be added as a buildInput in your dev shell if its some executable)
  • shell - a devShell which gives you the npm deps to run the package you are trying to build, this would be equilvant to you cloning the package and runing npm install

so if you just want to use the package for another project your flake would look like

 outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import ./nix { inherit pkgs system;};
      in with pkgs;{
        devShell = mkShell { buildInputs = [ npm_pack.package ];};
      });

If you want to just make it so you auto have npm deps "installed" you can use the shell, so the flake would look like

 outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import ./nix { inherit pkgs system;};
      in with pkgs;{
        devShell = npm_pack.shell
      });
  • Related