如何定义默认为Python 3.5的Nix环境

时间:2016-10-12 21:33:56

标签: python nix

我在default.nix中定义了以下环境:

with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "env";
  env = buildEnv { name = name; paths = buildInputs; };
  buildInputs = [
    python35
    python35Packages.pyyaml
  ];
}

如果我运行nix-shellpython仍然是/usr/bin/python的系统python(在Ubuntu上运行),而python3是由安装的Python 3.5二进制文件的符号链接尼克斯。有没有办法定义环境,以便python指向Nix Python 3.5?

2 个答案:

答案 0 :(得分:2)

您可以使用runCommand创建仅包含python符号链接的新派生。

with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "env";
  env = buildEnv { name = name; paths = buildInputs; };
  buildInputs = [
    (runCommand "python-alias" {} ''
      mkdir -p $out/bin
      ln -s ${python35}/bin/python3 $out/bin/python
    '')
    python35
    python35Packages.pyyaml
  ];
}


nix-shell --pure --run 'python --version'
Python 3.5.3

答案 1 :(得分:1)

一个简单的解决方案是向您的环境添加shell挂钩,以定义从pythonpython3的别名。只有在您运行nix-shell

时,此别名才会处于活动状态
with import <nixpkgs> {};
stdenv.mkDerivation rec {
  name = "env";
  env = buildEnv { name = name; paths = buildInputs; };
  buildInputs = [
    python35
    python35Packages.pyyaml
  ];
  # Customizable development shell setup
  shellHook = ''
    alias python='python3'
  '';
}