BOSh
文章227
标签320
分类74
315晚会 36氪 80后 ADB AI AI Agent AI 代理 AI 助手 AI 网关 AI 评测 AI助手 AI大模型 AI安全 AI应用 AI智能体 AI网关 API API 集成 Agent AionUi Android Automation C++ CLI CLI Proxy API CLIProxyAPI CRM Chrome 插件 Claude Opus 4.6 ConnectBot Debian DeepSeek DenchClaw DevOps Docker GEO GPL GPS GPU Gemini Gemini 3.1 Pro GitHub Gmail Gog Google AI Pro Google API Google Gemini HKUDS Hermes Hermes Agent Hexo Hugo IPV6 Jetpack Compose Kimi-K2.5 Kotlin LINUX LaTeX Linux Markdow Markdown MemU Bot MiniMax NAT64 NIX NODE NVIDIA Build NanoClaw Netcatty Newsletter Open WebUI OpenAI OpenAI 兼容接口 OpenCLI OpenClaw PDF 编译 PicoClaw Prismer QClaw QQ机器人 RAG Reddit Rust SFTP SSH Skills Subagent SuperCall Telegram Bot WebSSH Windows WorkBuddy X X热榜 YouTube ZeroClaw arXiv arch c++ git hugo iMessage n8n nanobot node js ntfs pacman podman zz.ac 东海 两性关系 个人助理 中东 中东冲突 中东局势 中关村论坛 中南大学 中国 中美 习惯养成 云同步 亚洲 代理 以色列 任务管理 伊朗 伊朗危机 伊朗战争 伦理 体育 保护主义 信息流 信息管理 停火 健康管理 光通信 免费试用 共和党 养老金 内容工厂 内容生产 内容筛选 军事冲突 军事动态 军民融合 农村 分享 创业 办公自动化 加密 加密货币 加沙 北斗 医学生 半导体 华为 博客 博客助手 博客部署成功 卫星 原生 JS 反重力 台海局势 台湾 命令 喷嚏网 国产 国产化 国产替代 国际 国际关系 国际局势 国际新闻 图卦 图说 地缘政治 基础设施 多代理 多模态AI 大学分析 大模型 孙少平 学习 安全 实时监控 家庭助理 家庭服务器 家装设计 工作总结 工作效率 工作流编排 工具链 平凡的世界 平台责任 开发 开发实录 开源 开源项目 张雪峰 微信 心理健康 情感 战争 投资工具 指标看板 提示词工程 播客 收件箱清理 效率 效率工具 教程 教育制度 数据分析 数据投毒 文献管理 新能源汽车 新闻汇总 日历聚合 时事 时事总结 显卡 晨报 智能体 智能体生态 朝鲜 架构 架构实践 核协议 核武器 桌面Cowork 模型接入 每日图说 比亚迪 油价 活动运营 浏览器自动化 消息通道 消费者权益 渔船 游戏开发 湘雅医院 热点新闻 版本更新 特朗普 生态系统 生活 生活自动化 生物识别 用例 甲骨文云 电池技术 症状追踪 皮皮虾 监管 目标管理 知识库 社交媒体 社会保障 社会百态 社会观察 科技 科研助手 笔记 第一财经 算法推荐 纽森 经济 经济观察 经验分享 编程 网关 网络 网络安全 美伊关系 美伊冲突 美国 美国大选 美国政治 能源安全 能源市场 腾讯 腾讯,龙虾,OpenClaw 腾讯云 自动化 自动化创作 自动化协作 自动化提醒 自动化流水线 自动化运维 自律教练 自由软件 行为改变 视频摘要 记录 许可证 论文写作 论文阅读 语义搜索 语音代理 读书 读书笔记 读后感 财报季 路遥 迁移 运维 远程运维 邀请确认 部署指南 量子计算 销售自动化 阅读感悟 随笔 霍尔木兹海峡 项目管理 飞书 高中生活 高考志愿 黎巴嫩 龙虾

一言

文章归档

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

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

涛叔在ZZ.NIC 公益 Linux 主机空间这篇文章里说到:

高级玩法

虽然申请到的用户没有 root 权限,不能直接在系统层面安装软件。用户还是可以通过 Linux 版的 Homebrew 或者 Nix 包管理器在自己的家目录安装需要的工具,然后通过 systemd 来管理。

nix是什么呢?

Nix 是一个独特的函数式包管理器和构建系统,支持 Linux 和 macOS,它以声明式方式管理软件包和开发环境,提供原子升级、依赖隔离、环境可复现、一键回滚等强大特性,核心是基于其函数式语言定义软件构建过程,常用于构建可复现的开发环境和 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
4
5
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
9
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

本文作者:BOSh
本文链接:http://bosh.zz.ac/posts/3857780209.html
版权声明:本文由BoSh发布,部分内容来源于网络。