✨

nix flake check で dotfiles をテストする(Home Manager)

に公開

想定読者

  • すでにhome-managerでdotfiles構築をしている
  • flakeがなにか大体理解している

まえがき

以前、私はNix home-managerをflake環境で使っていながら、テストもせずnix flake updateをGithub Action上で実行していました。
そのため、ローカルリポジトリで更新をするときにエラーで落ちてしまうことがありました。
https://github.com/s0racat/dotfiles-nix/blob/559ccaff4fae92071de90893515cfabcc2a85904/.github/workflows/updater.yml#L41-L52

flake.nixにHome Manager 設定のテストを追加する

https://github.com/s0racat/dotfiles-nix/blob/61e39e11274557e6ae46db369f2909be9b3459a4/flake.nix#L37-L51
https://github.com/s0racat/dotfiles-nix/blob/61e39e11274557e6ae46db369f2909be9b3459a4/flake.nix#L57-L66

Github Actionでテストをする

https://github.com/s0racat/dotfiles-nix/blob/61e39e11274557e6ae46db369f2909be9b3459a4/.github/workflows/updater.yml

あとがき

テストを追加することで、home-managerに破壊的変更がされた, 設定のメンテナンスをサボっていて古いなどの理由でローカルでのhome-managerが落ちることを防止することができます。
例:
https://github.com/s0racat/dotfiles-nix/actions/runs/21363300804/job/61488115248

warning: Git tree '/home/takumi/dotfiles-nix' is dirty
evaluating flake...
checking flake output 'overlays'...
checking overlay 'overlays.default'...
checking flake output 'checks'...
checking derivation checks.x86_64-linux."hm_takumi@sub-laptop"...
evaluation warning: 'system' has been renamed to/replaced by 'stdenv.hostPlatform.system'
derivation evaluated to /nix/store/r7h05xa4g9m6sgnzr59r3h4y3p2dvgji-home-manager-generation.drv
checking derivation checks.x86_64-linux."hm_takumi@takumi-Venus-series"...
evaluation warning: 'system' has been renamed to/replaced by 'stdenv.hostPlatform.system'
derivation evaluated to /nix/store/q427ki517k5fixvnn2n4a0wbp3afikfz-home-manager-generation.drv
checking flake output 'homeConfigurations'...
checking flake output 'apps'...
checking app 'apps.x86_64-linux.alejandra'...
checking flake output 'formatter'...
checking derivation formatter.x86_64-linux...
derivation evaluated to /nix/store/pqzap3fr5y3pl099r3y7rzblky17sj3c-treefmt.drv
checking flake output 'nixosConfigurations'...
checking NixOS configuration 'nixosConfigurations.um690pro'...
evaluation warning: 'system' has been renamed to/replaced by 'stdenv.hostPlatform.system'
running 4 flake checks...
building '/nix/store/3z4farxn09g9v03cx8ixa20n8w21sjr5-home-manager-files.drv'...
building '/nix/store/mcv4vxp7wb7wb5z7clf6pca24l2g3jcx-home-manager-files.drv'...
building '/nix/store/r7h05xa4g9m6sgnzr59r3h4y3p2dvgji-home-manager-generation.drv'...
building '/nix/store/q427ki517k5fixvnn2n4a0wbp3afikfz-home-manager-generation.drv'...
all checks passed!
warning: The check omitted these incompatible systems: aarch64-darwin, aarch64-linux, x86_64-darwin
Use '--all-systems' to check all.

evaluation warningがhome-managerによって出されたエラーになります。

ちなみに、nix flake check --option abort-on-warn trueとすることで警告で停止させることができます。

良いNixライフを!

Discussion