nix
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNix
Nix
Package manager and functional language for reproducible environments.
软件包管理器与函数式编程语言,用于构建可复现的环境。
Quick Start
快速开始
bash
undefinedbash
undefinedRun 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
undefinednix shell nixpkgs#git nixpkgs#vim --command git --version
undefinedRunning Applications
运行应用程序
Run any application from without installing it permanently.
nixpkgsbash
undefined无需永久安装,即可运行中的任意应用程序。
nixpkgsbash
undefinedRun 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'运行长期运行的应用程序:tmux new -d 'nix run nixpkgs#some-server'
tmux new -d 'nix run nixpkgs#some-server'undefinedundefinedFormatting
格式化
Format Nix files in your project:
bash
undefined格式化项目中的Nix文件:
bash
undefinedFormat current flake
格式化当前flake
nix fmt
nix fmt
Check formatting
检查格式化结果
nix fmt -- --check
undefinednix fmt -- --check
undefinedHash Conversion
哈希格式转换
Convert hashes between formats (replaces deprecated /).
nix hash to-srito-base16bash
undefined在不同格式间转换哈希值(替代已弃用的/命令)。
nix hash to-srito-base16bash
undefinedConvert 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=
undefinednix hash convert --hash-algo sha256 --to nix32 sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=
undefinedPrefetching Hashes
预取哈希值
Use prefetch helpers to fetch sources and get the nix32 hash, then convert to SRI if needed.
bash
undefined使用预取工具获取源码并得到nix32哈希值,如有需要再转换为SRI格式。
bash
undefinedFetch 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>
undefinednix hash convert --hash-algo sha256 --from nix32 --to sri <nix32-hash>
undefinedEvaluating Expressions
评估表达式
Since the environment is headless and non-interactive, use instead of the REPL for debugging.
nix evalbash
undefined由于环境是无头且非交互式的,调试时请使用而非REPL。
nix evalbash
undefinedEvaluate 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> {})'
undefinedix eval --expr 'builtins.attrNames (import <nixpkgs> {})'
undefinedSearching for Packages
搜索软件包
bash
undefinedbash
undefinedSearch for a package by name or description
按名称或描述搜索软件包
nix search nixpkgs python3
undefinednix search nixpkgs python3
undefinedNix Language Patterns
Nix语言模式
Variables and Functions
变量与函数
nix
undefinednix
undefinedLet 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
undefinedlet
multiply = x: y: x * y;
in
multiply 2 3
undefinedAttribute 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.comOr 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 to see the build output of a derivation.
nix log - Dependency Issues: Use to see what a program depends on.
nix-store -q --references $(which program) - Cache Issues: Add to force a local build if you suspect a bad binary cache.
--no-substitute - Why Depends: Use to see dependency chain.
nix why-depends nixpkgs#hello nixpkgs#glibc
- 构建失败:使用查看派生包的构建输出。
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的可用配置选项。