Skip to content

Commit

Permalink
refactor: nixos-with-flakes-enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan4yin committed Feb 15, 2024
1 parent 8c923ec commit 6a6f9ab
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 158 deletions.
10 changes: 5 additions & 5 deletions docs/best-practices/remote-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ In your system's `flake.nix`, add a new outputs named `colmena`. A simple exampl
};
};
# Host name = "nixos-test"
"nixos-test" = { name, nodes, ... }: {
# Host name = "my-nixos"
"my-nixos" = { name, nodes, ... }: {
# Parameters related to remote deployment
deployment.targetHost = "192.168.5.42"; # Remote host's IP address
deployment.targetUser = "root"; # Remote host's username
Expand Down Expand Up @@ -103,14 +103,14 @@ For more advanced usage, refer to colmena's official documentation at <https://c

Using `nixos-rebuild` for remote deployment has the advantage of being similar to deploying to a local host. It only requires a few additional parameters to specify the remote host's IP address, username, and other details.

For instance, to deploy the configuration defined in the `nixosConfigurations.nixos-test` of your flake to a remote host, use the following command:
For instance, to deploy the configuration defined in the `nixosConfigurations.my-nixos` of your flake to a remote host, use the following command:

```bash
nixos-rebuild switch --flake .#nixos-text \
--target-host root@192.168.4.1 --build-host localhost --verbose
```

The above command will build and deploy the configuration of `nixos-test` to a server with IP `192.168.4.1`. The system build process will occur locally.
The above command will build and deploy the configuration of `my-nixos` to a server with IP `192.168.4.1`. The system build process will occur locally.

If you prefer to build the configuration on the remote host, replace `--build-host localhost` with `--build-host root@192.168.4.1`.

Expand All @@ -133,7 +133,7 @@ Host aquamarine
With this setup, you can use host aliases for deployment:

```bash
nixos-rebuild switch --flake .#nixos-test --target-host root@aquamarine --build-host root@aquamarine --verbose
nixos-rebuild switch --flake .#my-nixos --target-host root@aquamarine --build-host root@aquamarine --verbose
```

This offers a more convenient way to deploy using the defined host aliases.
2 changes: 1 addition & 1 deletion docs/nixos-with-flakes/add-custom-cache-servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ sudo nixos-rebuild switch --option substituers "https://nix-community.cachix.org

Choose one of the above three methods for configuration and deployment. After a successful deployment, all subsequent packages will preferentially search for caches from domestic mirror sources.

> If your system hostname is not `nixos-test`, you need to modify the name of `nixosConfigurations` in `flake.nix` or use `--flake /etc/nixos#nixos-test` to specify the configuration name.
> If your system hostname is not `my-nixos`, you need to modify the name of `nixosConfigurations` in `flake.nix` or use `--flake /etc/nixos#my-nixos` to specify the configuration name.
### The `extra-` Prefix for Nix Options Parameters

Expand Down
4 changes: 2 additions & 2 deletions docs/nixos-with-flakes/downgrade-or-upgrade-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Here's an example of how you can add multiple nixpkgs inputs, each using a diffe
...
}: {
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem rec {
my-nixos = nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
# The `specialArgs` parameter passes the
Expand All @@ -51,7 +51,7 @@ Here's an example of how you can add multiple nixpkgs inputs, each using a diffe
};
modules = [
./hosts/nixos-test
./hosts/my-nixos
# Omit other configurations...
];
Expand Down
10 changes: 5 additions & 5 deletions docs/nixos-with-flakes/modularize-the-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ A more complicated example, [ryan4yin/nix-config/i3-kickstarter](https://github.
│ ├── msi-rtx4090 # My main machine's configuration
│ │ ├── default.nix # This is the old configuration.nix, but most of the content has been split out to modules.
│ │ └── hardware-configuration.nix # hardware & disk related configuration, autogenerated by nixos
│ └── nixos-test # my test machine's configuration
│ └── my-nixos # my test machine's configuration
│ ├── default.nix
│ └── hardware-configuration.nix
├── modules # some common NixOS modules that can be reused
Expand Down Expand Up @@ -218,7 +218,7 @@ To test the usage of `lib.mkBefore` and `lib.mkAfter`, let's create a simple Fla
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
outputs = {nixpkgs, ...}: {
nixosConfigurations = {
"nixos-test" = nixpkgs.lib.nixosSystem {
"my-nixos" = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
Expand Down Expand Up @@ -262,7 +262,7 @@ The flake above contains the usage of `lib.mkBefore` and `lib.mkAfter` on multil

```bash
# Example 1: multiline string merging
echo $(nix eval .#nixosConfigurations.nixos-test.config.programs.bash.shellInit)
echo $(nix eval .#nixosConfigurations.my-nixos.config.programs.bash.shellInit)
trace: warning: system.stateVersion is not set, defaulting to 23.11. Read why this matters on https://nixos.org/manual/nixos/stable/options.html#opt-system.stateVersio
n.
"echo 'insert before default'
Expand All @@ -279,13 +279,13 @@ echo 'insert after default'
"
# example 2: single-line string merging
echo $(nix eval .#nixosConfigurations.nixos-test.config.programs.zsh.shellInit)
echo $(nix eval .#nixosConfigurations.my-nixos.config.programs.zsh.shellInit)
"echo 'insert before default';
echo 'this is default';
echo 'insert after default';"
# Example 3: list merging
› nix eval .#nixosConfigurations.nixos-test.config.nix.settings.substituters
› nix eval .#nixosConfigurations.my-nixos.config.nix.settings.substituters
[ "https://nix-community.cachix.org" "https://nix-community.cachix.org" "https://cache.nixos.org/" "https://ryan4yin.cachix.org" ]
```
Expand Down
14 changes: 7 additions & 7 deletions docs/nixos-with-flakes/nixos-with-flakes-enabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Note that the copied template cannot be used directly. You need to modify it to
outputs = { self, nixpkgs, ... }@inputs: {
nixosConfigurations = {
# By default, NixOS will try to refer the nixosConfiguration with
# its hostname, so the system named `nixos-test` will use this one.
# its hostname, so the system named `my-nixos` will use this one.
# However, the configuration name can also be specified using:
# sudo nixos-rebuild switch --flake /path/to/flakes/directory#<name>
#
Expand All @@ -100,8 +100,8 @@ Note that the copied template cannot be used directly. You need to modify it to
#
# Run the following command in the flake's directory to
# deploy this configuration on any NixOS system:
# sudo nixos-rebuild switch --flake .#nixos-test
"nixos-test" = nixpkgs.lib.nixosSystem {
# sudo nixos-rebuild switch --flake .#my-nixos
"my-nixos" = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
# The Nix module system can modularize configuration,
Expand Down Expand Up @@ -155,9 +155,9 @@ Note that the copied template cannot be used directly. You need to modify it to
}
```

We defined a NixOS system called `nixos-test` with a configuration file at `./configuration.nix`, which is the classic configuration we modified before. Therefore, we can still make use of it.
We defined a NixOS system called `my-nixos` with a configuration file at `./configuration.nix`, which is the classic configuration we modified before. Therefore, we can still make use of it.

To apply the configuration to a system with hostname `nixos-test`, run `sudo nixos-rebuild switch --flake /etc/nixos#nixos-test`. No changes will be made to the system because we imported the old configuration file in `/etc/nixos/flake.nix`, so the actual state we declared remains unchanged.
To apply the configuration to a system with hostname `my-nixos`, run `sudo nixos-rebuild switch --flake /etc/nixos#my-nixos`. No changes will be made to the system because we imported the old configuration file in `/etc/nixos/flake.nix`, so the actual state we declared remains unchanged.

The comments in the above code are already quite detailed, but let's emphasize a few points here:

Expand Down Expand Up @@ -193,7 +193,7 @@ First, we need to add Helix as an input in `flake.nix`:
outputs = inputs@{ self, nixpkgs, ... }: {
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem {
my-nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
# Set all input parameters as specialArgs of all sub-modules
Expand Down Expand Up @@ -237,7 +237,7 @@ To deploy the changes, run `sudo nixos-rebuild switch`, this will take a while t

After that, you can start the Helix editor by running the `hx` command.

> If your system's hostname is not `nixos-test`, you need to modify the name of `nixosConfigurations` in `flake.nix`, or use `--flake /etc/nixos#nixos-test` to specify the configuration name.
> If your system's hostname is not `my-nixos`, you need to modify the name of `nixosConfigurations` in `flake.nix`, or use `--flake /etc/nixos#my-nixos` to specify the configuration name.
> You can always try to add `--show-trace -L` to the `nixos-rebuild` command to get the detailed error message if you encounter any errors during the deployment.
Expand Down
10 changes: 5 additions & 5 deletions docs/nixos-with-flakes/other-useful-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ Another approach is to delete `/etc/nixos` directly and specify the configuratio
sudo mv /etc/nixos /etc/nixos.bak
cd ~/nixos-config

# `--flake .#nixos-test` deploys the flake.nix located in
# the current directory, and the nixosConfiguration's name is `nixos-test`
sudo nixos-rebuild switch --flake .#nixos-test
# `--flake .#my-nixos` deploys the flake.nix located in
# the current directory, and the nixosConfiguration's name is `my-nixos`
sudo nixos-rebuild switch --flake .#my-nixos
```

Choose the method that suits you best. Afterward, system rollback becomes simple. Just switch to the previous commit and deploy it:
Expand All @@ -47,8 +47,8 @@ cd ~/nixos-config
# Switch to the previous commit
git checkout HEAD^1
# Deploy the flake.nix located in the current directory,
# with the nixosConfiguration's name `nixos-test`
sudo nixos-rebuild switch --flake .#nixos-test
# with the nixosConfiguration's name `my-nixos`
sudo nixos-rebuild switch --flake .#my-nixos
```

More advanced Git operations are not covered here, but in general, rollback can be performed directly using Git. Only in cases of complete system crashes would you need to restart into the bootloader and boot the system from a previous historical version.
Expand Down
4 changes: 2 additions & 2 deletions docs/nixos-with-flakes/start-using-home-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ After adjusting the parameters, the content of `/etc/nixos/flake.nix` is as foll
outputs = inputs@{ nixpkgs, home-manager, ... }: {
nixosConfigurations = {
# TODO please change the hostname to your own
nixos-test = nixpkgs.lib.nixosSystem {
my-nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
Expand All @@ -221,7 +221,7 @@ After adjusting the parameters, the content of `/etc/nixos/flake.nix` is as foll

Then run `sudo nixos-rebuild switch` to apply the configuration, and home-manager will be installed automatically.

> If your system's hostname is not `nixos-test`, you need to modify the name of `nixosConfigurations` in `flake.nix`, or use `--flake /etc/nixos#nixos-test` to specify the configuration name.
> If your system's hostname is not `my-nixos`, you need to modify the name of `nixosConfigurations` in `flake.nix`, or use `--flake /etc/nixos#my-nixos` to specify the configuration name.
After the installation, all user-level packages and configuration can be managed through `/etc/nixos/home.nix`. When running `sudo nixos-rebuild switch`, the configuration of home-manager will be applied automatically. (**It's not necessary to run `home-manager switch` manually**!)

Expand Down
2 changes: 1 addition & 1 deletion docs/nixpkgs/overlays.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ One example of importing the above configuration as a NixOS module is as follows
outputs = inputs@{ nixpkgs, ... }: {
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem {
my-nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
Expand Down
10 changes: 5 additions & 5 deletions docs/zh/best-practices/remote-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ ssh-add ~/.ssh/your-private-key
};
};
# 主机名 = "nixos-test"
"nixos-test" = { name, nodes, ... }: {
# 主机名 = "my-nixos"
"my-nixos" = { name, nodes, ... }: {
# 与远程部署相关的参数
deployment.targetHost = "192.168.5.42"; # 远程主机的 IP 地址
deployment.targetUser = "root"; # 远程主机的用户名
Expand Down Expand Up @@ -102,14 +102,14 @@ nix run nixpkgs#colmena apply

`nixos-rebuild` 进行远程部署的好处在于,它的工作方式与部署到本地主机完全相同,只需要多传几个参数,指定下远程主机的 IP 地址、用户名等信息即可。

例如,使用以下命令将 flake 中的 `nixosConfigurations.nixos-test` 这份配置部署到远程主机:
例如,使用以下命令将 flake 中的 `nixosConfigurations.my-nixos` 这份配置部署到远程主机:

```bash
nixos-rebuild switch --flake .#nixos-text \
--target-host root@192.168.4.1 --build-host localhost --verbose
```

上述命令将会构建并部署 nixos-test 的配置到 IP 为 `192.168.4.1` 的服务器,系统构建过程将在本机执行。
上述命令将会构建并部署 my-nixos 的配置到 IP 为 `192.168.4.1` 的服务器,系统构建过程将在本机执行。

如果你希望在远程主机上构建系统,只需要将 `--build-host localhost` 替换为 `--build-host root@192.168.4.1`

Expand All @@ -132,6 +132,6 @@ Host aquamarine
然后就可以直接使用主机别名进行部署了:

```bash
nixos-rebuild switch --flake .#nixos-test --target-host root@aquamarine --build-host root@aquamarine --verbose
nixos-rebuild switch --flake .#my-nixos --target-host root@aquamarine --build-host root@aquamarine --verbose
```

2 changes: 1 addition & 1 deletion docs/zh/nixos-with-flakes/add-custom-cache-servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ sudo nixos-rebuild switch --option substituers "https://nix-community.cachix.org

选择上述三种方案的任一一种进行配置并部署,部署成功之后,后续所有的包都会优先从国内镜像源查找缓存。

> 如果你的系统 Hostname 不是 `nixos-test`,你需要在 `flake.nix` 中修改 `nixosConfigurations` 的名称,或者使用 `--flake /etc/nixos#nixos-test` 来指定配置名称。
> 如果你的系统 Hostname 不是 `my-nixos`,你需要在 `flake.nix` 中修改 `nixosConfigurations` 的名称,或者使用 `--flake /etc/nixos#my-nixos` 来指定配置名称。
### Nix options 参数的 `extra-` 前缀

Expand Down
4 changes: 2 additions & 2 deletions docs/zh/nixos-with-flakes/downgrade-or-upgrade-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
...
}: {
nixosConfigurations = {
nixos-test = nixpkgs.lib.nixosSystem rec {
my-nixos = nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
# 核心参数是这个,将非默认的 nixpkgs 数据源传到其他 modules 中
Expand All @@ -52,7 +52,7 @@
};
};
modules = [
./hosts/nixos-test
./hosts/my-nixos
# 省略其他模块配置...
];
Expand Down
10 changes: 5 additions & 5 deletions docs/zh/nixos-with-flakes/modularize-the-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $ tree
│ ├── msi-rtx4090 # PC 主机的配置
│ │ ├── default.nix # 这就是之前的 configuration.nix,不过大部分内容都拆出到 modules 了
│ │ └── hardware-configuration.nix # 与系统硬件相关的配置,安装 nixos 时自动生成的
│ └── nixos-test # 测试用的虚拟机配置
│ └── my-nixos # 测试用的虚拟机配置
│ ├── default.nix
│ └── hardware-configuration.nix
├── modules # 从 configuration.nix 中拆分出的一些通用配置
Expand Down Expand Up @@ -197,7 +197,7 @@ Nix Flakes 对目录结构没有任何要求,你可以参考上面的例子,
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
outputs = {nixpkgs, ...}: {
nixosConfigurations = {
"nixos-test" = nixpkgs.lib.nixosSystem {
"my-nixos" = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
Expand Down Expand Up @@ -241,7 +241,7 @@ Nix Flakes 对目录结构没有任何要求,你可以参考上面的例子,

```bash
# 示例一:多行字符串合并
echo $(nix eval .#nixosConfigurations.nixos-test.config.programs.bash.shellInit)
echo $(nix eval .#nixosConfigurations.my-nixos.config.programs.bash.shellInit)
trace: warning: system.stateVersion is not set, defaulting to 23.11. Read why this matters on https://nixos.org/manual/nixos/stable/options.html#opt-system.stateVersio
n.
"echo 'insert before default'
Expand All @@ -258,13 +258,13 @@ echo 'insert after default'
"
# 示例二:单行字符串合并
echo $(nix eval .#nixosConfigurations.nixos-test.config.programs.zsh.shellInit)
echo $(nix eval .#nixosConfigurations.my-nixos.config.programs.zsh.shellInit)
"echo 'insert before default';
echo 'this is default';
echo 'insert after default';"
# 示例三:列表合并
› nix eval .#nixosConfigurations.nixos-test.config.nix.settings.substituters
› nix eval .#nixosConfigurations.my-nixos.config.nix.settings.substituters
[ "https://nix-community.cachix.org" "https://nix-community.cachix.org" "https://cache.nixos.org/" "https://ryan4yin.cachix.org" ]
```
Expand Down

0 comments on commit 6a6f9ab

Please sign in to comment.