Skip to content

Nix Flakes

[!quote] - getting started with flakes - Godot export tunnelvr/flake.nix - GitHub - macalimlim/godot-rust-template - nix flake

  • Flakes 是 Nix 的实验功能,已被广泛采用。

启用 Flake

  • 确保所用的 Nix 版本支持 flake,可使用 nix-unstable-installer
  • 非 NixOS 下,修改 ~/.config/nix/nix.conf 启用命令
echo 'experimental-features = nix-command flakes' >> /home/user/.config/nix/nix.conf

常用命令

通过 flake 的模板输出 outputs.templates 创建项目

# initialize empty
nix flake init

# initialize with a template
nix flake init -t github:nix-community/templates#<name>
nix flake new -t github:nix-community/templates#<name> ./project

Profile

  • profile 是 flake 中的概念。profile 是一系列的包。
  • ~/.nix-profile 链接到 /nix/var/nix/profiles/per-user/username/profile
  • ~/.nix-profile/bin 应在 PATH

查看用户的所有 profiles

ls -l /nix/var/nix/profiles/per-user/username/profile*

查看当前 profile 安装的包

nix profile list

# update all packages
nix profile upgrade '.*'

nix profile upgrade packages.x86_64-linux.hello

# install arbitrary packages from registry
nix registry list
nix profile add nixpkgs#hello

# remove by name
nix profile remove hello
nix profile remove packages.x86_64-linux.hello

# remove all
nix profile remove '.*'

# remove by path
nix profile remove /nix/store/rr3y0c6zyk7kjjl8y19s4lsrhn4aiq1z-hello-2.10
# install an *installable* expression
nix profile install --impure --expr 'with builtins.getFlake "nixpkgs"; with legacyPackages.${builtins.currentPlatform}; haskellPackages.ghcWithPackages (ps: [ ps.tidal ps.haskell-language-server])'

编写 Flake 文件

推荐使用 flake-parts 组织 flake 文件