|
1 | 1 | { |
2 | 2 | description = "NixOS configuration of Ryan Yin"; |
3 | 3 |
|
4 | | - # flake 为了确保够纯,它不依赖系统自身的 /etc/nix/nix.conf,而是在 flake.nix 中通过 nixConfig 设置 |
5 | | - # 但是为了确保安全性,flake 默认仅允许直接设置少数 nixConfig 参数,其他参数都需要在执行 nix 命令时指定 `--accept-flake-config`,否则会被忽略 |
6 | | - # <https://nixos.org/manual/nix/stable/command-ref/conf-file.html> |
7 | | - # 如果有些包国内镜像下载不到,它仍然会走国外,这时候就得靠旁路由来解决了。 |
8 | | - # 临时修改默认网关为旁路由: sudo ip route add default via 192.168.5.201 |
9 | | - # sudo ip route del default via 192.168.5.201 |
10 | 4 | nixConfig = { |
11 | 5 | experimental-features = [ "nix-command" "flakes" ]; |
12 | 6 |
|
|
31 | 25 | ]; |
32 | 26 | }; |
33 | 27 |
|
34 | | - # 这是 flake.nix 的标准格式,inputs 是 flake 的依赖,outputs 是 flake 的输出 |
35 | | - # inputs 中的每一项都被拉取、构建后,被作为参数传递给 outputs 函数 |
| 28 | + |
| 29 | + # This is the standard format for flake.nix. `inputs` are the dependencies of the flake, |
| 30 | + # and `outputs` function will return all the build results of the flake. |
| 31 | + # Each item in `inputs` will be passed as a parameter to the `outputs` function after being pulled and built. |
36 | 32 | inputs = { |
37 | | - # flake inputs 有很多种引用方式,应用最广泛的是 github 的引用方式 |
| 33 | + # There are many ways to reference flake inputs. The most widely used is github:owner/name/reference, |
| 34 | + # which represents the GitHub repository URL + branch/commit-id/tag. |
38 | 35 |
|
39 | | - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; # 使用 nixos-unstable 分支 for nix flakes |
40 | | - nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.05"; # unstable branch may be broken sometimes, use stable branch when necessary |
| 36 | + # Official NixOS package source, using nixos-unstable branch here |
| 37 | + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; |
| 38 | + nixpkgs-stable.url = "github:nixos/nixpkgs/nixos-23.05"; |
41 | 39 |
|
42 | 40 | # for macos |
43 | 41 | nixpkgs-darwin.url = "github:nixos/nixpkgs/nixpkgs-23.05-darwin"; |
|
51 | 49 | # e.g. wechat-uos/qqmusic/dingtalk |
52 | 50 | nur.url = "github:nix-community/NUR"; |
53 | 51 |
|
54 | | - home-manager.url = "github:nix-community/home-manager"; |
55 | | - # follows 是 inputs 中的继承语法 |
56 | | - # 这里使 home-manager 的 nixpkgs 这个 inputs 与当前 flake 的 inputs.nixpkgs 保持一致,避免依赖的 nixpkgs 版本不一致导致问题 |
57 | | - home-manager.inputs.nixpkgs.follows = "nixpkgs"; |
| 52 | + # home-manager, used for managing user configuration |
| 53 | + home-manager = { |
| 54 | + url = "github:nix-community/home-manager"; |
| 55 | + # The `follows` keyword in inputs is used for inheritance. |
| 56 | + # Here, `inputs.nixpkgs` of home-manager is kept consistent with the `inputs.nixpkgs` of the current flake, |
| 57 | + # to avoid problems caused by different versions of nixpkgs dependencies. |
| 58 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 59 | + }; |
58 | 60 |
|
59 | 61 | # modern window compositor |
60 | 62 | hyprland.url = "github:hyprwm/Hyprland/v0.25.0"; |
|
77 | 79 | nil.url = "github:oxalica/nil/2023-05-09"; |
78 | 80 | }; |
79 | 81 |
|
80 | | - # outputs 的参数都是 inputs 中定义的依赖项,可以通过它们的名称来引用。 |
81 | | - # 不过 self 是个例外,这个特殊参数指向 outputs 自身(自引用),以及 flake 根目录 |
82 | | - # 这里的 @ 语法将函数的参数 attribute set 取了个别名,方便在内部使用 |
| 82 | + # `outputs` are all the build result of the flake. |
| 83 | + # A flake can have many use cases and different types of outputs. |
| 84 | + # parameters in `outputs` are defined in `inputs` and can be referenced by their names. |
| 85 | + # However, `self` is an exception, This special parameter points to the `outputs` itself (self-reference) |
| 86 | + # The `@` syntax here is used to alias the attribute set of the inputs's parameter, making it convenient to use inside the function. |
83 | 87 | outputs = inputs@{ |
84 | 88 | self, |
85 | 89 | nixpkgs, |
|
88 | 92 | ... |
89 | 93 | }: { |
90 | 94 | nixosConfigurations = { |
| 95 | + # By default, NixOS will try to refer the nixosConfiguration with its hostname. |
| 96 | + # so the system named `msi-rtx4090` will use this configuration. |
| 97 | + # However, the configuration name can also be specified using `sudo nixos-rebuild switch --flake /path/to/flakes/directory#<name>`. |
| 98 | + # The `nixpkgs.lib.nixosSystem` function is used to build this configuration, the following attribute set is its parameter. |
| 99 | + # Run `sudo nixos-rebuild switch --flake .#msi-rtx4090` in the flake's directory to deploy this configuration on any NixOS system |
91 | 100 | msi-rtx4090 = nixpkgs.lib.nixosSystem { |
92 | 101 | system = "x86_64-linux"; |
93 | 102 |
|
94 | | - specialArgs = inputs; |
| 103 | + # The Nix module system can modularize configurations, improving the maintainability of configurations. |
| 104 | + # |
| 105 | + # Each parameter in the `modules` is a Nix Module, and there is a partial introduction to it in the nixpkgs manual: |
| 106 | + # <https://nixos.org/manual/nixpkgs/unstable/#module-system-introduction> |
| 107 | + # It is said to be partial because the documentation is not complete, only some simple introductions |
| 108 | + # (such is the current state of Nix documentation...) |
| 109 | + # A Nix Module can be an attribute set, or a function that returns an attribute set. |
| 110 | + # If a Module is a function, according to the Nix Wiki description, this function can have up to four parameters: |
| 111 | + # |
| 112 | + # config: The configuration of the entire system |
| 113 | + # options: All option declarations refined with all definition and declaration references. |
| 114 | + # pkgs: The attribute set extracted from the Nix package collection and enhanced with the nixpkgs.config option. |
| 115 | + # modulesPath: The location of the module directory of Nix. |
| 116 | + # |
| 117 | + # Only these four parameters can be passed by default. |
| 118 | + # If you need to pass other parameters, you must use `specialArgs` by uncomment the following line |
| 119 | + specialArgs = inputs; # pass all inputs into all sub modules. |
95 | 120 | modules = [ |
96 | 121 | ./hosts/msi-rtx4090 |
97 | 122 |
|
98 | | - # home-manager 作为 nixos 的一个 module |
99 | | - # 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令 |
| 123 | + # make home-manager as a module of nixos |
| 124 | + # so that home-manager configuration will be deployed automatically when executing `nixos-rebuild switch` |
100 | 125 | home-manager.nixosModules.home-manager |
101 | 126 | { |
102 | 127 | home-manager.useGlobalPkgs = true; |
103 | 128 | home-manager.useUserPackages = true; |
104 | 129 |
|
105 | | - # 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数 |
| 130 | + # pass all inputs into home manager's all sub modules |
106 | 131 | home-manager.extraSpecialArgs = inputs; |
107 | | - home-manager.users.ryan = import ./home/home-x11.nix; |
| 132 | + home-manager.users.ryan = import ./home/linux/x11.nix; |
108 | 133 | } |
109 | 134 | ]; |
110 | 135 | }; |
111 | 136 |
|
112 | | - |
113 | 137 | nixos-test = nixpkgs.lib.nixosSystem { |
114 | 138 | system = "x86_64-linux"; |
115 | 139 | specialArgs = inputs; |
116 | 140 | modules = [ |
117 | 141 | ./hosts/nixos-test |
118 | 142 |
|
119 | | - # home-manager 作为 nixos 的一个 module |
120 | | - # 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令 |
121 | 143 | home-manager.nixosModules.home-manager |
122 | 144 | { |
123 | 145 | home-manager.useGlobalPkgs = true; |
124 | 146 | home-manager.useUserPackages = true; |
125 | 147 |
|
126 | | - # 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数 |
127 | 148 | home-manager.extraSpecialArgs = inputs; |
128 | | - home-manager.users.ryan = import ./home/home-wayland.nix; |
| 149 | + home-manager.users.ryan = import ./home/linux/wayland.nix; |
129 | 150 | } |
130 | 151 | ]; |
131 | 152 | }; |
132 | 153 | }; |
133 | 154 |
|
| 155 | + # configurations for MacOS |
134 | 156 | darwinConfigurations."harmonica" = darwin.lib.darwinSystem { |
135 | 157 | system = "x86_64-darwin"; |
136 | 158 |
|
137 | 159 | specialArgs = inputs; |
138 | 160 | modules = [ |
139 | 161 | ./hosts/harmonica |
140 | 162 |
|
141 | | - # home-manager 作为 nixos 的一个 module |
142 | | - # 这样在 nixos-rebuild switch 时,home-manager 也会被自动部署,不需要额外执行 home-manager switch 命令 |
143 | 163 | home-manager.darwinModules.home-manager |
144 | 164 | { |
145 | 165 | home-manager.useGlobalPkgs = true; |
146 | 166 | home-manager.useUserPackages = true; |
147 | | - |
148 | | - # 使用 home-manager.extraSpecialArgs 自定义传递给 ./home 的参数 |
| 167 | + |
149 | 168 | home-manager.extraSpecialArgs = inputs; |
150 | | - home-manager.users.admin = import ./home/home-darwin.nix; |
| 169 | + home-manager.users.admin = import ./home/darwin; |
151 | 170 | } |
152 | 171 | ]; |
153 | 172 | }; |
|
0 commit comments