2024-03-17 18:48:49 +00:00
|
|
|
{
|
|
|
|
clan-core,
|
|
|
|
nixpkgs,
|
|
|
|
lib,
|
|
|
|
}:
|
|
|
|
{
|
|
|
|
directory, # The directory containing the machines subdirectory
|
|
|
|
specialArgs ? { }, # Extra arguments to pass to nixosSystem i.e. useful to make self available
|
|
|
|
machines ? { }, # allows to include machine-specific modules i.e. machines.${name} = { ... }
|
2024-05-31 15:22:38 +00:00
|
|
|
# DEPRECATED: use meta.name instead
|
|
|
|
clanName ? null, # Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to.
|
|
|
|
# DEPRECATED: use meta.icon instead
|
2024-03-17 18:48:49 +00:00
|
|
|
clanIcon ? null, # A path to an icon to be used for the clan, should be the same for all machines
|
2024-05-31 15:22:38 +00:00
|
|
|
meta ? { }, # A set containing clan meta: name :: string, icon :: string, description :: string
|
2024-06-21 20:46:12 +00:00
|
|
|
# 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.
|
|
|
|
pkgsForSystem ? (_system: null),
|
|
|
|
/*
|
|
|
|
Low level inventory configuration.
|
|
|
|
Overrides the services configuration.
|
|
|
|
*/
|
|
|
|
inventory ? { },
|
2023-08-29 13:00:03 +00:00
|
|
|
}:
|
|
|
|
let
|
2024-06-22 19:31:01 +00:00
|
|
|
# Internal inventory, this is the result of merging all potential inventory sources:
|
|
|
|
# - Default instances configured via 'services'
|
|
|
|
# - The inventory overrides
|
|
|
|
# - Machines that exist in inventory.machines
|
|
|
|
# - Machines explicitly configured via 'machines' argument
|
|
|
|
# - Machines that exist in the machines directory
|
|
|
|
# Checks on the module level:
|
|
|
|
# - Each service role must reference a valid machine after all machines are merged
|
|
|
|
mergedInventory =
|
|
|
|
(lib.evalModules {
|
|
|
|
modules = [
|
2024-06-24 19:28:45 +00:00
|
|
|
clan-core.lib.inventory.interface
|
2024-06-22 19:31:01 +00:00
|
|
|
{ inherit meta; }
|
2024-06-24 13:14:48 +00:00
|
|
|
(
|
|
|
|
if
|
|
|
|
builtins.pathExists "${directory}/inventory.json"
|
|
|
|
# Is recursively applied. Any explicit nix will override.
|
|
|
|
then
|
|
|
|
lib.mkDefault (builtins.fromJSON (builtins.readFile "${directory}/inventory.json"))
|
|
|
|
else
|
|
|
|
{ }
|
|
|
|
)
|
2024-06-21 20:46:12 +00:00
|
|
|
inventory
|
2024-06-22 19:31:01 +00:00
|
|
|
# Machines explicitly configured via 'machines' argument
|
|
|
|
{
|
|
|
|
# { ${name} :: meta // { name, tags } }
|
|
|
|
machines = lib.mapAttrs (
|
|
|
|
name: config:
|
|
|
|
(lib.attrByPath [
|
2024-06-21 20:46:12 +00:00
|
|
|
"clan"
|
|
|
|
"meta"
|
2024-06-22 19:31:01 +00:00
|
|
|
] { } config)
|
|
|
|
// {
|
|
|
|
# meta.name default is the attribute name of the machine
|
|
|
|
name = lib.mkDefault (
|
|
|
|
lib.attrByPath [
|
|
|
|
"clan"
|
|
|
|
"meta"
|
|
|
|
"name"
|
|
|
|
] name config
|
|
|
|
);
|
|
|
|
tags = lib.attrByPath [
|
|
|
|
"clan"
|
|
|
|
"tags"
|
|
|
|
] [ ] config;
|
|
|
|
}
|
|
|
|
) machines;
|
2024-06-21 20:46:12 +00:00
|
|
|
}
|
2024-06-25 10:12:09 +00:00
|
|
|
# Will be deprecated
|
2024-06-25 10:51:03 +00:00
|
|
|
{ machines = lib.mapAttrs (_n: _: lib.mkDefault { }) machinesDirs; }
|
2024-06-24 13:14:48 +00:00
|
|
|
|
|
|
|
# Deprecated interface
|
|
|
|
(if clanName != null then { meta.name = clanName; } else { })
|
|
|
|
(if clanIcon != null then { meta.icon = clanIcon; } else { })
|
2024-06-22 19:31:01 +00:00
|
|
|
];
|
|
|
|
}).config;
|
2024-06-21 20:46:12 +00:00
|
|
|
|
2024-06-24 19:28:45 +00:00
|
|
|
inherit (clan-core.lib.inventory) buildInventory;
|
2024-06-21 20:46:12 +00:00
|
|
|
|
2024-06-24 13:14:48 +00:00
|
|
|
# map from machine name to service configuration
|
|
|
|
# { ${machineName} :: Config }
|
2024-06-22 19:31:01 +00:00
|
|
|
serviceConfigs = buildInventory mergedInventory;
|
2024-06-21 20:46:12 +00:00
|
|
|
|
2024-06-25 10:35:21 +00:00
|
|
|
machinesDirs = lib.optionalAttrs (builtins.pathExists "${directory}/machines") (
|
|
|
|
builtins.readDir (directory + /machines)
|
|
|
|
);
|
2024-06-25 10:12:09 +00:00
|
|
|
|
|
|
|
machineSettings =
|
|
|
|
machineName:
|
|
|
|
# CLAN_MACHINE_SETTINGS_FILE allows to override the settings file temporarily
|
|
|
|
# This is useful for doing a dry-run before writing changes into the settings.json
|
|
|
|
# Using CLAN_MACHINE_SETTINGS_FILE requires passing --impure to nix eval
|
|
|
|
if builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE" != "" then
|
|
|
|
builtins.fromJSON (builtins.readFile (builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE"))
|
|
|
|
else
|
|
|
|
lib.optionalAttrs (builtins.pathExists "${directory}/machines/${machineName}/settings.json") (
|
|
|
|
builtins.fromJSON (builtins.readFile (directory + /machines/${machineName}/settings.json))
|
|
|
|
);
|
|
|
|
|
|
|
|
machineImports =
|
|
|
|
machineSettings: map (module: clan-core.clanModules.${module}) (machineSettings.clanImports or [ ]);
|
|
|
|
|
2024-05-31 15:22:38 +00:00
|
|
|
deprecationWarnings = [
|
|
|
|
(lib.warnIf (
|
|
|
|
clanName != null
|
|
|
|
) "clanName is deprecated, please use meta.name instead. ${clanName}" null)
|
|
|
|
(lib.warnIf (clanIcon != null) "clanIcon is deprecated, please use meta.icon instead" null)
|
|
|
|
];
|
|
|
|
|
2023-09-28 12:13:23 +00:00
|
|
|
# TODO: remove default system once we have a hardware-config mechanism
|
2024-03-17 18:48:49 +00:00
|
|
|
nixosConfiguration =
|
|
|
|
{
|
|
|
|
system ? "x86_64-linux",
|
|
|
|
name,
|
|
|
|
pkgs ? null,
|
|
|
|
extraConfig ? { },
|
|
|
|
}:
|
|
|
|
nixpkgs.lib.nixosSystem {
|
2024-06-25 10:12:09 +00:00
|
|
|
modules =
|
|
|
|
let
|
|
|
|
settings = machineSettings name;
|
|
|
|
in
|
|
|
|
(machineImports settings)
|
|
|
|
++ [
|
2024-06-25 11:35:38 +00:00
|
|
|
settings
|
2024-06-25 10:12:09 +00:00
|
|
|
clan-core.nixosModules.clanCore
|
|
|
|
extraConfig
|
|
|
|
(machines.${name} or { })
|
|
|
|
# Inherit the inventory assertions ?
|
|
|
|
{ inherit (mergedInventory) assertions; }
|
|
|
|
{ imports = serviceConfigs.${name} or { }; }
|
|
|
|
(
|
|
|
|
{
|
|
|
|
# Settings
|
|
|
|
clan.core.clanDir = directory;
|
|
|
|
# Inherited from clan wide settings
|
|
|
|
clan.core.clanName = meta.name or clanName;
|
|
|
|
clan.core.clanIcon = meta.icon or clanIcon;
|
2024-06-24 13:14:48 +00:00
|
|
|
|
2024-06-25 10:12:09 +00:00
|
|
|
# Machine specific settings
|
|
|
|
clan.core.machineName = name;
|
|
|
|
networking.hostName = lib.mkDefault name;
|
|
|
|
nixpkgs.hostPlatform = lib.mkDefault system;
|
2024-06-24 13:14:48 +00:00
|
|
|
|
2024-06-25 10:12:09 +00:00
|
|
|
# speeds up nix commands by using the nixpkgs from the host system (especially useful in VMs)
|
|
|
|
nix.registry.nixpkgs.to = {
|
|
|
|
type = "path";
|
|
|
|
path = lib.mkDefault nixpkgs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (pkgs != null) { nixpkgs.pkgs = lib.mkForce pkgs; }
|
|
|
|
)
|
|
|
|
];
|
2024-04-02 09:03:45 +00:00
|
|
|
specialArgs = {
|
|
|
|
inherit clan-core;
|
|
|
|
} // specialArgs;
|
2024-03-17 18:48:49 +00:00
|
|
|
};
|
2023-09-20 16:08:47 +00:00
|
|
|
|
2024-06-24 13:14:48 +00:00
|
|
|
allMachines = mergedInventory.machines or { };
|
2023-09-20 16:08:47 +00:00
|
|
|
|
2023-09-27 15:25:17 +00:00
|
|
|
supportedSystems = [
|
2023-09-20 16:08:47 +00:00
|
|
|
"x86_64-linux"
|
|
|
|
"aarch64-linux"
|
|
|
|
"riscv64-linux"
|
|
|
|
"x86_64-darwin"
|
|
|
|
"aarch64-darwin"
|
|
|
|
];
|
|
|
|
|
2023-09-27 15:25:17 +00:00
|
|
|
nixosConfigurations = lib.mapAttrs (name: _: nixosConfiguration { inherit name; }) allMachines;
|
|
|
|
|
|
|
|
# This instantiates nixos for each system that we support:
|
2023-09-27 15:25:17 +00:00
|
|
|
# configPerSystem = <system>.<machine>.nixosConfiguration
|
2023-09-27 15:25:17 +00:00
|
|
|
# We need this to build nixos secret generators for each system
|
2024-03-17 18:48:49 +00:00
|
|
|
configsPerSystem = builtins.listToAttrs (
|
|
|
|
builtins.map (
|
|
|
|
system:
|
|
|
|
lib.nameValuePair system (
|
|
|
|
lib.mapAttrs (
|
|
|
|
name: _:
|
|
|
|
nixosConfiguration {
|
2024-02-21 09:44:00 +00:00
|
|
|
inherit name system;
|
|
|
|
pkgs = pkgsForSystem system;
|
2024-03-17 18:48:49 +00:00
|
|
|
}
|
|
|
|
) allMachines
|
|
|
|
)
|
|
|
|
) supportedSystems
|
|
|
|
);
|
2024-02-10 10:47:09 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
configsFuncPerSystem = builtins.listToAttrs (
|
|
|
|
builtins.map (
|
|
|
|
system:
|
|
|
|
lib.nameValuePair system (
|
|
|
|
lib.mapAttrs (
|
|
|
|
name: _: args:
|
|
|
|
nixosConfiguration (
|
|
|
|
args
|
|
|
|
// {
|
|
|
|
inherit name system;
|
|
|
|
pkgs = pkgsForSystem system;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
) allMachines
|
|
|
|
)
|
|
|
|
) supportedSystems
|
|
|
|
);
|
2023-09-27 15:25:17 +00:00
|
|
|
in
|
2024-05-31 15:22:38 +00:00
|
|
|
builtins.deepSeq deprecationWarnings {
|
2023-09-27 15:25:17 +00:00
|
|
|
inherit nixosConfigurations;
|
|
|
|
|
2023-09-20 16:08:47 +00:00
|
|
|
clanInternals = {
|
2024-06-25 19:17:01 +00:00
|
|
|
inherit (clan-core) clanModules;
|
|
|
|
source = "${clan-core}";
|
|
|
|
|
2024-06-24 13:14:48 +00:00
|
|
|
meta = mergedInventory.meta;
|
2024-06-22 19:31:01 +00:00
|
|
|
inventory = mergedInventory;
|
2024-05-31 15:22:38 +00:00
|
|
|
|
2024-06-24 19:42:55 +00:00
|
|
|
inventoryFile = "${directory}/inventory.json";
|
2024-06-24 13:14:48 +00:00
|
|
|
|
2024-05-31 15:22:38 +00:00
|
|
|
# machine specifics
|
2023-09-29 09:56:02 +00:00
|
|
|
machines = configsPerSystem;
|
2024-02-10 10:47:09 +00:00
|
|
|
machinesFunc = configsFuncPerSystem;
|
2024-03-17 18:48:49 +00:00
|
|
|
all-machines-json = lib.mapAttrs (
|
|
|
|
system: configs:
|
|
|
|
nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (
|
|
|
|
lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs
|
|
|
|
)
|
|
|
|
) configsPerSystem;
|
2023-09-20 16:08:47 +00:00
|
|
|
};
|
2023-09-27 15:25:17 +00:00
|
|
|
}
|