如何修改这个 flake.nix,以便每次加载环境时都不必加载 nix 包。

huangapple go评论91阅读模式
英文:

How to modify this flake.nix so that I don't have to load the nix package every time I load the environment

问题

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

I try to create a development environment with an npm package that does not have a Nix equivalent. I think I'm very close to the end, but I'm probably missing 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_modules
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 input in a flake.nix in order to have a development environment with this package.

I've tried with this 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.

英文:

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

答案1

得分: 2

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

  • package - 包含您正在构建的软件包的Nix输出(如果它是可执行文件,则可以将其添加为开发shell中的 buildInput
  • shell - 一个 devShell,用于提供运行您要构建的软件包所需的npm依赖项,这相当于克隆软件包并运行 npm install

因此,如果您只想将软件包用于另一个项目,您的 flake 将如下所示:

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 ]; };
    });

如果您只想使其自动具有 npm 依赖项 "已安装",则可以使用 shell,因此 flake 将如下所示:

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;
    });
英文:

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
      });

huangapple
  • 本文由 发表于 2023年1月7日 00:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75033884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定