Featured image of post 【NIX】探索在zz.ac主机中使用nix

【NIX】探索在zz.ac主机中使用nix

理软件包和开发环境,提供原子升级、依赖隔离、环境可复现、一键回滚等强大特性,核心是基于其函数式语言定义软件构建过程,常用于构建可复现的开发环境和 NixOS 操作系统。

Nix Store (/nix/store):所有包都安装在这里。

在zz.ac中运行ls /nix/store/,列出了在/nix/store/下大量的软件包。

运行 du -sh /nix/store/发现这些软件包有1.7G !

1
2
bosh@h1:~$ du -sh /nix/store/
1.7G    /nix/store/

nix是基本使用命令有哪些?

核心命令与用法

  • nix search: 搜索软件包。

    • nix search nixpkgs <包名>: 查找 nixpkgs 仓库中的包,例如 nix search nixpkgs firefox
  • nix run: 在隔离环境中临时运行一个包,无需全局安装。

    • nix run nixpkgs#<包名>: 直接运行,例如 nix run nixpkgs#cowsay
    • nix run nixpkgs#<包名> -- <参数>: 传递参数给包,如 nix run nixpkgs#cowsay -- "Hello Nix"
  • nix profile (旧版 nix-env): 管理用户环境的软件包,类似传统包管理器(apt/dnf)。

    安装软件nix profile add nixpkgs#nano
    查看已安装列表nix profile list
    更新所有已安装软件nix profile upgrade --all
    卸载软件 (通过编号)nix profile remove 0 (编号通过 list 查看)
    卸载软件 (通过名称)nix profile remove nixpkgs#nano 为:nix profile remove nano
  • nix build: 构建软件包或 Nix 表达式。

    • nix build nixpkgs#<包名>: 构建指定包,会生成一个指向输出的 symlink。
  • 垃圾回收: 清理未使用的包和世代。

  • nix shell: 进入一个包含指定包的环境(旧版类似 nix-shell -p)。

  • 执行命令报错的解决方案(来自Gemini):
1
2
3
bosh@h1:~$ nix run nixpkgs#nano -- Caddyfile

error: experimental Nix feature 'nix-command' is disabled; add '--extra-experimental-features nix-command' to enable it

方法 1:临时开启(最快,无需改配置)

在命令后面直接加上报错提示中的那个参数:

Bash

1
nix run nixpkgs#nano --extra-experimental-features 'nix-command flakes' -- Caddyfile

注意:这里我额外加上了 flakes,因为通常 nix-commandflakes 是一对,很多新功能都需要两者同时开启。


方法 2:永久开启(强烈推荐)

如果你打算长期学习和使用 Nix,建议永久开启这些功能,避免每次都输入一长串参数。

  1. 手动编辑配置文件: 如果文件不存在,就创建一个。

    Bash

    1
    2
    3
    
    # 如果没有权限,请在命令前加 sudo
    mkdir -p ~/.config/nix
    nano ~/.config/nix/nix.conf
    
  2. 在文件中添加以下内容

    Plaintext

    1
    
    experimental-features = nix-command flakes
    
  3. 保存并退出。现在你可以直接运行之前的命令了:

    Bash

    1
    
    nix run nixpkgs#nano -- Caddyfile
    

zz.ac主机没有安装nano,那么我测试下使用nix 安装nano:

1. 运行nix search nixpkgs ,输出了非常多的结果:

1
2
3
4
5
6
7
8
9
bosh@h1:~$ nix search nixpkgs nano
* legacyPackages.aarch64-linux.age-plugin-ledger (0.1.2)
  Ledger Nano plugin for age

* legacyPackages.aarch64-linux.akkuPackages.nanopass (1.9.2)
  Nanopass Compiler Infrastructure

* legacyPackages.aarch64-linux.chickenPackages_5.chickenEggs.nanomsg (1.0.0.6)
……

2. 在隔离环境中临时运行一个包,无需全局安装:运行nix run nixpkgs#nano ,进入了nano编辑器,在编辑器中输入内容,保存试试。

image.png

1
2
3
4
5
bosh@h1:~/nixtest$ nix run nixpkgs#nano
bosh@h1:~/nixtest$ ls
测试文件.txt
bosh@h1:~/nixtest$ cat 测试文件.txt 
这是由nix在隔离环境中临时运行一个包,无需全局安装的nano

3. 使用nix profile管理用户环境的软件包,类似传统包管理器(apt/dnf)

1
nix profile install nano

运行报错:

1
2
3
nix profile install nano
warning: 'install' is a deprecated alias for 'add'
error: cannot find flake 'flake:nano' in the flake registries

经过查询AI,得到如下结果:

请尝试以下命令:

Bash

1
nix profile add nixpkgs#nano

注意:如果依然提示 experimental feature 错误,记得在命令后面加上 --extra-experimental-features 'nix-command flakes'


2. 深度解析:为什么不能直接写 nano

在传统的包管理器(如 apt)中,源是全局唯一的。但在 Nix 的新体系中,你可以同时拥有多个源。

  • nixpkgs:这是官方的软件仓库。
  • #:这是一个分隔符,用来指向仓库里的具体某个包。
  • nano:包的具体名称。

如果你只输入 nano,Nix 会以为你试图在本地目录或默认注册表中寻找一个名为 nano 的 Flake 项目,所以会报错 cannot find flake

使用以下命令正常安装:

1
nix profile add nixpkgs#nano
1
2
3
4
bosh@h1:~/nixtest$ nano -V
 GNU nano, version 8.7
 (C) 2025 the Free Software Foundation and various contributors
 Compiled options: --enable-utf8

4. 查看已安装列表: nix profile list

1
2
3
4
5
6
bosh@h1:~/nixtest$ nix profile list
Name:               nano
Flake attribute:    legacyPackages.aarch64-linux.nano
Original flake URL: flake:nixpkgs
Locked flake URL:   github:NixOS/nixpkgs/16c7794d0a28b5a37904d55bcca36003b9109aaa?narHash=sha256-fFUnEYMla8b7UKjijLnMe%2BoVFOz6HjijGGNS1l7dYaQ%3D
Store paths:        /nix/store/vcpkw57hqdfk33kgbxarxbv3jjx6dd92-nano-8.7

5. 更新所有已安装软件: `nix profile upgrade –all

6.卸载已安装软件: nix profile remove nixpkgs#nano

1
2
3
 bosh@h1:~/nixtest$ nix profile remove nixpkgs#nano
warning: Package name 'nixpkgs#nano' does not match any packages in the profile.
warning: No packages to remove. Use 'nix profile list' to see the current profile.
  • 更换nix profile remove nano,卸载成功
1
2
3
4
  bosh@h1:~/nixtest$ nix profile remove nano
  removing 'flake:nixpkgs#legacyPackages.aarch64-linux.nano'
  bosh@h1:~/nixtest$ nona -V
  -bash: nona: command not found

7. 交互式环境使用

1
2
3
4
5
6
7
8
bosh@h1:~/nixtest$ nix-shell -p nano

[nix-shell:~/nixtest]$ nano -v

[nix-shell:~/nixtest]$ nano -V
 GNU nano, version 8.7
 (C) 2025 the Free Software Foundation and various contributors
 Compiled options: --enable-utf8
Licensed under CC BY-NC-SA 4.0
热爱生活 学无止境
使用 Hugo 构建
主题 StackJimmy 设计