Do not deprecate directory argument

This commit is contained in:
Jörg Thalheim
2025-02-02 16:10:51 +07:00
committed by clan-bot
parent fe6cca3c47
commit 8634087309
7 changed files with 75 additions and 39 deletions

View File

@@ -8,8 +8,9 @@
}: }:
{ {
## Inputs ## Inputs
directory ? null, # The directory containing the machines subdirectory # allows to include machine-specific modules i.e. machines.${name} = { ... } self ? null, # Reference to the current flake
self ? null, 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. # 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. # This improves performance, but all nipxkgs.* options will be ignored.
# deadnix: skip # deadnix: skip
@@ -20,27 +21,23 @@
... ...
}@attrs: }@attrs:
let let
evalUnchecked = import ./eval.nix { eval = import ./eval.nix {
inherit inherit
lib lib
nixpkgs nixpkgs
clan-core clan-core
; ;
inherit specialArgs; 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" ]; rest = builtins.removeAttrs attrs [ "specialArgs" ];
in in
eval { eval {

View File

@@ -2,8 +2,9 @@
lib, lib,
nixpkgs, nixpkgs,
clan-core, clan-core,
specialArgs ? { },
self, self,
directory ? null,
specialArgs ? { },
}: }:
# Returns a function that takes self, which should point to the directory of the flake # Returns a function that takes self, which should point to the directory of the flake
module: module:
@@ -14,6 +15,9 @@ module:
modules = [ modules = [
./interface.nix ./interface.nix
module module
{ inherit specialArgs; } {
inherit specialArgs;
directory = lib.mkIf (directory != null) directory;
}
]; ];
}).config }).config

View File

@@ -9,8 +9,18 @@ in
{ {
options = { options = {
self = lib.mkOption { self = lib.mkOption {
type = types.path; type = types.raw;
default = self; 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"; defaultText = "Root directory of the flake";
description = '' description = ''
The directory containing the clan. The directory containing the clan.

View File

@@ -7,7 +7,7 @@
}: }:
let let
inherit (config) inherit (config)
self directory
machines machines
pkgsForSystem pkgsForSystem
specialArgs specialArgs
@@ -44,7 +44,7 @@ let
serviceConfigs = ( serviceConfigs = (
buildInventory { buildInventory {
inherit inventory; inherit inventory;
inherit self; inherit directory;
} }
); );
@@ -59,14 +59,14 @@ let
nixpkgs.lib.nixosSystem { nixpkgs.lib.nixosSystem {
modules = modules =
let let
hwConfig = "${self}/machines/${name}/hardware-configuration.nix"; hwConfig = "${directory}/machines/${name}/hardware-configuration.nix";
diskoConfig = "${self}/machines/${name}/disko.nix"; diskoConfig = "${directory}/machines/${name}/disko.nix";
in in
[ [
{ {
# Autoinclude configuration.nix and hardware-configuration.nix # Autoinclude configuration.nix and hardware-configuration.nix
imports = builtins.filter builtins.pathExists [ imports = builtins.filter builtins.pathExists [
"${self}/machines/${name}/configuration.nix" "${directory}/machines/${name}/configuration.nix"
hwConfig hwConfig
diskoConfig diskoConfig
]; ];
@@ -81,7 +81,7 @@ let
{ {
# Settings # Settings
clan.core.settings = { clan.core.settings = {
directory = self; inherit directory;
inherit (config.inventory.meta) name icon; inherit (config.inventory.meta) name icon;
machine = { machine = {
@@ -160,7 +160,7 @@ let
) supportedSystems ) supportedSystems
); );
inventoryFile = "${self}/inventory.json"; inventoryFile = "${directory}/inventory.json";
inventoryLoaded = inventoryLoaded =
if builtins.pathExists inventoryFile then if builtins.pathExists inventoryFile then
@@ -172,7 +172,6 @@ in
{ {
imports = [ imports = [
./auto-imports.nix ./auto-imports.nix
(lib.mkRenamedOptionModule [ "directory" ] [ "self" ])
# Merge the inventory file # Merge the inventory file
{ {
inventory = _: { inventory = _: {
@@ -182,9 +181,9 @@ in
} }
# TODO: Figure out why this causes infinite recursion # 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: { }) ( builtins.mapAttrs (_n: _v: { }) (
(lib.filterAttrs (_: t: t == "directory") (builtins.readDir "${self}/machines")) lib.filterAttrs (_: t: t == "directory") (builtins.readDir "${directory}/machines")
) )
); );
} }

View File

@@ -16,6 +16,10 @@ in
test_only_required = test_only_required =
let let
config = evalClan { config = evalClan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test"; meta.name = "test";
imports = [ ./module.nix ]; imports = [ ./module.nix ];
}; };
@@ -28,7 +32,10 @@ in
test_all_simple = test_all_simple =
let let
config = evalClan { config = evalClan {
self = ./.; self = {
inputs = { };
};
directory = ./.;
machines = { }; machines = { };
inventory = { inventory = {
meta.name = "test"; meta.name = "test";
@@ -43,6 +50,10 @@ in
test_outputs_clanInternals = test_outputs_clanInternals =
let let
config = evalClan { config = evalClan {
self = {
inputs = { };
};
directory = ./.;
imports = [ imports = [
# What the user needs to specif # What the user needs to specif
{ {
@@ -68,6 +79,9 @@ in
test_fn_simple = test_fn_simple =
let let
result = buildClan { result = buildClan {
self = {
inputs = { };
};
directory = ./.; directory = ./.;
meta.name = "test"; meta.name = "test";
}; };
@@ -84,6 +98,9 @@ in
test_fn_extensiv_meta = test_fn_extensiv_meta =
let let
result = buildClan { result = buildClan {
self = {
inputs = { };
};
directory = ./.; directory = ./.;
meta.name = "test"; meta.name = "test";
meta.description = "test"; meta.description = "test";
@@ -104,6 +121,9 @@ in
test_fn_clan_core = test_fn_clan_core =
let let
result = buildClan { result = buildClan {
self = {
inputs = { };
};
directory = ../../.; directory = ../../.;
meta.name = "test-clan-core"; meta.name = "test-clan-core";
}; };
@@ -119,6 +139,9 @@ in
test_buildClan_all_machines = test_buildClan_all_machines =
let let
result = buildClan { result = buildClan {
self = {
inputs = { };
};
directory = ./.; directory = ./.;
meta.name = "test"; meta.name = "test";
inventory.machines.machine1.meta.name = "machine1"; inventory.machines.machine1.meta.name = "machine1";
@@ -138,6 +161,9 @@ in
test_buildClan_specialArgs = test_buildClan_specialArgs =
let let
result = buildClan { result = buildClan {
self = {
inputs = { };
};
directory = ./.; directory = ./.;
meta.name = "test"; meta.name = "test";
specialArgs.foo = "dream2nix"; specialArgs.foo = "dream2nix";

View File

@@ -197,7 +197,7 @@ let
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration } machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
*/ */
buildInventory = buildInventory =
{ inventory, self }: { inventory, directory }:
# For every machine in the inventory, build a NixOS configuration # For every machine in the inventory, build a NixOS configuration
# For each machine generate config, forEach service, if the machine is used. # For each machine generate config, forEach service, if the machine is used.
builtins.mapAttrs ( builtins.mapAttrs (
@@ -207,8 +207,8 @@ let
machineName machineName
machineConfig machineConfig
inventory inventory
directory
; ;
directory = self;
} }
) (inventory.machines or { }); ) (inventory.machines or { });
in in

View File

@@ -13,14 +13,14 @@ in
# Empty inventory should return an empty module # Empty inventory should return an empty module
expr = buildInventory { expr = buildInventory {
inventory = { }; inventory = { };
self = ./.; directory = ./.;
}; };
expected = { }; expected = { };
}; };
test_inventory_role_imports = test_inventory_role_imports =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -62,7 +62,7 @@ in
test_inventory_tag_resolve = test_inventory_tag_resolve =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -102,7 +102,7 @@ in
test_inventory_multiple_roles = test_inventory_multiple_roles =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -132,7 +132,7 @@ in
test_inventory_module_doesnt_exist = test_inventory_module_doesnt_exist =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -157,7 +157,7 @@ in
test_inventory_role_doesnt_exist = test_inventory_role_doesnt_exist =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -183,7 +183,7 @@ in
test_inventory_tag_doesnt_exist = test_inventory_tag_doesnt_exist =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {
@@ -211,7 +211,7 @@ in
test_inventory_disabled_service = test_inventory_disabled_service =
let let
configs = buildInventory { configs = buildInventory {
self = ./.; directory = ./.;
inventory = { inventory = {
modules = clan-core.clanModules; modules = clan-core.clanModules;
services = { services = {