nix-flakes

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Nix Flakes

Nix Flakes

Modern Nix project management with hermeticity and reproducibility through flake.lock.
通过flake.lock实现封闭性和可复现性的现代Nix项目管理方式。

Core Commands

核心命令

Project Management

项目管理

bash
undefined
bash
undefined

Initialize 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
undefined
nix flake clone nixpkgs --dest ./nixpkgs
undefined

Running and Building

运行与构建

Always prefix local flake paths with
path:
(e.g.,
path:.
) to ensure Nix uses all files in the directory without requiring them to be staged in Git.
bash
undefined
本地flake路径务必以
path:
为前缀(例如
path:.
),以确保Nix使用目录中的所有文件,而无需将它们暂存到Git中。
bash
undefined

Build 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
undefined
nix run github:numtide/treefmt
undefined

Development Environments

开发环境

In a headless environment, use
nix develop
with
--command
to run tasks within the environment.
bash
undefined
在无界面环境中,使用带有
--command
参数的
nix develop
在环境内运行任务。
bash
undefined

Run 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
undefined
nix develop path:. --command env
undefined

Flake Structure (
flake.nix
)

Flake结构(
flake.nix

A basic
flake.nix
pattern:
nix
{
  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.nix
模板:
nix
{
  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
    flake.lock
    file to ensure reproducibility.
  • 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
    nix develop
    , always use the
    --command
    flag to ensure scripts remain non-interactive.
  • 锁定依赖:管理
    flake.lock
    文件以确保构建可复现。
  • 纯净性:Flakes默认是“纯净”的。除非被追踪(例如在Git仓库中),否则它们无法访问flake目录外的文件。
  • 非交互式:使用
    nix develop
    时,始终使用
    --command
    参数以确保脚本保持非交互式。

Debugging Flakes

调试Flakes

bash
undefined
bash
undefined

Inspect inputs

检查输入项

nix flake metadata path:.
nix flake metadata path:.

Evaluate a specific output

评估特定输出

nix eval path:.#packages.x86_64-linux.default.name
undefined
nix eval path:.#packages.x86_64-linux.default.name
undefined

Related 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的配置选项。