Created
February 14, 2022 08:49
-
-
Save luochen1990/20f2ed53450b419e720413395328417d to your computer and use it in GitHub Desktop.
(示例) 在 NixOS 中构建可用 pip 的 python 隔离环境
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env nix-shell | |
#! nix-shell shell.nix | |
{ pkgs ? import <nixpkgs> {} }: | |
(pkgs.buildFHSUserEnv { | |
name = "devbox"; | |
targetPkgs = pkgs: (with pkgs; [ | |
python38 | |
python38Packages.pip | |
python38Packages.virtualenv | |
openssl | |
zlib | |
stdenv.cc.cc.lib | |
pythonManylinuxPackages.manylinux2014Package | |
]); | |
profile = '' | |
python3 -m venv ${builtins.toString ./.venv-py38} | |
source ${builtins.toString ./.venv-py38/bin/activate} | |
pip3 install -e ${builtins.toString ./.} | |
''; | |
runScript = "bash"; | |
}).env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用方法:
之后就进入了一个虚拟环境, 该环境满足:
虽然可以在虚拟的隔离环境里用 pip, 但是还是建议能写进 nix 脚本的依赖就写到 nix 脚本里, 更 reproducible (虽然并没有, 因为没flake锁定版本).
另外即使要用 pip 安装, 也建议写在 profile 脚本里 (如 20行), 至少这样是声明式的, 即使 venv 目录被删了也没关系, 因为依赖信息记录在该脚本里.