mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2024-11-23 08:33:41 +01:00
feat: update Accelerating Dotfiles Debugging
This commit is contained in:
parent
1b6e250f1c
commit
092d789980
@ -1,45 +1,50 @@
|
||||
# Accelerating Dotfiles Debugging
|
||||
|
||||
When managing our Dotfiles with Home Manager, a common challenge arises – each
|
||||
modification to our Dotfiles requires executing `sudo nixos-rebuild switch`(or
|
||||
`home-manager switch` if you use don't integrate home-manager into NixOS) to take effect.
|
||||
However, running this command recalculates the entire system state each time, even though
|
||||
Nix internally employs various caching mechanisms to expedite the process, it can still be
|
||||
cumbersome.
|
||||
After managing our Dotfiles with Home Manager, one issue we may encounter is that each
|
||||
time we modify our Dotfiles, we need to run `sudo nixos-rebuild switch` (or
|
||||
`home-manager switch` if you are using Home Manager standalone) for the changes to take
|
||||
effect. However, running this command recalculates the entire system state each time,
|
||||
which is painful despite the many caching mechanisms already in place within Nix to
|
||||
accelerate this computation.
|
||||
|
||||
Take my Neovim/Emacs configurations as an example; I frequently make high-frequency
|
||||
modifications to them, sometimes dozens or hundreds of times a day. If each modification
|
||||
necessitates waiting for `nixos-rebuild` to run for several seconds, it becomes a
|
||||
significant time drain.
|
||||
Taking my Neovim/Emacs configuration as an example, I make frequent modifications to them,
|
||||
sometimes dozens or even hundreds of times a day. Having to wait for `nixos-rebuild` to
|
||||
run for tens of seconds every time is a sheer waste of time.
|
||||
|
||||
Fortunately, with the solution outlined in
|
||||
[Simplifying NixOS Commands using Justfile](./simplify-nixos-related-commands.md), we can
|
||||
expedite testing and verification of frequently modified Dotfiles by adding specific
|
||||
configurations to the `Justfile`.
|
||||
Fortunately, Home Manager provides a [mkOutOfStoreSymlink][mkOutOfStoreSymlink] function,
|
||||
which can create a symlink pointing to the absolute path of your Dotfiles, thereby
|
||||
bypassing Home Manager itself and allowing your modifications to take effect immediately.
|
||||
|
||||
For instance, I've added the following content to my Justfile:
|
||||
This method is effective under the premise that your Dotfiles content is not generated by
|
||||
Nix. For instance, my Emacs/Neovim configurations are native and are only linked to the
|
||||
correct locations through Nix Home-Manager's `home.file` or `xdg.configFile`.
|
||||
|
||||
> The latest Justfile I'm using:
|
||||
> [ryan4yin/nix-config/Justfile](https://github.com/ryan4yin/nix-config/blob/main/Justfile)
|
||||
Below is a brief explanation of how to use this function to accelerate Dotfiles debugging.
|
||||
|
||||
```Makefile
|
||||
###############################################################
|
||||
# Quick Test - Neovim
|
||||
###############################################################
|
||||
Assuming you have placed your Neovim configuration under `~/nix-config/home/nvim`, add the
|
||||
following code to your Home Manager configuration (e.g., `~/nix-config/home/default.nix`):
|
||||
|
||||
|
||||
nvim-clean:
|
||||
rm -rf ${HOME}.config/astronvim/lua/user
|
||||
|
||||
nvim-test: nvim-clean
|
||||
rsync -avz --copy-links --chmod=D2755,F744 home/base/desktop/editors/neovim/astronvim_user/ ${HOME}/.config/astronvim/lua/user
|
||||
```nix
|
||||
{ config, pkgs, ... }: let
|
||||
# path to your nvim config directory
|
||||
nvimPath = "${config.home.homeDirectory}/nix-config/home/nvim";
|
||||
# path to your doom emacs config directory
|
||||
doomPath = "${config.home.homeDirectory}/nix-config/home/doom";
|
||||
in
|
||||
{
|
||||
xdg.configFile."nvim".source = config.lib.file.mkOutOfStoreSymlink nvimPath;
|
||||
xdg.configFile."doom".source = config.lib.file.mkOutOfStoreSymlink doomPath;
|
||||
# other configurations
|
||||
}
|
||||
```
|
||||
|
||||
Now, when I need to quickly test my Neovim configuration after making changes, I simply
|
||||
run `just nvim-test`. Once testing is complete, I execute `just nvim-clean`, followed by
|
||||
redeploying the configuration using `nixos-rebuild`. This allows for swift testing and
|
||||
seamless restoration of the configuration.
|
||||
After modifying the configuration, run `sudo nixos-rebuild switch` (or
|
||||
`home-manager switch` if you are using Home Manager standalone) to apply the changes. From
|
||||
then on, any modifications you make to `~/nix-config/home/nvim` or `~/nix-config/home/doom` will be
|
||||
immediately observed by Neovim/Emacs.
|
||||
|
||||
This method is effective under the condition that your Dotfiles content is not generated
|
||||
by Nix. For instance, my Emacs/Neovim configurations are native and are linked to the
|
||||
appropriate locations solely through Nix Home-Manager's `home.file` or `xdg.configFile`.
|
||||
This way, you can manage all your Dotfiles using a single nix-config repository, while
|
||||
frequently modified non-Nix configurations can take effect quickly, unaffected by Nix.
|
||||
|
||||
[mkOutOfStoreSymlink]:
|
||||
https://github.com/search?q=repo%3Anix-community%2Fhome-manager%20outOfStoreSymlink&type=code
|
||||
|
@ -8,32 +8,40 @@ Dotfiles,都需要通过跑一遍 `sudo nixos-rebuild switch`(或者如果你
|
||||
以我的 Neovim/Emacs 配置为例,我日常修改它们的频率非常高,有时候一天要改几十上百次,如果每
|
||||
次修改都要等 `nixos-rebuild` 跑个几十秒,这简直是在浪费时间。
|
||||
|
||||
幸运的是,在有了 [使用 Justfile 简化 NixOS 相关命令](./simplify-nixos-related-commands.md)
|
||||
这个方案后,我们可以通过往 `Justfile` 里添加些配置来实现快速的测试验证这些需要频繁修改的
|
||||
Dotfiles.
|
||||
幸运的是,Home Manager 提供了一个 [mkOutOfStoreSymlink][mkOutOfStoreSymlink] 函数, 它可以
|
||||
创建一个指向你 Dotfiles 绝对路径的软链接, 这样就能绕过 Home Manager 自身,你对 Dotfiles 的
|
||||
修改就能立即生效了.
|
||||
|
||||
比如我现在添加了这些 Justfile 内容:
|
||||
|
||||
> 我使用的 Justfile 最新版:
|
||||
> [ryan4yin/nix-config/Justfile](https://github.com/ryan4yin/nix-config/blob/main/Justfile)
|
||||
|
||||
```Makefile
|
||||
###############################################################
|
||||
# Quick Test - Neovim
|
||||
###############################################################
|
||||
|
||||
|
||||
nvim-clean:
|
||||
rm -rf ${HOME}.config/astronvim/lua/user
|
||||
|
||||
nvim-test: nvim-clean
|
||||
rsync -avz --copy-links --chmod=D2755,F744 home/base/desktop/editors/neovim/astronvim_user/ ${HOME}/.config/astronvim/lua/user
|
||||
```
|
||||
|
||||
然后在需要快速测试 Neovim 配置时,每次修改完配置后,跑一下 `just nvim-test`,我的配置就更
|
||||
新了。测试完毕后,运行下 `just nvim-clean`,再重新用 `nixos-rebuild` 部署下配置,就完成了
|
||||
配置的还原。
|
||||
|
||||
这种方法能生效的前提是,你的 Dotfiles 内容不是由 Nix 生成的,比如我的 Emacs/Neovim 配置都
|
||||
这种方法能有用的前提是,你的 Dotfiles 内容不是由 Nix 生成的,比如我的 Emacs/Neovim 配置都
|
||||
是原生的,仅通过 Nix Home-Manager 的 `home.file` 或 `xdg.configFile` 将它们链接到正确的位
|
||||
置。
|
||||
|
||||
下面简单说明下如何通过这个函数加速 Dotfiles 的调试.
|
||||
|
||||
假设你将你的 Neovim 配置放在了 `~/nix-config/home/nvim` 下,在你的 Home Manager 配置(如
|
||||
`/etc/nixos/home.nix`) 中添加如下代码:
|
||||
|
||||
```nix
|
||||
{ config, pkgs, ... }: let
|
||||
# path to your nvim config directory
|
||||
nvimPath = "${config.home.homeDirectory}/nix-config/home/nvim";
|
||||
# path to your doom emacs config directory
|
||||
doomPath = "${config.home.homeDirectory}/nix-config/home/doom";
|
||||
in
|
||||
{
|
||||
xdg.configFile."nvim".source = config.lib.file.mkOutOfStoreSymlink nvimPath;
|
||||
xdg.configFile."doom".source = config.lib.file.mkOutOfStoreSymlink doomPath;
|
||||
|
||||
# other configurations
|
||||
}
|
||||
```
|
||||
|
||||
修改完配置后,运行 `sudo nixos-rebuild switch` (或者如果你是单独使用 home manager的话,应
|
||||
该是这个指令 `home-manager switch`)即可生效。这之后,你对 `~/nix-config/home/nvim` 或
|
||||
`~/nix-config/home/doom` 的修改就能立即被 Neovim/Emacs 观察到了.
|
||||
|
||||
这样你就既能使用一个 nix-config 仓库统一管理所有 Dotfiles, 一些频繁修改的非 Nix 配置也能快
|
||||
速生效,不受 Nix 的影响.
|
||||
|
||||
[mkOutOfStoreSymlink]:
|
||||
https://github.com/search?q=repo%3Anix-community%2Fhome-manager%20outOfStoreSymlink&type=code
|
||||
|
Loading…
Reference in New Issue
Block a user