Resources

https://nixos.org/manual/nixpkgs/stable/

https://nix.dev/manual/nix/2.32/

https://nix.dev/tutorials/

https://nix.dev/reference/

https://nix.dev/guides/

https://nix.dev/concepts/

nixos-and-flakes-book

Concepts

flake

flake.nix 核心文件

</home/disk/Dev/hlissner-dotfiles/flake.nix>

• flake.nix 是 Nix flake 的“入口文件”,在这个仓库里基本就是整套配置的中枢,大致做这些事:

  1. 声明依赖 (inputs)

    比如用到的 Nixpkgs 版本、外部 flake(home-manager 等),都在 flake.nix 里统一锁定和引用。
  2. 导出结果 (outputs)

    定义这个仓库对外提供什么:
    • NixOS 主机配置(hosts/ 下的机器)
    • 模块、包、overlay、devShell 等

      这样你可以用 nix build、nix flake check、nix develop 等直接调用。
  3. 自动发现项目结构

    按本仓库的约定,它会自动发现并挂上:
    • hosts/ 里的主机
    • modules/ 里的模块
    • packages/ 里的包
    • overlays/、test/ 等
  4. 给工具链提供统一入口

    像 bin/hey sync、bin/hey build、nix flake check、nix develop 等命令,最终都是围绕 flake.nix 中的定义在跑。

简单说:flake.nix 定义了“这套 dotfiles/系统配置由哪些依赖组成、导出哪些配置和命令”,其它目录 (hosts/, modules/ 等) 都是被它组织起来的。

module

在 NixOS / home-manager 体系里,module = 一块可组合的配置单元,专门用来描述“这个功能需要哪些选项、怎么启用”。

更形式化一点,一个 Nix 模块基本就是这样的函数:

{ config, lib, pkgs, ... }:
 
let
  cfg = config.modules.desktop.hyprland;
in {
  # 1. 声明这个模块有哪些配置项(options)
  options.modules.desktop.hyprland.enable = lib.mkOption {
    type = lib.types.bool;
    default = false;
    description = "Enable Hyprland desktop.";
  };
 
  # 2. 根据这些配置项,实际写系统配置(config)
  config = lib.mkIf cfg.enable {
    services.xserver.enable = true;
    # ...
  };
}

可以这样理解:

  1. options:声明“我提供哪些设置”
    • 起名:modules.desktop.hyprland.enable 这种 kebab-case 分层命名。
    • 指明类型(bool/string/list)、默认值、说明等。
    • 其它模块/主机就可以设置这些选项,比如 modules.desktop.hyprland.enable = true;。
  2. config:根据选项生成最终系统配置
    • 里面通常写的是 NixOS / home-manager 的标准选项:比如 services.*, users.*, programs.*。
    • 多个 module 可以同时给同一个 config.services.sshd 之类的字段赋值,Nix 会按规则自动合并(列表拼接、attrset 合并等)。
  3. 模块是“可合并的配置片段”
    • 顶层会把所有模块(官方模块、自定义模块、host 自己写的模块)收集起来,一起 merge 成一个巨大的 config。
    • 你不需要有“主配置文件把所有内容写满”,而是拆成很多小模块,每个模块负责一个功能/子系统。

overlay

pakcages

CLI Reference

nix flake

我现在就用过这两个

  • nix-flake-update
  • nix-flake-show

nix develop

nix develop builds a development shell defined by a flake (or shell.nix) and drops you into it with the declared tools on PATH.

nix-env

https://stop-using-nix-env.privatevoid.net/

这里的 nix envnix-env 还是有区别的

NixOS

Manual

https://nixos.org/manual/nixos/stable/

Install

在尝试用安装镜像安装的时候出现了很多问题

他甚至自动在 windows 的 uefi 分区里写了 nixos 的启动分区。。。

这种情况如何处理?查看这里 多系统共用一个 EFI

最后还是在 codex 的帮助下安装的 btrfs 和直接用命令行安装的 nixos

他直接在 btrfs 前面写了一个推荐,这让我很意外啊

(其实 codex 也是基本上就是按照手册上的步骤来的)

codex resume 019a7e68-af45-7aa3-99fb-53b580a3a024

我觉得nix的构建可以放在一个地方?

关于 NixOS 虚拟机

https://nix.dev/tutorials/nixos/nixos-configuration-on-vm.html#creating-a-qemu-based-virtual-machine-from-a-nixos-configuration

https://github.com/microvm-nix/microvm.nix

https://github.com/nix-community/nixos-generators

这个已经合并到 nixpkgs 中了

nixos-container

https://www.reddit.com/r/NixOS/comments/1ajfl8c/nixoscontainer_vs_docker_and_friends/

https://www.nijho.lt/post/proxmox-to-nixos/

https://flox.dev/blog/nix-and-containers-why-not-both/

compose2nix

compose2nix: Generate a NixOS config from a Docker Compose project - YouTube

从 Docker Compose 项目生成 NixOS 配置。

Nix Language

Insight

说实话我不是很能接受这个奇怪的语言风格

Alternative language - #4 by zimbatm - NixOS Discourse

Nix 包管理器并不局限于 Nix 语言。Guix 在底层使用 Nix,但所有表达式都在 Guile 中。

可能我想做的是就是想要一个 guix?

和list完全就是两个极端,nix 函数调用都是没括号的。。。

Manaul

这个手册是非常重要的信息源,其实写得挺好的,推荐

https://nix.dev/manual/nix/2.32/language/index.html

Data Types

Attribute set (Attrs) 属性集

这是最关键的数据类型

list

nixpkgs

Nix Packages collection