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

echo 'experimental-features = nix-command flakes' >> /home/user/.config/nix/nix.conf

基本命令

初始化项目

nix flake init

使用模板创建 flake,模板是指在 flake.nix 的输出中包含 templates

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/alice/profile*

查看当前 profile 安装的包

nix profile list

升级 profile 中的包

nix profile upgrade '.*' # 升级所有
nix profile upgrade packages.x86_64-linux.hello
  • 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])'

  • 安装包
    • nix profile install nixpkgs#hello
  • 移除包
    • nix profile remove nixpkgs#hello
    • nix profile remove packages.x86_64-linux.hello
  • 移除所有包
    • nix profile remove '.*'
  • 移除指定 path
    • nix profile remove /nix/store/rr3y0c6zyk7kjjl8y19s4lsrhn4aiq1z-hello-2.10