nix

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Nix

Nix

Package manager and functional language for reproducible environments.
软件包管理器与函数式编程语言,用于构建可复现的环境。

Quick Start

快速开始

bash
undefined
bash
undefined

Run a package once

一次性运行某个软件包

nix run nixpkgs#hello
nix run nixpkgs#hello

Create a shell with multiple packages

创建包含多个软件包的Shell环境

nix shell nixpkgs#git nixpkgs#vim --command git --version
undefined
nix shell nixpkgs#git nixpkgs#vim --command git --version
undefined

Running Applications

运行应用程序

Run any application from
nixpkgs
without installing it permanently.
bash
undefined
无需永久安装,即可运行
nixpkgs
中的任意应用程序。
bash
undefined

Run a package with specific arguments

带特定参数运行软件包

nix run nixpkgs#cowsay -- "Hello from Nix!"
nix run nixpkgs#cowsay -- "Hello from Nix!"

Run long-running applications:
tmux new -d 'nix run nixpkgs#some-server'

运行长期运行的应用程序:
tmux new -d 'nix run nixpkgs#some-server'

undefined
undefined

Formatting

格式化

Format Nix files in your project:
bash
undefined
格式化项目中的Nix文件:
bash
undefined

Format current flake

格式化当前flake

nix fmt
nix fmt

Check formatting

检查格式化结果

nix fmt -- --check
undefined
nix fmt -- --check
undefined

Hash Conversion

哈希格式转换

Convert hashes between formats (replaces deprecated
nix hash to-sri
/
to-base16
).
bash
undefined
在不同格式间转换哈希值(替代已弃用的
nix hash to-sri
/
to-base16
命令)。
bash
undefined

Convert a nix32 hash to SRI (sha256)

将nix32哈希转换为SRI格式(sha256)

nix hash convert --hash-algo sha256 --from nix32 --to sri 1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s
nix hash convert --hash-algo sha256 --from nix32 --to sri 1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s

Convert to nix32 explicitly

显式转换为nix32格式

nix hash convert --hash-algo sha256 --to nix32 sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=
undefined
nix hash convert --hash-algo sha256 --to nix32 sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=
undefined

Prefetching Hashes

预取哈希值

Use prefetch helpers to fetch sources and get the nix32 hash, then convert to SRI if needed.
bash
undefined
使用预取工具获取源码并得到nix32哈希值,如有需要再转换为SRI格式。
bash
undefined

Fetch a tarball and print its nix32 hash

获取tarball包并打印其nix32哈希值

nix-prefetch-url --unpack https://example.com/source.tar.gz
nix-prefetch-url --unpack https://example.com/source.tar.gz

Convert the nix32 hash to SRI for fetchFromGitHub/fetchurl

将nix32哈希转换为SRI格式,用于fetchFromGitHub/fetchurl

nix hash convert --hash-algo sha256 --from nix32 --to sri <nix32-hash>
undefined
nix hash convert --hash-algo sha256 --from nix32 --to sri <nix32-hash>
undefined

Evaluating Expressions

评估表达式

Since the environment is headless and non-interactive, use
nix eval
instead of the REPL for debugging.
bash
undefined
由于环境是无头且非交互式的,调试时请使用
nix eval
而非REPL。
bash
undefined

Evaluate a simple expression

评估简单表达式

nix eval --expr '1 + 2'
nix eval --expr '1 + 2'

Inspect an attribute from nixpkgs

查看nixpkgs中的某个属性

nix eval nixpkgs#hello.name
nix eval nixpkgs#hello.name

Evaluate a local nix file

评估本地Nix文件

nix eval --file ./default.nix
nix eval --file ./default.nix

List keys in a set (useful for exploration)

列出集合中的键(用于探索时很有用)

nix eval --expr 'builtins.attrNames (import <nixpkgs> {})'
undefined
ix eval --expr 'builtins.attrNames (import <nixpkgs> {})'
undefined

Searching for Packages

搜索软件包

bash
undefined
bash
undefined

Search for a package by name or description

按名称或描述搜索软件包

nix search nixpkgs python3
undefined
nix search nixpkgs python3
undefined

Nix Language Patterns

Nix语言模式

Variables and Functions

变量与函数

nix
undefined
nix
undefined

Let binding

Let绑定

let name = "Nix"; in "Hello ${name}"
let name = "Nix"; in "Hello ${name}"

Function definition

函数定义

let multiply = x: y: x * y; in multiply 2 3
undefined
let multiply = x: y: x * y; in multiply 2 3
undefined

Attribute Sets

属性集合

nix
{
  a = 1;
  b = "foo";
}
nix
{
  a = 1;
  b = "foo";
}

Shebang Scripts

Shebang脚本

Use Nix as a script interpreter:
bash
#!/usr/bin/env nix
#! nix shell nixpkgs#bash nixpkgs#curl --command bash

curl -s https://example.com
Or with flakes:
bash
#!/usr/bin/env nix
#! nix shell nixpkgs#python3 --command python3

print("Hello from Nix!")
将Nix用作脚本解释器:
bash
#!/usr/bin/env nix
#! nix shell nixpkgs#bash nixpkgs#curl --command bash

curl -s https://example.com
或者使用flakes:
bash
#!/usr/bin/env nix
#! nix shell nixpkgs#python3 --command python3

print("Hello from Nix!")

Troubleshooting

故障排查

  • Broken Builds: Use
    nix log
    to see the build output of a derivation.
  • Dependency Issues: Use
    nix-store -q --references $(which program)
    to see what a program depends on.
  • Cache Issues: Add
    --no-substitute
    to force a local build if you suspect a bad binary cache.
  • Why Depends: Use
    nix why-depends nixpkgs#hello nixpkgs#glibc
    to see dependency chain.
  • 构建失败:使用
    nix log
    查看派生包的构建输出。
  • 依赖问题:使用
    nix-store -q --references $(which program)
    查看程序的依赖项。
  • 缓存问题:如果怀疑二进制缓存损坏,添加
    --no-substitute
    参数强制本地构建。
  • 依赖链查询:使用
    nix why-depends nixpkgs#hello nixpkgs#glibc
    查看依赖链。

Related Skills

相关技能

  • nix-flakes: Use Nix Flakes for reproducible builds and dependency management in Nix projects.
  • nh: Manage NixOS and Home Manager operations with improved output using nh.
  • nix-flakes:在Nix项目中使用Nix Flakes实现可复现构建与依赖管理。
  • nh:使用nh工具管理NixOS与Home Manager操作,输出更友好。

Related Tools

相关工具

  • search-nix-packages: Search for packages available in the NixOS package repository.
  • search-nix-options: Find configuration options available in NixOS.
  • search-home-manager-options: Find configuration options for Home Manager.
  • search-nix-packages:搜索NixOS软件包仓库中的可用软件包。
  • search-nix-options:查找NixOS中的可用配置选项。
  • search-home-manager-options:查找Home Manager的可用配置选项。