nix-flakes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNix Flakes
Nix Flakes
Modern Nix project management with hermeticity and reproducibility through flake.lock.
通过flake.lock实现封闭性和可复现性的现代Nix项目管理方式。
Core Commands
核心命令
Project Management
项目管理
bash
undefinedbash
undefinedInitialize a new flake in the current directory
在当前目录初始化一个新的flake
nix flake init
nix flake init
Create a new flake from template
从模板创建新的flake
nix flake new hello -t templates#hello
nix flake new hello -t templates#hello
Update flake.lock (updates all inputs)
更新flake.lock(更新所有输入项)
nix flake update
nix flake update
Update specific input only
仅更新特定输入项
nix flake update nixpkgs
nix flake update nixpkgs
Lock without updating (create missing entries)
锁定依赖而不更新(创建缺失条目)
nix flake lock
nix flake lock
Check flake for syntax and common errors
检查flake的语法和常见错误
nix flake check
nix flake check
Show flake outputs
显示flake输出
nix flake show
nix flake show
Show flake metadata (inputs, revisions)
显示flake元数据(输入项、修订版本)
nix flake metadata path:.
nix flake info path:. # Alias for metadata
nix flake metadata path:.
nix flake info path:. # metadata的别名
Prefetch flake and inputs into store
将flake和输入项预取到存储中
nix flake prefetch github:NixOS/nixpkgs
nix flake prefetch-inputs path:.
nix flake prefetch github:NixOS/nixpkgs
nix flake prefetch-inputs path:.
Clone flake repository
克隆flake仓库
nix flake clone nixpkgs --dest ./nixpkgs
undefinednix flake clone nixpkgs --dest ./nixpkgs
undefinedRunning and Building
运行与构建
Always prefix local flake paths with (e.g., ) to ensure Nix uses all files in the directory without requiring them to be staged in Git.
path:path:.bash
undefined本地flake路径务必以为前缀(例如),以确保Nix使用目录中的所有文件,而无需将它们暂存到Git中。
path:path:.bash
undefinedBuild the default package
构建默认软件包
nix build path:.
nix build path:.
Build a specific output
构建特定输出
nix build path:.#packageName
nix build path:.#packageName
Run the default app
运行默认应用
nix run path:.
nix run path:.
Run a specific app from a flake
运行flake中的特定应用
nix run path:.#appName
nix run path:.#appName
Run an app from a remote flake
运行远程flake中的应用
nix run github:numtide/treefmt
undefinednix run github:numtide/treefmt
undefinedDevelopment Environments
开发环境
In a headless environment, use with to run tasks within the environment.
nix develop--commandbash
undefined在无界面环境中,使用带有参数的在环境内运行任务。
--commandnix developbash
undefinedRun a command inside the devShell
在devShell内运行命令
nix develop path:. --command make build
nix develop path:. --command make build
Check if current environment matches devShell
检查当前环境是否与devShell匹配
nix develop path:. --command env
undefinednix develop path:. --command env
undefinedFlake Structure (flake.nix
)
flake.nixFlake结构(flake.nix
)
flake.nixA basic pattern:
flake.nixnix
{
description = "A basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system}.default = pkgs.hello;
devShells.${system}.default = pkgs.mkShell {
buildInputs = [ pkgs.git pkgs.vim ];
};
};
}基础模板:
flake.nixnix
{
description = "A basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system}.default = pkgs.hello;
devShells.${system}.default = pkgs.mkShell {
buildInputs = [ pkgs.git pkgs.vim ];
};
};
}Best Practices
最佳实践
- Locking: Manage the file to ensure reproducibility.
flake.lock - Purity: Flakes are "pure" by default. They cannot access files outside the flake directory unless they are tracked (e.g. in the git tree if using git).
- Non-Interactive: When using , always use the
nix developflag to ensure scripts remain non-interactive.--command
- 锁定依赖:管理文件以确保构建可复现。
flake.lock - 纯净性:Flakes默认是“纯净”的。除非被追踪(例如在Git仓库中),否则它们无法访问flake目录外的文件。
- 非交互式:使用时,始终使用
nix develop参数以确保脚本保持非交互式。--command
Debugging Flakes
调试Flakes
bash
undefinedbash
undefinedInspect inputs
检查输入项
nix flake metadata path:.
nix flake metadata path:.
Evaluate a specific output
评估特定输出
nix eval path:.#packages.x86_64-linux.default.name
undefinednix eval path:.#packages.x86_64-linux.default.name
undefinedRelated Skills
相关技能
- nix: Run applications without installation and create development environments using Nix.
- nh: Manage NixOS and Home Manager operations with improved output using nh.
- nix:无需安装即可运行应用,并使用Nix创建开发环境。
- nh:通过nh管理NixOS和Home Manager操作,获得更友好的输出。
Related Tools
相关工具
- search-nix-packages: Search for packages available in the NixOS package repository when working with flakes.
- search-nix-options: Find configuration options available in NixOS for flake configurations.
- search-home-manager-options: Find configuration options for Home Manager in flake setups.
- search-nix-packages:使用flakes时,搜索NixOS软件包仓库中的可用软件包。
- search-nix-options:查找适用于flake配置的NixOS配置选项。
- search-home-manager-options:查找flake设置中Home Manager的配置选项。