You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

nix run可执行但不符合预期,nix build因缺失poetry.lock文件报错

Nix Flake配置问题:构建失败与运行不符合预期

问题场景

用户提供了用于创建包含Python和Streamlit虚拟环境的Nix Flake配置,执行nix run仅打开Python解释器(次要问题),执行nix build时直接报错失败。

原Flake配置代码

{
  description = "virtual environment with python and streamlit";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        python=pkgs.python311;
        f = ps: with ps;[
          ipython
          matplotlib
          pandas
        ];
        pip_python_packages= python.withPackages(f);

        myDevTools = [
          pip_python_packages
          pkgs.streamlit
        ];
        outputName = builtins.attrNames self.outputs self.outputs;
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = myDevTools;
        };

        packages.default = pkgs.poetry2nix.mkPoetryApplication {
          projectDir = self;
        };
        apps.default = {
          program  = "${python}/bin/python";
          args = [ "main.py" ];
          src = "././";
          type = "app";
      };

      });
}

报错信息

error:
… while evaluating the attribute 'pkgs.buildPythonPackage'

at /nix/store/s1z7nb9n6r5n0r34fabp6yybwkbr8mjk-source/pkgs/development/interpreters/python/passthrufun.nix:87:5:

   86|     withPackages = import ./with-packages.nix { inherit buildEnv pythonPackages;};
   87|     pkgs = pythonPackages;
     |     ^
   88|     interpreter = "${self}/bin/${executable}";

… while calling the 'mapAttrs' builtin

at /nix/store/s1z7nb9n6r5n0r34fabp6yybwkbr8mjk-source/pkgs/development/interpreters/python/passthrufun.nix:31:8:

   30|           value;
   31|     in lib.mapAttrs func items;
     |        ^
   32|   in ensurePythonModules (callPackage

(stack trace truncated; use '--show-trace' to show the full trace)

error: getting status of '/nix/store/ggvg85rp5qzyr9bngsl6r0pcrkyxqa49-source/poetry.lock': No such file or directory

问题根源

  1. nix build失败原因:配置中使用了pkgs.poetry2nix.mkPoetryApplication,这个函数要求项目目录下必须存在poetry.lock和pyproject.toml文件,但当前项目未生成这些Poetry依赖管理文件,导致Nix找不到所需文件报错。
  2. nix run不符合预期原因:apps.default里的src设置为././路径无效,导致Nix无法正确定位main.py,最终直接启动Python解释器而非执行目标脚本。

修复方案

方案1:移除Poetry相关配置(无Poetry需求时)

如果不需要用Poetry管理依赖,直接删除packages.default部分,同时修复apps.default的路径问题:

{
  description = "virtual environment with python and streamlit";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        python = pkgs.python311;
        f = ps: with ps; [
          ipython
          matplotlib
          pandas
        ];
        pip_python_packages = python.withPackages(f);

        myDevTools = [
          pip_python_packages
          pkgs.streamlit
        ];
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = myDevTools;
        };

        apps.default = {
          program = "${python}/bin/python";
          args = [ "${self}/main.py" ]; # 直接通过self指向flake目录下的main.py
          type = "app";
        };
      });
}

方案2:添加Poetry配置文件(需要Poetry管理时)

  1. 先在项目目录初始化Poetry并生成配置文件:
    poetry init
    poetry add streamlit ipython matplotlib pandas
    
    执行后会生成pyproject.toml和poetry.lock文件。
  2. 调整Flake配置,保留Poetry部分并修复应用启动逻辑:
    {
      description = "virtual environment with python and streamlit";
      inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      inputs.flake-utils.url = "github:numtide/flake-utils";
    
      outputs = { self, nixpkgs, flake-utils }:
        flake-utils.lib.eachDefaultSystem (system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
          in {
            devShells.default = pkgs.mkShell {
              buildInputs = with pkgs; [ streamlit ipython matplotlib pandas ];
            };
    
            packages.default = pkgs.poetry2nix.mkPoetryApplication {
              projectDir = self;
            };
            apps.default = {
              program = "${self.packages.${system}.default}/bin/你的项目命令"; # 替换为pyproject.toml中定义的命令
              type = "app";
            };
          });
    }
    

验证修复

  • 执行nix develop进入开发Shell,检查Python、Streamlit及依赖是否正常可用。
  • 执行nix run,方案1会直接执行main.py,方案2会启动Poetry定义的应用命令。
  • 执行nix build,方案1会默认构建开发Shell环境,方案2会基于Poetry配置构建完整应用包。

内容的提问来源于stack exchange,提问作者Pierre-olivier Gendraud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 01:37:26