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
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 {

View File

@@ -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

View File

@@ -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.

View File

@@ -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")
)
);
}

View File

@@ -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";

View File

@@ -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

View File

@@ -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 = {