From 863408730925be94a8df8d7034ca0b33ae8efc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 2 Feb 2025 16:10:51 +0700 Subject: [PATCH] Do not deprecate directory argument --- lib/build-clan/default.nix | 27 ++++++++++------------ lib/build-clan/eval.nix | 8 +++++-- lib/build-clan/interface.nix | 12 +++++++++- lib/build-clan/module.nix | 19 ++++++++------- lib/build-clan/tests.nix | 28 ++++++++++++++++++++++- lib/inventory/build-inventory/default.nix | 4 ++-- lib/inventory/tests/default.nix | 16 ++++++------- 7 files changed, 75 insertions(+), 39 deletions(-) diff --git a/lib/build-clan/default.nix b/lib/build-clan/default.nix index c48fd33cb..60c4e494a 100644 --- a/lib/build-clan/default.nix +++ b/lib/build-clan/default.nix @@ -8,8 +8,9 @@ }: { ## Inputs - directory ? null, # The directory containing the machines subdirectory # allows to include machine-specific modules i.e. machines.${name} = { ... } - self ? null, + self ? null, # Reference to the current flake + directory ? null, # the directory containing the machines subdirectory. Optional: can be used if the machines is not in the root of the flake + # allows to include machine-specific modules i.e. machines.${name} = { ... } # A map from arch to pkgs, if specified this nixpkgs will be only imported once for each system. # This improves performance, but all nipxkgs.* options will be ignored. # deadnix: skip @@ -20,27 +21,23 @@ ... }@attrs: let - evalUnchecked = import ./eval.nix { + eval = import ./eval.nix { inherit lib nixpkgs clan-core ; inherit specialArgs; - self = if self != null then self else directory; + self = + if self == null then + lib.warn "The buildClan function requires argument 'self' to be set to the current flake" { + inputs = { }; + } + else + self; + directory = if directory == null then self else directory; }; - # Doing `self ? lib.trace "please use self" directory`, doesn't work - # as when both (directory and self) are set we get an infinite recursion error - eval = - if directory == null && self == null then - throw "The buildClan function requires argument 'self' to be set" - else if directory != null && self != null then - throw "Both 'self' and 'directory' are set, please remove 'directory' in favor of the 'self' argument" - else if directory != null then - lib.warn "The 'directory' argument in buildClan has been deprecated in favor of the 'self' argument" evalUnchecked - else - evalUnchecked; rest = builtins.removeAttrs attrs [ "specialArgs" ]; in eval { diff --git a/lib/build-clan/eval.nix b/lib/build-clan/eval.nix index 123d980f0..bb1add813 100644 --- a/lib/build-clan/eval.nix +++ b/lib/build-clan/eval.nix @@ -2,8 +2,9 @@ lib, nixpkgs, clan-core, - specialArgs ? { }, self, + directory ? null, + specialArgs ? { }, }: # Returns a function that takes self, which should point to the directory of the flake module: @@ -14,6 +15,9 @@ module: modules = [ ./interface.nix module - { inherit specialArgs; } + { + inherit specialArgs; + directory = lib.mkIf (directory != null) directory; + } ]; }).config diff --git a/lib/build-clan/interface.nix b/lib/build-clan/interface.nix index ddbb946c0..eff0d0627 100644 --- a/lib/build-clan/interface.nix +++ b/lib/build-clan/interface.nix @@ -9,8 +9,18 @@ in { options = { self = lib.mkOption { - type = types.path; + type = types.raw; default = self; + readOnly = true; + defaultText = "Reference to the current flake"; + description = '' + This is used to import external clan modules. + ''; + }; + + directory = lib.mkOption { + type = types.path; + default = builtins.toString self; defaultText = "Root directory of the flake"; description = '' The directory containing the clan. diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index f48282639..e8a5be5fa 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -7,7 +7,7 @@ }: let inherit (config) - self + directory machines pkgsForSystem specialArgs @@ -44,7 +44,7 @@ let serviceConfigs = ( buildInventory { inherit inventory; - inherit self; + inherit directory; } ); @@ -59,14 +59,14 @@ let nixpkgs.lib.nixosSystem { modules = let - hwConfig = "${self}/machines/${name}/hardware-configuration.nix"; - diskoConfig = "${self}/machines/${name}/disko.nix"; + hwConfig = "${directory}/machines/${name}/hardware-configuration.nix"; + diskoConfig = "${directory}/machines/${name}/disko.nix"; in [ { # Autoinclude configuration.nix and hardware-configuration.nix imports = builtins.filter builtins.pathExists [ - "${self}/machines/${name}/configuration.nix" + "${directory}/machines/${name}/configuration.nix" hwConfig diskoConfig ]; @@ -81,7 +81,7 @@ let { # Settings clan.core.settings = { - directory = self; + inherit directory; inherit (config.inventory.meta) name icon; machine = { @@ -160,7 +160,7 @@ let ) supportedSystems ); - inventoryFile = "${self}/inventory.json"; + inventoryFile = "${directory}/inventory.json"; inventoryLoaded = if builtins.pathExists inventoryFile then @@ -172,7 +172,6 @@ in { imports = [ ./auto-imports.nix - (lib.mkRenamedOptionModule [ "directory" ] [ "self" ]) # Merge the inventory file { inventory = _: { @@ -182,9 +181,9 @@ in } # TODO: Figure out why this causes infinite recursion { - inventory.machines = lib.optionalAttrs (builtins.pathExists "${self}/machines") ( + inventory.machines = lib.optionalAttrs (builtins.pathExists "${directory}/machines") ( builtins.mapAttrs (_n: _v: { }) ( - (lib.filterAttrs (_: t: t == "directory") (builtins.readDir "${self}/machines")) + lib.filterAttrs (_: t: t == "directory") (builtins.readDir "${directory}/machines") ) ); } diff --git a/lib/build-clan/tests.nix b/lib/build-clan/tests.nix index b66f44248..1b995e07d 100644 --- a/lib/build-clan/tests.nix +++ b/lib/build-clan/tests.nix @@ -16,6 +16,10 @@ in test_only_required = let config = evalClan { + self = { + inputs = { }; + }; + directory = ./.; meta.name = "test"; imports = [ ./module.nix ]; }; @@ -28,7 +32,10 @@ in test_all_simple = let config = evalClan { - self = ./.; + self = { + inputs = { }; + }; + directory = ./.; machines = { }; inventory = { meta.name = "test"; @@ -43,6 +50,10 @@ in test_outputs_clanInternals = let config = evalClan { + self = { + inputs = { }; + }; + directory = ./.; imports = [ # What the user needs to specif { @@ -68,6 +79,9 @@ in test_fn_simple = let result = buildClan { + self = { + inputs = { }; + }; directory = ./.; meta.name = "test"; }; @@ -84,6 +98,9 @@ in test_fn_extensiv_meta = let result = buildClan { + self = { + inputs = { }; + }; directory = ./.; meta.name = "test"; meta.description = "test"; @@ -104,6 +121,9 @@ in test_fn_clan_core = let result = buildClan { + self = { + inputs = { }; + }; directory = ../../.; meta.name = "test-clan-core"; }; @@ -119,6 +139,9 @@ in test_buildClan_all_machines = let result = buildClan { + self = { + inputs = { }; + }; directory = ./.; meta.name = "test"; inventory.machines.machine1.meta.name = "machine1"; @@ -138,6 +161,9 @@ in test_buildClan_specialArgs = let result = buildClan { + self = { + inputs = { }; + }; directory = ./.; meta.name = "test"; specialArgs.foo = "dream2nix"; diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index 47133499d..63f5e7678 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -197,7 +197,7 @@ let machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration } */ buildInventory = - { inventory, self }: + { inventory, directory }: # For every machine in the inventory, build a NixOS configuration # For each machine generate config, forEach service, if the machine is used. builtins.mapAttrs ( @@ -207,8 +207,8 @@ let machineName machineConfig inventory + directory ; - directory = self; } ) (inventory.machines or { }); in diff --git a/lib/inventory/tests/default.nix b/lib/inventory/tests/default.nix index 7cc407188..182069e4a 100644 --- a/lib/inventory/tests/default.nix +++ b/lib/inventory/tests/default.nix @@ -13,14 +13,14 @@ in # Empty inventory should return an empty module expr = buildInventory { inventory = { }; - self = ./.; + directory = ./.; }; expected = { }; }; test_inventory_role_imports = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -62,7 +62,7 @@ in test_inventory_tag_resolve = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -102,7 +102,7 @@ in test_inventory_multiple_roles = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -132,7 +132,7 @@ in test_inventory_module_doesnt_exist = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -157,7 +157,7 @@ in test_inventory_role_doesnt_exist = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -183,7 +183,7 @@ in test_inventory_tag_doesnt_exist = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = { @@ -211,7 +211,7 @@ in test_inventory_disabled_service = let configs = buildInventory { - self = ./.; + directory = ./.; inventory = { modules = clan-core.clanModules; services = {