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;
  };

启用 swap

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

zramSwap.enable = true;

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

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 in REPL
dev = derivation { ... }
:b dev

fetchers

Fetchers | nixpkgs

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

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 = "...";
  };
})