跳转至

Nix包管理 — Nix可复现环境

一句话概述:Nix 是一个"函数式"包管理器,核心思想是"同一份配置=同一个环境",保证你的开发环境在任何机器上都能完美复现,彻底告别"在我电脑上能跑"的问题。

核心知识点速查表

概念白话解释
Nix一个包管理器,但跟 apt/brew 完全不同,强调"可复现"
NixpkgsNix 的包仓库,12.2万+ 个包,比任何包管理器都多
FlakesNix 的"项目配置文件",锁定所有依赖的精确版本
nix-shell临时进入一个包含特定工具的环境,退出就消失
NixOS基于 Nix 构建的整个操作系统(可选,不是必须用)
DevboxNix 的简化版前端,对新手更友好

当前版本信息(2026年)

信息详情
Nixpkgs 包数122,000+ 个(全球最多)
NixOS 最新版25.11
Nix 语言Nix 表达式语言(函数式)
官网https://nixos.org
GitHub Stars24,400+
许可证LGPL 2.1

安装配置

# 方法1:官方安装脚本(推荐)
sh <(curl -L https://nixos.org/nix/install) --daemon

# 方法2:Determinate Nix(更友好的发行版)
curl --proto '=https' --tlsv1.2 -sSf -L \
  https://install.determinate.systems/nix | sh -s -- install

# 验证安装
nix --version

# 启用 Flakes(实验特性,但已成为事实标准)
# 在 ~/.config/nix/nix.conf 中添加:
# experimental-features = nix-command flakes
mkdir -p ~/.config/nix
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf

基本使用

临时环境(nix-shell / nix shell)

# 临时使用某个工具(用完就消失,不污染系统)
nix shell nixpkgs#python312     # 临时获得 Python 3.12
nix shell nixpkgs#nodejs_22     # 临时获得 Node.js 22
nix shell nixpkgs#samtools      # 临时获得 samtools

# 同时加载多个工具
nix shell nixpkgs#python312 nixpkgs#nodejs_22 nixpkgs#git

# 运行一次性命令(不进入 shell)
nix run nixpkgs#cowsay -- "Hello Nix"
nix run nixpkgs#python312 -- -c "print('hello')"

# 搜索包
nix search nixpkgs samtools     # 搜索 samtools 相关包
nix search nixpkgs python       # 搜索 Python 相关包

开发环境(Flakes)

# flake.nix — 项目根目录
{
  description = "我的生信项目开发环境";

  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 = nixpkgs.legacyPackages.${system};  # 获取包集合
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            python312           # Python 3.12
            python312Packages.numpy   # NumPy
            python312Packages.pandas  # Pandas
            samtools            # SAM/BAM 工具
            fastp               # 质控工具
            git                 # Git
            curl                # 下载工具
          ];

          shellHook = ''
            echo "生信开发环境已加载"
            python --version
          '';
        };
      }
    );
}
# 进入开发环境
nix develop               # 根据 flake.nix 创建环境

# 首次使用会自动生成 flake.lock(锁定精确版本)
# flake.lock 应该提交到 git,确保团队环境一致

Devbox(Nix 简化版,推荐新手)

# 安装 Devbox
curl -fsSL https://get.jetify.com/devbox | bash

# 初始化项目
devbox init

# 添加包(自动用 Nix 安装)
devbox add python@3.12
devbox add nodejs@22
devbox add samtools

# 进入环境
devbox shell

# 生成的 devbox.json:
# {
#   "packages": ["python@3.12", "nodejs@22", "samtools"]
# }

高级用法

Home Manager(管理用户配置)

# home.nix — 声明式管理你的 shell 配置
{ config, pkgs, ... }:
{
  home.packages = with pkgs; [
    htop vim ripgrep fd fzf      # 常用工具
  ];

  programs.git = {
    enable = true;
    userName = "张三";
    userEmail = "zhangsan@example.com";
  };

  programs.zsh = {
    enable = true;
    shellAliases = {
      ll = "ls -la";
      gs = "git status";
    };
  };
}

可复现的数据分析(rix — R 语言)

# 用 rix 包为 R 项目创建 Nix 环境
library(rix)

rix(
  r_ver = "4.3.3",
  r_pkgs = c("dplyr", "ggplot2", "vegan"),
  system_pkgs = c("samtools"),
  ide = "rstudio",
  project_path = "."
)
# 这会生成 default.nix,任何人都能复现你的 R 环境

常见报错与解决

报错信息原因解决方案
experimental feature 'flakes' is disabled没启用 Flakes在 nix.conf 中添加 experimental-features
error: cannot find flakeflake.nix 不存在在项目根目录创建 flake.nix
hash mismatch下载的包校验失败nix store gc 清理后重试
下载慢默认源在国外使用清华或 USTC 的 Nix 镜像
磁盘占用大Nix store 积累太多nix store gc 垃圾回收
Permission denied没用 daemon 模式重新安装时加 --daemon

速查表

# ===== 包管理 =====
nix search nixpkgs keyword       # 搜索包
nix shell nixpkgs#package        # 临时使用包
nix run nixpkgs#package          # 运行包的默认命令
nix profile install nixpkgs#pkg  # 永久安装到用户环境

# ===== 开发环境 =====
nix develop                      # 进入 flake 开发环境
nix develop --command bash       # 用 bash 进入
nix flake init                   # 初始化 flake

# ===== 维护 =====
nix store gc                     # 垃圾回收
nix store optimise               # 去重优化
nix flake update                 # 更新 flake 依赖

# ===== Devbox(简化版) =====
devbox init                      # 初始化
devbox add package               # 添加包
devbox shell                     # 进入环境
devbox run command               # 运行命令

同类工具对比

特性NixDockercondaPixi
可复现性极高(函数式)高(容器)
学习曲线陡峭 ⭐⭐⭐⭐⭐中等 ⭐⭐⭐低 ⭐⭐低 ⭐⭐
包数量12.2万+N/A2万+2万+
跨语言✅ 全语言
磁盘占用
适用场景完美可复现部署数据科学数据科学
入门推荐DevboxDocker DesktopMinicondapixi init

总结:Nix 是可复现性最强的包管理器,2026 年社区增长很快(Reddit 5.2万订阅)。但学习曲线陡峭,新手建议从 Devbox 入门。生信项目如果需要绝对的环境一致性(论文复现),Nix 是最好的选择。