diff --git a/README.md b/README.md index 88d2f5f..5a611b9 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,29 @@ -# Diamond dependencies with the nixpkgs module system - -This flake declares 2 `nixosConfigurations`: - -- `.#nixosConfigurations.use-module-binding` -- `.#nixosConfigurations.use-module-filepath` - -They both include the `./vim-option.nix` module twice (simulating a "diamond -dependency"), but in different ways: +EDIT: I found which describes +this problem. -`use-module-filepath.nix`: -```nix -{ - imports = [ - ./vim-option.nix - ./vim-option.nix - ]; +# Diamond dependencies with the nixpkgs module system - ... -} -``` +This flake declares 3 `nixosConfigurations`. They all include a nixos module +module twice (simulating a "diamond dependency"), but in different ways: -`use-module-binding.nix`: -```nix -let vim-option = import ./vim-option.nix; -in -{ - imports = [ - vim-option - vim-option - ]; - - ... -} -``` +- `.#nixosConfigurations.use-module-filepath`: References the module by path twice. +- `.#nixosConfigurations.use-module-binding`: Imports the module, stashes it in a `let`, and then uses it twice. +- `.#nixosConfigurations.use-module-binding-with-key`: Like + `use-module-binding`, but uses a different module that declares a `key`. This "double import via a binding" feels like something that's more likely to happen as the nix flake ecosystem evolves: someday you're going to end up using module A and module B where module B itself uses module A. -Note that `.#nixosConfigurations.use-module-filepath` evaluates fine: +I don't know if the fix for this is for all modules to declare a human-chosen +key, or if this is some not-yet-robustly-solved problem. That might be what the +discussion over on + is +talking about. + +## Demo + +### `.#nixosConfigurations.use-module-filepath` works: ```shell $ nix eval .#nixosConfigurations.use-module-filepath.config.system.build.toplevel @@ -46,7 +31,7 @@ trace: declaring _includeVim «derivation /nix/store/dmmxnrhn6c6yibzlcdlwjb1i5akdcjsi-nixos-system-nixos-24.11.20240923.30439d9.drv» ``` -However, `.#nixosConfigurations.use-module-binding` fails to evaluate: +### `.#nixosConfigurations.use-module-binding` fails to evaluate: ```shell $ nix eval .#nixosConfigurations.use-module-binding.config.system.build.toplevel @@ -91,3 +76,11 @@ error: error: The option `_includeVim' in `/nix/store/vxrd24hjs9k56cjfhchyy57zs0n8kg84-source/use-module-binding.nix' is already declared in `/nix/store/vxrd24hjs9k56cjfhchyy57zs0n8kg84-source/use-module-binding.nix'. ``` + +### `.#nixosConfigurations.use-module-binding-with-key` works: + +```shell +$ nix eval .#nixosConfigurations.use-module-binding-with-key.config.system.build.toplevel +trace: declaring _includeVim +«derivation /nix/store/dmmxnrhn6c6yibzlcdlwjb1i5akdcjsi-nixos-system-nixos-24.11.20240923.30439d9.drv» +``` diff --git a/configs/use-module-binding-with-key.nix b/configs/use-module-binding-with-key.nix new file mode 100644 index 0000000..1d8391f --- /dev/null +++ b/configs/use-module-binding-with-key.nix @@ -0,0 +1,17 @@ +{ + lib, + pkgs, + config, + ... +}: +let + vim-option = import ../shared-module/vim-option-with-key.nix; +in +{ + imports = [ + vim-option + vim-option + ]; + + environment.systemPackages = lib.mkIf config._includeVim [ pkgs.vim ]; +} diff --git a/use-module-binding.nix b/configs/use-module-binding.nix similarity index 75% rename from use-module-binding.nix rename to configs/use-module-binding.nix index 054df89..68bf0f1 100644 --- a/use-module-binding.nix +++ b/configs/use-module-binding.nix @@ -5,7 +5,7 @@ ... }: let - vim-option = import ./vim-option.nix; + vim-option = import ../shared-module/vim-option.nix; in { imports = [ diff --git a/use-module-filepath.nix b/configs/use-module-filepath.nix similarity index 64% rename from use-module-filepath.nix rename to configs/use-module-filepath.nix index 2033d4a..92b66ef 100644 --- a/use-module-filepath.nix +++ b/configs/use-module-filepath.nix @@ -6,8 +6,8 @@ }: { imports = [ - ./vim-option.nix - ./vim-option.nix + ../shared-module/vim-option.nix + ../shared-module/vim-option.nix ]; environment.systemPackages = lib.mkIf config._includeVim [ pkgs.vim ]; diff --git a/flake.nix b/flake.nix index 79a91f8..74bbfef 100644 --- a/flake.nix +++ b/flake.nix @@ -8,18 +8,25 @@ outputs = { nixpkgs, ... }: { + nixosConfigurations.use-module-filepath = nixpkgs.lib.nixosSystem { + modules = [ + "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix" + ./base.nix + ./configs/use-module-filepath.nix + ]; + }; nixosConfigurations.use-module-binding = nixpkgs.lib.nixosSystem { modules = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix" ./base.nix - ./use-module-binding.nix + ./configs/use-module-binding.nix ]; }; - nixosConfigurations.use-module-filepath = nixpkgs.lib.nixosSystem { + nixosConfigurations.use-module-binding-with-key = nixpkgs.lib.nixosSystem { modules = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix" ./base.nix - ./use-module-filepath.nix + ./configs/use-module-binding-with-key.nix ]; }; }; diff --git a/shared-module/vim-option-with-key.nix b/shared-module/vim-option-with-key.nix new file mode 100644 index 0000000..989589d --- /dev/null +++ b/shared-module/vim-option-with-key.nix @@ -0,0 +1,14 @@ +{ lib, ... }: +{ + key = "is this a best practice, or is this a workaround? caught in a landslide, no escape from reality"; + options._includeVim = builtins.trace "declaring _includeVim" ( + lib.mkOption { + internal = true; + default = false; + } + ); + + # Interestingly, this does not reproduce the same issue: + # options._includeVim = builtins.trace "declaring _includeVim" (lib.mkOption {internal = true;}); + # config._includeVim = true; +} diff --git a/vim-option.nix b/shared-module/vim-option.nix similarity index 100% rename from vim-option.nix rename to shared-module/vim-option.nix