Skip to content

Nix Snippets

Rust

Rust bindgen

Using rust-bindgen in Nix
nixpkgs/pkgs/applications/networking/browsers/firefox/common.nix at 1fab95f5190d087e66a3502481e34e15d62090aa · NixOS/nixpkgs · GitHub
nix-env to build cloudflare/boring · GitHub
nixpkgs/doc/languages-frameworks/rust.section.md at master · NixOS/nixpkgs · GitHub

Rust buildPackage

naive method

packages.fromNaive = with pkgs;
  rustPlatform.buildRustPackage {
    pname = "package-name";
    inherit ((lib.importTOML ./Cargo.toml).package) version;
    doCheck = false;
    src = lib.cleanSource ./.;
    cargoLock = {
      lockFile = ./Cargo.lock;
      allowBuiltinFetchGit = true;
    };
    buildInputs = [
      clang
      gcc.cc.lib
    ];
    nativeBuildInputs = [
      pkg-config
      rustPlatform.bindgenHook
    ];
    inherit env;
};

pkgs overlay

overlay 的参数中,final 为覆盖后的 pkgs,prev 为覆盖前的 pkgs

overlay = final: prev: {
    newPackage = {# some modifications};
};
nixpkgs.overlays = [ overlay ];

Derivation

{stdenv}: stdenv.mkDerivation {
    name = baseNameOf (toString url);
    buildInputs = [curl];
}

evaluate derivation

dev = derivation { ... }
:b dev

查看 flake 的 drv

nix show-derivation .#nixosConfigurations.yoshika.config.system.build.toplevel

查看 outPath

nix eval nixpkgs#firefox.outPath

fetchers

Fetchers | nixpkgs

  • prefetch hash
  • build package from a tarball

path filter

Working with local files — nix.dev documentation
Nixpkgs Reference Manual

docs = builtins.path {
  path = ./.;
  filter = path: type:
    builtins.elem (/. + path) [
      ./README.md
    ];
};
docs = with pkgs.lib.fileset;
  toSource {
    root = ./.;
    fileset = unions [
      (fileFilter (file: file.hasExt "md") ./.)
    ];
};

Enabling CUDA in Pytorch

GitHub - nix-community/nix-ld: Run unpatched dynamic binaries on NixOS [maintainer=@Mic92]

  • 虚拟环境下的 Python 需要手动设置 LD_LIBRARY_PATH
LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH:/run/opengl-driver/lib
poetry run python main.py

Build Pnpm

Nixpkgs Reference Manual

stdenv.mkDerivation (finalAttrs: {
  pname = "foo";
  version = "0-unstable-1980-01-01";

  src = ...;

  nativeBuildInputs = [
    nodejs
    pnpm.configHook
  ];

  pnpmDeps = pnpm.fetchDeps {
    inherit (finalAttrs) pname version src;
    hash = "...";
  };
})

REPL

调试 Nix 表达式

  • 载入 flake 文件 :lf <ref>
  • :e <expr>
# 进入解释器
nix repl

# 读取当前 flake 输出
:lf .
# 查看变量
<TAB>
# 查看 inputs 下的变量
inputs.<TAB>

# 加载 nixpkgs
nix repl -f '<nixpkgs>'
# 禁用 NIX_PATH 的情况
nix repl -f flake:nixpkgs

NixOS 配置

启用 swap

nixpkgs/zram.nix at master · NixOS/nixpkgs · GitHub

zramSwap.enable = true;

zramSwap.memoryPercent  默认参数是 50,使用 zramctl 查看分配大小

Nix 命令行

安装程序

  • 不推荐使用 nix-env 安装 packages,其不会被记录到 NixOS 的声明式配置中
# 安装包
nix-env -iA nixpkgs.hello
# 移除包
nix-env -e nixpkgs.hello
# 搜索包
nix-env -qaP hello
  • nix-shell 一般用于临时使用某个包,退出 shell 后不会污染环境
nix-shell -p hello
# interpret installables from Nix expression
nix-shell -p 'python38.withPackages (packages: [ packages.django ])'
  • 应使用 nix-command 中的对应命令 nix profile

操作 nix store

  • 使用 nix-store 手动处理 nix store
  • 构建指定的 path,输入的 path 可以是 derivation
nix-store --realize path --add-root path
  • nix-instantiate
  • 通过有输出 derivation 的文件实例化为一个 drv 文件
nix-instantiate test.nix
# 之后通过 `nix-store` 进行构建
nix-store -r $(nix-instantiate test.nix)
  • 通过 store path 寻找 drv 来源
nix-store --query --deriver /nix/store/to/some/executable

比较 closures

nix store diff-clousures path1 path2

垃圾回收

# 查询所有历史版本
nix profile history --profile /nix/var/nix/profiles/system
# 清理历史版本,不会删除数据
nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/system

# `-d` 指示删除 `/nix/var/nix/profiles` 下所有的 old generations
nix-collect-garbage -d

更新配置 *

  • 使用镜像更新
# use impure if system not specified
home-manager switch \
    --flake ".#$(hostname)" --impure \
    --option substituters "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store"

nix flake check \
    --option substituters "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/store"

使当前 flake 与系统版本同步

nix flake update \
    --override-input nixpkgs "github:nixos/nixpkgs/$(nixos-version --revision)"

更新单个 input

nix flake update nixpkgs

查看当前配置

nix show-config