|
| 1 | +# Configuration Initialization |
| 2 | +This section will describe creating the `minimal` template from scratch. |
| 3 | + |
| 4 | +You do not have to do this; you can simply clone the minimal configuration template with the following command: |
| 5 | +```sh |
| 6 | +nix flake init -t github:yunfachi/denix#minimal |
| 7 | +``` |
| 8 | + |
| 9 | +You can also copy the minimal configuration template without the rices: |
| 10 | +```sh |
| 11 | +nix flake init -t github:yunfachi/denix#minimal-no-rices |
| 12 | +``` |
| 13 | + |
| 14 | +## Flake |
| 15 | +First, create a directory for your configuration and a `flake.nix` file with the following content: |
| 16 | +```nix |
| 17 | +{ |
| 18 | + inputs = { |
| 19 | + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; |
| 20 | + home-manager = { |
| 21 | + url = "github:nix-community/home-manager/master"; |
| 22 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 23 | + }; |
| 24 | + denix = { |
| 25 | + url = "github:yunfachi/denix"; |
| 26 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 27 | + inputs.home-manager.follows = "home-manager"; |
| 28 | + }; |
| 29 | + }; |
| 30 | + |
| 31 | + outputs = { |
| 32 | + denix, |
| 33 | + nixpkgs, |
| 34 | + ... |
| 35 | + } @ inputs: let |
| 36 | + mkConfigurations = isHomeManager: |
| 37 | + denix.lib.configurations { |
| 38 | + homeManagerNixpkgs = nixpkgs; |
| 39 | + homeManagerUser = "sjohn"; #!!! REPLACEME |
| 40 | + inherit isHomeManager; |
| 41 | +
|
| 42 | + paths = [./hosts ./modules ./rices]; |
| 43 | +
|
| 44 | + specialArgs = { |
| 45 | + inherit inputs; |
| 46 | + }; |
| 47 | + }; |
| 48 | + in { |
| 49 | + nixosConfigurations = mkConfigurations false; |
| 50 | + homeConfigurations = mkConfigurations true; |
| 51 | + }; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +If you are not familiar with `inputs` and `outputs`, read [NixOS Wiki Flakes](https://nixos.wiki/wiki/Flakes). |
| 56 | + |
| 57 | +Code explanation: |
| 58 | +- `mkConfigurations` - a function to reduce code repetition, which takes `isHomeManager` and passes it to `denix.lib.configurations`. |
| 59 | +- `denix.lib.configurations` - [Configurations (flakes) - Introduction](/configurations/introduction). |
| 60 | +- `paths = [./hosts ./modules ./rices];` - paths to be recursively imported by Denix. Remove `./rices` if you don't want to use rices. |
| 61 | + |
| 62 | +## Hosts |
| 63 | +Create a `hosts` directory, and within it, create a subdirectory with the name of your host, for example, `desktop`. |
| 64 | + |
| 65 | +In this subdirectory, create a `default.nix` file with the following content: |
| 66 | +```nix |
| 67 | +{delib, ...}: |
| 68 | +delib.host { |
| 69 | + name = "desktop"; #!!! REPLACEME |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +In the same directory, create a `hardware.nix` file: |
| 74 | +```nix |
| 75 | +{delib, ...}: |
| 76 | +delib.host { |
| 77 | + name = "desktop"; #!!! REPLACEME |
| 78 | +
|
| 79 | + homeManagerSystem = "x86_64-linux"; #!!! REPLACEME |
| 80 | + home.home.stateVersion = "24.05"; #!!! REPLACEME |
| 81 | +
|
| 82 | + nixos = { |
| 83 | + nixpkgs.hostPlatform = "x86_64-linux"; #!!! REPLACEME |
| 84 | + system.stateVersion = "24.05"; #!!! REPLACEME |
| 85 | +
|
| 86 | + # nixos-generate-config --show-hardware-config |
| 87 | + # other generated code here... |
| 88 | + }; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +The `default.nix` file will be modified later after adding modules and rices, so you can keep it open. |
| 93 | + |
| 94 | +## Rices |
| 95 | +Skip this section if you do not wish to use rices. |
| 96 | + |
| 97 | +Create a `rices` directory, and within it, create a subdirectory with the name of your rice, for example, `dark`. |
| 98 | + |
| 99 | +In this subdirectory, create a `default.nix` file with the following content: |
| 100 | +```nix |
| 101 | +{delib, ...}: |
| 102 | +delib.rice { |
| 103 | + name = "dark"; #!!! REPLACEME |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Modules |
| 108 | +Create a `modules` directory, and within it, create a `config` subdirectory (typically, it contains modules that are not tied to a specific program or service). |
| 109 | + |
| 110 | +It should be mentioned that modules represent your configuration, meaning it's up to your imagination, and you are free to change the modules as you wish. |
| 111 | + |
| 112 | +### Constants {#modules-constants} |
| 113 | +In this subdirectory, create a `constants.nix` file with the following content: |
| 114 | +```nix |
| 115 | +{delib, ...}: |
| 116 | +delib.module { |
| 117 | + name = "constants"; |
| 118 | +
|
| 119 | + options.constants = with delib; { |
| 120 | + username = readOnly (strOption "sjohn"); #!!! REPLACEME |
| 121 | + userfullname = readOnly (strOption "John Smith"); #!!! REPLACEME |
| 122 | + useremail = readOnly (strOption "johnsmith@example.com"); #!!! REPLACEME |
| 123 | + }; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +This file is optional, as are any of its options, which are only used by you, but it is strongly recommended as good practice. |
| 128 | + |
| 129 | +### Hosts |
| 130 | +Also, create a `hosts.nix` file in this same directory (`modules/config`), and write any example from [Hosts - Examples](/hosts/examples). |
| 131 | + |
| 132 | +For example, we will take ["With the `type` Option"](/hosts/examples#type-option): |
| 133 | +```nix |
| 134 | +{delib, ...}: |
| 135 | +delib.module { |
| 136 | + name = "hosts"; |
| 137 | +
|
| 138 | + options = with delib; let |
| 139 | + host = {config, ...}: { |
| 140 | + options = |
| 141 | + hostSubmoduleOptions |
| 142 | + // { |
| 143 | + type = noDefault (enumOption ["desktop" "server"] null); |
| 144 | +
|
| 145 | + isDesktop = boolOption (config.type == "desktop"); |
| 146 | + isServer = boolOption (config.type == "server"); |
| 147 | + }; |
| 148 | + }; |
| 149 | + in { |
| 150 | + host = hostOption host; |
| 151 | + hosts = hostsOption host; |
| 152 | + }; |
| 153 | +
|
| 154 | + home.always = {myconfig, ...}: { |
| 155 | + assertions = delib.hostNamesAssertions myconfig.hosts; |
| 156 | + }; |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +If you added an example with new options (`type`, `displays`, etc.) or made your own options, don't forget to add values for these options in the hosts. |
| 161 | + |
| 162 | +In our example, we added the `type` option, so open the `default.nix` file in your host's directory and add the attribute `type` to the `delib.host` function: |
| 163 | +```nix |
| 164 | +{delib, ...}: |
| 165 | +delib.host { |
| 166 | + name = "desktop"; #!!! REPLACEME |
| 167 | +
|
| 168 | + type = "desktop" #!!! REPLACEME (desktop/server) |
| 169 | +
|
| 170 | + # ... |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Rices |
| 175 | +Skip this section if you are not using rices. |
| 176 | + |
| 177 | +In the `modules/config` directory, create a `rices.nix` file, and write any example from [Rices - Examples](/rices/examples). |
| 178 | + |
| 179 | +For example, we will take ["Minimally Recommended Rice Module"](/rices/examples#minimally-recommended): |
| 180 | +```nix |
| 181 | +delib.module { |
| 182 | + name = "rices"; |
| 183 | +
|
| 184 | + options = with delib; let |
| 185 | + rice = { |
| 186 | + options = riceSubmoduleOptions; |
| 187 | + }; |
| 188 | + in { |
| 189 | + rice = riceOption rice; |
| 190 | + rices = ricesOption rice; |
| 191 | + }; |
| 192 | +
|
| 193 | + home.always = {myconfig, ...}: { |
| 194 | + assertions = delib.riceNamesAssertions myconfig.rices; |
| 195 | + }; |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +Also, open the `default.nix` file of your host and add the attribute `rice` to the `delib.host` function: |
| 200 | +```nix |
| 201 | +{delib, ...}: |
| 202 | +delib.host { |
| 203 | + name = "desktop"; #!!! REPLACEME |
| 204 | +
|
| 205 | + rice = "dark" #!!! REPLACEME |
| 206 | +
|
| 207 | + # ... |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +### Home Manager |
| 212 | +If you created a [constants module](#modules-constants), just create a `home.nix` file with the following content: |
| 213 | +```nix |
| 214 | +{delib, ...}: |
| 215 | +delib.module { |
| 216 | + name = "home"; |
| 217 | +
|
| 218 | + home.always = {myconfig, ...}: let |
| 219 | + inherit (myconfig.constants) username; |
| 220 | + in { |
| 221 | + home = { |
| 222 | + inherit username; |
| 223 | + homeDirectory = "/home/${username}"; |
| 224 | + }; |
| 225 | + }; |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | +If you did not use the [constants module](#modules-constants), the content of the file will be: |
| 230 | +```nix |
| 231 | +{delib, ...}: |
| 232 | +delib.module { |
| 233 | + name = "home"; |
| 234 | +
|
| 235 | + home.always.home = { |
| 236 | + username = "sjohn"; #!!! REPLACEME |
| 237 | + homeDirectory = "/home/sjohn"; #!!! REPLACEME |
| 238 | + }; |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +### User |
| 243 | +You can also create a `user.nix` file with the configuration of your NixOS user: |
| 244 | +```nix |
| 245 | +{delib, ...}: |
| 246 | +delib.module { |
| 247 | + name = "user"; |
| 248 | +
|
| 249 | + nixos.always = {myconfig, ...}: let |
| 250 | + inherit (myconfig.constants) username; |
| 251 | + in { |
| 252 | + users = { |
| 253 | + groups.${username} = {}; |
| 254 | +
|
| 255 | + users.${username} = { |
| 256 | + isNormalUser = true; |
| 257 | + initialPassword = username; |
| 258 | + extraGroups = ["wheel"]; |
| 259 | + }; |
| 260 | + }; |
| 261 | + }; |
| 262 | +} |
| 263 | +``` |
| 264 | + |
| 265 | +If you did not use the [constants module](#modules-constants), the content of the file will be: |
| 266 | +```nix |
| 267 | +{delib, ...}: |
| 268 | +delib.module { |
| 269 | + name = "user"; |
| 270 | +
|
| 271 | + nixos.always.users = { |
| 272 | + groups.sjohn = {}; #!!! REPLACEME |
| 273 | +
|
| 274 | + users.sjohn = { #!!! REPLACEME |
| 275 | + isNormalUser = true; |
| 276 | + initialPassword = "sjohn"; #!!! REPLACEME |
| 277 | + extraGroups = ["wheel"]; |
| 278 | + }; |
| 279 | + }; |
| 280 | +} |
| 281 | +``` |
| 282 | + |
| 283 | +## Conclusion |
| 284 | +If you have followed the instructions precisely, you will end up with the following configuration directory tree: |
| 285 | +```plaintext |
| 286 | +hosts |
| 287 | +- desktop |
| 288 | + - default.nix |
| 289 | + - hardware.nix |
| 290 | +modules |
| 291 | +- config |
| 292 | + - constants.nix |
| 293 | + - home.nix |
| 294 | + - hosts.nix |
| 295 | + - rices.nix |
| 296 | + - user.nix |
| 297 | +rices |
| 298 | +- dark |
| 299 | + - default.nix |
| 300 | +flake.nix |
| 301 | +``` |
| 302 | + |
| 303 | +You can check if everything is set up correctly using the command: |
| 304 | +```sh |
| 305 | +nix flake check .# |
| 306 | +``` |
0 commit comments